Commit f7ee1bb6 authored by tpetaja1's avatar tpetaja1
Browse files

F1 score, dynamic and static GL

parent aa55cf29
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
*.pyc
*~
drafts/*

AsyncProTVGL.py

deleted100644 → 0
+0 −68
Original line number Diff line number Diff line

from SerialTVGL import SerialTVGL
import numpy as np
import multiprocessing
import mp_workers_async as mp


class AsyncProTVGL(SerialTVGL):

    def __init__(self, filename, blocks=10,
                 lambd=30, beta=4, processes=2):
        super(AsyncProTVGL, self).__init__(filename, blocks,
                                           lambd, beta, processes)

    def init_algorithm(self):
        self.pool = multiprocessing.Pool(self.processes)

    def terminate_pools(self):
        self.pool.close()
        self.pool.join()

    """
    def theta_update(self):
        for i in range(self.blocks):
            a = (self.z0s[i] + self.z1s[i] + self.z2s[i] -
                 self.u0s[i] - self.u1s[i] - self.u2s[i])/3
            at = a.transpose()
            m = self.nju*(a + at)/2 - self.emp_cov_mat[i]
            d, q = np.linalg.eig(m)
            qt = q.transpose()
            sqrt_matrix = np.sqrt(d**2 + 4/self.nju*np.ones(self.dimension))
            diagonal = np.diag(d) + np.diag(sqrt_matrix)
            self.thetas[i] = np.real(
                self.nju/2*np.dot(np.dot(q, diagonal), qt))
    """

    def z_update(self):
        res_z0s = self.pool.apply_async(mp.z0_update,
                                        (self.thetas, self.z0s,
                                         self.u0s, self.lambd,
                                         self.rho, self.blocks))
        res_z1z2s = self.pool.apply_async(mp.z1_z2_update,
                                          (self.thetas, self.z1s, self.z2s,
                                           self.u1s, self.u2s, self.beta,
                                           self.rho, self.blocks))
        self.z0s = res_z0s.get(timeout=1)
        z1s_z2s = res_z1z2s.get(timeout=1)
        self.z1s = z1s_z2s[0]
        self.z2s = z1s_z2s[1]
        #pool.close()

    """
    def u_update(self):
        pool = multiprocessing.Pool(self.processes)
        res_u0s = pool.apply_async(mp.u0_update,
                                   (self.u0s, self.thetas,
                                    self.z0s, self.blocks))
        res_u1s = pool.apply_async(mp.u1_update,
                                   (self.u1s, self.thetas,
                                    self.z1s, self.blocks))
        res_u2s = pool.apply_async(mp.u2_update,
                                   (self.u2s, self.thetas,
                                    self.z2s, self.blocks))
        self.u0s = res_u0s.get()
        self.u1s = res_u1s.get()
        self.u2s = res_u2s.get()
        pool.close()
    """
+16 −10
Original line number Diff line number Diff line
@@ -101,7 +101,8 @@ class DataHandler(object):
            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_type)
            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)
@@ -114,17 +115,22 @@ class DataHandler(object):
            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)
            f.write("Matching edge ratio: {0:.3f}"
                    .format(alg.matching_edges_ratio))
            f.write("    (Matching edges: %s)\n" % alg.matching_edges)
            f.write("False edge ratio: {0:.3f}"
                    .format(alg.false_edges_ratio))
            f.write("    (False edges: %s)\n" % alg.false_edges)
            f.write("Correct positive edges: %s\n" % alg.correct_positives)
            f.write("All positives: %s\n" % alg.all_positives)
            f.write("F1 Score: {0:.3f}\n"
                    .format(alg.f1score))
            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)
            f.write("\n")


DynamicGL.py

0 → 100644
+67 −0
Original line number Diff line number Diff line

from TVGL import TVGL
import penalty_functions as pf
import numpy as np
import multiprocessing
import time
import traceback


def mp_dynamic_gl((theta, z0, u0, emp_cov_mat, rho,
                  lambd, nju, dimension, max_iter)):
    try:
        iteration = 0
        stopping_criteria = False
        theta_pre = []
        while iteration < max_iter and stopping_criteria is False:
            """ Theta update """
            a = z0 - u0
            at = a.transpose()
            m = nju*(a + at)/2 - emp_cov_mat
            d, q = np.linalg.eig(m)
            qt = q.transpose()
            sqrt_matrix = np.sqrt(d**2 + 4/nju*np.ones(dimension))
            diagonal = np.diag(d) + np.diag(sqrt_matrix)
            theta = np.real(
                nju/2*np.dot(np.dot(q, diagonal), qt))
            """ Z-update """
            z0 = pf.soft_threshold_odd(theta + u0, lambd, rho)
            """ U-update """
            u0 += theta - z0
            """ Check stopping criteria """
            if iteration > 0:
                dif = theta - theta_pre
                fro_norm = np.linalg.norm(dif)
                if fro_norm < 1e-5:
                    stopping_criteria = True
            theta_pre = list(theta)
            iteration += 1
    except Exception as e:
        traceback.print_exc()
        raise e
    return theta


class DynamicGL(TVGL):

    def __init__(self, *args, **kwargs):
        super(DynamicGL, self).__init__(beta=0, *args, **kwargs)
        self.nju = float(self.obs)/float(self.rho)
        self.iteration = "n/a"
        self.penalty_function = "n/a"

    def get_rho(self):
        return self.obs + 1

    def run_algorithm(self, max_iter=10000):
        start_time = time.time()
        p = multiprocessing.Pool(self.processes)
        inputs = [(self.thetas[i], self.z0s[i], self.u0s[i],
                   self.emp_cov_mat[i], self.rho,
                   self.lambd, self.nju, self.dimension, max_iter)
                  for i in range(self.blocks)]
        self.thetas = p.map(mp_dynamic_gl, inputs)
        p.close()
        p.join()
        self.run_time = '{0:.3g}'.format(time.time() - start_time)
        self.thetas = [np.round(theta, 3) for theta in self.thetas]

MultiProTVGL.py

deleted100644 → 0
+0 −62
Original line number Diff line number Diff line

from TVGL import TVGL
import multiprocessing
import mp_workers_multi as mp


class MultiProTVGL(TVGL):

    def __init__(self, filename, blocks=10,
                 lambd=30, beta=4, processes=2):
        super(MultiProTVGL, self).__init__(filename, blocks,
                                           lambd, beta, processes)

    def theta_update(self):
        inputs = [(self.thetas[i], self.z0s[i], self.z1s[i], self.z2s[i],
                   self.u0s[i], self.u1s[i], self.u2s[i],
                   self.emp_cov_mat[i], self.nju)
                  for i in range(self.blocks)]
        pool = multiprocessing.Pool(self.processes)
        self.thetas = pool.map(mp.theta_update, inputs)
        pool.close()

    def z_update(self):
        """ z0-update """
        inputs = [(self.thetas[i], self.u0s[i], self.lambd, self.rho)
                  for i in range(self.blocks)]
        pool = multiprocessing.Pool(self.processes)
        self.z0s = pool.map(mp.z0_update, inputs)
        pool.close()
        """ z1-z2-update """
        inputs = [(self.thetas[i], self.thetas[i-1],
                   self.u1s[i], self.u1s[i-1], self.u2s[i],
                   self.beta, self.rho)
                  for i in range(1, self.blocks)]
        pool = multiprocessing.Pool(self.processes)
        zs = pool.map(mp.z1_z2_update, inputs)
        pool.close()
        for i in range(self.blocks - 1):
            self.z1s[i] = zs[i][0]
            self.z2s[i+1] = zs[i][1]

    def u_update(self):
        """ u0-update """
        inputs = [(self.thetas[i], self.u0s[i], self.z0s[i])
                  for i in range(self.blocks)]
        pool = multiprocessing.Pool(self.processes)
        self.u0s = pool.map(mp.u0_update, inputs)
        pool.close()
        """ u1-update """
        inputs = [(self.thetas[i], self.u1s[i], self.z1s[i])
                  for i in range(self.blocks - 1)]
        pool = multiprocessing.Pool(self.processes)
        u1s = pool.map(mp.u1_update, inputs)
        self.u1s[0:self.blocks - 1] = u1s
        pool.close()
        """ u2-update """
        inputs = [(self.thetas[i], self.u2s[i], self.z2s[i])
                  for i in range(1, self.blocks)]
        pool = multiprocessing.Pool(self.processes)
        u2s = pool.map(mp.u2_update, inputs)
        self.u2s[1:self.blocks] = u2s
        pool.close()
Loading