samila/examples/demo.ipynb

501 wiersze
11 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Samila Demo"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"try:\n",
" import google.colab\n",
" !{sys.executable} -m pip -q -q install samila\n",
"except:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import random\n",
"import math\n",
"from samila import GenerativeImage, Projection, Marker"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Generation\n",
"If we call `generate` with no input parameters, we get a plot from two random equations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1 = GenerativeImage()\n",
"g1.generate()\n",
"g1.plot()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Assigning Functions\n",
"By defining `f1` and `f2`, we can control the general shape of the plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f1(x, y):\n",
" result = random.uniform(-1,1) * x**2 - math.sin(y**2) + abs(y-x)\n",
" return result\n",
"\n",
"def f2(x, y):\n",
" result = random.uniform(-1,1) * y**3 - math.cos(x**2) + 2*x\n",
" return result\n",
"\n",
"g2 = GenerativeImage(f1, f2)\n",
"g2.generate()\n",
"g2.plot()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Projection\n",
"We can use the `projection` attribute to define the coordinate system to transform our functions\n",
"\n",
"The avaliable projections are `RECTILINEAR`, `POLAR`, `AITOFF`, `HAMMER`, `LAMBERT`, `MOLLWEIDE` and `RANDOM`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = GenerativeImage(f1, f2)\n",
"g2.generate()\n",
"\n",
"for p in list(Projection):\n",
" print(p)\n",
" g2.plot(projection=p)\n",
" plt.show()\n",
" plt.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Marker\n",
"We can use the `marker` attribute to change the plotting marker.\n",
"\n",
"The available markers are `POINT`, `PIXEL`, `CIRCLE`, `TRIANGLE_DOWN`, `TRIANGLE_UP`, `TRIANGLE_LEFT`, `TRIANGLE_RIGHT`, `TRI_DOWN`, `TRI_UP`, `TRI_LEFT`, `TRI_RIGHT`, `OCTAGON`, `SQUARE`, `PENTAGON`, `PLUS`, `PLUS_FILLED`, `STAR`, `HEXAGON_VERTICAL`, `HEXAGON_HORIZONTAL`, `X`, `X_FILLED`, `DIAMOND`, `DIAMON_THIN`, `VLINE`, `HLINE` and `RANDOM`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = GenerativeImage(f1, f2)\n",
"g2.generate()\n",
"\n",
"for m in list(Marker):\n",
" print(m)\n",
" g2.plot(marker=m, spot_size=100)\n",
" plt.show()\n",
" plt.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rotation\n",
"You can even rotate your art by using `rotation` parameter. Enter your desired rotation for the image in degrees and you will have it.\n",
"\n",
"* Default rotation is 0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g = GenerativeImage(f1, f2)\n",
"g.generate()\n",
"g.plot(rotation=45)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Range\n",
"\n",
"Control the range over which the input values span"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = GenerativeImage(f1, f2)\n",
"g2.generate(start=-1.5*math.pi, step=0.007, stop=0)\n",
"g2.plot(projection=Projection.POLAR)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Color\n",
"\n",
"We can assign colors for both the background as well as the line\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = GenerativeImage(f1, f2)\n",
"g2.generate()\n",
"g2.plot(color=\"maroon\", bgcolor=\"antiquewhite\", projection=Projection.POLAR)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Supported colors are available in `VALID_COLORS` list\n",
"* `color` and `bgcolor` parameters supported formats:\n",
"\n",
" 1. Color name (example: `color=\"yellow\"`)\n",
" 2. RGB/RGBA (example: `color=(0.1,0.1,0.1)`, `color=(0.1,0.1,0.1,0.1)`)\n",
" 3. Hex (example: `color=\"#eeefff\"`)\n",
" 4. Random (example: `color=\"random\"`)\n",
" 5. Complement (example: `color=\"complement\", bgcolor=\"blue\"`)\n",
" 6. Transparent (example: `bgcolor=\"transparent\"`)\n",
" 7. List (example: `color=[\"black\", \"#fffeef\",...]`)\n",
"\n",
"⚠️ **Transparent** mode is only available for background\n",
"\n",
"⚠️ **List** mode is only available for color\n",
"\n",
"⚠️ In **List** mode, the length of this list must be equal to the lengths of data1 and data2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Point Color\n",
"\n",
"You can make your custom color map and use it in Samila"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"colorarray = [\n",
" [0.7, 0.2, 0.2, 1],\n",
" [0.6, 0.3, 0.2, 1],\n",
" \"black\",\n",
" [0.4, 0.4, 0.3, 1],\n",
" [0.3, 0.4, 0.4, 1],\n",
" \"#ff2561\"]\n",
"g2.generate()\n",
"g2.plot(cmap=colorarray, color=g2.data2, projection=Projection.POLAR)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Spot Size\n",
"\n",
"We can also change the size of the plot marker"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = GenerativeImage(f1, f2)\n",
"g2.generate()\n",
"g2.plot(color=\"yellow\", bgcolor=\"darkslategray\", spot_size=0.25)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random Seeds\n",
"If we do not pass in a seed when calling `generate`, a random seed will be created for us\n",
"\n",
"Passing this seed into `generate` will guarantee plot reproducibility"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"seed = g2.seed\n",
"print(seed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = GenerativeImage(f1, f2)\n",
"g2.generate(seed)\n",
"g2.plot(color=\"yellow\", bgcolor=\"darkslategray\", spot_size=0.05)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Saving Images\n",
"\n",
"Save generated images\n",
"\n",
"Use the `depth` attribute to increase the resolution"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2.save_image(file_adr=\"test.png\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2.save_image(file_adr=\"test_high_res.png\", depth=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Saving Data\n",
"\n",
"Save generated image data\n",
"\n",
"It can then be loaded into a `GenerativeImage` instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2.save_data(file_adr=\"data.json\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = GenerativeImage(data=open('data.json', 'r'))\n",
"g2.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Saving Config\n",
"\n",
"Save generated image config. It contains string formats of functions which is also human readable\n",
"\n",
"It can then be loaded into a `GenerativeImage` instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1.save_config(file_adr=\"config.json\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1 = GenerativeImage(config=open('config.json', 'r'))\n",
"g1.generate()\n",
"g1.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NFT.storage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Upload generated image directly to [NFT.storage](https://nft.storage)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1.nft_storage(api_key=\"YOUR_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1.nft_storage(api_key=\"YOUR_API_KEY\", timeout=5000)"
]
},
{
"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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Default timeout is **3000** seconds"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Samila Demo",
"title_sidebar": "Samila Demo",
"toc_cell": false,
"toc_position": {
"height": "382px",
"left": "10px",
"top": "10px",
"width": "256px"
},
"toc_section_display": false,
"toc_window_display": true
},
"vscode": {
"interpreter": {
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}