{
 "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"
   ]
  },
  {
   "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": [
    "## 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",
    "\n",
    "Supported colors are available in `VALID_COLORS` list\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": [
    "* `color` and `bgcolor` parameters supported formats:\n",
    "\n",
    "    1. Color name (example: `yellow`)\n",
    "    2. RGB/RGBA (example: `(0.1,0.1,0.1)`, `(0.1,0.1,0.1,0.1)`)\n",
    "    3. Hex (example: `#eeefff`)\n",
    "    4. Random (example: `random`)"
   ]
  },
  {
   "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\")"
   ]
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
