{
  "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# 02. Convert Coordinate Frames of Electrodes\n\nWhen working with intracranial electrophysiological data in the\niEEG-BIDS_ format, we usually have iEEG coordinate data either in\n``voxel``, or real world coordinates space (xyz coordinates).\nThen within xyz coordinates, it can either be ``RAS``, or\n``tkRAS`` if one uses FreeSurfer.\n\nIn this tutorial, we show how to quickly use the ``Sensors``\ndata class and quickly go back and forth between coordinate frames\nusing ``convert_elec_coords``.\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": [
        "## 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\nfrom mne_bids import BIDSPath\n\nfrom seek_localize import read_dig_bids, convert_coord_units, convert_coord_space"
      ]
    },
    {
      "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/\")).absolute()\nsubjects_dir = bids_root / \"derivatives\" / \"freesurfer\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now 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\"\nelectrodes_fpath = 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(electrodes_fpath.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 = electrodes_fpath.copy().update(\n    suffix=\"coordsystem\", extension=\".json\"\n)\nprint(coordsystem_fpath.fpath)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's load in the electrode coordinates as an instance of the\n`seek_localize.Sensors` class. Rather then instantiating the class\ndirectly, we use `seek_localize.read_dig_bids` to read in the\ncorrect data. This will perform extra work, such as figuring\nout the full path to the ``IntendedFor`` volumetric image. The\nimage corresponds to the coordinate space to interpret the\nelectrode coordinates in (e.g. a T1w image in FreeSurfer space).\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sensors = read_dig_bids(electrodes_fpath, root=bids_root)\nprint(sensors)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The data already saved was originally written in ``'mm'``, so we can\nconvert to ``voxel`` space denoted by the ``mri`` coordinate frame.\nThis is in-line with how MNE_ does things\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sensors_vox = convert_coord_units(sensors, to_unit=\"voxel\")\nprint(sensors_vox)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We could convert it to ``mm``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sensors_mm = convert_coord_units(sensors_vox, to_unit=\"mm\")\nprint(sensors_mm)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The data was originally saved according to the ``mri`` space, intended\nfor the ``T1.mgz`` image in FreeSurfer. One can also use seek_localize to\ntransform to standard coordinate spaces, such as ``tkras`` and ``mni``.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We could convert it to ``tkras``.\nsensors_tkras = convert_coord_space(sensors_vox, to_frame=\"tkras\")\nprint(sensors_tkras)\n\n# We could convert it to ``mni``.\nsensors_mni = convert_coord_space(\n    sensors_vox, to_frame=\"mni\", subjects_dir=subjects_dir\n)\nprint(sensors_mni)\n\n# We could convert it to back to ``mri``.\nsensors_mri = convert_coord_space(sensors_vox, to_frame=\"mri\")\nprint(sensors_mri)"
      ]
    },
    {
      "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
}