Skip to content

Commit

Permalink
Merge PR #368 | Fix smart load | Make extension check case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-yu authored Nov 28, 2024
2 parents c0a79c7 + 63d38eb commit 2d6a83e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plantseg/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def to_h5(self, path: Path | str, key: str | None, mode: Literal["a", "w", "w-"]
if isinstance(path, str):
path = Path(path)

if path.suffix not in H5_EXTENSIONS:
if path.suffix.lower() not in H5_EXTENSIONS:
raise ValueError(f"File format {path.suffix} not supported, should be one of {H5_EXTENSIONS}")

key = key if key is not None else self.name
Expand Down
2 changes: 1 addition & 1 deletion plantseg/headless/headless.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def parse_import_image_task(input_path, allow_dir: bool) -> list[Path]:
else:
raise ValueError(f"Path {input_path} is not a file or a directory.")

list_files = [f for f in list_files if f.suffix in allowed_data_format]
list_files = [f for f in list_files if f.suffix.lower() in allowed_data_format]
if not list_files:
raise ValueError(f"No valid files found in {input_path}.")

Expand Down
4 changes: 2 additions & 2 deletions plantseg/io/h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def _validate_h5_file(path: Path) -> None:
assert path.suffix in H5_EXTENSIONS, f"File extension not supported. Supported extensions: {H5_EXTENSIONS}"
assert path.suffix.lower() in H5_EXTENSIONS, f"File extension not supported. Supported extensions: {H5_EXTENSIONS}"
assert path.exists(), f"File not found: {path}"


Expand Down Expand Up @@ -73,7 +73,7 @@ def load_h5(
np.ndarray: dataset as numpy array
"""

assert path.suffix in H5_EXTENSIONS, f"File extension not supported. Supported extensions: {H5_EXTENSIONS}"
assert path.suffix.lower() in H5_EXTENSIONS, f"File extension not supported. Supported extensions: {H5_EXTENSIONS}"
assert path.exists(), f"File not found: {path}"

with h5py.File(path, "r") as f:
Expand Down
10 changes: 5 additions & 5 deletions plantseg/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def smart_load(path: Path, key: str | None = None, default=load_tiff) -> np.ndar
>>> data = smart_load('path/to/file.h5', key='raw')
"""
ext = path.suffix
ext = (path.suffix).lower()
if key == "":
key = None

Expand Down Expand Up @@ -72,7 +72,7 @@ def smart_load_with_vs(path: Path, key: str | None = None, default=load_tiff) ->
>>> data = smart_load('path/to/file.h5', key='raw')
"""
ext = path.suffix
ext = (path.suffix).lower()
if key == "":
key = None

Expand All @@ -85,9 +85,9 @@ def smart_load_with_vs(path: Path, key: str | None = None, default=load_tiff) ->
if ext in PIL_EXTENSIONS:
return load_pil(path), None

if ".zarr" in path.suffixes:
if ext in ZARR_EXTENSIONS:
return load_zarr(path, key), read_zarr_voxel_size(path, key)

else:
logger.warning(f"No default found for {ext}, reverting to default loader.")
return default(path)
logger.warning(f"No default found for {ext}, reverting to default loader with no voxel size reader.")
return default(path), None
2 changes: 1 addition & 1 deletion plantseg/viewer_napari/widgets/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def generate_layer_name(path: Path, dataset_key: str) -> str:

def look_up_dataset_keys(path: Path):
path = _return_value_if_widget(path)
ext = path.suffix
ext = path.suffix.lower()

if ext in H5_EXTENSIONS:
widget_open_file.dataset_key.show()
Expand Down
2 changes: 1 addition & 1 deletion plantseg/viewer_napari/widgets/proofreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def redo(self):
def save_state_to_disk(self, filepath: Path, raw: Image | None, pmap: Image | None = None):
"""Saves the current state to disk as an HDF5 file."""

if filepath.suffix not in H5_EXTENSIONS:
if filepath.suffix.lower() not in H5_EXTENSIONS:
log(
f'Invalid file extension: {filepath.suffix}. Please use a valid HDF5 file extensions: {H5_EXTENSIONS}',
thread='Save State',
Expand Down

0 comments on commit 2d6a83e

Please sign in to comment.