-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
60 lines (51 loc) · 2.19 KB
/
utils.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
from argparse import ArgumentParser, Namespace
import matplotlib.pyplot as plt
import numpy as np
import os
import imageio
plt.axis("off")
def get_arguments():
parser = ArgumentParser()
parser.add_argument("-gfs", "--game_field_size", dest="game_field_size", default=5, type=int)
parser.add_argument("-p", "--pattern_idx", dest="pattern_idx", default=0, type=int, help="0: blinker, 1: beacon, 2: Gosper glider gun <- still not implemented") #TODO Gosper glider gun <- still not implemented
parser.add_argument("-d", "--pattern_storage_directory", dest="pattern_storage_directory", default="patterns", type=str)
parser.add_argument("-fps", "--frames_per_second", dest="frames_per_second", default=2, type=int)
args = parser.parse_args()
if args.pattern_idx == 0:
try:
assert args.game_field_size == 5, f"{args.game_field_size} == 5"
except AssertionError as e:
print(e)
print("game field size for chosen pattern must be 5!")
elif args.pattern_idx == 1:
try:
assert args.game_field_size == 6, f"{args.game_field_size} == 6"
except AssertionError as e:
print(e)
print("game field size for chosen pattern must be 6!")
else:
print("pattern still not implemented!!!")
exit(1)
if not os.path.isdir(args.pattern_storage_directory):
os.makedirs(args.pattern_storage_directory)
return args
PATTERN_DICT = {
0:"blinker",
1:"beacon", #<- TODO
2:"Gosper glider gun" #<- TODO
}
def save_fig(args:Namespace,pattern:np.ndarray, it:int):
"""
@brief: save the pattern to file
"""
full_fn = os.path.join(args.pattern_storage_directory, PATTERN_DICT[args.pattern_idx]+f"_{it}.png")
plt.imshow(pattern)
plt.savefig(full_fn)
def generate_gif(args):
pattern_name = PATTERN_DICT[args.pattern_idx]
fns_oi = [os.path.join(args.pattern_storage_directory, fn) for fn in os.listdir(args.pattern_storage_directory) if pattern_name in fn]
frames=[]
for fn in fns_oi:
frames.append(imageio.v2.imread(fn))
gif_name = pattern_name+".gif"
imageio.mimsave(gif_name, frames,fps=args.frames_per_second,loop=1)