Changeset 7971:b8643ea6aed5 in orange


Ignore:
Timestamp:
06/02/11 23:07:51 (3 years ago)
Author:
miha <miha.stajdohar@…>
Branch:
default
Convert:
310720f7352a12af75b76333cb3351c67337237e
Message:

Added to_orange_network method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/network/network.py

    r7963 r7971  
     1import copy 
    12import math 
    23import numpy 
     
    5859         
    5960        self._links = links 
     61         
     62    def to_orange_network(self): 
     63        """Convert the network to Orange NetworkX standard. All node IDs are transformed to range [0, no_of_nodes - 1]."""  
     64        if isinstance(self, Orange.network.Graph): 
     65            G = Orange.network.Graph() 
     66        elif isinstance(self, Orange.network.DiGraph): 
     67            G = Orange.network.DiGraph() 
     68        elif isinstance(self, Orange.network.MultiGraph): 
     69            G = Orange.network.MultiGraph() 
     70        elif isinstance(self, Orange.network.MultiDiGraph): 
     71            G = Orange.network.DiGraph() 
     72        else: 
     73            raise TypeError('WTF!?') 
     74         
     75        node_list = sorted(self.nodes()) 
     76        node_to_index = dict(zip(node_list, range(self.number_of_nodes()))) 
     77        index_to_node = dict(zip(range(self.number_of_nodes()), node_list)) 
     78         
     79        G.add_nodes_from(zip(range(self.number_of_nodes()), [copy.deepcopy(self.node[nid]) for nid in node_list])) 
     80        G.add_edges_from(((node_to_index[u], node_to_index[v], copy.deepcopy(self.edge[u][v])) for u,v in self.edges())) 
     81         
     82        for id in G.node.keys(): 
     83            G.node[id]['old_id'] = index_to_node[id] 
     84         
     85        if self.items(): 
     86            G.set_items(self.items()) 
     87 
     88        if self.links(): 
     89            G.set_links(self.links()) 
     90         
     91        return G 
    6092         
    6193    ### TODO: OVERRIDE METHODS THAT CHANGE GRAPH STRUCTURE, add warning prints 
Note: See TracChangeset for help on using the changeset viewer.