Changeset 8783:afac174fb1f7 in orange


Ignore:
Timestamp:
08/25/11 16:38:03 (3 years ago)
Author:
miha <miha.stajdohar@…>
Branch:
default
Convert:
1837a8248e82f8ecd983517fbb23ff8a63fbbe0b
Message:

Build data table automatically from the network file.

File:
1 edited

Legend:

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

    r8212 r8783  
    1717""" 
    1818 
     19import os 
    1920import os.path 
    2021import warnings 
    2122import itertools 
     23import tempfile 
    2224 
    2325import networkx as nx 
     
    8587    return str(t) 
    8688 
    87 def read(path, encoding='UTF-8'): 
     89def graph_to_table(G): 
     90    """Builds a Data Table from node values.""" 
     91    if G.number_of_nodes() > 0: 
     92        features = list(set(itertools.chain.from_iterable(node.iterkeys() for node in G.node.itervalues()))) 
     93        data = [[node.get(f).replace('\t', ' ') if type(node.get(f, 1)) == str else str(node.get(f, '?')) for f in features] for node in G.node.itervalues()] 
     94        fp = tempfile.NamedTemporaryFile('wt', suffix='.txt', delete=False) 
     95        fp.write('\n'.join('\t'.join(line) for line in [features] + data)) 
     96        fp.close() 
     97        table = Orange.data.Table(fp.name) 
     98        os.unlink(fp.name) 
     99         
     100    return table 
     101 
     102def read(path, encoding='UTF-8', auto_table=0): 
    88103    """Read graph in any of the supported file formats (.gpickle, .net, .gml). 
    89104    The parser is chosen based on the file extension. 
     
    109124     
    110125    if ext == '.net': 
    111         return read_pajek(path, encoding) 
     126        return read_pajek(path, encoding, auto_table=auto_table) 
    112127     
    113128    if ext == '.gml': 
    114         return read_gml(path, encoding) 
     129        return read_gml(path, encoding, auto_table=auto_table) 
    115130     
    116131    if ext == '.gpickle': 
    117         return read_gpickle(path) 
     132        return read_gpickle(path, auto_table=auto_table) 
    118133 
    119134def write(G, path, encoding='UTF-8'): 
     
    151166        G.links().save(root + '_links.tab') 
    152167 
    153 def read_gpickle(path): 
     168def read_gpickle(path, auto_table=False): 
    154169    """NetworkX read_gpickle method and wrap graph to Orange network. 
    155170     
    156171    """ 
    157172     
    158     return _wrap(rwgpickle.read_gpickle(path)) 
     173    G = _wrap(rwgpickle.read_gpickle(path)) 
     174    if auto_table: 
     175        G.set_items(graph_to_table(G)) 
     176    return G 
    159177 
    160178_add_doc(read_gpickle, rwgpickle.read_gpickle) 
     
    169187_add_doc(write_gpickle, rwgpickle.write_gpickle) 
    170188 
    171 def read_pajek(path, encoding='UTF-8', project=False): 
    172     """A completely reimplemented method for reading Pajek files. Written in  
     189def read_pajek(path, encoding='UTF-8', project=False, auto_table=False): 
     190    """A completely reimplemented Unhandled exception of type KeyError occured at 13:45:31: 
     191Traceback: 
     192  File: orngSignalManager.py, line 555 in processNewSignals 
     193  Code: self.widgets[i].processSignals() 
     194    File: OWBaseWidget.py, line 686 in processSignals 
     195    Code: self.handleNewSignals() 
     196      File: OWScatterPlot3D.py, line 616 in handleNewSignals 
     197      Code: self.update_plot() 
     198        File: OWScatterPlot3D.py, line 671 in update_plot 
     199        Code: self.label_attr) 
     200          File: OWScatterPlot3D.py, line 97 in update_data 
     201          Code: x_index = self.attribute_name_index[x_attr] 
     202            KeyError: 8  
     203method for reading Pajek files. Written in  
    173204    C++ for maximum performance.   
    174205     
     
    217248            G.add_edges_from(((v,u,dict(d.items()+[('weight',w)])) for u,v,w,d in edges)) 
    218249            G.add_edges_from(((u,v,dict(d.items()+[('weight',w)])) for u,v,w,d in arcs)) 
    219             G.set_items(items) 
     250            if auto_table: 
     251                G.set_items(items) 
    220252        else: 
    221253            G = Orange.network.Graph() 
    222254            G.add_nodes_from(range(len(items))) 
    223255            G.add_edges_from(((u,v,dict(d.items()+[('weight',w)])) for u,v,w,d in edges)) 
    224             G.set_items(items) 
     256            if auto_table: 
     257                G.set_items(items) 
    225258        for i, vdata in zip(range(len(G.node)), vertices): 
    226259            G.node[i].update(vdata) 
     
    323356#_add_doc(generate_pajek, rwpajek.generate_pajek) 
    324357         
    325 def read_gml(path, encoding='latin-1', relabel=False): 
     358def read_gml(path, encoding='latin-1', relabel=False, auto_table=False): 
    326359    """NetworkX read_gml method and wrap graph to Orange network. 
    327360     
    328361    """ 
    329      
    330     return _wrap(rwgml.read_gml(path, encoding, relabel)) 
     362    G = _wrap(rwgml.read_gml(path, encoding, relabel)) 
     363    if auto_table: 
     364        G.set_items(graph_to_table(G)) 
     365    return G 
    331366 
    332367_add_doc(read_gml, rwgml.read_gml) 
Note: See TracChangeset for help on using the changeset viewer.