Skip to content

Commit

Permalink
Allow selecy to which units the layers should be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Czaki committed Dec 9, 2024
1 parent 8b332c7 commit 3fa548f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package/PartSeg/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ contributions:
- id: PartSeg.Metadata
title: View Layer Metadata
python_name: PartSeg.plugins.napari_widgets:LayerMetadata
- id: PartSeg.Settings
title: PartSeg Settings
python_name: PartSeg.plugins.napari_widgets:SettingsEditor
readers:
- command: PartSeg.load_roi_project
filename_patterns:
Expand Down Expand Up @@ -162,3 +165,5 @@ contributions:
display_name: Watershed
- command: PartSeg.Metadata
display_name: Layer Metadata
- command: PartSeg.Settings
display_name: Settings Editor
5 changes: 4 additions & 1 deletion package/PartSeg/plugins/napari_io/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import numpy as np
from packaging.version import parse as parse_version

from PartSeg.plugins.napari_widgets._settings import get_settings
from PartSegCore import UNIT_SCALE
from PartSegCore.analysis import ProjectTuple
from PartSegCore.io_utils import LoadBase, WrongFileTypeException
from PartSegCore.mask.io_functions import MaskProjectTuple
Expand Down Expand Up @@ -84,7 +86,8 @@ def _image_to_layers(project_info, scale, translate):
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()
settings = get_settings()
scale = project_info.image.normalized_scaling(UNIT_SCALE[settings.io_units.value])
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))
Expand Down
2 changes: 2 additions & 0 deletions package/PartSeg/plugins/napari_widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from PartSeg._launcher.main_window import PartSegGUILauncher
from PartSeg.plugins.napari_widgets._settings import SettingsEditor
from PartSeg.plugins.napari_widgets.algorithm_widgets import (
BorderSmooth,
ConnectedComponents,
Expand Down Expand Up @@ -33,4 +34,5 @@
"ROIAnalysisExtraction",
"ROIMaskExtraction",
"SearchLabel",
"SettingsEditor",
)
25 changes: 24 additions & 1 deletion package/PartSeg/plugins/napari_widgets/_settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os

from magicgui.widgets import Container, create_widget
from napari.layers import Layer

from PartSeg._roi_analysis.partseg_settings import PartSettings
from PartSeg.common_backend import napari_get_settings
from PartSegCore import Units
from PartSegCore.json_hooks import PartSegEncoder

_SETTINGS = None
Expand All @@ -19,8 +21,16 @@ def default(self, o):
class PartSegNapariSettings(PartSettings):
json_encoder_class = PartSegNapariEncoder

@property
def io_units(self) -> Units:
return self.get("io_units", Units.nm)

def get_settings() -> PartSettings:
@io_units.setter
def io_units(self, value: Units):
self.set("io_units", value)


def get_settings() -> PartSegNapariSettings:
global _SETTINGS # noqa: PLW0603 # pylint: disable=global-statement
if _SETTINGS is None:
napari_settings = napari_get_settings()
Expand All @@ -31,3 +41,16 @@ def get_settings() -> PartSettings:
_SETTINGS = PartSegNapariSettings(os.path.join(save_path, "PartSeg_napari_plugins"))
_SETTINGS.load()
return _SETTINGS


class SettingsEditor(Container):
def __init__(self):
super().__init__()
self.settings = get_settings()
self.units_select = create_widget(self.settings.io_units, annotation=Units, label="Units for io")
self.units_select.changed.connect(self.units_changed)
self.append(self.units_select)

def units_changed(self, value):
self.settings.io_units = value
self.settings.dump()

0 comments on commit 3fa548f

Please sign in to comment.