{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How can I visualise events?\n",
    "Given raw events in the form of a structured numpy array, you might want to plot them in one way or another. Tonic provides two utility methods to do that.\n",
    "\n",
    "## Plotting event grids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tonic\n",
    "\n",
    "nmnist = tonic.datasets.NMNIST(\"../tutorials/data\", train=False)\n",
    "events, label = nmnist[10]\n",
    "\n",
    "tonic.utils.plot_event_grid(events, axis_array=(1, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This also works with events from an audio dataset that has (txp) ordering. The `axis_array` parameter will be ignored in that case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shd = tonic.datasets.SHD(\"../tutorials/data\", train=False)\n",
    "audio_events, label = shd[10]\n",
    "\n",
    "tonic.utils.plot_event_grid(audio_events)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting event animations\n",
    "\n",
    "Alternatively, you might want to use a matplotlib animation that gives you more control. You'll first have to create the frames yourself though. If below example is not rendered correctly, please check out the transform gallery for some example of animated events."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transform = tonic.transforms.ToFrame(\n",
    "    sensor_size=nmnist.sensor_size,\n",
    "    time_window=20000,\n",
    ")\n",
    "\n",
    "frames = transform(events)\n",
    "\n",
    "animation = tonic.utils.plot_animation(frames=frames)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "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.7"
  },
  "vscode": {
   "interpreter": {
    "hash": "65b6f4b806bbaf5b54d6ccaa27abf7e5307b1f0e4411e9da36d5256169cebdd7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
