Commit 021ef5c9 authored by tpetaja1's avatar tpetaja1
Browse files

added static graphical lasso

-development to tvgl files
-text files development
parent c38c289c
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -7,8 +7,10 @@ import mp_workers_async as mp

class AsyncProTVGL(TVGL):

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

    def theta_update(self):
        for i in range(self.blocks):
+19 −12
Original line number Diff line number Diff line
@@ -10,14 +10,15 @@ class DataHandler(object):
        self.sigmas = []
        self.network_files = []

    def read_network(self, filename, comment="#"):
    def read_network(self, filename, comment="#", splitter=",",
                     inversion=True):
        nodes = []
        self.network_files.append(filename)
        with open(filename, "r") as f:
            for i, line in enumerate(f):
                if comment in line:
                    continue
                data = line.split()
                data = line.split(splitter)
                if data[0] not in nodes:
                    nodes.append(int(data[0]))
                if data[1] not in nodes:
@@ -28,10 +29,11 @@ class DataHandler(object):
            for i, line in enumerate(f):
                if comment in line:
                    continue
                data = line.split()
                data = line.split(splitter)
                network[int(data[0])-1, int(data[1])-1] = float(data[2])
                network[int(data[1])-1, int(data[0])-1] = float(data[2])
        self.inverse_sigmas.append(network)
        if inversion:
            sigma = np.linalg.inv(network)
            print np.linalg.eigvals(sigma)
            self.sigmas.append(sigma)
@@ -78,6 +80,7 @@ class DataHandler(object):
        header = "# Data generated from networks:\n# "
        for f, datacount in zip(self.network_files, counts):
            header += "%s: %s, " % (f, datacount)
        header = header[:-2]
        header += "\n"
        with open(filename, "w") as new_file:
            new_file.write(header)
@@ -109,6 +112,10 @@ 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 (nonzeros): {0:.3f}\n"
                    .format(alg.nonzero_ratio))
            f.write("Temporal deviations ratio (max/mean): {0:.3f}\n"
                    .format(alg.dev_ratio))
            f.write("Temporal deviations: ")
            for dev in alg.deviations:
                f.write("{0:.3f} ".format(dev))
@@ -117,6 +124,6 @@ class DataHandler(object):

if __name__ == "__main__":
    dh = DataHandler()
    dh.read_network("networks/network1")
    dh.read_network("networks/network2")
    dh.generate_real_data([500, 500])
    dh.read_network("networks/network1.csv")
    dh.read_network("networks/network2.csv")
    dh.generate_real_data([50, 50])
+4 −2
Original line number Diff line number Diff line
@@ -6,8 +6,10 @@ import mp_workers_multi as mp

class MultiProTVGL(TVGL):

    def __init__(self, filename, blocks=10, processes=2):
        super(MultiProTVGL, self).__init__(filename, blocks, processes)
    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],

ProcTVGL.py

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

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


class ProcTVGL(SerialTVGL):

    def __init__(self, filename, blocks=10,
                 lambd=30, beta=4, processes=2):
        super(ProcTVGL, self).__init__(filename, blocks,
                                       lambd, beta, processes)
        self.chunk = int(np.round(self.blocks/float(self.processes)))

    def theta_update(self):
        procs = []
        out_queue = multiprocessing.Queue()
        for i in range(self.processes):
            if i == self.processes - 1:
                p = multiprocessing.Process(
                    target=mp.theta_update,
                    args=((self.thetas[self.chunk * i:],
                           self.z0s[self.chunk * i:],
                           self.z1s[self.chunk * i:],
                           self.z2s[self.chunk * i:],
                           self.u0s[self.chunk * i:],
                           self.u1s[self.chunk * i:],
                           self.u2s[self.chunk * i:],
                           self.emp_cov_mat[self.chunk * i:],
                           self.nju,
                           range(self.chunk * i, self.blocks)),
                          out_queue))
            else:
                p = multiprocessing.Process(
                    target=mp.theta_update,
                    args=((self.thetas[self.chunk * i:self.chunk*(i+1)],
                           self.z0s[self.chunk * i:self.chunk*(i+1)],
                           self.z1s[self.chunk * i:self.chunk*(i+1)],
                           self.z2s[self.chunk * i:self.chunk*(i+1)],
                           self.u0s[self.chunk * i:self.chunk*(i+1)],
                           self.u1s[self.chunk * i:self.chunk*(i+1)],
                           self.u2s[self.chunk * i:self.chunk*(i+1)],
                           self.emp_cov_mat[self.chunk * i:self.chunk*(i+1)],
                           self.nju,
                           range(self.chunk * i, self.chunk*(i+1))),
                          out_queue))
            procs.append(p)
            p.start()
        results = {}
        for i in range(self.processes):
            results.update(out_queue.get())
        for i in results:
            self.thetas[i] = results[i]
        for p in procs:
            p.join()

#    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()
+6 −4
Original line number Diff line number Diff line
@@ -6,8 +6,10 @@ import numpy as np

class SerialTVGL(TVGL):

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

    def theta_update(self):
        for i in range(self.blocks):
@@ -36,9 +38,9 @@ class SerialTVGL(TVGL):
            a = self.thetas[i] - self.thetas[i-1] + self.u2s[i] - self.u1s[i-1]
            e = pf.group_lasso_penalty(a, 2*self.beta/self.rho)
            self.z1s[i-1] = 0.5*(self.thetas[i-1] + self.thetas[i]
                                 + self.u1s[i] + self.u2s[i]) - 0.5*e
                                 + self.u1s[i-1] + self.u2s[i]) - 0.5*e
            self.z2s[i] = 0.5*(self.thetas[i-1] + self.thetas[i]
                               + self.u1s[i] + self.u2s[i]) + 0.5*e
                               + self.u1s[i-1] + self.u2s[i]) + 0.5*e

    def u_update(self):
        for i in range(self.blocks):
Loading