Skip to content

Commit

Permalink
fix: optimize interface to minimize memory fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Aug 27, 2024
1 parent d5b13cb commit 0c9dc65
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
21 changes: 6 additions & 15 deletions mriqc/interfaces/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,12 @@ class Spikes(SimpleInterface):

def _run_interface(self, runtime):
func_nii = nb.load(self.inputs.in_file)
func_data = func_nii.get_fdata()
func_data = func_nii.get_fdata(dtype='float32')
func_shape = func_data.shape
ntsteps = func_shape[-1]
tr = func_nii.header.get_zooms()[-1]
nskip = self.inputs.skip_frames

if self.inputs.detrend:
from nilearn.signal import clean

data = func_data.reshape(-1, ntsteps)
clean_data = clean(data[:, nskip:].T, t_r=tr, standardize=False).T
new_shape = (
func_shape[0],
func_shape[1],
func_shape[2],
clean_data.shape[-1],
)
func_data = np.zeros(func_shape)
func_data[..., nskip:] = clean_data.reshape(new_shape)

mask_data = np.bool_(nb.load(self.inputs.in_mask).dataobj)
mask_data[..., :nskip] = 0
mask_data = np.stack([mask_data] * ntsteps, axis=-1)
Expand All @@ -256,6 +242,11 @@ def _run_interface(self, runtime):
mask_data[..., : self.inputs.skip_frames] = 1
brain = np.ma.array(func_data, mask=(mask_data == 1))

if self.inputs.detrend:
from nilearn.signal import clean

brain = clean(brain[:, nskip:].T, t_r=tr, standardize=False).T

if self.inputs.no_zscore:
ts_z = find_peaks(brain)
total_spikes = []
Expand Down
1 change: 1 addition & 0 deletions mriqc/workflows/functional/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def init_func_report_wf(name='func_report_wf'):
name='SpikesFinderBgMask',
mem_gb=mem_gb * 2.5,
iterfield=['in_file', 'in_mask'],
n_procs=(config.nipype.nprocs + 3) // 4, # spikes is a memory hog
)

# Generate crown mask
Expand Down

0 comments on commit 0c9dc65

Please sign in to comment.