ziggy.GraphReduce.Degree_reducer

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

# Source Code for Module ziggy.GraphReduce.Degree_reducer

     1  #! /usr/bin/env python
     2  '''
     3  Created on Aug 25, 2010
     4
     5  @author: dwmclary
     6  '''
     7  import sys
     8
     9 -class DegreeNode(object):
    10 -    def __init__(self, nID=None, in_degree=None, adj=None, out_degree=None):
    11          if nID != None:
    12              self.nID = nID
    13              self.in_degree = in_degree
    14              self.adj = adj
    15              self.out_degree = out_degree
    16
    17 -    def write(self):
    18          s = str(self.nID)+  " "+ " ".join(map(str,self.adj)) +\
    19          " in: "+" ".join(map(str, self.in_degree)) + " out: " + " ".join(map(str,self.out_degree))
    20          return s
    21
    22 -    def __str__(self):
    23          return self.write()
    24
    25 -def read_map_output(file, sep=None):
    26      for line in file:
    27          line = line.rstrip().split()
    28          nid = line[0]
    29
    30          if not "indegree:" in line:
    31              type = "node"
    32              d_index = line.index("in:")
    33              p_index = line.index("out:")
    34              in_degree = line[d_index+1:p_index]
    35              out_degree = line[p_index+1:]
    36              adj = line[1:d_index]
    37              node = DegreeNode(nid, in_degree, adj, out_degree)
    38              yield type, nid, node
    39          else:
    40              type = "degree"
    41              d_index = line.index("indegree:")
    42              in_degree = line[d_index+1]
    43              yield type, nid, in_degree
    44
    45 -def main():
    46      in_degrees = {}
    47      M = {}
    48      changed = 0
    49      data = read_map_output(sys.stdin)
    50      for type, key, value in data:
    51          if type == "degree":
    52              if key in in_degrees:
    53                  in_degrees[key].append(value)
    54
    55              else:
    56                  in_degrees[key] = [value]
    57          elif type == "node":
    58              M[key] = value
    59      for key in M.keys():
    60          if key in in_degrees:
    61              degree_before = len(M[key].in_degree)
    62              M[key].in_degree = set(in_degrees[key]).union(M[key].in_degree)
    63              degree_after = len(M[key].in_degree)
    64              if degree_before != degree_after:
    65                  changed +=1
    66          print M[key]
    67      print "#Changed:"+str(changed)
    68
    69
    70  if __name__ == "__main__":
    71      try:
    72          import psyco
    73          psyco.full()
    74      except ImportError:
    75          pass
    76      main()
    77