示例#1
0
    def test_load_graph_from_text(self):
        toy_graph_snap = """#some comment string
                         #some more comment string
                         1\t2
                         1\t3
                         2\t3
                         2\t1
                         3\t1
                         3\t2"""

        toy_graph_tsv = """1\t2
                        1\t3
                        2\t3
                        2\t1
                        3\t1
                        3\t2"""
        toy_graph_csv = """1,2
                        1,3
                        2,3
                        2,1
                        3,1
                        3,2"""

        with tempfile.NamedTemporaryFile() as fsnap, tempfile.NamedTemporaryFile() as ftsv, tempfile.NamedTemporaryFile() as fcsv:
            fsnap.write(toy_graph_snap)
            fsnap.file.flush()
            ftsv.write(toy_graph_tsv)
            ftsv.file.flush()
            fcsv.write(toy_graph_csv)
            fcsv.file.flush()
            for (fname, fmt) in zip([fsnap.name, ftsv.name, fcsv.name], ['snap', 'tsv', 'csv']):
                g = load_graph('remote://' + fname, fmt)
                self.assertEqual(g.summary(), {'num_vertices': 3, 'num_edges': 6})
示例#2
0
    def test_save_load(self):
        g = SGraph().add_vertices(self.vertices,
                                  'vid').add_edges(self.edges, 'src_id',
                                                   'dst_id')
        with util.TempDirectory() as f:
            g.save(f)
            g2 = load_graph(f, 'binary')
            self.assertEqual(g2.summary(), {'num_vertices': 4, 'num_edges': 3})
            self.assertItemsEqual(
                g2.get_fields(),
                {'__id', '__src_id', '__dst_id', 'color', 'vec', 'weight'})

        with util.TempDirectory() as f:
            g.save(f, format='csv')
            vertices = SFrame.read_csv(f + "/vertices.csv")
            edges = SFrame.read_csv(f + "/edges.csv")
            g2 = SGraph().add_edges(edges, '__src_id',
                                    '__dst_id').add_vertices(vertices, '__id')
            self.assertEqual(g2.summary(), {'num_vertices': 4, 'num_edges': 3})
            self.assertItemsEqual(
                g2.get_fields(),
                {'__id', '__src_id', '__dst_id', 'color', 'vec', 'weight'})

        with tempfile.NamedTemporaryFile(suffix='.json') as f:
            g.save(f.name)
            with open(f.name, 'r') as f2:
                data = f2.read()
                g2 = json.loads(data)
            self.assertTrue("vertices" in g2)
            self.assertTrue("edges" in g2)
示例#3
0
    def test_load_graph_from_text(self):
        toy_graph_snap = """#some comment string
                         #some more comment string
                         1\t2
                         1\t3
                         2\t3
                         2\t1
                         3\t1
                         3\t2"""

        toy_graph_tsv = """1\t2
                        1\t3
                        2\t3
                        2\t1
                        3\t1
                        3\t2"""
        toy_graph_csv = """1,2
                        1,3
                        2,3
                        2,1
                        3,1
                        3,2"""

        with tempfile.NamedTemporaryFile(
        ) as fsnap, tempfile.NamedTemporaryFile(
        ) as ftsv, tempfile.NamedTemporaryFile() as fcsv:
            fsnap.write(toy_graph_snap)
            fsnap.file.flush()
            ftsv.write(toy_graph_tsv)
            ftsv.file.flush()
            fcsv.write(toy_graph_csv)
            fcsv.file.flush()
            for (fname, fmt) in zip([fsnap.name, ftsv.name, fcsv.name],
                                    ['snap', 'tsv', 'csv']):
                g = load_graph('remote://' + fname, fmt)
                self.assertEqual(g.summary(), {
                    'num_vertices': 3,
                    'num_edges': 6
                })
示例#4
0
    def test_save_load(self):
        g = SGraph().add_vertices(self.vertices, 'vid').add_edges(self.edges, 'src_id', 'dst_id')
        with util.TempDirectory() as f:
            g.save(f)
            g2 = load_graph(f, 'binary')
            self.assertEqual(g2.summary(), {'num_vertices': 4, 'num_edges': 3})
            self.assertItemsEqual(g2.get_fields(), {'__id', '__src_id', '__dst_id', 'color', 'vec', 'weight'})

        with util.TempDirectory() as f:
            g.save(f, format='csv')
            vertices = SFrame.read_csv(f + "/vertices.csv")
            edges = SFrame.read_csv(f + "/edges.csv")
            g2 = SGraph().add_edges(edges, '__src_id', '__dst_id').add_vertices(vertices, '__id')
            self.assertEqual(g2.summary(), {'num_vertices': 4, 'num_edges': 3})
            self.assertItemsEqual(g2.get_fields(), {'__id', '__src_id', '__dst_id', 'color', 'vec', 'weight'})

        with tempfile.NamedTemporaryFile(suffix='.json') as f:
            g.save(f.name)
            with open(f.name, 'r') as f2:
                data = f2.read()
                g2 = json.loads(data)
            self.assertTrue("vertices" in g2)
            self.assertTrue("edges" in g2)