2021-09-20 19:58:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Samila generative image."""
|
2022-11-22 09:31:58 +00:00
|
|
|
import json
|
2021-09-26 20:40:20 +00:00
|
|
|
import random
|
2022-02-04 12:25:05 +00:00
|
|
|
import gc
|
2021-09-26 20:40:20 +00:00
|
|
|
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-04-03 06:35:52 +00:00
|
|
|
from .functions import _GI_initializer, plot_params_filter, generate_params_filter, save_params_filter
|
2023-01-06 07:06:21 +00:00
|
|
|
from .functions import get_config, get_data, get_python_version
|
2022-01-04 15:24:12 +00:00
|
|
|
from .functions import float_range, save_data_file, save_fig_file, save_fig_buf, save_config_file
|
2022-11-10 14:22:52 +00:00
|
|
|
from .functions import load_data, load_config, random_equation_gen, nft_storage_upload
|
2023-03-26 15:00:40 +00:00
|
|
|
from .functions import set_background, rotate
|
2021-09-26 20:40:20 +00:00
|
|
|
from .params import *
|
2022-11-27 12:37:43 +00:00
|
|
|
from warnings import warn, catch_warnings, simplefilter
|
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.
|
|
|
|
|
2022-01-05 21:17:19 +00:00
|
|
|
:param function1: function 1
|
2021-09-28 12:05:44 +00:00
|
|
|
:type function1: python or lambda function
|
2022-01-05 21:17:19 +00:00
|
|
|
:param function2: function 2
|
2021-09-28 12:05:44 +00:00
|
|
|
: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-05 21:17:19 +00:00
|
|
|
_GI_initializer(self, function1, function2)
|
2022-01-04 15:24:12 +00:00
|
|
|
if config is not None:
|
|
|
|
load_config(self, config)
|
2022-01-05 21:24:58 +00:00
|
|
|
elif data is not None:
|
2022-01-04 15:24:12 +00:00
|
|
|
load_data(self, data)
|
2023-01-06 07:06:21 +00:00
|
|
|
if self.matplotlib_version != matplotlib.__version__ or \
|
2023-03-31 14:07:17 +00:00
|
|
|
self.python_version != get_python_version() or \
|
|
|
|
self.__version__ != SAMILA_VERSION:
|
2022-01-04 15:24:12 +00:00
|
|
|
warn(
|
2023-01-06 07:06:21 +00:00
|
|
|
VERSION_WARNING.format(
|
|
|
|
self.matplotlib_version,
|
2023-03-31 14:07:17 +00:00
|
|
|
self.python_version,
|
|
|
|
self.__version__),
|
2022-01-04 15:24:12 +00:00
|
|
|
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-11-10 14:22:52 +00:00
|
|
|
self.missed_points_number = 0
|
2022-01-05 21:11:33 +00:00
|
|
|
range1 = list(float_range(self.start, self.stop, self.step))
|
2022-11-10 14:22:52 +00:00
|
|
|
range_prod = itertools.product(range1, range1)
|
2022-04-30 14:19:40 +00:00
|
|
|
for point in range_prod:
|
2022-11-10 14:22:52 +00:00
|
|
|
random.seed(self.seed)
|
|
|
|
try:
|
|
|
|
data1_ = self.function1(point[0], point[1]).real
|
|
|
|
data2_ = self.function2(point[0], point[1]).real
|
|
|
|
self.data1.append(data1_)
|
|
|
|
self.data2.append(data2_)
|
|
|
|
except Exception:
|
|
|
|
self.missed_points_number += 1
|
|
|
|
if len(self.data1) < (len(range1) ** 2):
|
2022-04-30 14:19:40 +00:00
|
|
|
warn(CALCULATION_EXCEPTION_WARNING, RuntimeWarning)
|
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,
|
2022-08-27 19:57:20 +00:00
|
|
|
cmap=None,
|
2022-01-04 15:24:12 +00:00
|
|
|
spot_size=None,
|
|
|
|
size=None,
|
2022-02-04 11:34:31 +00:00
|
|
|
projection=None,
|
2022-11-05 22:16:28 +00:00
|
|
|
marker=None,
|
2022-03-25 12:55:01 +00:00
|
|
|
alpha=None,
|
2023-03-26 15:00:40 +00:00
|
|
|
linewidth=None,
|
|
|
|
rotation=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
|
2022-08-27 19:57:20 +00:00
|
|
|
:param cmap: color map
|
|
|
|
:type cmap: matplotlib.colors.Colormap or list of colors
|
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
|
2022-11-05 22:16:28 +00:00
|
|
|
:param marker: marker type
|
|
|
|
:type marker: str
|
2022-02-04 18:26:03 +00:00
|
|
|
:param alpha: point transparency
|
|
|
|
:type alpha: float
|
2022-03-25 12:55:01 +00:00
|
|
|
:param linewidth: width of line
|
|
|
|
:type linewidth: float
|
2023-03-26 15:00:40 +00:00
|
|
|
:param rotation: desired rotation (in degrees)
|
|
|
|
:type rotation: float
|
2021-09-27 07:38:40 +00:00
|
|
|
:return: None
|
|
|
|
"""
|
2022-02-04 18:26:03 +00:00
|
|
|
plot_params_filter(
|
|
|
|
self,
|
|
|
|
color,
|
|
|
|
bgcolor,
|
2022-08-27 19:57:20 +00:00
|
|
|
cmap,
|
2022-02-04 18:26:03 +00:00
|
|
|
spot_size,
|
|
|
|
size,
|
|
|
|
projection,
|
2022-11-05 22:16:28 +00:00
|
|
|
marker,
|
2022-03-25 12:55:01 +00:00
|
|
|
alpha,
|
2023-03-26 15:00:40 +00:00
|
|
|
linewidth,
|
|
|
|
rotation)
|
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])
|
|
|
|
ax = fig.add_subplot(111, projection=self.projection)
|
2022-05-19 21:49:51 +00:00
|
|
|
set_background(self.bgcolor, fig, ax)
|
2022-11-27 12:37:43 +00:00
|
|
|
with catch_warnings():
|
|
|
|
simplefilter("ignore")
|
|
|
|
ax.scatter(
|
|
|
|
self.data2,
|
|
|
|
self.data1,
|
|
|
|
alpha=self.alpha,
|
|
|
|
c=self.color,
|
|
|
|
cmap=self.cmap,
|
|
|
|
s=self.spot_size,
|
|
|
|
lw=self.linewidth,
|
|
|
|
marker=self.marker)
|
2021-09-28 06:21:31 +00:00
|
|
|
ax.set_axis_off()
|
|
|
|
ax.patch.set_zorder(-1)
|
|
|
|
ax.add_artist(ax.patch)
|
2023-03-26 15:00:40 +00:00
|
|
|
ax = rotate(fig, ax, self.rotation)
|
2021-09-30 01:14:16 +00:00
|
|
|
self.fig = fig
|
|
|
|
|
2022-11-22 09:31:58 +00:00
|
|
|
def nft_storage(
|
|
|
|
self,
|
|
|
|
api_key,
|
|
|
|
upload_data=False,
|
|
|
|
upload_config=False,
|
2023-03-26 21:39:43 +00:00
|
|
|
depth=None,
|
|
|
|
timeout=3000):
|
2021-09-30 01:14:16 +00:00
|
|
|
"""
|
|
|
|
Upload image to nft.storage.
|
|
|
|
|
|
|
|
:param api_key: API key
|
|
|
|
:type api_key: str
|
2022-11-22 09:31:58 +00:00
|
|
|
:param upload_data: upload data flag
|
|
|
|
:type upload_data: bool
|
|
|
|
:param upload_config: upload config flag
|
|
|
|
:type upload_config: bool
|
2022-01-29 13:05:02 +00:00
|
|
|
:param depth: image depth
|
|
|
|
:type depth: float
|
2023-03-26 21:39:43 +00:00
|
|
|
:param timeout: upload timeout (in seconds)
|
|
|
|
:type timeout: int
|
2021-09-30 01:14:16 +00:00
|
|
|
:return: result as dict
|
|
|
|
"""
|
2022-04-03 06:35:52 +00:00
|
|
|
save_params_filter(self, depth)
|
|
|
|
response = save_fig_buf(self.fig, self.depth)
|
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"]
|
2023-03-26 21:39:43 +00:00
|
|
|
response = nft_storage_upload(
|
|
|
|
api_key=api_key,
|
|
|
|
data=buf.getvalue(),
|
|
|
|
timeout=timeout)
|
2022-11-22 09:31:58 +00:00
|
|
|
if upload_config == False and upload_data == False:
|
|
|
|
return response
|
|
|
|
result = {key: {'image': value} for key, value in response.items()}
|
|
|
|
if upload_config:
|
|
|
|
response = nft_storage_upload(
|
|
|
|
api_key=api_key,
|
2023-03-26 21:39:43 +00:00
|
|
|
data=json.dumps(get_config(self)),
|
|
|
|
timeout=timeout)
|
2022-11-22 09:31:58 +00:00
|
|
|
for key, value in response.items():
|
|
|
|
result[key]['config'] = value
|
|
|
|
if upload_data:
|
|
|
|
response = nft_storage_upload(
|
|
|
|
api_key=api_key,
|
2023-03-26 21:39:43 +00:00
|
|
|
data=json.dumps(get_data(self)),
|
|
|
|
timeout=timeout)
|
2022-11-22 09:31:58 +00:00
|
|
|
for key, value in response.items():
|
|
|
|
result[key]['data'] = value
|
|
|
|
return result
|
2021-10-12 12:42:34 +00:00
|
|
|
|
2022-04-03 06:35:52 +00:00
|
|
|
def save_image(self, file_adr, depth=None):
|
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
|
|
|
|
"""
|
2022-04-03 06:35:52 +00:00
|
|
|
save_params_filter(self, depth)
|
|
|
|
return save_fig_file(self.fig, file_adr, self.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:20:03 +00:00
|
|
|
return save_data_file(self, 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
|
|
|
|
"""
|
2022-01-05 21:20:03 +00:00
|
|
|
return save_config_file(self, file_adr)
|
2022-02-04 12:25:05 +00:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
"""
|
2022-04-06 12:40:16 +00:00
|
|
|
Destructor.
|
2022-02-04 12:25:05 +00:00
|
|
|
|
|
|
|
:return:None
|
|
|
|
"""
|
2022-04-06 12:40:16 +00:00
|
|
|
try:
|
|
|
|
del self.data1
|
|
|
|
del self.data2
|
|
|
|
if self.fig is not None:
|
|
|
|
self.fig.clf()
|
|
|
|
plt.close(self.fig)
|
|
|
|
gc.collect()
|
|
|
|
except Exception:
|
|
|
|
gc.collect()
|