Repository navigation
Expand file tree
/
Copy pathtest_temporal.py
More file actions
143 lines (107 loc) · 2.84 KB
/
Copy pathtest_temporal.py
File metadata and controls
143 lines (107 loc) · 2.84 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import numpy as np
import pytest
from lifeforge.experiment import (
ExperimentConfig,
run_experiment,
)
from lifeforge.rules import ConwayRule
from lifeforge.temporal import (
analyze_temporal_dynamics,
find_exact_recurrence_period,
find_extinction_time,
lag_autocorrelation,
persistence_ratio,
)
from lifeforge.world import World
def make_block() -> World:
world = World.empty(5, 5)
world.state[2, 2] = 1
world.state[2, 3] = 1
world.state[3, 2] = 1
world.state[3, 3] = 1
return world
def test_find_extinction_time():
assert find_extinction_time([5, 4, 2, 0, 0]) == 3
def test_no_extinction():
assert find_extinction_time([5, 4, 2, 1]) is None
def test_persistence_ratio():
assert persistence_ratio([1, 2, 0, 0, 3]) == pytest.approx(0.6)
def test_lag_autocorrelation():
result = lag_autocorrelation(
[1, 2, 3, 4, 5],
lag=1,
)
assert result == pytest.approx(1.0)
def test_constant_series_has_no_autocorrelation():
assert lag_autocorrelation(
[5, 5, 5, 5],
lag=1,
) is None
def test_static_world_has_period_one():
world = make_block()
states = [
world.state.copy(),
world.state.copy(),
world.state.copy(),
]
assert find_exact_recurrence_period(states) == 1
def test_blinker_has_period_two():
config = ExperimentConfig(
name="blinker",
width=5,
height=5,
density=0.0,
steps=2,
seed=42,
)
world = World.empty(5, 5)
world.state[1, 2] = 1
world.state[2, 2] = 1
world.state[3, 2] = 1
from lifeforge.simulation import Simulation
simulation = Simulation(
world=world,
rule=ConwayRule(),
)
simulation.run(2)
assert find_exact_recurrence_period(
[
item.state
for item in simulation.history
]
) == 2
def test_temporal_analysis():
world = make_block()
from lifeforge.simulation import Simulation
simulation = Simulation(
world=world,
rule=ConwayRule(),
)
simulation.run(20)
result = type(
"Result",
(),
{
"generations": list(range(21)),
"populations": [
item.population
for item in simulation.history
],
"activities": [
0.0
for _ in simulation.history
],
"states": [
item.state.copy()
for item in simulation.history
],
},
)()
summary = analyze_temporal_dynamics(
result,
burn_in_fraction=0.25,
)
assert summary.persistence_ratio == 1.0
assert summary.population_mean == pytest.approx(4.0)
assert summary.population_std == pytest.approx(0.0)
assert summary.exact_recurrence_period == 1