Exemplo n.º 1
0
    def test_landscape_data(self):
        net = ECA(30, size=5)
        data_before_setup = net.landscape_data
        self.assertEqual(data_before_setup.__dict__, {})

        data_after_setup = net.landscape().landscape_data
        self.assertEqual(data_before_setup.__dict__, {})
        self.assertEqual(
            list(data_after_setup.__dict__.keys()), ['transitions'])

        data_after_expound = net.expound().landscape_data
        self.assertEqual(data_before_setup.__dict__, {})
        self.assertEqual(set(data_after_setup.__dict__.keys()), set([
            "transitions",
            "basins",
            "basin_sizes",
            "attractors",
            "attractor_lengths",
            "in_degrees",
            "heights",
            "recurrence_times",
            "basin_entropy"
        ]))
        self.assertEqual(set(data_after_expound.__dict__.keys()), set([
            "transitions",
            "basins",
            "basin_sizes",
            "attractors",
            "attractor_lengths",
            "in_degrees",
            "heights",
            "recurrence_times",
            "basin_entropy"
        ]))
Exemplo n.º 2
0
    def test_fail_init(self):
        with self.assertRaises(ValueError):
            ECA(-1, 5)

        with self.assertRaises(ValueError):
            ECA(256, 5)

        with self.assertRaises(TypeError):
            ECA([1, 1, 0, 1, 1, 0, 0, 1])

        with self.assertRaises(TypeError):
            ECA("30", 5)

        with self.assertRaises(TypeError):
            ECA(30, "5")

        with self.assertRaises(ValueError):
            ECA(30, -1)

        with self.assertRaises(ValueError):
            ECA(30, 0)

        with self.assertRaises(TypeError):
            ECA(30, 5, boundary=[1, 2])

        with self.assertRaises(ValueError):
            ECA(30, 5, boundary=(1, 0, 1))

        with self.assertRaises(ValueError):
            ECA(30, 5, boundary=(1, 2))
Exemplo n.º 3
0
    def test_attractors_eca(self):
        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, size in networks:
            self.assertEqual(size, len(rule.attractors))
Exemplo n.º 4
0
    def test_to_networkx_metadata(self):
        net = ECA(30, 3)
        net.boundary = (1, 0)

        nx_net = net.network_graph()

        self.assertEqual(nx_net.graph['code'], 30)
        self.assertEqual(nx_net.graph['boundary'], (1, 0))
Exemplo n.º 5
0
 def test_update_long_time_open(self):
     eca = ECA(45, 14, (0, 1))
     lattice = [1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1]
     expected = [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1]
     if lattice in eca:
         for n in range(1000):
             eca._unsafe_update(lattice)
     self.assertEqual(expected, lattice)
Exemplo n.º 6
0
 def test_update_values_pin_clash(self):
     eca = ECA(30, 5)
     with self.assertRaises(ValueError):
         eca.update([0, 0, 0, 0, 0], pin=[0], values={0: 1})
     with self.assertRaises(ValueError):
         eca.update([0, 0, 0, 0, 0], pin=[1], values={1: 0})
     with self.assertRaises(ValueError):
         eca.update([0, 0, 0, 0, 0], pin=[1], values={0: 0, 1: 0})
     with self.assertRaises(ValueError):
         eca.update([0, 0, 0, 0, 0], pin=[1, 0], values={0: 0})
Exemplo n.º 7
0
 def test_timeseries_eca(self):
     for size in [5, 7, 11]:
         rule = ECA(30, size)
         time = 10
         series = rule.timeseries(time)
         self.assertEqual((size, 2**size, time + 1), series.shape)
         for index, state in enumerate(rule):
             traj = rule.trajectory(state, timesteps=time)
             for t, expect in enumerate(traj):
                 got = series[:, index, t]
                 self.assertTrue(np.array_equal(expect, got))
Exemplo n.º 8
0
 def test_reproduce_open_ecas(self):
     """
     Ensure that RewiredECAs can reproduce open ECAs
     """
     reca = RewiredECA(30, boundary=(1, 0), size=7)
     eca = ECA(30, size=7, boundary=(1, 0))
     state = [0, 0, 0, 1, 0, 0, 0]
     for _ in range(10):
         expect = eca.update(np.copy(state))
         got = reca.update(state)
         self.assertTrue(np.array_equal(expect, got))
Exemplo n.º 9
0
    def test_basins_eca(self):
        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, size in networks:
            self.assertEqual(rule.volume, len(rule.basins))
            self.assertEqual(size, 1 + np.max(rule.basins))

            unique = list(set(rule.basins))
            unique.sort()
            self.assertEqual(list(range(size)), unique)
