Exemplo n.º 1
0
    def add_layer(self, layer, **attr):
        if self.num_nodes_in_layers is 0:
            self.list_of_layers=[layer]
        else:
            self.list_of_layers.append(layer)
            
        self.num_layers = len(self.list_of_layers)
        self.num_nodes_in_layers = self.list_of_layers[0].number_of_nodes()
        
        for i,j in layer.edges():
			self.intra_layer_edges.append((
			i+(len(self.list_of_layers)-1)*layer.number_of_nodes(),
			j+(len(self.list_of_layers)-1)*layer.number_of_nodes()))
			
        try:
            Graph.__init__(self,
                        Graph(disjoint_union_all(self.list_of_layers),
                        **attr))
        except multinetxError:
            raise multinetxError("Multiplex cannot inherit Graph properly")

        ## Check if all graphs have the same number of nodes
        for lg in self.list_of_layers:
            try:
                assert(lg.number_of_nodes() == self.num_nodes_in_layers)
            except AssertionError:
                raise multinetxError("Graph at layer does not have the same number of nodes")  
Exemplo n.º 2
0
    def add_layer(self, layer, **attr):
        if self.num_nodes_in_layers is 0:
            self.list_of_layers = [layer]
        else:
            self.list_of_layers.append(layer)

        self.num_layers = len(self.list_of_layers)
        self.num_nodes_in_layers = self.list_of_layers[0].number_of_nodes()

        for i, j in layer.edges():
            self.intra_layer_edges.append(
                (i + (len(self.list_of_layers) - 1) * layer.number_of_nodes(),
                 j + (len(self.list_of_layers) - 1) * layer.number_of_nodes()))

        try:
            Graph.__init__(
                self, Graph(disjoint_union_all(self.list_of_layers), **attr))
        except multinetxError:
            raise multinetxError("Multiplex cannot inherit Graph properly")

        ## Check if all graphs have the same number of nodes
        for lg in self.list_of_layers:
            try:
                assert (lg.number_of_nodes() == self.num_nodes_in_layers)
            except AssertionError:
                raise multinetxError(
                    "Graph at layer does not have the same number of nodes")
Exemplo n.º 3
0
    def add_layer(self, layer, **attr):
        if self.num_nodes is 0:
            self.list_of_layers = [layer]
        else:
            self.list_of_layers.append(layer)
            self.num_nodes_in_layers.append(layer.number_of_nodes())

        self.num_layers = len(self.list_of_layers)

        for i, j in layer.edges():
            self.intra_layer_edges.append(
                (i + (len(self.list_of_layers) - 1) * layer.number_of_nodes(),
                 j + (len(self.list_of_layers) - 1) * layer.number_of_nodes()))
            try:
                Graph.__init__(
                    self, Graph(disjoint_union_all(self.list_of_layers),
                                **attr))
            except multinetxError:
                raise multinetxError("Multiplex cannot inherit Graph properly")
Exemplo n.º 4
0
 def layers_interconnect(self, inter_adjacency_matrix=None):
     """Parameters:
        -----------
        Examples:
        ---------
        """
     # Make a zero lil matrix for inter_adjacency_matrix
     if inter_adjacency_matrix is None:
         inter_adjacency_matrix = lil_matrix(
             zeros((self.num_nodes, self.num_nodes)))
     # Check if the matrix inter_adjacency_matrix is lil
     try:
         assert (inter_adjacency_matrix.format == "lil")
     except AssertionError:
         raise multinetxError("interconnecting_adjacency_matrix\
                              is not scipy.sparse.lil")
     for i, row in enumerate(inter_adjacency_matrix.rows):
         for pos, j in enumerate(row):
             if i > j:
                 self.inter_layer_edges.append((i, j))
     self.add_edges_from(self.inter_layer_edges)
