def test_active_info_not_fixed_size(self): """ Raise a ``ValueError`` if the provided network is not fixed sized, and the ``size`` argument is ``None`` """ with self.assertRaises(ValueError): active_information(ECA(30), k=3, timesteps=10, local=False) active_information(ECA(30), k=3, timesteps=10, size=5, local=False)
def test_entropy_rate_not_fixed_size(self): """ Raise a ``ValueError`` if the provided network is not fixed sized, and the ``size`` argument is ``None`` """ with self.assertRaises(ValueError): entropy_rate(ECA(30), k=3, timesteps=10, local=False) entropy_rate(ECA(30), k=3, timesteps=10, size=5, local=False)
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, width, size in networks: landscape = Landscape(rule, size=width) self.assertEqual(size, len(landscape.attractors))
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)))
def test_timeseries_eca(self): rule = ECA(30) for size in [5, 7, 11]: landscape = Landscape(rule, size=size) time = 10 series = landscape.timeseries(time) self.assertEqual((size, 2**size, time + 1), series.shape) for index, state in enumerate(rule.state_space(size)): traj = landscape.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 """ from neet.automata import ECA reca = RewiredECA(30, boundary=(1, 0), size=7) eca = ECA(30, 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_basins_eca(self): """ test ``basins`` on ECAs """ networks = [(ECA(30), 2, [2, 1, 1]), (ECA(30), 3, [8]), (ECA(30), 4, [2, 12, 1, 1]), (ECA(30), 5, [2, 30]), (ECA(30), 6, [62, 1, 1]), (ECA(110), 2, [4]), (ECA(110), 3, [8]), (ECA(110), 4, [4, 6, 6]), (ECA(110), 5, [32]), (ECA(110), 6, [10, 27, 27])] for net, width, basin_sizes in networks: basin_counter = Counter([len(c) for c in basins(net, size=width)]) self.assertEqual(Counter(basin_sizes), basin_counter)
def test_basin_entropy_eca(self): """ test ``basin_entropy`` on ECAs """ networks = [(ECA(30), 2, 1.5), (ECA(30), 3, 0.), (ECA(30), 4, 1.186278124459133), (ECA(30), 5, 0.3372900666170139), (ECA(30), 6, 0.23187232431271465), (ECA(110), 2, 0.), (ECA(110), 3, 0.), (ECA(110), 4, 1.561278124459133), (ECA(110), 5, 0.), (ECA(110), 6, 1.4690124052234232)] for net, width, entropy in networks: self.assertAlmostEqual(basin_entropy(net, size=width), entropy)
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, width, size in networks: landscape = Landscape(rule, size=width) self.assertEqual(landscape.volume, len(landscape.basins)) self.assertEqual(size, 1 + np.max(landscape.basins)) unique = list(set(landscape.basins)) unique.sort() self.assertEqual(list(range(size)), unique)
def test_is_state_space(self): self.assertTrue(issubclass(Landscape, StateSpace)) space = s_pombe.state_space() landscape = Landscape(s_pombe) self.assertEqual(list(space), list(landscape)) self.assertEqual([space.encode(state) for state in space], list(map(landscape.encode, landscape))) ca = ECA(30) space = ca.state_space(10) landscape = Landscape(ca, size=10) self.assertEqual(list(space), list(landscape)) self.assertEqual([space.encode(state) for state in space], list(map(landscape.encode, landscape)))
def test_transitions_not_fixed_sized(self): """ ``transitions`` should raise an error if ``net`` is not fixed sized and ``size`` is ``None`` """ with self.assertRaises(ValueError): transitions(ECA(30), size=None)
def test_transitions_eca(self): ca = ECA(30) landscape = Landscape(ca, size=1) self.assertEqual(ca, landscape.network) self.assertEqual(1, landscape.size) self.assertEqual([0, 0], list(landscape.transitions)) landscape = Landscape(ca, size=2) self.assertEqual(ca, landscape.network) self.assertEqual(2, landscape.size) self.assertEqual([0, 1, 2, 0], list(landscape.transitions)) landscape = Landscape(ca, size=3) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(landscape.transitions)) landscape = Landscape(ca, size=10) trans = landscape.transitions self.assertEqual(ca, landscape.network) self.assertEqual(10, landscape.size) 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:]))
def test_basin_entropy_variable_sized(self): """ ``basin_entropy`` should raise an error if ``net`` is a variable sized network and ``size`` is ``None`` """ with self.assertRaises(ValueError): basin_entropy(ECA(30), size=None)
def test_timeseries_too_short(self): landscape = Landscape(ECA(30), size=3) with self.assertRaises(ValueError): landscape.timeseries(-1) with self.assertRaises(ValueError): landscape.timeseries(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)
def test_transition_graph_eca(self): """ test ``transitions_graph`` on ``ECA`` """ graph = transition_graph(ECA(30), size=8) self.assertEqual(256, graph.number_of_nodes()) self.assertEqual(256, graph.number_of_edges())
def test_transition_graph_variable_sized(self): """ ``transitions_graph`` should raise an error if ``net`` is variable sized and ``size`` is ``None`` """ with self.assertRaises(ValueError): transition_graph(ECA(30))
def test_trajectory_too_short(self): landscape = Landscape(ECA(30), size=3) with self.assertRaises(ValueError): landscape.trajectory([0, 0, 0], timesteps=-1) with self.assertRaises(ValueError): landscape.trajectory([0, 0, 0], timesteps=0)
def test_timeseries_variable_sized(self): """ ``timeseries`` should raise an error if ``net`` is variable sized and ``size`` is ``None`` """ with self.assertRaises(ValueError): timeseries(ECA(30), size=None, timesteps=5)
def test_trajectory_eca(self): landscape = Landscape(ECA(30), size=3) with self.assertRaises(ValueError): landscape.trajectory([]) with self.assertRaises(ValueError): landscape.trajectory([0, 1]) xs = [0, 1, 0] got = landscape.trajectory(xs) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0]], got) got = landscape.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 = landscape.trajectory(xs, encode=False) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0]], got) got = landscape.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 = landscape.trajectory(xs, encode=True) self.assertEqual([0, 1, 0], xs) self.assertEqual([2, 7, 0], list(got)) got = landscape.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 = landscape.trajectory(xs) self.assertEqual([2, 7, 0], list(got)) got = landscape.trajectory(xs, timesteps=5) self.assertEqual([2, 7, 0, 0, 0, 0], list(got)) got = landscape.trajectory(xs, encode=True) self.assertEqual([2, 7, 0], list(got)) got = landscape.trajectory(xs, timesteps=5, encode=True) self.assertEqual([2, 7, 0, 0, 0, 0], list(got)) got = landscape.trajectory(xs, encode=False) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0]], got) got = landscape.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_attractor_lengths(self): for code in [30, 110, 21, 43]: for size in range(2, 7): landscape = Landscape(ECA(code), size=size) lengths = list(map(len, landscape.attractors)) self.assertEqual(lengths, list(landscape.attractor_lengths)) for net in [s_pombe, s_cerevisiae, c_elegans]: landscape = Landscape(net) lengths = list(map(len, landscape.attractors)) self.assertEqual(lengths, list(landscape.attractor_lengths))
def test_basin_entropy_eca(self): networks = [(ECA(30), 2, 1.500000, 0.451545), (ECA(30), 3, 0.000000, 0.000000), (ECA(30), 4, 1.186278, 0.357105), (ECA(30), 5, 0.337290, 0.101534), (ECA(30), 6, 0.231872, 0.069801), (ECA(110), 2, 0.000000, 0.000000), (ECA(110), 3, 0.000000, 0.000000), (ECA(110), 4, 1.561278, 0.469992), (ECA(110), 5, 0.000000, 0.000000), (ECA(110), 6, 1.469012, 0.442217)] for net, width, base2, base10 in networks: landscape = Landscape(net, size=width) self.assertAlmostEqual(base2, landscape.basin_entropy(), places=6) self.assertAlmostEqual(base2, landscape.basin_entropy(base=2), places=6) self.assertAlmostEqual(base10, landscape.basin_entropy(base=10), places=6)
def test_transitions_eca_encoded(self): """ test ``transitions`` on ECAs; encoding the states """ rule30 = ECA(30) got = transitions(rule30, size=1, encode=True) self.assertEqual([0, 0], got) got = transitions(rule30, size=2, encode=True) self.assertEqual([0, 1, 2, 0], got) got = transitions(rule30, size=3, encode=True) self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], got)
def test_transitions_eca(self): """ test ``transitions`` on ECAs; encoding the states """ rule30 = ECA(30) got = transitions(rule30, size=1) self.assertEqual([[0], [0]], got) got = transitions(rule30, size=2) self.assertEqual([[0, 0], [1, 0], [0, 1], [0, 0]], got) got = transitions(rule30, size=3) self.assertEqual([[0, 0, 0], [1, 1, 1], [1, 1, 1], [1, 0, 0], [1, 1, 1], [0, 0, 1], [0, 1, 0], [0, 0, 0]], got)
def test_in_degree(self): for code in [30, 110, 21, 43]: for size in range(2, 7): landscape = Landscape(ECA(code), size=size) in_degrees = np.empty(landscape.volume, dtype=np.int) for i in range(landscape.volume): in_degrees[i] = np.count_nonzero( landscape.transitions == i) self.assertEqual(list(in_degrees), list(landscape.in_degrees)) for net in [s_pombe, s_cerevisiae, c_elegans]: landscape = Landscape(net) in_degrees = np.empty(landscape.volume, dtype=np.int) for i in range(landscape.volume): in_degrees[i] = np.count_nonzero(landscape.transitions == i) self.assertEqual(list(in_degrees), list(landscape.in_degrees))
def test_transitions_eca_values(self): ca = ECA(30) landscape = Landscape(ca, size=3, values={0: 1}) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([1, 7, 7, 1, 7, 5, 3, 1], list(landscape.transitions)) landscape = Landscape(ca, size=3, values={1: 0}) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 5, 5, 1, 5, 4, 0, 0], list(landscape.transitions)) landscape = Landscape(ca, size=3, values={}) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(landscape.transitions))
def test_transitions_eca_pin(self): ca = ECA(30) landscape = Landscape(ca, size=3, pin=[1]) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 5, 7, 3, 5, 4, 2, 2], list(landscape.transitions)) landscape = Landscape(ca, size=3, pin=[0]) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 7, 6, 1, 6, 5, 2, 1], list(landscape.transitions)) landscape = Landscape(ca, size=3, pin=None) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(landscape.transitions))
def test_transitions_eca_index(self): ca = ECA(30) landscape = Landscape(ca, size=3, index=1) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 3, 2, 1, 6, 5, 6, 5], list(landscape.transitions)) landscape = Landscape(ca, size=3, index=0) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 1, 3, 3, 5, 4, 6, 6], list(landscape.transitions)) landscape = Landscape(ca, size=3, index=None) self.assertEqual(ca, landscape.network) self.assertEqual(3, landscape.size) self.assertEqual([0, 7, 7, 1, 7, 4, 2, 0], list(landscape.transitions))
def test_trajectory_eca_encoded(self): """ test ``trajectory`` on ECAs; encoding the states """ rule30 = ECA(30) with self.assertRaises(ValueError): trajectory(rule30, [], encode=True) state = [0, 1, 0] got = trajectory(rule30, state, encode=True) self.assertEqual([0, 1, 0], state) self.assertEqual([2, 7], got) got = trajectory(rule30, state, timesteps=2, encode=True) self.assertEqual([0, 1, 0], state) self.assertEqual([2, 7, 0], got)
def test_trajectory_eca(self): """ test ``trajectory`` on ECAs """ rule30 = ECA(30) with self.assertRaises(ValueError): trajectory(rule30, []) xs = [0, 1, 0] got = trajectory(rule30, xs) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1]], got) got = trajectory(rule30, xs, timesteps=2) self.assertEqual([0, 1, 0], xs) self.assertEqual([[0, 1, 0], [1, 1, 1], [0, 0, 0]], got)