From 3e5f041e64613d7c998ebeef8f274afdd3ed9baa Mon Sep 17 00:00:00 2001 From: pswain <peter.swain@ed.ac.uk> Date: Thu, 20 Jun 2024 12:08:55 +0100 Subject: [PATCH] feature(imageviewer): dded sample_traps_with_cells --- examples/run_imageviewer.py | 3 ++- src/wela/imageviewer.py | 27 ++++++++++++++++++++++++--- src/wela/plotting.py | 6 +++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/examples/run_imageviewer.py b/examples/run_imageviewer.py index 6afe25e..40ccbdc 100644 --- a/examples/run_imageviewer.py +++ b/examples/run_imageviewer.py @@ -14,12 +14,13 @@ server_info = { h5file = f"{aliby_output}{omero_name}/{position}.h5" iv = ImageViewer.remote(h5file, server_info, 2104) tpt_end = 10 +no_cells = 4 # for information only iv.print_traps_with_cells(tpt_end) # use Napari to view cells # python -m pip install "napari[all]" --upgrade iv.view( - trap_ids=[4, 8, 13], + trap_ids=iv.sample_traps_with_cells(tpt_end=tpt_end, no_cells=no_cells), tpt_end=tpt_end, channels_to_skip=["cy5"], no_vertical_tiles=1, diff --git a/src/wela/imageviewer.py b/src/wela/imageviewer.py index b251a08..629d69e 100644 --- a/src/wela/imageviewer.py +++ b/src/wela/imageviewer.py @@ -1,7 +1,14 @@ from pathlib import Path import h5py -import napari + +try: + import napari +except ModuleNotFoundError: + print( + "Napari cannot be imported.\nRun", + ' python -m pip install "napari[all]"', + ) import numpy as np from agora.io.cells import Cells from aliby.io.image import dispatch_image @@ -62,7 +69,7 @@ class ImageViewer: iv.cells = Cells.from_source(iv.h5file_path) return iv - def print_traps_with_cells(self, tpt_end, tpt_start=0): + def get_all_traps_with_cells(self, tpt_end, tpt_start=0, display=True): """List traps with cells.""" cells = self.cells tpts = range(tpt_start, tpt_end) @@ -72,9 +79,23 @@ class ImageViewer: if tpt in cells.nonempty_tp_in_trap(trap_id): traps_with_cells.append(trap_id) break - print(f"Traps with cells {traps_with_cells}") + if display: + print(f"Traps with cells {traps_with_cells}") return traps_with_cells + def sample_traps_with_cells(self, no_cells, tpt_end, tpt_start=0): + """Sample some traps that have cells.""" + traps_with_cells = self.get_all_traps_with_cells( + tpt_end, tpt_start, display=False + ) + rng = np.random.default_rng() + samples = rng.integers( + low=0, + high=len(traps_with_cells), + size=np.min([no_cells, len(traps_with_cells)]), + ) + return samples + def get_tiles(self, trap_id, tps, channels_to_skip=None, cell_only=True): """Get dict of tiles with channel indices as keys.""" tiles_dict = {} diff --git a/src/wela/plotting.py b/src/wela/plotting.py index 5040d7e..56a412e 100644 --- a/src/wela/plotting.py +++ b/src/wela/plotting.py @@ -496,7 +496,7 @@ def plot2Dhist( -------- Load data: >>> from wela.dataloader import dataloader - >>> from wela.plotting import plothist + >>> from wela.plotting import plot2Dhist >>> dl = dataloader() >>> dl.load("1334_2023_03_28_pyrTo2Gal_01glc_00", use_tsv=True) >>> dlc = dataloader() @@ -507,8 +507,8 @@ def plot2Dhist( >>> tc, dc = dlc.get_time_series("median_GFP") Plot both data sets using axes with the same range: - >>> bins = plothist(t, dc, title="2% Gal", figsize=(4, 3))[0] - >>> plothist(t, d, title="2% Gal and 0.1% Glu", bins=bins, figsize=(4, 3)) + >>> bins = plot2Dhist(t, dc, title="2% Gal", figsize=(4, 3))[0] + >>> plot2Dhist(t, d, title="2% Gal and 0.1% Glu", bins=bins, figsize=(4, 3)) """ if x.ndim == 1: # make into a 2D array -- GitLab