{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hands-on session 1: Basics\n", "\n", "These exercises cover the basics introduced in Tutorial 1\n", "\n", "## Exercise 1: File handling\n", "\n", "1. Create a new nix-file with the name `file_1.nix` using the ``nixio.FileMode.ReadWrite`` or ``nixio.FileMode.Overwrite`` flag.\n", "2. Print out the `ìd`` of the file entity and when it was created.\n", "3. Close the file and reopen it with the ``nixio.FileMode.ReadWrite`` and print it's ``id`` and when it was created.\n", "4. Close and reopen with the ``nixio.FileMode.Overwrite`` mode. Compare the id and created_at properties.\n", "5. Create a new nix-file with the name `file_2.nix` using the ``nixio.FileMode.ReadOnly`` mode. What happens.\n", "\n", "### Your solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2: Storing regularly sampled data\n", "\n", "For this exercise we will load data from the resources folder. Use the ``load_intra_data`` (below) to load the data.\n", "\n", "1. load the data\n", "2. Try to figure out a few things about the data:\n", " 1. What is stored?\n", " 2. What is the sampling rate (or sampling interval)?\n", "3. You may want to plot it to get an impression of the data.\n", " 1. What might be the correct labeling of the plot?\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import scipy.io as spio\n", "\n", "def load_intra_data():\n", " \"\"\"\n", " Returns the time and the voltage vectors stored in the \"intra_data.mat\" file.\n", "\n", " Returns:\n", " time, voltage: the vectors containing time and measured voltage.\n", " \"\"\"\n", " data = spio.loadmat(os.path.join(\"resources\", \"intra_data.mat\"))\n", "\n", " return data[\"time\"][0], data[\"voltage\"][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Now we want to store the data in a nix file\n", "\n", "1. Open a new file (\"regularly_sampled_data.nix\")\n", "2. Within the file create a **Block** with ``block = nixfile.create_block(\"test block\", \"test\")``.\n", "3. Within the ``block`` we create a **DataArray** (\"intracellular data\")\n", "4. Append the dimension descriptor for the time axis.\n", "5. Close the file to ensure proper saving (best practice).\n", "\n", "### Your solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3: reading data from the file and creating a fully labeled plot\n", "\n", "1. Open the data file in ``ReadOnly`` mode (best practice).\n", "2. Find the **Block**.\n", "3. Find the **DataArray** that contains the data.\n", "4. Read the data into a variable.\n", "4. Get the dimension descriptor of the time-axis.\n", "5. Get a time axis of the appropriate length \n", "6. Plot the data\n", "7. label the x- and y-axis (you may want to use the function ``get_label`` below)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def get_label(obj):\n", " \"\"\" Returns a string that can be used as an axis label. Constructs the label from DataArray or Dimension objects.\n", "\n", " Args:\n", " obj (nixio.DataArray or nixio.Dimension): the object of which an axis label should be created.\n", "\n", " Returns:\n", " string: The label incling unit, if given. Empty string for invalid objects\n", " \"\"\"\n", " label = \"\"\n", " if isinstance(obj, (nixio.DataArray, nixio.SampledDimension, nixio.RangeDimension)):\n", " label = \"%s %s\" % (obj.label, (\"\" if obj.unit is None else \"[%s]\" % obj.unit))\n", " elif isinstance(obj, (nixio.SetDimension)) and hasattr(obj, \"label\"):\n", " label = obj.label\n", "\n", " return label\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Your solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4: Irregularly sampled data.\n", "\n", "1. Reopen the file from above in ``ReadWrite`` mode since we will extend it.\n", "2. Read the membrane voltage data and identify the times of the action potentials in it.\n", "3. Store the spike times in a second data array.\n", "4. Close the file.\n", "\n", "5. If you have time, reopen in ``ReadOnly`` mode and create a fully labeled plot that contains the membrane voltage and the respective spike times.\n", "\n", "### Your solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 5: Storing multidimensional data\n", "\n", "1. Load the data and find out the dimensionality of the data. (One dimension represents time, the other the local field potentials measured in parallel recording channels).\n", "2. Store it in a nix file. Don't forget to add the dimension descriptors.\n", "\n", "3. Create a labeled plot of the data." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "def load_lfp_fake_data():\n", " \"\"\"\n", " Returns the time and the voltage vectors stored in the \"lfp_fake_data.mat\" file.\n", "\n", " Returns:\n", " time, voltage: the vectors containing time and measured voltage.\n", " \"\"\"\n", " data = spio.loadmat(os.path.join(\"resources\", \"lfp_fake_data.mat\"))\n", "\n", " return data[\"time\"][0], data[\"lfp\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Your solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bonus: Did you bring your own data? Try to store it in NIX..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" }, "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }