# Path: ephypype/nodes/FT_tools.py
# class Reference(MatlabCommand):
#     """
#     Description:
# 
#     Apply the specified reference
# 
#     Inputs
#     ------
# 
#     data_file : str
#         data structure .mat (matlab format)
# 
#     ft_path : str
#         path of FieldTrip package
# 
#     channels : str
#         channels name to include in the reference process
# 
#     updatesens : bool
#         update sensors information
# 
#     refmethod : str
#         kind of reference (avg, bipolar)
# 
#     Outputs
#     -------
# 
#     matlab_output : str
#         file path of new FieldTrip data structure
# 
#     data_output : str
#         fieldtrip data structure in .mat containing the rereferce data
#     """
#     input_spec = ReferenceInputSpec
#     output_spec = ReferenceOutputSpec
# 
#     def _avg_reference(self):
#         """call FieldTrip function to Re-montage the cortical grids to a
#         common average reference """
#         script = """
#         fpath = '%s'
#         addpath(fpath)
#         disp(fpath)
#         ft_defaults
#         load('%s');
#         cfg             = [];
#         cfg.channel     = %s;
#         cfg.reref       = 'yes';
#         cfg.refchannel  = 'all';
#         cfg.refmethod   = '%s';
#         reref_data = ft_preprocessing(cfg, data);
#         save('%s', 'reref_data')
#         """ % (self.inputs.ft_path, self.inputs.data_file,
#                self.inputs.channels, self.inputs.refmethod, self.data_output)
#         return script
# 
#     def _bipolar_reference(self):
#         """call FieldTrip function to Apply a bipolar montage to the depth
#         electrodes"""
#         script = """
#         fpath = '%s'
#         addpath(fpath)
#         disp(fpath)
#         ft_defaults
#         load('%s');
#         depths = %s;
#         for d = 1:numel(depths)
#             cfg             = [];
#             cfg.channel     = ft_channelselection(depths{d}, data.label);
#             cfg.reref       = 'yes';
#             cfg.refchannel  = 'all';
#             cfg.refmethod   = '%s';
#             cfg.updatesens  = 'yes';
#             reref_depths{d} = ft_preprocessing(cfg, data);
#         end
#         cfg            = [];
#         cfg.appendsens = 'yes';
#         reref_data = ft_appenddata(cfg, reref_depths{:});
#         save('%s', 'reref_data')
#         """ % (self.inputs.ft_path, self.inputs.data_file,
#                self.inputs.channels, self.inputs.refmethod, self.data_output)
#         return script
# 
#     def run(self, **inputs):
#         # Inject your script
#         self.data_output = os.path.abspath('reref_data.mat')
#         if self.inputs.refmethod == 'avg':
#             print('*** APPLY {} reference'.format(self.inputs.refmethod))
#             self.inputs.script = self._avg_reference()
#         elif self.inputs.refmethod == 'bipolar':
#             print('*** APPLY {} montage'.format(self.inputs.refmethod))
#             self.inputs.script = self._bipolar_reference()
# 
#         results = super(MatlabCommand, self).run(**inputs)
#         stdout = results.runtime.stdout
#         # Attach stdout to outputs to access matlab results
#         results.outputs.matlab_output = stdout
#         print(stdout)
#         return results
# 
#     def _list_outputs(self):
#         outputs = self._outputs().get()
#         outputs['data_output'] = self.data_output
#         return outputs
# 
# Path: ephypype/datasets.py
# def fetch_ieeg_dataset(base_path):
#     src_url = ('https://www.dropbox.com/s/njqtyk8j8bzdz8s/'
#                'SubjectUCI29_data.mat?dl=1')
# 
#     data_path = os.path.join(base_path, 'ieeg')
#     if not os.path.exists(data_path):
#         os.makedirs(data_path)
#     target = os.path.join(data_path, 'SubjectUCI29_data.mat')
#     if not os.path.exists(target):
#         _fetch_file(src_url, target)
#         print('Downloading files ...')
#     return os.path.abspath(data_path)


import os.path as op
import ephypype
from ephypype.nodes.FT_tools import Reference
from ephypype.datasets import fetch_ieeg_dataset
"""
.. _ft_seeg_example:

=========================================
Apply bipolar montage to depth electrodes
=========================================
This scripts shows a very simple example on how to create an Interface wrapping
a desired function of a Matlab toolbox (|FieldTrip|).

.. |FieldTrip| raw:: html

    <a href="http://www.fieldtriptoolbox.org/" target="_blank">FieldTrip</a>

The **input** data should be a **.mat** file containing a FieldTrip data struct
"""

# Authors: Annalisa Pascarella <a.pascarella@iac.cnr.it>
# License: BSD (3-clause)




###############################################################################
# Let us fetch the data first. It is around 675 MB download.

base_path = op.join(op.dirname(ephypype.__file__), '..', 'examples')

