Changeset 7968:6e5e4c60311e in orange


Ignore:
Timestamp:
06/02/11 13:17:00 (3 years ago)
Author:
miha <miha.stajdohar@…>
Branch:
default
Convert:
5384207d8ccefa492038a2b8a298f2207ccb25df
Message:

Read / write NetworkX network from Python pickle (.gpickle).

File:
1 edited

Legend:

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

    r7966 r7968  
    2424def read(path, encoding='UTF-8'): 
    2525    #supported = ['.net', '.gml', '.gpickle', '.gz', '.bz2', '.graphml'] 
    26     supported = ['.net', '.gml'] 
     26    supported = ['.net', '.gml', '.gpickle'] 
    2727     
    2828    if not os.path.isfile(path): 
     
    3838    if ext == '.gml': 
    3939        return read_gml(path, encoding) 
     40     
     41    if ext == '.gpickle': 
     42        return read_gpickle(path) 
    4043 
    4144def write(G, path, encoding='UTF-8'): 
    4245    #supported = ['.net', '.gml', '.gpickle', '.gz', '.bz2', '.graphml'] 
    43     supported = ['.net', '.gml'] 
     46    supported = ['.net', '.gml', '.gpickle'] 
    4447     
    4548    root, ext = os.path.splitext(path) 
     
    5356        write_gml(G, path) 
    5457         
     58    if ext == '.gpickle': 
     59        write_gpickle(G, path) 
     60         
    5561    if G.items() is not None: 
    5662        G.items().save(root + '_items.tab') 
     
    5864    if G.links() is not None: 
    5965        G.links().save(root + '_links.tab') 
     66 
     67def read_gpickle(path): 
     68    return _wrap(rw.read_gpickle(path)) 
     69 
     70def write_gpickle(G, path): 
     71    rw.write_gpickle(G, path) 
    6072 
    6173def read_pajek(path, encoding='UTF-8'): 
     
    6779        # directed graph 
    6880        G = Orange.network.DiGraph() 
     81        G.add_nodes_from(range(len(items))) 
    6982        G.add_edges_from(((u,v,{'weight':d}) for u,v,d in edges)) 
    7083        G.add_edges_from(((v,u,{'weight':d}) for u,v,d in edges)) 
     
    7386    else: 
    7487        G = Orange.network.Graph() 
     88        G.add_nodes_from(range(len(items))) 
    7589        G.add_edges_from(((u,v,{'weight':d}) for u,v,d in edges)) 
    7690        G.set_items(items) 
     
    8094    #lines = (line.decode(encoding) for line in fh) 
    8195    #return parse_pajek(lines) 
     96 
     97def write_pajek(G, path, encoding='UTF-8'): 
     98    """ 
     99    A copy&paste of networkx's function with some bugs fixed: 
     100     - call the new generate_pajek. 
     101    """ 
     102    fh=_get_fh(path, 'wb') 
     103    for line in generate_pajek(G): 
     104        line+='\n' 
     105        fh.write(line.encode(encoding)) 
    82106 
    83107def parse_pajek(lines): 
     
    137161        yield s 
    138162         
    139 def write_pajek(G, path, encoding='UTF-8'): 
    140     """ 
    141     A copy&paste of networkx's function with some bugs fixed: 
    142      - call the new generate_pajek. 
    143     """ 
    144     fh=_get_fh(path, 'wb') 
    145     for line in generate_pajek(G): 
    146         line+='\n' 
    147         fh.write(line.encode(encoding)) 
    148  
    149 def parse_pajek_project(lines): 
    150     network_lines = [] 
    151     result = [] 
    152     for i, line in enumerate(itertools.chain(lines, ["*"])): 
    153         line_low = line.strip().lower() 
    154         if not line_low: 
    155             continue 
    156         if line_low[0] == "*" and not any(line_low.startswith(x) 
    157                                           for x in ["*vertices", "*arcs", "*edges"]): 
    158             if network_lines != []: 
    159                 result.append(parse_pajek(network_lines)) 
    160                 network_lines = [] 
    161         if line_low.startswith("*network") or network_lines != []: 
    162             network_lines.append(line) 
    163     return result 
    164  
    165 def read_pajek_project(path, encoding='UTF-8'): 
    166     fh = _get_fh(path, 'rb') 
    167     lines = (line.decode(encoding) for line in fh) 
    168     return parse_pajek_project(lines) 
    169  
    170163def read_gml(path, encoding='latin-1', relabel=False): 
    171     G = _wrap(rw.read_gml(path, encoding, relabel)) 
    172     return G 
     164    return _wrap(rw.read_gml(path, encoding, relabel)) 
    173165 
    174166def write_gml(G, path): 
    175167    rw.write_gml(G, path) 
    176168 
    177  
    178169#read_pajek.__doc__ = rw.read_pajek.__doc__ 
    179170#parse_pajek.__doc__ = rw.parse_pajek.__doc__ 
Note: See TracChangeset for help on using the changeset viewer.