Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Allow tedpca argument to be a float inside a string #665

Merged
merged 5 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tedana/tests/test_workflows_parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
34 changes: 15 additions & 19 deletions tedana/workflows/parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@
"""
import os.path as op
import logging
from numbers import Number

import argparse


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):
Expand Down