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))
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))
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)
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)
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)
def test_neighbors_in(self): net = ECA(30, 3) self.assertEqual(net.neighbors_in(0), set([2, 0, 1])) self.assertEqual(net.neighbors_in(1), set([0, 1, 2])) self.assertEqual(net.neighbors_in(2), set([0, 1, 2])) with self.assertRaises(ValueError): self.assertEqual(net.neighbors_in(3)) net.boundary = (1, 1) self.assertEqual(net.neighbors_in(2), set([1, 2, 3])) with self.assertRaises(ValueError): net.neighbors_in(3) with self.assertRaises(ValueError): net.neighbors_in(5) with self.assertRaises(TypeError): net.neighbors_in('2') with self.assertRaises(ValueError): net.neighbors_in(-1)
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)
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" ]))
def test_timeseries_too_short(self): net = ECA(30, 3) with self.assertRaises(ValueError): net.timeseries(-1) with self.assertRaises(ValueError): net.timeseries(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)
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})
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])
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))
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)
def test_trajectory_eca(self): net = ECA(30, 3) with self.assertRaises(ValueError): net.trajectory([]) with self.assertRaises(ValueError): net.trajectory([0, 1]) xs = [0, 1, 0] got = net.trajectory(xs) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0]], got) got = net.trajectory(xs, timesteps=5) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], got) got = net.trajectory(xs, encode=False) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0]], got) got = net.trajectory(xs, timesteps=5, encode=False) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], got) got = net.trajectory(xs, encode=True) self.assertEqual([0, 1, 0], xs) self.assertEqual([2, 7, 0], list(got)) got = net.trajectory(xs, timesteps=5, encode=True) self.assertEqual([0, 1, 0], xs) self.assertEqual([2, 7, 0, 0, 0, 0], list(got)) xs = 2 got = net.trajectory(xs) self.assertEqual([2, 7, 0], list(got)) got = net.trajectory(xs, timesteps=5) self.assertEqual([2, 7, 0, 0, 0, 0], list(got)) got = net.trajectory(xs, encode=True) self.assertEqual([2, 7, 0], list(got)) got = net.trajectory(xs, timesteps=5, encode=True) self.assertEqual([2, 7, 0, 0, 0, 0], list(got)) got = net.trajectory(xs, encode=False) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0]], got) got = net.trajectory(xs, timesteps=5, encode=False) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], got)
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})
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))
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))
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))
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))
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))
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}))
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]))
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))
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"
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"
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)
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))
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" ]))
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]