Skip to content

Commit cf2242c

Browse files
committed
FIX: revert confounds changes
1 parent bfe5c6f commit cf2242c

File tree

2 files changed

+7
-61
lines changed

2 files changed

+7
-61
lines changed

petprep/workflows/pet/confounds.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
"""
3030

31+
from nipype.algorithms import confounds as nac
3132
from nipype.interfaces import utility as niu
3233
from nipype.pipeline import engine as pe
3334
from templateflow.api import get as get_template
@@ -242,11 +243,7 @@ def init_pet_confs_wf(
242243

243244
# DVARS
244245
dvars = pe.Node(
245-
niu.Function(
246-
function=_compute_dvars,
247-
input_names=['pet', 'mask'],
248-
output_names=['out_nstd', 'out_std'],
249-
),
246+
nac.ComputeDVARS(save_nstd=True, save_std=True, remove_zerovariance=True),
250247
name='dvars',
251248
mem_gb=mem_gb,
252249
)
@@ -354,8 +351,8 @@ def init_pet_confs_wf(
354351

355352
workflow.connect([
356353
# connect inputnode to each non-anatomical confound node
357-
(inputnode, dvars, [('pet', 'pet'),
358-
('pet_mask', 'mask')]),
354+
(inputnode, dvars, [('pet', 'in_file'),
355+
('pet_mask', 'in_mask')]),
359356
(inputnode, motion_params, [('motion_xfm', 'xfm_file'),
360357
('petref', 'petref_file')]),
361358
(inputnode, rmsd, [('motion_xfm', 'xfm_file'),
@@ -562,30 +559,6 @@ def init_carpetplot_wf(
562559
return workflow
563560

564561

565-
def _compute_dvars(pet, mask):
566-
"""Compute DVARS only when the timeseries has at least three frames."""
567-
from pathlib import Path
568-
569-
import nibabel as nb
570-
import numpy as np
571-
from nipype.algorithms import confounds as nac
572-
573-
nvols = nb.load(pet).shape[-1]
574-
if nvols < 3:
575-
data = np.zeros((nvols,), dtype=float)
576-
out_nstd = Path('dvars.nstd.tsv').absolute()
577-
out_std = Path('dvars.std.tsv').absolute()
578-
np.savetxt(out_nstd, data)
579-
np.savetxt(out_std, data)
580-
return str(out_nstd), str(out_std)
581-
582-
dvars = nac.ComputeDVARS(save_nstd=True, save_std=True, remove_zerovariance=True)
583-
dvars.inputs.in_file = pet
584-
dvars.inputs.in_mask = mask
585-
res = dvars.run()
586-
return res.outputs.out_nstd, res.outputs.out_std
587-
588-
589562
def _binary_union(mask1, mask2):
590563
"""Generate the union of two masks."""
591564
from pathlib import Path

petprep/workflows/pet/tests/test_confounds.py

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_dvars_connects_pet_mask(tmp_path):
1515
)
1616

1717
edge = wf._graph.get_edge_data(wf.get_node('inputnode'), wf.get_node('dvars'))
18-
assert ('pet_mask', 'mask') in edge['connect']
18+
assert ('pet_mask', 'in_mask') in edge['connect']
1919

2020
img = nb.Nifti1Image(np.random.rand(2, 2, 2, 5), np.eye(4))
2121
mask = nb.Nifti1Image(np.ones((2, 2, 2), dtype=np.uint8), np.eye(4))
@@ -26,36 +26,9 @@ def test_dvars_connects_pet_mask(tmp_path):
2626

2727
node = wf.get_node('dvars')
2828
node.base_dir = tmp_path
29-
node.inputs.pet = str(pet_file)
30-
node.inputs.mask = str(mask_file)
29+
node.inputs.in_file = str(pet_file)
30+
node.inputs.in_mask = str(mask_file)
3131
result = node.run()
3232

3333
assert result.outputs.out_nstd
3434
assert result.outputs.out_std
35-
36-
37-
def test_dvars_short_series(tmp_path):
38-
"""DVARS returns empty outputs when fewer than three frames."""
39-
wf = init_pet_confs_wf(
40-
mem_gb=0.01,
41-
metadata={},
42-
regressors_all_comps=False,
43-
regressors_dvars_th=1.5,
44-
regressors_fd_th=0.5,
45-
)
46-
47-
img = nb.Nifti1Image(np.random.rand(2, 2, 2, 2), np.eye(4))
48-
mask = nb.Nifti1Image(np.ones((2, 2, 2), dtype=np.uint8), np.eye(4))
49-
pet_file = tmp_path / 'pet.nii.gz'
50-
mask_file = tmp_path / 'mask.nii.gz'
51-
img.to_filename(pet_file)
52-
mask.to_filename(mask_file)
53-
54-
node = wf.get_node('dvars')
55-
node.base_dir = tmp_path
56-
node.inputs.pet = str(pet_file)
57-
node.inputs.mask = str(mask_file)
58-
result = node.run()
59-
60-
assert np.loadtxt(result.outputs.out_nstd).sum() == 0
61-
assert np.loadtxt(result.outputs.out_std).sum() == 0

0 commit comments

Comments
 (0)