2021-09-20 19:58:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Samila generative image."""
|
2021-09-26 20:40:20 +00:00
|
|
|
import random
|
|
|
|
import itertools
|
2021-11-02 20:22:02 +00:00
|
|
|
import matplotlib
|
2021-09-26 20:40:20 +00:00
|
|
|
import matplotlib.pyplot as plt
|
2022-01-04 15:24:12 +00:00
|
|
|
from .functions import _GI_initializer, plot_params_filter, generate_params_filter
|
|
|
|
from .functions import float_range, save_data_file, save_fig_file, save_fig_buf, save_config_file
|
|
|
|
from .functions import load_data, load_config, random_equation_gen, nft_storage_upload
|
2021-09-26 20:40:20 +00:00
|
|
|
from .params import *
|
2021-10-22 09:18:34 +00:00
|
|
|
from warnings import warn
|
2021-09-26 20:40:20 +00:00
|
|
|
|
2021-09-27 07:18:06 +00:00
|
|
|
|
2021-09-26 20:40:20 +00:00
|
|
|
class GenerativeImage:
|
2021-09-28 12:05:44 +00:00
|
|
|
"""
|
|
|
|
Generative Image class.
|
2021-09-26 20:40:20 +00:00
|
|
|
|
2021-09-28 12:05:44 +00:00
|
|
|
>>> def f1(x, y):
|
|
|
|
... return random.uniform(-1, 1) * x**2 - math.sin(y**3)
|
|
|
|
>>> def f2(x, y):
|
|
|
|
... return random.uniform(-1, 1) * y**3 - math.cos(x**2)
|
|
|
|
>>> GI = GenerativeImage(f1, f2)
|
|
|
|
"""
|
2021-09-29 05:51:55 +00:00
|
|
|
|
2022-01-04 15:24:12 +00:00
|
|
|
def __init__(self, function1=None, function2=None, data=None, config=None):
|
2021-09-28 12:05:44 +00:00
|
|
|
"""
|
|
|
|
Init method.
|
|
|
|
|
|
|
|
:param function1: Function 1
|
|
|
|
:type function1: python or lambda function
|
|
|
|
:param function2: Function 2
|
|
|
|
:type function2: python or lambda function
|
2021-10-22 09:18:34 +00:00
|
|
|
:param data: prior generated data
|
|
|
|
:type data: (io.IOBase & file)
|
2022-01-04 15:24:12 +00:00
|
|
|
:param config: generative image config
|
|
|
|
:type config: (io.IOBase & file)
|
2021-09-28 12:05:44 +00:00
|
|
|
"""
|
2022-01-04 15:24:12 +00:00
|
|
|
_GI_initializer(self)
|
|
|
|
self.matplotlib_version = matplotlib.__version__
|
2021-12-14 15:56:36 +00:00
|
|
|
self.function1 = function1
|
2021-12-18 19:36:15 +00:00
|
|
|
self.function1_str = None
|
2022-01-04 15:24:12 +00:00
|
|
|
self.function2 = function2
|
2021-12-18 19:36:15 +00:00
|
|
|
self.function2_str = None
|
2021-12-14 15:56:36 +00:00
|
|
|
self.fig = None
|
2022-01-04 15:24:12 +00:00
|
|
|
if config is not None:
|
|
|
|
load_config(self, config)
|
|
|
|
if data is not None:
|
|
|
|
load_data(self, data)
|
|
|
|
if self.matplotlib_version != matplotlib.__version__:
|
|
|
|
warn(
|
|
|
|
MATPLOTLIB_VERSION_WARNING.format(
|
|
|
|
self.matplotlib_version),
|
|
|
|
RuntimeWarning)
|
|
|
|
if self.function1 is None:
|
|
|
|
if self.function1_str is None:
|
2021-12-18 19:36:15 +00:00
|
|
|
self.function1_str = random_equation_gen()
|
2022-01-04 15:24:12 +00:00
|
|
|
self.function1 = eval("lambda x,y:" + self.function1_str)
|
|
|
|
if self.function2 is None:
|
|
|
|
if self.function2_str is None:
|
2021-12-18 19:36:15 +00:00
|
|
|
self.function2_str = random_equation_gen()
|
2022-01-04 15:24:12 +00:00
|
|
|
self.function2 = eval("lambda x,y:" + self.function2_str)
|
2021-09-26 20:40:20 +00:00
|
|
|
|
2021-09-27 07:18:06 +00:00
|
|
|
def generate(
|
|
|
|
self,
|
|
|
|
seed=None,
|
2022-01-04 15:24:12 +00:00
|
|
|
start=None,
|
|
|
|
step=None,
|
|
|
|
stop=None):
|
2021-09-27 07:38:15 +00:00
|
|
|
"""
|
|
|
|
Generate a raw format of art.
|
|
|
|
|
|
|
|
:param seed: random seed
|
|
|
|
:type seed: int
|
|
|
|
:param start: range start point
|
|
|
|
:type start: float
|
|
|
|
:param step: range step size
|
|
|
|
:type step: float
|
|
|
|
:param stop: range stop point
|
|
|
|
:type stop: float
|
|
|
|
:return: None
|
|
|
|
"""
|
2022-01-05 21:11:33 +00:00
|
|
|
generate_params_filter(self, seed, start, step, stop)
|
2021-09-26 20:40:20 +00:00
|
|
|
self.data1 = []
|
|
|
|
self.data2 = []
|
2022-01-05 21:11:33 +00:00
|
|
|
range1 = list(float_range(self.start, self.stop, self.step))
|
|
|
|
range2 = list(float_range(self.start, self.stop, self.step))
|
2021-09-26 20:40:20 +00:00
|
|
|
range_prod = list(itertools.product(range1, range2))
|
|
|
|
for item in range_prod:
|
2021-09-28 10:36:11 +00:00
|
|
|
random.seed(self.seed)
|
2021-10-30 06:25:48 +00:00
|
|
|
self.data1.append(self.function1(item[0], item[1]).real)
|
|
|
|
self.data2.append(self.function2(item[0], item[1]).real)
|
2021-09-26 20:40:20 +00:00
|
|
|
|
2021-09-27 07:18:06 +00:00
|
|
|
def plot(
|
|
|
|
self,
|
2022-01-04 15:24:12 +00:00
|
|
|
color=None,
|
|
|
|
bgcolor=None,
|
|
|
|
spot_size=None,
|
|
|
|
size=None,
|
|
|
|
projection=None):
|
2021-09-27 07:38:40 +00:00
|
|
|
"""
|
|
|
|
Plot the generated art.
|
|
|
|
|
|
|
|
:param color: point colors
|
|
|
|
:type color: str
|
2021-09-28 06:21:31 +00:00
|
|
|
:param bgcolor: background color
|
|
|
|
:type bgcolor: str
|
2021-09-27 07:38:40 +00:00
|
|
|
:param spot_size: point spot size
|
|
|
|
:type spot_size: float
|
|
|
|
:param size: figure size
|
|
|
|
:type size: tuple
|
|
|
|
:param projection: projection type
|
|
|
|
:type projection: str
|
|
|
|
:return: None
|
|
|
|
"""
|
2022-01-05 21:11:33 +00:00
|
|
|
plot_params_filter(self, color, bgcolor, spot_size, size, projection)
|
2021-09-26 20:40:20 +00:00
|
|
|
fig = plt.figure()
|
2022-01-05 21:11:33 +00:00
|
|
|
fig.set_size_inches(self.size[0], self.size[1])
|
|
|
|
fig.set_facecolor(self.bgcolor)
|
|
|
|
ax = fig.add_subplot(111, projection=self.projection)
|
|
|
|
ax.set_facecolor(self.bgcolor)
|
2021-09-28 06:21:31 +00:00
|
|
|
ax.scatter(
|
|
|
|
self.data2,
|
|
|
|
self.data1,
|
2021-09-28 06:23:41 +00:00
|
|
|
alpha=DEFAULT_ALPHA,
|
2022-01-05 21:11:33 +00:00
|
|
|
c=self.color,
|
|
|
|
s=self.spot_size)
|
2021-09-28 06:21:31 +00:00
|
|
|
ax.set_axis_off()
|
|
|
|
ax.patch.set_zorder(-1)
|
|
|
|
ax.add_artist(ax.patch)
|
2021-09-30 01:14:16 +00:00
|
|
|
self.fig = fig
|
|
|
|
|
2021-09-30 04:07:01 +00:00
|
|
|
def nft_storage(self, api_key):
|
2021-09-30 01:14:16 +00:00
|
|
|
"""
|
|
|
|
Upload image to nft.storage.
|
|
|
|
|
|
|
|
:param api_key: API key
|
|
|
|
:type api_key: str
|
|
|
|
:return: result as dict
|
|
|
|
"""
|
2021-10-12 12:49:12 +00:00
|
|
|
response = save_fig_buf(self.fig)
|
2021-10-12 13:06:42 +00:00
|
|
|
if not response["status"]:
|
2021-10-12 12:49:12 +00:00
|
|
|
return {"status": False, "message": response["message"]}
|
|
|
|
buf = response["buffer"]
|
2021-09-30 04:07:01 +00:00
|
|
|
response = nft_storage_upload(api_key=api_key, data=buf.getvalue())
|
|
|
|
return response
|
2021-10-12 12:42:34 +00:00
|
|
|
|
2021-10-14 13:02:25 +00:00
|
|
|
def save_image(self, file_adr, depth=1):
|
2021-10-12 12:42:34 +00:00
|
|
|
"""
|
|
|
|
Save generated image.
|
|
|
|
|
2021-11-04 08:42:46 +00:00
|
|
|
:param file_adr: file address
|
2021-10-12 12:42:34 +00:00
|
|
|
:type file_adr: str
|
2021-10-15 15:12:21 +00:00
|
|
|
:param depth: image depth
|
|
|
|
:type depth: float
|
2021-10-12 12:42:34 +00:00
|
|
|
:return: result as dict
|
|
|
|
"""
|
2021-10-15 15:12:21 +00:00
|
|
|
return save_fig_file(figure=self.fig, file_adr=file_adr, depth=depth)
|
2021-10-22 07:33:07 +00:00
|
|
|
|
2021-10-22 09:38:34 +00:00
|
|
|
def save_data(self, file_adr='data.json'):
|
2021-10-22 07:33:07 +00:00
|
|
|
"""
|
2021-10-23 17:12:36 +00:00
|
|
|
Save data into a file.
|
2021-10-22 07:33:07 +00:00
|
|
|
|
2021-11-04 08:42:46 +00:00
|
|
|
:param file_adr: file address
|
2021-10-22 07:33:07 +00:00
|
|
|
:type file_adr: str
|
|
|
|
:return: result as dict
|
|
|
|
"""
|
2022-01-05 21:11:33 +00:00
|
|
|
return save_data_file(self, matplotlib.__version__, file_adr)
|
2022-01-04 15:24:12 +00:00
|
|
|
|
|
|
|
def save_config(self, file_adr='config.json'):
|
|
|
|
"""
|
|
|
|
Save config into a file.
|
|
|
|
|
|
|
|
:param file_adr: file address
|
|
|
|
:type file_adr: str
|
|
|
|
:return: result as a dict
|
|
|
|
"""
|
|
|
|
return save_config_file(self, matplotlib.__version__, file_adr)
|