{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "from matplotlib import lines\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "\n",
    "plt.style.use('ggplot')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Databases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def adjust_lightness(color, amount=0.5):\n",
    "    import matplotlib.colors as mc\n",
    "    import colorsys\n",
    "    try:\n",
    "        c = mc.cnames[color]\n",
    "    except:\n",
    "        c = color\n",
    "    c = colorsys.rgb_to_hls(*mc.to_rgb(c))\n",
    "    return colorsys.hls_to_rgb(c[0], max(0, min(1, amount * c[1])), c[2])\n",
    "\n",
    "\n",
    "def build_paradigm(data):\n",
    "#     ax = plt.subplot(111)\n",
    "#     ax = fig.add_subplot(axes_class=AxesZero)\n",
    "\n",
    "    ax = plt.gca()\n",
    "    \n",
    "#     ax = plt.axes(frameon=False)\n",
    "    ax.set_frame_on(False)\n",
    "    ax.get_xaxis().tick_bottom()\n",
    "    ax.axes.get_yaxis().set_visible(False)\n",
    "\n",
    "    Y = 1\n",
    "    ticks = []\n",
    "    ticks_label = []\n",
    "    extra = []\n",
    "    alpha = 1\n",
    "\n",
    "\n",
    "    max_y = 0\n",
    "    min_y = 0\n",
    "    for i, frame  in  enumerate(data):\n",
    "        \n",
    "        p = frame['time']\n",
    "        alpha = frame.get('alpha', 1)\n",
    "        label = frame['label']\n",
    "        label_extra = frame.get('label_extra', None)\n",
    "        color = frame.get('color', f'C{i}')\n",
    "        \n",
    "        rotation = frame.get('rotation', 0)\n",
    "        fontsize = frame.get('fontsize', 14)\n",
    "        \n",
    "        level = frame.get('level', (0, 1))\n",
    "        level = [0.1+l for l in level]\n",
    "        max_y = max(max_y, *level)  \n",
    "        min_y = min(min_y, *level)  \n",
    "        \n",
    "        \n",
    "        restart = frame.get('restart', False)\n",
    "        \n",
    "        \n",
    "        if restart:\n",
    "            ticks_label.extend([i-p[0] for i in p]) \n",
    "            p = [i+ticks[-1] for i in p]\n",
    "        else:\n",
    "            ticks_label.extend(p)\n",
    "        \n",
    "        ticks.extend(p)\n",
    "            \n",
    "\n",
    "        plt.fill_between([p[0], p[-1]], *level, facecolor=color, alpha=alpha, linewidth=0, edgecolor=color)\n",
    "        \n",
    "        if hatch := frame.get('hatch', ''):\n",
    "            plt.fill_between([p[0], p[-1]], *level, edgecolor=adjust_lightness(color, 0.85), facecolor='none', alpha=1, hatch=hatch, linewidth=0)\n",
    "                \n",
    "        \n",
    "        if len(p) > 2:\n",
    "            plt.fill_between(p[1:], *level, edgecolor=adjust_lightness(color, 0.85), facecolor='none', alpha=1, hatch='///', linewidth=0)\n",
    "            if label_extra is None:\n",
    "#                 label_extra = f'{1000*(p[2]-p[1]):.0f} ms'\n",
    "                label_extra = f\"\\n{abs(p[1] - p[0]):.1f}s ~ {abs(p[-1]- p[0]):.1f}s\"\n",
    "#             if label_extra != '':    \n",
    "#                 plt.annotate(label_extra, (np.mean(p[1:]), np.mean(level)), ha='center', va='center', color='w', fontsize=fontsize, rotation=rotation)\n",
    "            elif label_extra:\n",
    "                label_extra = f\"\\n{label_extra}\"\n",
    "        else:\n",
    "            label_extra = ''\n",
    "            \n",
    "#         if frame.get('ha', 'center') == 'center':\n",
    "#             x_ = np.mean(p)\n",
    "#         else:\n",
    "#             x_ = np.min(p)+0.1\n",
    "\n",
    "        if marker := frame.get('marker', False):\n",
    "            \n",
    "            lev = [1+(0.2*marker['level']), 1.2+(0.2*marker['level'])]\n",
    "#             q = p[:]\n",
    "            \n",
    "            max_y = max(max_y, *lev)  \n",
    "            lev = [0.1+l for l in lev]\n",
    "        \n",
    "        \n",
    "            q = p[0]\n",
    "            extra.append(q+marker['width'])\n",
    "            plt.fill_between([q, q+marker['width']], *lev, facecolor=color, alpha=alpha, linewidth=0, edgecolor=color)\n",
    "            plt.fill_between([q, q+marker['width']], *lev, edgecolor=adjust_lightness(color, 0.85), facecolor='none', alpha=1, hatch='...', linewidth=0)\n",
    "            plt.annotate(marker['label'], (np.mean([q, q+marker['width']]), np.mean(lev)), ha='center', va='center', color='w', fontsize=12, zorder=999)\n",
    "            plt.vlines(q, marker.get('min', 0.1), lev[1], linestyle='--', linewidth=1, color='k', zorder=99)        \n",
    "        \n",
    "            \n",
    "            if marker.get('label_end', False):\n",
    "                q = p[-1]\n",
    "                extra.append(q+marker['width'])\n",
    "                plt.fill_between([q, q+marker['width']], *lev, facecolor=color, alpha=alpha, linewidth=0, edgecolor=color)\n",
    "                plt.fill_between([q, q+marker['width']], *lev, edgecolor=adjust_lightness(color, 0.85), facecolor='none', alpha=1, hatch='...', linewidth=0)\n",
    "                plt.annotate(marker['label_end'], (np.mean([q, q+marker['width']]), np.mean(lev)), ha='center', va='center', color='w', fontsize=12, zorder=999)\n",
    "                plt.vlines(q, 0.1, lev[1], linestyle='--', linewidth=1, color='k', zorder=99)\n",
    "            \n",
    "            \n",
    "\n",
    "    \n",
    "        a, b = 0, 0\n",
    "#         if rotation == 90:\n",
    "#             a = -0.05\n",
    "#             if label_extra:\n",
    "#                 a -= 1\n",
    "#         else:\n",
    "#             b = 0.05\n",
    "#             if label_extra:\n",
    "#                 b -= 0.05\n",
    "    \n",
    "        plt.annotate(f\"{label}{label_extra}\", (np.mean([p[0], p[-1]])-a, np.mean(level)-b), ha=frame.get('ha', 'center'), va='center', color='w', fontsize=fontsize, rotation=rotation)\n",
    "\n",
    "        \n",
    "    \n",
    "    xmin, xmax = min(ticks), max(ticks+extra)\n",
    "    \n",
    "    plt.xticks([])\n",
    "    ax.set_xticklabels([])\n",
    "#     plt.xticks(list(ticks))\n",
    "#     ax.set_xticklabels([f\"{i:.1f}s\" for i in list(ticks_label)])\n",
    "    ax.add_artist(lines.Line2D((xmin, xmax), (0, 0), color='black', linewidth=2))\n",
    "    \n",
    "    plt.xlabel('Time')\n",
    "    \n",
    "    plt.xlim(xmin, xmax)\n",
    "    \n",
    "    plt.ylim(min_y, max_y+0.1)\n",
    "\n",
    "    \n",
    "    ax.arrow(0, 0, xmax, 0, fc='k', ec='none',\n",
    "             head_width=max_y * 0.07, head_length=max_y * 0.07,\n",
    "             length_includes_head= False,\n",
    "             clip_on = False) \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29272 characters]\n",
      "text/plain": [
       "<Figure size 900x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# from matplotlib import pyplot as plt\n",
    "# from bci_framework.utils import build_paradigm\n",
    "fig = plt.figure(figsize=(10, 4), dpi=90)\n",
    "\n",
    "paradigm = (\n",
    "    {\n",
    "        'time': (-3, 0),\n",
    "        'level': (0.5, 1),        \n",
    "        'label': 'Fixation cross',\n",
    "        'color': 'C0',\n",
    "    },\n",
    "    \n",
    "    {\n",
    "        'time': (0, -2, -3),\n",
    "        'level': (0, 0.5),    \n",
    "        'label': 'Stimulus onset\\nasynchronously',\n",
    "        'color': 'C3',\n",
    "#         'rotation': 90,\n",
    "    },       \n",
    "  \n",
    "    \n",
    "    {\n",
    "        'time': (0, 4, 6),\n",
    "#         'level': (1, 1.5),\n",
    "        'label': 'Cue',\n",
    "        'color': 'C2',\n",
    "        'rotation': 90,\n",
    "        \n",
    "        'marker': {\n",
    "            'label': 'Marker streamed',\n",
    "            'width': 2,\n",
    "            'level': 1,\n",
    "        }\n",
    "        \n",
    "    },\n",
    "    \n",
    ")\n",
    "\n",
    "\n",
    "build_paradigm(paradigm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24628 characters]\n",
      "text/plain": [
       "<Figure size 1800x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "# from bci_framework.utils import build_paradigm\n",
    "fig = plt.figure(figsize=(20, 4), dpi=90)\n",
    "\n",
    "paradigm = [\n",
    "    {\n",
    "        'time': (-1.5, 0),  \n",
    "        'level': (0.5, 1), \n",
    "        'label': 'Target notice',\n",
    "        'color': 'C0',\n",
    "    },\n",
    "    \n",
    "    \n",
    "    {\n",
    "        'time': (0, -1.5, -2.5),\n",
    "        'level': (0, 0.5), \n",
    "        'label': 'Break',\n",
    "        'color': 'C3',\n",
    "#         'rotation': 90,\n",
    "    },   \n",
    "    \n",
    "    {\n",
    "        'time': (0, 0.4*12, 12),\n",
    "        'label': 'Sequential flash',\n",
    "        'color': 'C2',\n",
    "    },\n",
    " \n",
    "\n",
    "]\n",
    "\n",
    "build_paradigm(paradigm)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 75820 characters]\n",
      "text/plain": [
       "<Figure size 1350x540 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(15, 6), dpi=90)\n",
    "\n",
    "N = 5\n",
    "\n",
    "paradigm_flash = []\n",
    "for i in range(N):\n",
    "    \n",
    "#     T = (i-1)*1 + 1.5\n",
    "    T = (i-1)*0.8 + 1.1\n",
    "    \n",
    "    stimulus = [\n",
    "        {\n",
    "        'time': (-0.3+T, -0.3+T, 0+T),\n",
    "        'label': 'Inter stimulus',\n",
    "        'color': 'C3',\n",
    "        'rotation': 90,\n",
    "#         'alpha': 0.2,\n",
    "        },\n",
    "        {\n",
    "        'time': (0+T, 0.5+T),\n",
    "        'label': 'Non-target',\n",
    "        'color': 'C4',\n",
    "        'rotation': 90,\n",
    "#         'alpha': 0.2,        \n",
    "            \n",
    "        'marker': {\n",
    "            'label': 'Marker streamed',\n",
    "            'width': 0.75,\n",
    "            'level': 1,\n",
    "        }\n",
    "        },\n",
    "             \n",
    "        \n",
    "    \n",
    "    ]\n",
    "    \n",
    "    \n",
    "    if i == N//2:\n",
    "        \n",
    "        stimulus[0]['time'] = (-0.3+T, -0.3+T, 0+T)\n",
    "#         stimulus[0]['label'] = 'Inter stimulus'\n",
    "        stimulus[0]['color'] = 'C3'\n",
    "#         stimulus[0]['alpha'] = 1\n",
    "        \n",
    "        stimulus[1]['time'] = (0+T, 0.2+T, 0.5+T)\n",
    "        stimulus[1]['label'] = 'Target'\n",
    "        stimulus[1]['color'] = 'C2'\n",
    "#         stimulus[1]['alpha'] = 1\n",
    "        \n",
    " \n",
    "\n",
    "    paradigm_flash.extend(stimulus)\n",
    "\n",
    "    \n",
    "build_paradigm(paradigm_flash)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 88948 characters]\n",
      "text/plain": [
       "<Figure size 1800x540 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "# from bci_framework.utils import build_paradigm\n",
    "fig = plt.figure(figsize=(20, 6), dpi=90)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "paradigm = [\n",
    "    {\n",
    "        'time': (-1.5, 0),  \n",
    "        'level': (0.5, 1), \n",
    "        'label': 'Target notice',\n",
    "        'color': 'C0',\n",
    "    },\n",
    "    \n",
    "    \n",
    "    {\n",
    "        'time': (0, -1.5, -2.5),\n",
    "        'level': (0, 0.5), \n",
    "        'label': 'Break',\n",
    "        'color': 'C3',\n",
    "#         'rotation': 90,\n",
    "    },   \n",
    "\n",
    "#     {\n",
    "#         'time': (0, 0.4*12, 12),\n",
    "#         'label': 'Secuencial flash',\n",
    "#         'color': 'C2',\n",
    "#     },    \n",
    "    \n",
    "\n",
    "    {\n",
    "        'time': (0, 4),\n",
    "        'level': (0, 0.3), \n",
    "        'label': 'Sequential flash over targets set',\n",
    "        'color': 'C2',\n",
    "    },    \n",
    "    \n",
    "    \n",
    "]\n",
    "\n",
    "\n",
    "N = 5\n",
    "\n",
    "paradigm_flash = []\n",
    "for i in range(N):\n",
    "    \n",
    "    T = (i-1)*0.8 + 1.1\n",
    "    \n",
    "    stimulus = [\n",
    "        {\n",
    "        'time': (-0.3+T, -0.3+T, 0+T),\n",
    "        'level': (0.3, 1),   \n",
    "        'label': 'Inter stimulus',\n",
    "        'color': 'C3',\n",
    "        'rotation': 90,\n",
    "#         'alpha': 0.2,\n",
    "        },\n",
    "        {\n",
    "        'time': (0+T, 0.5+T),\n",
    "        'level': (0.3, 1),   \n",
    "        'label': 'Non-target',\n",
    "        'color': 'C4',\n",
    "        'rotation': 90,\n",
    "#         'alpha': 0.2,        \n",
    "            \n",
    "        'marker': {\n",
    "            'label': 'Marker streamed',\n",
    "            'width': 0.75,\n",
    "            'level': 1,\n",
    "            'min': 0.4,\n",
    "        }\n",
    "        },\n",
    "             \n",
    "        \n",
    "    \n",
    "    ]\n",
    "    \n",
    "    \n",
    "    if i == N//2:\n",
    "        \n",
    "        stimulus[0]['time'] = (-0.3+T, -0.3+T, 0+T)\n",
    "#         stimulus[0]['label'] = 'Inter stimulus'\n",
    "        stimulus[0]['color'] = 'C3'\n",
    "#         stimulus[0]['alpha'] = 1\n",
    "        \n",
    "        stimulus[1]['time'] = (0+T, 0.1+T, 0.5+T)\n",
    "        stimulus[1]['label'] = 'Target'\n",
    "        stimulus[1]['color'] = 'C2'\n",
    "#         stimulus[1]['alpha'] = 1\n",
    "\n",
    "\n",
    "    if i == 0:\n",
    "        stimulus[0]['marker']={\n",
    "            'label': 'Marker streamed\\n(Sequential start)',\n",
    "            'width': 1,\n",
    "            'level': 2,\n",
    "        }\n",
    "        \n",
    " \n",
    "\n",
    "    paradigm_flash.extend(stimulus)\n",
    "\n",
    "\n",
    "    \n",
    "paradigm_flash.extend([\n",
    "    \n",
    "    \n",
    "\n",
    "#         {\n",
    "#         'time': (0, 0.2),\n",
    "#         'level': (1, 2),\n",
    "#         'label': 'Marker streamed',\n",
    "#         'color': 'C2',\n",
    "#         'rotation': 90,\n",
    "#         'fontsize': 12,\n",
    "#         'hatch': '...',\n",
    "#     },  \n",
    "    \n",
    "])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "build_paradigm(paradigm + paradigm_flash)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 97052 characters]\n",
      "text/plain": [
       "<Figure size 1800x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(20, 8), dpi=90)\n",
    "\n",
    "paradigm = (\n",
    "    \n",
    "    {\n",
    "        'time': (-0.4, -1.5, -2),\n",
    "        'label': 'Inter trial break',\n",
    "        'color': 'C3',\n",
    "#         'rotation': 90,\n",
    "    },\n",
    "    \n",
    "    {\n",
    "        'time': (-0.4, -0.2, 0),\n",
    "#         'level': (1, 1.5),        \n",
    "        'label': 'Cue',\n",
    "        'color': 'C2',\n",
    "        'rotation': 90,\n",
    "        \n",
    "        'marker': {\n",
    "            'label': 'Marker streamed\\n(cue)',\n",
    "            'width': 1.3,\n",
    "            'level': 3,\n",
    "        }\n",
    "        \n",
    "    },\n",
    "    \n",
    "    \n",
    "    \n",
    "    {\n",
    "        'time': (0, 0.1, 0.5),\n",
    "        'label': 'Memory array',\n",
    "        'color': 'C0',\n",
    "        'rotation': 90,        \n",
    "        'marker': {\n",
    "            'label': 'Marker streamed',\n",
    "            'width': 1.3,\n",
    "            'level': 2,\n",
    "        }\n",
    "    },\n",
    "    \n",
    "\n",
    "    \n",
    "    {\n",
    "        'time': (0, 0.5, 1.5),\n",
    "        'label': 'Retention\\ninterval',\n",
    "        'color': 'C5',\n",
    "        'restart': True,       \n",
    "        'marker': {\n",
    "            'label': 'Marker streamed',\n",
    "            'width': 1.3,\n",
    "            'level': 1,\n",
    "        }\n",
    "    },\n",
    "    {\n",
    "        'time': (0, 1.5, 2.5),\n",
    "        'label': 'Test Array',\n",
    "        'color': 'C1',\n",
    "        'restart': True,       \n",
    "        'marker': {\n",
    "            'label': 'Marker streamed',\n",
    "            'label_end': 'Marker streamed \\n(response)',\n",
    "            'width': 1.3,\n",
    "            'level': 1,\n",
    "        }\n",
    "    },\n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "\n",
    ")\n",
    "\n",
    "\n",
    "build_paradigm(paradigm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
