-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description This is a precursor for the soon to come save prediction feature. Save functions needed to be added; then because they mirror the structure of the read functions, the read functions have been moved from `dataset.dataset_utils` to a new `file_io` package. This package has `read` and `write` subpackages that mirror each other's structures, for ease of understanding. Additionally, the `get_read_func` is slightly refactored to match how the `get_write_func` is implemented. Read functions are stored in a module level dictionary with the keys being `SupportedData`, the `get_read_func` indexes this dictionary based on the `data_type` passed. This removes the eventuality of a long list of if/else statements. This wasn't strictly necessary as we do not plan to support a large number of data types, but the option is always there. An additional extra change that snuck into this PR is renaming `SupportedData.get_extension` to `SupportedData.get_extension_pattern`, and adding a different `SupportedData.get_extension`. The new `SupportedData.get_extension` returns the literal string without the unix wildcard patterns and will be used for saving predictions in a future PR. - **What**: - Added a `file_io` package to contain functions to read and write image files. - `SupportedData.get_extension` modification and addition. - **Why**: Partly an aesthetic choice, removes the responsibility of file reading and writing from the `datasets` package in accordance with trying to follow the single-responsibility principle. - **How**: Added write functions. Moved and slightly refactored read functions. ### Changes Made - **Added**: - `file_io`, `file_io.read`, `file_io.write` packages. - `write_tiff` function - `get_write_func` function - New `SupportedData.get_extension` to return literal extension string - Tests for all the above new functions - **Modified**: - Old `SupportedData.get_extension` renamed to `SupportedData.get_extension_pattern` - Renamed tests to mirror name change in function - **Removed**: - File reading from `datasets.dataset_utils` ### Breaking changes - Code calling read functions from `datasets.dataset_utils` directly. - Code using `SupportedData.get_extension` directly. ### Additional Notes and Examples In a future PR `SupportedData.get_extension` and `SupportedData.get_extension_pattern` could be moved to the `file_io` package. These functions do not need to be bound to `SupportedData` and none of the other "support" `Enum` classes have additional methods. It might make sense to store these functions closer to where they are used. This is again a stylistic choice, feel free to share other opinions. --- **Please ensure your PR meets the following requirements:** - [x] Code builds and passes tests locally, including doctests - [x] New tests have been added (for bug fixes/features) - [x] Pre-commit passes - [ ] PR to the documentation exists (for bug fixes / features) --------- Co-authored-by: jdeschamps <6367888+jdeschamps@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ff20596
commit 57ceab2
Showing
25 changed files
with
320 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"""Functions relating reading and writing image files.""" | ||
|
||
__all__ = ["read", "write", "get_read_func", "get_write_func"] | ||
|
||
from . import read, write | ||
from .read import get_read_func | ||
from .write import get_write_func |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Functions relating to reading image files of different formats.""" | ||
|
||
__all__ = [ | ||
"get_read_func", | ||
"read_tiff", | ||
"read_zarr", | ||
] | ||
|
||
from .get_func import get_read_func | ||
from .tiff import read_tiff | ||
from .zarr import read_zarr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Module to get read functions.""" | ||
|
||
from pathlib import Path | ||
from typing import Callable, Dict, Protocol, Union | ||
|
||
from numpy.typing import NDArray | ||
|
||
from careamics.config.support import SupportedData | ||
|
||
from .tiff import read_tiff | ||
|
||
|
||
# This is very strict, function signature has to match including arg names | ||
# See WriteFunc notes | ||
class ReadFunc(Protocol): | ||
"""Protocol for type hinting read functions.""" | ||
|
||
def __call__(self, file_path: Path, *args, **kwargs) -> NDArray: | ||
""" | ||
Type hinted callables must match this function signature (not including self). | ||
Parameters | ||
---------- | ||
file_path : pathlib.Path | ||
Path to file. | ||
*args | ||
Other positional arguments. | ||
**kwargs | ||
Other keyword arguments. | ||
""" | ||
|
||
|
||
READ_FUNCS: Dict[SupportedData, ReadFunc] = { | ||
SupportedData.TIFF: read_tiff, | ||
} | ||
|
||
|
||
def get_read_func(data_type: Union[str, SupportedData]) -> Callable: | ||
""" | ||
Get the read function for the data type. | ||
Parameters | ||
---------- | ||
data_type : SupportedData | ||
Data type. | ||
Returns | ||
------- | ||
callable | ||
Read function. | ||
""" | ||
if data_type in READ_FUNCS: | ||
data_type = SupportedData(data_type) # mypy complaining about dict key type | ||
return READ_FUNCS[data_type] | ||
else: | ||
raise NotImplementedError(f"Data type '{data_type}' is not supported.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Functions relating to writing image files of different formats.""" | ||
|
||
__all__ = [ | ||
"get_write_func", | ||
"write_tiff", | ||
] | ||
|
||
from .get_func import get_write_func | ||
from .tiff import write_tiff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Module to get write functions.""" | ||
|
||
from pathlib import Path | ||
from typing import Protocol, Union | ||
|
||
from numpy.typing import NDArray | ||
|
||
from careamics.config.support import SupportedData | ||
|
||
from .tiff import write_tiff | ||
|
||
|
||
# This is very strict, arguments have to be called file_path & img | ||
# Alternative? - doesn't capture *args & **kwargs | ||
# WriteFunc = Callable[[Path, NDArray], None] | ||
class WriteFunc(Protocol): | ||
"""Protocol for type hinting write functions.""" | ||
|
||
def __call__(self, file_path: Path, img: NDArray, *args, **kwargs) -> None: | ||
""" | ||
Type hinted callables must match this function signature (not including self). | ||
Parameters | ||
---------- | ||
file_path : pathlib.Path | ||
Path to file. | ||
img : numpy.ndarray | ||
Image data to save. | ||
*args | ||
Other positional arguments. | ||
**kwargs | ||
Other keyword arguments. | ||
""" | ||
|
||
|
||
WRITE_FUNCS: dict[SupportedData, WriteFunc] = { | ||
SupportedData.TIFF: write_tiff, | ||
} | ||
|
||
|
||
def get_write_func(data_type: Union[str, SupportedData]) -> WriteFunc: | ||
""" | ||
Get the write function for the data type. | ||
Parameters | ||
---------- | ||
data_type : SupportedData | ||
Data type. | ||
Returns | ||
------- | ||
callable | ||
Write function. | ||
""" | ||
if data_type in WRITE_FUNCS: | ||
data_type = SupportedData(data_type) # mypy complaining about dict key type | ||
return WRITE_FUNCS[data_type] | ||
else: | ||
raise NotImplementedError(f"Data type {data_type} is not supported.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Write tiff function.""" | ||
|
||
from fnmatch import fnmatch | ||
from pathlib import Path | ||
|
||
import tifffile | ||
from numpy.typing import NDArray | ||
|
||
from careamics.config.support import SupportedData | ||
|
||
|
||
def write_tiff(file_path: Path, img: NDArray, *args, **kwargs) -> None: | ||
""" | ||
Write tiff files. | ||
Parameters | ||
---------- | ||
file_path : pathlib.Path | ||
Path to file. | ||
img : numpy.ndarray | ||
Image data to save. | ||
*args | ||
Positional arguments passed to `tifffile.imwrite`. | ||
**kwargs | ||
Keyword arguments passed to `tifffile.imwrite`. | ||
Raises | ||
------ | ||
ValueError | ||
When the file extension of `file_path` does not match the Unix shell-style | ||
pattern '*.tif*'. | ||
""" | ||
if not fnmatch( | ||
file_path.suffix, SupportedData.get_extension_pattern(SupportedData.TIFF) | ||
): | ||
raise ValueError( | ||
f"Unexpected extension '{file_path.suffix}' for save file type 'tiff'." | ||
) | ||
tifffile.imwrite(file_path, img, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.