nipy / nipype

Workflows and interfaces for neuroimaging packages
https://nipype.readthedocs.org/en/latest/
Other
745 stars 529 forks source link

polynomial features #1925

Open YSanchezAraujo opened 7 years ago

YSanchezAraujo commented 7 years ago

trying to implement what's described here: https://github.com/nipy/nipype/blob/master/nipype/algorithms/modelgen.py#L221

my changes to the script have been:

added a function and an import:

from sklearn.preprocessing import PolynomialFeatures
def poly(X, high, col_names):
    degree = PolynomialFeatures(high)
    degree.fit(X)
    names = ['x'.join(['{}^{}'.format(pair[0],pair[1]) for
             pair in tuple if pair[1]!=0]) for tuple in
             [zip(col_names, p) for p in degree.powers_]]
    return degree.transform(X), names

then

 polynomial_order = traits.Int(mandatory=False,
                                  desc='Number of polynomial functions to model '
                                        'high pass filter.')

and in the _generate_standard_design:

            if hasattr(info, 'poly_reg') and info.poly_reg is not None:
                polyX, col_names = poly(np.array(info.regressors),
                                        info.poly_reg,
                                        info.regressor_names)

                for j, r in enumerate(list(range(info.poly_reg))):
                    sessinfo[i]['regress'][j]['poly_name'] = col_names[j]
                    sessinfo[i]['regress'][j]['poly_feat'] = polyX[:, j]

but my output doesnt look quite right:

from nipype.algorithms import modelgen
from nipype.interfaces.base import Bunch
s = modelgen.SpecifyModel()

s.inputs.input_units = 'secs'
s.inputs.functional_runs = [
             '/home/yoel/nipype_workshop/data/ds000114/sub-01/func/sub-01_task-fingerfootlips_bold.nii.gz', 
             '/home/yoel/nipype_workshop/data/ds000114/sub-01/func/sub-01_task-linebisection_bold.nii.gz']

s.inputs.time_repetition = 6
s.inputs.high_pass_filter_cutoff = 128.

evs_run2 = Bunch(conditions=['cond1', 'cond2'], 
                 onsets=[[2, 50], [100, 180]], 
                 durations=[[0], [0]], 
                 pmod=[Bunch(name=['amp'], 
                 poly=[2], 
                 param=[[1, 2]]),
                 None],
                 regressors=[[3,4],[5,4]],
                 regressor_names=['t1','t2'],
                 poly_reg=2)

evs_run3 = Bunch(conditions=['cond1', 'cond2'], 
                 onsets=[[20, 120], [80, 160]], 
                 durations=[[0], [0]], 
                 pmod=[Bunch(name=['amp'], 
                 poly=[2], param=[[1, 2]]), 
                 None],
                 regressors=[[1,2],[5,4]],
                 regressor_names=['t3','t4'],
                 poly_reg=2)

s.inputs.subject_info = [evs_run2, evs_run3]
res = s.run()
print(res.outputs)
out[1]: session_info = [{'regress': [{'name': 't1', 'val': [3, 4], 'poly_feat': array([ 1.,  1.]), 'poly_name': ''}, {'name': 't2', 'val': [5, 4], 'poly_feat': array([ 3.,  5.]), 'poly_name': 't1^1'}], 'hpf': 128.0, 'scans': '/home/yoel/nipype_workshop/data/ds000114/sub-01/func/sub-01_task-fingerfootlips_bold.nii.gz', 'cond': [{'pmod': [{'name': 'amp', 'poly': 2, 'param': [1, 2]}], 'name': 'cond1', 'duration': [0.0], 'onset': [2.0, 50.0]}, {'name': 'cond2', 'duration': [0.0], 'onset': [100.0, 180.0]}]}, {'regress': [{'name': 't3', 'val': [1, 2], 'poly_feat': array([ 1.,  1.]), 'poly_name': ''}, {'name': 't4', 'val': [5, 4], 'poly_feat': array([ 1.,  5.]), 'poly_name': 't3^1'}], 'hpf': 128.0, 'scans': '/home/yoel/nipype_workshop/data/ds000114/sub-01/func/sub-01_task-linebisection_bold.nii.gz', 'cond': [{'pmod': [{'name': 'amp', 'poly': 2, 'param': [1, 2]}], 'name': 'cond1', 'duration': [0.0], 'onset': [20.0, 120.0]}, {'name': 'cond2', 'duration': [0.0], 'onset': [80.0, 160.0]}]}]

Any advice / suggestions on how to complete this ?

satra commented 7 years ago

@YSanchezAraujo - the intent of polynomial order is to add legendre polynomials as columns to each run's design. as an example see:

https://github.com/nipy/nipype/blob/master/examples/rsfmri_vol_surface_preprocessing_nipy.py#L223