-
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
feat: add reading skew from czi files #1205
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,13 +51,13 @@ def add_color(image: Image, idx: int) -> dict: # noqa: ARG001 | |
return {} | ||
|
||
|
||
def _image_to_layers(project_info, scale, translate): | ||
def _image_to_layers(project_info, scale, translate, shear): | ||
res_layers = [] | ||
if project_info.image.name == "ROI" and project_info.image.channels == 1: | ||
res_layers.append( | ||
( | ||
project_info.image.get_channel(0), | ||
{"scale": scale, "name": project_info.image.channel_names[0], "translate": translate}, | ||
{"scale": scale, "name": project_info.image.channel_names[0], "translate": translate, "shear": shear}, | ||
"labels", | ||
) | ||
) | ||
|
@@ -71,6 +71,7 @@ def _image_to_layers(project_info, scale, translate): | |
"blending": "additive", | ||
"translate": translate, | ||
"metadata": project_info.image.metadata, | ||
"shear": shear, | ||
**add_color(project_info.image, i), | ||
}, | ||
"image", | ||
|
@@ -85,14 +86,15 @@ def project_to_layers(project_info: typing.Union[ProjectTuple, MaskProjectTuple] | |
res_layers = [] | ||
if project_info.image is not None and not isinstance(project_info.image, str): | ||
scale = project_info.image.normalized_scaling() | ||
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. issue (code-quality): Extract code out into function ( |
||
shear = project_info.image.shear | ||
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. Handle potential When assigning Consider adding a check to handle Apply this diff to handle possible - shear = project_info.image.shear
+ shear = project_info.image.shear if project_info.image.shear is not None else np.zeros((ndim, ndim)) Replace
|
||
translate = project_info.image.shift | ||
translate = (0,) * (len(project_info.image.axis_order.replace("C", "")) - len(translate)) + translate | ||
res_layers.extend(_image_to_layers(project_info, scale, translate)) | ||
res_layers.extend(_image_to_layers(project_info, scale, translate, shear)) | ||
if project_info.roi_info.roi is not None: | ||
res_layers.append( | ||
( | ||
project_info.image.fit_array_to_image(project_info.roi_info.roi), | ||
{"scale": scale, "name": "ROI", "translate": translate}, | ||
{"scale": scale, "name": "ROI", "translate": translate, "shear": shear}, | ||
"labels", | ||
) | ||
) | ||
|
@@ -105,6 +107,7 @@ def project_to_layers(project_info: typing.Union[ProjectTuple, MaskProjectTuple] | |
"name": name, | ||
"translate": translate, | ||
"visible": False, | ||
"shear": shear, | ||
}, | ||
"labels", | ||
) | ||
|
@@ -115,7 +118,7 @@ def project_to_layers(project_info: typing.Union[ProjectTuple, MaskProjectTuple] | |
res_layers.append( | ||
( | ||
project_info.image.fit_array_to_image(project_info.mask), | ||
{"scale": scale, "name": "Mask", "translate": translate}, | ||
{"scale": scale, "name": "Mask", "translate": translate, "shear": shear}, | ||
"labels", | ||
) | ||
) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||
import inspect | ||||||||||||||||||||||||||||||||||
import os.path | ||||||||||||||||||||||||||||||||||
import typing | ||||||||||||||||||||||||||||||||||
import warnings | ||||||||||||||||||||||||||||||||||
from abc import abstractmethod | ||||||||||||||||||||||||||||||||||
from contextlib import suppress | ||||||||||||||||||||||||||||||||||
from importlib.metadata import version | ||||||||||||||||||||||||||||||||||
|
@@ -389,8 +390,36 @@ | |||||||||||||||||||||||||||||||||
axes_order=self.return_order(), | ||||||||||||||||||||||||||||||||||
metadata_dict=metadata, | ||||||||||||||||||||||||||||||||||
channel_info=self._get_channel_info(), | ||||||||||||||||||||||||||||||||||
shear=self._read_shear(metadata), | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def _read_shear(self, metadata: dict): | ||||||||||||||||||||||||||||||||||
skew = self._read_skew(metadata) | ||||||||||||||||||||||||||||||||||
shear = np.diag([1.0] * len(skew)) | ||||||||||||||||||||||||||||||||||
for i, val in enumerate(skew): | ||||||||||||||||||||||||||||||||||
if val == 0: | ||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||
shear[i, i + 1] = np.tan(np.radians(val)) | ||||||||||||||||||||||||||||||||||
return shear | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Comment on lines
+396
to
+404
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. Potential IndexError in In the Apply this diff to prevent the IndexError by adjusting the loop: def _read_shear(self, metadata: dict):
skew = self._read_skew(metadata)
shear = np.diag([1.0] * len(skew))
- for i, val in enumerate(skew):
+ for i, val in enumerate(skew[:-1]):
if val == 0:
continue
shear[i, i + 1] = np.tan(np.radians(val))
return shear Alternatively, add a boundary check within the loop: def _read_shear(self, metadata: dict):
skew = self._read_skew(metadata)
shear = np.diag([1.0] * len(skew))
for i, val in enumerate(skew):
if val == 0:
continue
+ if i + 1 >= len(skew):
+ continue
shear[i, i + 1] = np.tan(np.radians(val))
return shear 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
@staticmethod | ||||||||||||||||||||||||||||||||||
def _read_skew(metadata: dict): | ||||||||||||||||||||||||||||||||||
dimensions = metadata["ImageDocument"]["Metadata"]["Information"]["Image"]["Dimensions"] | ||||||||||||||||||||||||||||||||||
res = [0.0] * 4 | ||||||||||||||||||||||||||||||||||
for i, dim in enumerate("TZYX"): | ||||||||||||||||||||||||||||||||||
if dim not in dimensions: | ||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||
if f"{dim}AxisShear" not in dimensions[dim]: | ||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
shear_value = dimensions[dim][f"{dim}AxisShear"] | ||||||||||||||||||||||||||||||||||
if not shear_value.startswith("Skew"): | ||||||||||||||||||||||||||||||||||
warnings.warn(f"Unknown shear value {shear_value}", stacklevel=1) | ||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||
res[i] = float(shear_value[4:]) | ||||||||||||||||||||||||||||||||||
Comment on lines
+416
to
+419
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. Handle potential Converting Apply this diff to add a try-except block: if not shear_value.startswith("Skew"):
warnings.warn(f"Unknown shear value {shear_value}", stacklevel=2)
continue
+try:
res[i] = float(shear_value[4:])
+except ValueError:
+ warnings.warn(f"Invalid shear value '{shear_value}' cannot be converted to float.", stacklevel=2)
+ res[i] = 0.0
continue 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
return res | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||||||
def update_array_shape(cls, array: np.ndarray, axes: str): | ||||||||||||||||||||||||||||||||||
if "B" in axes: | ||||||||||||||||||||||||||||||||||
|
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.
Ensure 'shear' parameter is compatible with all targeted napari versions
The addition of the
shear
parameter to the_image_to_layers
function enhances functionality. However, it's important to verify that theshear
parameter is supported in all versions of napari that your application targets. Using unsupported parameters could lead to runtime errors in older versions of napari.Consider adding a version check, similar to the one used for the
add_color
function, to conditionally include theshear
parameter based on the napari version.