.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/run_fieldtrip_wf.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_run_fieldtrip_wf.py: .. _ft_wf_seeg_example: ==================================== Time-frequency analysis on sEEG data ==================================== This pipeline shows a very simple example on how to create a workflow connecting a Node that wraps a desired function of a Matlab toolbox (|FieldTrip|) with a Node that wraps a function of a python toolbox (|MNE|) .. |FieldTrip| raw:: html FieldTrip .. |MNE| raw:: html MNE The **input** data should be a **.mat** file containing a FieldTrip data structure. .. GENERATED FROM PYTHON SOURCE LINES 22-39 .. code-block:: default # Authors: Annalisa Pascarella # License: BSD (3-clause) # sphinx_gallery_thumbnail_number = 2 import os.path as op import numpy as np import nipype.pipeline.engine as pe import ephypype from ephypype.nodes import create_iterator, create_datagrabber from ephypype.nodes import ImportFieldTripEpochs, Reference from ephypype.interfaces import TFRmorlet from ephypype.datasets import fetch_ieeg_dataset .. GENERATED FROM PYTHON SOURCE LINES 40-46 Let us fetch the data first. It is around 200 MB download. We use the intracranial EEG (iEEG) data of SubjectUCI29 data available |here| .. |here| raw:: html here .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: default base_path = op.join(op.dirname(ephypype.__file__), '..', 'examples') data_path = fetch_ieeg_dataset(base_path) .. GENERATED FROM PYTHON SOURCE LINES 51-56 then read the parameters for time frequency analysis from a :download:`json ` file and print it .. note:: The code needs the FieldTrip package installed, with path properly setup, for this example to run. .. GENERATED FROM PYTHON SOURCE LINES 56-65 .. code-block:: default import json # noqa import pprint # noqa params = json.load(open("params.json")) pprint.pprint({'time frequency parameters': params["tfr"]}) ft_path = params["tfr"]['fieldtrip_path'] refmethod = params["tfr"]['refmethod'] channels_name = params["tfr"]['channels_name'] .. GENERATED FROM PYTHON SOURCE LINES 66-68 Then, we create our workflow and specify the `base_dir` which tells nipype the directory in which to store the outputs. .. GENERATED FROM PYTHON SOURCE LINES 68-74 .. code-block:: default workflow_name = 'time_frequency_analysis' main_workflow = pe.Workflow(name=workflow_name) main_workflow.base_dir = data_path .. GENERATED FROM PYTHON SOURCE LINES 75-76 Then we create a node to pass input filenames to DataGrabber from nipype .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: default subject_ids = ['SubjectUCI29'] infosource = create_iterator(['subject_id'], [subject_ids]) .. GENERATED FROM PYTHON SOURCE LINES 81-82 and a node to grab data. .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: default template_path = '%s*.mat' template_args = [['subject_id']] datasource = create_datagrabber(data_path, template_path, template_args, infields=['subject_id']) .. GENERATED FROM PYTHON SOURCE LINES 89-91 We then the output (subject_id) of the infosource node to the datasource one. So, these two nodes taken together can grab data. .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: default main_workflow.connect(infosource, 'subject_id', datasource, 'subject_id') .. GENERATED FROM PYTHON SOURCE LINES 94-98 Now, the :class:`ephypype.nodes.Reference` interface is encapsulated in a node and connected to the datasource node. We set the channel names of sEEG data and refmethod equal to 'bipolar' in order to apply a bipolar montage to the depth electrodes. .. GENERATED FROM PYTHON SOURCE LINES 98-108 .. code-block:: default reference_node = pe.Node(interface=Reference(), name='rereference') reference_node.inputs.channels = channels_name reference_node.inputs.ft_path = ft_path reference_node.inputs.refmethod = refmethod reference_node.inputs.script = '' # Then we connect the output of datasource node to the input of reference_node main_workflow.connect(datasource, 'raw_file', reference_node, 'data_file') .. GENERATED FROM PYTHON SOURCE LINES 109-118 The output of the reference_node will be a FieldTrip data structure containing the sEEG data in bipolar montage. Now we create and connect two new nodes (import_epochs, compute_tfr_morlet) that convert the FieldTrip data in MNE format and compute time-frequency representation of the data using |Morlet_wavelets| rispectively. .. |Morlet_wavelets| raw:: html Morlet wavelets .. GENERATED FROM PYTHON SOURCE LINES 118-131 .. code-block:: default import_epochs = pe.Node(interface=ImportFieldTripEpochs(), name='import_epochs') # noqa import_epochs.inputs.data_field_name = 'reref_data' main_workflow.connect(reference_node, 'data_output', import_epochs, 'epo_mat_file') compute_tfr_morlet = pe.Node(interface=TFRmorlet(), name='tfr_morlet') compute_tfr_morlet.inputs.freqs = np.arange(1, 150, 2) main_workflow.connect(import_epochs, 'fif_file', compute_tfr_morlet, 'epo_file') .. GENERATED FROM PYTHON SOURCE LINES 132-134 Finally, we are now ready to execute our workflow. Before, we plot the graph of the workflow (optional) .. GENERATED FROM PYTHON SOURCE LINES 134-137 .. code-block:: default main_workflow.write_graph(graph2use='colored') # colored .. GENERATED FROM PYTHON SOURCE LINES 138-140 and visualize it. Take a moment to pause and notice how the connections here correspond to how we connected the nodes. .. GENERATED FROM PYTHON SOURCE LINES 140-152 .. code-block:: default import matplotlib.pyplot as plt # noqa img = plt.imread(op.join(data_path, workflow_name, 'graph.png')) plt.figure(figsize=(6, 6)) plt.imshow(img) plt.axis('off') main_workflow.write_graph(graph2use='colored') # colored main_workflow.config['execution'] = {'remove_unnecessary_outputs': 'false'} # Run workflow locally on 1 CPU main_workflow.run(plugin='MultiProc', plugin_args={'n_procs': 1}) .. GENERATED FROM PYTHON SOURCE LINES 153-159 To visualize the results we first grab it from workflow directort (base_dir) and use an MNE |visualization_function| .. |visualization_function| raw:: html visualization function .. GENERATED FROM PYTHON SOURCE LINES 159-170 .. code-block:: default import mne # noqa from ephypype.gather import get_results # noqa tfr_files, _ = get_results(main_workflow.base_dir, main_workflow.name, pipeline='tfr_morlet') for tfr_file in tfr_files: power = mne.time_frequency.read_tfrs(tfr_file) power[0].plot([0], baseline=(-.3, -.1), mode='logratio', title=power[0].ch_names[1]) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_run_fieldtrip_wf.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: run_fieldtrip_wf.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: run_fieldtrip_wf.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_