From 8cd46567cb8006f367a39bb5d98663c04a597095 Mon Sep 17 00:00:00 2001 From: Zaki A Date: Mon, 25 Jan 2021 16:53:16 -0600 Subject: [PATCH 1/4] Allow tedpca argument to be string containing float --- tedana/tests/test_workflows_parser_utils.py | 1 + tedana/workflows/parser_utils.py | 32 +++++++++------------ 2 files changed, 15 insertions(+), 18 deletions(-) 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..91f9aeb78 100644 --- a/tedana/workflows/parser_utils.py +++ b/tedana/workflows/parser_utils.py @@ -11,24 +11,20 @@ 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 must be a float or one of: {}".format( + ", ".join(valid_options) + ) + raise error(msg) + if not (0 <= floatarg <= 1): + raise error("Argument must be between 0 and 1.") + return floatarg def is_valid_file(parser, arg): From 60818131080537901351be895fef4e28d8e95198 Mon Sep 17 00:00:00 2001 From: Zaki A Date: Wed, 27 Jan 2021 18:30:01 -0600 Subject: [PATCH 2/4] Appease the style checker --- tedana/workflows/parser_utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tedana/workflows/parser_utils.py b/tedana/workflows/parser_utils.py index 91f9aeb78..10f6b257b 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 @@ -23,7 +22,7 @@ def check_tedpca_value(string, is_parser=True): ) raise error(msg) if not (0 <= floatarg <= 1): - raise error("Argument must be between 0 and 1.") + raise error("Argument must be between 0 and 1.") return floatarg From 1a50642f353b5355f75fa306ff0888f19021352f Mon Sep 17 00:00:00 2001 From: Zaki A Date: Thu, 28 Jan 2021 13:07:04 -0600 Subject: [PATCH 3/4] Add space between lines for readability --- tedana/workflows/parser_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tedana/workflows/parser_utils.py b/tedana/workflows/parser_utils.py index 10f6b257b..262d6fb0e 100644 --- a/tedana/workflows/parser_utils.py +++ b/tedana/workflows/parser_utils.py @@ -21,6 +21,7 @@ def check_tedpca_value(string, is_parser=True): ", ".join(valid_options) ) raise error(msg) + if not (0 <= floatarg <= 1): raise error("Argument must be between 0 and 1.") return floatarg From 1540513d0bb10658dc568f2cb620cef3e6b3c6e4 Mon Sep 17 00:00:00 2001 From: Zaki A Date: Thu, 28 Jan 2021 13:10:39 -0600 Subject: [PATCH 4/4] Specify that error message is for tedpca argument --- tedana/workflows/parser_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tedana/workflows/parser_utils.py b/tedana/workflows/parser_utils.py index 262d6fb0e..f16a9518f 100644 --- a/tedana/workflows/parser_utils.py +++ b/tedana/workflows/parser_utils.py @@ -17,13 +17,13 @@ def check_tedpca_value(string, is_parser=True): try: floatarg = float(string) except ValueError: - msg = "Argument must be a float or one of: {}".format( + 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("Argument must be between 0 and 1.") + raise error("Float argument to tedpca must be between 0 and 1.") return floatarg