Skip to content

Commit

Permalink
Generate mode (#219)
Browse files Browse the repository at this point in the history
* feat : GenerateMode enum added

* feat : mode added to generate method

* fix : _GI_initializer updated

* fix : generate_params_filter function updated

* fix : __init__.py updated

* fix : minor bug in generate method fixed

* fix : generate_params_filter function updated

* fix : minor bug in generate method fixed

* update : readme updated.

* update : save/load config functions update accordingly.

* test : a test for generation_mode added.

* log : changes logged.

* test : more tests added.

* doc : minor edit in generate mode section in README.md

* fix : autopep8

* doc : generate_mode example updated

---------

Co-authored-by: Sadra Sabouri <[email protected]>
  • Loading branch information
sepandhaghighi and sadrasabouri authored Aug 15, 2024
1 parent 04d21c0 commit e895b97
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- `deprecated` function
### Changed
- `mode` parameter added to `generate` method
- `README.md` updated
## [1.2] - 2024-06-25
### Added
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ Samila is a generative art generator written in Python, Samila lets you create i
```
<img src="https://github.com/sepandhaghighi/samila/raw/master/otherfiles/images/1.png">

### Generation mode
```pycon
>>> from samila import GenerateMode
>>> g = GenerativeImage(f1, f2)
>>> g.generate(mode=GenerateMode.F1)
>>> g.plot()
>>> g.seed
883114
>>> plt.show()
```
<img src="https://github.com/sepandhaghighi/samila/raw/master/otherfiles/images/10.png">

* Supported modes : `F1_VS_F2`, `F2_VS_F1`, `F1`, and `F2`
* Default mode is `F1_VS_F2`

### Projection
```pycon
>>> from samila import Projection
Expand Down
Binary file added otherfiles/images/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion samila/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""Samila modules."""
from .genimage import GenerativeImage
from .params import Projection, Marker, Gateway, VALID_COLORS, SAMILA_VERSION
from .params import Projection, GenerateMode, Marker, Gateway, VALID_COLORS, SAMILA_VERSION
from .errors import samilaDataError, samilaGenerateError
__version__ = SAMILA_VERSION
33 changes: 28 additions & 5 deletions samila/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from functools import wraps
from .params import SAMILA_VERSION
from .params import DEFAULT_MARKER, DEFAULT_START, DEFAULT_STOP, DEFAULT_STEP, DEFAULT_COLOR, DEFAULT_IMAGE_SIZE, DEFAULT_DEPTH
from .params import DEFAULT_CMAP_NAME, DEFAULT_CMAP_RANGE, DEFAULT_ROTATION
from .params import DEFAULT_CMAP_NAME, DEFAULT_CMAP_RANGE, DEFAULT_ROTATION, DEFAULT_GENERATE_MODE
from .params import DEFAULT_BACKGROUND_COLOR, DEFAULT_SPOT_SIZE, DEFAULT_PROJECTION, DEFAULT_ALPHA, DEFAULT_LINEWIDTH
from .params import Projection, Marker, VALID_COLORS, HEX_COLOR_PATTERN, NFT_STORAGE_API, OVERVIEW
from .params import Projection, GenerateMode, Marker, VALID_COLORS, HEX_COLOR_PATTERN, NFT_STORAGE_API, OVERVIEW
from .params import DATA_TYPE_ERROR, DATA_FORMAT_ERROR, CONFIG_TYPE_ERROR, CONFIG_FORMAT_ERROR, PLOT_DATA_ERROR, CONFIG_NO_STR_FUNCTION_ERROR
from .params import NO_FIG_ERROR_MESSAGE, FIG_SAVE_SUCCESS_MESSAGE, NFT_STORAGE_SUCCESS_MESSAGE, SAVE_NO_DATA_ERROR
from .params import INVALID_COLOR_TYPE_ERROR, COLOR_SIZE_ERROR
Expand Down Expand Up @@ -305,6 +305,20 @@ def filter_projection(projection):
return None


def filter_generate_mode(generate_mode):
"""
Filter given generate-mode.
:param generate_mode: given generate-mode
:type generate_mode: GenerateMode enum
:return: filtered version of generate-mode
"""
if isinstance(generate_mode, GenerateMode):
generate_mode_value = generate_mode.value
return generate_mode_value
return None


def filter_marker(marker):
"""
Filter given marker.
Expand Down Expand Up @@ -459,7 +473,8 @@ def generate_params_filter(
seed=None,
start=None,
step=None,
stop=None):
stop=None,
mode=None):
"""
Filter generate method parameters.
Expand All @@ -473,9 +488,14 @@ def generate_params_filter(
:type step: float
:param stop: range stop point
:type stop: float
:param mode: generate mode
:type mode: GenerateMode enum
:return: None
"""
start, step, stop = map(filter_float, [start, step, stop])
generate_mode = filter_generate_mode(mode)
if generate_mode is None:
generate_mode = g.generate_mode
if start is None:
start = g.start
if step is None:
Expand All @@ -486,7 +506,7 @@ def generate_params_filter(
seed = g.seed
if g.seed is None:
seed = random.randint(SEED_LOWER_BOUND, SEED_UPPER_BOUND)
g.seed, g.start, g.step, g.stop = seed, start, step, stop
g.seed, g.start, g.step, g.stop, g.generate_mode = seed, start, step, stop, generate_mode


def save_params_filter(g, depth=None):
Expand Down Expand Up @@ -534,6 +554,7 @@ def _GI_initializer(g, function1, function2):
g.spot_size = DEFAULT_SPOT_SIZE
g.size = DEFAULT_IMAGE_SIZE
g.projection = DEFAULT_PROJECTION
g.generate_mode = DEFAULT_GENERATE_MODE
g.marker = DEFAULT_MARKER
g.alpha = DEFAULT_ALPHA
g.linewidth = DEFAULT_LINEWIDTH
Expand Down Expand Up @@ -660,7 +681,8 @@ def get_config(g):
"seed": g.seed,
"start": g.start,
"step": g.step,
"stop": g.stop
"stop": g.stop,
"mode": g.generate_mode
}
config['plot'] = {
"color": g.color,
Expand Down Expand Up @@ -896,6 +918,7 @@ def load_config(g, config):
g.start = generate_config.get("start", DEFAULT_START)
g.step = generate_config.get("step", DEFAULT_STEP)
g.stop = generate_config.get("stop", DEFAULT_STOP)
g.generate_mode = generate_config.get("mode", DEFAULT_GENERATE_MODE)
plot_config = config.get("plot")
if plot_config is not None:
g.color = plot_config.get("color", DEFAULT_COLOR)
Expand Down
23 changes: 18 additions & 5 deletions samila/genimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def generate(
seed=None,
start=None,
step=None,
stop=None):
stop=None,
mode=None):
"""
Generate a raw format of art.
Expand All @@ -89,19 +90,31 @@ def generate(
:type step: float
:param stop: range stop point
:type stop: float
:param mode: generate mode
:type mode: GenerateMode enum
:return: None
"""
generate_params_filter(self, seed, start, step, stop)
generate_params_filter(self, seed, start, step, stop, mode)
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:
for index, point in enumerate(range_prod):
random.seed(self.seed)
try:
data1_ = self.function1(point[0], point[1]).real
data2_ = self.function2(point[0], point[1]).real
if self.generate_mode == "f1_vs_f2":
data1_ = self.function1(point[0], point[1]).real
data2_ = self.function2(point[0], point[1]).real
elif self.generate_mode == "f2_vs_f1":
data2_ = self.function1(point[0], point[1]).real
data1_ = self.function2(point[0], point[1]).real
elif self.generate_mode == "f2":
data2_ = index
data1_ = self.function2(point[0], point[1]).real
elif self.generate_mode == "f1":
data2_ = index
data1_ = self.function1(point[0], point[1]).real
self.data1.append(data1_)
self.data2.append(data2_)
except Exception:
Expand Down
16 changes: 16 additions & 0 deletions samila/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
DEFAULT_DEPTH = 1
DEFAULT_ROTATION = 0.0
DEFAULT_PROJECTION = "rectilinear"
DEFAULT_GENERATE_MODE = "f1_vs_f2"
DEFAULT_MARKER = "."
SEED_LOWER_BOUND = 0
SEED_UPPER_BOUND = 2**20
Expand Down Expand Up @@ -56,6 +57,21 @@
DEPRECATION_WARNING = "`{}` is deprecated and may be removed in future releases."


