From ef3c62ea395af05231bb5e2e9ae575261cdc724a Mon Sep 17 00:00:00 2001 From: Jennings Zhang Date: Thu, 1 Feb 2024 23:19:38 -0500 Subject: [PATCH] Limit operation to dirs containing *.nii.gz --- pubchrisvisual/__init__.py | 2 +- pubchrisvisual/one.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pubchrisvisual/__init__.py b/pubchrisvisual/__init__.py index 11d1f91..882e030 100644 --- a/pubchrisvisual/__init__.py +++ b/pubchrisvisual/__init__.py @@ -9,4 +9,4 @@ |_| """ -__version__ = '0.0.2' +__version__ = '0.0.3' diff --git a/pubchrisvisual/one.py b/pubchrisvisual/one.py index 141e6a3..de75433 100644 --- a/pubchrisvisual/one.py +++ b/pubchrisvisual/one.py @@ -41,7 +41,7 @@ def main(options: Namespace, inputdir: Path, outputdir: Path): order = [name.strip() for name in ','.strip(options.order)] print(DISPLAY_TITLE, flush=True) shutil.copytree(inputdir, outputdir, dirs_exist_ok=True) - for folder in folders(outputdir): + for folder in subject_folders(outputdir): files = [p for p in folder.glob('*') if p.is_file()] preferred = get_preferred_file(files, order) for file in files: @@ -69,14 +69,18 @@ def get_preferred_file(files: Sequence[Path], order: Sequence[str]) -> Path: return files[0] -def folders(p: Path) -> Iterable[Path]: - return filter(is_nonempty_dir, p.glob('*')) +def subject_folders(p: Path) -> Iterable[Path]: + return filter(is_dir_containing_nifti, p.glob('*')) -def is_nonempty_dir(p: Path) -> bool: +def is_dir_containing_nifti(p: Path) -> bool: if not p.is_dir(): return False - return next(p.glob('*'), None) is not None + return next(filter(is_nifti_file, p.glob('*.nii.gz')), None) is not None + + +def is_nifti_file(p: Path): + return p.is_file() and p.name.endswith('.nii.gz') def is_nifti(p: Path) -> bool: