Skip to content
Snippets Groups Projects
Commit 1ebd37bd authored by Alán Muñoz's avatar Alán Muñoz
Browse files

tweak(meta): Create ABC for ImageLocal

parent d9d9c00e
No related branches found
No related tags found
No related merge requests found
...@@ -47,9 +47,22 @@ def get_image_class(source: t.Union[str, int, t.Dict[str, str], PosixPath]): ...@@ -47,9 +47,22 @@ def get_image_class(source: t.Union[str, int, t.Dict[str, str], PosixPath]):
return instatiator return instatiator
class ImageLocal: class BaseLocalImage:
"""
Base class to set path and provide context management method.
"""
def __init__(self, path: t.Union[str, PosixPath]):
# If directory, assume contents are naturally sorted
self.path = Path(path)
def __enter__(self):
return self
class ImageLocal(BaseLocalImage):
def __init__(self, path: str, dimorder=None): def __init__(self, path: str, dimorder=None):
self.path = path super().__init__(path)
self._id = str(path) self._id = str(path)
meta = dict() meta = dict()
...@@ -154,16 +167,26 @@ class ImageLocal: ...@@ -154,16 +167,26 @@ class ImageLocal:
reshaped, range(len(reshaped.shape)), target_order reshaped, range(len(reshaped.shape)), target_order
) )
self._formatted_img = da.rechunk( return self.format_data(img)
img,
chunks=(1, 1, 1, self._meta["size_y"], self._meta["size_x"]), def format_data(self, img):
)
self._formatted_img = da.rechunk(
img,
chunks=(
1,
1,
1,
*[self._meta[f"size_{n}"] for n in self.dimorder[-2:]],
),
)
return self._formatted_img return self._formatted_img
class ImageDir(ImageLocal): class ImageDir(ImageLocal):
""" """
Image class for case where all images are split in one or multiple folders with time-points and channels as independent files. Image class for the case in which all images are split in one or
multiple folders with time-points and channels as independent files.
It inherits from Imagelocal so we only override methods that are critical. It inherits from Imagelocal so we only override methods that are critical.
Assumptions: Assumptions:
...@@ -174,9 +197,7 @@ class ImageDir(ImageLocal): ...@@ -174,9 +197,7 @@ class ImageDir(ImageLocal):
""" """
def __init__(self, path: t.Union[str, PosixPath]): def __init__(self, path: t.Union[str, PosixPath]):
super().__init__(path)
# Assume they are naturally sorted
self.path = Path(path)
self.image_id = str(self.path.stem) self.image_id = str(self.path.stem)
filenames = list(self.path.glob("*.tiff")) filenames = list(self.path.glob("*.tiff"))
...@@ -220,13 +241,8 @@ class ImageDir(ImageLocal): ...@@ -220,13 +241,8 @@ class ImageDir(ImageLocal):
img = img[..., 0] img = img[..., 0]
self._meta["size_x"], self._meta["size_y"] = img.shape[-2:] self._meta["size_x"], self._meta["size_y"] = img.shape[-2:]
reshaped = da.reshape(img, (*self._dim_shapes, *img.shape[1:])) img = da.reshape(img, (*self._dim_shapes, *img.shape[1:]))
return self.format_data(img)
self._formatted_img = da.rechunk(
reshaped,
chunks=(1, 1, 1, self._meta["size_y"], self._meta["size_x"]),
)
return self._formatted_img
class Image(BridgeOmero): class Image(BridgeOmero):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment