-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecipe.py
62 lines (50 loc) · 2.64 KB
/
recipe.py
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
import json
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate
from scipy.stats import chisqprob
from gmpy2 import digits
def read_results(filename):
results = (File_bytes, Entropy, Chi_square, Mean, Monte_Carlo_Pi, Serial_Correlation) = [[] for _ in range(6)]
with open(filename) as f:
data = json.load(f)
variables = {"File-bytes": File_bytes, "Entropy": Entropy, "Chi-square": Chi_square, "Mean": Mean,
"Monte-Carlo-Pi": Monte_Carlo_Pi, "Serial-Correlation": Serial_Correlation}
for i in range(256):
for k, v in variables.items():
v.append(data[str(i)][k])
results = np.array([np.array(r) for r in results]).T
headers = ["File-bytes", "Entropy", "Chi-square", "Mean", "Monte-Carlo-Pi", "Serial-Correlation"]
return pd.DataFrame(results, columns=headers)
two = read_results("results/results-200ksamples.json")
five = read_results("results/results-500ksamples.json")
for d in (two, five):
d["pi_deviation"] = np.abs(d["Monte-Carlo-Pi"] - np.pi)
d["mean_deviation"] = np.abs(d["Mean"] - 255 / 2)
d["p-value"] = chisqprob(d["Chi-square"], 255)
d["rule"] = range(256)
d["langton"] = [sum([int(b) for b in bin(v)[2:].zfill(8)])/8 for v in d["rule"]]
def result_play():
randp2 = two[(two["p-value"] > 0.05) &
(two["p-value"] < 0.95 )]
randp5 = five[(five["p-value"] > 0.05) &
(five["p-value"] < 0.95 )]
randchi2 = two[(two["Chi-square"] < 10**5 )]
randchi5 = five[(five["Chi-square"] < 10**5 )]
# Plot Entropy of all rules against the langton parameter
# ax1 = plt.gca()
# five.plot("langton", "Entropy", ax=ax1, kind="scatter", marker='o', alpha=.5, s=40)
# randp5.plot("langton", "Entropy", ax=ax1, kind="scatter", color="r", marker='o', alpha=.5, s=40)
print(set(randchi2.rule) - set(randp2.rule))
print(set(randchi5.rule) - set(randp5.rule))
print(set(randchi5.rule) - set(randchi2.rule))
ax2 = plt.gca()
randchi2.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", marker='o', alpha=.5, s=40)
randp2.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", color="r", marker='o', alpha=.5, s=40)
#plt.semilogy(x, Serial_Correlation)
#plt.plot(x, Serial_Correlation)
plt.show()
# The 1D CA rules that are random according to the paper
# "When are cellular automata random?" (http://stacks.iop.org/0295-5075/84/i=5/a=50005)
rands_paper = set([15, 30, 75, 90, 60, 105, 120, 150, 210, 240, 85, 149, 101, 165, 153, 105, 169, 150, 166, 170, 15, 135, 45, 165, 195, 105, 225, 150, 180, 240, 85, 86, 89, 90, 102, 105, 106, 150, 154, 170])