浏览代码

CSV Reader added , Evaluator added, dummy files added

Maksim Melnik 6 年之前
父节点
当前提交
825e64715e

+ 67 - 0
CSVReader.py

@@ -0,0 +1,67 @@
+import csv
+from CertainTrust import Opinion
+
+
+class CSVReader:
+    @staticmethod
+    def readCSV(inputfile, f, months):
+        '''
+        Converts input csv file into a dictionary of opinions,
+        The CSV input file should contain the following structure:
+        packageName:prediction:errorComplement
+        :param inputfile: relative path to input csv file
+        :param f: initial expectation value
+        :param months: number of months
+        :return: dictionary of package-names as key and opinions as value
+        '''
+        result = {}
+        with open(inputfile, newline="") as csvfile:
+            reader = csv.reader(csvfile, delimiter=':', quotechar='|')
+            for row in reader:
+                if not len(row) == 0:
+                    package = row [0]
+                    prediction = row [1]
+                    errorCompl = float(row [2])
+                    # FIXME: HARDCODED MONTHS
+                    resT = 1 - int(prediction) / (30 * months)
+                    result[package]=Opinion(resT , errorCompl, f)
+        return result
+
+    @staticmethod
+    def readCSVModel(months):
+        '''
+        Reads a model from CSV file into python dictionary
+        :param months: number of months
+        :return: dictionary of package-names as key and opinions as value
+        '''
+        result = {}
+        with open("models/dummy_model_"+str(months)+".csv", newline="") as csvfile:
+            reader = csv.reader(csvfile, delimiter=':', quotechar='|')
+            for row in reader:
+                if not len(row) == 0:
+                    result[row[0]]=Opinion(float(row[1]),float(row[2]),float(row[3]))
+        return result
+
+    @staticmethod
+    def readCSVPackages(filename):
+        '''
+        reads package names from csv file
+        :param filename:
+        :return: dictionary of package names
+        '''
+        result = []
+        with open(filename, newline="") as csvfile:
+            reader = csv.reader(csvfile, delimiter=':', quotechar='|')
+            for row in reader:
+                if not len(row) == 0:
+                    result.append(row[0])
+        return result
+
+
+'''
+res =CSVReader.readCSV("inputs/dummy_input.csv", 1)
+for key in res:
+    print(key + ":" + str(res[key].t)+ ":" + str(res[key].c)+ ":" + str(res[key].f))
+
+CSVReader.readCSVPackages("inputs/dummy_input_package.csv")
+'''

+ 15 - 13
CertainTrust.py

@@ -3,8 +3,8 @@ import numpy as np
 class Opinion:
     """
     This class represents opinion.
-    c - certainty
     t - average rating
+    c - certainty
     f - initial expectation value
     doc - degree of conflict
     """
@@ -18,11 +18,9 @@ class Opinion:
     def __str__(self):
         return "Opinion{t: "+str(self.t)+", c: "+str(self.c)+", f: "+str(self.f)+"}"
 
-    @staticmethod
-    def _adjust_value(val):
-        return max(min(val, 1), 0)
+    def expectation_value(self):
+        return self.c * self.t + ( 1 - self.c ) * self.f
 
-    @staticmethod
     def _single_or(o_A, o_B):
         """
         Computes OR function of two Opinion objects. Result is returned as a new object,
@@ -124,13 +122,13 @@ class Opinion:
         return
 
     @staticmethod
-    def _internal_fusion(args, weights, doc):
+    def _internal_fusion(args, weights, doc=1.0):
         """
         An internal implementation of fusion function.
         Is called by _weighted_fusion and cFusion
-        :param args:
-        :param weights:
-        :param doc:
+        :param args: list of opinions
+        :param weights: list of weights
+        :param doc: degree of conflict (float)
         :return:
         """
         allOne = True;
@@ -162,7 +160,7 @@ class Opinion:
                 r_t = 0
             else:
                 for i in range(0,arrLength):
-                    numeratorT += weights[i] * args[i].getT()
+                    numeratorT += weights[i] * args[i].t
                     denominatorT += weights[i]
         else:
             if atLeastOne1:
@@ -242,6 +240,12 @@ class Opinion:
         else:
             raise Exception("_conflicted_fusion is not allowed for these arguments")
 
+    @staticmethod
+    def _adjust_value(val):
+        return max(min(val, 1), 0)
+
+""" 
+// TESTS
 os = [Opinion(0.7,0.9,0.9), Opinion(0.4,0.7,1),Opinion(0.7,0.7,1),Opinion(0.2,0.9,1),Opinion(0.55,0.8,1)]
 w = [1, 0.3, 0.2, 1, 0.5]
 print(Opinion._single_or(os[0],os[1]))
