def check_lil_diags(self): assert_array_equal(lil_diags([[1,2,3],[4,5],[6]], [0,1,2],(3,3)).todense(), [[1,4,6], [0,2,5], [0,0,3]]) assert_array_equal(lil_diags([[6],[4,5],[1,2,3]], [2,1,0],(3,3)).todense(), [[1,4,6], [0,2,5], [0,0,3]]) assert_array_equal(lil_diags([[6,7,8],[4,5],[1,2,3]], [2,1,0],(3,3)).todense(), [[1,4,6], [0,2,5], [0,0,3]]) assert_array_equal(lil_diags([[1,2,3],[4,5],[6]], [0,-1,-2],(3,3)).todense(), [[1,0,0], [4,2,0], [6,5,3]]) assert_array_equal(lil_diags([[6,7,8],[4,5]], [-2,-1],(3,3)).todense(), [[0,0,0], [4,0,0], [6,5,0]])
def smooth(self,verts = None,alpha = 1.0): if self._adj is None : self._compute_neighbours() nverts = len(self.vertices) ns = lil_diags( [1./np.array(self._adj.sum(1)).flatten()],[0],(nverts,nverts) ) reg = ns*self._adj*np.matrix(self.vertices) if verts is None : self.vertices = np.array((1.-alpha)*self.vertices + alpha*reg) else : self.vertices[verts] = (1.-alpha)*self.vertices[verts] + alpha*np.array(reg)[verts]
def buildTransitionMatrix(self): """Build Markov transition matrix from the affinity matrix.""" start = time.time() self._transition = np.dot( self._affinity, sparse.lil_diags([1 / np.array(self._affinity.sum(0)).flatten()], [0], [self._n_nodes, self._n_nodes]), ) LOGGER.debug("Markov transition matrix was built in {0:.2f}s.".format(time.time() - start))
def __init__(self, model_graph, node_sizes, clqs, lattice=False): """ Initializes MRF object. model_graph: Numpy array or Scipy.sparse matrix A matrix defining the edges between nodes in the network. If graph[i, j] = 1 there exists a undirected edge from node i to j. node_sizes: List or Int A list of the possible number of values a discrete node can have. If node_sizes[i] = 2, then the discrete node i can have one of 2 possible values, such as True or False. If this parameter is passed as an integer, it indicates that all nodes have the size indicated by the integer. clqs: List of clique objects (cliques.py) A list of the cliques in the MRF. lattice: Bool Lattice is true if this MRF has a lattice graph structure, and false otherwise. """ """Assign the input values to their respective internal data members""" self.lattice = lattice self.num_nodes = model_graph.shape[0] self.cliques = clqs self.node_sizes = node_sizes """Convert the graph to a sparse matrix""" if ((type(model_graph) == type(np.matrix([0]))) or (type(model_graph) == type(np.array([0])))): model_graph = sparse.lil_matrix(model_graph) """In an MRF, all edges are bi-directional""" self.model_graph = model_graph - \ sparse.lil_diags([sparse.extract_diagonal(\ model_graph)], [0], (model_graph.shape[0], \ model_graph.shape[0]))\ + model_graph.T """ Obtain elimination order, which is just the input order in the case of a lattice. """ if self.lattice == True: self.order = range(0, self.model_graph.shape[0]) else: self.order = graph.topological_sort(self.model_graph)