Exemplo n.º 5
0
 def layers_interconnect(self, inter_adjacency_matrix=None):
     """Parameters:
     -----------
     Examples:
     ---------
     """
     ## Make a zero lil matrix for inter_adjacency_matrix
     if inter_adjacency_matrix is None:
        inter_adjacency_matrix = \
                    lil_matrix(zeros(
                    (self.num_nodes_in_layers*self.num_layers,
                    self.num_nodes_in_layers*self.num_layers)))
     
     ## Check if the matrix inter_adjacency_matrix is lil
     try:
         assert(inter_adjacency_matrix.format == "lil")
     except AssertionError:    
         raise multinetxError("interconnecting_adjacency_matrix "\
                              "is not scipy.sparse.lil") 
     for i,row in enumerate(inter_adjacency_matrix.rows):
         for pos,j in enumerate(row):
             if i>j:
                 self.inter_layer_edges.append((i,j))
     self.add_edges_from(self.inter_layer_edges)
Exemplo n.º 6
0
    def __init__(self, 
                list_of_layers=None, 
                inter_adjacency_matrix=None,
                **attr):
        """Constructor of a MultilayerGraph. 
        It creates a symmetric (undirected) MultilayerGraph object 
        inheriting methods from networkx.Graph
        
        Parameters:
        -----------
        list_of_layers : Python list of networkx.Graph objects
         
        inter_adjacency_matrix : a lil sparse matrix (NxN) with zero 
								 diagonal elements and off-diagonal 
								 block elements defined by the 
                                 inter-connectivity architecture.
        
        Return: a MultilayerGraph object
        
        Examples:
        ---------
        import multinetx as mx
        N = 10
		g1 = mx.erdos_renyi_graph(N,0.07,seed=218)
		g2 = mx.erdos_renyi_graph(N,0.07,seed=211)
		g3 = mx.erdos_renyi_graph(N,0.07,seed=211)
                
        adj_block = mx.lil_matrix(np.zeros((N*3,N*3)))
		adj_block[0:  N,  N:2*N] = np.identity(N)    # L_12
		adj_block[0:  N,2*N:3*N] = np.identity(N)    # L_13
		#adj_block[N:2*N,2*N:3*N] = np.identity(N)    # L_23
		adj_block += adj_block.T

		mg = mx.MultilayerGraph(list_of_layers=[g1,g2,g3], 
								inter_adjacency_matrix=adj_block)

		mg.set_edges_weights(inter_layer_edges_weight=4)
		mg.set_intra_edges_weights(layer=0,weight=1)
		mg.set_intra_edges_weights(layer=1,weight=2)
		mg.set_intra_edges_weights(layer=2,weight=3)
		
		
        """       
        ## Give an empty graph in the list_of_layers
        if list_of_layers is None:
            self.list_of_layers = [Graph()]
        else:
            self.list_of_layers = list_of_layers
        
        ## Number of layers
        self.num_layers = len(self.list_of_layers)
        
        ## Number of nodes in each layer
        self.num_nodes_in_layers = self.list_of_layers[0].number_of_nodes()       
        
        ## Create the MultilayerGraph without inter-layer links.
        try:
            Graph.__init__(self,
                        Graph(disjoint_union_all(self.list_of_layers),
                        **attr))
        except multinetxError:
            raise multinetxError("Multiplex cannot inherit Graph properly")
            
        ## Check if all graphs have the same number of nodes
        for lg in self.list_of_layers:
            try:
                assert(lg.number_of_nodes() == self.num_nodes_in_layers)
            except AssertionError:    
                raise multinetxError("Graph at layer does not have")
                                     
        
        ## Make a zero lil matrix for inter_adjacency_matrix
        if inter_adjacency_matrix is None:
           inter_adjacency_matrix = \
                       lil_matrix(zeros(
                       (self.num_nodes_in_layers*self.num_layers,
                       self.num_nodes_in_layers*self.num_layers)))
        
        ## Check if the matrix inter_adjacency_matrix is lil
        try:
            assert(inter_adjacency_matrix.format == "lil")
        except AssertionError:    
            raise multinetxError("interconnecting_adjacency_matrix "\
                                 "is not scipy.sparse.lil")         
                
        ## Lists for intra-layer and inter-layer edges
        if list_of_layers is None:
		    self.intra_layer_edges = []
        else:
		    self.intra_layer_edges = self.edges()			
        self.inter_layer_edges = []
        
        ## Inter-layer connection
        self.layers_interconnect(inter_adjacency_matrix)
    
        ## MultiNetX name
        self.name = "multilayer"
        for layer in self.list_of_layers:
            self.name += "_" + layer.name   
