Repository: spacegraphcats/spacegraphcats

Issue:
Parser should make sure that ids are consecutive
The parser should ensure that ids are consecutive so that the graph can use a more efficient list for the vertices. For this to work, we also need to return a list that maps back to the original ids. 

We could make the list more efficient if there is a constant offset. 

Comments:

Patch:
diff --git a/spacegraphcats/graph_parser.py b/spacegraphcats/graph_parser.py
index 740e811..9977400 100644
--- a/spacegraphcats/graph_parser.py
+++ b/spacegraphcats/graph_parser.py
@@ -11,16 +11,33 @@ def _mapstr(items):
     return list(map(str, items))
 
 
-def parse(graph_file, add_vertex=None, add_edge=None):
+def parse(graph_file, add_vertex=None, add_edge=None, consecutive_ids=False):
     """Parse a graph and call provided methods with vertices and edges."""
     # read vertices
     vertex_attributes = _parse_line(graph_file.readline())[2:]
 
+    # consecutive id to original id
+    id_map = []
+    # original id to consecutive id
+    id_map_reverse = {}
+
+    def _get_consecutive_id(id):
+        if not consecutive_ids:
+            return id
+
+        if id in id_map_reverse:
+            return id_map_reverse[id]
+        else:
+            consecutive_id = len(id_map)
+            id_map_reverse[id] = consecutive_id
+            id_map.append(id)
+            return consecutive_id
+
     next_line = graph_file.readline()
     while len(next_line) > 1:
         if add_vertex is not None:
             parsed = _parse_line(next_line)
-            add_vertex(int(parsed[0]), int(parsed[1]),
+            add_vertex(_get_consecutive_id(int(parsed[0])), int(parsed[1]),
                        vertex_attributes, parsed[2:])
         next_line = graph_file.readline()
 
@@ -34,10 +51,12 @@ def parse(graph_file, add_vertex=None, add_edge=None):
     next_line = graph_file.readline()
     while len(next_line) > 1:
         parsed = _parse_line(next_line)
-        add_edge(int(parsed[0]), int(parsed[1]),
+        add_edge(_get_consecutive_id(int(parsed[0])), _get_consecutive_id(int(parsed[1])),
                  edge_attributes, parsed[2:])
         next_line = graph_file.readline()
 
+    return id_map
+
 
 def parse_minhash(minhash_file, add_minhash):
     """Parse minhash (.mxt) file."""


Test patch:
diff --git a/spacegraphcats/test_parser.py b/spacegraphcats/test_parser.py
index ea58823..f03a864 100644
--- a/spacegraphcats/test_parser.py
+++ b/spacegraphcats/test_parser.py
@@ -33,18 +33,20 @@ class ParserTest(unittest.TestCase):
             all_values_e.append(values)
 
         with open(os.path.join(DIR, 'parser-examples/graph.gxt')) as f:
-            self.p = parser.parse(f, collect_vertex, collect_edge)
+            id_map = parser.parse(f, collect_vertex, collect_edge, True)
 
-        self.assertEqual(all_ids, [1, 2, 3])
+        self.assertEqual(all_ids, [0, 1, 2])
         self.assertEqual(all_sizes, [2, 3, 1])
         self.assertEqual(all_names_v, [['label', 'fill'], ['label', 'fill'], ['label', 'fill']])
         self.assertEqual(all_values_v, [['foo', 'red'], ['bar', 'green'], ['batman', 'black']])
 
-        self.assertEqual(all_srcs, [1, 2])
-        self.assertEqual(all_dests, [2, 3])
+        self.assertEqual(all_srcs, [0, 1])
+        self.assertEqual(all_dests, [1, 2])
         self.assertEqual(all_names_e, [['label'], ['label']])
         self.assertEqual(all_values_e, [['a'], ['b']])
 
+        self.assertEqual(id_map, [1, 2, 3])
+
     def test_mxt_parsing(self):
         minhashes = {}