Skip to content

Commit

Permalink
Add everything matcher default, so that program can be run without args
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Mar 1, 2024
1 parent 38c0937 commit 45285ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion visualdataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
|_|
"""

__version__ = '0.2.3'
__version__ = '0.3.0'
12 changes: 6 additions & 6 deletions visualdataset/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
parser = ArgumentParser(description='Prepares a dataset for use with the ChRIS_ui '
'"Visual Datasets" feature.',
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--mode', type=str, default='file', choices=['file', 'string', 'freesurfer-7.3.3', 'malpem-1.3'],
help='File matching and option selection mode. file=accept JSON files from '
'--matchers and --options. string=accept JSON strings from --matchers and --options. '
'freesurfer=built-in support for the FreeSurfer output files.')
parser.add_argument('--mode', type=str, default='none', choices=['freesurfer-7.3.3', 'malpem-1.3', 'none'],
help='Use default values for --matchers and --options for a well known program\'s output. ')
parser.add_argument('--matchers', type=str,
help='Regular expressions used to assign tags to files')
help='Regular expressions used to assign tags to files, '
'as stringified JSON or a path to a file relative to inputdir')
parser.add_argument('--options', type=str,
help='Metadata to go with tag sets')
help='Metadata to go with tag sets, '
'as stringified JSON or a path to a file relative to inputdir')
parser.add_argument('--first-run-files', type=str, default='[]',
help='List of files to show on first run, '
'as a stringified JSON list of paths relative to inputdir')
Expand Down
50 changes: 35 additions & 15 deletions visualdataset/json_arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,50 @@
from visualdataset.wellknown import FREESURFER_MATCHERS, FREESURFER_OPTIONS, MALPEM_MATCHERS, MALPEM_OPTIONS


EVERYTHING_MATCHER: Sequence[Matcher] = [Matcher(key='type', value='file', regex=r'\.(nii(\.gz)?)|(mgz)$')]
"""
a Matcher matching all supported file formats.
"""


def parse_args(input_dir: Path, mode: str, matchers: str | None, options: str | None
) -> tuple[Sequence[Matcher], Sequence[OptionsLink]]:
mode = mode.lower()
if mode.startswith('freesurfer'):
return FREESURFER_MATCHERS, FREESURFER_OPTIONS
if mode.startswith('malpem'):
return MALPEM_MATCHERS, MALPEM_OPTIONS
if mode == 'file':
matchers_str = '[]' if matchers is None else (input_dir / matchers).read_text()
options_str = '[]' if options is None else (input_dir / options).read_text()
elif mode == 'string':
matchers_str = '[]' if matchers is None else matchers
options_str = '[]' if options is None else options
else:
print(f'Unsupported option --mode={mode}')
sys.exit(1)
matchers_list = deserialize_list(matchers_str, Matcher, '--matchers')
options_list = deserialize_list(options_str, OptionsLink, '--options')
matchers_list = None
options_list = []

if 'freesurfer' in mode:
matchers_list = FREESURFER_MATCHERS
options_list = FREESURFER_OPTIONS
if 'malpem' in mode:
matchers_list = MALPEM_MATCHERS
options_list = MALPEM_OPTIONS

if matchers is not None:
matchers_list = deserialize_list(file_or_string(input_dir, matchers), Matcher, '--matchers')
if options is not None:
options_list = deserialize_list(file_or_string(input_dir, options), OptionsLink, '--options')

if matchers_list is None:
matchers_list = EVERYTHING_MATCHER

return matchers_list, options_list


_M = TypeVar('_M', bound=BaseModel)


def file_or_string(dir: Path, arg: str) -> str:
path = dir / arg
try:
is_file = path.is_file()
except OSError: # in case file name is too long (it's probably the value!)
is_file = False
if is_file:
return path.read_text()
return arg


def deserialize_list(s: str, t: Type[_M], flag: str) -> Sequence[_M]:
try:
data = json.loads(s)
Expand Down

0 comments on commit 45285ea

Please sign in to comment.