Exemplo n.º 10
0
    def test_basin_entropy_eca(self):
        networks = [(ECA(30, 2), 1.500000),
                    (ECA(30, 3), 0.000000),
                    (ECA(30, 4), 1.186278),
                    (ECA(30, 5), 0.337290),
                    (ECA(30, 6), 0.231872),
                    (ECA(110, 2), 0.000000),
                    (ECA(110, 3), 0.000000),
                    (ECA(110, 4), 1.561278),
                    (ECA(110, 5), 0.000000),
                    (ECA(110, 6), 1.469012)]

        for net, entropy in networks:
            self.assertAlmostEqual(entropy, net.basin_entropy, places=6)
Exemplo n.º 11
0
 def test_update_pin_index_clash(self):
     eca = ECA(30, 2)
     with self.assertRaises(ValueError):
         eca.update([0, 0], index=0, pin=[1])
     with self.assertRaises(ValueError):
         eca.update([0, 0], index=1, pin=[1])
     with self.assertRaises(ValueError):
         eca.update([0, 0], index=1, pin=[0, 1])
Exemplo n.º 12
0
 def test_update_values_index_clash(self):
     eca = ECA(30, 5)
     with self.assertRaises(ValueError):
         eca.update([0, 0, 0, 0, 0], index=0, values={0: 1})
     with self.assertRaises(ValueError):
         eca.update([0, 0, 0, 0, 0], index=1, values={1: 0})
     with self.assertRaises(ValueError):
         eca.update([0, 0, 0, 0, 0], index=1, values={0: 0, 1: 0})
Exemplo n.º 13
0
 def test_expound(self):
     net = ECA(30, size=5)
     before = net.landscape_data
     after = net.expound().landscape_data
     self.assertEqual(before.__dict__, {})
     self.assertEqual(set(after.__dict__.keys()), set([
         "transitions",
         "basins",
         "basin_sizes",
         "attractors",
         "attractor_lengths",
         "in_degrees",
         "heights",
         "recurrence_times",
         "basin_entropy"
     ]))
Exemplo n.º 14
0
    def test_transitions_eca(self):
        net = ECA(30, 1)
        self.assertEqual([0, 0], list(net.transitions))

        net.size = 2
        self.assertEqual([0, 1, 2, 0], list(net.transitions))

        net.size = 3
        self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(net.transitions))

        net.size = 10
        trans = net.transitions
        self.assertEqual(1024, len(trans))
        self.assertEqual([0, 515, 7, 517, 14, 525, 11,
                          521, 28, 543], list(trans[:10]))
        self.assertEqual([18, 16, 13, 14, 10, 8, 7, 4, 2, 0],
                         list(trans[-10:]))
Exemplo n.º 15
0
    def test_transitions_eca_index(self):
        net = ECA(30, 3)
        net.landscape(index=1)
        self.assertEqual([0, 3, 2, 1, 6, 5, 6, 5], list(net.transitions))

        net.landscape(index=0)
        self.assertEqual([0, 1, 3, 3, 5, 4, 6, 6], list(net.transitions))

        net.landscape(index=None)
        self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(net.transitions))
Exemplo n.º 16
0
    def test_transitions_eca_values(self):
        net = ECA(30, 3)
        net.landscape(values={0: 1})
        self.assertEqual([1, 7, 7, 1, 7, 5, 3, 1], list(net.transitions))

        net.landscape(values={1: 0})
        self.assertEqual([0, 5, 5, 1, 5, 4, 0, 0], list(net.transitions))

        net.landscape(values={})
        self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(net.transitions))
Exemplo n.º 17
0
    def test_transitions_eca_pin(self):
        net = ECA(30, 3)
        net.landscape(pin=[1])
        self.assertEqual([0, 5, 7, 3, 5, 4, 2, 2], list(net.transitions))

        net.landscape(pin=[0])
        self.assertEqual([0, 7, 6, 1, 6, 5, 2, 1], list(net.transitions))

        net.landscape(pin=None)
        self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(net.transitions))
Exemplo n.º 18
0
 def test_init(self):
     for code in range(256):
         for size in range(1, 5):
             for left in range(2):
                 for right in range(2):
                     eca = ECA(code, size, (left, right))
                     self.assertEqual(code, eca.code)
                     self.assertEqual(size, eca.size)
                     self.assertEqual((left, right), eca.boundary)
Exemplo n.º 19
0
    def test_update_index_numpy(self):
        eca = ECA(30, 5, (1, 1))

        lattice = np.asarray([0, 0, 0, 0, 0])
        eca.update(lattice, index=0)
        self.assertTrue(np.array_equal([1, 0, 0, 0, 0], lattice))

        lattice = np.asarray([0, 0, 0, 0, 0])
        eca.update(lattice, index=1)
        self.assertTrue(np.array_equal([0, 0, 0, 0, 0], lattice))

        lattice = np.asarray([0, 0, 1, 0, 0])
        eca.update(lattice, index=1)
        self.assertTrue(np.array_equal([0, 1, 1, 0, 0], lattice))
