Skip to content
Snippets Groups Projects
Commit 9ff355a4 authored by pswain's avatar pswain
Browse files

change(imageviewer): one class with classmethods

parent 4dd9423f
No related branches found
No related tags found
No related merge requests found
import typing as t
from abc import ABC
from pathlib import Path from pathlib import Path
import h5py import h5py
...@@ -12,25 +10,57 @@ from aliby.io.omero import UnsafeImage as OImage ...@@ -12,25 +10,57 @@ from aliby.io.omero import UnsafeImage as OImage
from aliby.tile.tiler import Tiler from aliby.tile.tiler import Tiler
def colormap(channel): class ImageViewer:
"""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 __init__(self, h5file):
self.h5file_path = Path(h5file)
print(f"Viewing {str(self.h5file_path)}")
self.full = {}
class BaseImageViewer(ABC): @classmethod
"""Base class for all ImageViewers.""" def local(cls, h5file: str, image_file: str):
"""
View images from local files.
def __init__(self, h5file_path): Files are either zarr or organised in directories.
"""Initialise from a Path to a h5 file.""" """
self.h5file_path = h5file_path iv = cls(h5file)
print(f"Viewing {str(h5file_path)}") image_file_path = Path(image_file)
self.full = {} 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): def find_traps_with_cells(self, tpt_end, tpt_start=0):
"""List traps with cells.""" """List traps with cells."""
...@@ -192,55 +222,66 @@ class BaseImageViewer(ABC): ...@@ -192,55 +222,66 @@ class BaseImageViewer(ABC):
) )
class LocalImageViewer(BaseImageViewer): # class LocalImageViewer(BaseImageViewer):
""" # """
View images from local files. # 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): # def __init__(self, h5file: str, image_file: str):
"""Initialise using a h5file and a zarr file of images.""" # """Initialise using a h5file and a zarr file of images."""
h5file_path = Path(h5file) # h5file_path = Path(h5file)
image_file_path = Path(image_file) # image_file_path = Path(image_file)
if h5file_path.exists() and image_file_path.exists(): # if h5file_path.exists() and image_file_path.exists():
super().__init__(h5file_path) # super().__init__(h5file_path)
with dispatch_image(image_file_path)(image_file_path) as image: # with dispatch_image(image_file_path)(image_file_path) as image:
self.tiler = Tiler.from_h5(image, h5file_path) # self.tiler = Tiler.from_h5(image, h5file_path)
self.cells = Cells.from_source(h5file_path) # self.cells = Cells.from_source(h5file_path)
else: # else:
if not h5file_path.exists(): # if not h5file_path.exists():
print(f" Trouble loading {h5file}.") # print(f" Trouble loading {h5file}.")
if not image_file_path.exists(): # if not image_file_path.exists():
print(f" Trouble loading {image_file}.") # print(f" Trouble loading {image_file}.")
class RemoteImageViewer(BaseImageViewer): # class RemoteImageViewer(BaseImageViewer):
"""View images from OMERO.""" # """View images from OMERO."""
def __init__( # def __init__(
self, h5file: str, server_info: t.Dict[str, str], omero_id: int # self, h5file: str, server_info: t.Dict[str, str], omero_id: int
): # ):
"""Initialise using a h5file and importing aliby.io.omero.""" # """Initialise using a h5file and importing aliby.io.omero."""
h5file_path = Path(h5file) # h5file_path = Path(h5file)
super().__init__(h5file_path) # super().__init__(h5file_path)
with h5py.File(h5file_path, "r") as f: # with h5py.File(h5file_path, "r") as f:
# get image_id from the h5 file # # get image_id from the h5 file
image_id = f.attrs.get("image_id") # image_id = f.attrs.get("image_id")
if image_id is None: # if image_id is None:
# get image_id from OMERO # # get image_id from OMERO
with Dataset(omero_id, **server_info) as dataset_om: # with Dataset(omero_id, **server_info) as dataset_om:
positions = dataset_om.get_position_ids() # positions = dataset_om.get_position_ids()
image_id = positions.get(h5file_path.name.split(".")[0]) # image_id = positions.get(h5file_path.name.split(".")[0])
if image_id is None: # if image_id is None:
print("Can't find an image.") # print("Can't find an image.")
else: # else:
print(f"Using image ID {image_id}.") # print(f"Using image ID {image_id}.")
self.image_id = image_id # self.image_id = image_id
image = OImage(image_id, **server_info) # image = OImage(image_id, **server_info)
print("Connected to OMERO.") # print("Connected to OMERO.")
self.tiler = Tiler.from_h5(image, h5file_path) # self.tiler = Tiler.from_h5(image, h5file_path)
self.cells = Cells.from_source(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( def get_files(
......
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