Skip to content

Commit

Permalink
Stop interpolating edges.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Oct 2, 2023
1 parent 207a086 commit 7533507
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions xcp_d/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7533507

Please sign in to comment.