modified_adsorption.py
def calculate_mad(self):
        print "\n...Calculating modified adsorption."
        nr_nodes = self._W.shape[0]

        # 1. Initialize Yhat
        self._Yh = lil_matrix(self._Y.copy())

        # 2. Calculate Mvv
        self._M = lil_matrix((nr_nodes, nr_nodes))
        # self._M = lil_matrix(np.diag((self._mu1*self._Pinj).toarray().flatten()) + (np.eye(nr_nodes)*self._mu3))
        """
        TODO: This does not work cause flattening and to array-ing is not memory cool so it fails. Need to find a way to
            build this initial matrices with sparse matrices
        """
        for v in range(nr_nodes):
            first_part = self._mu1 * self._Pinj[v, 0]
            second_part = 0.

            for u in self._W[v, :].nonzero()[1]:
                if u != v:
                    second_part += (self._Pcont[v, 0] * self._W[v, u] + self._Pcont[u, 0] * self._W[u, v])
            self._M[v, v] = first_part + (self._mu2 * second_part) + self._mu3

        # W_coo = self._W.tocoo()
        #
        # for i, j, v in itertools.izip(W_coo.row, W_coo.col, W_coo.data):
        # second_part = self._mu2 * ((self._Pcont[i, 0] * v) + self._Pcont[j, 0] * v)
        # self._M[i, i] += second_part

        # self._M = csr_matrix(self._M)

        # 3. Begin main loop
        itero = 0
        r = lil_matrix((1, self._Y.shape[1]))
        r[-1, -1] = 1.
        Yh_old = lil_matrix((self._Y.shape[0], self._Y.shape[1]))

        # Main loop begins
        Pcont = self._Pcont.toarray()

        while not self._check_convergence(Yh_old, self._Yh, ) and self.max_iter > itero:
            itero += 1
            print ">>>>>Iteration:%d" % itero
            self._D = lil_matrix((nr_nodes, self._Y.shape[1]))
            # 4. Calculate Dv
            print "\t\tCalculating D..."
            time_d = time.time()
            W_coo = self._W.tocoo()
            for i, j, v in itertools.izip(W_coo.row, W_coo.col, W_coo.data):
                # self._D[i, :] += (Pcont[i][0] * v + Pcont[j][0] * v) * self._Yh[j, :]
                self._D[i, :] += (v * (Pcont[i][0] + Pcont[j][0])) * self._Yh[j, :]
                # print i

            print "\t\tTime it took to calculate D:", time.time() - time_d
            print
            # for v in range(nr_nodes):
            # for u in self._W[v, :].nonzero()[1]:
            # self._D[v, :] += (self._Pcont[v, 0] * self._W[v, u] + self._Pcont[u, 0] * self._W[u, v])\
            # * self._Yh[u, :]
            # print v,  self._D[v, :].todense()

            print "\t\tUpdating Y..."
            # 5. Update Yh
            time_y = time.time()
            Yh_old = self._Yh.copy()
            for v in range(nr_nodes):
                # 6.
                second_part = ((self._mu1 * self._Pinj[v, 0] * self._Y[v, :]) +
                               (self._mu2 * self._D[v, :]) +
                               (self._mu3 * self._Pabnd[v, 0] * r))
                self._Yh[v, :] = 1. / (self._M[v, v]) * second_part
                # print v
            # self._Yh = csr_matrix(self._Yh)
            print "\t\tTime it took to calculate Y:", time.time() - time_y
            print
            # repeat until convergence.

        TODO: This does not work cause flattening and to array-ing is not memory cool so it fails. Need to find a way to
            build this initial matrices with sparse matrices
        
This does not work cause flattening and to array-ing is not memory cool so it fails. Need to find a way to
build this initial matrices with sparse matrices
This does not work cause flattening and to array - ing is not memory cool so it fails . Need to find a way to build this initial matrices with sparse matrices
def calculate_mad(self):
        print "\n...Calculating modified adsorption."
        nr_nodes = self._W.shape[0]
        self._Yh = lil_matrix(self._Y.copy())
        self._M = lil_matrix((nr_nodes, nr_nodes))
        for v in range(nr_nodes):
            first_part = self._mu1 * self._Pinj[v, 0]
            second_part = 0.
            for u in self._W[v, :].nonzero()[1]:
                if u != v:
                    second_part += (self._Pcont[v, 0] * self._W[v, u] + self._Pcont[u, 0] * self._W[u, v])
            self._M[v, v] = first_part + (self._mu2 * second_part) + self._mu3
         for i, j, v in itertools.izip(W_coo.row, W_coo.col, W_coo.data):
         second_part = self._mu2 * ((self._Pcont[i, 0] * v) + self._Pcont[j, 0] * v)
         self._M[i, i] += second_part
         self._M = csr_matrix(self._M)
         3. Begin main loop
        itero = 0
        r = lil_matrix((1, self._Y.shape[1]))
        r[-1, -1] = 1.
        Yh_old = lil_matrix((self._Y.shape[0], self._Y.shape[1]))
         Main loop begins
        Pcont = self._Pcont.toarray()
        while not self._check_convergence(Yh_old, self._Yh, ) and self.max_iter > itero:
            itero += 1
            print ">>>>>Iteration:%d" % itero
            self._D = lil_matrix((nr_nodes, self._Y.shape[1]))
             4. Calculate Dv
            print "\t\tCalculating D..."
            time_d = time.time()
            W_coo = self._W.tocoo()
            for i, j, v in itertools.izip(W_coo.row, W_coo.col, W_coo.data):
                 self._D[i, :] += (Pcont[i][0] * v + Pcont[j][0] * v) * self._Yh[j, :]
                self._D[i, :] += (v * (Pcont[i][0] + Pcont[j][0])) * self._Yh[j, :]
                 print i
            print "\t\tTime it took to calculate D:", time.time() - time_d
            print
             for v in range(nr_nodes):
             for u in self._W[v, :].nonzero()[1]:
             self._D[v, :] += (self._Pcont[v, 0] * self._W[v, u] + self._Pcont[u, 0] * self._W[u, v])\
             * self._Yh[u, :]
             print v,  self._D[v, :].todense()
            print "\t\tUpdating Y..."
             5. Update Yh
            time_y = time.time()
            Yh_old = self._Yh.copy()
            for v in range(nr_nodes):
                 6.
                second_part = ((self._mu1 * self._Pinj[v, 0] * self._Y[v, :]) +
                               (self._mu2 * self._D[v, :]) +
                               (self._mu3 * self._Pabnd[v, 0] * r))
                self._Yh[v, :] = 1. / (self._M[v, v]) * second_part
                 print v
             self._Yh = csr_matrix(self._Yh)
            print "\t\tTime it took to calculate Y:", time.time() - time_y
            print
             repeat until convergence.