# Path: ephypype/nodes/FT_tools.py
# class Reference(MatlabCommand):
#     """
#     Description:
# 
#     Apply the specified reference
# 
#     Inputs
#     ------
# 
#     data_file : str
#         data structure .mat (matlab format)
# 
#     ft_path : str
#         path of FieldTrip package
# 
#     channels : str
#         channels name to include in the reference process
# 
#     updatesens : bool
#         update sensors information
# 
#     refmethod : str
#         kind of reference (avg, bipolar)
# 
#     Outputs
#     -------
# 
#     matlab_output : str
#         file path of new FieldTrip data structure
# 
#     data_output : str
#         fieldtrip data structure in .mat containing the rereferce data
#     """
#     input_spec = ReferenceInputSpec
#     output_spec = ReferenceOutputSpec
# 
#     def _avg_reference(self):
#         """call FieldTrip function to Re-montage the cortical grids to a
#         common average reference """
#         script = """
#         fpath = '%s'
#         addpath(fpath)
#         disp(fpath)
#         ft_defaults
#         load('%s');
#         cfg             = [];
#         cfg.channel     = %s;
#         cfg.reref       = 'yes';
#         cfg.refchannel  = 'all';
#         cfg.refmethod   = '%s';
#         reref_data = ft_preprocessing(cfg, data);
#         save('%s', 'reref_data')
#         """ % (self.inputs.ft_path, self.inputs.data_file,
#                self.inputs.channels, self.inputs.refmethod, self.data_output)
#         return script
# 
#     def _bipolar_reference(self):
#         """call FieldTrip function to Apply a bipolar montage to the depth
#         electrodes"""
#         script = """
#         fpath = '%s'
#         addpath(fpath)
#         disp(fpath)
#         ft_defaults
#         load('%s');
#         depths = %s;
#         for d = 1:numel(depths)
#             cfg             = [];
#             cfg.channel     = ft_channelselection(depths{d}, data.label);
#             cfg.reref       = 'yes';
#             cfg.refchannel  = 'all';
#             cfg.refmethod   = '%s';
#             cfg.updatesens  = 'yes';
#             reref_depths{d} = ft_preprocessing(cfg, data);
#         end
#         cfg            = [];
#         cfg.appendsens = 'yes';
#         reref_data = ft_appenddata(cfg, reref_depths{:});
#         save('%s', 'reref_data')
#         """ % (self.inputs.ft_path, self.inputs.data_file,
#                self.inputs.channels, self.inputs.refmethod, self.data_output)
#         return script
# 
#     def run(self, **inputs):
#         # Inject your script
#         self.data_output = os.path.abspath('reref_data.mat')
#         if self.inputs.refmethod == 'avg':
#             print('*** APPLY {} reference'.format(self.inputs.refmethod))
#             self.inputs.script = self._avg_reference()
#         elif self.inputs.refmethod == 'bipolar':
#             print('*** APPLY {} montage'.format(self.inputs.refmethod))
#             self.inputs.script = self._bipolar_reference()
# 
#         results = super(MatlabCommand, self).run(**inputs)
#         stdout = results.runtime.stdout
#         # Attach stdout to outputs to access matlab results
#         results.outputs.matlab_output = stdout
#         print(stdout)
#         return results
# 
#     def _list_outputs(self):
#         outputs = self._outputs().get()
#         outputs['data_output'] = self.data_output
#         return outputs
# 
# Path: ephypype/datasets.py
# def fetch_ieeg_dataset(base_path):
#     src_url = ('https://www.dropbox.com/s/njqtyk8j8bzdz8s/'
#                'SubjectUCI29_data.mat?dl=1')
# 
#     data_path = os.path.join(base_path, 'ieeg')
#     if not os.path.exists(data_path):
#         os.makedirs(data_path)
#     target = os.path.join(data_path, 'SubjectUCI29_data.mat')
#     if not os.path.exists(target):
#         _fetch_file(src_url, target)
#         print('Downloading files ...')
#     return os.path.abspath(data_path)


# Path: examples/run_fieldtrip_IF.py
import os.path as op
import ephypype
from ephypype.nodes.FT_tools import Reference
from ephypype.datasets import fetch_ieeg_dataset

"""
.. _ft_seeg_example:

=========================================
Apply bipolar montage to depth electrodes
=========================================
This scripts shows a very simple example on how to create an Interface wrapping
a desired function of a Matlab toolbox (|FieldTrip|).

.. |FieldTrip| raw:: html

    <a href="http://www.fieldtriptoolbox.org/" target="_blank">FieldTrip</a>

The **input** data should be a **.mat** file containing a FieldTrip data struct
"""

# Authors: Annalisa Pascarella <a.pascarella@iac.cnr.it>
# License: BSD (3-clause)




###############################################################################
# Let us fetch the data first. It is around 675 MB download.

base_path = op.join(op.dirname(ephypype.__file__), '..', 'examples')

data_path = fetch_ieeg_dataset(base_path)