class GenerateMode(Enum):
"""
Samila generate mode class.
>>> import samila
>>> generate_mode = samila.GenerateMode.F2_VS_F1
"""

DEFAULT = DEFAULT_GENERATE_MODE
F1_VS_F2 = "f1_vs_f2"
F2_VS_F1 = "f2_vs_f1"
F1 = "f1"
F2 = "f2"


class Gateway(Enum):
"""
IPFS gateway class.
Expand Down
16 changes: 15 additions & 1 deletion test/overall_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
>>> import matplotlib
>>> def guard(*args, **kwargs):
... raise Exception("No internet connection!")
>>> from samila import GenerativeImage, Projection, Marker
>>> from samila import GenerativeImage, Projection, Marker, GenerateMode
>>> from samila.functions import is_same_data
>>> import pickle
>>> def f1(x,y):
Expand Down Expand Up @@ -370,6 +370,20 @@
True
>>> g.function2_str == g_.function2_str
True
>>> random.seed(22)
>>> g = GenerativeImage()
>>> g.generate(start=-2*math.pi, step=0.1, stop=math.pi/2, mode=GenerateMode.F1)
>>> g.generate_mode == GenerateMode.F1.value
True
>>> g.generate(start=-2*math.pi, step=0.1, stop=math.pi/2, mode=GenerateMode.F2)
>>> g.generate_mode == GenerateMode.F2.value
True
>>> g.generate(start=-2*math.pi, step=0.1, stop=math.pi/2, mode=GenerateMode.F2)
>>> g.generate_mode == GenerateMode.F2.value
True
>>> g.generate(start=-2*math.pi, step=0.1, stop=math.pi/2, mode=GenerateMode.F2_VS_F1)
>>> g.generate_mode == GenerateMode.F2_VS_F1.value
True
>>> os.remove("test.png")
>>> os.remove("test2.png")
>>> os.remove("data.json")
Expand Down

0 comments on commit e895b97

Please sign in to comment.