{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Time-frequency analysis on sEEG data\nThis pipeline shows a very simple example on how to create a workflow\nconnecting a Node that wraps a desired function of a Matlab toolbox\n(|FieldTrip|) with a Node that wraps a function of a python toolbox (|MNE|)\n\n.. |FieldTrip| raw:: html\n\n FieldTrip\n\n.. |MNE| raw:: html\n\n MNE\n\nThe **input** data should be a **.mat** file containing a FieldTrip data\nstructure.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Annalisa Pascarella \n# License: BSD (3-clause)\n\n# sphinx_gallery_thumbnail_number = 2\n\nimport os.path as op\nimport numpy as np\nimport nipype.pipeline.engine as pe\n\nimport ephypype\nfrom ephypype.nodes import create_iterator, create_datagrabber\nfrom ephypype.nodes import ImportFieldTripEpochs, Reference\nfrom ephypype.interfaces import TFRmorlet\nfrom ephypype.datasets import fetch_ieeg_dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us fetch the data first. It is around 200 MB download. We use the\nintracranial EEG (iEEG) data of SubjectUCI29 data available |here|\n\n.. |here| raw:: html\n\n here\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "base_path = op.join(op.dirname(ephypype.__file__), '..', 'examples')\ndata_path = fetch_ieeg_dataset(base_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "then read the parameters for time frequency analysis from a\n:download:`json `\nfile and print it\n\n

Note

The code needs the FieldTrip package installed, with path properly setup, for this example to run.

\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import json # noqa\nimport pprint # noqa\nparams = json.load(open(\"params.json\"))\npprint.pprint({'time frequency parameters': params[\"tfr\"]})\nft_path = params[\"tfr\"]['fieldtrip_path']\nrefmethod = params[\"tfr\"]['refmethod']\nchannels_name = params[\"tfr\"]['channels_name']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, we create our workflow and specify the `base_dir` which tells\nnipype the directory in which to store the outputs.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "workflow_name = 'time_frequency_analysis'\n\nmain_workflow = pe.Workflow(name=workflow_name)\nmain_workflow.base_dir = data_path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we create a node to pass input filenames to DataGrabber from nipype\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "subject_ids = ['SubjectUCI29']\ninfosource = create_iterator(['subject_id'], [subject_ids])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and a node to grab data.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "template_path = '%s*.mat'\ntemplate_args = [['subject_id']]\ndatasource = create_datagrabber(data_path, template_path, template_args,\n infields=['subject_id'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We then the output (subject_id) of the infosource node to the datasource one.\nSo, these two nodes taken together can grab data.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "main_workflow.connect(infosource, 'subject_id', datasource, 'subject_id')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, the :class:`ephypype.nodes.Reference` interface is encapsulated in a\nnode and connected to the datasource node.\nWe set the channel names of sEEG data and refmethod equal to 'bipolar' in\norder to apply a bipolar montage to the depth electrodes.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reference_node = pe.Node(interface=Reference(), name='rereference')\nreference_node.inputs.channels = channels_name\nreference_node.inputs.ft_path = ft_path\nreference_node.inputs.refmethod = refmethod\nreference_node.inputs.script = ''\n\n# Then we connect the output of datasource node to the input of reference_node\nmain_workflow.connect(datasource, 'raw_file', reference_node, 'data_file')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output of the reference_node will be a FieldTrip data structure\ncontaining the sEEG data in bipolar montage. Now we create and connect\ntwo new nodes (import_epochs, compute_tfr_morlet) that convert the FieldTrip\ndata in MNE format and compute time-frequency representation of the\ndata using |Morlet_wavelets| rispectively.\n\n.. |Morlet_wavelets| raw:: html\n\n Morlet wavelets\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import_epochs = pe.Node(interface=ImportFieldTripEpochs(), name='import_epochs') # noqa\nimport_epochs.inputs.data_field_name = 'reref_data'\n\nmain_workflow.connect(reference_node, 'data_output',\n import_epochs, 'epo_mat_file')\n\ncompute_tfr_morlet = pe.Node(interface=TFRmorlet(), name='tfr_morlet')\ncompute_tfr_morlet.inputs.freqs = np.arange(1, 150, 2)\n\nmain_workflow.connect(import_epochs, 'fif_file',\n compute_tfr_morlet, 'epo_file')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we are now ready to execute our workflow. Before, we plot the\ngraph of the workflow (optional)\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "main_workflow.write_graph(graph2use='colored') # colored" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and visualize it. Take a moment to pause and notice how the connections\nhere correspond to how we connected the nodes.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt # noqa\nimg = plt.imread(op.join(data_path, workflow_name, 'graph.png'))\nplt.figure(figsize=(6, 6))\nplt.imshow(img)\nplt.axis('off')\nmain_workflow.write_graph(graph2use='colored') # colored\nmain_workflow.config['execution'] = {'remove_unnecessary_outputs': 'false'}\n\n# Run workflow locally on 1 CPU\nmain_workflow.run(plugin='MultiProc', plugin_args={'n_procs': 1})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To visualize the results we first grab it from workflow directort (base_dir)\nand use an MNE |visualization_function|\n\n.. |visualization_function| raw:: html\n\n visualization function \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import mne # noqa\nfrom ephypype.gather import get_results # noqa\n\ntfr_files, _ = get_results(main_workflow.base_dir,\n main_workflow.name, pipeline='tfr_morlet')\n\nfor tfr_file in tfr_files:\n power = mne.time_frequency.read_tfrs(tfr_file)\n power[0].plot([0], baseline=(-.3, -.1), mode='logratio',\n title=power[0].ch_names[1])" ] } ], "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.10.8" } }, "nbformat": 4, "nbformat_minor": 0 }