-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMandelbrot.py
265 lines (227 loc) · 8.45 KB
/
Mandelbrot.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
262
263
264
265
from itertools import product
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
import matplotlib.cm as cm
from numpy.lib.shape_base import take_along_axis
from util.timer import timer
from collections import OrderedDict
backends = OrderedDict()
try:
import methods.CUDADouble
backends['cudaD'] = methods.CUDADouble.mandelbrot
except Exception as e:
print(e)
try:
import methods.CUDA
backends['cuda'] = methods.CUDA.mandelbrot
except Exception as e:
print(e)
try:
import methods.NumbaDouble
backends['numbaD'] = methods.NumbaDouble.mandelbrot
except Exception as e:
print(e)
try:
import methods.Numba
backends['numba'] = methods.Numba.mandelbrot
except Exception as e:
print(e)
try:
import methods.Tensorflow
backends['tf'] = methods.Tensorflow.mandelbrot
except Exception as e:
print(e)
try:
import methods.Numpy
backends['numpy'] = methods.Numpy.mandelbrot
except Exception as e:
print(e)
try:
import methods.Plain
backends['complex'] = methods.Plain.mandelbrot
except Exception as e:
print(e)
try:
import methods.PlainKomplex
backends['komplex'] = methods.PlainKomplex.mandelbrot
except Exception as e:
print(e)
def remapCmap(image, filter=None):
if filter:
return filter(image)
return image
def getFunction(backend = None):
if backend is None:
backend = list(backends.keys())[0]
if not backend in backends:
print(f'"{backend}" not found, available backends:{",".join(backends.keys())}')
backend = list(backends.keys())[0]
print(f"fallback to {backend}")
return backends[backend]
def getImage(x, y, zoom, iterations,yRes, aspectRatio=1,filter = None,backend = None):
return remapCmap(getFunction(backend = backend)(x, y, zoom, iterations,yRes, aspectRatio=aspectRatio),filter= filter)
def plotImage(images, cmaps=("hot",), filters=(None,), titles=(None,), windowTitle=None, **kwargs):
images = np.array(images)
titles = np.atleast_1d(titles)
if images.ndim == 2:
images = np.array([images])
toPlot = list(product(cmaps, enumerate(filters),images))
sideLength = int(np.ceil(np.sqrt(len(toPlot))))
(height, width) = images[0].shape
aspectRatio = width/height
fig, axes = plt.subplots(sideLength, sideLength, figsize=(
aspectRatio*10, 10), squeeze=False, sharey=True, sharex=True)
if titles:
fig.suptitle(titles[0])
for xi in range(sideLength):
for yi in range(sideLength):
index = yi*sideLength + xi
if index >= len(toPlot):
break
cmap, (n, filter),image = toPlot[index]
axes[yi, xi].imshow(remapCmap(image, filter), cmap=cmap)
axes[yi, xi].set_title(f'"{cmap}", filter {n}')
if windowTitle:
fig.canvas.set_window_title(windowTitle)
plt.show(**kwargs)
def testBackends(x, y, zoom, iterations, yRes, aspectRatio=1, cmap='hot', title=None, functions=None, filter=None, windowTitle=None, **kwargs):
if not functions:
functions = list(backends.keys())
prevImage = None
prevKey = ""
sideLength = int(np.ceil(np.sqrt(len(functions))))
fig, axes = plt.subplots(sideLength, sideLength, figsize=(
aspectRatio*10, 10), squeeze=False, sharey=True, sharex=True)
if title:
fig.suptitle(title)
else:
fig.suptitle(
f"x:{x}, y:{y}, zoom:{zoom:.2f}, {iterations} iterations, cmap={cmap}")
for xi in range(sideLength):
for yi in range(sideLength):
index = yi*sideLength + xi
if index >= len(functions):
break
key = functions[index]
image, t = timer(getImage, x, y, zoom, iterations, yRes, aspectRatio,backend = key,filter = filter)
if index == 0:
prevImage = image
prevKey = key
msg = ""
if not np.array_equal(prevImage, image):
msg = f"\ndiffers from {prevKey}"
prevImage = image
prevKey = key
axes[yi, xi].imshow(image, cmap=cmap)
axes[yi, xi].set_title(f'{key}: {t:.3f}s{msg}')
if windowTitle:
fig.canvas.set_window_title(windowTitle)
plt.show(**kwargs)
def plotMandelbrot(coords, yRes, aspectRatio=1, cmaps=("hot",), title=None, subtitle=None, backend=None, filters=(None,), windowTitle=None, **kwargs):
# kwargs for plt.show(**kwargs)
coords = np.atleast_2d(coords)
x, y, zoom, iterations = coords[0]
count, t = timer(getImage, x, y, zoom,
iterations, yRes, aspectRatio,backend= backend)
if not title:
title = f"x:{x}, y:{y}, zoom:{zoom:.2f}, {iterations} iterations ({t:.3f}s)"
if subtitle:
title = title + "\n" + subtitle
plotImage(count, cmaps, titles=title, filters=filters,
windowTitle=windowTitle, **kwargs)
def animateZoom(x, y, zoomMin, zoomMax, iterations, yRes, aspectRatio=1, cmap="afmhot", speed=0.1, fps=2, duration=5, backend=None, filter=None):
fig = plt.figure(figsize=(int(aspectRatio*10), 10))
plt.tight_layout()
plt.axis("off")
plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
dt = 1/fps * 1000 # in ms
count = getImage(x, y, zoomMin, iterations, yRes, aspectRatio,backend=backend,filter = filter)
image = plt.imshow(count, cmap=cmap, animated=True)
zooms = list(np.logspace(np.log(zoomMin)/np.log(1+speed), np.log(zoomMax) /
np.log(1+speed), base=1+speed, num=int(duration*fps)))
def update(zoom):
print(f" {zooms.index(zoom)+1}/{len(zooms)}", end="\r")
count = getImage(x, y, zoom, iterations, yRes, aspectRatio,backend=backend,filter = filter)
image.set_array(count)
return image,
# TODO: tqdm on zooms is broken?? remove print from update
return FuncAnimation(fig, update, zooms, interval=dt, blit=True)
def interactive(cmap = "hot",backend = None):
yRes = 1000
iterations = 10000
plt.figure()
ax = plt.gca()
def getImageByBounds(minX,maxX,minY,maxY):
x = (minX+maxX)/2
y = (minY + maxY)/2
width = maxX-minX
height = maxY-minY
zoom = 1/(maxY-y)
aspectRatio = width/height
return getImage(x,y,zoom,iterations,yRes,aspectRatio,backend=backend)
image = ax.imshow(getImageByBounds(-2,1,-1.5,1.5),extent = (-2,1,-1.5,1.5),cmap = cmap,origin = "top")
def on_ylims_change(axes):
xmin,xmax = axes.get_xlim()
ymin,ymax = axes.get_ylim()
im = getImageByBounds(xmin,xmax,ymin,ymax)
image.set_array(im)
image.set_extent((xmin,xmax,ymin,ymax))
ax.callbacks.connect('ylim_changed', on_ylims_change)
plt.show()
cv2Available = False
try:
import cv2
cv2Available = True
except Exception as e:
print(e)
import copy
def animateZoomCv2(x, y, zoomMin, zoomMax, iterations, yRes, aspectRatio=1, cmap="afmhot", speed=0.1, fps=30, duration=10, backend=None):
if not cv2Available:
print("cv2 not imported")
return
if backend is None:
backend = list(backends.keys())[0]
if not backend in backends:
print(
f'"{backend}" not found, available backends:{",".join(backends.keys())}')
return
dt = 1/fps * 1000
cmap = copy.copy(cm.get_cmap(cmap))
# force to return bgr instead of rgba
cmap._init()
cmap._lut = cmap._lut[..., [2, 1, 0]]
frames = int(duration*fps)
zooms = np.logspace(np.log(zoomMin)/np.log(1+speed),
np.log(zoomMax)/np.log(1+speed), base=1+speed, num=frames)
bufferedFrames = [None]*frames
def getFrame(i):
if bufferedFrames[i] is None:
zoom = zooms[i]
image = backends[backend](
x, y, zoom, iterations, yRes, aspectRatio)
bufferedFrames[i] = cmap(image/image.max(), bytes=True)
return bufferedFrames[i]
def update(i):
start = time.time()
image = getFrame(i)
cv2.imshow('Quit with Q', image)
end = time.time()
return max(int(dt-(end-start)*1000), 1)
i = 0
while(True):
wait = update(i)
if cv2.waitKey(wait) & 0xff == ord('q'):
break
i = (i+1) % frames
cv2.destroyAllWindows()
def forceCompile(output=False):
if output:
print("forcing the jit compiler...")
for key, f in backends.items():
if output:
print(f"{key}...")
f(0, 0, 1, 1, 3, 1)
if output:
print("done")