ziggy.GraphReduce.GraphLoader

Package ziggy :: Package GraphReduce :: Module GraphLoader |
[hide private]
[frames] | no frames]

# Source Code for Module ziggy.GraphReduce.GraphLoader

     1  '''
     2  Created on Aug 11, 2010
     3
     4  @author: dwmclary
     5  '''
     6  import networkx as nx
     7  from .. hdmc import hdfs
     8 -class GraphLoader():
     9      '''
    10      Provides graph handles in Hadoop MapReduce
    11      '''
    12
    13
    14 -    def __init__(self, G = None, graph_handle=None):
    15          '''
    16          Constructor
    17          '''
    18
    19          if G != None:
    20              self.G = G
    21          #sets a string pointing to an flat-file adjacency list
    22          if graph_handle != None:
    23              self.graph_handle = graph_handle
    24
    25 -    def read_edgelist(self, filename):
    26          self.G = nx.read_edgelist(filename)
    27
    28 -    def read_adjlist(self, filename, undirected=True):
    29          if undirected:
    30              self.G = nx.read_adjlist(filename).to_undirected()
    31          else:
    32              self.G = nx.read_adjlist(filename)
    33          self.graph_handle = filename
    34
    35 -    def write_adjlist(self, filename=None):
    36          if filename:
    37              nx.write_adjlist(self.G.to_directed(), filename)
    38              self.graph_handle = filename
    39              hdfs.copyToHDFS(filename, filename)
    40          else:
    41              if self.graph_handle:
    42                  nx.write_adjlist(self.G.to_directed(), self.graph_handle)
    43
    44 -    def write_edgelist(self, filename):
    45          nx.write_edgelist(self.G,filename)
    46
    47 -    def in_hdfs(self):
    48          listing = hdfs.ls(self.graph_handle)["stdout"].split("\n")
    49          found = False
    50          disk_tail = self.graph_handle.split("/")[-1]
    51          for line in listing:
    52              hdfs_tail = line.split("/")[-1]
    53              if disk_tail == hdfs_tail:
    54                  found = True
    55                  break
    56          return found
    57