def calculate_mad ( self ) : print "\n...Calculating modified adsorption." nr_nodes = self . _W . shape [ 0 ] self . _Yh = lil_matrix ( self . _Y . copy ( ) ) self . _M = lil_matrix ( ( nr_nodes , nr_nodes ) ) for v in range ( nr_nodes ) : first_part = self . _mu1 * self . _Pinj [ v , 0 ] second_part = 0. for u in self . _W [ v , : ] . nonzero ( ) [ 1 ] : if u != v : second_part += ( self . _Pcont [ v , 0 ] * self . _W [ v , u ] + self . _Pcont [ u , 0 ] * self . _W [ u , v ] ) self . _M [ v , v ] = first_part + ( self . _mu2 * second_part ) + self . _mu3 itero = 0 r = lil_matrix ( ( 1 , self . _Y . shape [ 1 ] ) ) r [ - 1 , - 1 ] = 1. Yh_old = lil_matrix ( ( self . _Y . shape [ 0 ] , self . _Y . shape [ 1 ] ) ) Pcont = self . _Pcont . toarray ( ) while not self . _check_convergence ( Yh_old , self . _Yh , ) and self . max_iter > itero : itero += 1 print ">>>>>Iteration:%d" % itero self . _D = lil_matrix ( ( nr_nodes , self . _Y . shape [ 1 ] ) ) print "\t\tCalculating D..." time_d = time . time ( ) W_coo = self . _W . tocoo ( ) for i , j , v in itertools . izip ( W_coo . row , W_coo . col , W_coo . data ) : self . _D [ i , : ] += ( v * ( Pcont [ i ] [ 0 ] + Pcont [ j ] [ 0 ] ) ) * self . _Yh [ j , : ] print "\t\tTime it took to calculate D:" , time . time ( ) - time_d print print "\t\tUpdating Y..." time_y = time . time ( ) Yh_old = self . _Yh . copy ( ) for v in range ( nr_nodes ) : second_part = ( ( self . _mu1 * self . _Pinj [ v , 0 ] * self . _Y [ v , : ] ) + ( self . _mu2 * self . _D [ v , : ] ) + ( self . _mu3 * self . _Pabnd [ v , 0 ] * r ) ) self . _Yh [ v , : ] = 1. / ( self . _M [ v , v ] ) * second_part print "\t\tTime it took to calculate Y:" , time . time ( ) - time_y print
TODO: This does not work cause flattening and to array-ing is not memory cool so it fails.
TODO : This does not work cause flattening and to array - ing is not memory cool so it fails .
# 1. Initialize Yhat # 2. Calculate Mvv # self._M = lil_matrix(np.diag((self._mu1*self._Pinj).toarray().flatten()) + (np.eye(nr_nodes)*self._mu3)) """
        TODO: This does not work cause flattening and to array-ing is not memory cool so it fails. Need to find a way to
            build this initial matrices with sparse matrices
        """ # W_coo = self._W.tocoo() # # for i, j, v in itertools.izip(W_coo.row, W_coo.col, W_coo.data): # second_part = self._mu2 * ((self._Pcont[i, 0] * v) + self._Pcont[j, 0] * v) # self._M[i, i] += second_part # self._M = csr_matrix(self._M) # 3. Begin main loop # Main loop begins # 4. Calculate Dv # self._D[i, :] += (Pcont[i][0] * v + Pcont[j][0] * v) * self._Yh[j, :] # print i # for v in range(nr_nodes): # for u in self._W[v, :].nonzero()[1]: # self._D[v, :] += (self._Pcont[v, 0] * self._W[v, u] + self._Pcont[u, 0] * self._W[u, v])\ # * self._Yh[u, :] # print v,  self._D[v, :].todense() # 5. Update Yh # 6. # print v # self._Yh = csr_matrix(self._Yh) # repeat until convergence.
self