-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_species.py
59 lines (46 loc) · 1.56 KB
/
plot_species.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
from collections import defaultdict
from os.path import join
import pickle
from pprint import pprint
import sys
import matplotlib.pyplot as plt
def process_generation(distribution, generation):
num_genomes = 0
num_species = 0
for l in distribution.values():
if l[generation] != 0:
num_genomes += l[generation]
num_species += 1
return num_genomes, num_species
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Please supply a directory to process')
exit()
directory = sys.argv[1]
with open(join(directory, 'final.pkl'), 'rb') as f:
nl = pickle.load(f)
if nl is None:
print("Couldn't load pickled file")
exit()
print('Num generations:', nl.generations)
species = {}
for spec in nl.species:
species[spec.ID] = spec
for spec in nl.graveyard:
species[spec.ID] = spec
distribution = defaultdict(list)
print(max(((x.birth,x.ID) for x in species.values())))
for spec in species.values():
distribution[spec.ID] = [0] * (spec.birth-1) + spec.genomes_history
for spec in species.values():
distribution[spec.ID] += [0] * (nl.generations - len(distribution[spec.ID]))
spec_over_time = []
for gen in range(nl.generations):
spec_over_time.append(process_generation(distribution, gen)[1])
plt.plot(spec_over_time)
plt.xlabel('Generation')
plt.ylabel('Species')
plt.title('Species Over Time')
plt.axhline(y=40, color='r')
plt.savefig(join(directory, 'time_species.png'))
plt.close()