Spaces:
Sleeping
Sleeping
gabriel lopez commited on
Commit ·
67c09ab
1
Parent(s): 7522996
improved
Browse files- .gitignore +1 -0
- README.md +1 -1
- app.py +55 -0
- fractal_generator.py +31 -36
- requirements.txt +2 -3
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Fractal Generator
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: Fractal Generator
|
| 3 |
+
emoji: 😀
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from numpy import *
|
| 3 |
+
|
| 4 |
+
from fractal_generator import FractalGenerator
|
| 5 |
+
|
| 6 |
+
TITLE = "Fractal Generator"
|
| 7 |
+
DESCRIPTION = "<center>Create your own fractal art!</center>"
|
| 8 |
+
EXAMPLES = [
|
| 9 |
+
["Julia", "sin(z**4 + 1.41)"],
|
| 10 |
+
["Julia", "sin(z**4 + 1.41)*exp(2.4*1J)"],
|
| 11 |
+
["Julia", "sin(z**4 + 3.41)*exp(2.5*1J)"],
|
| 12 |
+
]
|
| 13 |
+
ARTICLE = r"""<center>
|
| 14 |
+
This application uses Julia and Mandelbrot fractal algorithms.
|
| 15 |
+
These plots show the convergence plot for infinitely composed complex functions <br>
|
| 16 |
+
These functions are based on artist-defined generating functions $f(z)$ with $z /in /mathbb{C}$ as follows<br>
|
| 17 |
+
$$ F(z) = /prod^{/inf} f(z) $$<br>
|
| 18 |
+
Done by dr. Gabriel Lopez<br>
|
| 19 |
+
For more please visit: <a href='https://sites.google.com/view/dr-gabriel-lopez/home'>My Page</a><br>
|
| 20 |
+
</center>"""
|
| 21 |
+
|
| 22 |
+
# interactive function
|
| 23 |
+
def plot_fractal(fractal_type: str, python_function: str):
|
| 24 |
+
frac = FractalGenerator(n=500, max_iter=10)
|
| 25 |
+
if fractal_type == "Julia":
|
| 26 |
+
frac.create_julia(lambda z: eval(python_function))
|
| 27 |
+
elif fractal_type == "Mandelbrot":
|
| 28 |
+
frac.create_mandelbrot()
|
| 29 |
+
else:
|
| 30 |
+
print("Current wrong option: ", fractal_type)
|
| 31 |
+
return frac.plot()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# gradio frontend elements
|
| 35 |
+
in_dropdown = gr.Dropdown(
|
| 36 |
+
choices=["Julia", "Mandelbrot"], label="Select a type of fractal:", value="Julia"
|
| 37 |
+
)
|
| 38 |
+
in_text = gr.Textbox(
|
| 39 |
+
value="sin(z**4 + 1.41)",
|
| 40 |
+
label="Enter function using $z$ as complex-variable. You can use all numpy functions. 1J = /sqrt{-1}",
|
| 41 |
+
placeholder="your own z function",
|
| 42 |
+
lines=4,
|
| 43 |
+
)
|
| 44 |
+
out_plot = gr.Plot(label="Fractal plot")
|
| 45 |
+
|
| 46 |
+
# gradio interface
|
| 47 |
+
gr.Interface(
|
| 48 |
+
inputs=[in_dropdown, in_text],
|
| 49 |
+
outputs=out_plot,
|
| 50 |
+
fn=plot_fractal,
|
| 51 |
+
examples=EXAMPLES,
|
| 52 |
+
title=TITLE,
|
| 53 |
+
description=DESCRIPTION,
|
| 54 |
+
article=ARTICLE,
|
| 55 |
+
).launch()
|
fractal_generator.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
-
from
|
| 2 |
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
| 5 |
-
import
|
| 6 |
-
from enum import Enum
|
| 7 |
|
| 8 |
|
| 9 |
class FractalType(Enum):
|
|
@@ -12,17 +10,17 @@ class FractalType(Enum):
|
|
| 12 |
|
| 13 |
|
| 14 |
class FractalGenerator:
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
def __init__(self, n=256, xlim=(-2, 2), ylim=(-2, 2), thr=2, max_iter=10):
|
| 28 |
self.type_ = None
|
|
@@ -34,49 +32,46 @@ class FractalGenerator:
|
|
| 34 |
self.max_iter = max_iter
|
| 35 |
|
| 36 |
def create_julia(self, complex_function=lambda z: np.sin(z ** 4 + 1.41)):
|
| 37 |
-
"""
|
| 38 |
-
fractal = np.zeros((self.n, self.n), dtype=
|
| 39 |
x_space = np.linspace(self.xlim[0], self.xlim[1], self.n)
|
| 40 |
y_space = np.linspace(self.ylim[0], self.ylim[1], self.n)
|
| 41 |
for ix, x in enumerate(x_space):
|
| 42 |
for iy, y in enumerate(y_space):
|
| 43 |
for i in range(self.max_iter):
|
| 44 |
-
if i == 0:
|
|
|
|
| 45 |
z = complex_function(z)
|
| 46 |
-
if np.abs(z) >= self.thr:
|
|
|
|
|
|
|
| 47 |
fractal[ix, iy] = z
|
| 48 |
self.fractal = np.abs(fractal)
|
| 49 |
self.type_ = FractalType.Julia
|
| 50 |
return self
|
| 51 |
|
| 52 |
def create_mandelbrot(self):
|
| 53 |
-
"""
|
| 54 |
-
fractal = np.zeros((self.n, self.n), dtype=
|
| 55 |
x_space = np.linspace(self.xlim[0], self.xlim[1], self.n)
|
| 56 |
y_space = np.linspace(self.ylim[0], self.ylim[1], self.n)
|
| 57 |
for ix, x in enumerate(x_space):
|
| 58 |
for iy, y in enumerate(y_space):
|
| 59 |
for i in range(self.max_iter):
|
| 60 |
-
if i == 0:
|
|
|
|
| 61 |
z = z ** 2 + complex(x, y)
|
| 62 |
-
if np.abs(z) >= self.thr:
|
|
|
|
|
|
|
| 63 |
fractal[ix, iy] = z
|
| 64 |
self.fractal = np.abs(fractal.transpose())
|
| 65 |
self.type_ = FractalType.Mandelbrot
|
| 66 |
return self
|
| 67 |
|
| 68 |
-
def plot(self,
|
| 69 |
if self.fractal is None:
|
| 70 |
-
print(
|
| 71 |
return None
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
plt.gca().axes.get_yaxis().set_visible(False)
|
| 75 |
-
plt.clim(clim)
|
| 76 |
-
return plt.gcf()
|
| 77 |
-
|
| 78 |
-
def persist_plot(self, filename, container, clim=None, **kwargs):
|
| 79 |
-
if not os.path.isdir(container): os.mkdir(container)
|
| 80 |
-
self.plot(clim=clim, **kwargs)
|
| 81 |
-
plt.savefig(str(Path(container) / filename), png='png', dpi=None)
|
| 82 |
-
plt.close(plt.gcf())
|
|
|
|
| 1 |
+
from enum import Enum
|
| 2 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import plotly.express as px
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class FractalType(Enum):
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
class FractalGenerator:
|
| 13 |
+
"""Creates a single fractal object and either returns it as as a numpy array, plot it or persists it as an pgn
|
| 14 |
+
image. The output of this class is used by FractalTrainingValidationSet to generate training/val sets
|
| 15 |
+
Args:
|
| 16 |
+
complex_function -- complex function to make a Julia fractal
|
| 17 |
+
n -- fractal size will ne n*n
|
| 18 |
+
xlim,ylim -- tuples with the plotting region on the complex plane
|
| 19 |
+
thr -- once a function grows larger that this number is considered to be divergent to infinity
|
| 20 |
+
max_iter -- number of compositions of the complex function with itself
|
| 21 |
+
type_ -- fractal type
|
| 22 |
+
fractal -- numpy array with the fractal
|
| 23 |
+
"""
|
| 24 |
|
| 25 |
def __init__(self, n=256, xlim=(-2, 2), ylim=(-2, 2), thr=2, max_iter=10):
|
| 26 |
self.type_ = None
|
|
|
|
| 32 |
self.max_iter = max_iter
|
| 33 |
|
| 34 |
def create_julia(self, complex_function=lambda z: np.sin(z ** 4 + 1.41)):
|
| 35 |
+
"""Creates a fractal of the Julia family, the fractal is stored inside self.fractal"""
|
| 36 |
+
fractal = np.zeros((self.n, self.n), dtype="complex")
|
| 37 |
x_space = np.linspace(self.xlim[0], self.xlim[1], self.n)
|
| 38 |
y_space = np.linspace(self.ylim[0], self.ylim[1], self.n)
|
| 39 |
for ix, x in enumerate(x_space):
|
| 40 |
for iy, y in enumerate(y_space):
|
| 41 |
for i in range(self.max_iter):
|
| 42 |
+
if i == 0:
|
| 43 |
+
z = complex(x, y)
|
| 44 |
z = complex_function(z)
|
| 45 |
+
if np.abs(z) >= self.thr:
|
| 46 |
+
z = self.thr
|
| 47 |
+
break
|
| 48 |
fractal[ix, iy] = z
|
| 49 |
self.fractal = np.abs(fractal)
|
| 50 |
self.type_ = FractalType.Julia
|
| 51 |
return self
|
| 52 |
|
| 53 |
def create_mandelbrot(self):
|
| 54 |
+
"""Creates a fractal of the Mandelbrot family, the fractal is stored inside self.fractal"""
|
| 55 |
+
fractal = np.zeros((self.n, self.n), dtype="complex")
|
| 56 |
x_space = np.linspace(self.xlim[0], self.xlim[1], self.n)
|
| 57 |
y_space = np.linspace(self.ylim[0], self.ylim[1], self.n)
|
| 58 |
for ix, x in enumerate(x_space):
|
| 59 |
for iy, y in enumerate(y_space):
|
| 60 |
for i in range(self.max_iter):
|
| 61 |
+
if i == 0:
|
| 62 |
+
z = 0
|
| 63 |
z = z ** 2 + complex(x, y)
|
| 64 |
+
if np.abs(z) >= self.thr:
|
| 65 |
+
z = self.thr
|
| 66 |
+
break
|
| 67 |
fractal[ix, iy] = z
|
| 68 |
self.fractal = np.abs(fractal.transpose())
|
| 69 |
self.type_ = FractalType.Mandelbrot
|
| 70 |
return self
|
| 71 |
|
| 72 |
+
def plot(self, **kwargs):
|
| 73 |
if self.fractal is None:
|
| 74 |
+
print("Nothing to plot. Generate a fractal first.")
|
| 75 |
return None
|
| 76 |
+
fig = px.imshow(img=self.fractal, color_continuous_scale="orrd", **kwargs)
|
| 77 |
+
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
numpy==1.19.5
|
|
|
|
| 1 |
+
numpy==1.19.5
|
| 2 |
+
plotly==5.11.0
|
|
|