Skip to content
Snippets Groups Projects
Commit c4884883 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Focus 3d patches now brought into workflow parameters

parent 22210950
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,16 @@ from extensions.chaeo.annotators import draw_box_on_patch ...@@ -13,6 +13,16 @@ from extensions.chaeo.annotators import draw_box_on_patch
from model_server.accessors import GenericImageDataAccessor from model_server.accessors import GenericImageDataAccessor
from model_server.process import pad, rescale, resample_to_8bit 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): def _write_patch_to_file(where, fname, data):
ext = fname.split('.')[-1].upper() ext = fname.split('.')[-1].upper()
where.mkdir(parents=True, exist_ok=True) where.mkdir(parents=True, exist_ok=True)
...@@ -41,6 +51,7 @@ def export_patches_from_zstack( ...@@ -41,6 +51,7 @@ def export_patches_from_zstack(
pad_to: int = 256, pad_to: int = 256,
make_3d: bool = False, make_3d: bool = False,
prefix='patch', prefix='patch',
focus_metric: str = None,
**kwargs **kwargs
): ):
assert stack.chroma == 1, 'Expecting monochromatic image data' assert stack.chroma == 1, 'Expecting monochromatic image data'
...@@ -65,7 +76,9 @@ def export_patches_from_zstack( ...@@ -65,7 +76,9 @@ def export_patches_from_zstack(
patch = patch3d patch = patch3d
# make a 2d patch, find optimal z-position determined by focus_metric function # 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, :, :] sp_sl = np.s_[y0: y1, x0: x1, :, :]
subpatch = patch3d[sp_sl] subpatch = patch3d[sp_sl]
...@@ -125,14 +138,7 @@ def export_3d_patches_with_focus_metrics( ...@@ -125,14 +138,7 @@ def export_3d_patches_with_focus_metrics(
def get_zstack_focus_metrics(zs): def get_zstack_focus_metrics(zs):
nz = zs.shape[3] nz = zs.shape[3]
me = { me = _focus_metrics()
'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),
}
dd = {} dd = {}
for zi in range(0, nz): for zi in range(0, nz):
spf = zs[:, :, :, zi] spf = zs[:, :, :, zi]
......
...@@ -60,33 +60,29 @@ def export_patches_from_multichannel_zstack( ...@@ -60,33 +60,29 @@ def export_patches_from_multichannel_zstack(
zmask_acc = InMemoryDataAccessor(zmask) zmask_acc = InMemoryDataAccessor(zmask)
ti.click('generate_zmasks') 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( files = export_patches_from_zstack(
# Path(where_output) / '2d_patches',
Path(where_output) / '3d_patches', Path(where_output) / '3d_patches',
stack.get_one_channel_data(patches_channel), stack.get_one_channel_data(patches_channel),
zmask_meta, zmask_meta,
prefix=fstem, prefix=fstem,
# draw_bounding_box=True,
draw_bounding_box=False, draw_bounding_box=False,
rescale_clip=0.0, rescale_clip=0.0,
# projector=zs_projector,
make_3d=True, make_3d=True,
) )
ti.click('export_patches') 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 # export annotated zstack
annotated = InMemoryDataAccessor( annotated = InMemoryDataAccessor(
draw_boxes_on_3d_image( draw_boxes_on_3d_image(
......
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