From 7533507e12979cdfd6c295199c008a6ecad122a3 Mon Sep 17 00:00:00 2001 From: Taylor Salo Date: Mon, 2 Oct 2023 11:59:04 -0400 Subject: [PATCH] Stop interpolating edges. --- xcp_d/utils/utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/xcp_d/utils/utils.py b/xcp_d/utils/utils.py index 06397ab3c..39dde2816 100644 --- a/xcp_d/utils/utils.py +++ b/xcp_d/utils/utils.py @@ -463,6 +463,24 @@ def denoise_with_nilearn( sample_mask=sample_mask, t_r=TR, ) + # Replace any high-motion volumes at the beginning or end of the run with the closest + # low-motion volume's data. + # From https://stackoverflow.com/a/48106843/2589328 + nums = sorted(set(np.where(sample_mask)[0])) + gaps = [[s, e] for s, e in zip(nums, nums[1:]) if s + 1 < e] + edges = iter(nums[:1] + sum(gaps, []) + nums[-1:]) + consecutive_outliers_idx = list(zip(edges, edges)) + if consecutive_outliers_idx[0][0] == 0: + for i_vol in range(consecutive_outliers_idx[0][0], consecutive_outliers_idx[0][1] + 1): + interpolated_unfiltered_bold[i_vol, :] = interpolated_unfiltered_bold[ + consecutive_outliers_idx[0][1] + 1, : + ] + + if consecutive_outliers_idx[-1][1] == n_volumes: + for i_vol in range(consecutive_outliers_idx[-1][0], consecutive_outliers_idx[0][1] + 1): + interpolated_unfiltered_bold[i_vol, :] = interpolated_unfiltered_bold[ + consecutive_outliers_idx[-1][0] - 1, : + ] # Now apply the bandpass filter to the interpolated, denoised data if low_pass is not None and high_pass is not None: