-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
load_image_wrapper returns 5D data with channel_axis:1 #14
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from typing import List | ||
from typing import List, Dict | ||
|
||
import dask.array as da | ||
|
||
|
@@ -7,7 +7,7 @@ | |
|
||
from napari.types import LayerData | ||
from omero.cli import ProxyStringType | ||
from omero.gateway import BlitzGateway, ImageWrapper, ChannelWrapper | ||
from omero.gateway import BlitzGateway, ImageWrapper | ||
from omero.model import IObject | ||
|
||
from ..utils import parse_omero_url, timer, lookup_obj, PIXEL_TYPES | ||
|
@@ -65,79 +65,68 @@ def omero_proxy_reader( | |
|
||
|
||
def load_image_wrapper(image: ImageWrapper) -> List[LayerData]: | ||
return [ | ||
load_omero_channel(image, channel, c) | ||
for c, channel in enumerate(image.getChannels()) | ||
data = get_data_lazy(image) | ||
meta = get_omero_metadata(image) | ||
# contrast limits range ... not accessible from plugin interface | ||
# win_min = channel.getWindowMin() | ||
# win_max = channel.getWindowMax() | ||
return [(data, meta)] | ||
|
||
|
||
def get_omero_metadata(image: ImageWrapper) -> Dict: | ||
"""Get metadata from OMERO as a Dict to pass to napari.""" | ||
channels = image.getChannels() | ||
|
||
colors = [] | ||
for ch in channels: | ||
# use current rendering settings from OMERO | ||
color = ch.getColor().getRGB() | ||
color = [r / 256 for r in color] | ||
colors.append(Colormap([[0, 0, 0], color])) | ||
|
||
contrast_limits = [ | ||
[ch.getWindowStart(), ch.getWindowEnd()] for ch in channels | ||
] | ||
|
||
visibles = [ch.isActive() for ch in channels] | ||
names = [ch.getLabel() for ch in channels] | ||
|
||
def load_omero_channel( | ||
image: ImageWrapper, channel: ChannelWrapper, c_index: int | ||
) -> LayerData: | ||
data = get_data_lazy(image, c_index=c_index) | ||
color = channel.getColor().getRGB() | ||
color = [r / 256 for r in color] | ||
cmap = Colormap([[0, 0, 0], color]) | ||
scale = None | ||
|
||
# FIXME: still getting size mismatch sometimes is there a getNDim()? | ||
if image.getSizeZ() > 1: | ||
size_x = image.getPixelSizeX() | ||
size_z = image.getPixelSizeZ() | ||
if size_x is not None and size_z is not None: | ||
if image.getSizeT() > 1: | ||
scale = [1, size_z / size_x, 1, 1] | ||
else: | ||
scale = [size_z / size_x, 1, 1] | ||
|
||
meta = { | ||
"blending": "additive", | ||
"colormap": ("from_omero", cmap), | ||
"scale": scale, | ||
"name": channel.getLabel(), | ||
"visible": channel.isActive(), | ||
"contrast_limits": [channel.getWindowStart(), channel.getWindowEnd()], | ||
scale = [1, size_z / size_x, 1, 1] | ||
|
||
return { | ||
'channel_axis': 1, | ||
'colormap': colors, | ||
'contrast_limits': contrast_limits, | ||
'name': names, | ||
'visible': visibles, | ||
'scale': scale, | ||
} | ||
# contrast limits range ... not accessible from plugin interface | ||
# win_min = channel.getWindowMin() | ||
# win_max = channel.getWindowMax() | ||
return (data, meta) | ||
|
||
|
||
@timer | ||
def get_data_lazy(image: ImageWrapper, c_index: int = 0) -> da.Array: | ||
"""Get n-dimensional dask array, with delayed reading from OMERO image.""" | ||
size_z = image.getSizeZ() | ||
size_t = image.getSizeT() | ||
size_x = image.getSizeX() | ||
size_y = image.getSizeY() | ||
def get_data_lazy(image: ImageWrapper) -> da.Array: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. proposal for trimming this down a bit? @timer
def get_data_lazy(image: ImageWrapper) -> da.Array:
"""Get 5D dask array, with delayed reading from OMERO image."""
nt, nc, nz, ny, nx = [getattr(image, f'getSize{x}')() for x in 'TCZYX']
pixels = image.getPrimaryPixels()
dtype = PIXEL_TYPES.get(pixels.getPixelsType().value, None)
get_plane = delayed(timer(lambda idx: pixels.getPlane(*idx)))
def get_lazy_plane(zct):
return da.from_delayed(get_plane(zct), shape=(ny, nx), dtype=dtype)
# 5D stack: TCZXY
t_stacks = []
for t in range(nt):
c_stacks = []
for c in range(nc):
z_stack = []
for z in range(nz):
z_stack.append(get_lazy_plane((z, c, t)))
c_stacks.append(da.stack(z_stack))
t_stacks.append(da.stack(c_stacks))
return da.stack(t_stacks) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will continue this discussion on ome/omero-py#227 |
||
"""Get 5D dask array, with delayed reading from OMERO image.""" | ||
nt, nc, nz, ny, nx = [getattr(image, f'getSize{x}')() for x in 'TCZYX'] | ||
pixels = image.getPrimaryPixels() | ||
|
||
@delayed | ||
@timer | ||
def get_plane(plane_name): | ||
z, c, t = [int(n) for n in plane_name.split(",")] | ||
p = pixels.getPlane(z, c, t) | ||
return p | ||
|
||
dtype = PIXEL_TYPES.get(pixels.getPixelsType().value, None) | ||
|
||
plane_names = [ | ||
f"{z},{c_index},{t}" for t in range(size_t) for z in range(size_z) | ||
] | ||
lazy_arrays = [get_plane(pn) for pn in plane_names] | ||
dask_arrays = [ | ||
da.from_delayed(delayed_reader, shape=(size_y, size_x), dtype=dtype) | ||
for delayed_reader in lazy_arrays | ||
] | ||
# Stack into one large dask.array | ||
if size_z == 1 or size_t == 1: | ||
return da.stack(dask_arrays, axis=0) | ||
|
||
z_stacks = [] | ||
for t in range(size_t): | ||
z_stacks.append( | ||
da.stack(dask_arrays[t * size_z : (t + 1) * size_z], axis=0) | ||
) | ||
stack = da.stack(z_stacks, axis=0) | ||
return stack | ||
get_plane = delayed(timer(lambda idx: pixels.getPlane(*idx))) | ||
|
||
def get_lazy_plane(zct): | ||
return da.from_delayed(get_plane(zct), shape=(ny, nx), dtype=dtype) | ||
|
||
# 5D stack: TCZXY | ||
t_stacks = [] | ||
for t in range(nt): | ||
c_stacks = [] | ||
for c in range(nc): | ||
z_stack = [] | ||
for z in range(nz): | ||
z_stack.append(get_lazy_plane((z, c, t))) | ||
c_stacks.append(da.stack(z_stack)) | ||
t_stacks.append(da.stack(c_stacks)) | ||
return da.stack(t_stacks) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while we're at it, we might want to add something to the
{'metadata': ... }
field (in the met dict). That's a place where devs can put anything they want... we could drop the actual wrapper object there? or perhaps just the proxy string like "Image:1"?Ultimately, I'm thinking down the road about saving ROIs and stuff back to the server. That's easy when we know we only have a single image open... but if we eventually allow multiple images to be open, we'll need to know which image the ROI corresponds to, and retaining some of the OMERO metadata will make it easier to know what our "options" are. thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #16 so we don't have to decide before merging this