-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (60 loc) · 2.09 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
import random
import operator
import math
import matplotlib.pyplot as plt
from samila import GenerativeImage, Projection, VALID_COLORS
import os
import sys
class Generate:
def __init__(self, num_pieces, bg_color="black"):
projections = [ Projection.POLAR, Projection.AITOFF, Projection.LAMBERT, Projection.MOLLWEIDE ]
func = self.algo_functions()
self.dir_path = os.path.dirname(os.path.abspath(__file__))
os.makedirs(f'{self.dir_path}/output', exist_ok=True)
for i in range(num_pieces):
self.g = GenerativeImage(func, func)
self.g.generate()
self.g.plot(color=random.choice(VALID_COLORS), bgcolor=bg_color, projection=random.choice(projections))
self.save(i +1)
def f_pow(self, a, b):
try:
return a ** b
except:
return a * b
def algo_functions(self):
math_funcs = [
math.cos,
math.sin,
math.tan,
math.asinh,
math.cosh,
math.sinh,
math.tanh,
abs
]
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'**': self.f_pow
}
def func(x, y):
result = random.uniform(-3,3)
for n in range(0, random.randint(3, 30)):
if n == 0:
result = operators[random.choice(list(operators.keys()))](
random.choice(math_funcs)(result),
random.choice([x, y])
)
else:
result = operators[random.choice(list(operators.keys()))](
random.choice(math_funcs)(random.choice([x, y])),
random.choice([x, y, result])
)
return result
return func
def save(self, filename):
self.g.save_data(file_adr=f'{self.dir_path}/output/{filename}.json')
plt.savefig(f'{self.dir_path}/output/{filename}.png')
if __name__ == '__main__':
Generate(num_pieces=300)