Skip to content

Commit

Permalink
Use brain mask in NIFTI connectivity workflow (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo authored Dec 19, 2022
1 parent 786533d commit 76ce7d7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .circleci/tests/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def test_nifti_conn(fmriprep_with_freesurfer_data, tmp_path_factory):
fcon_ts_wf.inputs.inputnode.t1w_to_native = t1w_to_native_xform
fcon_ts_wf.inputs.inputnode.clean_bold = fake_bold_file
fcon_ts_wf.inputs.inputnode.bold_file = bold_file
fcon_ts_wf.inputs.inputnode.bold_mask = bold_mask
fcon_ts_wf.inputs.inputnode.ref_file = boldref
fcon_ts_wf.base_dir = tmp_path_factory.mktemp("fcon_nifti_test_2")
fcon_ts_wf.run()
Expand Down Expand Up @@ -86,7 +87,7 @@ def test_nifti_conn(fmriprep_with_freesurfer_data, tmp_path_factory):
atlas = nb.load(atlas)

# Masking img
masker = NiftiLabelsMasker(atlas, smoothing_fwhm=None, standardize=False)
masker = NiftiLabelsMasker(atlas, mask_img=bold_mask, smoothing_fwhm=None, standardize=False)
# Fitting mask
masker.fit(fake_bold_file)
signals = masker.transform(fake_bold_file)
Expand Down
12 changes: 10 additions & 2 deletions xcp_d/interfaces/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

class _NiftiConnectInputSpec(BaseInterfaceInputSpec):
filtered_file = File(exists=True, mandatory=True, desc="filtered file")
mask = File(exists=True, mandator=True, desc="brain mask file")
atlas = File(exists=True, mandatory=True, desc="atlas file")


Expand All @@ -48,10 +49,16 @@ def _run_interface(self, runtime):
# Then write out functional correlation matrix of
# timeseries using numpy.
self._results["time_series_tsv"] = fname_presuffix(
self.inputs.filtered_file, suffix="time_series.tsv", newpath=runtime.cwd, use_ext=False
self.inputs.filtered_file,
suffix="time_series.tsv",
newpath=runtime.cwd,
use_ext=False,
)
self._results["fcon_matrix_tsv"] = fname_presuffix(
self.inputs.filtered_file, suffix="fcon_matrix.tsv", newpath=runtime.cwd, use_ext=False
self.inputs.filtered_file,
suffix="fcon_matrix.tsv",
newpath=runtime.cwd,
use_ext=False,
)

(
Expand All @@ -60,6 +67,7 @@ def _run_interface(self, runtime):
) = extract_timeseries_funct(
in_file=self.inputs.filtered_file,
atlas=self.inputs.atlas,
mask=self.inputs.mask,
timeseries=self._results["time_series_tsv"],
fconmatrix=self._results["fcon_matrix_tsv"],
)
Expand Down
13 changes: 11 additions & 2 deletions xcp_d/utils/fcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
from templateflow.api import get as get_template


def extract_timeseries_funct(in_file, atlas, timeseries, fconmatrix):
def extract_timeseries_funct(in_file, mask, atlas, timeseries, fconmatrix):
"""Use Nilearn NiftiLabelsMasker to extract timeseries.
Parameters
----------
in_file : str
bold file timeseries
mask : str
BOLD file's associated brain mask file.
atlas : str
atlas in the same space with bold
timeseries : str
Expand All @@ -30,9 +32,16 @@ def extract_timeseries_funct(in_file, atlas, timeseries, fconmatrix):
fconmatrix : str
functional connectivity matrix filename
"""
masker = NiftiLabelsMasker(labels_img=atlas, smoothing_fwhm=None, standardize=False)
masker = NiftiLabelsMasker(
labels_img=atlas,
mask_img=mask,
smoothing_fwhm=None,
standardize=False,
)

# Use nilearn for time_series
time_series = masker.fit_transform(in_file)

# Use numpy for correlation matrix
correlation_matrices = np.corrcoef(time_series.T)

Expand Down
7 changes: 5 additions & 2 deletions xcp_d/workflow/bold.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,11 @@ def init_boldpostprocess_wf(

# functional connect workflow
workflow.connect([
(downcast_data, fcon_ts_wf, [('bold_file', 'inputnode.bold_file'),
('ref_file', 'inputnode.ref_file')]),
(downcast_data, fcon_ts_wf, [
('bold_file', 'inputnode.bold_file'),
("bold_mask", "inputnode.bold_mask"),
('ref_file', 'inputnode.ref_file'),
]),
(inputnode, fcon_ts_wf, [('template_to_t1w', 'inputnode.template_to_t1w'),
('t1w_to_native', 'inputnode.t1w_to_native')]),
(filtering_wf, fcon_ts_wf, [('filtered_file', 'inputnode.clean_bold')])
Expand Down
14 changes: 12 additions & 2 deletions xcp_d/workflow/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ def init_nifti_functional_connectivity_wf(

inputnode = pe.Node(
niu.IdentityInterface(
fields=["bold_file", "ref_file", "clean_bold", "template_to_t1w", "t1w_to_native"],
fields=[
"bold_file",
"bold_mask",
"ref_file",
"clean_bold",
"template_to_t1w",
"t1w_to_native",
],
),
name="inputnode",
)
Expand Down Expand Up @@ -145,7 +152,10 @@ def init_nifti_functional_connectivity_wf(
("template_to_t1w", "template_to_t1w"),
("t1w_to_native", "t1w_to_native")]),
(inputnode, warp_atlases_to_bold_space, [("ref_file", "reference_image")]),
(inputnode, nifti_connect, [("clean_bold", "filtered_file")]),
(inputnode, nifti_connect, [
("clean_bold", "filtered_file"),
("bold_mask", "mask"),
]),
(inputnode, matrix_plot, [("clean_bold", "in_file")]),
(atlas_name_grabber, outputnode, [("atlas_names", "atlas_names")]),
(atlas_name_grabber, atlas_file_grabber, [("atlas_names", "atlas_name")]),
Expand Down

0 comments on commit 76ce7d7

Please sign in to comment.