Merge pull request #79 from sepandhaghighi/random_eq

Random equation
pull/78/head
Sadra Sabouri 2021-12-19 09:36:12 +03:30 zatwierdzone przez GitHub
commit 68c0a96aa9
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
9 zmienionych plików z 91 dodań i 22 usunięć

Wyświetl plik

@ -48,7 +48,7 @@ jobs:
- name: Other tests
run: |
python -m vulture samila/ setup.py --min-confidence 65 --exclude=__init__.py --sort-by-size
python -m bandit -r samila -s B311
python -m bandit -r samila -s B311,B307
python -m pydocstyle -v --match-dir=samila
if: matrix.python-version == 3.7
- name: Codecov

Wyświetl plik

@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- Random equations mode
- `function1_str` attribute
- `function2_str` attribute
### Changed
- Test system updated
## [0.3] - 2021-11-10
### Added
- Discord channel

Wyświetl plik

@ -99,12 +99,28 @@ Samila is a generative art generator written in Python, Samila let's you create
## Usage
### Magic
```pycon
>>> import matplotlib.pyplot as plt
>>> from samila import GenerativeImage
>>> g = GenerativeImage()
>>> g.generate()
>>> g.plot()
>>> g.seed
70120
>>> g.function1_str
'random.uniform(-1,1)*abs((x**2)*y)-random.uniform(-1,1)*math.ceil(y-x)+random.uniform(-1,1)*math.ceil(x*(y**3))-random.uniform(-1,1)*math.cos(x-y)+random.uniform(-1,1)*math.floor((y**2)*x)+random.uniform(-1,1)*math.cos(x*(y**3))+random.uniform(-1,1)*math.floor(x)-random.uniform(-1,1)*math.sin(y*(x**3))'
>>> g.function2_str
'random.uniform(-1,1)*math.floor(x*(y**3))-random.uniform(-1,1)*math.cos(y-x)+random.uniform(-1,1)*math.floor(x)+random.uniform(-1,1)*abs(y*(x**3))-random.uniform(-1,1)*math.ceil(y**2)-random.uniform(-1,1)*math.ceil((x**2)*y)+random.uniform(-1,1)*abs((y**2)*x)-random.uniform(-1,1)*abs(x)'
>>> plt.show()
```
* `function1_str` and `function2_str` are only available in this mode
<img src="https://github.com/sepandhaghighi/samila/raw/master/otherfiles/images/7.png">
### Basic
```pycon
>>> import random
>>> import math
>>> import matplotlib.pyplot as plt
>>> from samila import GenerativeImage
>>> def f1(x,y):
result = random.uniform(-1,1) * x**2 - math.sin(y**2) + abs(y-x)
return result

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 209 KiB

Wyświetl plik

@ -4,12 +4,34 @@
import requests
import io
import json
import random
from .params import Projection, DEFAULT_PROJECTION, VALID_COLORS, NFT_STORAGE_API, OVERVIEW
from .params import DATA_TYPE_ERROR, DATA_PARSING_ERROR, NO_FIG_ERROR_MESSAGE
from .params import FIG_SAVE_SUCCESS_MESSAGE, NFT_STORAGE_SUCCESS_MESSAGE, DATA_SAVE_SUCCESS_MESSAGE
from .params import ELEMENTS_LIST, ARGUMENTS_LIST, OPERATORS_LIST
from .errors import samilaDataError
def random_equation_gen():
"""
Generate random equation.
:return: equation as str
"""
num_elements = random.randint(2, len(ELEMENTS_LIST) + 3)
result = ""
index = 1
random_coef = "random.uniform(-1,1)"
while(index <= num_elements):
argument = random.choice(ARGUMENTS_LIST)
result = result + \
random.choice(ELEMENTS_LIST).format(random_coef, argument)
if index < num_elements:
result = result + random.choice(OPERATORS_LIST)
index = index + 1
return result
def float_range(start, stop, step):
"""
Generate float range.
@ -240,6 +262,6 @@ def load_data(data):
try:
data = json.load(data)
return data['data1'], data['data2'], data['matplotlib_version']
except:
except Exception:
raise samilaDataError(DATA_PARSING_ERROR)
raise samilaDataError(DATA_TYPE_ERROR)

Wyświetl plik

@ -4,7 +4,8 @@ import random
import itertools
import matplotlib
import matplotlib.pyplot as plt
from .functions import float_range, filter_color, filter_projection, nft_storage_upload, save_data_file, save_fig_file, save_fig_buf, load_data
from .functions import float_range, filter_color, filter_projection, nft_storage_upload
from .functions import save_data_file, save_fig_file, save_fig_buf, load_data, random_equation_gen
from .errors import samilaGenerateError
from .params import *
from warnings import warn
@ -32,18 +33,24 @@ class GenerativeImage:
:param data: prior generated data
:type data: (io.IOBase & file)
"""
self.function1 = function1
self.function2 = function2
self.function1_str = None
self.function2_str = None
self.fig = None
if function1 is None or function2 is None:
if data is None:
warn(NOTHING_PROVIDED_WARNING, RuntimeWarning)
self.function1_str = random_equation_gen()
self.function2_str = random_equation_gen()
self.function1 = eval("lambda x,y:" + self.function1_str)
self.function2 = eval("lambda x,y:" + self.function2_str)
else:
warn(JUST_DATA_WARNING, RuntimeWarning)
if data is not None:
self.data1, self.data2, matplotlib_version = load_data(data)
if matplotlib_version != matplotlib.__version__:
warn(MATPLOTLIB_VERSION_WARNING.format(matplotlib_version), RuntimeWarning)
self.function1 = function1
self.function2 = function2
self.fig = None
warn(MATPLOTLIB_VERSION_WARNING.format(
matplotlib_version), RuntimeWarning)
def generate(
self,

Wyświetl plik

@ -32,7 +32,6 @@ DATA_TYPE_ERROR = "Provided data file is not supported. It should be either file
DATA_PARSING_ERROR = "Provided data format is wrong. It should be in JSON format including data1 and data2 fields."
NO_FUNCTION_ERROR = "At least one of the given functions are None."
JUST_DATA_WARNING = "Just data is provided, generate method is not available in this mode."
NOTHING_PROVIDED_WARNING = "Neither function nor data is provided."
MATPLOTLIB_VERSION_WARNING = "Source matplotlib version({0}) is different from yours, plots may be different."
@ -50,3 +49,30 @@ class Projection(Enum):
LAMBERT = "lambert"
MOLLWEIDE = "mollweide"
RECTILINEAR = "rectilinear"
ELEMENTS_LIST = [
"{0}*math.cos({1})",
"{0}*math.sin({1})",
"{0}*{1}",
"{0}*abs({1})",
"{0}*math.ceil({1})",
"{0}*math.floor({1})"]
ARGUMENTS_LIST = [
"x*y",
"x",
"y",
"y-x",
"x-y",
"x+y",
"x**2",
"y**2",
"(x**2)*y",
"(y**2)*x",
"(x**2)*(y**3)",
"(x**3)*(y**2)",
"x*(y**3)",
"y*(x**3)"]
OPERATORS_LIST = ["+", "-"]

Wyświetl plik

@ -6,13 +6,7 @@
>>> import time
>>> from samila import GenerativeImage, Projection
>>> from samila.params import VALID_COLORS
>>> def f1(x,y):
... result = random.uniform(-1,1) * x**2 - math.sin(y**2) + abs(y-x)
... return result
>>> def f2(x,y):
... result = random.uniform(-1,1) * y**3 - math.cos(x**2) + 2*x
... return result
>>> g = GenerativeImage(f1,f2)
>>> g = GenerativeImage()
>>> g.generate()
>>> g.plot()
>>> NFT_STORAGE_API_KEY = os.environ["NFT_STORAGE_API_KEY"]
@ -22,13 +16,13 @@
>>> random_bgcolor = random.choice(VALID_COLORS)
>>> g.plot(projection=random_projection,color=random_color,bgcolor=random_bgcolor)
>>> counter = 0
>>> try_limit = 5
>>> try_limit = 10
>>> status = False
>>> while(status == False and counter<try_limit):
... result = g.nft_storage(api_key=NFT_STORAGE_API_KEY)
... counter = counter + 1
... status = result["status"]
... time.sleep(5)
... time.sleep(10)
>>> status
True
"""

Wyświetl plik

@ -4,8 +4,6 @@
>>> import json
>>> from samila import *
>>> from pytest import warns
>>> with warns(RuntimeWarning, match="Neither function nor data is provided."):
... g = GenerativeImage()
>>> g = GenerativeImage(lambda x,y: 0, lambda x,y: 0)
>>> g.generate(step=0.1)
>>> result = g.save_data()