File: test_motifs.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 (68 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download | duplicates (4)
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
import unittest

from igraph import Graph


class MotifTests(unittest.TestCase):
    def setUp(self):
        self.g = Graph.Erdos_Renyi(100, 0.2, directed=True)

    def testDyads(self):
        # @note: this test is not exhaustive, it only checks whether the
        # L{DyadCensus} objects "understand" attribute and item accessors
        dc = self.g.dyad_census()
        accessors = ["mut", "mutual", "asym", "asymm", "asymmetric", "null"]
        for a in accessors:
            self.assertTrue(isinstance(getattr(dc, a), int))
            self.assertTrue(isinstance(dc[a], int))
        self.assertTrue(isinstance(list(dc), list))
        self.assertTrue(isinstance(tuple(dc), tuple))
        self.assertTrue(len(list(dc)) == 3)
        self.assertTrue(len(tuple(dc)) == 3)

    def testTriads(self):
        # @note: this test is not exhaustive, it only checks whether the
        # L{TriadCensus} objects "understand" attribute and item accessors
        tc = self.g.triad_census()
        accessors = ["003", "012", "021d", "030C"]
        for a in accessors:
            self.assertTrue(isinstance(getattr(tc, "t" + a), int))
            self.assertTrue(isinstance(tc[a], int))
        self.assertTrue(isinstance(list(tc), list))
        self.assertTrue(isinstance(tuple(tc), tuple))
        self.assertTrue(len(list(tc)) == 16)
        self.assertTrue(len(tuple(tc)) == 16)


class TrianglesTests(unittest.TestCase):
    def testListTriangles(self):
        g = Graph.Famous("petersen")
        self.assertEqual([], g.list_triangles())

        g = Graph([(0, 1), (1, 2), (2, 0), (1, 3), (3, 2), (4, 2), (4, 3)])
        observed = g.list_triangles()
        self.assertTrue(all(isinstance(x, tuple) for x in observed))
        observed = sorted(sorted(tri) for tri in observed)
        expected = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
        self.assertEqual(observed, expected)

        g = Graph.GRG(100, 0.2)
        tri = Graph.Full(3)
        observed = sorted(sorted(tri) for tri in g.list_triangles())
        expected = sorted(x for x in g.get_subisomorphisms_vf2(tri) if x == sorted(x))
        self.assertEqual(observed, expected)


def suite():
    motif_suite = unittest.defaultTestLoader.loadTestsFromTestCase(MotifTests)
    triangles_suite = unittest.defaultTestLoader.loadTestsFromTestCase(TrianglesTests)
    return unittest.TestSuite([motif_suite, triangles_suite])


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


if __name__ == "__main__":
    test()