Commit 5f4872f1 authored by tpetaja1's avatar tpetaja1
Browse files

support for real data

-network results to csv files
parent d8cfd339
Loading
Loading
Loading
Loading
+61 −1
Original line number Diff line number Diff line
@@ -91,6 +91,65 @@ class DataHandler(object):
                line = line[1:]
                new_file.write("%s\n" % line)

    def write_network_results(self, datafile, alg_type, alg, splitter=","):
        run_time = datetime.datetime.now()
        results_name = "network_results/%s_di%sbl%sob%sla%sbe%s_%s.csv" % (
            alg_type, alg.dimension, alg.blocks, alg.obs, alg.lambd,
            alg.beta, run_time.strftime("%Y%m%d%H%M%S"))
        """ Read features """
        with open(datafile, "r") as f:
            for i, line in enumerate(f):
                if i == 0:
                    feats = line.strip().split(splitter)[1:]
                    break
        features = {}
        for i, feat in enumerate(feats):
            features[i] = feat
        """ Write Results """
        with open(results_name, "w") as f:
            f.write("# Information\n")
            f.write("Run datetime, %s\n" %
                    run_time.strftime("%Y-%m-%d %H:%M:%S"))
            f.write("Data file, %s\n" % datafile)
            f.write("Algorithm type, %s\n" % alg.__class__.__name__)
            f.write("Penalty function, %s\n" % alg.penalty_function)
            f.write("Data dimension, %s\n" % alg.dimension)
            f.write("Blocks, %s\n" % alg.blocks)
            f.write("Observations in a block, %s\n" % alg.obs)
            f.write("Rho, %s\n" % alg.rho)
            f.write("Beta, %s\n" % alg.beta)
            f.write("Lambda, %s\n" % alg.lambd)
            f.write("Processes used, %s\n" % alg.processes)
            f.write("\n")
            f.write("# Results\n")
            f.write("Algorithm run time, %s seconds\n" % alg.run_time)
            f.write("Iterations to complete, %s\n\n" % alg.iteration)
            try:
                f.write("Temporal deviations ratio (max/mean), {0:.3f}\n"
                        .format(alg.dev_ratio))
            except ValueError:
                f.write("Temporal deviations ratio (max/mean), %s\n"
                        % alg.dev_ratio)
            f.write("Temporal deviations ")
            for dev in alg.deviations:
                try:
                    f.write(",{0:.3f}".format(dev))
                except ValueError:
                    f.write(",%s" % dev)
            """ Write networks """
            f.write("\n\n#Networks:\n\n")
            for k in range(alg.blocks):
                f.write("Block %s\n " % k)
                for feat in feats:
                    f.write("," + feat)
                f.write("\n")
                for i in range(alg.dimension):
                    f.write(features[i])
                    for j in range(alg.dimension):
                        f.write("," + str(alg.thetas[k][i, j]))
                    f.write("\n")
                f.write("\n\n")

    def write_results(self, datafile, alg_type, alg):
        run_time = datetime.datetime.now()
        results_name = "results/%s_di%sbl%sob%sla%sbe%s_%s.txt" % (
@@ -111,7 +170,8 @@ class DataHandler(object):
            f.write("Lambda: %s\n" % alg.lambd)
            f.write("Processes used: %s\n" % alg.processes)
            f.write("Total edges: %s\n" % alg.real_edges)
            f.write("Total edgeless: %s\n\n" % alg.real_edgeless)
            f.write("Total edgeless: %s\n" % alg.real_edgeless)
            f.write("\n")
            f.write("# Results\n")
            f.write("Algorithm run time: %s seconds\n" % alg.run_time)
            f.write("Iterations to complete: %s\n\n" % alg.iteration)
+13 −4
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ class TVGL(object):
        self.e = 1e-5
        self.roundup = 1

    def read_data(self, filename, comment="#", splitter=","):
    def read_data(self, filename, comment="#", splitter=",", datecolumn=True):
        with open(filename, "r") as f:
            comment_count = 0
            for i, line in enumerate(f):
@@ -43,6 +43,9 @@ class TVGL(object):
                    comment_count += 1
                else:
                    if self.dimension is None:
                        if datecolumn:
                            self.dimension = len(line.split(splitter)) - 1
                        else:
                            self.dimension = len(line.split(splitter))
        datasamples = i + 1 - comment_count
        print "Total data samples: %s" % datasamples
@@ -58,8 +61,14 @@ class TVGL(object):
                    if i == 1:
                        self.generate_real_thetas(line, splitter)
                    continue
                if datecolumn:
                    lst.append([float(x)
                                for x in np.array(line.strip().
                                                  split(splitter)[1:])])
                else:
                    lst.append([float(x)
                            for x in np.array(line.strip().split(splitter))])
                                for x in np.array(line.strip().
                                                  split(splitter))])
                count += 1
                if count == self.obs:
                    datablck = np.array(lst)
+12 −8
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ from StaticGL import StaticGL


if __name__ == "__main__" and len(sys.argv) > 1:
    synthetic = False
    datahandler = DataHandler()
    start_time = time.time()
    algo_type = sys.argv[1]
@@ -42,6 +43,7 @@ if __name__ == "__main__" and len(sys.argv) > 1:
    algorithm.temporal_deviations()
    print "Temp deviations: "
    print algorithm.deviations
    if synthetic:
        algorithm.correct_edges()
        print "Total Edges: %s" % algorithm.real_edges
        print "Correct Edges: %s" % algorithm.correct_positives
@@ -50,5 +52,7 @@ if __name__ == "__main__" and len(sys.argv) > 1:
        print "False Edges: %s" % false_edges
        print "F1 Score: %s" % algorithm.f1score
        datahandler.write_results(filename, algo_type, algorithm)
    else:
        datahandler.write_network_results(filename, algo_type, algorithm)
    print "Algorithm run time: %s seconds" % (algorithm.run_time)
    print "Execution time: %s seconds" % (time.time() - start_time)