Commit 0dbcbe00 authored by tpetaja1's avatar tpetaja1
Browse files

network results

parent 50680bf4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2,3 +2,4 @@
*~
drafts/*
results/*
matlab/*
+8 −2
Original line number Diff line number Diff line
@@ -94,8 +94,8 @@ class DataHandler(object):
    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"))
            alg_type, alg.dimension, alg.blocks, alg.obs, int(alg.lambd),
            int(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):
@@ -136,6 +136,12 @@ class DataHandler(object):
                    f.write(",{0:.3f}".format(dev))
                except ValueError:
                    f.write(",%s" % dev)
            f.write("\nNormalized Temporal deviations ")
            for dev in alg.norm_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):
+41 −8
Original line number Diff line number Diff line
@@ -14,6 +14,13 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s,
                      indexes, out_queue, prev_pipe, next_pipe,
                      proc_index, stopping_criteria, pen_func),
                     last=False):
    # Multiprocessing worker computing the TVGL algorithm for the given
    # set of blocks. Communicates the variables with neighboring
    # processes through multiprocessing Pipes, between every iteration.
    # Stopping criteria is fulfilled as every process reaches its own
    # stopping criteria. After global stopping criteria, poison pills
    # are sent to Pipes to terminate the iterations in every process.

    try:
        iteration = 0
        n = len(indexes)
@@ -26,8 +33,10 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s,
        thetas_pre = []
        final_thetas = {}
        dimension = np.shape(thetas[0])[0]
        while iteration < MAX_ITER:  # and stopping_criteria is False:
            """ Theta Update """
        while iteration < MAX_ITER:

            """ Send last Z2, U2 values to next process,
                Receive first Z2, U2 values from previous process """
            if next_pipe is not None:
                next_pipe.send((z2s[-1], u2s[-1]))
            if prev_pipe is not None:
@@ -35,10 +44,11 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s,
                if received is None:
                    break
                z2s[0], u2s[0] = received

                """ Theta Update """
            for j, i in zip(indexes[:end], range(nn)):
                a = (z0s[i] + z1s[i] + z2s[i] - u0s[i] - u1s[i] - u2s[i])/3
                at = a.transpose()
                #m = nju*(a + at)/2 - emp_cov_mat[i]
                m = (a + at)/(2 * nju) - emp_cov_mat[i]
                d, q = np.linalg.eig(m)
                qt = q.transpose()
@@ -47,6 +57,9 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s,
                thetas[i] = np.real(
                    nju/2*np.dot(np.dot(q, diagonal), qt))
                final_thetas[j] = thetas[i]

            """ Send first Theta value to previous process,
                Receive last Theta value from next process """
            if prev_pipe is not None:
                prev_pipe.send(thetas[0])
            if next_pipe is not None:
@@ -54,9 +67,11 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s,
                if received is None:
                    break
                thetas[-1] = received

            """ Z0 Update """
            for i in range(nn):
                z0s[i] = pf.soft_threshold_odd(thetas[i] + u0s[i], lambd, rho)

            """ Z1-Z2 Update """
            for i in range(1, n):
                a = thetas[i] - thetas[i-1] + u2s[i] - u1s[i-1]
@@ -64,13 +79,16 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s,
                summ = thetas[i] + thetas[i-1] + u2s[i] + u1s[i-1]
                z1s[i-1] = 0.5*(summ - e)
                z2s[i] = 0.5*(summ + e)

            """ U0 Update """
            for i in range(nn):
                u0s[i] = u0s[i] + thetas[i] - z0s[i]

            """ U1-U2 Update """
            for i in range(1, n):
                u1s[i-1] = u1s[i-1] + thetas[i-1] - z1s[i-1]
                u2s[i] = u2s[i] + thetas[i] - z2s[i]

            """ Check stopping criteria """
            if iteration > 0:
                fro_norm = 0
@@ -85,12 +103,15 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s,
            iteration += 1
            if iteration % 500 == 0 and proc_index == 0:
                print "*** Iteration: %s ***\n" % iteration

        """ When stopping criteria reached, send poison pills to pipes """
        if next_pipe is not None:
            next_pipe.send(None)
        if prev_pipe is not None:
            prev_pipe.send(None)

        """ Put final Thetas into result Queue """
        out_queue.put((final_thetas, iteration))
        #print "Process %s finished" % proc_index
    except Exception as e:
        traceback.print_exc()
        raise e
@@ -103,15 +124,21 @@ class ParallelTVGL(TVGL):
        if self.processes > self.blocks:
            self.processes = self.blocks
        self.chunk = int(np.round(self.blocks/float(self.processes)))
        print "Processes: %s" % self.processes

    def init_algorithm(self):

        """ Create result Queue, Pipes for communication between processes,
            initialize list for processes, stopping criteria Manager """
        self.results = multiprocessing.JoinableQueue()
        self.pipes = [multiprocessing.Pipe() for i in range(self.processes-1)]
        self.procs = []
        stopping_criteria = multiprocessing.Manager().list()
        for i in range(self.processes):
            stopping_criteria.append(False)

        """ Create processes. The blocks are divided into chunks based on their index.
            Each process will get a chunk of blocks,
            The last process gets the remaining blocks """
        for i in range(self.processes):
            if i == 0:
                p = multiprocessing.Process(target=mp_parallel_tvgl,
@@ -179,10 +206,16 @@ class ParallelTVGL(TVGL):
            self.procs.append(p)

    def run_algorithm(self, max_iter=10000):

        """ Initialize algorithm """
        self.init_algorithm()
        start_time = time.time()

        """ Start processes / algorithm """
        for p in self.procs:
            p.start()

        """ Get results """
        results = {}
        for i in range(self.processes):
            result, iteration = self.results.get()
@@ -193,12 +226,12 @@ class ParallelTVGL(TVGL):
            self.thetas[i] = results[i]
        for p in self.procs:
            p.join()

        """ Perform final adjustments """
        self.iteration = iteration
        #print self.iteration
        self.run_time = '{0:.3g}'.format(time.time() - start_time)
        self.final_tuning(True, MAX_ITER)
        #self.thetas = [np.round(theta, self.roundup) for theta in self.thetas]

    def terminate_pools(self):
    def terminate_processes(self):
        for p in self.procs:
            p.terminate()
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ class SerialTVGL(TVGL):
    def z1_z2_update(self):
        aa = [self.thetas[i] - self.thetas[i-1] + self.u2s[i] - self.u1s[i-1]
              for i in range(1, self.blocks)]
        ee = [pf.group_lasso_penalty(a, self.beta, self.rho) for a in aa]
        ee = [getattr(pf, self.penalty_function)(a, self.beta, self.rho) for a in aa]
        #ee = pf.group_lasso_penaltys(aa, self.beta, self.rho)
        for i in range(1, self.blocks):
        #    a = self.thetas[i] - self.thetas[i-1] + self.u2s[i] - self.u1s[i-1]
+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ class StaticGL(TVGL):

    def temporal_deviations(self):
        self.deviations = ["n/a"]
        self.norm_deviations = ["n/a"]
        self.dev_ratio = "n/a"

    def correct_edges(self):
Loading