-
Notifications
You must be signed in to change notification settings - Fork 0
/
Julia.py
executable file
·261 lines (200 loc) · 8.41 KB
/
Julia.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""Create sequence of images based on inputs from 'input.json'.
For descriptions of constants & inputs, see JuliaInputs.py.
Sequence types:
Linear -- Every frame shows a fixed region of the complex plane
centered at the origin. Each successive frame is made by moving
c along a straight line in the complex plane.
Radial -- Every frame shows a fixed region of the complex plane
centered at the origin. Each successive frame is made by moving
c along a circular arc centered at the origin.
Zoom -- Every frame shows the same Julia fractal, i.e. `c` is not changed.
The center of each image is also constant, but it may be centered
anywhere in the plane. Each successive frame shows a slightly
smaller or larger region of the complex plane to give
a zooming effect.
Power - The constant `c` is not changed, but the exponent `p` is changed
frame-by-frame from 2 to 3, taking on fractional values in between.
Each sequence type adjusts its starting and/or ending c values so
that they are 'interesting' - this means that the fractal created
by c is not an entirely black image.
String to be tweeted is written to 'tweet.txt'.
"""
import timeit
import itertools
import json
import numpy as np
from pathos.multiprocessing import ProcessingPool as Pool
import JuliaTools
start = timeit.default_timer()
with open("input.json", "r") as infile:
d = json.load(infile)
seqType = str(d["seqType"])
n, r, p = d["n"], d["r"], d["p"]
split, iters, frameCount = d["split"], d["iters"], d["frameCount"]
c = complex(d["c"][0], d["c"][1])
# List of coordinates of subimages inside the full image
coords = list(itertools.product(range(split), range(split)))
def makeLinear():
global c
cEnd = complex(d["linear"]["cEnd"][0], d["linear"]["cEnd"][1])
pool = Pool(4)
# Get interesting starting c
while True:
subIm = JuliaTools.subImage(c=c, r=r, p=p, n=10, iters=iters,
split=split, save=False, aura=False)
isBlackList = pool.map(subIm, coords)
if not all(isBlackList):
break
else:
c *= 0.975
# Get interesting cEnd
while True:
subIm = JuliaTools.subImage(c=cEnd, r=r, p=p, n=10, iters=iters,
split=split, save=False, aura=False)
isBlackList = pool.map(subIm, coords)
if not all(isBlackList):
break
else:
cEnd *= 0.975
# Straight line c follows in complex plane
cPath = np.linspace(c, cEnd, frameCount)
for frame in xrange(frameCount):
subIm = JuliaTools.subImage(c=cPath[frame], r=r, n=n, p=p,
iters=iters, split=split)
isBlackList = pool.map(subIm, coords)
allBlack = all(isBlackList)
if not allBlack:
JuliaTools.makeFrame(frame, n, split, coords)
pool.close()
JuliaTools.prepareForFFmpeg(frameCount=frameCount, loop=True)
# Write tweet string
s1 = '+' if c.imag >= 0 else '-'
s2 = '+' if cEnd.imag >= 0 else '-'
with open("tweet.txt","w") as out:
out.write("Images generated using constants"
" on a straight path from {:03.2f} {} {:03.2f}i"
" to {:03.2f} {} {:03.2f}i."
.format(c.real, s1, abs(c.imag),
cEnd.real, s2, abs(cEnd.imag)))
stop = timeit.default_timer()
print stop - start
def makeRadial():
rad, angle = d["radial"]["rad"], d["radial"]["angle"]
args = np.linspace(angle, angle + np.pi, frameCount)
pool = Pool(4)
while True:
subIm = JuliaTools.subImage(c=rad*np.exp(1j*angle), r=r, n=10, p=p,
iters=iters, split=split, save=False,
aura=False)
isBlackList = pool.map(subIm, coords)
if not all(isBlackList):
break
else:
rad *= 0.975
# Circular arc c follows in complex plane
cPath = rad*np.exp(1j*args)
for frame in xrange(frameCount):
subIm = JuliaTools.subImage(c=cPath[frame], r=r, n=n, p=p,
iters=iters, split=split)
isBlackList = pool.map(subIm, coords)
allBlack = all(isBlackList)
if not allBlack:
JuliaTools.makeFrame(frame, n, split, coords)
pool.close()
JuliaTools.prepareForFFmpeg(frameCount=frameCount, loop=True)
with open("tweet.txt","w") as out:
out.write("Images generated using constants"
" on a circular arc of radius {:03.2f}."
.format(rad))
stop = timeit.default_timer()
print stop - start
def makeZoom():
global c
center = complex(d["zoom"]["center"][0], d["zoom"]["center"][1])
rStart, rEnd = d["zoom"]["rStart"], d["zoom"]["rEnd"]
h = 0.25
pool = Pool(4)
# Get interesting c
while True:
subIm = JuliaTools.subImage(c=c, r=rStart, n=10, p=p, iters=iters,
split=split, save=False, aura=False)
isBlackList = pool.map(subIm, coords)
if not all(isBlackList):
break
else:
c *= 0.99
# Get interesting center (not entirely black or color)
decreased, increased = False, False
while True:
subIm = JuliaTools.subImage(c=c, r=0.25*rStart, n=10, p=p, iters=iters,
split=split, center=center, save=False,
aura=False)
isBlackList = pool.map(subIm, coords)
someBlack = any(isBlackList)
someColor = not all(isBlackList)
if someBlack and someColor:
break
elif not someColor:
center *= 1 - h
decreased = True
else:
center *= 1 + h
increased = True
if increased and decreased:
h *= 0.5
increased, decreased = False, False
# Ensures frame size is changed at consant proportion
factor = (rEnd/rStart)**(1.0/float(frameCount))
for frame in xrange(frameCount):
subIm = JuliaTools.subImage(c=c, r=rStart, n=n, p=p, iters=iters,
split=split, center=center, aura=False)
pool.map(subIm, coords)
JuliaTools.makeFrame(frame, n, split, coords)
rStart *= factor
pool.close()
JuliaTools.prepareForFFmpeg(frameCount=frameCount, loop=True)
s1 = '+' if c.imag >= 0 else '-'
s2 = '+' if center.imag >= 0 else '-'
with open("tweet.txt","w") as out:
out.write("Image generated using c = {:03.2f} {} {:03.2f}i"
" centered at the point {:03.2f} {} {:03.2f}i."
.format(c.real, s1, abs(c.imag),
center.real, s2, abs(center.imag)))
stop = timeit.default_timer()
print stop - start
def makePower():
global c
pMin, pMax = d["power"]["pMin"], d["power"]["pMax"]
pPath = np.linspace(pMin, pMax, frameCount)
pool = Pool(4)
# Get interesting c
while True:
subIm = JuliaTools.subImage(c=c, n=10, iters=iters/2, r=r, p=pMin,
split=split, save=False, aura=False)
isBlackList = pool.map(subIm, coords)
if not all(isBlackList):
break
else:
c *= 0.975
for frame in xrange(frameCount):
subIm = JuliaTools.subImage(c=c, r=r, n=n, p=pPath[frame],
iters=iters/2, split=split)
isBlackList = pool.map(subIm, coords)
allBlack = all(isBlackList)
if not allBlack:
JuliaTools.makeFrame(frame, n, split, coords)
pool.close()
JuliaTools.prepareForFFmpeg(frameCount=frameCount, loop=True)
with open("tweet.txt", "w") as out:
out.write("woooooooooooooooooooo")
stop = timeit.default_timer()
print stop - start
if __name__ == "__main__":
if seqType == 'linear':
makeLinear()
elif seqType == 'radial':
makeRadial()
elif seqType == 'zoom':
makeZoom()
elif seqType == 'power':
makePower()