diff --git a/tedana/tests/test_workflows_parser_utils.py b/tedana/tests/test_workflows_parser_utils.py index d25c89ace..da59be374 100644 --- a/tedana/tests/test_workflows_parser_utils.py +++ b/tedana/tests/test_workflows_parser_utils.py @@ -20,4 +20,5 @@ def test_check_tedpca_value(): check_tedpca_value(1.5, is_parser=False) assert check_tedpca_value(0.95) == 0.95 + assert check_tedpca_value('0.95') == 0.95 assert check_tedpca_value("mdl") == "mdl" diff --git a/tedana/workflows/parser_utils.py b/tedana/workflows/parser_utils.py index 7824cc12d..f16a9518f 100644 --- a/tedana/workflows/parser_utils.py +++ b/tedana/workflows/parser_utils.py @@ -3,7 +3,6 @@ """ import os.path as op import logging -from numbers import Number import argparse @@ -11,24 +10,21 @@ def check_tedpca_value(string, is_parser=True): """Check if argument is a float in range 0-1 or one of a list of strings.""" valid_options = ("mdl", "aic", "kic", "kundu", "kundu-stabilize") - msg = None - if string not in valid_options: - if not isinstance(string, Number): - msg = "Argument must be a float or one of: {}".format( - ", ".join(valid_options) - ) - elif not (0 <= float(string) <= 1): - msg = "Argument must be between 0 and 1." - else: - string = float(string) - - if msg: - if is_parser: - raise argparse.ArgumentTypeError(msg) - else: - raise ValueError(msg) - - return string + if string in valid_options: + return string + + error = argparse.ArgumentTypeError if is_parser else ValueError + try: + floatarg = float(string) + except ValueError: + msg = "Argument to tedpca must be a float or one of: {}".format( + ", ".join(valid_options) + ) + raise error(msg) + + if not (0 <= floatarg <= 1): + raise error("Float argument to tedpca must be between 0 and 1.") + return floatarg def is_valid_file(parser, arg):