Repository: DIAGNijmegen/rse-panimg (language: python)

Issue:
Add a `recurse_subdirectories: bool = True` kwarg to `convert`
I would like panimg to only traverse the top level directory. Add this kwarg to `convert` and `_convert_directory`, check if the subdirectory should be processed at https://github.com/DIAGNijmegen/rse-panimg/blob/faf0c347d092d2ed1d3a9f5c95dd4588dd0d87cd/panimg/panimg.py#L86. Probably pass the kwarg down to the calls of `_convert_directory` too (although that is probably unnecessary). Add a test with an image in a subdirectory too and check that only the images from the top level directory are created.


Discussion / hints:

Fix patch:
diff --git a/README.md b/README.md
index ef255c7..f981b2c 100644
--- a/README.md
+++ b/README.md
@@ -23,10 +23,12 @@ Under the hood we use:
 
 ## Usage
 
-`panimg` takes a folder and tries to covert the containing files to MHA or TIFF.
-For each subdirectory of files it will try several strategies for loading the contained files, and if an image is found it will output it to the output folder.
+`panimg` takes a folder and tries to convert the containing files to MHA or TIFF.
+By default, it will try to convert files from subdirectories as well. To only convert files in the top level directory, set `recurse_subdirectories` to `False`.
+It will try several strategies for loading the contained files, and if an image is found it will output it to the output folder.
 It will return a structure containing information about what images were produced, what images were used to form the new images, image metadata, and any errors from any of the strategies.
 
+
 **NOTE: Alpha software, do not run this on folders you do not have a backup of.**
 
 ```python
diff --git a/panimg/panimg.py b/panimg/panimg.py
index 043ae10..2096a9d 100644
--- a/panimg/panimg.py
+++ b/panimg/panimg.py
@@ -24,6 +24,7 @@ def convert(
     output_directory: Path,
     builders: Optional[Iterable[ImageBuilder]] = None,
     post_processors: Optional[Iterable[PostProcessor]] = None,
+    recurse_subdirectories: bool = True,
 ) -> PanImgResult:
     new_images: Set[PanImg] = set()
     new_image_files: Set[PanImgFile] = set()
@@ -45,6 +46,7 @@ def convert(
         new_image_files=new_image_files,
         new_folders=new_folders,
         file_errors=file_errors,
+        recurse_subdirectories=recurse_subdirectories,
     )
 
     result = _post_process(
@@ -75,6 +77,7 @@ def _convert_directory(
     new_image_files: Set[PanImgFile],
     new_folders: Set[PanImgFolder],
     file_errors: DefaultDict[Path, List[str]],
+    recurse_subdirectories: bool = True,
 ):
     input_directory = Path(input_directory).resolve()
     output_directory = Path(output_directory).resolve()
@@ -83,7 +86,7 @@ def _convert_directory(
 
     files = set()
     for o in Path(input_directory).iterdir():
-        if o.is_dir():
+        if o.is_dir() and recurse_subdirectories:
             _convert_directory(
                 input_directory=input_directory
                 / o.relative_to(input_directory),
@@ -95,6 +98,7 @@ def _convert_directory(
                 new_image_files=new_image_files,
                 new_folders=new_folders,
                 file_errors=file_errors,
+                recurse_subdirectories=recurse_subdirectories,
             )
         elif o.is_file():
             files.add(o)


Test patch:
diff --git a/tests/test_background_tasks.py b/tests/test_background_tasks.py
index 7d0cad1..7a57009 100644
--- a/tests/test_background_tasks.py
+++ b/tests/test_background_tasks.py
@@ -140,3 +140,33 @@ def test_mhd_file_annotation_creation(tmpdir_factory):
     assert image.height == 6
     assert image.width == 5
     assert image.color_space == ColorSpace.GRAY
+
+
+def test_subdirectory_traverse_setting(tmpdir_factory):
+    input_dir = tmpdir_factory.mktemp("input")
+    sub_dir = input_dir / "sub"
+    sub_dir.mkdir()
+
+    image = RESOURCE_PATH / "image10x11x12.nii"
+    image_sub = RESOURCE_PATH / "image10x10x10.mha"
+
+    shutil.copy(image, input_dir)
+    shutil.copy(image_sub, sub_dir)
+    output_dir = tmpdir_factory.mktemp("output")
+
+    result = convert(
+        input_directory=input_dir,
+        output_directory=output_dir,
+        recurse_subdirectories=True,
+    )
+
+    assert len(result.new_images) == 2
+
+    output_dir = tmpdir_factory.mktemp("output2")
+    result = convert(
+        input_directory=input_dir,
+        output_directory=output_dir,
+        recurse_subdirectories=False,
+    )
+
+    assert len(result.new_images) == 1