Exemplo n.º 7
0
    def __init__(self,
                 list_of_layers=None,
                 inter_adjacency_matrix=None,
                 **attr):
        """Constructor of a MultilayerGraph. 
        It creates a symmetric (undirected) MultilayerGraph object 
        inheriting methods from networkx.Graph
        
        Parameters:
        -----------
        list_of_layers : Python list of networkx.Graph objects
         
        inter_adjacency_matrix : a lil sparse matrix (NxN) with zero 
								 diagonal elements and off-diagonal 
								 block elements defined by the 
                                 inter-connectivity architecture.
        
        Return: a MultilayerGraph object
        
        Examples:
        ---------
        import multinetx as mx
        N = 10
		g1 = mx.erdos_renyi_graph(N,0.07,seed=218)
		g2 = mx.erdos_renyi_graph(N,0.07,seed=211)
		g3 = mx.erdos_renyi_graph(N,0.07,seed=211)
                
        adj_block = mx.lil_matrix(np.zeros((N*3,N*3)))
		adj_block[0:  N,  N:2*N] = np.identity(N)    # L_12
		adj_block[0:  N,2*N:3*N] = np.identity(N)    # L_13
		#adj_block[N:2*N,2*N:3*N] = np.identity(N)    # L_23
		adj_block += adj_block.T

		mg = mx.MultilayerGraph(list_of_layers=[g1,g2,g3], 
								inter_adjacency_matrix=adj_block)

		mg.set_edges_weights(inter_layer_edges_weight=4)
		mg.set_intra_edges_weights(layer=0,weight=1)
		mg.set_intra_edges_weights(layer=1,weight=2)
		mg.set_intra_edges_weights(layer=2,weight=3)
		
		
        """
        ## Give an empty graph in the list_of_layers
        if list_of_layers is None:
            self.list_of_layers = [Graph()]
        else:
            self.list_of_layers = list_of_layers

        ## Number of layers
        self.num_layers = len(self.list_of_layers)

        ## Number of nodes in each layer
        self.num_nodes_in_layers = self.list_of_layers[0].number_of_nodes()

        ## Create the MultilayerGraph without inter-layer links.
        try:
            Graph.__init__(
                self, Graph(disjoint_union_all(self.list_of_layers), **attr))
        except multinetxError:
            raise multinetxError("Multiplex cannot inherit Graph properly")

        ## Check if all graphs have the same number of nodes
        for lg in self.list_of_layers:
            try:
                assert (lg.number_of_nodes() == self.num_nodes_in_layers)
            except AssertionError:
                raise multinetxError("Graph at layer does not have")

        ## Make a zero lil matrix for inter_adjacency_matrix
        if inter_adjacency_matrix is None:
            inter_adjacency_matrix = \
                        lil_matrix(zeros(
                        (self.num_nodes_in_layers*self.num_layers,
                        self.num_nodes_in_layers*self.num_layers)))

        ## Check if the matrix inter_adjacency_matrix is lil
        try:
            assert (inter_adjacency_matrix.format == "lil")
        except AssertionError:
            raise multinetxError("interconnecting_adjacency_matrix "\
                                 "is not scipy.sparse.lil")

        ## Lists for intra-layer and inter-layer edges
        if list_of_layers is None:
            self.intra_layer_edges = []
        else:
            self.intra_layer_edges = self.edges()
        self.inter_layer_edges = []

        ## Inter-layer connection
        self.layers_interconnect(inter_adjacency_matrix)

        ## MultiNetX name
        self.name = "multilayer"
        for layer in self.list_of_layers:
            self.name += "_" + layer.name