Beispiel #1
0
    def add_network_from_matrix_with_pos(self, name, matrix, pos, nodeinfo_like_graph = None, \
                                         metadata = None, directed = False, hierarchical = False, hypergraph = False):
        """ Add a network to the cfil based on the given connectivity matrix and
        position information.
        
        Parameters
        ----------
        name : String
            Name of the newly added network
        matrix : NxN array
            A NxN Numpy array with the values for the edges. N must be the same
            as len(like_network.graph.nodes()). The first row of the matrix
            corresponds to the `NetworkX` node with id 'n1'
        pos : Nx3 array
            A Nx3 array with the X,Y,Z coordinates for every row (=node) of the matrix
        nodeinfo_like_graph : `NetworkX` graph
            Use the node information for the new network
        metadata : Dict
            A dictionary of metadata
        directed : bool
            Is the network directed?
        hierarchical : bool
            Is the network hierarchical? (default: '0') Not implemented yet.
        hypergraph : bool
            Is the network a hypergraph? (default: '0') Not implemented yet.
            
        Returns
        -------
        networkid : int
            The index in the list of networks in the cfile
        """
        from cviewer.plugins.cff.network import Network
        
        # create a new network object
        my_network = Network(name = name, directed = directed, hierarchical = hierarchical, \
                             hypergraph = hypergraph)
        
        if not metadata is None:
            my_network.metadata = metadata

        # add graph 
        import networkx as nx
        test_G = nx.from_numpy_matrix(matrix)
        
        # relabeling function
        addn = lambda nmod:'n'+str(nmod+1)
        
        # relabel graph
        test_G_corrected = nx.relabel_nodes(test_G, addn)

        # update the node position
        for i in range(matrix.shape[0]):
            nodeid = addn(i)
            if not nodeinfo_like_graph is None and nodeinfo_like_graph.has_node(nodeid):
                test_G_corrected.node[nodeid] = nodeinfo_like_graph.node[nodeid]
            test_G_corrected.node[nodeid]['dn_position'] = ','.join(list(map(str,pos[i, :])))

        my_network.graph = test_G_corrected
        
        # setting the correct parent cfile
        my_network._parentcfile = self


        # add the created network object to this cfile
        self.networks.append(my_network)
        
        # returns the id of the added network
        return len(self.networks)-1