From 0c9dc6589ec0b168e3ae25bd1f542419574c9dbb Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Tue, 27 Aug 2024 13:53:40 +0200 Subject: [PATCH] fix: optimize interface to minimize memory fingerprint --- mriqc/interfaces/functional.py | 21 ++++++--------------- mriqc/workflows/functional/output.py | 1 + 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/mriqc/interfaces/functional.py b/mriqc/interfaces/functional.py index 39d34ead..7d321ad7 100644 --- a/mriqc/interfaces/functional.py +++ b/mriqc/interfaces/functional.py @@ -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) @@ -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 = [] diff --git a/mriqc/workflows/functional/output.py b/mriqc/workflows/functional/output.py index 427b7683..df7318be 100644 --- a/mriqc/workflows/functional/output.py +++ b/mriqc/workflows/functional/output.py @@ -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