From c48848838cced3ebab0273806b70ece82fba461d Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Mon, 9 Oct 2023 16:42:35 +0200 Subject: [PATCH] Focus 3d patches now brought into workflow parameters --- extensions/chaeo/products.py | 24 +++++++++++++++--------- extensions/chaeo/workflows.py | 28 ++++++++++++---------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/extensions/chaeo/products.py b/extensions/chaeo/products.py index 98a087e8..0595e6a3 100644 --- a/extensions/chaeo/products.py +++ b/extensions/chaeo/products.py @@ -13,6 +13,16 @@ from extensions.chaeo.annotators import draw_box_on_patch from model_server.accessors import GenericImageDataAccessor from model_server.process import pad, rescale, resample_to_8bit +def _focus_metrics(): + return { + 'max_intensity': lambda x: np.max(x), + 'stdev': lambda x: np.std(x), + 'max_sobel': lambda x: np.max(sobel(x)), + 'rms_sobel': lambda x: sqrt(np.mean(sobel(x) ** 2)), + 'entropy': lambda x: shannon_entropy(x), + 'moment': lambda x: moment(x.flatten(), moment=2), + } + def _write_patch_to_file(where, fname, data): ext = fname.split('.')[-1].upper() where.mkdir(parents=True, exist_ok=True) @@ -41,6 +51,7 @@ def export_patches_from_zstack( pad_to: int = 256, make_3d: bool = False, prefix='patch', + focus_metric: str = None, **kwargs ): assert stack.chroma == 1, 'Expecting monochromatic image data' @@ -65,7 +76,9 @@ def export_patches_from_zstack( patch = patch3d # make a 2d patch, find optimal z-position determined by focus_metric function - elif foc := kwargs.get('focus_metric'): + elif focus_metric is not None: + foc = _focus_metrics()[focus_metric] + sp_sl = np.s_[y0: y1, x0: x1, :, :] subpatch = patch3d[sp_sl] @@ -125,14 +138,7 @@ def export_3d_patches_with_focus_metrics( def get_zstack_focus_metrics(zs): nz = zs.shape[3] - me = { - 'max_intensity': lambda x: np.max(x), - 'stdev': lambda x: np.std(x), - 'max_sobel': lambda x: np.max(sobel(x)), - 'rms_sobel': lambda x: sqrt(np.mean(sobel(x) ** 2)), - 'entropy': lambda x: shannon_entropy(x), - 'moment': lambda x: moment(x.flatten(), moment=2), - } + me = _focus_metrics() dd = {} for zi in range(0, nz): spf = zs[:, :, :, zi] diff --git a/extensions/chaeo/workflows.py b/extensions/chaeo/workflows.py index b0d6287a..50b9ec52 100644 --- a/extensions/chaeo/workflows.py +++ b/extensions/chaeo/workflows.py @@ -60,33 +60,29 @@ def export_patches_from_multichannel_zstack( zmask_acc = InMemoryDataAccessor(zmask) ti.click('generate_zmasks') - # export patches - import numpy as np - from skimage.filters import gaussian, sobel - - # def zs_projector(zs): - # sigma = 1.5 - # blur = gaussian(sobel(zs), sigma) - # argmax = np.argmax(blur, axis=3, keepdims=True) - # return np.take_along_axis(zs, argmax, axis=3) - # - # def zs_annotate_best_focus(zs): - # pass - files = export_patches_from_zstack( - # Path(where_output) / '2d_patches', Path(where_output) / '3d_patches', stack.get_one_channel_data(patches_channel), zmask_meta, prefix=fstem, - # draw_bounding_box=True, draw_bounding_box=False, rescale_clip=0.0, - # projector=zs_projector, make_3d=True, ) ti.click('export_patches') + files = export_patches_from_zstack( + Path(where_output) / '2d_patches', + stack.get_one_channel_data(patches_channel), + zmask_meta, + prefix=fstem, + draw_bounding_box=False, + rescale_clip=0.0, + make_3d=False, + focus_metric='max_sobel', + ) + ti.click('export_patches') + # export annotated zstack annotated = InMemoryDataAccessor( draw_boxes_on_3d_image( -- GitLab