Skip to content
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

fix: Fix napari 0.5.0 compatybility #1116

Merged
merged 7 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion package/PartSeg/_roi_mask/image_view.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from importlib.metadata import version

from packaging.version import parse as parse_version
from vispy.app import MouseEvent

from PartSeg._roi_mask.stack_settings import StackSettings
from PartSeg.common_gui.channel_control import ChannelProperty
from PartSeg.common_gui.napari_image_view import ImageInfo, ImageView, LabelEnum

_napari_ge_0_5_0 = parse_version(version("napari")) >= parse_version("0.5.0a1")


class StackImageView(ImageView):
"""
Expand All @@ -14,7 +19,10 @@ class StackImageView(ImageView):

def __init__(self, settings: StackSettings, channel_property: ChannelProperty, name: str):
super().__init__(settings, channel_property, name)
self.viewer_widget.canvas.events.mouse_press.connect(self.component_click)
if _napari_ge_0_5_0:
self.viewer_widget.canvas._scene_canvas.events.mouse_press.connect(self.component_click)
else:
self.viewer_widget.canvas.events.mouse_press.connect(self.component_click)

def refresh_selected(self):
if (
Expand Down
5 changes: 5 additions & 0 deletions package/PartSeg/common_backend/base_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from napari.utils import Colormap
from napari.utils.theme import get_theme
from napari.utils.theme import template as napari_template
from packaging.version import parse as parse_version
from qtpy.QtCore import QObject, Signal
from qtpy.QtWidgets import QMessageBox, QWidget

Expand All @@ -37,6 +38,8 @@
from napari.settings import NapariSettings
logger = logging.getLogger(__name__)

_napari_ge_5 = parse_version(napari.__version__) >= parse_version("0.5.0a1")

DIR_HISTORY = "io.dir_location_history"
FILE_HISTORY = "io.files_open_history"
MULTIPLE_FILES_OPEN_HISTORY = "io.multiple_files_open_history"
Expand Down Expand Up @@ -268,6 +271,8 @@ def theme_name(self) -> str:
@property
def theme(self):
"""Theme as structure."""
if _napari_ge_5:
return get_theme(self.theme_name)
try:
return get_theme(self.theme_name, as_dict=False)
except TypeError: # pragma: no cover
Expand Down
8 changes: 7 additions & 1 deletion package/PartSeg/common_gui/error_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import traceback
import typing
from contextlib import suppress
from importlib.metadata import version

import numpy as np
import requests
import sentry_sdk
from napari.settings import get_settings
from napari.utils.theme import get_theme
from packaging.version import parse as parse_version
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import (
QApplication,
Expand Down Expand Up @@ -53,6 +55,7 @@
_FEEDBACK_URL = "https://sentry.io/api/0/projects/{organization_slug}/{project_slug}/user-feedback/".format(
organization_slug="cent", project_slug="partseg"
)
_napari_ge_5 = parse_version(version("napari")) >= parse_version("0.5.0a1")


def _print_traceback(exception, file_):
Expand Down Expand Up @@ -82,7 +85,10 @@ def __init__(self, exception: Exception, description: str, additional_notes: str
self.create_issue_btn = QPushButton("Create issue")
self.cancel_btn = QPushButton("Cancel")
self.error_description = QTextEdit()
theme = get_theme(get_settings().appearance.theme, as_dict=False)
if _napari_ge_5:
theme = get_theme(get_settings().appearance.theme)
else:
theme = get_theme(get_settings().appearance.theme, as_dict=False)
self._highlight = Pylighter(self.error_description.document(), "python", theme.syntax_style)
self.traceback_summary = additional_info
if additional_info is None:
Expand Down
11 changes: 9 additions & 2 deletions package/PartSeg/common_gui/napari_image_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from napari.layers.labels import Labels
from napari.qt import QtViewer
from napari.qt.threading import thread_worker
from napari.utils.colormaps.colormap import ColormapInterpolationMode
from napari.utils.colormaps.colormap import ColormapInterpolationMode, DirectLabelColormap
from packaging.version import parse as parse_version
from qtpy.QtCore import QEvent, QPoint, Qt, QTimer, Signal, Slot
from qtpy.QtWidgets import QApplication, QCheckBox, QHBoxLayout, QLabel, QMenu, QSpinBox, QToolTip, QVBoxLayout, QWidget
Expand All @@ -42,6 +42,7 @@
from napari._qt.widgets.qt_viewer_buttons import QtViewerPushButton as QtViewerPushButton_
_napari_ge_4_13 = parse_version(napari.__version__) >= parse_version("0.4.13a1")
_napari_ge_4_17 = parse_version(napari.__version__) >= parse_version("0.4.17a1")
_napari_ge_5 = parse_version(napari.__version__) >= parse_version("0.5.0a1")


class QtViewerPushButton(QtViewerPushButton_):
Expand Down Expand Up @@ -838,7 +839,7 @@ def _mark_layer(self, num: int, flash: bool, image_info: ImageInfo):
component_mark,
scale=image_info.roi.scale,
blending="translucent",
color={0: (0, 0, 0, 0), 1: "white"},
colormap=DirectLabelColormap(color_dict={0: (0, 0, 0, 0), 1: "white", None: (0, 0, 0, 0)}),
opacity=0.7,
)
self.viewer.layers.selection.active = active_layer
Expand Down Expand Up @@ -965,6 +966,12 @@ def closeEvent(self, event):
self.close()
super().closeEvent(event)

if _napari_ge_5:

@property
def view(self):
return self.canvas.view


class SearchComponentModal(QtPopup):
def __init__(self, image_view: ImageView, search_type: SearchType, component_num: int, max_components):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from magicgui.widgets import Container, HBox, PushButton, SpinBox, create_widget
from napari import Viewer
from napari.layers import Labels
from napari.utils import DirectLabelColormap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant entry in color_dict.

The color_dict parameter in DirectLabelColormap has a redundant entry for None. The DirectLabelColormap can handle missing keys without explicit None entry.

- colormap=DirectLabelColormap(color_dict={0: (0, 0, 0, 0), 1: "white", None: (0, 0, 0, 0)}),
+ colormap=DirectLabelColormap(color_dict={0: (0, 0, 0, 0), 1: "white"}),

Also applies to: 81-81

from napari.utils.notifications import show_info
from qtpy.QtCore import QTimer
from vispy.geometry import Rect
Expand Down Expand Up @@ -77,7 +78,7 @@ def _highlight(self):
name=HIGHLIGHT_LABEL_NAME,
scale=labels.scale,
blending="translucent",
color={0: (0, 0, 0, 0), 1: "white"},
colormap=DirectLabelColormap(color_dict={0: (0, 0, 0, 0), 1: "white", None: (0, 0, 0, 0)}),
opacity=0.7,
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ dependencies = [
"mahotas>=1.4.10",
"napari>=0.4.14",
"nme>=0.1.7",
"numpy>=1.18.5",
"numpy>=1.18.5,<2", # mahotas requires rebuild for numpy 2.
"oiffile>=2020.1.18",
"openpyxl>=2.5.7",
"packaging>=20.0",
Expand Down
Loading