Skip to content

Commit be215f5

Browse files
author
Shoshana Berleant
committed
refactor rsfmri fsl workflow
1 parent 083c16f commit be215f5

File tree

2 files changed

+9
-41
lines changed

2 files changed

+9
-41
lines changed

nipype/algorithms/compcor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ def _run_interface(self, runtime):
5656
# "The constant and linear trends of the columns in the matrix M were
5757
# removed [prior to ...]"
5858
if self.inputs.use_regress_poly:
59-
regressed = regress_poly(self.inputs.regress_poly_degree,
59+
voxel_timecourses = regress_poly(self.inputs.regress_poly_degree,
6060
voxel_timecourses)
61-
regressed = regressed - np.mean(regressed, axis=1)[:,None]
61+
voxel_timecourses = voxel_timecourses - np.mean(voxel_timecourses,
62+
axis=1)[:, np.newaxis]
6263

6364
# "Voxel time series from the noise ROI (either anatomical or tSTD) were
6465
# placed in a matrix M of size Nxm, with time along the row dimension
6566
# and voxels along the column dimension."
66-
M = regressed.T
67+
M = voxel_timecourses.T
6768
numvols = M.shape[0]
6869
numvoxels = M.shape[1]
6970

nipype/workflows/rsfmri/fsl/resting.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,7 @@
88
from ....interfaces import utility as util # utility
99
from ....pipeline import engine as pe # pypeline engine
1010
from ....algorithms.misc import TSNR
11-
12-
13-
def extract_noise_components(realigned_file, noise_mask_file, num_components):
14-
"""Derive components most reflective of physiological noise
15-
"""
16-
import os
17-
from nibabel import load
18-
import numpy as np
19-
import scipy as sp
20-
imgseries = load(realigned_file)
21-
components = None
22-
mask = load(noise_mask_file).get_data()
23-
voxel_timecourses = imgseries.get_data()[mask > 0]
24-
voxel_timecourses[np.isnan(np.sum(voxel_timecourses, axis=1)), :] = 0
25-
# remove mean and normalize by variance
26-
# voxel_timecourses.shape == [nvoxels, time]
27-
X = voxel_timecourses.T
28-
stdX = np.std(X, axis=0)
29-
stdX[stdX == 0] = 1.
30-
stdX[np.isnan(stdX)] = 1.
31-
stdX[np.isinf(stdX)] = 1.
32-
X = (X - np.mean(X, axis=0)) / stdX
33-
u, _, _ = sp.linalg.svd(X, full_matrices=False)
34-
if components is None:
35-
components = u[:, :num_components]
36-
else:
37-
components = np.hstack((components, u[:, :num_components]))
38-
components_file = os.path.join(os.getcwd(), 'noise_components.txt')
39-
np.savetxt(components_file, components, fmt=b"%.10f")
40-
return components_file
41-
11+
from ....algorithms import compcor as cc
4212

4313
def select_volume(filename, which):
4414
"""Return the middle index of a file
@@ -148,11 +118,8 @@ def create_resting_preproc(name='restpreproc', base_dir=None):
148118
getthresh = pe.Node(interface=fsl.ImageStats(op_string='-p 98'),
149119
name='getthreshold')
150120
threshold_stddev = pe.Node(fsl.Threshold(), name='threshold')
151-
compcor = pe.Node(util.Function(input_names=['realigned_file',
152-
'noise_mask_file',
153-
'num_components'],
154-
output_names=['noise_components'],
155-
function=extract_noise_components),
121+
compcor = pe.Node(cc.ACompCor(components_file="noise_components.txt",
122+
use_regress_poly=False),
156123
name='compcor')
157124
remove_noise = pe.Node(fsl.FilterRegressor(filter_all=True),
158125
name='remove_noise')
@@ -170,12 +137,12 @@ def create_resting_preproc(name='restpreproc', base_dir=None):
170137
restpreproc.connect(realigner, 'outputspec.realigned_file',
171138
compcor, 'realigned_file')
172139
restpreproc.connect(threshold_stddev, 'out_file',
173-
compcor, 'noise_mask_file')
140+
compcor, 'mask_file')
174141
restpreproc.connect(inputnode, 'num_noise_components',
175142
compcor, 'num_components')
176143
restpreproc.connect(tsnr, 'detrended_file',
177144
remove_noise, 'in_file')
178-
restpreproc.connect(compcor, 'noise_components',
145+
restpreproc.connect(compcor, 'components_file',
179146
remove_noise, 'design_file')
180147
restpreproc.connect(inputnode, 'highpass_sigma',
181148
bandpass_filter, 'highpass_sigma')

0 commit comments

Comments
 (0)