Beispiel #1
0
 def test_attractors_fixed_sized(self):
     """
     ``attractors`` should raise an error if ``net`` is either a fixed sized
     network or a networkx digraph, and ``size`` is not ``None``
     """
     with self.assertRaises(ValueError):
         attractors(MockFixedSizedNetwork(), size=5)
Beispiel #2
0
 def test_attractors_variable_sized(self):
     """
     ``attractors`` should raise an error if ``net`` is a variable sized
     network and ``size`` is ``None``
     """
     with self.assertRaises(ValueError):
         attractors(ECA(30), size=None)
Beispiel #3
0
    def test_attractors_transition_graph(self):
        """
        test ``attractors`` on ``s_pombe`` transition graph
        """
        att_from_graph = attractors(transition_graph(s_pombe))
        att_from_network = attractors(s_pombe)

        for (a, b) in zip(att_from_graph, att_from_network):
            a.sort()
            b.sort()

        att_from_graph.sort()
        att_from_network.sort()

        self.assertEqual(att_from_network, att_from_graph)
Beispiel #4
0
 def test_attractors_wtnetworks(self):
     """
     test ``attractors`` on WTNetworks
     """
     networks = [(s_pombe, 13), (s_cerevisiae, 7), (c_elegans, 5)]
     for net, size in networks:
         self.assertEqual(size, len(attractors(net)))
Beispiel #5
0
    def test_complex(self):
        net = nb.WTNetwork([
            [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, -1, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
            [1, 0, -1, -1, 1, 1, 0, 0, 0, 0],
            [0, -1, 0, -1, 1, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, -1, 1, 0, 0],
            [0, 0, 0, 0, 1, 0, -1, 1, 0, 0],
            [0, 0, 0, 0, 0, 1, 0, 0, -1, -1],
            [0, 0, 0, 0, -1, 0, 0, 0, 1, 0],
        ])

        g = net.to_networkx_graph()
        self.assertEqual([[8, 9], [6, 7], [4, 5], [0, 1], [2, 3]],
                         list(map(list, nx.strongly_connected_components(g))))
        self.assertEqual([(2, 1), (2, 0), (3, 2), (4, 2)],
                         list(nx.condensation(g).edges))

        expect = ns.attractors(net)

        got = main.attractors(net)

        self.assertUnorderedEqual(expect, got)
Beispiel #6
0
 def test_attractors_eca(self):
     """
     test ``attractors`` on ECA
     """
     networks = [(ECA(30), 2, 3), (ECA(30), 3, 1), (ECA(30), 4, 4),
                 (ECA(30), 5, 2), (ECA(30), 6, 3), (ECA(110), 2, 1),
                 (ECA(110), 3, 1), (ECA(110), 4, 3), (ECA(110), 5, 1),
                 (ECA(110), 6, 3)]
     for rule, width, size in networks:
         self.assertEqual(size, len(attractors(rule, width)))
Beispiel #7
0
    def test_attractors_invalid_net(self):
        """
        ``attractors`` should raise an error if ``net`` is neither a network
        nor a networkx digraph
        """
        with self.assertRaises(TypeError):
            attractors('blah')

        with self.assertRaises(TypeError):
            attractors(MockObject())

        with self.assertRaises(TypeError):
            attractors(nx.Graph())
Beispiel #8
0
    def test_moderate_grns(self):
        k, N = 0, 0

        dex = DataDex()
        data = dex.select(['name', 'filename'],
                          ['is_biological', 'node_count <= 22'])
        for name, filename in data:
            exp_filename = os.path.join(filename, 'expressions.txt')
            ext_filename = os.path.join(filename, 'external.txt')
            net = nb.LogicNetwork.read_logic(exp_filename, ext_filename)
            g = net.to_networkx_graph()
            print()
            print("{}: {}".format(name, net.size))
            print("  Modules: {}".format(
                list(map(len, nx.strongly_connected_components(g)))))
            print("  DAG: {}".format(list(nx.condensation(g).edges())))

            start = time.time()
            got = main.attractors(net)
            stop = time.time()
            modular = stop - start
            print("  Modular Time:     {}s".format(modular))

            start = time.time()
            expected = ns.attractors(net)
            stop = time.time()
            brute_force = stop - start
            print("  Brute Force Time: {}s".format(brute_force))
            print("  Factor: {}".format(modular / brute_force))

            k += modular / brute_force
            N += 1

            self.assertUnorderedEqual(expected, got, msg=name)
            self.assertUnorderedEqual(got, expected, msg=name)

        print(k / N)
        self.assertTrue(k / N < 1.0,
                        msg='algorithm is slower than brute force on average')
Beispiel #9
0
    def test_builtins(self):
        nets = [
            s_pombe, s_cerevisiae, c_elegans, p53_dmg, p53_no_dmg,
            mouse_cortical_7B, mouse_cortical_7C, myeloid
        ]

        k, N = 0, 0
        for net in nets:
            name = net.metadata['name']
            g = net.to_networkx_graph()
            print()
            print("{}: {}".format(name, net.size))
            print("  Modules: {}".format(
                list(map(len, nx.strongly_connected_components(g)))))
            print("  DAG: {}".format(list(nx.condensation(g).edges())))

            start = time.time()
            got = main.attractors(net)
            stop = time.time()
            modular = stop - start
            print("  Modular Time:     {}s".format(modular))

            start = time.time()
            expected = ns.attractors(net)
            stop = time.time()
            brute_force = stop - start
            print("  Brute Force Time: {}s".format(brute_force))
            print("  Factor: {}".format(modular / brute_force))

            k += modular / brute_force
            N += 1

            self.assertUnorderedEqual(expected, got, msg=name)
            self.assertUnorderedEqual(got, expected, msg=name)

        self.assertTrue(k / N < 1.0,
                        msg='algorithm is slower than brute force on average')