Skip to content

Commit

Permalink
refactor: merge all channel-specific attributes of the Image class (#…
Browse files Browse the repository at this point in the history
…1191)

Also make all except `data` arguments keyword only

<!-- Generated by sourcery-ai[bot]: start summary -->

## Summary by Sourcery

Refactor the Image class to consolidate channel-specific attributes into
a unified channel_info attribute, enforce keyword-only arguments, and
update tests accordingly.

Enhancements:
- Refactor the Image class to merge all channel-specific attributes into
a single channel_info attribute, improving the organization and
management of channel metadata.
- Introduce decorators to handle deprecated arguments and enforce
keyword-only arguments for the Image class, enhancing code clarity and
future-proofing.
- Add utility functions for color conversion from hex and named colors
to RGB, supporting more flexible color specifications.

Tests:
- Update tests to accommodate the refactored Image class, ensuring
compatibility with the new channel_info attribute and keyword-only
arguments.

<!-- Generated by sourcery-ai[bot]: end summary -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Release Notes

- **New Features**
- Introduced `ChannelInfo` and `ChannelInfoFull` classes for enhanced
channel metadata management.
- Updated image processing to use `spacing` instead of `image_spacing`,
improving parameter clarity.

- **Bug Fixes**
- Corrected parameter names across various tests and functionalities to
align with the updated `Image` class interface.

- **Tests**
- Updated numerous test cases to reflect changes in parameter names and
structures, ensuring consistency throughout the test suite.
- Added a new test for saving color images with specific channel
information.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 22, 2024
1 parent 2f6c259 commit c417134
Show file tree
Hide file tree
Showing 23 changed files with 567 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _calculate(self, event=None):
)
return
data_scale = data_layer.scale[-3:] / UNIT_SCALE[self.scale_units_select.get_value().value]
image = Image(data_layer.data, data_scale, axes_order="TZYX"[-data_ndim:])
image = Image(data_layer.data, spacing=data_scale, axes_order="TZYX"[-data_ndim:])
worker = _prepare_data(profile, image, self.labels_choice.value.data)
worker.returned.connect(self._calculate_next)
worker.errored.connect(self._finished)
Expand Down
6 changes: 3 additions & 3 deletions package/PartSeg/plugins/napari_widgets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from PartSeg.common_gui.custom_save_dialog import FormDialog
from PartSegCore import UNIT_SCALE, Units
from PartSegCore.algorithm_describe_base import AlgorithmProperty
from PartSegImage import Channel, Image
from PartSegImage import Channel, ChannelInfo, Image


class QtNapariAlgorithmProperty(QtAlgorithmProperty):
Expand Down Expand Up @@ -69,9 +69,9 @@ def generate_image(viewer: Viewer, *layer_names):
image_list.append(
Image(
image_layer.data,
data_scale,
spacing=data_scale,
axes_order=axis_order[-image_layer.data.ndim :],
channel_names=[image_layer.name],
channel_info=[ChannelInfo(name=name.value)],
)
)
res_image = image_list[0]
Expand Down
4 changes: 2 additions & 2 deletions package/PartSegCore/analysis/measurement_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def get_global_parameters(self):
res = [name]
iterator = iter(self._data_dict.keys())
with suppress(StopIteration):
next(iterator) # skipcq: PTC-W0063`
next(iterator) # skipcq: PTC-W0063
else:
res = []
iterator = iter(self._data_dict.keys())
Expand All @@ -233,7 +233,7 @@ def _prepare_res_iterator(self, counts):
res = [[name] for _ in range(counts)]
iterator = iter(self._data_dict.keys())
with suppress(StopIteration):
next(iterator) # skipcq: PTC-W0063`
next(iterator) # skipcq: PTC-W0063
else:
res = [[] for _ in range(counts)]
iterator = iter(self._data_dict.keys())
Expand Down
4 changes: 2 additions & 2 deletions package/PartSegCore/image_transforming/image_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def transform(
return (
image.__class__(
data=new_channels,
image_spacing=tuple(spacing),
channel_names=image.channel_names,
spacing=tuple(spacing),
channel_info=image.channel_info,
mask=new_mask,
axes_order=image.axis_order,
),
Expand Down
2 changes: 1 addition & 1 deletion package/PartSegCore/mask/io_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _save_mask_roi(project: MaskProjectTuple, tar_file: tarfile.TarFile, paramet
spacing = project.image.spacing
else:
spacing = parameters.spacing
segmentation_image = Image(project.roi_info.roi, spacing, axes_order=Image.axis_order.replace("C", ""))
segmentation_image = Image(project.roi_info.roi, spacing=spacing, axes_order=Image.axis_order.replace("C", ""))
try:
ImageWriter.save(segmentation_image, segmentation_buff, compression=None)
except ValueError:
Expand Down
10 changes: 5 additions & 5 deletions package/PartSegCore/napari_plugins/save_tiff_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from napari.types import FullLayerData

from PartSegImage import Image, ImageWriter
from PartSegImage import ChannelInfo, Image, ImageWriter
from PartSegImage.image import DEFAULT_SCALE_FACTOR


Expand All @@ -15,9 +15,9 @@ def napari_write_labels(path: str, data: Any, meta: dict) -> Optional[str]:
scale_shift = min(data.ndim, 3)
image = Image(
data,
np.divide(meta["scale"], DEFAULT_SCALE_FACTOR)[-scale_shift:],
spacing=np.divide(meta["scale"], DEFAULT_SCALE_FACTOR)[-scale_shift:],
axes_order="TZYX"[-data.ndim :],
channel_names=[meta["name"]],
channel_info=[ChannelInfo(name=meta["name"])],
shift=np.divide(meta["translate"], DEFAULT_SCALE_FACTOR)[-scale_shift:],
name="ROI",
)
Expand Down Expand Up @@ -50,9 +50,9 @@ def napari_write_images(path: str, layer_data: List[FullLayerData]) -> List[str]
scale_shift -= 1
image = Image(
data,
np.divide(meta["scale"], DEFAULT_SCALE_FACTOR)[-scale_shift:],
spacing=np.divide(meta["scale"], DEFAULT_SCALE_FACTOR)[-scale_shift:],
axes_order=axes,
channel_names=channel_names,
channel_info=[ChannelInfo(name=x) for x in channel_names],
shift=np.divide(meta["translate"], DEFAULT_SCALE_FACTOR)[-scale_shift:],
name="Image",
)
Expand Down
4 changes: 3 additions & 1 deletion package/PartSegImage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from PartSegImage import tifffile_fixes # noqa: F401
from PartSegImage.channel_class import Channel
from PartSegImage.image import Image
from PartSegImage.image import ChannelInfo, ChannelInfoFull, Image
from PartSegImage.image_reader import (
CziImageReader,
GenericImageReader,
Expand All @@ -17,6 +17,8 @@
__all__ = (
"BaseImageWriter",
"Channel",
"ChannelInfo",
"ChannelInfoFull",
"Image",
"TiffImageReader",
"IMAGEJImageWriter",
Expand Down
Loading

0 comments on commit c417134

Please sign in to comment.