Skip to content

Commit

Permalink
Store lengths as strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Nov 19, 2024
1 parent 955d1da commit 1e53cea
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 23 deletions.
16 changes: 4 additions & 12 deletions xcp_d/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,9 +967,7 @@ def _validate_parameters(opts, build_log, parser):
opts.confounds_config = (
'36P' if (opts.confounds_config == 'auto') else opts.confounds_config
)
opts.correlation_lengths = (
[] if opts.correlation_lengths is None else opts.correlation_lengths
)
opts.correlation_lengths = opts.correlation_lengths if opts.correlation_lengths else []
opts.despike = True if (opts.despike == 'auto') else opts.despike
opts.fd_thresh = 0.3 if (opts.fd_thresh == 'auto') else opts.fd_thresh
opts.file_format = 'cifti' if (opts.file_format == 'auto') else opts.file_format
Expand All @@ -989,9 +987,7 @@ def _validate_parameters(opts, build_log, parser):
opts.confounds_config = (
'36P' if (opts.confounds_config == 'auto') else opts.confounds_config
)
opts.correlation_lengths = (
[] if opts.correlation_lengths is None else opts.correlation_lengths
)
opts.correlation_lengths = opts.correlation_lengths if opts.correlation_lengths else []
opts.despike = True if (opts.despike == 'auto') else opts.despike
opts.fd_thresh = 0.3 if (opts.fd_thresh == 'auto') else opts.fd_thresh
opts.file_format = 'cifti' if (opts.file_format == 'auto') else opts.file_format
Expand Down Expand Up @@ -1034,9 +1030,7 @@ def _validate_parameters(opts, build_log, parser):
opts.confounds_config = (
'36P' if (opts.confounds_config == 'auto') else opts.confounds_config
)
opts.correlation_lengths = (
'all' if opts.correlation_lengths is None else opts.correlation_lengths
)
opts.correlation_lengths = opts.correlation_lengths if opts.correlation_lengths else 'all'
opts.despike = True if (opts.despike == 'auto') else opts.despike
opts.fd_thresh = 0 if (opts.fd_thresh == 'auto') else opts.fd_thresh
opts.file_format = 'nifti' if (opts.file_format == 'auto') else opts.file_format
Expand All @@ -1056,9 +1050,7 @@ def _validate_parameters(opts, build_log, parser):
if opts.confounds_config == 'auto':
error_messages.append("'--nuisance-regressors' is required for 'none' mode.")

opts.correlation_lengths = (
[] if opts.correlation_lengths is None else opts.correlation_lengths
)
opts.correlation_lengths = opts.correlation_lengths if opts.correlation_lengths else []

if opts.despike == 'auto':
error_messages.append("'--despike' (y or n) is required for 'none' mode.")
Expand Down
2 changes: 1 addition & 1 deletion xcp_d/cli/parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _float_or_auto_or_none(string, is_parser=True):

if floatarg < 0:
raise error('Float argument must be nonnegative.')
return floatarg
return str(floatarg)


def _restricted_float(x):
Expand Down
6 changes: 3 additions & 3 deletions xcp_d/tests/test_cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def test_validate_parameters_linc_mode(base_opts, base_parser, capsys):
assert opts.correlation_lengths == ['all']

# --create-matrices is not supported
opts.correlation_lengths = [300]
opts.correlation_lengths = ['300']
with pytest.raises(SystemExit, match='2'):
parser._validate_parameters(deepcopy(opts), build_log, parser=base_parser)

Expand Down Expand Up @@ -459,7 +459,7 @@ def test_build_parser_01(tmp_path_factory):
opts = parser_obj.parse_args(args=test_args, namespace=None)
assert opts.fmri_dir == data_path
assert opts.output_dir == out_path
assert opts.correlation_lengths == ['all', 300, 480]
assert opts.correlation_lengths == ['all', '300', '480']


def test_build_parser_02(tmp_path_factory):
Expand Down Expand Up @@ -496,7 +496,7 @@ def test_build_parser_02(tmp_path_factory):
opts = parser_obj.parse_args(args=test_args, namespace=None)
assert opts.fmri_dir == data_path
assert opts.output_dir == out_path
assert opts.correlation_lengths == ['all', 300, 480]
assert opts.correlation_lengths == ['all', '300', '480']


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion xcp_d/utils/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
"""

docdict['correlation_lengths'] = """
correlation_lengths : :obj:`list` of :obj:`float`, optional
correlation_lengths : :obj:`list` of :obj:`str`, optional
If used, this parameter will produce correlation matrices limited to each requested
amount of time.
If there is more than the required amount of low-motion data,
Expand Down
9 changes: 9 additions & 0 deletions xcp_d/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,12 @@ def _create_mem_gb(bold_fname):
mem_gbz['resampled'] = 3

return mem_gbz


def is_number(s):
"""Check if a string is a number."""
try:
float(s)
return True
except ValueError:
return False
6 changes: 2 additions & 4 deletions xcp_d/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
)
from xcp_d.utils.doc import fill_doc
from xcp_d.utils.modified_data import calculate_exact_scans, flag_bad_run
from xcp_d.utils.utils import estimate_brain_radius
from xcp_d.utils.utils import estimate_brain_radius, is_number
from xcp_d.workflows.anatomical.parcellation import init_parcellate_surfaces_wf
from xcp_d.workflows.anatomical.surface import init_postprocess_surfaces_wf
from xcp_d.workflows.anatomical.volume import init_postprocess_anat_wf
Expand Down Expand Up @@ -458,9 +458,7 @@ def init_single_subject_wf(subject_id: str):

# Reduce exact_times to only include values greater than the post-scrubbing duration.
exact_scans = []
if any(
isinstance(length, int | float) for length in config.workflow.correlation_lengths
):
if any(is_number(length) for length in config.workflow.correlation_lengths):
exact_scans = calculate_exact_scans(
exact_times=config.workflow.correlation_lengths,
scan_length=post_scrubbing_duration,
Expand Down
4 changes: 2 additions & 2 deletions xcp_d/workflows/bold/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from xcp_d.utils.doc import fill_doc
from xcp_d.utils.plotting import plot_design_matrix as _plot_design_matrix
from xcp_d.utils.utils import fwhm2sigma
from xcp_d.utils.utils import fwhm2sigma, is_number


@fill_doc
Expand Down Expand Up @@ -327,7 +327,7 @@ def init_prepare_confounds_wf(
]),
]) # fmt:skip

if any(isinstance(length, int | float) for length in config.workflow.correlation_lengths):
if any(is_number(length) for length in config.workflow.correlation_lengths):
random_censor = pe.Node(
RandomCensor(exact_scans=exact_scans, random_seed=config.seeds.master),
name='random_censor',
Expand Down

0 comments on commit 1e53cea

Please sign in to comment.