Skip to content

Commit

Permalink
Data/Config Upload to IPFS (#164)
Browse files Browse the repository at this point in the history
* add : `get_config` and `get_data`.

* add : `upload_data` and `upload_config` parameters added to `nft_storage` method.

* log : chages logged. (#144, #145)

* edit : config and data are now json dumped.

* doc : document updated.

* test : tests added.

* fix : minor issues fixed in tests.

* change : minor changes applied.

* test : test splited for inreasing coverage.
  • Loading branch information
sadrasabouri authored Nov 22, 2022
1 parent b8967ac commit f21f82c
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Added
- `Marker` enum
- `get_data` function
- `get_config` function
### Changed
- `marker` parameter added to `plot` method
- `upload_data` parameter added to `nft_storage` method
- `upload_config` parameter added to `nft_storage` method
- `generate` method optimized
- Test system modified
- `Python 3.11` added to `test.yml`
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ Upload generated image directly to [NFT.storage](https://NFT.storage)
{'status': True, 'message': 'FILE_LINK'}
```

You can also upload your config/data to nft storage as follows:
```pycon
>>> g.nft_storage(api_key="API_KEY", upload_config=True)
{'status': {'image': True, 'config':True}, 'message': {'image':'IMAGE_FILE_LINK', 'config':'CONFIG_FILE_LINK'}
```
or
```pycon
>>> g.nft_storage(api_key="API_KEY", upload_data=True)
{'status': {'image': True, 'data':True}, 'message': {'image':'IMAGE_FILE_LINK', 'data':'DATA_FILE_LINK'}
```

### Save image
Save generated image

Expand Down
27 changes: 26 additions & 1 deletion examples/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,31 @@
"source": [
"g1.nft_storage(api_key=\"YOUR_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also upload your config/data to nft storage as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1.nft_storage(api_key=\"YOUR_API_KEY\", upload_config=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1.nft_storage(api_key=\"YOUR_API_KEY\", upload_data=True)"
]
}
],
"metadata": {
Expand Down Expand Up @@ -429,7 +454,7 @@
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
}
}
},
Expand Down
66 changes: 45 additions & 21 deletions samila/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,26 @@ def save_data_file(g, file_adr):
:type file_adr: str
:return: result as dict
"""
data = get_data(g)
result = {"status": True, "message": DATA_SAVE_SUCCESS_MESSAGE}
try:
with open(file_adr, 'w') as fp:
json.dump(data, fp)
result["message"] = os.path.abspath(file_adr)
except Exception as e:
result["status"] = False
result["message"] = str(e)
return result


def get_data(g):
"""
Return data.
:param g: generative image instance
:type g: GenerativeImage
:return: data as a dict
"""
matplotlib_version = matplotlib.__version__
data = {}
if g.data1 is None or g.data2 is None:
Expand All @@ -521,40 +541,30 @@ def save_data_file(g, file_adr):
"depth": g.depth
}
data['matplotlib_version'] = matplotlib_version
result = {"status": True, "message": DATA_SAVE_SUCCESS_MESSAGE}
try:
with open(file_adr, 'w') as fp:
json.dump(data, fp)
result["message"] = os.path.abspath(file_adr)
except Exception as e:
result["status"] = False
result["message"] = str(e)
return result
return data


def save_config_file(g, file_adr):
def get_config(g):
"""
Save config as file.
Return config.
:param g: generative image instance
:type g: GenerativeImage
:param file_adr: file address
:type file_adr: str
:return: result as dict
:return: config as a dict
"""
matplotlib_version = matplotlib.__version__
data = {}
config = {}
if g.function1_str is None or g.function2_str is None:
raise samilaConfigError(CONFIG_NO_STR_FUNCTION_ERROR)
data['f1'] = g.function1_str
data['f2'] = g.function2_str
data['generate'] = {
config['f1'] = g.function1_str
config['f2'] = g.function2_str
config['generate'] = {
"seed": g.seed,
"start": g.start,
"step": g.step,
"stop": g.stop
}
data['plot'] = {
config['plot'] = {
"color": g.color,
"bgcolor": g.bgcolor,
"cmap": _serialize_cmap(g.cmap),
Expand All @@ -565,11 +575,25 @@ def save_config_file(g, file_adr):
"linewidth": g.linewidth,
"depth": g.depth
}
data['matplotlib_version'] = matplotlib_version
config['matplotlib_version'] = matplotlib_version
return config


def save_config_file(g, file_adr):
"""
Save config as file.
:param g: generative image instance
:type g: GenerativeImage
:param file_adr: file address
:type file_adr: str
:return: result as dict
"""
config = get_config(g)
result = {"status": True, "message": DATA_SAVE_SUCCESS_MESSAGE}
try:
with open(file_adr, 'w') as fp:
json.dump(data, fp, indent=4)
json.dump(config, fp, indent=4)
result["message"] = os.path.abspath(file_adr)
except Exception as e:
result["status"] = False
Expand Down
30 changes: 28 additions & 2 deletions samila/genimage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
"""Samila generative image."""
import json
import random
import gc
import itertools
import matplotlib
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
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
Expand Down Expand Up @@ -156,12 +158,21 @@ def plot(
ax.add_artist(ax.patch)
self.fig = fig

def nft_storage(self, api_key, depth=None):
def nft_storage(
self,
api_key,
upload_data=False,
upload_config=False,
depth=None):
"""
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
:return: result as dict
Expand All @@ -172,7 +183,22 @@ def nft_storage(self, api_key, depth=None):
return {"status": False, "message": response["message"]}
buf = response["buffer"]
response = nft_storage_upload(api_key=api_key, data=buf.getvalue())
return response
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)))
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)))
for key, value in response.items():
result[key]['data'] = value
return result

def save_image(self, file_adr, depth=None):
"""
Expand Down
22 changes: 22 additions & 0 deletions test/nft_upload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,26 @@
... time.sleep(10)
>>> status
True
>>> status = False
>>> counter = 0
>>> while(status == False and counter<try_limit):
... result = g.nft_storage(api_key=NFT_STORAGE_API_KEY, upload_config=True)
... counter = counter + 1
... status = result["status"]
... time.sleep(10)
>>> status["image"]
True
>>> status["config"]
True
>>> status = False
>>> counter = 0
>>> while(status == False and counter<try_limit):
... result = g.nft_storage(api_key=NFT_STORAGE_API_KEY, upload_data=True)
... counter = counter + 1
... status = result["status"]
... time.sleep(10)
>>> status["image"]
True
>>> status["data"]
True
"""

0 comments on commit f21f82c

Please sign in to comment.