samila/samila/genimage.py

270 wiersze
8.4 KiB
Python
Czysty Zwykły widok Historia

2021-09-20 19:58:52 +00:00
# -*- coding: utf-8 -*-
"""Samila generative image."""
import json
2021-09-26 20:40:20 +00:00
import random
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
from .functions import _GI_initializer, plot_params_filter, generate_params_filter, save_params_filter
from .functions import get_config, get_data, get_python_version
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
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
from .functions import load_data, load_config, random_equation_gen, nft_storage_upload
from .functions import set_background, rotate
2021-09-26 20:40:20 +00:00
from .params import *
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:
"""
Generative Image class.
2021-09-26 20:40:20 +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)
"""
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
2022-01-04 15:24:12 +00:00
def __init__(self, function1=None, function2=None, data=None, config=None):
"""
Init method.
2022-01-05 21:17:19 +00:00
:param function1: function 1
:type function1: python or lambda function
2022-01-05 21:17:19 +00:00
: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)
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
2022-01-04 15:24:12 +00:00
:param config: generative image config
:type config: (io.IOBase & file)
"""
2022-01-05 21:17:19 +00:00
_GI_initializer(self, function1, function2)
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
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:
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
2022-01-04 15:24:12 +00:00
load_data(self, data)
if self.matplotlib_version != matplotlib.__version__ or \
self.python_version != get_python_version() or \
self.__version__ != SAMILA_VERSION:
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
2022-01-04 15:24:12 +00:00
warn(
VERSION_WARNING.format(
self.matplotlib_version,
self.python_version,
self.__version__),
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
2022-01-04 15:24:12 +00:00
RuntimeWarning)
if self.function1 is None:
if self.function1_str is None:
self.function1_str = random_equation_gen()
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
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:
self.function2_str = random_equation_gen()
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
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,
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
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
"""
generate_params_filter(self, seed, start, step, stop)
2021-09-26 20:40:20 +00:00
self.data1 = []
self.data2 = []
self.missed_points_number = 0
range1 = list(float_range(self.start, self.stop, self.step))
range_prod = itertools.product(range1, range1)
for point in range_prod:
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):
warn(CALCULATION_EXCEPTION_WARNING, RuntimeWarning)
2021-09-26 20:40:20 +00:00
2021-09-27 07:18:06 +00:00
def plot(
self,
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
2022-01-04 15:24:12 +00:00
color=None,
bgcolor=None,
cmap=None,
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
2022-01-04 15:24:12 +00:00
spot_size=None,
size=None,
2022-02-04 11:34:31 +00:00
projection=None,
marker=None,
alpha=None,
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
: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
:param marker: marker type
:type marker: str
:param alpha: point transparency
:type alpha: float
:param linewidth: width of line
:type linewidth: float
:param rotation: desired rotation (in degrees)
:type rotation: float
2021-09-27 07:38:40 +00:00
:return: None
"""
plot_params_filter(
self,
color,
bgcolor,
cmap,
spot_size,
size,
projection,
marker,
alpha,
linewidth,
rotation)
2021-09-26 20:40:20 +00:00
fig = plt.figure()
fig.set_size_inches(self.size[0], self.size[1])
ax = fig.add_subplot(111, projection=self.projection)
set_background(self.bgcolor, fig, ax)
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)
ax = rotate(fig, ax, self.rotation)
2021-09-30 01:14:16 +00:00
self.fig = fig
def nft_storage(
self,
api_key,
upload_data=False,
upload_config=False,
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
:param upload_data: upload data flag
:type upload_data: bool
:param upload_config: upload config flag
:type upload_config: bool
:param depth: image depth
:type depth: float
:param timeout: upload timeout (in seconds)
:type timeout: int
2021-09-30 01:14:16 +00:00
:return: result as dict
"""
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"]
response = nft_storage_upload(
api_key=api_key,
data=buf.getvalue(),
timeout=timeout)
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,
data=json.dumps(get_config(self)),
timeout=timeout)
for key, value in response.items():
result[key]['config'] = value
if upload_data:
response = nft_storage_upload(
api_key=api_key,
data=json.dumps(get_data(self)),
timeout=timeout)
for key, value in response.items():
result[key]['data'] = value
return result
2021-10-12 12:42:34 +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
"""
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
"""
return save_data_file(self, file_adr)
Config json Added (#80) * fix : typo fixed in docstring. * unifx : never mind :). * remove : extra lines removed from Magic section. * add : samilaConfigError added. * edit : DEFAULT_PROJECTION edited. * add : CONFIG_TYPE_ERROR added. * add : filter_float function added. * add : save_config_file function added. * add : load_config function added. * add : minor imports addded to functions.py. * add : some imports added to genimage.py. * fix : whitespace removed. * add : config parameter added. * add : save_config method added. * edit : minor edits in genimage.py. * edit : minor edits in save/load config. * edit: minor edits in save/load config. * add : filter_size added to functions. * update : minor updated due to last change. * edit : minor edit in filter_size. * add : plot_params_filter function added. * add : generate_params_filter added. * remove : unneccessary errors and warnings removed. * update : GenerativeImage __init__ updated due to last commit. * add : _GI_initializer added. * edit : make load_data structure similar to load_config. * fix : typo fixed in functions. * test : some tests added. * test : tests added for warning. * fix : minor bug fixed. * add : filter_size tests added. * add : error tests added. * add : save_config part added. * fix : typo fixed. * tests : minor tests added. * move : function[12]_str moved to __init__ root. * fix : typo fixed in errors.py. * fix : typo fixed in README.md. * fix : typo fixed in CHANGELOG.md. * add : matplotlib_version attribute added. * add : samilaPlotError added. * add : PLOT_DATA_ERROR added. * update : minor updates due to latest changes. * remove : extra tests removed. * edit : PLOT_DATA_ERROR edited. * test : tests added. * edit : minor edits in save_config/data. * add : new error messages added. * test : tests added. * fix : typo fixed. * replace : DATA_NO_DATA_ERROR -> SAVE_NO_DATA_ERROR. * edit : minor edits in plot_params_filter function. * add : two parameter added. * edit : __init__ docstring edited. * fix : typo fixed in genimage. * fix : typo fixed in params.py. * fix : typo fixed in tests. * fix : typo fixed in CHANGELOG.md.
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, file_adr)
def __del__(self):
"""
Destructor.
:return:None
"""
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()