Skip to content

Commit dbf88a4

Browse files
committed
Move lag map reading and lag_step and lag_max estimation out of regr_dir is None check and reorganise lagged regression block
1 parent 6792125 commit dbf88a4

1 file changed

Lines changed: 50 additions & 48 deletions

File tree

phys2cvr/workflows.py

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,39 @@ def phys2cvr(
466466
else:
467467
petco2hrf = co2
468468

469+
# If the user provided a lag map, read it and extract information from it
470+
if lag_map:
471+
LGR.info('Load lag map')
472+
lag, _, _ = io.load_nifti_get_mask(lag_map)
473+
if func.shape[:3] != lag.shape:
474+
raise ValueError(f'{lag_map} and {fname_func} have different sizes!')
475+
476+
# Read lag_step and lag_max from file (or try to)
477+
lag = lag * mask
478+
479+
lag_list = np.unique(lag[mask])
480+
481+
if lag_step is None:
482+
# np.unique sorts results already
483+
lag_step = np.unique(np.round(lag_list[1:] - lag_list[:-1], 3))
484+
if lag_step.size > 1:
485+
# Check if extra steps found are multiple of the first within numerical tolerance
486+
if not np.isclose(np.mod(lag_step, lag_step[0]), 0).all():
487+
raise ValueError(
488+
f'phys2cvr found different delta lags in {lag_map}: {lag_step}'
489+
)
490+
491+
lag_step = lag_step[0]
492+
LGR.warning(f'phys2cvr detected a delta lag of {lag_step} seconds')
493+
else:
494+
LGR.warning(f'Forcing delta lag to be {lag_step}')
495+
496+
if lag_max is None:
497+
lag_max = np.abs(lag_list).max()
498+
LGR.warning(f'phys2cvr detected a max lag of {lag_max} seconds')
499+
else:
500+
LGR.warning(f'Forcing max lag to be {lag_max}')
501+
469502
# If a regressor dir is specified, try load the data,
470503
# If failing or otherwise, compute the regressors.
471504
if run_regression and regr_dir is not None:
@@ -480,35 +513,6 @@ def phys2cvr(
480513
regr_dir = None
481514

482515
if regr_dir is None:
483-
if lag_map:
484-
lag, _, _ = io.load_nifti_get_mask(lag_map)
485-
if func.shape[:3] != lag.shape:
486-
raise ValueError(f'{lag_map} and {fname_func} have different sizes!')
487-
488-
# Read lag_step and lag_max from file (or try to)
489-
lag = lag * mask
490-
491-
lag_list = np.unique(lag[mask > 0])
492-
493-
if lag_step is None:
494-
lag_step = np.unique(np.round(lag_list[1:] - lag_list[:-1], 3))
495-
if lag_step.size > 1:
496-
raise ValueError(
497-
f'phys2cvr found different delta lags in {lag_map}'
498-
)
499-
else:
500-
lag_step = lag_step[0]
501-
LGR.warning(f'phys2cvr detected a delta lag of {lag_step} seconds')
502-
else:
503-
LGR.warning(f'Forcing delta lag to be {lag_step}')
504-
505-
step = int(lag_step * freq)
506-
507-
if lag_max is None:
508-
lag_max = np.abs(lag_list).max()
509-
LGR.warning(f'phys2cvr detected a max lag of {lag_max} seconds')
510-
else:
511-
LGR.warning(f'Forcing max lag to be {lag_max}')
512516
regr, regr_shifts = create_physio_regressor(
513517
func_avg,
514518
petco2hrf,
@@ -612,37 +616,25 @@ def phys2cvr(
612616
r_square, oimg, f'{fname_out_func}_r_square_simple{fname_ext}'
613617
)
614618

615-
if (
616-
lagged_regression
617-
and regr_shifts is not None
618-
and ((lag_max and lag_step) or lag_map)
619-
):
620-
if lag_max:
621-
LGR.info(
622-
f'Running lagged CVR estimation with max lag = {lag_max}! '
623-
'(might take a while...)'
624-
)
625-
elif lag_map is not None:
619+
if lagged_regression and regr_shifts is not None and (lag_max and lag_step):
620+
# If user specified a lag map, run regression based on it (see "Load lag map")
621+
if lag_map is not None:
626622
LGR.info(
627623
f'Running lagged CVR estimation with lag map {lag_map}! '
628624
'(might take a while...)'
629625
)
630-
if legacy:
631-
nrep = int(lag_max * freq * 2)
632-
else:
633-
nrep = int(lag_max * freq * 2) + 1
634626

635-
# If user specified a lag map, use that one to regress things
636-
if lag_map:
627+
step = int(lag_step * freq)
628+
637629
lag_idx = np.round((lag + lag_max) * freq / step).astype(int)
638630
lag_idx_list = np.unique(lag_idx)
639631

640632
# Prepare empty matrices
641633
beta = np.empty_like(lag, dtype='float32')
642634
tstat = np.empty_like(lag, dtype='float32')
643635

644-
for index, i in enumerate(lag_idx_list):
645-
LGR.info(f'Perform L-GLM number {index + 1} of {len(lag_idx_list)}')
636+
for n, i in enumerate(lag_idx_list):
637+
LGR.info(f'Perform L-GLM number {n + 1} of {len(lag_idx_list)}')
646638
regr = regr_shifts[(i * step), :, np.newaxis]
647639

648640
x1D = os.path.join(outdir, 'mat', f'mat_{i:04g}.1D')
@@ -660,6 +652,16 @@ def phys2cvr(
660652
)
661653

662654
else:
655+
LGR.info(
656+
f'Running lagged CVR estimation with max lag = {lag_max}! '
657+
'(might take a while...)'
658+
)
659+
660+
if legacy:
661+
nrep = int(lag_max * freq * 2)
662+
else:
663+
nrep = int(lag_max * freq * 2) + 1
664+
663665
# Check the number of repetitions first
664666
if lag_step:
665667
step = int(lag_step * freq)

0 commit comments

Comments
 (0)