diff --git a/src/wela/imageviewer.py b/src/wela/imageviewer.py index 84be7cf4a31926a5f94cbbe94c9d42cddc1c6b53..75f5363fed233fc4da0580ff8b6e8015594b6f46 100644 --- a/src/wela/imageviewer.py +++ b/src/wela/imageviewer.py @@ -1,5 +1,3 @@ -import typing as t -from abc import ABC from pathlib import Path import h5py @@ -12,25 +10,57 @@ from aliby.io.omero import UnsafeImage as OImage from aliby.tile.tiler import Tiler -def colormap(channel): - """Find default colormap.""" - if "GFP" in channel: - colormap = "green" - elif "Cherry" in channel or "RFP" in channel: - colormap = "red" - else: - colormap = "gray" - return colormap +class ImageViewer: + def __init__(self, h5file): + self.h5file_path = Path(h5file) + print(f"Viewing {str(self.h5file_path)}") + self.full = {} -class BaseImageViewer(ABC): - """Base class for all ImageViewers.""" + @classmethod + def local(cls, h5file: str, image_file: str): + """ + View images from local files. - def __init__(self, h5file_path): - """Initialise from a Path to a h5 file.""" - self.h5file_path = h5file_path - print(f"Viewing {str(h5file_path)}") - self.full = {} + Files are either zarr or organised in directories. + """ + iv = cls(h5file) + image_file_path = Path(image_file) + if iv.h5file_path.exists() and image_file_path.exists(): + with dispatch_image(image_file_path)(image_file_path) as image: + tiler = Tiler.from_h5(image, iv.h5file_path) + cells = Cells.from_source(iv.h5file_path) + iv.tiler = tiler + iv.cells = cells + return iv + else: + if not iv.h5file_path.exists(): + print(f" Trouble loading {h5file}.") + if not image_file_path.exists(): + print(f" Trouble loading {image_file}.") + + @classmethod + def remote(cls, h5file: str, server_info: dict, omero_id: int): + """View images from OMERO.""" + iv = cls(h5file) + with h5py.File(iv.h5file_path, "r") as f: + # get image_id from the h5 file + image_id = f.attrs.get("image_id") + if image_id is None: + # get image_id from OMERO + with Dataset(omero_id, **server_info) as dataset_om: + positions = dataset_om.get_position_ids() + image_id = positions.get(iv.h5file_path.name.split(".")[0]) + if image_id is None: + print("Can't find an image.") + else: + print(f"Using image ID {image_id}.") + iv.image_id = image_id + image = OImage(image_id, **server_info) + print("Connected to OMERO.") + iv.tiler = Tiler.from_h5(image, iv.h5file_path) + iv.cells = Cells.from_source(iv.h5file_path) + return iv def find_traps_with_cells(self, tpt_end, tpt_start=0): """List traps with cells.""" @@ -192,55 +222,66 @@ class BaseImageViewer(ABC): ) -class LocalImageViewer(BaseImageViewer): - """ - View images from local files. +# class LocalImageViewer(BaseImageViewer): +# """ +# View images from local files. - Files are either zarr or organised in directories. - """ +# Files are either zarr or organised in directories. +# """ - def __init__(self, h5file: str, image_file: str): - """Initialise using a h5file and a zarr file of images.""" - h5file_path = Path(h5file) - image_file_path = Path(image_file) - if h5file_path.exists() and image_file_path.exists(): - super().__init__(h5file_path) - with dispatch_image(image_file_path)(image_file_path) as image: - self.tiler = Tiler.from_h5(image, h5file_path) - self.cells = Cells.from_source(h5file_path) - else: - if not h5file_path.exists(): - print(f" Trouble loading {h5file}.") - if not image_file_path.exists(): - print(f" Trouble loading {image_file}.") +# def __init__(self, h5file: str, image_file: str): +# """Initialise using a h5file and a zarr file of images.""" +# h5file_path = Path(h5file) +# image_file_path = Path(image_file) +# if h5file_path.exists() and image_file_path.exists(): +# super().__init__(h5file_path) +# with dispatch_image(image_file_path)(image_file_path) as image: +# self.tiler = Tiler.from_h5(image, h5file_path) +# self.cells = Cells.from_source(h5file_path) +# else: +# if not h5file_path.exists(): +# print(f" Trouble loading {h5file}.") +# if not image_file_path.exists(): +# print(f" Trouble loading {image_file}.") -class RemoteImageViewer(BaseImageViewer): - """View images from OMERO.""" +# class RemoteImageViewer(BaseImageViewer): +# """View images from OMERO.""" - def __init__( - self, h5file: str, server_info: t.Dict[str, str], omero_id: int - ): - """Initialise using a h5file and importing aliby.io.omero.""" - h5file_path = Path(h5file) - super().__init__(h5file_path) - with h5py.File(h5file_path, "r") as f: - # get image_id from the h5 file - image_id = f.attrs.get("image_id") - if image_id is None: - # get image_id from OMERO - with Dataset(omero_id, **server_info) as dataset_om: - positions = dataset_om.get_position_ids() - image_id = positions.get(h5file_path.name.split(".")[0]) - if image_id is None: - print("Can't find an image.") - else: - print(f"Using image ID {image_id}.") - self.image_id = image_id - image = OImage(image_id, **server_info) - print("Connected to OMERO.") - self.tiler = Tiler.from_h5(image, h5file_path) - self.cells = Cells.from_source(h5file_path) +# def __init__( +# self, h5file: str, server_info: t.Dict[str, str], omero_id: int +# ): +# """Initialise using a h5file and importing aliby.io.omero.""" +# h5file_path = Path(h5file) +# super().__init__(h5file_path) +# with h5py.File(h5file_path, "r") as f: +# # get image_id from the h5 file +# image_id = f.attrs.get("image_id") +# if image_id is None: +# # get image_id from OMERO +# with Dataset(omero_id, **server_info) as dataset_om: +# positions = dataset_om.get_position_ids() +# image_id = positions.get(h5file_path.name.split(".")[0]) +# if image_id is None: +# print("Can't find an image.") +# else: +# print(f"Using image ID {image_id}.") +# self.image_id = image_id +# image = OImage(image_id, **server_info) +# print("Connected to OMERO.") +# self.tiler = Tiler.from_h5(image, h5file_path) +# self.cells = Cells.from_source(h5file_path) + + +def colormap(channel): + """Find default colormap.""" + if "GFP" in channel: + colormap = "green" + elif "Cherry" in channel or "RFP" in channel: + colormap = "red" + else: + colormap = "gray" + return colormap def get_files(