File: test_foreign.py

package info (click to toggle)
python-igraph 0.11.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,480 kB
  • sloc: ansic: 24,545; python: 21,699; sh: 107; makefile: 35; sed: 2
file content (902 lines) | stat: -rw-r--r-- 28,424 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
import gzip
import io
import unittest
import warnings

from igraph import Graph, InternalError

from .utils import temporary_file

try:
    import networkx as nx
except ImportError:
    nx = None

try:
    import graph_tool as gt
except ImportError:
    gt = None

try:
    import pandas as pd
except ImportError:
    pd = None


GRAPHML_EXAMPLE_FILE = """\
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
        http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<!-- Created by igraph -->
<key id="v_name" for="node" attr.name="name" attr.type="string"/>
<graph id="G" edgedefault="undirected">
    <node id="n0">
    <data key="v_name">a</data>
    </node>
    <node id="n1">
    <data key="v_name">b</data>
    </node>
    <node id="n2">
    <data key="v_name">c</data>
    </node>
    <node id="n3">
    <data key="v_name">d</data>
    </node>
    <node id="n4">
    <data key="v_name">e</data>
    </node>
    <node id="n5">
    <data key="v_name">f</data>
    </node>
    <edge source="n0" target="n1">
    </edge>
    <edge source="n0" target="n2">
    </edge>
    <edge source="n0" target="n3">
    </edge>
    <edge source="n1" target="n2">
    </edge>
    <edge source="n3" target="n4">
    </edge>
    <edge source="n3" target="n5">
    </edge>
    <edge source="n4" target="n5">
    </edge>
</graph>
</graphml>
"""