Exemplo n.º 20
0
    def test_update_index(self):
        eca = ECA(30, 5, (1, 1))

        lattice = [0, 0, 0, 0, 0]
        eca.update(lattice, index=0)
        self.assertEqual([1, 0, 0, 0, 0], lattice)

        lattice = [0, 0, 0, 0, 0]
        eca.update(lattice, index=1)
        self.assertEqual([0, 0, 0, 0, 0], lattice)

        lattice = [0, 0, 1, 0, 0]
        eca.update(lattice, index=1)
        self.assertEqual([0, 1, 1, 0, 0], lattice)
Exemplo n.º 21
0
    def test_update_index_error(self):
        eca = ECA(30, 2)
        with self.assertRaises(IndexError):
            eca.update([0, 0], index=2)

        with self.assertRaises(IndexError):
            eca.update([0, 0], index=-1)
Exemplo n.º 22
0
    def test_trajectory_too_short(self):
        net = ECA(30, 3)
        with self.assertRaises(ValueError):
            net.trajectory([0, 0, 0], timesteps=-1)

        with self.assertRaises(ValueError):
            net.trajectory([0, 0, 0], timesteps=0)
Exemplo n.º 23
0
    def test_update_values(self):
        eca = ECA(30, 5)

        xs = [0, 0, 1, 0, 0]
        self.assertEqual([0, 1, 0, 1, 0], eca.update(xs, values={2: 0}))
        self.assertEqual([1, 0, 0, 0, 1], eca.update(xs, values={1: 0, 3: 0}))
        self.assertEqual([0, 1, 0, 1, 0], eca.update(xs, values={-1: 0}))

        eca.boundary = (1, 1)
        xs = [0, 0, 0, 0, 0]
        self.assertEqual([1, 0, 1, 0, 1], eca.update(xs, values={2: 1}))
Exemplo n.º 24
0
    def test_timeseries_too_short(self):
        net = ECA(30, 3)

        with self.assertRaises(ValueError):
            net.timeseries(-1)

        with self.assertRaises(ValueError):
            net.timeseries(0)
Exemplo n.º 25
0
    def test_update_pin(self):
        eca = ECA(30, 5)

        xs = [0, 0, 1, 0, 0]
        self.assertEqual([0, 0, 1, 1, 0], eca.update(xs, pin=[1]))
        self.assertEqual([0, 0, 1, 0, 1], eca.update(xs, pin=[1]))
        self.assertEqual([1, 0, 1, 0, 1], eca.update(xs, pin=[1]))

        eca.boundary = (1, 1)
        xs = [0, 0, 0, 0, 0]
        self.assertEqual([1, 0, 0, 0, 0], eca.update(xs, pin=[-1]))
        self.assertEqual([1, 1, 0, 0, 0], eca.update(xs, pin=[0, -1]))
Exemplo n.º 26
0
    def test_invalid_boundary(self):
        eca = ECA(30, 5)

        eca.boundary = (0, 0)
        eca.boundary = None

        with self.assertRaises(ValueError):
            eca.boundary = (1, 1, 1)

        with self.assertRaises(ValueError):
            eca.boundary = (1, 2)

        with self.assertRaises(TypeError):
            eca.boundary = 1

        with self.assertRaises(TypeError):
            eca.boundary = [0, 1]
Exemplo n.º 27
0
    def test_attractor_lengths(self):
        for code in [30, 110, 21, 43]:
            for size in range(2, 7):
                net = ECA(code, size)
                lengths = list(map(len, net.attractors))
                net.clear_landscape()
                self.assertEqual(lengths, list(net.attractor_lengths))

        for net in [s_pombe, s_cerevisiae, c_elegans]:
            lengths = list(map(len, net.attractors))
            net.clear_landscape()
            self.assertEqual(lengths, list(net.attractor_lengths))
Exemplo n.º 28
0
    def test_invalid_size(self):
        eca = ECA(30, 5)

        eca.size = 8

        with self.assertRaises(ValueError):
            eca.size = -1

        with self.assertRaises(ValueError):
            eca.size = 0

        with self.assertRaises(TypeError):
            eca.size = "5"
Exemplo n.º 29
0
    def test_invalid_code(self):
        eca = ECA(30, 5)

        eca.code = 45

        with self.assertRaises(ValueError):
            eca.code = -1

        with self.assertRaises(ValueError):
            eca.code = 256

        with self.assertRaises(TypeError):
            eca.code = "30"
Exemplo n.º 30
0
    def test_in_degree(self):
        for code in [30, 110, 21, 43]:
            for size in range(2, 7):
                net = ECA(code, size)
                in_degrees = np.empty(net.volume, dtype=np.int)
                for i in range(net.volume):
                    in_degrees[i] = np.count_nonzero(
                        net.transitions == i)
                net.clear_landscape()
                self.assertEqual(list(in_degrees), list(net.in_degrees))

        for net in [s_pombe, s_cerevisiae, c_elegans]:
            in_degrees = np.empty(net.volume, dtype=np.int)
            for i in range(net.volume):
                in_degrees[i] = np.count_nonzero(net.transitions == i)
            net.clear_landscape()
            self.assertEqual(list(in_degrees), list(net.in_degrees))