-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerations_controller.py
61 lines (48 loc) · 1.76 KB
/
generations_controller.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
import subprocess
import threading
import random
import string
import os
from os.path import expanduser
import sys
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def auto_plot(num_generations,num_organisms,fifoname=None):
if fifoname == None:
fifoname = ''.join([random.choice(string.ascii_letters) for a in range(10)])
args = [os.getcwd() + "/bio",str(num_organisms),str(num_generations),fifoname]
threading.Thread(target=subprocess.run,args=(args,)).start()
num_frames = int(num_generations/100)
print(f"Running for {num_frames} frames")
results = [[np.nan]*num_frames,[np.nan]*num_frames,[np.nan]*num_frames] # [[aa1,aa2...],[ab],[bb]]
def animate(i):
nonlocal results
print("Ran animate")
result = file.readline()
if result == "":
return
result = [int(a.split(":")[1])/num_organisms*100 for a in result.split("\t")]
results[0][i//100] = result[0]
results[1][i//100] = result[1]
results[2][i//100] = result[2]
print(f"{results}")
aa_line.set_ydata(results[0])
ab_line.set_ydata(results[1])
bb_line.set_ydata(results[2])
return (aa_line,ab_line,bb_line)
fig, ax = plt.subplots()
ax.set_xlim(0,num_generations)
ax.set_ylim(0,100)
aa_line, = ax.plot([a for a in range(0,num_generations,100)], [np.nan for a in range(num_frames)], \
animated=True,label="aa")
ab_line, = ax.plot([a for a in range(0,num_generations,100)], [np.nan for a in range(num_frames)], \
animated=True,label="ab")
bb_line, = ax.plot([a for a in range(0,num_generations,100)], [np.nan for a in range(num_frames)], \
animated=True,label="bb")
ax.legend()
file = open(fifoname,'r')
ani = FuncAnimation(fig, animate, frames=range(0,num_generations,100), interval=200)
os.unlink(fifoname)
auto_plot(1000,65536)