{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n.. currentmodule:: seek_localize\n\n\n# 01. Label Anatomy of Electrodes\n\nWhen working with intracranial electrophysiological data in the\nBIDS format, we usually have\n\n- iEEG (ECoG and SEEG)\n- the anatomical MRI scan of a study participant\n- the CT scan of the study participant with iEEG electrodes implanted\n\nIn this tutorial, we show how ``label_elecs_anat`` can be used to\nquickly and easily label anatomy of electrodes.\n\nWe assume that you have already localized the electrodes and coregistered\nthem over to the T1w image FreeSurfer space.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: Adam Li <adam2392@gmail.com>\n#\n# License: BSD (3-clause)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 1: Imports\nWe are importing everything we need for this example:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pathlib import Path\n\nimport pandas as pd\nfrom mne_bids import BIDSPath, print_dir_tree, make_report\n\nfrom seek_localize import label_elecs_anat, fs_lut_fpath"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We will be using the `testing dataset`, which\nis already stored in BIDS format and stored with the\n``seek-localize`` repository.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "bids_root = Path.cwd() / Path(\"../data/\")\nfs_root = bids_root / \"derivatives\" / \"freesurfer\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 2: Explore the dataset contents\n\nWe can use MNE-BIDS to print a tree of all\nincluded files and folders. We pass the ``max_depth`` parameter to\n`mne_bids.print_dir_tree` to the output to three levels of folders, for\nbetter readability in this example.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print_dir_tree(bids_root, max_depth=3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can even ask MNE-BIDS to produce a human-readable summary report\non the dataset contents.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(make_report(bids_root))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 3: Label the anatomy of electrodes\nNow it's time to get ready for labeling some of the data! First, we need to\ncreate a :func:`mne_bids.BIDSPath`, which will point to the corresponding\n``*electrodes.tsv`` file.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "subject = \"la02\"\nsession = \"presurgery\"\nacquisition = \"seeg\"\nspace = \"fs\"\nsuffix = \"electrodes\"\nextension = \".tsv\"\ndatatype = \"ieeg\"\nbids_path = BIDSPath(\n    root=bids_root,\n    datatype=datatype,\n    subject=subject,\n    session=session,\n    acquisition=acquisition,\n    space=space,\n    suffix=suffix,\n    extension=extension,\n)\n\n# the full file path to the electrodes.tsv file\nprint(bids_path.fpath)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The necessary iEEG files are the\n``sub-la02_ses-presurgery_acq-seeg_space-fs_electrodes.tsv``,\n``sub-la02_ses-presurgery_acq-seeg_space-fs_coordsystem.json`` files. Note\nthese are co-occurring files in iEEG-BIDS_ (one present requires the other to\nbe present).\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "coordsystem_fpath = bids_path.copy().update(suffix=\"coordsystem\", extension=\".json\")\nprint(coordsystem_fpath.fpath)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's explore the contents of the current electrodes.tsv file.\nNote that the current data already has the atlas labels, so we\npretend they are not there and only read in the bare minimum columns.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "elec_df = pd.read_csv(\n    bids_path, sep=\"\\t\", index_col=None, usecols=[\"name\", \"x\", \"y\", \"z\"]\n)\nprint(elec_df)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The necessary imaging files are the\n``sub-la02_ses-presurgery_space-fs_T1w.nii`` file, which the electrode\ncoordinates are assumed to be in.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "atlas_img_fpath = fs_root / f\"sub-{subject}\" / \"mri\" / \"aparc+aseg.mgz\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's label the anatomy!\nNote: seek_localize.fs_lut_fpath provides the file path to a local\n``FreeSurferColorLUT.txt`` file.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "elec_df = label_elecs_anat(bids_path, atlas_img_fpath, fs_lut_fpath=fs_lut_fpath)\n\nprint(elec_df)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. LINKS\n\n   https://bids-specification.readthedocs.io/en/stable/04-modality-specific-files/04-intracranial-electroencephalography.html\n\n"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}