@@ -255,7 +259,5 @@ print(Opinion._cum_and(os))
 print(Opinion._internal_fusion(os, w, 1))
 
 print(Opinion._conflicted_fusion(os, w))
-
-
-
+"""
 

+ 49 - 0
Evaluation.py

@@ -0,0 +1,49 @@
+from CertainTrust import Opinion
+from CSVReader import CSVReader
+
+class Evaluator:
+    @staticmethod
+    def evaluate_system(packagefile, months):
+        '''
+        Evaluates a system, based on its packages and months to evaluate
+        :param packagefile: packages installed on the system
+        :param months: for how many months the prediction is done
+        :return: system evaluation
+        '''
+        packages=CSVReader.readCSVPackages(packagefile)
+        model=CSVReader.readCSVModel(months)
+
+        system={}
+        for p in packages:
+            if p in model:
+                system[p]=model[p]
+
+        return Opinion._cum_and(list(system.values()))
+
+    @staticmethod
+    def fuse_models_equal_weights(*models):
+        '''
+        Fuses models , assume models have equal lists of packages
+        :param models: list of model-dictionaries
+        :return: fused model-dictionary
+        '''
+        result_dict = {}
+        package_list = list(models[0].keys())
+        for p in package_list:
+            # get list of items to fuse
+            p_ops = []
+            for model in models:
+                p_ops.append(model[p])
+            print(p_ops)
+            weights = [1]*len(p_ops)
+            # fusion
+            fused_op = Opinion._internal_fusion(p_ops, weights)
+            result_dict[p]=fused_op
+        return result_dict
+
+
+
+'''
+print( Evaluator.fuse_models_equal_weights(CSVReader.readCSVModel(9),CSVReader.readCSVModel(12)))
+print(Evaluator.evaluate_system("inputs/dummy_input_package.csv", 9))
+'''

+ 8 - 0
inputs/dummy_input_package.csv

@@ -0,0 +1,8 @@
+linux
+openssl
+php7.0
+chromium-browser
+openjdk-8
+
+
+

+ 10 - 0
inputs/dummy_input_package_prediction_errorcompl.csv

@@ -0,0 +1,10 @@
+linux:59:0.57
+firefox-esr:43:0.72
+chromium-browser:82:0.94
+openjdk-8:26:0.87
+icedove:26:0.84
+wireshark:14:0.93
+php7.0:13:0.56
+mysql-transitional:17:0.63
+openssl:5:0.67
+qemu:23:0

+ 10 - 0
models/dummy_model_12.csv

@@ -0,0 +1,10 @@
+wireshark:0.9611111111111111:0.93:1
+icedove:0.9277777777777778:0.84:1
+firefox-esr:0.8805555555555555:0.72:1
+qemu:0.9361111111111111:0.0:1
+openjdk-8:0.9277777777777778:0.87:1
+openssl:0.9861111111111112:0.67:1
+mysql-transitional:0.9527777777777777:0.63:1
+linux:0.8361111111111111:0.57:1
+php7.0:0.9638888888888889:0.56:1
+chromium-browser:0.7722222222222223:0.94:1

+ 10 - 0
models/dummy_model_6.csv

@@ -0,0 +1,10 @@
+openjdk-8:0.8555555555555556:0.87:1
+php7.0:0.9277777777777778:0.56:1
+firefox-esr:0.7611111111111111:0.72:1
+linux:0.6722222222222223:0.57:1
+mysql-transitional:0.9055555555555556:0.63:1
+wireshark:0.9222222222222223:0.93:1
+qemu:0.8722222222222222:0.0:1
+chromium-browser:0.5444444444444445:0.94:1
+icedove:0.8555555555555556:0.84:1
+openssl:0.9722222222222222:0.67:1

+ 10 - 0
models/dummy_model_9.csv

@@ -0,0 +1,10 @@
+linux:0.7814814814814814:0.57:1
+wireshark:0.9481481481481482:0.93:1
+openjdk-8:0.9037037037037037:0.87:1
+qemu:0.9148148148148149:0.0:1
+firefox-esr:0.8407407407407408:0.72:1
+php7.0:0.9518518518518518:0.56:1
+chromium-browser:0.6962962962962963:0.94:1
+mysql-transitional:0.937037037037037:0.63:1
+icedove:0.9037037037037037:0.84:1
+openssl:0.9814814814814815:0.67:1