class ForeignTests(unittest.TestCase):
    def testDIMACS(self):
        with temporary_file(
            """\
        c
        c        This is a simple example file to demonstrate the
        c     DIMACS input file format for minimum-cost flow problems.
        c
        c problem line :
        p max 4 5
        c
        c node descriptor lines :
        n 1 s
        n 4 t
        c
        c arc descriptor lines :
        a 1 2 4
        a 1 3 2
        a 2 3 2
        a 2 4 3
        a 3 4 5
        """
        ) as tmpfname:
            graph = Graph.Read_DIMACS(tmpfname, False)
            self.assertTrue(isinstance(graph, Graph))
            self.assertTrue(graph.vcount() == 4 and graph.ecount() == 5)
            self.assertTrue(graph["source"] == 0 and graph["target"] == 3)
            self.assertTrue(graph.es["capacity"] == [4, 2, 2, 3, 5])
            graph.write_dimacs(tmpfname)

    def testDL(self):
        with temporary_file(
            """\
        dl n=5
        format = fullmatrix
        labels embedded
        data:
        larry david lin pat russ
        Larry 0 1 1 1 0
        david 1 0 0 0 1
        Lin 1 0 0 1 0
        Pat 1 0 1 0 1
        russ 0 1 0 1 0
        """
        ) as tmpfname:
            g = Graph.Read_DL(tmpfname)
            self.assertTrue(isinstance(g, Graph))
            self.assertTrue(g.vcount() == 5 and g.ecount() == 12)
            self.assertTrue(g.is_directed())
            self.assertTrue(
                sorted(g.get_edgelist())
                == [
                    (0, 1),
                    (0, 2),
                    (0, 3),
                    (1, 0),
                    (1, 4),
                    (2, 0),
                    (2, 3),
                    (3, 0),
                    (3, 2),
                    (3, 4),
                    (4, 1),
                    (4, 3),
                ]
            )

        with temporary_file(
            """\
        dl n=5
        format = fullmatrix
        labels:
        barry,david
        lin,pat
        russ
        data:
        0 1 1 1 0
        1 0 0 0 1
        1 0 0 1 0
        1 0 1 0 1
        0 1 0 1 0
        """
        ) as tmpfname:
            g = Graph.Read_DL(tmpfname)
            self.assertTrue(isinstance(g, Graph))
            self.assertTrue(g.vcount() == 5 and g.ecount() == 12)
            self.assertTrue(g.is_directed())
            self.assertTrue(
                sorted(g.get_edgelist())
                == [
                    (0, 1),
                    (0, 2),
                    (0, 3),
                    (1, 0),
                    (1, 4),
                    (2, 0),
                    (2, 3),
                    (3, 0),
                    (3, 2),
                    (3, 4),
                    (4, 1),
                    (4, 3),
                ]
            )

        with temporary_file(
            """\
        DL n=5
        format = edgelist1
        labels:
        george, sally, jim, billy, jane
        labels embedded:
        data:
        george sally 2
        george jim 3
        sally jim 4
        billy george 5
        jane jim 6
        """
        ) as tmpfname:
            g = Graph.Read_DL(tmpfname, False)
            self.assertTrue(isinstance(g, Graph))
            self.assertTrue(g.vcount() == 5 and g.ecount() == 5)
            self.assertTrue(not g.is_directed())
            self.assertTrue(
                sorted(g.get_edgelist()) == [(0, 1), (0, 2), (0, 3), (1, 2), (2, 4)]
            )

    def _testNCOLOrLGL(self, func, fname, can_be_reopened=True):
        g = func(fname, names=False, weights=False, directed=False)
        self.assertTrue(isinstance(g, Graph))
        self.assertTrue(g.vcount() == 4 and g.ecount() == 5)
        self.assertTrue(not g.is_directed())
        self.assertTrue(
            sorted(g.get_edgelist()) == [(0, 1), (0, 2), (1, 1), (1, 3), (2, 3)]
        )
        self.assertTrue(
            "name" not in g.vertex_attributes() and "weight" not in g.edge_attributes()
        )
        if not can_be_reopened:
            return

        g = func(fname, names=False, directed=False)
        self.assertTrue(
            "name" not in g.vertex_attributes() and "weight" in g.edge_attributes()
        )
        self.assertTrue(g.es["weight"] == [1, 2, 0, 3, 0])

        g = func(fname, directed=False)
        self.assertTrue(
            "name" in g.vertex_attributes() and "weight" in g.edge_attributes()
        )
        self.assertTrue(g.vs["name"] == ["eggs", "spam", "ham", "bacon"])
        self.assertTrue(g.es["weight"] == [1, 2, 0, 3, 0])

    def testNCOL(self):
        with temporary_file(
            """\
        eggs spam 1
        ham eggs 2
        ham bacon
        bacon spam 3
        spam spam"""
        ) as tmpfname:
            self._testNCOLOrLGL(func=Graph.Read_Ncol, fname=tmpfname)

        with temporary_file(
            """\
        eggs spam
        ham eggs
        ham bacon
        bacon spam
        spam spam"""
        ) as tmpfname:
            g = Graph.Read_Ncol(tmpfname)
            self.assertTrue(
                "name" in g.vertex_attributes() and "weight" not in g.edge_attributes()
            )

    @unittest.skipIf(pd is None, "test case depends on Pandas")
    def testNCOLWithDataFrame(self):
        # Regression test for https://github.com/igraph/python-igraph/issues/446
        from pandas import DataFrame

        df = DataFrame({"from": [1, 2], "to": [2, 3]})
        self.assertRaises(TypeError, Graph.Read_Ncol, df)

    def testLGL(self):
        with temporary_file(
            """\
        # eggs
        spam 1
        # ham
        eggs 2
        bacon
        # bacon
        spam 3
        # spam
        spam"""
        ) as tmpfname:
            self._testNCOLOrLGL(func=Graph.Read_Lgl, fname=tmpfname)

        with temporary_file(
            """\
        # eggs
        spam
        # ham
        eggs
        bacon
        # bacon
        spam
        # spam
        spam"""
        ) as tmpfname:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                g = Graph.Read_Lgl(tmpfname)
            self.assertTrue(
                "name" in g.vertex_attributes() and "weight" not in g.edge_attributes()
            )

        # This is not an LGL file; we are testing error handling here
        with temporary_file(
            """\
        1 2
        1 3
        """
        ) as tmpfname:
            with self.assertRaises(InternalError):
                Graph.Read_Lgl(tmpfname)

    def testLGLWithIOModule(self):
        with temporary_file(
            """\
        # eggs
        spam 1
        # ham
        eggs 2
        bacon
        # bacon
        spam 3
        # spam
        spam"""
        ) as tmpfname:
            with io.open(tmpfname, "r") as fp:
                self._testNCOLOrLGL(
                    func=Graph.Read_Lgl, fname=fp, can_be_reopened=False
                )

    def testAdjacency(self):
        with temporary_file(
            """\
        # Test comment line
        0 1 1 0 0 0
        1 0 1 0 0 0
        1 1 0 0 0 0
        0 0 0 0 2 2
        0 0 0 2 0 2
        0 0 0 2 2 0
        """
        ) as tmpfname:
            g = Graph.Read_Adjacency(tmpfname)
            self.assertTrue(isinstance(g, Graph))
            self.assertEqual(g.vcount(), 6)
            self.assertEqual(g.ecount(), 18)
            self.assertTrue(g.is_directed())
            self.assertTrue("weight" not in g.edge_attributes())

            g = Graph.Read_Adjacency(tmpfname, attribute="weight")
            self.assertTrue(isinstance(g, Graph))
            self.assertEqual(g.vcount(), 6)
            self.assertEqual(g.ecount(), 12)
            self.assertTrue(g.is_directed())
            self.assertTrue(g.es["weight"] == [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2])

            g.write_adjacency(tmpfname)

    def testGraphML(self):
        with temporary_file(GRAPHML_EXAMPLE_FILE) as tmpfname:
            try:
                g = Graph.Read_GraphML(tmpfname)
            except NotImplementedError as e:
                self.skipTest(str(e))

            self.assertTrue(isinstance(g, Graph))
            self.assertEqual(g.vcount(), 6)
            self.assertEqual(g.ecount(), 7)
            self.assertFalse(g.is_directed())
            self.assertTrue("name" in g.vertex_attributes())

            g.write_graphml(tmpfname)
            g.write_graphml(tmpfname, prefixattr=False)

    def testGraphMLz(self):
        with temporary_file(
            gzip.compress(GRAPHML_EXAMPLE_FILE.encode("utf-8"))
        ) as tmpfname:
            try:
                g = Graph.Read_GraphMLz(tmpfname)
            except NotImplementedError as e:
                self.skipTest(str(e))

            self.assertTrue(isinstance(g, Graph))
            self.assertEqual(g.vcount(), 6)
            self.assertEqual(g.ecount(), 7)
            self.assertFalse(g.is_directed())
            self.assertTrue("name" in g.vertex_attributes())

    def testPickle(self):
        pickle = [
            128,
            2,
            99,
            105,
            103,
            114,
            97,
            112,
            104,
            10,
            71,
            114,
            97,
            112,
            104,
            10,
            113,
            1,
            40,
            75,
            3,
            93,
            113,
            2,
            75,
            1,
            75,
            2,
            134,
            113,
            3,
            97,
            137,
            125,
            125,
            125,
            116,
            82,
            113,
            4,
            125,
            98,
            46,
        ]
        pickle = bytes(pickle)
        with temporary_file(pickle, "wb", binary=True) as tmpfname:
            g = Graph.Read_Pickle(pickle)
            self.assertTrue(isinstance(g, Graph))
            self.assertTrue(g.vcount() == 3 and g.ecount() == 1 and not g.is_directed())
            g.write_pickle(tmpfname)

    def testDictList(self):
        g = Graph.Full(3)

        # Check with vertex ids
        self.assertEqual(
            g.to_dict_list(),
            (
                [{}, {}, {}],
                [
                    {"source": 0, "target": 1},
                    {"source": 0, "target": 2},
                    {"source": 1, "target": 2},
                ],
            ),
        )

        # Check failure for vertex names
        self.assertRaises(AttributeError, g.to_dict_list, False)

        # Check with vertex names
        g.vs["name"] = ["apple", "pear", "peach"]
        self.assertEqual(
            g.to_dict_list(),
            (
                [{"name": "apple"}, {"name": "pear"}, {"name": "peach"}],
                [
                    {"source": 0, "target": 1},
                    {"source": 0, "target": 2},
                    {"source": 1, "target": 2},
                ],
            ),
        )
        self.assertEqual(
            g.to_dict_list(use_vids=False),
            (
                [{"name": "apple"}, {"name": "pear"}, {"name": "peach"}],
                [
                    {"source": "apple", "target": "pear"},
                    {"source": "apple", "target": "peach"},
                    {"source": "pear", "target": "peach"},
                ],
            ),
        )

    def testTupleList(self):
        g = Graph.Full(3)

        # Check with vertex ids
        self.assertEqual(
            g.to_tuple_list(),
            [(0, 1), (0, 2), (1, 2)],
        )

        # Check failure for edge names
        self.assertRaises(AttributeError, g.to_tuple_list, True, "name")

        # Edge attributes
        g.es["name"] = ["first_edge", "second", None]
        self.assertEqual(
            g.to_tuple_list(edge_attrs="name"),
            [(0, 1, "first_edge"), (0, 2, "second"), (1, 2, None)],
        )
        self.assertEqual(
            g.to_tuple_list(edge_attrs=["name"]),
            [(0, 1, "first_edge"), (0, 2, "second"), (1, 2, None)],
        )

        # Missing vertex names
        self.assertRaises(AttributeError, g.to_tuple_list, False)

        # Vertex names
        g.vs["name"] = ["apple", "pear", "peach"]
        self.assertEqual(
            g.to_tuple_list(use_vids=False, edge_attrs="name"),
            [
                ("apple", "pear", "first_edge"),
                ("apple", "peach", "second"),
                ("pear", "peach", None),
            ],
        )

    def testSequenceDict(self):
        g = Graph.Full(3)

        # Check with vertex ids
        self.assertEqual(g.to_list_dict(), {0: [1, 2], 1: [2]})
        self.assertEqual(
            g.to_list_dict(sequence_constructor=tuple),
            {0: (1, 2), 1: (2,)},
        )

        # Check failure for vertex names
        self.assertRaises(AttributeError, g.to_list_dict, False)

        # Check with vertex names
        g.vs["name"] = ["apple", "pear", "peach"]
        self.assertEqual(
            g.to_list_dict(use_vids=False),
            {"apple": ["pear", "peach"], "pear": ["peach"]},
        )

    def testDictDict(self):
        g = Graph([(0, 1), (0, 2), (1, 2)])

        # Check with vertex ids, no edge attrs
        self.assertEqual(
            g.to_dict_dict(),
            {0: {1: {}, 2: {}}, 1: {2: {}}},
        )

        # With vertex ids, edge attrs
        g.es["name"] = ["first_edge", "second", None]
        # Check with vertex ids, incomplete edge attrs
        self.assertEqual(
            g.to_dict_dict(),
            {
                0: {1: {"name": "first_edge"}, 2: {"name": "second"}},
                1: {2: {"name": None}},
            },
        )
        self.assertEqual(
            g.to_dict_dict(skip_none=True),
            {0: {1: {"name": "first_edge"}, 2: {"name": "second"}}, 1: {2: {}}},
        )

        # With vertex names
        g.vs["name"] = ["apple", "pear", "peach"]
        self.assertEqual(
            g.to_dict_dict(use_vids=False),
            {
                "apple": {"pear": {"name": "first_edge"}, "peach": {"name": "second"}},
                "pear": {"peach": {"name": None}},
            },
        )
        self.assertEqual(
            g.to_dict_dict(use_vids=False, skip_none=True),
            {
                "apple": {"pear": {"name": "first_edge"}, "peach": {"name": "second"}},
                "pear": {"peach": {}},
            },
        )

    @unittest.skipIf(pd is None, "test case depends on Pandas")
    def testVertexDataFrames(self):
        g = Graph([(0, 1), (0, 2), (0, 3), (1, 2), (2, 4)])

        # No vertex names, no attributes
        df = g.get_vertex_dataframe()
        self.assertEqual(df.shape, (5, 0))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])

        # Vertex names, no attributes
        g.vs["name"] = ["eggs", "spam", "ham", "bacon", "yello"]
        df = g.get_vertex_dataframe()
        self.assertEqual(df.shape, (5, 1))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])
        self.assertEqual(list(df["name"]), g.vs["name"])
        self.assertEqual(list(df.columns), ["name"])

        # Vertex names and attributes (common case)
        g.vs["weight"] = [0, 5, 1, 4, 42]
        df = g.get_vertex_dataframe()
        self.assertEqual(df.shape, (5, 2))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])
        self.assertEqual(list(df["name"]), g.vs["name"])
        self.assertEqual(set(df.columns), {"name", "weight"})
        self.assertEqual(list(df["weight"]), g.vs["weight"])

        # No vertex names, with attributes (common case)
        g = Graph([(0, 1), (0, 2), (0, 3), (1, 2), (2, 4)])
        g.vs["weight"] = [0, 5, 1, 4, 42]
        df = g.get_vertex_dataframe()
        self.assertEqual(df.shape, (5, 1))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])
        self.assertEqual(list(df.columns), ["weight"])
        self.assertEqual(list(df["weight"]), g.vs["weight"])

    @unittest.skipIf(pd is None, "test case depends on Pandas")
    def testEdgeDataFrames(self):
        g = Graph([(0, 1), (0, 2), (0, 3), (1, 2), (2, 4)])

        # No edge names, no attributes
        df = g.get_edge_dataframe()
        self.assertEqual(df.shape, (5, 2))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])
        self.assertEqual(list(df.columns), ["source", "target"])

        # Edge names, no attributes
        g.es["name"] = ["my", "list", "of", "five", "edges"]
        df = g.get_edge_dataframe()
        self.assertEqual(df.shape, (5, 3))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])
        self.assertEqual(list(df["name"]), g.es["name"])
        self.assertEqual(set(df.columns), {"source", "target", "name"})

        # No edge names, with attributes
        g = Graph([(0, 1), (0, 2), (0, 3), (1, 2), (2, 4)])
        g.es["weight"] = [6, -0.4, 0, 1, 3]
        df = g.get_edge_dataframe()
        self.assertEqual(df.shape, (5, 3))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])
        self.assertEqual(set(df.columns), {"source", "target", "weight"})
        self.assertEqual(list(df["weight"]), g.es["weight"])

        # Edge names, with weird attributes
        g.es["name"] = ["my", "list", "of", "five", "edges"]
        g.es["weight"] = [6, -0.4, 0, 1, 3]
        g.es["source"] = ["this", "is", "a", "little", "tricky"]
        df = g.get_edge_dataframe()
        self.assertEqual(df.shape, (5, 5))
        self.assertEqual(list(df.index), [0, 1, 2, 3, 4])
        self.assertEqual(set(df.columns), {"source", "target", "name", "weight"})
        self.assertEqual(list(df["name"]), g.es["name"])
        self.assertEqual(list(df["weight"]), g.es["weight"])

        i = 2 + list(df.columns[2:]).index("source")
        self.assertEqual(list(df.iloc[:, i]), g.es["source"])

    @unittest.skipIf(nx is None, "test case depends on networkx")
    def testGraphNetworkx(self):
        # Undirected
        g = Graph.Ring(10)
        g["gattr"] = "graph_attribute"
        g.vs["vattr"] = list(range(g.vcount()))
        g.es["eattr"] = list(range(len(g.es)))

        # Go to networkx and back
        g_nx = g.to_networkx()
        g2 = Graph.from_networkx(g_nx)

        self.assertFalse(g2.is_directed())
        self.assertTrue(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))

        # Test attributes
        self.assertEqual(g.attributes(), g2.attributes())
        self.assertEqual(sorted(["vattr", "_nx_name"]), sorted(g2.vertex_attributes()))
        for i, vertex in enumerate(g.vs):
            vertex2 = g2.vs[i]
            for an in vertex.attribute_names():
                if an == "vattr":
                    continue
                self.assertEqual(vertex.attributes()[an], vertex2.attributes()[an])
        self.assertEqual(g.edge_attributes(), g2.edge_attributes())
        for edge in g.es:
            eid = g2.get_eid(edge.source, edge.target)
            edge2 = g2.es[eid]
            for an in edge.attribute_names():
                self.assertEqual(edge.attributes()[an], edge2.attributes()[an])

        # Directed
        g = Graph.Ring(10, directed=True)

        # Go to networkx and back
        g_nx = g.to_networkx()
        g2 = Graph.from_networkx(g_nx)

        self.assertTrue(g2.is_directed())
        self.assertTrue(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))

        # Test networkx with custom node hashables
        # In this case each node is a tuple of ints
        g_nx = nx.DiGraph()
        inf = float("inf")
        g_nx.add_edges_from(
            (
                ((0, 0), (1, 1), {"weight": 3.0}),
                ((0, 0), (1, 0), {"weight": inf}),
            )
        )
        g = Graph.from_networkx(g_nx)
        self.assertTrue(g.is_directed())
        self.assertEqual(g.vcount(), 3)
        self.assertTrue(g.es["weight"] == [3.0, inf] or g.es["weight"] == [inf, 3.0])

    @unittest.skipIf(nx is None, "test case depends on networkx")
    def testMultigraphNetworkx(self):
        # Undirected
        g = Graph.Ring(10)
        g.add_edge(0, 1)
        g["gattr"] = "graph_attribute"
        g.vs["vattr"] = list(range(g.vcount()))
        g.es["eattr"] = list(range(len(g.es)))

        # Go to networkx and back
        g_nx = g.to_networkx()
        g2 = Graph.from_networkx(g_nx)

        self.assertFalse(g2.is_directed())
        self.assertFalse(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))

        # Test attributes
        self.assertEqual(g.attributes(), g2.attributes())
        self.assertEqual(
            sorted(["vattr", "_nx_name"]),
            sorted(g2.vertex_attributes()),
        )
        self.assertEqual(
            sorted(["eattr", "_nx_multiedge_key"]),
            sorted(g2.edge_attributes()),
        )

        # Testing parallel edges is a bit more tricky
        edge2_found = set()
        for edge in g.es:
            # Go through all parallel edges between these two vertices
            for edge2 in g2.es:
                if edge2 in edge2_found:
                    continue
                if edge.source != edge2.source:
                    continue
                if edge.target != edge2.target:
                    continue
                # Check all attributes between these two
                for an in edge.attribute_names():
                    if edge.attributes()[an] != edge2.attributes()[an]:
                        break
                else:
                    # Correspondence found
                    edge2_found.add(edge2)
                    break

            else:
                self.assertTrue(False)

        # Directed
        g = Graph.Ring(10, directed=True)
        g.add_edge(0, 1)

        # Go to networkx and back
        g_nx = g.to_networkx()
        g2 = Graph.from_networkx(g_nx)

        self.assertTrue(g2.is_directed())
        self.assertFalse(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))

    @unittest.skipIf(gt is None, "test case depends on graph-tool")
    def testGraphGraphTool(self):
        # Undirected
        g = Graph.Ring(10)
        g["gattr"] = "graph_attribute"
        g.vs["vattr"] = list(range(g.vcount()))
        g.es["eattr"] = list(range(len(g.es)))

        # Go to graph-tool and back
        g_gt = g.to_graph_tool(
            graph_attributes={"gattr": "object"},
            vertex_attributes={"vattr": "int"},
            edge_attributes={"eattr": "int"},
        )
        g2 = Graph.from_graph_tool(g_gt)

        self.assertFalse(g2.is_directed())
        self.assertTrue(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))

        # Test attributes
        self.assertEqual(g.attributes(), g2.attributes())
        self.assertEqual(g.vertex_attributes(), g2.vertex_attributes())
        for i, vertex in enumerate(g.vs):
            vertex2 = g2.vs[i]
            for an in vertex.attribute_names():
                self.assertEqual(vertex.attributes()[an], vertex2.attributes()[an])
        self.assertEqual(g.edge_attributes(), g2.edge_attributes())
        for edge in g.es:
            eid = g2.get_eid(edge.source, edge.target)
            edge2 = g2.es[eid]
            for an in edge.attribute_names():
                self.assertEqual(edge.attributes()[an], edge2.attributes()[an])

        # Directed
        g = Graph.Ring(10, directed=True)

        # Go to graph-tool and back
        g_gt = g.to_graph_tool()

        g2 = Graph.from_graph_tool(g_gt)

        self.assertTrue(g2.is_directed())
        self.assertTrue(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))

    @unittest.skipIf(gt is None, "test case depends on graph-tool")
    def testMultigraphGraphTool(self):
        # Undirected
        g = Graph.Ring(10)
        g.add_edge(0, 1)
        g["gattr"] = "graph_attribute"
        g.vs["vattr"] = list(range(g.vcount()))
        g.es["eattr"] = list(range(len(g.es)))

        # Go to graph-tool and back
        g_gt = g.to_graph_tool(
            graph_attributes={"gattr": "object"},
            vertex_attributes={"vattr": "int"},
            edge_attributes={"eattr": "int"},
        )
        g2 = Graph.from_graph_tool(g_gt)

        self.assertFalse(g2.is_directed())
        self.assertFalse(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))

        # Test attributes
        self.assertEqual(g.attributes(), g2.attributes())
        self.assertEqual(g.vertex_attributes(), g2.vertex_attributes())
        for i, vertex in enumerate(g.vs):
            vertex2 = g2.vs[i]
            for an in vertex.attribute_names():
                self.assertEqual(vertex.attributes()[an], vertex2.attributes()[an])
        self.assertEqual(g.edge_attributes(), g2.edge_attributes())
        # Testing parallel edges is a bit more tricky
        edge2_found = set()
        for edge in g.es:
            # Go through all parallel edges between these two vertices
            for edge2 in g2.es:
                if edge2 in edge2_found:
                    continue
                if edge.source != edge2.source:
                    continue
                if edge.target != edge2.target:
                    continue
                # Check all attributes between these two
                for an in edge.attribute_names():
                    if edge.attributes()[an] != edge2.attributes()[an]:
                        break
                else:
                    # Correspondence found
                    edge2_found.add(edge2)
                    break

            else:
                self.assertTrue(False)

        # Directed
        g = Graph.Ring(10, directed=True)
        g.add_edge(0, 1)

        # Go to graph-tool and back
        g_gt = g.to_graph_tool()
        g2 = Graph.from_graph_tool(g_gt)

        self.assertTrue(g2.is_directed())
        self.assertFalse(g2.is_simple())
        self.assertEqual(g.vcount(), g2.vcount())
        self.assertEqual(sorted(g.get_edgelist()), sorted(g2.get_edgelist()))


def suite():
    foreign_suite = unittest.defaultTestLoader.loadTestsFromTestCase(ForeignTests)
    return unittest.TestSuite([foreign_suite])


def test():
    runner = unittest.TextTestRunner()
    runner.run(suite())


if __name__ == "__main__":
    test()