{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0edbdd47",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pynsn as nsn\n",
    "from pynsn import distributions as distr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b378b7a",
   "metadata": {},
   "source": [
    "To generate a random nsn stimulus, you need to define\n",
    "\n",
    "....\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e064fd3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "factory = nsn.NSNFactory(target_area_radius=200, min_dist_between_objects=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "eac0326b",
   "metadata": {},
   "outputs": [],
   "source": [
    "factory.set_appearance_dot(diameter=distr.Normal(mu=13, sigma=4, min_max=(10,25)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4e909fe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate random nsn stimulus with 15 dots\n",
    "stimulus = factory.create_random_array(n_objects=15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d681101",
   "metadata": {},
   "source": [
    "## Make pictures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3ee3b63c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30224 characters]",
      "text/plain": [
       "<PIL.Image.Image image mode=RGBA size=400x400>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pynsn.image import pil_image\n",
    "\n",
    "# To make picture (pillow), first define some colour features\n",
    "my_colours = nsn.ImageColours(target_area=\"#EEEEEE\", background=\"gray\",\n",
    "                         default_object_colour=\"darkmagenta\") # show named colours see Colour.NAMED_COLOURS\n",
    "pil_image.create(stimulus, colours=my_colours)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "17da0115",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 58644 characters]",
      "text/plain": [
       "<PIL.Image.Image image mode=RGBA size=400x400>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# example: to plot field area and center of mass, just define the repective colours and plot\n",
    "my_colours2 = nsn.ImageColours(target_area=None,\n",
    "                          field_area_positions=\"red\",\n",
    "                          field_area=\"green\",\n",
    "                          center_of_mass=\"darkgray\",\n",
    "                          center_of_field_area=\"lightgray\",\n",
    "                          background=None,\n",
    "                          default_object_colour=\"darkmagenta\")\n",
    "pil_image.create(stimulus, colours=my_colours2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "64d2c86d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## to save the stimulus as vector graphic, generate SVG stimuli\n",
    "# svg_stimulus = svg.create(stimulus, coddlours=my_colours, filename=\"demo.svg\")\n",
    "# svg_stimulus.save()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd8373bd",
   "metadata": {},
   "source": [
    "# Visual Features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7f8cc4c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- DotArray\n",
      "  Hash: 0c1f6e90d9c5863a831ca02fed85e28c\n",
      "  Numerosity............        15.00\n",
      "  Average dot diameter..        16.22\n",
      "  Average perimeter.....        50.97\n",
      "  Average surface area..       215.62\n",
      "  Total perimeter.......       764.48\n",
      "  Total surface area....      3234.31\n",
      "  Field area............     68203.79\n",
      "  Sparsity..............      4546.92\n",
      "  Coverage..............         0.05\n",
      "  Log Size..............        19.41\n",
      "  Log Spacing...........        28.21\n"
     ]
    }
   ],
   "source": [
    "# show properties for the current stimulus\n",
    "print(stimulus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0094e328",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- DotArray\n",
      "  Hash: 9d08ba22d74c0403e4e09475ff2084a1\n",
      "  Numerosity............        15.00\n",
      "  Average dot diameter..        43.26\n",
      "  Average perimeter.....       135.91\n",
      "  Average surface area..      1533.33\n",
      "  Total perimeter.......      2038.63\n",
      "  Total surface area....     23000.00\n",
      "  Field area............     78860.99\n",
      "  Sparsity..............      5257.40\n",
      "  Coverage..............         0.29\n",
      "  Log Size..............        25.07\n",
      "  Log Spacing...........        28.63\n"
     ]
    }
   ],
   "source": [
    "# change surface array\n",
    "stimulus.properties.fit_total_surface_area(23000)\n",
    "print(stimulus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2f0937a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 79360 characters]",
      "text/plain": [
       "<PIL.Image.Image image mode=RGBA size=400x400>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "figure = pil_image.create(stimulus, colours=my_colours2)\n",
    "pil_image.create(stimulus, colours=my_colours2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e4a186ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# realign dot arry to avoid overlap\n",
    "#stimulus.realign()\n",
    "#pil_image.create(stimulus, colours=my_colours)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1028ad98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- DotArray\n",
      "  Hash: 0dd0792a3be0ff291aa64cb9c4b5c190\n",
      "  Numerosity............        15.00\n",
      "  Average dot diameter..        12.70\n",
      "  Average perimeter.....        39.91\n",
      "  Average surface area..       132.20\n",
      "  Total perimeter.......       598.59\n",
      "  Total surface area....      1982.97\n",
      "  Field area............     66873.46\n",
      "  Sparsity..............      4458.23\n",
      "  Coverage..............         0.03\n",
      "  Log Size..............        18.00\n",
      "  Log Spacing...........        28.15\n"
     ]
    }
   ],
   "source": [
    "# change log_size \n",
    "stimulus.properties.fit_log_size(18)\n",
    "print(stimulus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "02ab3aa7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27496 characters]",
      "text/plain": [
       "<PIL.Image.Image image mode=RGBA size=400x400>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pil_image.create(stimulus, colours=my_colours)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "eda03c30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- DotArray\n",
      "  Hash: 30d70a3464a7bb6b7360d84d81fbe02c\n",
      "  Numerosity............        15.00\n",
      "  Average dot diameter..        12.70\n",
      "  Average perimeter.....        39.91\n",
      "  Average surface area..       132.20\n",
      "  Total perimeter.......       598.59\n",
      "  Total surface area....      1982.97\n",
      "  Field area............     15863.74\n",
      "  Sparsity..............      1057.58\n",
      "  Coverage..............         0.13\n",
      "  Log Size..............        18.00\n",
      "  Log Spacing...........        24.00\n"
     ]
    }
   ],
   "source": [
    "# change spacing parameter (iterative procedure)\n",
    "stimulus.properties.fit_log_spacing(24,  precision=0.01)\n",
    "print(stimulus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "2c96ff7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 38104 characters]",
      "text/plain": [
       "<PIL.Image.Image image mode=RGBA size=400x400>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# as you see the surface area and sparsity (1/density) is smaller\n",
    "pil_image.create(stimulus, colours=nsn.ImageColours(field_area=\"red\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c31b6de",
   "metadata": {},
   "source": [
    "# 2 matched stimuli"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c5aaea70",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_colours = nsn.ImageColours(target_area=\"#EEEEEE\", \n",
    "                          default_object_colour=\"lime\") \n",
    "\n",
    "factory.set_appearance_dot(diameter=distr.Normal(mu=13, sigma=4, min_max=(10,25)))\n",
    "stim1 = factory.create_random_array(30)\n",
    "stim2 = factory.create_random_array(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "861a840e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
