# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
"""
.. Base panel objects (see parent package :mod:`datalab.gui.panel`)
"""
# pylint: disable=invalid-name # Allows short reference names like x, y, ...
from __future__ import annotations
import abc
import copy
import glob
import os
import os.path as osp
import re
import warnings
from collections.abc import Sequence
from contextlib import ExitStack, contextmanager
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Generator, Generic, Literal, Type
import guidata.dataset as gds
import guidata.dataset.qtwidgets as gdq
import h5py
import numpy as np
import plotpy.io
from guidata.configtools import get_icon
from guidata.dataset import restore_dataset, update_dataset
from guidata.dataset.datatypes import ComputedProp
from guidata.qthelpers import add_actions, create_action, exec_dialog
from plotpy.plot import BasePlot, BasePlotOptions, PlotDialog, SyncPlotDialog
from plotpy.tools import ActionTool
from qtpy import QtCore as QC # type: ignore[import]
from qtpy import QtWidgets as QW
from qtpy.compat import (
getexistingdirectory,
getopenfilename,
getopenfilenames,
getsavefilename,
)
from sigima.io import (
read_annotations,
read_metadata,
read_roi,
write_annotations,
write_metadata,
write_roi,
)
from sigima.io.base import get_file_extensions
from sigima.io.common.basename import format_basenames
from sigima.objects import (
ImageObj,
NewImageParam,
SignalObj,
TypeObj,
TypeROI,
create_image_from_param,
create_signal,
create_signal_from_param,
)
from sigima.objects.base import ROI_KEY
from sigima.params import SaveToDirectoryParam
from sigima.tools.signal.pulse import LegacyPeakParameterizationError
from datalab import objectmodel
from datalab.adapters_metadata import (
GeometryAdapter,
ResultData,
TableAdapter,
create_resultdata_dict,
show_resultdata,
)
from datalab.adapters_plotpy import create_adapter_from_object
from datalab.config import APP_NAME, Conf, _
from datalab.env import execenv
from datalab.gui import actionhandler, objectview
from datalab.gui.newobject import (
CREATION_PARAMETERS_OPTION,
LEGACY_CREATION_PARAMETERS_OPTION,
NewSignalParam,
convert_legacy_creation_parameters,
extract_creation_parameters,
insert_creation_parameters,
)
from datalab.gui.plugins.applications import get_declared_metadata_keys
from datalab.gui.processor.base import (
PROCESSING_PARAMETERS_OPTION,
ProcessingParameters,
ProcessingReport,
apply_processing_result,
clear_analysis_parameters,
extract_analysis_parameters,
extract_processing_parameters,
insert_processing_parameters,
)
from datalab.gui.roieditor import TypeROIEditor
from datalab.objectmodel import (
ObjectGroup,
get_number,
get_short_id,
get_short_uuid,
get_title_reference,
get_uuid,
get_uuid_display_id,
remap_title_references,
set_number,
set_uuid,
)
from datalab.utils.qthelpers import (
CallbackWorker,
create_progress_bar,
qt_long_callback,
qt_try_except,
qt_try_loadsave_file,
save_restore_stds,
)
from datalab.widgets.provenance import ProvenanceWidget
from datalab.widgets.textimport import TextImportWizard
if TYPE_CHECKING:
from plotpy.items import CurveItem, LabelItem, MaskedXYImageItem
from sigima.io import ImageExportParam
from sigima.io.image import ImageIORegistry
from sigima.io.signal import SignalIORegistry
from datalab.gui import ObjItf
from datalab.gui.main import DLMainWindow
from datalab.gui.plothandler import ImagePlotHandler, SignalPlotHandler
from datalab.gui.processor.image import ImageProcessor
from datalab.gui.processor.signal import SignalProcessor
from datalab.h5.native import NativeH5Reader, NativeH5Writer
from datalab.processingcall import ProcessingCall
# Metadata keys that should not be pasted when copying metadata between objects
METADATA_PASTE_EXCLUSIONS = {
ROI_KEY, # ROI has dedicated copy/paste operations
"__uuid", # Each object must have a unique identifier
f"__{PROCESSING_PARAMETERS_OPTION}", # Object-specific processing history
f"__{CREATION_PARAMETERS_OPTION}", # Object-specific creation parameters
f"__{LEGACY_CREATION_PARAMETERS_OPTION}", # Historical creation parameters
}
[docs]
def is_plot_item_serializable(item: Any) -> bool:
"""Return True if plot item is serializable"""
try:
plotpy.io.item_class_from_name(item.__class__.__name__)
return True
except AssertionError:
return False
[docs]
def is_hdf5_file(filename: str, check_content: bool = False) -> bool:
"""Return True if filename has an HDF5 extension or is an HDF5 file.
Args:
filename: Path to the file to check
check_content: If True, also attempts to open the file to verify it's a
valid HDF5 file. If False, only checks the file extension.
Returns:
True if the file is (likely) an HDF5 file, False otherwise.
"""
# First, check by extension (fast)
has_hdf5_extension = filename.lower().endswith(
(".h5", ".hdf5", ".hdf", ".he5", ".nxs", ".nx5")
)
if not check_content:
return has_hdf5_extension
# If checking content, try to open as HDF5 file
if has_hdf5_extension:
return True # Trust common HDF5 extensions
# For other extensions, attempt to open the file to verify it's HDF5
try:
with h5py.File(filename, "r"):
return True
except (OSError, IOError, ValueError):
# Not a valid HDF5 file
return False
[docs]
class ObjectProp(QW.QWidget):
"""Object handling panel properties
Args:
panel: parent data panel
objclass: class of the object handled by the panel (SignalObj or ImageObj)
"""
def __init__(self, panel: BaseDataPanel, objclass: SignalObj | ImageObj) -> None:
super().__init__(panel)
# Create the tab widget
self.tabwidget = QW.QTabWidget(self)
self.tabwidget.setTabBarAutoHide(True)
self.tabwidget.setTabPosition(QW.QTabWidget.West)
self.panel = panel
self.objclass = objclass
# Object creation tab
self.creation_param_editor: gdq.DataSetEditGroupBox | None = None
self.current_creation_obj: SignalObj | ImageObj | None = None
self.creation_scroll: QW.QScrollArea | None = None
# Object processing tab
self.processing_param_editor: gdq.DataSetEditGroupBox | None = None
self.current_processing_obj: SignalObj | ImageObj | None = None
self.current_processing_call: ProcessingCall | None = None
self.processing_scroll: QW.QScrollArea | None = None
# Object analysis tab (editable 1-to-0 analysis parameters)
self.analysis_param_editor: gdq.DataSetEditGroupBox | None = None
self.current_analysis_obj: SignalObj | ImageObj | None = None
self.analysis_scroll: QW.QScrollArea | None = None
# Auto-recompute toggle (session-only state, not persisted to Conf).
self.__auto_recompute_enabled: bool = False
self.__auto_recompute_timer = QC.QTimer(self)
self.__auto_recompute_timer.setSingleShot(True)
self.__auto_recompute_timer.timeout.connect(self.__auto_recompute_trigger)
# Properties tab
self.properties = gdq.DataSetEditGroupBox("", objclass)
self.properties.SIG_APPLY_BUTTON_CLICKED.connect(panel.properties_changed)
self.properties.setEnabled(False)
self.__original_values: dict[str, Any] = {}
# Create Analysis and History widgets
font = Conf.small_mono_font.get_font()
self.processing_history = QW.QTextEdit()
self.processing_history.setReadOnly(True)
self.processing_history.setFont(font)
# The Provenance tab shows the provenance recorded in the workspace ledger,
# or the processing metadata of objects without recorded production.
self.provenance_widget = ProvenanceWidget(panel.mainwindow)
self.history_stack = QW.QStackedWidget()
self.history_stack.addWidget(self.processing_history)
self.history_stack.addWidget(self.provenance_widget)
self.analysis_parameters = QW.QTextEdit()
self.analysis_parameters.setReadOnly(True)
self.analysis_parameters.setFont(font)
# Track newly created objects to show Creation tab only once
self.newly_created_obj_uuid: str | None = None
# Track when analysis results were just computed
self.fresh_analysis_obj_uuid: str | None = None
# Track when object was just processed (1-to-1)
self.fresh_processing_obj_uuid: str | None = None
# UUID of the object shown (see update_properties_from)
self.__shown_uuid: str | None = None
#: Tab kept current when another object is shown (see ``keeping_tab``)
self.__kept_tab: str | None = None
self.tabwidget.addTab(
self.history_stack, get_icon("history.svg"), _("Provenance")
)
self.tabwidget.addTab(
self.analysis_parameters, get_icon("analysis.svg"), _("Analysis parameters")
)
self.tabwidget.addTab(
self.properties, get_icon("properties.svg"), _("Properties")
)
self.processing_history.textChanged.connect(self._update_tab_visibility)
self.analysis_parameters.textChanged.connect(self._update_tab_visibility)
# Create vertical layout for the container
layout = QW.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Add tab widget and button area to main layout
layout.addWidget(self.tabwidget)
# Here we could add another widget or layout if needed (in DataLab v0.20, we
# had a permanent button area here, but it was removed to avoid clutter)
def _update_tab_visibility(self) -> None:
"""Update visibility of tabs based on their content."""
index = self.tabwidget.indexOf(self.history_stack)
if index >= 0:
if self.history_stack.currentWidget() is self.provenance_widget:
has_content = self.provenance_widget.has_content()
else:
has_content = bool(self.processing_history.toPlainText().strip())
self.tabwidget.setTabVisible(index, has_content)
index = self.tabwidget.indexOf(self.analysis_parameters)
if index >= 0:
has_content = bool(self.analysis_parameters.toPlainText().strip())
self.tabwidget.setTabVisible(index, has_content)
[docs]
def display_analysis_parameters(self, obj: SignalObj | ImageObj) -> bool:
"""Set analysis parameter label.
Args:
obj: Signal or Image object
Returns:
True if analysis parameters were found and displayed, False otherwise.
"""
text = ""
# Iterate through all result adapters and extract parameter info
for adapter_class in (GeometryAdapter, TableAdapter):
for adapter in adapter_class.iterate_from_obj(obj):
param = adapter.get_param()
if param is not None:
if text:
text += "<br><br>"
# Get function name for context
func_name = adapter.func_name
if func_name:
# Add function name as a header for better context
param.set_comment(
"(" + _("Parameters for function `%s`") % func_name + ")"
)
text += param.to_html()
self.analysis_parameters.setText(text)
return bool(text)
def _build_processing_history(self, obj: SignalObj | ImageObj) -> str:
"""Build processing history as a simple text list.
Args:
obj: Signal or Image object
Returns:
Processing history as text
"""
history_items = []
has_creation_problem = False
current_obj = obj
max_depth = 20 # Prevent infinite loops
# Walk backwards through processing chain, collecting items
while current_obj is not None and len(history_items) < max_depth:
proc_params = extract_processing_parameters(current_obj, parse_param=False)
if proc_params is None:
# Check for creation parameters
try:
creation_params = extract_creation_parameters(current_obj)
except LegacyPeakParameterizationError:
history_items.append(_("Created: historical peak parameters"))
has_creation_problem = True
break
except ValueError:
history_items.append(_("Created: invalid creation parameters"))
has_creation_problem = True
break
if creation_params is not None:
text = f"{_('Created')}: {creation_params.title}"
history_items.append(text)
else:
history_items.append(_("Original object"))
break
# Skip 1-to-0 operations (analysis) as they don't transform the object
# They just add metadata, so they shouldn't appear in processing history
if proc_params.pattern == "1-to-0":
# For 1-to-0 operations, there's no processing history to show
# (they analyze but don't transform the object)
# Check if there's any earlier processing
try:
creation_params = extract_creation_parameters(current_obj)
except LegacyPeakParameterizationError:
history_items.append(_("Created: historical peak parameters"))
has_creation_problem = True
break
except ValueError:
history_items.append(_("Created: invalid creation parameters"))
has_creation_problem = True
break
if creation_params is not None:
text = f"{_('Created')}: {creation_params.title}"
history_items.append(text)
else:
history_items.append(_("Original object"))
break
# Add current processing step (without any plugin namespace)
local_name = proc_params.func_name.rsplit(":", 1)[-1]
func_name = local_name.replace("_", " ").title()
history_items.append(func_name)
# Try to find source object
if proc_params.source_uuid:
current_obj = self.panel.mainwindow.find_object_by_uuid(
proc_params.source_uuid
)
if current_obj is None:
history_items.append(_("(source deleted)"))
break
else:
if proc_params.source_uuids:
# Multiple sources (n-to-1 or 2-to-1 pattern)
history_items.append(_("(multiple sources)"))
break
if len(history_items) <= 1 and not has_creation_problem:
return "" # Shows the history tab only when there is some history
# Reverse to show from oldest to newest, then add indentation
history_items.reverse()
history_lines = []
for i, item in enumerate(history_items):
indent = " " * i
history_lines.append(f"{indent}└─ {item}")
return "\n".join(history_lines)
[docs]
def display_processing_history(self, obj: SignalObj | ImageObj) -> bool:
"""Display the provenance of an object in the Provenance tab.
When the workspace ledger records a production, an old record or an
analysis of the object, the provenance view is shown (see
:class:`datalab.widgets.provenance.ProvenanceWidget`); otherwise, the
history rebuilt from the processing metadata of the object is shown.
Args:
obj: Signal or Image object
Returns:
True if a history was found and displayed, False otherwise.
"""
history_text = self._build_processing_history(obj)
self.processing_history.setText(history_text)
if self.provenance_widget.set_object(obj, history_text):
self.history_stack.setCurrentWidget(self.provenance_widget)
self._update_tab_visibility()
return True
self.history_stack.setCurrentWidget(self.processing_history)
self._update_tab_visibility()
return bool(history_text)
[docs]
def refresh_history(self) -> None:
"""Show again the History of the current object (ledger read again).
Called when activities were recorded or the workspace changed: results
of data checks and verifications shown before are dropped.
"""
obj = self.panel.objview.get_current_object()
if obj is not None:
self.display_processing_history(obj)
def __update_properties_dataset(self, obj: SignalObj | ImageObj) -> None:
"""Update properties dataset from signal/image dataset
Args:
obj: Signal or Image object
"""
dataset: SignalObj | ImageObj = self.properties.dataset
dataset.set_defaults()
update_dataset(dataset, obj)
self.properties.get()
self.properties.apply_button.setEnabled(False)
[docs]
def update_properties_from(
self,
obj: SignalObj | ImageObj | None = None,
force_tab: Literal["creation", "processing", "analysis", None] | None = None,
) -> None:
"""Update properties panel (properties, creation, processing) from object.
Args:
obj: Signal or Image object
force_tab: Force a specific tab to be current
"""
self.properties.setDisabled(obj is None)
# The tab shown stays current when the same object is shown again (e.g.
# reselected from the History panel); another object shows Properties.
uuid = None if obj is None else get_uuid(obj)
previous_tab = (
self.__current_tab_kind()
if uuid is not None and uuid == self.__shown_uuid
else None
)
if uuid is not None and self.__kept_tab is not None:
previous_tab = self.__kept_tab
self.__shown_uuid = uuid
if obj is None:
obj = self.objclass()
# Update the properties dataset
self.__update_properties_dataset(obj)
# Store original values to detect which properties have changed
# (using `restore_dataset` to convert the dataset to a dictionary)
self.__original_values = {}
restore_dataset(self.properties.dataset, self.__original_values)
# Display analysis parameters and processing history
has_analysis_parameters = self.display_analysis_parameters(obj)
self.display_processing_history(obj)
# Remove only Creation and Processing tabs (dynamic tabs)
# Use widget references instead of text labels for reliable identification
self.__auto_recompute_timer.stop()
if self.processing_param_editor is not None:
self.processing_param_editor.on_change = None
if self.creation_scroll is not None:
index = self.tabwidget.indexOf(self.creation_scroll)
if index >= 0:
self.tabwidget.removeTab(index)
if self.processing_scroll is not None:
index = self.tabwidget.indexOf(self.processing_scroll)
if index >= 0:
self.tabwidget.removeTab(index)
self.processing_scroll.deleteLater()
if self.analysis_scroll is not None:
index = self.tabwidget.indexOf(self.analysis_scroll)
if index >= 0:
self.tabwidget.removeTab(index)
# Reset references for dynamic tabs
self.creation_param_editor = None
self.current_creation_obj = None
self.creation_scroll = None
self.processing_param_editor = None
self.current_processing_obj = None
self.current_processing_call = None
self.processing_scroll = None
self.analysis_param_editor = None
self.current_analysis_obj = None
self.analysis_scroll = None
# Setup Creation and Processing tabs (if applicable)
has_creation_tab = False
has_processing_tab = False
has_analysis_tab = False
if obj is not None:
has_creation_tab = self.setup_creation_tab(obj)
has_processing_tab = self.setup_processing_tab(obj) # Processing tab setup
has_analysis_tab = self.setup_analysis_tab(obj) # Analysis tab setup
# Trigger visibility update for History and Analysis parameters tabs
# (will be called via textChanged signals, but we call explicitly
# here to ensure initial state is correct)
self._update_tab_visibility()
# Determine which tab to show based on force_tab parameter:
# - If force_tab="creation" and Creation tab exists, show it
# - If force_tab="processing" and Processing tab has a parameter editor,
# show it (a processing without editable parameters keeps Properties)
# - If force_tab="analysis" and Analysis tab has content, show it
# - Otherwise, always show Properties tab (default behavior)
if force_tab == "creation" and has_creation_tab:
self.tabwidget.setCurrentWidget(self.creation_scroll)
elif (
force_tab == "processing"
and has_processing_tab
and self.processing_param_editor is not None
):
self.tabwidget.setCurrentWidget(self.processing_scroll)
elif force_tab == "analysis" and has_analysis_tab:
self.tabwidget.setCurrentWidget(self.analysis_scroll)
elif force_tab == "analysis" and has_analysis_parameters:
self.tabwidget.setCurrentWidget(self.analysis_parameters)
elif previous_tab is not None and self.__restore_tab(previous_tab):
pass
else:
# Default: always show Properties tab when switching objects
self.tabwidget.setCurrentWidget(self.properties)
[docs]
@contextmanager
def keeping_tab(self, kind: str) -> Generator[None, None, None]:
"""Keep the tab of *kind* current when another object is shown meanwhile.
For navigation from a tab to another object (e.g. from the provenance of
a result to one of its inputs), which otherwise shows Properties.
"""
previous, self.__kept_tab = self.__kept_tab, kind
try:
yield
finally:
self.__kept_tab = previous
def __current_tab_kind(self) -> str | None:
"""Return the kind of the current tab (dynamic tabs are rebuilt)."""
widget = self.tabwidget.currentWidget()
kinds = {
"creation": self.creation_scroll,
"processing": self.processing_scroll,
"analysis": self.analysis_scroll,
"provenance": self.history_stack,
"analysis_parameters": self.analysis_parameters,
}
for kind, tab in kinds.items():
if tab is not None and widget is tab:
return kind
return None
def __restore_tab(self, kind: str) -> bool:
"""Make the tab of *kind* current again if it is shown; return success."""
widget = {
"creation": self.creation_scroll,
"processing": self.processing_scroll,
"analysis": self.analysis_scroll,
"provenance": self.history_stack,
"analysis_parameters": self.analysis_parameters,
}[kind]
index = -1 if widget is None else self.tabwidget.indexOf(widget)
if index < 0 or not self.tabwidget.isTabVisible(index):
return False
self.tabwidget.setCurrentWidget(widget)
return True
[docs]
def mark_as_newly_created(self, obj: SignalObj | ImageObj) -> None:
"""Mark object to show Creation tab on next selection.
Args:
obj: Object to mark
"""
self.newly_created_obj_uuid = get_uuid(obj)
[docs]
def mark_as_freshly_processed(self, obj: SignalObj | ImageObj) -> None:
"""Mark object to show Processing tab on next selection.
Args:
obj: Object to mark
"""
self.fresh_processing_obj_uuid = get_uuid(obj)
[docs]
def mark_as_fresh_analysis(self, obj: SignalObj | ImageObj) -> None:
"""Mark object to show Analysis tab on next selection.
Args:
obj: Object to mark
"""
self.fresh_analysis_obj_uuid = get_uuid(obj)
[docs]
def get_changed_properties(self) -> dict[str, Any]:
"""Get dictionary of properties that have changed from original values.
Returns:
Dictionary mapping property names to their new values, containing only
the properties that were modified by the user.
"""
dataset = self.properties.dataset
changed = {}
# Get current values as a dictionary
current_values = {}
restore_dataset(dataset, current_values)
# Compare with original values
for key, current_value in current_values.items():
original_value = self.__original_values.get(key)
# Check if value has changed
if not self._values_equal(current_value, original_value):
changed[key] = current_value
return changed
[docs]
def update_original_values(self) -> None:
"""Update the stored original values to the current dataset values.
This should be called after applying changes to reset the baseline
for detecting future changes.
"""
dataset = self.properties.dataset
self.__original_values = {}
restore_dataset(dataset, self.__original_values)
@staticmethod
def _values_equal(val1: Any, val2: Any) -> bool:
"""Compare two values, handling special cases like numpy arrays.
Args:
val1: first value
val2: second value
Returns:
True if values are equal
"""
# Handle numpy arrays
if isinstance(val1, np.ndarray) or isinstance(val2, np.ndarray):
if not isinstance(val1, np.ndarray) or not isinstance(val2, np.ndarray):
return False
return np.array_equal(val1, val2)
# Handle regular comparison
return val1 == val2
[docs]
def setup_creation_tab(
self, obj: SignalObj | ImageObj, set_current: bool = False
) -> bool:
"""Setup the Creation tab with parameter editor for interactive object creation.
Args:
obj: Signal or Image object
set_current: If True, set the Creation tab as current after creation
Returns:
True if Creation tab was set up, False otherwise
"""
try:
param = extract_creation_parameters(obj)
except LegacyPeakParameterizationError as exc:
if execenv.unattended:
raise
return self.setup_legacy_creation_tab(obj, str(exc), set_current)
except ValueError as exc:
if execenv.unattended:
raise
return self.setup_invalid_creation_tab(obj, str(exc), set_current)
if param is None:
return False
# Create parameter editor widget using the actual parameter class
# (which is a subclass of NewSignalParam or NewImageParam)
editor = gdq.DataSetEditGroupBox(_("Creation Parameters"), param.__class__)
update_dataset(editor.dataset, param)
editor.get()
# Connect Apply button to recreation handler
editor.SIG_APPLY_BUTTON_CLICKED.connect(self.apply_creation_parameters)
editor.set_apply_button_state(False)
# Store reference to be able to retrieve it later
self.creation_param_editor = editor
self.current_creation_obj = obj
# Remove existing Creation tab if it exists
if self.creation_scroll is not None:
index = self.tabwidget.indexOf(self.creation_scroll)
if index >= 0:
self.tabwidget.removeTab(index)
# Set the parameter editor as the scroll area widget
# Creation tab is always at index 0 (before all other tabs)
self.creation_scroll = QW.QScrollArea()
self.creation_scroll.setWidgetResizable(True)
self.creation_scroll.setWidget(editor)
icon_name = "new_sig.svg" if isinstance(obj, SignalObj) else "new_ima.svg"
self.tabwidget.insertTab(
0, self.creation_scroll, get_icon(icon_name), _("Creation")
)
# Set as current tab if requested
if set_current:
self.tabwidget.setCurrentWidget(self.creation_scroll)
return True
[docs]
def setup_legacy_creation_tab(
self,
obj: SignalObj | ImageObj,
error: str,
set_current: bool = False,
) -> bool:
"""Setup actions for explicitly converting historical peak parameters."""
widget = QW.QWidget(self)
layout = QW.QVBoxLayout(widget)
message = QW.QLabel(
_(
"This object uses historical area-based peak parameters. "
"Convert them to signed peak height before editing."
),
widget,
)
message.setWordWrap(True)
message.setToolTip(error)
layout.addWidget(message)
buttons = QW.QHBoxLayout()
convert_button = QW.QPushButton(_("Convert historical parameters"), widget)
cancel_button = QW.QPushButton(_("Cancel"), widget)
buttons.addWidget(convert_button)
buttons.addWidget(cancel_button)
buttons.addStretch(1)
layout.addLayout(buttons)
layout.addStretch(1)
self.creation_param_editor = None
self.current_creation_obj = obj
self.creation_scroll = QW.QScrollArea()
self.creation_scroll.setWidgetResizable(True)
self.creation_scroll.setWidget(widget)
icon_name = "new_sig.svg" if isinstance(obj, SignalObj) else "new_ima.svg"
self.tabwidget.insertTab(
0, self.creation_scroll, get_icon(icon_name), _("Creation")
)
convert_button.clicked.connect(self.convert_current_creation_parameters)
cancel_button.clicked.connect(
lambda: self.tabwidget.setCurrentWidget(self.properties)
)
if set_current:
self.tabwidget.setCurrentWidget(self.creation_scroll)
return True
[docs]
def setup_invalid_creation_tab(
self,
obj: SignalObj | ImageObj,
error: str,
set_current: bool = False,
) -> bool:
"""Show a read-only state for invalid creation metadata."""
widget = QW.QWidget(self)
layout = QW.QVBoxLayout(widget)
message = QW.QLabel(
_(
"Creation parameters cannot be edited because their metadata is "
"invalid or was written by a newer DataLab version."
),
widget,
)
message.setWordWrap(True)
message.setToolTip(error)
layout.addWidget(message)
layout.addStretch(1)
self.creation_param_editor = None
self.current_creation_obj = obj
self.creation_scroll = QW.QScrollArea()
self.creation_scroll.setWidgetResizable(True)
self.creation_scroll.setWidget(widget)
icon_name = "new_sig.svg" if isinstance(obj, SignalObj) else "new_ima.svg"
self.tabwidget.insertTab(
0, self.creation_scroll, get_icon(icon_name), _("Creation")
)
if set_current:
self.tabwidget.setCurrentWidget(self.creation_scroll)
return True
[docs]
def convert_current_creation_parameters(self) -> None:
"""Convert the current object's historical peak creation parameters."""
obj = self.current_creation_obj
if obj is None:
return
try:
convert_legacy_creation_parameters(obj)
except Exception as exc: # pylint: disable=broad-exception-caught
if execenv.unattended:
raise
QW.QMessageBox.warning(
self,
_("Error"),
_("Failed to convert historical creation parameters:\n%s") % str(exc),
)
return
self.__update_properties_dataset(obj)
self.setup_creation_tab(obj, set_current=True)
[docs]
def apply_creation_parameters(self) -> None:
"""Apply creation parameters: recreate object with updated parameters."""
editor = self.creation_param_editor
if editor is None or self.current_creation_obj is None:
return
# Recreate object with new parameters
# (serialization is done automatically in create_signal/image_from_param)
param = editor.dataset
try:
if isinstance(self.current_creation_obj, SignalObj):
new_obj = create_signal_from_param(param)
else: # ImageObj
new_obj = create_image_from_param(param)
except Exception as exc: # pylint: disable=broad-exception-caught
if execenv.unattended:
raise exc
QW.QMessageBox.warning(
self,
_("Error"),
_("Failed to recreate object with new parameters:\n%s") % str(exc),
)
return
# Update the current object in-place
obj_uuid = get_uuid(self.current_creation_obj)
self.current_creation_obj.title = new_obj.title
if isinstance(self.current_creation_obj, SignalObj):
self.current_creation_obj.xydata = new_obj.xydata
else: # ImageObj
self.current_creation_obj.data = new_obj.data
# Invalidate ROI mask cache when image dimensions change
# (the mask is computed based on image shape, so it must be recomputed)
self.current_creation_obj.invalidate_maskdata_cache()
# Update metadata with new creation parameters
insert_creation_parameters(self.current_creation_obj, param)
# Propagate the edited param to the History panel: mutate the matching
# creation action (snapshot originals first), refresh its tree display,
# then cascade recompute to downstream actions so the chain stays
# consistent with the new creation parameters. Creation actions are
# KIND_UI without a func_name, so look them up via output_to_action.
hpanel = self.panel.mainwindow.historypanel
action = hpanel.find_creation_action_for_output(obj_uuid)
if action is not None:
action.snapshot_kwargs()
action.kwargs["param"] = copy.deepcopy(param)
hpanel.refresh_action(action)
hpanel.recompute_cascade(action)
# Update the tree view item (to show new title if it changed)
self.panel.objview.update_item(obj_uuid)
# Refresh only the plot, not the entire panel
# (avoid calling selection_changed which would trigger a full refresh
# of the Properties tab and could cause recursion issues)
self.panel.refresh_plot(obj_uuid, update_items=True, force=True)
# Update the Properties tab to reflect the new object properties
# (e.g., data type, dimensions, etc.)
self.__update_properties_dataset(self.current_creation_obj)
if isinstance(self.current_creation_obj, SignalObj):
text = _("Signal was recreated.")
else:
text = _("Image was recreated.")
self.panel.SIG_STATUS_MESSAGE.emit("✅ " + text, 5000)
# Refresh the Creation tab with the new parameters
# Use QTimer to defer this until after the current event is processed
# Set the Creation tab as current to keep it visible after refresh
QC.QTimer.singleShot(
0,
lambda: self.setup_creation_tab(
self.current_creation_obj, set_current=True
),
)
[docs]
def setup_processing_tab(
self,
obj: SignalObj | ImageObj,
reset_params: bool = True,
set_current: bool = False,
) -> bool:
"""Setup the Processing tab to show and apply again a processing.
The call that produced the object is restored (see
:func:`datalab.processingcall.resolve_processing_call`): 1-to-1, 2-to-1
and n-to-1 processing, with every input. Parameters are edited in a
parameter editor; a processing without editable parameters gets an
explicit *Reapply* button. When the processing cannot be applied again
(deleted input, unavailable operation...), the reason is shown.
Args:
obj: Signal or Image object
reset_params: If True, call update_from_obj() to reset parameters from
source object. If False, use parameters as stored in metadata.
set_current: If True, set the Processing tab as current after creation
Returns:
True if Processing tab was set up, False otherwise
"""
# pylint: disable=import-outside-toplevel
from datalab.processingcall import resolve_processing_call
from datalab.widgets.processingparameters import ProcessingParametersEditor
self.__auto_recompute_timer.stop()
if self.processing_param_editor is not None:
self.processing_param_editor.on_change = None
call = resolve_processing_call(self.panel.mainwindow, obj)
if call is None:
return False
# Store reference to be able to retrieve it later
self.current_processing_obj = obj
self.current_processing_call = call
editor = None
param = call.param
if call.param_editable:
# Source-aware parameters may refresh transient editor context
# without replacing their persisted values. Legacy parameters keep
# the previous reset-only initialization behavior.
source_obj = call.inputs[0].obj if call.inputs else None
if hasattr(param, "update_editor_context"):
param.update_editor_context(source_obj)
elif (
reset_params
and source_obj is not None
and hasattr(param, "update_from_obj")
):
param.update_from_obj(source_obj)
editor = ProcessingParametersEditor(
_("Processing Parameters"), param.__class__, wordwrap=True
)
update_dataset(editor.dataset, param)
editor.get()
# Connect Apply button to reprocessing handler
editor.SIG_APPLY_BUTTON_CLICKED.connect(self.apply_processing_parameters)
editor.set_apply_button_state(False)
editor.on_change = lambda: self.__processing_parameters_changed(editor)
# Store reference to be able to retrieve it later
self.processing_param_editor = editor
# Remove existing Processing tab if it exists
if self.processing_scroll is not None:
index = self.tabwidget.indexOf(self.processing_scroll)
if index >= 0:
self.tabwidget.removeTab(index)
self.processing_scroll.deleteLater()
# Processing tab comes after Creation tab (if it exists)
# Find the correct insertion index: after Creation (index 0) if it exists,
# otherwise at index 0
has_creation = (
self.creation_scroll is not None
and self.tabwidget.indexOf(self.creation_scroll) >= 0
)
insert_index = 1 if has_creation else 0
# Create new processing scroll area and tab
self.processing_scroll = QW.QScrollArea()
self.processing_scroll.setWidgetResizable(True)
self.processing_scroll.setHorizontalScrollBarPolicy(QC.Qt.ScrollBarAlwaysOff)
self.processing_scroll.setSizePolicy(
QW.QSizePolicy.Expanding, QW.QSizePolicy.Preferred
)
container = QW.QWidget()
layout = QW.QVBoxLayout(container)
layout.addWidget(self.__processing_call_summary(call))
if editor is not None:
# Add the auto-recompute option below Apply, aligned with inputs.
auto_cb = QW.QCheckBox(_("Auto-recompute on edit"), editor)
auto_cb.setObjectName("auto_recompute_on_edit")
auto_cb.setIcon(get_icon("replay.svg"))
auto_cb.setToolTip(
_("Automatically re-run processing when parameters are modified")
)
auto_cb.setChecked(self.__auto_recompute_enabled)
auto_cb.toggled.connect(self.__set_auto_recompute_enabled)
form_layout = editor.edit.layout
apply_index = form_layout.indexOf(editor.apply_button)
apply_row, _column, _row_span, _column_span = form_layout.getItemPosition(
apply_index
)
input_column = 1
input_column_span = max(1, form_layout.columnCount() - input_column)
form_layout.addWidget(
auto_cb,
apply_row + 1,
input_column,
1,
input_column_span,
QC.Qt.AlignLeft,
)
layout.addWidget(editor)
else:
if param is None:
note = _("This processing has no parameters.")
else:
note = _("The parameters of this processing cannot be edited here.")
note_label = QW.QLabel(note)
note_label.setWordWrap(True)
layout.addWidget(note_label)
button = QW.QPushButton(get_icon("replay.svg"), _("Reapply"))
button.setObjectName("reapply_processing")
button.setToolTip(
_("Apply this processing again to the current input objects")
)
button.setEnabled(call.can_reapply)
button.clicked.connect(lambda _checked=False: self.reapply_processing(obj))
layout.addWidget(button, 0, QC.Qt.AlignLeft)
if call.reason is not None:
reason_label = QW.QLabel("⚠️ " + call.reason)
reason_label.setObjectName("processing_unavailable_reason")
reason_label.setTextFormat(QC.Qt.PlainText)
reason_label.setWordWrap(True)
layout.addWidget(reason_label)
layout.addStretch()
self.processing_scroll.setWidget(container)
self.tabwidget.insertTab(
insert_index,
self.processing_scroll,
get_icon("libre-tech-ram.svg"),
_("Processing"),
)
# Set as current tab if requested
if set_current:
self.tabwidget.setCurrentWidget(self.processing_scroll)
return True
def __processing_call_summary(self, call: ProcessingCall) -> QW.QLabel:
"""Return a label describing a processing call and its ordered inputs."""
lines = [call.title]
for item in call.inputs:
if item.obj is None:
name = _("(deleted object)")
else:
title = self.panel.mainwindow.render_object_title(item.obj.title)
name = f"{get_uuid_display_id(item.obj)}: {title}"
lines.append(f" • {item.role}: {name}")
label = QW.QLabel("\n".join(lines))
label.setObjectName("processing_call_summary")
label.setTextFormat(QC.Qt.PlainText)
label.setWordWrap(True)
return label
[docs]
def reapply_processing(self, obj: SignalObj | ImageObj | None = None) -> None:
"""Apply again the processing of an object without editable parameters.
Args:
obj: Processed object (default: the object of the Processing tab).
"""
obj = obj or self.current_processing_obj
if obj is not None:
self.apply_processing_parameters(obj, param=None)
[docs]
def setup_analysis_tab(
self, obj: SignalObj | ImageObj, set_current: bool = False
) -> bool:
"""Setup the Analysis tab with parameter editor for re-running analysis.
This tab lets the user edit the parameters of a 1-to-0 analysis
operation (peak detection, FWHM, segments, etc.) and re-run it in place
with the modified parameters.
Args:
obj: Signal or Image object
set_current: If True, set the Analysis tab as current after creation
Returns:
True if Analysis tab was set up, False otherwise
"""
# Extract analysis parameters (1-to-0 pattern only)
proc_params = extract_analysis_parameters(obj)
if proc_params is None or proc_params.pattern != "1-to-0":
return False
param = proc_params.param
if param is None or isinstance(param, list):
return False
# Store reference to be able to retrieve it later
self.current_analysis_obj = obj
# Create parameter editor widget
editor = gdq.DataSetEditGroupBox(
_("Analysis Parameters"), param.__class__, wordwrap=True
)
update_dataset(editor.dataset, param)
editor.get()
# Connect Apply button to re-analysis handler
editor.SIG_APPLY_BUTTON_CLICKED.connect(self.apply_analysis_parameters)
editor.set_apply_button_state(False)
# Store reference to be able to retrieve it later
self.analysis_param_editor = editor
# Remove existing Analysis tab if it exists
if self.analysis_scroll is not None:
index = self.tabwidget.indexOf(self.analysis_scroll)
if index >= 0:
self.tabwidget.removeTab(index)
# Analysis tab comes after Creation and Processing tabs (if they exist)
insert_index = 0
if (
self.creation_scroll is not None
and self.tabwidget.indexOf(self.creation_scroll) >= 0
):
insert_index += 1
if (
self.processing_scroll is not None
and self.tabwidget.indexOf(self.processing_scroll) >= 0
):
insert_index += 1
# Create new analysis scroll area and tab
self.analysis_scroll = QW.QScrollArea()
self.analysis_scroll.setWidgetResizable(True)
self.analysis_scroll.setHorizontalScrollBarPolicy(QC.Qt.ScrollBarAlwaysOff)
self.analysis_scroll.setSizePolicy(
QW.QSizePolicy.Expanding, QW.QSizePolicy.Preferred
)
self.analysis_scroll.setWidget(editor)
self.tabwidget.insertTab(
insert_index,
self.analysis_scroll,
get_icon("analysis.svg"),
_("Analysis"),
)
# Set as current tab if requested
if set_current:
self.tabwidget.setCurrentWidget(self.analysis_scroll)
return True
[docs]
def apply_analysis_parameters(
self,
obj: SignalObj | ImageObj | None = None,
interactive: bool = True,
param: gds.DataSet | None = None,
) -> bool:
"""Apply analysis parameters: re-run the 1-to-0 analysis in place.
Args:
obj: Signal or Image object to re-analyze. If None, uses the current
analysis object.
interactive: If True, show error messages in the UI.
param: Explicit analysis parameters to apply. When None (default),
fall back to the editor dataset or the stored analysis parameters.
"""
if execenv.unattended:
interactive = False
editor = self.analysis_param_editor
obj = obj or self.current_analysis_obj
if obj is None:
return False
# Extract analysis parameters
proc_params = extract_analysis_parameters(obj)
if proc_params is None:
if interactive:
QW.QMessageBox.warning(
self, _("Error"), _("Analysis metadata is incomplete.")
)
return False
func_name = proc_params.func_name
# Resolve the parameters to apply. An explicit ``param`` argument takes
# precedence; otherwise fall back to the editor (interactive Apply) or
# the stored analysis parameters.
if param is None:
param = editor.dataset if editor is not None else proc_params.param
recompute_param = copy.deepcopy(param)
# Re-run the analysis in place (no history entry: runs under replaying)
processor = self.__get_processor_associated_to(obj)
provenance = self.panel.mainwindow.provenance
try:
with provenance.collecting() as activity_ids:
success = processor.recompute_1_to_0(
func_name,
obj,
recompute_param,
plugin_origin=proc_params.plugin_origin,
)
except Exception as exc: # pylint: disable=broad-exception-caught
if execenv.unattended:
raise exc
QW.QMessageBox.warning(
self,
_("Error"),
_("Failed to recompute analysis:\n%s") % str(exc),
)
return False
if not success:
if interactive:
QW.QMessageBox.warning(
self, _("Error"), _("Failed to recompute analysis.")
)
return False
# In History edit mode only, propagate the edited param to the History
# panel: mutate the matching analysis action (snapshot originals first)
# and refresh its tree display. Outside edit mode, the action keeps its
# recipe and its links; the new execution is recorded in provenance
# only. Analysis is a leaf operation (1-to-0), so no cascade is needed.
hpanel = self.panel.mainwindow.historypanel
action = None
if hpanel.is_edit_mode():
action = hpanel.find_analysis_action(get_uuid(obj), func_name)
if action is not None:
action.snapshot_kwargs()
action.kwargs["param"] = copy.deepcopy(recompute_param)
self.__relink_analysis(action, get_uuid(obj), activity_ids)
hpanel.refresh_action(action)
# Refresh the object display after re-analysis
obj_uuid = get_uuid(obj)
self.display_analysis_parameters(obj)
self.panel.objview.update_item(obj_uuid)
# Analysis results are plot shapes, so force a plot refresh
self.panel.refresh_plot(obj_uuid, update_items=True, force=True)
self.panel.SIG_STATUS_MESSAGE.emit("✅ " + _("Analysis was recomputed."), 5000)
# Refresh the Analysis tab with the new parameters (defer, keep visible)
QC.QTimer.singleShot(
0,
lambda: self.setup_analysis_tab(
self.current_analysis_obj, set_current=True
),
)
return True
def __relink_analysis(
self, action: Any, obj_uuid: str, activity_ids: list[str]
) -> None:
"""Link an analysis action to the analysis of *obj_uuid* just recorded.
The activities of this object in the action links (its last execution)
are replaced by the new ones; those of the other analysed objects stay.
"""
if not activity_ids:
return
ledger = self.panel.mainwindow.provenance.ledger
def concerns(activity_id: str) -> bool:
try:
activity = ledger.activity(activity_id)
except KeyError:
return True # unknown here: superseded by the new execution
states = ledger.states
return any(
states[item["binding"]["state_id"]]["object_uuid"] == obj_uuid
for item in activity["call"]["inputs"]
if "state_id" in item["binding"]
)
links = list(action.provenance_activity_ids)
flags = [concerns(activity_id) for activity_id in links]
kept = [value for value, flag in zip(links, flags) if not flag]
# The new activities take the place of the first superseded one
position = flags.index(True) if True in flags else len(links)
position = sum(1 for flag in flags[:position] if not flag)
action.provenance_activity_ids = (
kept[:position] + list(activity_ids) + kept[position:]
)
def __get_processor_associated_to(
self, obj: SignalObj | ImageObj
) -> SignalProcessor | ImageProcessor:
"""Get the processor associated to the given object type.
Args:
obj: Signal or Image object
Returns:
Processor associated to the object's type
"""
assert isinstance(obj, (SignalObj, ImageObj))
if isinstance(obj, SignalObj):
return self.panel.mainwindow.signalpanel.processor
return self.panel.mainwindow.imagepanel.processor
def __set_auto_recompute_enabled(self, enabled: bool) -> None:
"""Toggle auto-recompute mode (session-only, not persisted)."""
self.__auto_recompute_enabled = bool(enabled)
if not self.__auto_recompute_enabled:
self.__auto_recompute_timer.stop()
def __processing_parameters_changed(self, editor) -> None:
"""Debounce real processing only for the current valid, released editor."""
if editor is not self.processing_param_editor:
return
self.__auto_recompute_timer.stop()
if (
self.__auto_recompute_enabled
and not editor.dragging
and editor.edit.check_all_values()
):
self.__auto_recompute_timer.start(300)
def __auto_recompute_trigger(self) -> None:
"""Debounced callback: push widget values then re-run processing."""
if not self.__auto_recompute_enabled:
return
editor = self.processing_param_editor
if editor is None or editor.dragging or not editor.edit.check_all_values():
return
# ``editor.set()`` synchronises widget values to the dataset and emits
# ``SIG_APPLY_BUTTON_CLICKED`` which is already wired to
# ``apply_processing_parameters``.
editor.set()
[docs]
def apply_processing_parameters(
self,
obj: SignalObj | ImageObj | None = None,
interactive: bool = True,
param: gds.DataSet | None = None,
) -> ProcessingReport:
# pylint: disable=too-many-return-statements
"""Apply processing parameters by recomputing the existing object in place.
Args:
obj: Signal or Image object to reprocess. If None, uses the current object.
interactive: If True, show progress and error messages in the UI.
param: Explicit processing parameters to apply. When provided, this
takes precedence and makes the call independent of the Processing
tab editor state (used e.g. by programmatic recompute paths).
When None (default), fall back to the editor dataset or the
stored processing parameters.
Returns:
ProcessingReport with success status, object UUID, and optional message.
"""
# pylint: disable=import-outside-toplevel
from datalab.processingcall import grouped_recipe_index, resolve_processing_call
if execenv.unattended:
interactive = False
self.__auto_recompute_timer.stop()
editor = self.processing_param_editor
obj = obj or self.current_processing_obj
if obj is None:
return ProcessingReport(
success=False, message=_("No processing object available.")
)
proc_params = resolve_processing_call(self.panel.mainwindow, obj)
if proc_params is None:
return ProcessingReport(
success=False,
obj_uuid=get_uuid(obj),
message=_("Processing metadata is incomplete."),
)
# Resolve the parameters to apply. An explicit ``param`` argument takes
# precedence and makes this method independent of the editor state;
# otherwise fall back to the editor (interactive Apply) or the restored
# processing parameters.
if param is None:
if editor is not None and obj is self.current_processing_obj:
param = editor.dataset
else:
param = proc_params.param
hpanel = self.panel.mainwindow.historypanel
is_edit_mode = hpanel.is_edit_mode()
report = self.panel.processor.recompute_processing(
obj=obj,
param=param,
interactive=interactive,
call=proc_params,
)
if report.success:
if is_edit_mode:
# Propagate the edited param to the History panel:
# Mutate the matching existing action (snapshot originals
# first), refresh its tree display, then cascade recompute
# to downstream actions so the chain stays consistent with
# the new parameters. The action is the one whose last
# execution recorded the restored activity; the output-based
# lookup is kept for actions recorded without this link.
action = None
if proc_params.activity_id is not None:
action = hpanel.find_action_for_activity(proc_params.activity_id)
if action is None:
action = hpanel.find_action_for_output(
get_uuid(obj), proc_params.func_name
)
if action is not None:
action.snapshot_kwargs()
if action.pattern in ("1_to_n", "multiple_1_to_1"):
# A grouped command replays its list of parameters: the
# element of the edited output is updated, the others
# are kept (for every object processed by the command).
index = grouped_recipe_index(
self.panel.mainwindow, action, proc_params, get_uuid(obj)
)
params = list(action.kwargs.get("params") or [])
if index is not None and index < len(params):
params[index] = copy.deepcopy(param)
action.kwargs["params"] = params
else:
action.kwargs["param"] = copy.deepcopy(param)
self.__relink_action(action, proc_params.activity_id, obj)
hpanel.refresh_action(action)
hpanel.recompute_cascade(action)
def refresh_current_processing_tab() -> None:
if (
self.current_processing_obj is obj
and self.panel.objview.get_current_object() is obj
and self.panel.objmodel.has_uuid(get_uuid(obj))
):
self.__update_properties_dataset(obj)
self.update_original_values()
self.display_processing_history(obj)
self.setup_processing_tab(obj, reset_params=False, set_current=True)
QC.QTimer.singleShot(0, refresh_current_processing_tab)
return report
def __relink_action(
self, action: Any, previous: str | None, obj: SignalObj | ImageObj
) -> None:
"""Link an edited History action to the execution that realized it.
The re-application from Processing executed the edited action for this
output: its new activity replaces, in the action links, the activity it
supersedes (the other outputs of the action keep theirs).
"""
latest = self.panel.mainwindow.provenance.ledger.latest_state(get_uuid(obj))
current = None if latest is None else latest["produced_by"]
if current is None or current == previous:
return
links = list(action.provenance_activity_ids)
if previous in links:
links[links.index(previous)] = current
elif len(action.output_uuids) <= 1:
links = [current]
else:
# The superseded link is the one that produced this output (its
# activity may have been replaced since outside the edit mode)
index = self.__link_of_output(links, get_uuid(obj))
if index is None:
return
links[index] = current
action.provenance_activity_ids = links
def __link_of_output(self, links: list[str], object_uuid: str) -> int | None:
"""Return the position of the link whose activity produced an object."""
ledger = self.panel.mainwindow.provenance.ledger
for index, activity_id in enumerate(links):
try:
outputs = ledger.activity(activity_id)["outputs"]
except KeyError:
continue
for output in outputs:
state = ledger.states.get(output.get("state_id"))
if state is not None and state["object_uuid"] == object_uuid:
return index
return None
[docs]
def apply_recomputed_object_in_place(
self,
obj: SignalObj | ImageObj,
new_obj: SignalObj | ImageObj,
proc_params: ProcessingParameters,
) -> None:
"""Apply a freshly recomputed object onto ``obj`` in place.
Copies scientific data, coordinates, labels and units while preserving
metadata, annotations and display settings. Only processing metadata changes.
Args:
obj: Existing object to update in place (identity preserved).
new_obj: Freshly recomputed object providing title + data.
proc_params: Updated processing parameters to store on ``obj``.
"""
apply_processing_result(obj, new_obj, proc_params)
[docs]
@dataclass
class H5ImportBatch:
"""Objects and UUID remapping created by one native HDF5 import."""
panel: BaseDataPanel
reference_remap: dict[str, str]
objects: list[TypeObj]
groups: list[ObjectGroup]
[docs]
def apply_reference_remap(
self, reference_remap: dict[str, str], title_remap: dict[str, str]
) -> None:
"""Remap title and processing references in this imported batch.
Args:
reference_remap: serialized UUID to imported UUID mapping
title_remap: serialized title reference to imported title reference
"""
for group in self.groups:
group.title = remap_title_references(group.title, title_remap)
for obj in self.objects:
obj.title = remap_title_references(obj.title, title_remap)
for extract_parameters in (
extract_processing_parameters,
extract_analysis_parameters,
):
parameters = extract_parameters(obj)
if parameters is None:
continue
changed = False
if parameters.source_uuid in reference_remap:
parameters.source_uuid = reference_remap[parameters.source_uuid]
changed = True
if parameters.source_uuids is not None:
source_uuids = [
reference_remap.get(uuid, uuid)
for uuid in parameters.source_uuids
]
if source_uuids != parameters.source_uuids:
parameters.source_uuids = source_uuids
changed = True
if changed:
insert_processing_parameters(obj, parameters)
[docs]
@classmethod
def finalize_imports(
cls, batches: list[H5ImportBatch], refresh_panels: bool = False
) -> None:
"""Apply the combined signal/image UUID remapping to imported batches."""
reference_remap: dict[str, str] = {}
for batch in batches:
reference_remap.update(batch.reference_remap)
group_uuids = {get_uuid(group) for batch in batches for group in batch.groups}
title_remap: dict[str, str] = {}
for old_uuid, new_uuid in reference_remap.items():
if old_uuid != new_uuid:
prefix = "g" if new_uuid in group_uuids else ""
old_reference = prefix + get_short_uuid(old_uuid)
title_remap[old_reference] = prefix + get_short_uuid(new_uuid)
for batch in batches:
batch.apply_reference_remap(reference_remap, title_remap)
if refresh_panels:
for batch in batches:
batch.panel.refresh_after_reference_remap()
[docs]
class AbstractPanel(QW.QSplitter, metaclass=AbstractPanelMeta):
"""Object defining DataLab panel interface,
based on a vertical QSplitter widget
A panel handle an object list (objects are signals, images, macros...).
Each object must implement ``datalab.gui.ObjItf`` interface
"""
H5_PREFIX = ""
SIG_OBJECT_ADDED = QC.Signal()
SIG_OBJECT_REMOVED = QC.Signal()
SIG_OBJECT_MODIFIED = QC.Signal()
@abc.abstractmethod
def __init__(self, parent):
super().__init__(QC.Qt.Vertical, parent)
self.setObjectName(self.__class__.__name__[0].lower())
# Check if the class implements __len__, __getitem__ and __iter__
for method in ("__len__", "__getitem__", "__iter__"):
if not hasattr(self, method):
raise NotImplementedError(
f"Class {self.__class__.__name__} must implement method {method}"
)
# pylint: disable=unused-argument
[docs]
def get_serializable_name(self, obj: ObjItf) -> str:
"""Return serializable name of object"""
title = re.sub("[^-a-zA-Z0-9_.() ]+", "", obj.title.replace("/", "_"))
identifier = get_short_id(obj)
name = f"{identifier}: {title}"
return name
[docs]
def serialize_object_to_hdf5(self, obj: ObjItf, writer: NativeH5Writer) -> None:
"""Serialize object to HDF5 file"""
with writer.group(self.get_serializable_name(obj)):
obj.serialize(writer)
[docs]
def deserialize_object_from_hdf5(
self, reader: NativeH5Reader, name: str, reset_all: bool = False
) -> ObjItf:
"""Deserialize object from a HDF5 file
Args:
reader: HDF5 reader
name: Object name in HDF5 file
reset_all: If True, preserve original UUIDs (workspace reload).
If False, regenerate only UUIDs that conflict with existing
objects (object import).
"""
with reader.group(name):
obj = self.create_object()
obj.deserialize(reader)
# UUID collision handling is deferred until the complete imported batch
# can record old-to-new references for titles and processing metadata.
del reset_all
return obj
[docs]
@abc.abstractmethod
def serialize_to_hdf5(self, writer: NativeH5Writer) -> None:
"""Serialize whole panel to a HDF5 file"""
[docs]
@abc.abstractmethod
def deserialize_from_hdf5(
self, reader: NativeH5Reader, reset_all: bool = False
) -> H5ImportBatch | None:
"""Deserialize whole panel from a HDF5 file
Args:
reader: HDF5 reader
reset_all: If True, preserve original UUIDs (workspace reload).
If False, regenerate only UUIDs that conflict with existing
objects (object import).
"""
[docs]
@abc.abstractmethod
def create_object(self) -> ObjItf:
"""Create and return object"""
[docs]
@abc.abstractmethod
def add_object(self, obj: ObjItf) -> None:
"""Add object to panel"""
[docs]
@abc.abstractmethod
def remove_all_objects(self):
"""Remove all objects"""
self.SIG_OBJECT_REMOVED.emit()
[docs]
class NonModalInfoDialog(QW.QMessageBox):
"""Non-modal information message box with selectable text.
This widget displays an information message in a message dialog box, allowing users
to select and copy the text content.
"""
def __init__(self, parent: QW.QWidget, title: str, text: str) -> None:
"""Create a non-modal information message box with selectable text.
Args:
parent: The parent widget.
title: The title of the message box.
text: The text to display in the message box.
"""
super().__init__(parent)
self.setIcon(QW.QMessageBox.Information)
self.setWindowTitle(title)
if re.search(r"<[a-zA-Z/][^>]*>", text):
self.setTextFormat(QC.Qt.RichText) # type: ignore[attr-defined]
self.setTextInteractionFlags(
QC.Qt.TextBrowserInteraction # type: ignore[attr-defined]
)
else:
self.setTextFormat(QC.Qt.PlainText) # type: ignore[attr-defined]
self.setTextInteractionFlags(
QC.Qt.TextSelectableByMouse # type: ignore[attr-defined]
| QC.Qt.TextSelectableByKeyboard # type: ignore[attr-defined]
)
self.setText(text)
self.setStandardButtons(QW.QMessageBox.Close)
self.setDefaultButton(QW.QMessageBox.Close)
# ! Necessary only on non-Windows platforms
self.setWindowFlags(QC.Qt.Window) # type: ignore[attr-defined]
self.setModal(False)
[docs]
class SaveToDirectoryGUIParam(gds.DataSet, title=_("Save to directory")):
"""Save to directory parameters"""
def __init__(
self,
objs: list[TypeObj] | None = None,
extensions: list[str] | None = None,
labels: list[str] | None = None,
) -> None:
super().__init__()
self.__objs = objs or []
self.__extensions = extensions or []
self.__labels = labels or [obj.title for obj in self.__objs]
[docs]
def get_extension_choices(self, _item=None, _value=None):
"""Return list of available extensions for choice item."""
return [("." + ext, "." + ext, None) for ext in self.__extensions]
[docs]
def build_filenames(self, objs: list[TypeObj] | None = None) -> list[str]:
"""Build filenames according to current parameters."""
objs = objs or self.__objs
extension = self.extension if self.extension is not None else ""
filenames = format_basenames(objs, self.basename + extension)
used: set[str] = set() # Ensure all filenames are unique.
for i, filename in enumerate(filenames):
root, ext = osp.splitext(filename)
filepath = osp.join(self.directory, filename)
k = 1
while (filename in used) or (not self.overwrite and osp.exists(filepath)):
filename = f"{root}_{k}{ext}"
filepath = osp.join(self.directory, filename)
k += 1
used.add(filename)
filenames[i] = filename
return filenames
[docs]
def generate_filepath_obj_pairs(
self, objs: list[TypeObj]
) -> Generator[tuple[str, TypeObj], None, None]:
"""Iterate over (filepath, object) pairs to be saved."""
for filename, obj in zip(self.build_filenames(objs), objs):
yield osp.join(self.directory, filename), obj
[docs]
def update_preview(self, _item=None, _value=None) -> None:
"""Update preview."""
try:
filenames = self.build_filenames()
preview_lines = [
f"{label}: {filename}"
for label, filename in zip(self.__labels, filenames)
]
self.preview = "\n".join(preview_lines)
except (ValueError, KeyError, TypeError) as exc:
# Handle formatting errors gracefully (e.g., incomplete format string)
self.preview = f"Invalid pattern:{os.linesep}{exc}"
directory = gds.DirectoryItem(_("Directory"), default=Conf.base_dir.get())
basename = gds.StringItem(
_("Basename pattern"),
default="{title}",
help=_("Python format string. See description for details."),
).set_prop("display", callback=update_preview)
help = gds.ButtonItem(_("Help"), on_button_click, "MessageBoxInformation").set_pos(
col=1
)
extension = gds.ChoiceItem(_("Extension"), get_extension_choices).set_prop(
"display", callback=update_preview
)
overwrite = gds.BoolItem(
_("Overwrite"), default=False, help=_("Overwrite existing files")
).set_pos(col=1)
preview = gds.TextItem(
_("Preview"), default=None, regexp=r"^(?!Invalid).*"
).set_prop("display", readonly=True)
[docs]
class BaseDataPanel(AbstractPanel, Generic[TypeObj, TypeROI, TypeROIEditor]):
"""Object handling the item list, the selected item properties and plot"""
PANEL_STR = "" # e.g. "Signal Panel"
PANEL_STR_ID = "" # e.g. "signal"
PARAMCLASS: TypeObj = None # Replaced in child object
ANNOTATION_TOOLS = ()
MINDIALOGSIZE = (800, 600)
MAXDIALOGSIZE = 0.95 # % of DataLab's main window size
# Replaced by the right class in child object:
IO_REGISTRY: SignalIORegistry | ImageIORegistry | None = None
SIG_STATUS_MESSAGE = QC.Signal(str, int) # emitted by "qt_try_except" decorator
SIG_REFRESH_PLOT = QC.Signal(
str, bool, bool, bool, bool
) # Connected to PlotHandler.refresh_plot
[docs]
@staticmethod
@abc.abstractmethod
def get_roi_class() -> Type[TypeROI]:
"""Return ROI class"""
[docs]
@staticmethod
@abc.abstractmethod
def get_roieditor_class() -> Type[TypeROIEditor]:
"""Return ROI editor class"""
@abc.abstractmethod
def __init__(self, parent: QW.QWidget) -> None:
super().__init__(parent)
self.mainwindow: DLMainWindow = parent
self.objprop = ObjectProp(self, self.PARAMCLASS)
self.objmodel = objectmodel.ObjectModel(f"g{self.PARAMCLASS.PREFIX}")
self.objview = objectview.ObjectView(self, self.objmodel)
self.objview.SIG_IMPORT_FILES.connect(self.handle_dropped_files)
self.objview.populate_tree()
self.plothandler: SignalPlotHandler | ImagePlotHandler = None
self.processor: SignalProcessor | ImageProcessor = None
self.acthandler: actionhandler.BaseActionHandler = None
self.metadata_clipboard = {}
self.annotations_clipboard: list[dict[str, Any]] = []
self.__roi_clipboard: TypeROI | None = None
self.context_menu = QW.QMenu()
self.__separate_views: dict[QW.QDialog, TypeObj] = {}
[docs]
def closeEvent(self, event):
"""Reimplement QMainWindow method"""
self.processor.close()
super().closeEvent(event)
# ------AbstractPanel interface-----------------------------------------------------
[docs]
def plot_item_parameters_changed(
self, item: CurveItem | MaskedXYImageItem | LabelItem
) -> None:
"""Plot items changed: update metadata of all objects from plot items"""
# Find the object corresponding to the plot item
obj = self.plothandler.get_obj_from_item(item)
if obj is not None:
# Unselect the item in the plot so that we update the item parameters
# in the right state (fix issue #184):
item.unselect()
# Ensure that item's parameters are up-to-date:
item.param.update_param(item)
# Update object metadata from plot item parameters
create_adapter_from_object(obj).update_metadata_from_plot_item(item)
if obj is self.objview.get_current_object():
self.objprop.update_properties_from(obj)
self.plothandler.update_resultproperty_from_plot_item(item)
# pylint: disable=unused-argument
[docs]
def plot_item_moved(
self, item: LabelItem, x0: float, y0: float, x1: float, y1: float
) -> None:
"""Plot item moved: update metadata of all objects from plot items
Args:
item: Plot item
x0: new x0 coordinate
y0: new y0 coordinate
x1: new x1 coordinate
y1: new y1 coordinate
"""
self.plothandler.update_resultproperty_from_plot_item(item)
[docs]
def serialize_object_to_hdf5(self, obj: TypeObj, writer: NativeH5Writer) -> None:
"""Serialize object to HDF5 file"""
# Before serializing, update metadata from plot item parameters, in order to
# save the latest visualization settings:
try:
item = self.plothandler[get_uuid(obj)]
create_adapter_from_object(obj).update_metadata_from_plot_item(item)
except KeyError:
# Plot item has not been created yet (this happens when auto-refresh has
# been disabled)
pass
super().serialize_object_to_hdf5(obj, writer)
[docs]
def serialize_to_hdf5(self, writer: NativeH5Writer) -> None:
"""Serialize whole panel to a HDF5 file"""
with writer.group(self.H5_PREFIX):
for group in self.objmodel.get_groups():
with writer.group(self.get_serializable_name(group)):
with writer.group("title"):
writer.write_str(group.title)
with writer.group("uuid"):
writer.write_str(get_uuid(group))
for obj in group.get_objects():
self.serialize_object_to_hdf5(obj, writer)
[docs]
def deserialize_from_hdf5(
self, reader: NativeH5Reader, reset_all: bool = False
) -> H5ImportBatch:
"""Deserialize whole panel from a HDF5 file
Args:
reader: HDF5 reader
reset_all: If True, preserve original UUIDs (workspace reload).
If False, regenerate only UUIDs that conflict with existing
objects (object import).
"""
batch = H5ImportBatch(self, {}, [], [])
objects_added = False
signals_blocked = self.blockSignals(True)
try:
with reader.group(self.H5_PREFIX):
for name in reader.h5.get(self.H5_PREFIX, []):
with reader.group(name):
with reader.group("title"):
group_title = reader.read_str()
serialized_group_uuid = reader.read("uuid", default=None)
if not isinstance(serialized_group_uuid, str):
serialized_group_uuid = None
group_uuid = None
if serialized_group_uuid and (
reset_all
or not self.objmodel.has_uuid(serialized_group_uuid)
):
group_uuid = serialized_group_uuid
group = self.add_group(group_title, group_uuid=group_uuid)
if serialized_group_uuid:
batch.reference_remap[serialized_group_uuid] = get_uuid(
group
)
batch.groups.append(group)
objects: list[TypeObj] = []
for obj_name in reader.h5.get(f"{self.H5_PREFIX}/{name}", []):
obj = self.deserialize_object_from_hdf5(
reader, obj_name, reset_all
)
serialized_obj_uuid = obj.metadata.get("__uuid")
if (
not isinstance(serialized_obj_uuid, str)
or not serialized_obj_uuid
):
serialized_obj_uuid = None
if serialized_obj_uuid is None or (
not reset_all
and self.objmodel.has_uuid(serialized_obj_uuid)
):
set_uuid(obj)
if serialized_obj_uuid is not None:
batch.reference_remap[serialized_obj_uuid] = get_uuid(
obj
)
objects.append(obj)
self._add_objects(objects, get_uuid(group), set_current=False)
batch.objects.extend(objects)
objects_added = objects_added or bool(objects)
finally:
self.blockSignals(signals_blocked)
if objects_added:
self.SIG_OBJECT_ADDED.emit()
if not objects_added:
self.selection_changed()
return batch
def __len__(self) -> int:
"""Return number of objects"""
return len(self.objmodel)
def __getitem__(self, nb: int) -> TypeObj:
"""Return object from its number (1 to N)"""
return self.objmodel.get_object_from_number(nb)
def __iter__(self):
"""Iterate over objects"""
return iter(self.objmodel)
[docs]
def create_object(self) -> TypeObj:
"""Create object (signal or image)
Returns:
SignalObj or ImageObj object
"""
return self.PARAMCLASS() # pylint: disable=not-callable
[docs]
@qt_try_except()
def add_object(
self,
obj: TypeObj,
group_id: str | None = None,
set_current: bool = True,
) -> None:
"""Add object
Args:
obj: SignalObj or ImageObj object
group_id: group id to which the object belongs. If None or empty string,
the object is added to the current group.
set_current: if True, set the added object as current
"""
self._add_object(obj, group_id, set_current)
[docs]
@qt_try_except()
def add_objects(
self,
objects: Sequence[TypeObj],
group_id: str | None = None,
set_current: bool = True,
) -> None:
"""Add multiple objects atomically.
Args:
objects: Objects to add, in insertion order
group_id: Group ID to which the objects belong. If None or empty,
the objects are added to the current group.
set_current: If True, set the last added object as current
"""
self._add_objects(objects, group_id, set_current)
def _add_object(
self,
obj: TypeObj,
group_id: str | None = None,
set_current: bool = True,
) -> None:
"""Add an object while propagating errors to transactional callers."""
self._add_objects((obj,), group_id, set_current)
def _add_objects(
self,
objects: Sequence[TypeObj],
group_id: str | None = None,
set_current: bool = True,
) -> None:
"""Add objects atomically while propagating transactional errors."""
objects = tuple(objects)
if not objects:
return
object_ids: set[str] = set()
for obj in objects:
obj_uuid = get_uuid(obj)
if obj in self.objmodel or obj_uuid in object_ids:
raise ValueError(
f"Object {hex(id(obj))} already in panel. "
f"The same object cannot be added twice: "
f"please use a copy of the object."
)
object_ids.add(obj_uuid)
obj.check_data()
created_group: ObjectGroup | None = None
if group_id is None or group_id == "":
group_id = self.objview.get_current_group_id()
if group_id is None:
groups = self.objmodel.get_groups()
if groups:
group_id = get_uuid(groups[0])
else:
created_group = self.add_group("")
group_id = get_uuid(created_group)
else:
self.objmodel.get_group(group_id)
current_item_id = self.objview.get_current_item_id()
transient_attributes = (
"newly_created_obj_uuid",
"fresh_processing_obj_uuid",
"fresh_analysis_obj_uuid",
)
transient_state = {
name: getattr(self.objprop, name) for name in transient_attributes
}
added_to_model = False
try:
self.objmodel.add_objects(objects, group_id)
added_to_model = True
for obj in objects:
# Don't overwrite fresh processing or analysis state: those tabs take
# precedence over the Creation tab on first selection.
obj_uuid = get_uuid(obj)
if obj_uuid not in (
self.objprop.fresh_processing_obj_uuid,
self.objprop.fresh_analysis_obj_uuid,
):
self.objprop.mark_as_newly_created(obj)
self.objview.add_object_items(
objects,
group_id,
set_current=set_current,
)
# Emit signal to ensure that the data panel is shown in the main window and
# that the plot is updated (trigger a refresh of the plot)
self.SIG_OBJECT_ADDED.emit()
except Exception:
if added_to_model:
for obj in reversed(objects):
self._remove_added_object(obj)
if created_group is not None:
self.objview.remove_item(get_uuid(created_group), refresh=False)
if created_group in self.objmodel.get_groups():
self.objmodel.remove_group(created_group)
for name, value in transient_state.items():
setattr(self.objprop, name, value)
signals_blocked = self.objview.blockSignals(True)
try:
if current_item_id is None:
self.objview.clearSelection()
self.objview.setCurrentItem(None)
elif self.objview.get_item_from_id(current_item_id) is not None:
self.objview.set_current_item_id(current_item_id)
finally:
self.objview.blockSignals(signals_blocked)
self.objview.update_tree()
raise
def _remove_added_object(self, obj: TypeObj) -> None:
"""Remove a specifically identified object after a failed transaction."""
obj_uuid = get_uuid(obj)
with ExitStack() as cleanup:
cleanup.callback(self._clear_added_object_state, obj_uuid)
if obj in self.objmodel:
cleanup.callback(self.objmodel.remove_object, obj)
cleanup.callback(self.objview.remove_item, obj_uuid, refresh=False)
cleanup.callback(self.plothandler.remove_item, obj_uuid)
def _clear_added_object_state(self, obj_uuid: str) -> None:
"""Clear transient property-panel references to a removed object."""
for attr_name in (
"newly_created_obj_uuid",
"fresh_processing_obj_uuid",
"fresh_analysis_obj_uuid",
):
if getattr(self.objprop, attr_name) == obj_uuid:
setattr(self.objprop, attr_name, None)
[docs]
def set_object(self, obj: TypeObj) -> None:
"""Update an existing object in-place with data from ``obj``.
The existing object is identified by UUID (carried by ``obj`` from a
previous :meth:`get_object` call). All data attributes are copied from
``obj`` to the existing object while preserving internal metadata
(number, group membership).
Args:
obj: SignalObj or ImageObj with the same UUID as an existing object.
Raises:
KeyError: if no object with matching UUID is found
"""
obj_uuid = get_uuid(obj)
existing = self.objmodel[obj_uuid] # KeyError if not found
# Preserve internal number metadata before overwriting
number = get_number(existing)
# Copy all public DataSet item values from obj to existing.
# Skip computed (read-only) items such as ImageObj.xmin/xmax/ymin/ymax,
# which would raise ValueError on assignment (see Issue #305).
for item in existing.get_items():
name = item.get_name()
if name.startswith("_"):
continue
if isinstance(item.get_prop("data", "computed", None), ComputedProp):
continue
setattr(existing, name, getattr(obj, name))
set_number(existing, number)
self.objview.update_tree()
# Refresh the object properties panel so updated values are shown
# immediately if the modified object is currently selected.
self.objview.item_selection_changed()
self.refresh_plot("selected", update_items=True, force=True)
self.SIG_OBJECT_MODIFIED.emit()
[docs]
def remove_all_objects(self) -> None:
"""Remove all objects"""
# iterate over a copy of self.__separate_views dict keys to avoid RuntimeError:
# dictionary changed size during iteration
for dlg in list(self.__separate_views):
dlg.done(QW.QDialog.DialogCode.Rejected)
self.objmodel.clear()
self.plothandler.clear()
self.objview.populate_tree()
self.refresh_plot("selected", True, False)
super().remove_all_objects()
# Update object properties panel to clear creation/processing tabs
self.selection_changed()
# ---- Signal/Image Panel API ------------------------------------------------------
[docs]
def setup_panel(self) -> None:
"""Setup panel"""
self.acthandler.create_all_actions()
self.processor.SIG_ADD_SHAPE.connect(self.plothandler.add_shapes)
self.SIG_REFRESH_PLOT.connect(self.plothandler.refresh_plot)
self.objview.SIG_SELECTION_CHANGED.connect(self.selection_changed)
self.objview.SIG_ITEM_DOUBLECLICKED.connect(
lambda oid: self.open_separate_view([oid])
)
self.objview.SIG_CONTEXT_MENU.connect(self.__popup_contextmenu)
self.objview.SIG_DELETE_REQUESTED.connect(self.__delete_from_button)
self.objprop.properties.SIG_APPLY_BUTTON_CLICKED.connect(
self.properties_changed
)
self.addWidget(self.objview)
self.addWidget(self.objprop)
[docs]
def refresh_plot(
self,
what: str,
update_items: bool = True,
force: bool = False,
only_visible: bool = True,
only_existing: bool = False,
) -> None:
"""Refresh plot.
Args:
what: string describing the objects to refresh.
Valid values are "selected" (refresh the selected objects),
"all" (refresh all objects), "existing" (refresh existing plot items),
or an object uuid.
update_items: if True, update the items.
If False, only show the items (do not update them).
Defaults to True.
force: if True, force refresh even if auto refresh is disabled.
Defaults to False.
only_visible: if True, only refresh visible items. Defaults to True.
Visible items are the ones that are not hidden by other items or the items
except the first one if the option "Show first only" is enabled.
This is useful for images, where the last image is the one that is shown.
If False, all items are refreshed.
only_existing: if True, only refresh existing items. Defaults to False.
Existing items are the ones that have already been created and are
associated to the object uuid. If False, create new items for the
objects that do not have an item yet.
Raises:
ValueError: if `what` is not a valid value
"""
if what not in ("selected", "all", "existing") and not isinstance(what, str):
raise ValueError(f"Invalid value for 'what': {what}")
self.SIG_REFRESH_PLOT.emit(
what, update_items, force, only_visible, only_existing
)
[docs]
def manual_refresh(self) -> None:
"""Manual refresh"""
self.refresh_plot("selected", True, True)
[docs]
def get_category_actions(
self, category: actionhandler.ActionCategory
) -> list[QW.QAction]: # pragma: no cover
"""Return actions for category"""
return self.acthandler.feature_actions.get(category, [])
def __popup_contextmenu(self, position: QC.QPoint) -> None: # pragma: no cover
"""Popup context menu at position"""
menu = self.get_context_menu()
menu.popup(position)
# ------Creating, adding, removing objects------------------------------------------
[docs]
def add_group(
self,
title: str,
select: bool = False,
group_uuid: str | None = None,
) -> objectmodel.ObjectGroup:
"""Add group
Args:
title: group title
select: if True, select the group in the tree view. Defaults to False.
group_uuid: optional group UUID. If None, a new UUID is generated.
Returns:
Created group object
"""
group = self.objmodel.add_group(title, group_uuid)
try:
self.objview.add_group_item(group)
if select:
self.objview.select_groups([group])
except Exception:
self.objview.remove_item(get_uuid(group), refresh=False)
self.objmodel.remove_group(group)
raise
return group
def __duplicate_individual_obj(
self, oid: str, new_group_id: str | None = None, set_current: bool = True
) -> None:
"""Duplicate individual object"""
obj = self.objmodel[oid]
if new_group_id is None:
new_group_id = self.objmodel.get_object_group_id(obj)
self.add_object(obj.copy(), group_id=new_group_id, set_current=set_current)
[docs]
def duplicate_object(self) -> None:
"""Duplication signal/image object"""
if not self.mainwindow.confirm_memory_state():
return
action = self.mainwindow.historypanel.add_ui_entry(
_("Duplicate object or group"),
target=self.PANEL_STR_ID + "panel",
method_name="duplicate_object",
save_state=False,
)
with self.mainwindow.historypanel.capture_outputs(action):
# Duplicate individual objects (exclusive with respect to groups)
for oid in self.objview.get_sel_object_uuids():
self.__duplicate_individual_obj(oid, set_current=False)
# Duplicate groups (exclusive with respect to individual objects)
for group in self.objview.get_sel_groups():
new_group = self.add_group(group.title)
for oid in self.objmodel.get_group_object_ids(get_uuid(group)):
self.__duplicate_individual_obj(
oid, get_uuid(new_group), set_current=False
)
self.selection_changed(update_items=True)
def _rename_results_in_clipboard(self, prefix: str) -> None:
"""Rename geometry and table results in clipboard to avoid conflicts.
Args:
prefix: Prefix to add to result titles
"""
for aclass in (GeometryAdapter, TableAdapter):
result_keys = [
k for k, v in self.metadata_clipboard.items() if aclass.match(k, v)
]
for dict_key in result_keys:
try:
# Get the result data
result_data = self.metadata_clipboard[dict_key]
# Update the title in the result data
if isinstance(result_data, dict) and "title" in result_data:
result_data = result_data.copy() # Don't modify original
result_data["title"] = prefix + result_data["title"]
# Create new key with updated title
new_dict_key = dict_key.replace(
aclass.META_PREFIX, aclass.META_PREFIX + prefix, 1
)
# Remove old entry and add new one
del self.metadata_clipboard[dict_key]
self.metadata_clipboard[new_dict_key] = result_data
except (KeyError, ValueError, IndexError, TypeError):
# If we can't process this result, leave it as is
continue
[docs]
def copy_roi(self, roi_data=None) -> None:
"""Copy regions of interest
Args:
roi_data: ROI snapshot kept for legacy session replay compatibility.
When ``None`` (interactive use), the ROI is read from the
currently selected object.
"""
# Copying to the clipboard mutates nothing: no history entry is
# recorded (the paste operation records the resulting ROI mutation).
if roi_data is None:
obj = self.objview.get_sel_objects()[0]
if obj.roi is None:
return
roi_data = obj.roi.copy()
self.__roi_clipboard = roi_data.copy()
[docs]
def paste_roi(self, roi_data=None) -> None:
"""Paste regions of interest
Args:
roi_data: ROI snapshot kept for legacy session replay compatibility.
When ``None`` (interactive use), the clipboard populated by
:meth:`copy_roi` is used.
"""
if roi_data is None:
roi_data = self.__roi_clipboard
if roi_data is None:
return
sel_objects = self.objview.get_sel_objects(include_groups=True)
title = _("Paste regions of interest into selected %s") % (
_("signal") if self.PANEL_STR_ID == "signal" else _("image")
)
for obj in sel_objects:
if obj.roi is None:
obj.roi = roi_data.copy()
else:
obj.roi = obj.roi.combine_with(roi_data)
# Pasting combines with any existing ROI, whereas mutation replay
# replaces the target's ROI: record one entry per object with the
# post-combination ROI so replay is deterministic.
self.mainwindow.historypanel.add_mutation_entry(
title,
panel_str=self.PANEL_STR_ID,
mutation_key="roi",
target_uuids=[get_uuid(obj)],
payload=obj.roi,
)
self.selection_changed(update_items=True)
self.refresh_plot(
"selected", update_items=True, only_visible=False, only_existing=True
)
[docs]
def remove_object(self, force: bool = False) -> None:
"""Remove signal/image object
Args:
force: if True, remove object without confirmation. Defaults to False.
"""
sel_groups = self.objview.get_sel_groups()
if sel_groups and not force and not execenv.unattended:
answer = QW.QMessageBox.warning(
self,
_("Delete group(s)"),
_("Are you sure you want to delete the selected group(s)?"),
QW.QMessageBox.Yes | QW.QMessageBox.No,
)
if answer == QW.QMessageBox.No:
return
self.__remove_selection(force)
def __remove_selection(
self, force: bool, restored_selection: Sequence[str] = ()
) -> None:
"""Remove the selected objects and groups
Args:
force: ``force`` argument of :meth:`remove_object`, recorded in history
restored_selection: UUIDs of the remaining objects or groups to select
after removal (the last one becoming current). If empty, the selection
is left to the object view.
"""
# IMPORTANT: save_state=True is required so that the selection of objects
# being deleted is captured. On replay, the captured selection is restored
# before remove_object runs, ensuring that the correct object is removed
# instead of whatever is currently selected.
self.mainwindow.historypanel.add_ui_entry(
_("Remove selected objects"),
target=self.PANEL_STR_ID + "panel",
method_name="remove_object",
save_state=True,
force=force,
)
sel_groups = self.objview.get_sel_groups()
sel_objects = self.objview.get_sel_objects(include_groups=True)
sel_objects = sorted(sel_objects, key=get_short_id, reverse=True)
for obj in sel_objects:
dlg_list: list[QW.QDialog] = []
for dlg, obj_i in self.__separate_views.items():
if obj_i is obj:
dlg_list.append(dlg)
for dlg in dlg_list:
dlg.done(QW.QDialog.DialogCode.Rejected)
# Bulk removal: the plot and the numbering are updated once, not per object
uuids = [get_uuid(obj) for obj in sel_objects]
self.plothandler.remove_items(uuids)
self.objview.remove_items(uuids, refresh=False)
self.objmodel.remove_objects(sel_objects)
for group in sel_groups:
self.objview.remove_item(get_uuid(group), refresh=False)
self.objmodel.remove_group(group)
self.objview.update_tree()
if restored_selection:
remaining = set(self.objmodel.get_object_ids())
remaining.update(get_uuid(group) for group in self.objmodel.get_groups())
self.objview.select_uuids(
[uuid for uuid in restored_selection if uuid in remaining],
notify=False,
)
self.selection_changed(update_items=True)
self.SIG_OBJECT_REMOVED.emit()
def __confirm_deletion(self, uuids: Sequence[str]) -> bool:
"""Ask the user to confirm the deletion of objects or groups
Args:
uuids: UUIDs of the objects, or of the groups, to delete
Returns:
True if the deletion is confirmed
"""
items = [self.objmodel.get_object_or_group(uuid) for uuid in uuids]
if len(items) > 1:
if isinstance(items[0], ObjectGroup):
message = _(
"Do you want to delete the %d selected groups and their objects?"
) % len(items)
else:
message = _("Do you want to delete the %d selected objects?") % len(
items
)
elif isinstance(items[0], ObjectGroup):
message = _('Do you want to delete group "%s" and its %d object(s)?') % (
items[0].title,
len(items[0]),
)
else:
message = _('Do you want to delete "%s"?') % items[0].title
answer = QW.QMessageBox.warning(
self, _("Delete"), message, QW.QMessageBox.Yes | QW.QMessageBox.No
)
return answer == QW.QMessageBox.Yes
def __delete_from_button(self, uuid: str) -> None:
"""Delete objects or groups with the delete button of an item of the
object view, after confirmation
If the item is part of the selection, the whole selection is deleted.
Otherwise, only the item is deleted, and the selection is preserved.
Args:
uuid: UUID of the item whose delete button was clicked
"""
targets = self.objview.get_delete_targets(uuid)
if not execenv.unattended and not self.__confirm_deletion(targets):
return
selection = self.objview.get_sel_object_uuids()
selection += self.objview.get_sel_group_uuids()
restored_selection: list[str] = []
if uuid not in selection:
current = self.objview.get_current_item_id()
restored_selection = [oid for oid in selection if oid != current]
if current in selection:
restored_selection.append(current)
# Selecting the item records it in history as the removed object
self.objview.select_uuids([uuid], notify=False)
self.__remove_selection(force=True, restored_selection=restored_selection)
[docs]
def delete_all_objects(self) -> None: # pragma: no cover
"""Confirm before removing all objects"""
if len(self) == 0:
return
if execenv.unattended:
raise RuntimeError(
"This method should not be executed in unattended mode: "
"call remove_all_objects instead."
)
answer = QW.QMessageBox.warning(
self,
_("Delete all"),
_("Do you want to delete all objects (%s)?") % self.PANEL_STR,
QW.QMessageBox.Yes | QW.QMessageBox.No,
)
if answer == QW.QMessageBox.Yes:
self.remove_all_objects()
[docs]
def add_annotations_from_items(
self, items: list, refresh_plot: bool = True
) -> None:
"""Add object annotations (annotation plot items).
Args:
items: annotation plot items
refresh_plot: refresh plot. Defaults to True.
"""
for obj in self.objview.get_sel_objects(include_groups=True):
create_adapter_from_object(obj).add_annotations_from_items(items)
if refresh_plot:
self.refresh_plot("selected", True, False)
[docs]
def copy_titles_to_clipboard(self) -> None:
"""Copy object titles to clipboard (for reproducibility)"""
QW.QApplication.clipboard().setText(str(self.objview))
[docs]
def new_group(self) -> None:
"""Create a new group"""
# Open a message box to enter the group name
group_name, ok = QW.QInputDialog.getText(self, _("New group"), _("Group name:"))
if ok:
self.mainwindow.historypanel.add_ui_entry(
_('New group "%s"') % group_name,
target=self.PANEL_STR_ID + "panel",
method_name="add_group",
save_state=False,
title=group_name,
select=False,
)
self.add_group(group_name)
[docs]
def rename_selected_object_or_group(self, new_name: str | None = None) -> None:
"""Rename selected object or group
Args:
new_name: new name (default: None, i.e. ask user)
"""
self.mainwindow.historypanel.add_ui_entry(
_("Rename selected object or group"),
target=self.PANEL_STR_ID + "panel",
method_name="rename_selected_object_or_group",
save_state=False,
new_name=new_name,
)
sel_objects = self.objview.get_sel_objects(include_groups=False)
sel_groups = self.objview.get_sel_groups()
if (not sel_objects and not sel_groups) or len(sel_objects) + len(
sel_groups
) > 1:
# Won't happen in the application, but could happen in tests or using the
# API directly
raise ValueError("Select one object or group to rename")
if sel_objects:
obj = sel_objects[0]
if new_name is None:
new_name, ok = QW.QInputDialog.getText(
self,
_("Rename object"),
_("Object name:"),
QW.QLineEdit.Normal,
obj.title,
)
if not ok:
return
obj.title = new_name
self.objview.update_item(get_uuid(obj))
self.objprop.update_properties_from(obj)
elif sel_groups:
group = sel_groups[0]
if new_name is None:
new_name, ok = QW.QInputDialog.getText(
self,
_("Rename group"),
_("Group name:"),
QW.QLineEdit.Normal,
group.title,
)
if not ok:
return
group.title = new_name
self.objview.update_item(get_uuid(group))
self.SIG_OBJECT_MODIFIED.emit()
[docs]
@abc.abstractmethod
def get_newparam_from_current(
self, newparam: NewSignalParam | NewImageParam | None = None
) -> NewSignalParam | NewImageParam | None:
"""Get new object parameters from the current object.
Args:
newparam: new object parameters. If None, create a new one.
Returns:
New object parameters
"""
[docs]
@abc.abstractmethod
def new_object(
self,
param: NewSignalParam | NewImageParam | None = None,
edit: bool = False,
add_to_panel: bool = True,
) -> TypeObj | None:
"""Create a new object (signal/image).
Args:
param: new object parameters
edit: Open a dialog box to edit parameters (default: False).
When False, the object is created with default parameters and creation
parameters are stored in metadata for interactive editing.
add_to_panel: Add object to panel (default: True)
Returns:
New object
"""
[docs]
def set_current_object_title(self, title: str) -> None:
"""Set current object title"""
obj = self.objview.get_current_object()
obj.title = title
self.objview.update_item(get_uuid(obj))
self.mainwindow.historypanel.add_ui_entry(
_('Set current object title to "%s"') % title,
target=self.PANEL_STR_ID + "panel",
method_name="set_current_object_title",
save_state=False,
title=title,
)
def __load_from_file(
self,
filename: str,
create_group: bool = True,
add_objects: bool = True,
import_param: gds.DataSet | None = None,
) -> list[SignalObj] | list[ImageObj]:
"""Open and return objects from file, optionally adding them to DataLab.
Args:
filename: file name
create_group: if True, create a new group if more than one object is loaded.
Defaults to True. If False, all objects are added to the current group.
add_objects: if True, add objects to the panel. Defaults to True.
import_param: Optional format-specific import parameters.
Returns:
New object or list of new objects
"""
def read_objects(worker):
if import_param is None:
return self.IO_REGISTRY.read(filename, worker)
return self.IO_REGISTRY.read(filename, worker, param=import_param)
worker = CallbackWorker(read_objects)
objs = qt_long_callback(self, _("Reading objects from file"), worker, True)
self.apply_import_parameters(objs, import_param)
group_id = None
if len(objs) > 1 and create_group:
# Create a new group if more than one object is loaded
group_id = get_uuid(self.add_group(osp.basename(filename)))
with create_progress_bar(
self, _("Adding objects to workspace"), max_=len(objs) - 1
) as progress:
for i_obj, obj in enumerate(objs):
progress.setValue(i_obj + 1)
if progress.wasCanceled():
break
if add_objects:
set_uuid(obj) # In case the object UUID was serialized in the file,
# we need to reset it to a new UUID to avoid conflicts
# (e.g. HDF5 file)
self.add_object(obj, group_id=group_id, set_current=obj is objs[-1])
self.selection_changed()
return objs
[docs]
def edit_import_parameters(
self, filenames: list[str], param: gds.DataSet | None = None
) -> list[tuple[str, gds.DataSet | None]] | None:
"""Return files to import with their optional format-specific parameters.
Args:
filenames: File names to import.
param: Optional import parameters applied to every file requiring
parameters, without showing any dialog.
Returns:
File names with aligned import parameters, without the files whose
parameter dialog was cancelled, or None if the whole import was
cancelled.
Raises:
TypeError: If ``param`` is given, as this panel has no import parameters.
"""
if param is not None:
raise TypeError(
f"{self.__class__.__name__} does not support import parameters"
)
return [(fname, None) for fname in filenames]
[docs]
def apply_import_parameters(
self,
objs: list[SignalObj] | list[ImageObj],
param: gds.DataSet | None,
) -> None:
"""Apply optional import parameters to newly read objects.
Args:
objs: Imported objects.
param: Optional format-specific import parameters.
"""
del objs, param
[docs]
def edit_export_parameters(
self, obj: TypeObj, filename: str
) -> tuple[bool, ImageExportParam | None]:
"""Edit optional object export parameters.
Args:
obj: Object to export
filename: File name
Returns:
A tuple containing the confirmation state and optional export parameters
"""
return True, None
[docs]
def write_object_to_file(
self,
obj: TypeObj,
filename: str,
param: ImageExportParam | None = None,
) -> None:
"""Save object to file (signal/image).
Args:
obj: object
filename: file name
param: optional image export parameters
"""
del param
self.IO_REGISTRY.write(filename, obj)
[docs]
def load_from_directory(
self,
directory: str | None = None,
import_filenames: list[str] | None = None,
import_params: list[gds.DataSet] | None = None,
param: gds.DataSet | None = None,
skipped_filenames: list[str] | None = None,
) -> list[TypeObj]:
"""Open objects from directory (signals or images, depending on the panel),
add them to DataLab and return them.
If the directory is not specified, ask the user to select a directory.
Args:
directory: directory name
import_filenames: Optional file names, relative to ``directory``, of
the files imported with format-specific parameters. Recorded by
history so that replay does not reopen import dialogs.
import_params: Optional format-specific parameters aligned with
``import_filenames``.
param: Optional format-specific parameters applied to every file
requiring import parameters (e.g. RAW images), without showing any
dialog.
skipped_filenames: Optional file names, relative to ``directory``, of
the files whose import dialog was cancelled. Recorded by history so
that replay skips them without reopening their dialog.
Returns:
list of new objects
"""
if not self.mainwindow.confirm_memory_state():
return []
if directory is None: # pragma: no cover
basedir = Conf.base_dir.get()
with save_restore_stds():
directory = getexistingdirectory(self, _("Open"), basedir)
if not directory:
return []
folders: list[tuple[str, list[str]]] = []
for path in glob.glob(osp.join(directory, "**"), recursive=True):
if osp.isdir(path):
path = osp.normpath(path)
fnames = sorted(
osp.join(path, fname)
for fname in os.listdir(path)
if osp.isfile(osp.join(path, fname))
)
if fnames:
folders.append((path, fnames))
def relative_key(filename: str) -> str:
return osp.normcase(osp.normpath(osp.relpath(filename, directory)))
# Parameters recorded by history are reused; import parameters are asked
# once for all the other files of the directory tree
recorded_params: dict[str, gds.DataSet] = {}
if import_filenames is not None:
assert import_params is not None and len(import_params) == len(
import_filenames
), "Number of import parameters must match number of files"
recorded_params = {
osp.normcase(osp.normpath(fname)): fparam
for fname, fparam in zip(import_filenames, import_params)
}
skipped = {osp.normcase(osp.normpath(f)) for f in skipped_filenames or []}
all_files = [fname for _path, fnames in folders for fname in fnames]
all_files = [fname for fname in all_files if relative_key(fname) not in skipped]
pending = [
fname for fname in all_files if relative_key(fname) not in recorded_params
]
prepared_pairs = self.edit_import_parameters(pending, param)
if prepared_pairs is None:
return []
file_params: dict[str, gds.DataSet | None] = dict(prepared_pairs)
for fname in all_files:
if relative_key(fname) in recorded_params:
file_params[fname] = recorded_params[relative_key(fname)]
history_kwargs: dict[str, Any] = {"directory": directory}
param_files = [
fname for fname in all_files if file_params.get(fname) is not None
]
if param_files:
history_kwargs["import_filenames"] = [
osp.relpath(fname, directory) for fname in param_files
]
history_kwargs["import_params"] = [
copy.deepcopy(file_params[fname]) for fname in param_files
]
cancelled = [
osp.relpath(f, directory) for f in all_files if f not in file_params
]
skipped_all = sorted(set(skipped_filenames or []) | set(cancelled))
if skipped_all:
history_kwargs["skipped_filenames"] = skipped_all
# Offer a fresh history session for this batch *before* loading anything.
self.mainwindow.historypanel.maybe_start_session_for_input(load=True)
action = self.mainwindow.historypanel.add_ui_entry(
_('Load from directory "%s"') % osp.basename(osp.normpath(directory)),
target=self.PANEL_STR_ID + "panel",
method_name="load_from_directory",
save_state=False,
**history_kwargs,
)
objs = []
with self.mainwindow.historypanel.session_prompt_suppressed():
with self.mainwindow.historypanel.capture_outputs(action):
with create_progress_bar(
self, _("Scanning directory"), max_=len(folders) - 1
) as progress:
# Iterate over all subfolders in the directory:
for i_path, (path, fnames) in enumerate(folders):
progress.setValue(i_path + 1)
if progress.wasCanceled():
break
# Files whose import dialog was cancelled are skipped
fnames = [fname for fname in fnames if fname in file_params]
if not fnames:
continue
new_objs = self.load_from_files(
fnames,
create_group=False,
add_objects=False,
ignore_errors=True,
import_params=[file_params[fname] for fname in fnames],
)
if new_objs:
objs += new_objs
grp_name = osp.relpath(path, directory)
if grp_name == ".":
grp_name = osp.basename(path)
grp = self.add_group(grp_name)
for obj in new_objs:
self.add_object(
obj, group_id=get_uuid(grp), set_current=False
)
return objs
[docs]
def load_from_files(
self,
filenames: list[str] | None = None,
create_group: bool = False,
add_objects: bool = True,
ignore_errors: bool = False,
import_params: list[gds.DataSet | None] | None = None,
param: gds.DataSet | None = None,
) -> list[TypeObj]:
"""Open and return objects from files, optionally adding them to DataLab.
Args:
filenames: File names
create_group: if True, create a new group if more than one object is loaded
for a single file. Defaults to False: all objects are added to the current
group.
add_objects: if True, add objects to the panel. Defaults to True.
ignore_errors: if True, ignore errors when loading files. Defaults to False.
import_params: Optional format-specific parameters aligned with file names.
When provided by history replay, import dialogs are not reopened.
param: Optional format-specific parameters applied to every file
requiring import parameters (e.g. RAW images), without showing any
dialog. Cannot be combined with ``import_params``.
Returns:
list of new objects
"""
if import_params is not None and param is not None:
raise ValueError("import_params and param cannot be combined")
if not self.mainwindow.confirm_memory_state():
return []
if filenames is None: # pragma: no cover
basedir = Conf.base_dir.get()
filters = self.IO_REGISTRY.get_read_filters()
with save_restore_stds():
filenames, _filt = getopenfilenames(self, _("Open"), basedir, filters)
if not filenames: # pragma: no cover
return []
if import_params is None:
prepared_pairs = self.edit_import_parameters(sorted(filenames), param)
if prepared_pairs is None:
return []
else:
assert len(import_params) == len(filenames), (
"Number of import parameters must match number of files"
)
prepared_pairs = sorted(
zip(filenames, import_params), key=lambda pair: pair[0]
)
if not prepared_pairs:
return []
filenames = [filename for filename, _param in prepared_pairs]
import_params = [param for _filename, param in prepared_pairs]
nbf = len(prepared_pairs)
if nbf > 1:
entry_title = _("Load from %d files") % nbf
else:
entry_title = _('Load "%s"') % osp.basename(filenames[0])
# Only record a history entry when this call actually adds the objects to
# the workspace; otherwise the caller is responsible for adding *and*
# recording (e.g. ``load_from_directory``).
action = None
if add_objects:
# Offer a fresh history session for this batch *before* recording
# any entry.
self.mainwindow.historypanel.maybe_start_session_for_input(load=True)
history_kwargs: dict[str, Any] = {
"filenames": list(filenames),
"create_group": create_group,
"add_objects": add_objects,
"ignore_errors": ignore_errors,
}
if any(param is not None for param in import_params):
history_kwargs["import_params"] = [
copy.deepcopy(param) if param is not None else None
for param in import_params
]
action = self.mainwindow.historypanel.add_ui_entry(
entry_title,
target=self.PANEL_STR_ID + "panel",
method_name="load_from_files",
save_state=False,
**history_kwargs,
)
objs = []
loaded_filenames: list[str] = []
loaded_params: list[gds.DataSet | None] = []
with self.mainwindow.historypanel.session_prompt_suppressed():
with self.mainwindow.historypanel.capture_outputs(action):
for filename, import_param in prepared_pairs:
with qt_try_loadsave_file(self.parentWidget(), filename, "load"):
Conf.base_dir.set(filename)
try:
new_objs = self.__load_from_file(
filename,
create_group=create_group,
add_objects=add_objects,
import_param=import_param,
)
except Exception as exc: # pylint: disable=broad-exception-caught
if ignore_errors:
# Ignore unknown file types
pass
else:
raise exc
else:
objs += new_objs
if new_objs:
loaded_filenames.append(filename)
loaded_params.append(import_param)
if action is not None and loaded_filenames and len(loaded_filenames) < nbf:
# Some files could not be loaded: make the recorded entry reflect
# the files actually loaded, so the title is accurate and replay
# does not re-attempt the failed files. (If *no* file was loaded,
# ``capture_outputs`` already discarded the entry.)
if len(loaded_filenames) > 1:
action.title = _("Load from %d files") % len(loaded_filenames)
else:
action.title = _('Load "%s"') % osp.basename(loaded_filenames[0])
action.kwargs["filenames"] = list(loaded_filenames)
if any(param is not None for param in loaded_params):
action.kwargs["import_params"] = [
copy.deepcopy(param) if param is not None else None
for param in loaded_params
]
else:
action.kwargs.pop("import_params", None)
self.mainwindow.historypanel.tree.refresh_action_item(action)
return objs
[docs]
def save_to_files(
self,
filenames: list[str] | str | None = None,
export_params: list[ImageExportParam | None] | None = None,
object_uuids: list[str] | None = None,
) -> None:
"""Save selected objects to files (signal/image).
Args:
filenames: File names
export_params: Optional image export parameters aligned with file names
object_uuids: Optional object UUIDs aligned with file names
"""
if isinstance(filenames, str):
filenames = [filenames]
interactive = filenames is None
if object_uuids is None:
objs = self.objview.get_sel_objects(include_groups=True)
object_uuids = [get_uuid(obj) for obj in objs]
else:
objs = None
object_count = len(object_uuids)
if interactive: # pragma: no cover
filenames = [None] * object_count
assert len(filenames) == object_count, (
"Number of filenames must match number of objects"
)
if export_params is None:
export_params = [None] * object_count
assert len(export_params) == object_count, (
"Number of export parameters must match number of objects"
)
aligned: list[tuple[TypeObj, str | None, ImageExportParam | None, str]] = []
if objs is not None:
aligned = list(zip(objs, filenames, export_params, object_uuids))
else:
for object_uuid, filename, param in zip(
object_uuids, filenames, export_params
):
if not self.objmodel.has_uuid(object_uuid):
warnings.warn(
_("Skipping export: object %r no longer exists.") % object_uuid
)
continue
aligned.append(
(self.objmodel[object_uuid], filename, param, object_uuid)
)
# Ask for missing file names first, so that the history entry reflects the
# actual files written (and is skipped altogether if the user cancels)
pairs: list[tuple[TypeObj, str, ImageExportParam | None, str]] = []
for obj, filename, param, object_uuid in aligned:
if filename is None: # pragma: no cover
basedir = Conf.base_dir.get()
filters = self.IO_REGISTRY.get_write_filters()
with save_restore_stds():
filename, _filt = getsavefilename(
self, _("Save as"), basedir, filters
)
if filename:
confirmed = True
if interactive: # pragma: no cover
confirmed, param = self.edit_export_parameters(obj, filename)
if confirmed:
pairs.append((obj, filename, param, object_uuid))
if not pairs: # pragma: no cover
return
successful_pairs: list[tuple[TypeObj, str, ImageExportParam | None, str]] = []
for obj, filename, param, object_uuid in pairs:
with qt_try_loadsave_file(self.parentWidget(), filename, "save"):
Conf.base_dir.set(filename)
self.write_object_to_file(obj, filename, param)
successful_pairs.append((obj, filename, param, object_uuid))
if not successful_pairs:
return
nbf = len(successful_pairs)
if nbf > 1:
entry_title = _("Save to %d different files") % nbf
else:
entry_title = _('Save to "%s"') % osp.basename(successful_pairs[0][1])
history_kwargs: dict[str, Any] = {
"filenames": [
filename for _obj, filename, _param, _uuid in successful_pairs
],
"object_uuids": [
object_uuid for _obj, _filename, _param, object_uuid in successful_pairs
],
}
if any(param is not None for _obj, _filename, param, _uuid in successful_pairs):
history_kwargs["export_params"] = [
param for _obj, _filename, param, _uuid in successful_pairs
]
self.mainwindow.historypanel.add_ui_entry(
entry_title,
target=self.PANEL_STR_ID + "panel",
method_name="save_to_files",
save_state=False,
**history_kwargs,
)
[docs]
def save_to_directory(self, param: SaveToDirectoryParam | None = None) -> None:
"""Save signals or images to directory using a filename pattern.
Opens a dialog to select the output directory, the basename pattern and the
extension.
Args:
param: parameters.
"""
objs = self.objview.get_sel_objects(include_groups=True)
if param is None:
extensions = get_file_extensions(self.IO_REGISTRY.get_write_filters())
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=gds.DataItemValidationWarning)
labels = []
for obj in objs:
group = self.objmodel.get_group_from_object(obj)
labels.append(f"[{group.title}] {obj.title}")
guiparam = SaveToDirectoryGUIParam(objs, extensions, labels)
# Restore settings from config
saved_param = Conf.save_to_directory_settings.get(
SaveToDirectoryParam()
)
update_dataset(guiparam, saved_param)
# Validate extension: set to first if None or not in available list
# Note: extensions list has no dots, but guiparam.extension has dot
extensions_with_dot = ["." + ext for ext in extensions]
if (
guiparam.extension is None
or guiparam.extension not in extensions_with_dot
):
guiparam.extension = extensions_with_dot[0] if extensions else ""
if not guiparam.edit(parent=self.parentWidget()):
return
param = SaveToDirectoryParam()
update_dataset(param, guiparam)
# Save settings to config
Conf.save_to_directory_settings.set(param)
Conf.base_dir.set(param.directory)
self.mainwindow.historypanel.add_ui_entry(
_("Save to directory"),
target=self.PANEL_STR_ID + "panel",
method_name="save_to_directory",
save_state=True,
param=param,
)
with create_progress_bar(self, _("Saving..."), max_=len(objs)) as progress:
for i, (path, obj) in enumerate(param.generate_filepath_obj_pairs(objs)):
progress.setValue(i + 1)
if progress.wasCanceled():
break
with qt_try_loadsave_file(self.parentWidget(), path, "save"):
self.write_object_to_file(obj, path)
[docs]
def handle_dropped_files(self, filenames: list[str] | None = None) -> None:
"""Handle dropped files
Args:
filenames: File names
Returns:
None
"""
dirnames = [fname for fname in filenames if osp.isdir(fname)]
h5_fnames = [
fname for fname in filenames if is_hdf5_file(fname, check_content=True)
]
other_fnames = sorted(list(set(filenames) - set(h5_fnames) - set(dirnames)))
if dirnames:
for dirname in dirnames:
self.load_from_directory(dirname)
if h5_fnames:
self.mainwindow.open_h5_files(h5_fnames, import_all=True)
if other_fnames:
self.load_from_files(other_fnames)
[docs]
def exec_import_wizard(self) -> None:
"""Execute import wizard"""
wizard = TextImportWizard(self.PANEL_STR_ID, parent=self.parentWidget())
if exec_dialog(wizard):
objs = wizard.get_objs()
if objs:
with create_progress_bar(
self, _("Adding objects to workspace"), max_=len(objs) - 1
) as progress:
for idx, obj in enumerate(objs):
progress.setValue(idx)
QW.QApplication.processEvents()
if progress.wasCanceled():
break
self.add_object(obj)
[docs]
def copy_annotations(self) -> None:
"""Copy annotations from selected object"""
obj = self.objview.get_sel_objects(include_groups=True)[0]
self.annotations_clipboard = obj.get_annotations()
# Update action states (e.g., "Paste annotations" should now be enabled)
self.selection_changed()
[docs]
def paste_annotations(self) -> None:
"""Paste annotations to selected object(s)"""
if not self.annotations_clipboard:
return
sel_objects = self.objview.get_sel_objects(include_groups=True)
for obj in sel_objects:
obj.set_annotations(self.annotations_clipboard)
self.refresh_plot("selected", True, False)
# Update action states (e.g., annotation-related actions should now be enabled)
self.selection_changed()
[docs]
def import_annotations_from_file(self, filename: str | None = None) -> None:
"""Import annotations from file (JSON).
Args:
filename: File name
"""
if filename is None: # pragma: no cover
basedir = Conf.base_dir.get()
with save_restore_stds():
filename, _filter = getopenfilename(
self, _("Import annotations"), basedir, "*.dlabann"
)
if filename:
with qt_try_loadsave_file(self.parentWidget(), filename, "load"):
Conf.base_dir.set(filename)
obj = self.objview.get_sel_objects(include_groups=True)[0]
annotations = read_annotations(filename)
obj.set_annotations(annotations)
self.refresh_plot("selected", True, False)
# Update action states (annotation-related actions should now be enabled)
self.selection_changed()
[docs]
def export_annotations_from_file(self, filename: str | None = None) -> None:
"""Export annotations to file (JSON).
Args:
filename: File name
"""
obj = self.objview.get_sel_objects(include_groups=True)[0]
if filename is None: # pragma: no cover
basedir = Conf.base_dir.get()
with save_restore_stds():
filename, _filt = getsavefilename(
self, _("Export annotations"), basedir, "*.dlabann"
)
if filename:
with qt_try_loadsave_file(self.parentWidget(), filename, "save"):
Conf.base_dir.set(filename)
annotations = obj.get_annotations()
write_annotations(filename, annotations)
[docs]
def delete_annotations(self) -> None:
"""Delete all annotations from selected object(s)"""
sel_objects = self.objview.get_sel_objects(include_groups=True)
for obj in sel_objects:
obj.clear_annotations()
self.refresh_plot("selected", True, False)
# Update action states (annotation-related actions should now be disabled)
self.selection_changed()
[docs]
def import_roi_from_file(self, filename: str | None = None) -> None:
"""Import regions of interest from file (JSON).
Args:
filename: File name
"""
if filename is None: # pragma: no cover
basedir = Conf.base_dir.get()
with save_restore_stds():
filename, _filter = getopenfilename(
self, _("Import ROI"), basedir, "*.dlabroi"
)
if filename:
with qt_try_loadsave_file(self.parentWidget(), filename, "load"):
Conf.base_dir.set(filename)
obj = self.objview.get_sel_objects(include_groups=True)[0]
roi = read_roi(filename)
if obj.roi is None:
obj.roi = roi
else:
obj.roi = obj.roi.combine_with(roi)
self.selection_changed(update_items=True)
self.refresh_plot("selected", True, False)
self.SIG_OBJECT_MODIFIED.emit()
[docs]
def export_roi_to_file(self, filename: str | None = None) -> None:
"""Export regions of interest to file (JSON).
Args:
filename: File name
"""
obj = self.objview.get_sel_objects(include_groups=True)[0]
assert obj.roi is not None
if filename is None: # pragma: no cover
basedir = Conf.base_dir.get()
with save_restore_stds():
filename, _filt = getsavefilename(
self, _("Export ROI"), basedir, "*.dlabroi"
)
if filename:
with qt_try_loadsave_file(self.parentWidget(), filename, "save"):
Conf.base_dir.set(filename)
write_roi(filename, obj.roi)
# ------Refreshing GUI--------------------------------------------------------------
[docs]
def refresh_after_reference_remap(self) -> None:
"""Refresh cached presentation after imported references are remapped."""
self.objview.update_tree()
self.selection_changed(update_items=True)
[docs]
def selection_changed(self, update_items: bool = False) -> None:
"""Object selection changed: update object properties, refresh plot and update
object view.
Args:
update_items: Update plot items (default: False)
"""
selected_objects = self.objview.get_sel_objects(include_groups=True)
selected_groups = self.objview.get_sel_groups()
# Determine which tab to show based on object state
current_obj = self.objview.get_current_object()
force_tab = None
if current_obj is not None:
obj_uuid = get_uuid(current_obj)
# Show Creation tab for newly created objects (only once)
if obj_uuid == self.objprop.newly_created_obj_uuid:
force_tab = "creation"
self.objprop.newly_created_obj_uuid = None
# Show Processing tab for freshly processed objects (only once)
elif obj_uuid == self.objprop.fresh_processing_obj_uuid:
force_tab = "processing"
self.objprop.fresh_processing_obj_uuid = None
# Show Analysis tab for objects with fresh analysis results
elif obj_uuid == self.objprop.fresh_analysis_obj_uuid:
force_tab = "analysis"
self.objprop.fresh_analysis_obj_uuid = None
self.objprop.update_properties_from(current_obj, force_tab=force_tab)
self.acthandler.selected_objects_changed(selected_groups, selected_objects)
self.refresh_plot("selected", update_items, False)
[docs]
def properties_changed(self) -> None:
"""The properties 'Apply' button was clicked: update object properties,
refresh plot and update object view."""
# Get only the properties that have changed from the original values
changed_props = self.objprop.get_changed_properties()
# Apply only the changed properties to all selected objects.
# The ``replaying()`` guard suppresses synthetic history capture.
with self.mainwindow.historypanel.replaying():
for obj in self.objview.get_sel_objects(include_groups=True):
obj.mark_roi_as_changed()
# Update only the changed properties instead of all properties
update_dataset(obj, changed_props)
self.objview.update_item(get_uuid(obj))
# Refresh all selected items, including non-visible ones (only_visible=False)
# This ensures that plot items are updated for all selected objects, even if
# they are temporarily hidden behind other objects
self.refresh_plot(
"selected", update_items=True, force=False, only_visible=False
)
# Update the stored original values to reflect the new state
# This ensures subsequent changes are compared against the current values
self.objprop.update_original_values()
self.SIG_OBJECT_MODIFIED.emit()
[docs]
def recompute_selected(self) -> None:
"""Recompute/rerun selected objects or group with stored parameters.
This method handles both single objects and groups. For each object, it
recomputes, on demand:
- the processing that produced it (1-to-1, 2-to-1 or n-to-1, as restored
by :func:`datalab.processingcall.resolve_processing_call`: from the
provenance ledger, else from the processing metadata), in place, and
- 1-to-0 analysis operations (statistics, measurements, detections, etc.).
Analysis results are *not* recomputed automatically when data, ROIs or
object properties change: this manual action is the single, explicit entry
point to refresh them. Objects without recomputable parameters are skipped.
"""
# Get selected objects (handles both individual selection and groups)
objects = self.objview.get_sel_objects(include_groups=True)
if not objects:
return
# pylint: disable-next=import-outside-toplevel
from datalab.processingcall import resolve_processing_call
# Filter objects that have a recorded processing and/or 1-to-0 analysis
# parameters (an object may have both)
recomputable_objects: list[SignalObj | ImageObj] = []
reanalyzable_objects: list[SignalObj | ImageObj] = []
for obj in objects:
if resolve_processing_call(self.mainwindow, obj) is not None:
recomputable_objects.append(obj)
analysis_params = extract_analysis_parameters(obj)
if analysis_params is not None and analysis_params.pattern == "1-to-0":
reanalyzable_objects.append(obj)
if not recomputable_objects and not reanalyzable_objects:
if not execenv.unattended:
QW.QMessageBox.information(
self,
_("Recompute"),
_(
"Selected object(s) do not have processing or analysis "
"parameters that can be recomputed."
),
)
return
# Silence history capture while explicitly recomputing the current state.
with self.mainwindow.historypanel.replaying():
# Recompute 1-to-1 operations first so analyses use updated data.
recomputed_uuids, was_interrupted = self.recompute_1_to_1_objects(
recomputable_objects
)
if was_interrupted:
return
analysis_targets = []
for obj in reanalyzable_objects:
obj_uuid = get_uuid(obj)
if obj in recomputable_objects and obj_uuid not in recomputed_uuids:
continue
analysis_targets.append(obj)
self.recompute_1_to_0_objects(analysis_targets)
[docs]
def recompute_1_to_1_objects(
self, objects: list[SignalObj | ImageObj]
) -> tuple[set[str], bool]:
"""Recompute in-place 1-to-1 processing operations for the given objects.
Args:
objects: Objects with stored 1-to-1 processing parameters
Returns:
Tuple containing:
- Set of object UUIDs successfully recomputed
- True if operation was interrupted (progress canceled or user chose
to stop after a failure), False otherwise
"""
if not objects:
return set(), False
recomputed_uuids: set[str] = set()
with create_progress_bar(
self, _("Recomputing objects"), max_=len(objects)
) as progress:
for index, obj in enumerate(objects):
progress.setValue(index + 1)
QW.QApplication.processEvents()
if progress.wasCanceled():
return recomputed_uuids, True
self.objview.set_current_object(obj)
report = self.processor.recompute_processing(obj, interactive=False)
if report.success:
recomputed_uuids.add(get_uuid(obj))
continue
if report.cancelled:
return recomputed_uuids, True
if execenv.unattended:
continue
failtxt = _("Failed to recompute object")
if index == len(objects) - 1:
QW.QMessageBox.warning(
self,
_("Recompute"),
f"{failtxt} '{obj.title}':\n{report.message}",
)
else:
conttxt = _("Do you want to continue with the next object?")
answer = QW.QMessageBox.warning(
self,
_("Recompute"),
f"{failtxt} '{obj.title}':\n{report.message}\n\n{conttxt}",
QW.QMessageBox.Yes | QW.QMessageBox.No,
)
if answer == QW.QMessageBox.No:
return recomputed_uuids, True
return recomputed_uuids, False
[docs]
def recompute_1_to_0_objects(
self, objects: list[SignalObj | ImageObj]
) -> tuple[set[str], bool]:
"""Recompute 1-to-0 analysis operations for the given objects.
Args:
objects: Objects with stored 1-to-0 analysis parameters
"""
if not objects:
return set(), False
recomputed_uuids: set[str] = set()
with create_progress_bar(
self, _("Recomputing analyses"), max_=len(objects)
) as progress:
for index, obj in enumerate(objects):
progress.setValue(index + 1)
QW.QApplication.processEvents()
if progress.wasCanceled():
return recomputed_uuids, True
try:
success = self.processor.recompute_analysis(obj)
message = _("Analysis computation failed.")
except Exception as exc: # pylint: disable=broad-exception-caught
success = False
message = str(exc)
if success:
recomputed_uuids.add(get_uuid(obj))
continue
if execenv.unattended:
continue
failtxt = _("Failed to recompute analysis")
if index == len(objects) - 1:
QW.QMessageBox.warning(
self,
_("Recompute"),
f"{failtxt} '{obj.title}':\n{message}",
)
else:
conttxt = _("Do you want to continue with the next object?")
answer = QW.QMessageBox.warning(
self,
_("Recompute"),
f"{failtxt} '{obj.title}':\n{message}\n\n{conttxt}",
QW.QMessageBox.Yes | QW.QMessageBox.No,
)
if answer == QW.QMessageBox.No:
return recomputed_uuids, True
return recomputed_uuids, False
[docs]
def select_source_objects(self) -> None:
"""Select source objects associated with the selected object's processing.
This method retrieves the source object UUIDs from the selected object's
processing parameters and selects them in the object view.
"""
# pylint: disable-next=import-outside-toplevel
from datalab.processingcall import resolve_processing_call
# Get the selected object (should be exactly one)
objects = self.objview.get_sel_objects(include_groups=False)
if len(objects) != 1:
return
obj = objects[0]
# Restore the processing call (provenance ledger, else metadata)
call = resolve_processing_call(self.mainwindow, obj)
if call is None:
if not execenv.unattended:
QW.QMessageBox.information(
self,
_("Select source objects"),
_("Selected object does not have processing metadata."),
)
return
# Source UUIDs, without repetitions (an object may be several inputs)
source_uuids = list(dict.fromkeys(item.object_uuid for item in call.inputs))
if not source_uuids:
if not execenv.unattended:
QW.QMessageBox.information(
self,
_("Select source objects"),
_("Selected object does not have source object references."),
)
return
# Objects still holding the sources (never an object appended later with
# the identifier of a deleted source)
present = {item.object_uuid: item.obj for item in call.inputs if item.obj}
existing_objs = [present[uuid] for uuid in source_uuids if uuid in present]
if not existing_objs:
if not execenv.unattended:
if len(source_uuids) == 1:
msg = _("Source object no longer exists.")
else:
msg = _("Source objects no longer exist.")
QW.QMessageBox.warning(self, _("Select source objects"), msg)
return
# Determine which panel contains the source objects
# Source objects are always in the same panel (either all signals or all images)
object_ids = self.objmodel.get_object_ids()
if all(get_uuid(source) in object_ids for source in existing_objs):
source_panel = self
elif isinstance(existing_objs[0], SignalObj):
source_panel = self.mainwindow.signalpanel
else: # ImageObj
source_panel = self.mainwindow.imagepanel
# Switch to the source panel if needed
if source_panel is not self:
self.mainwindow.set_current_panel(source_panel)
# Select the existing source objects
# Note: Since all sources are in the same panel, all UUIDs in existing_objs
# are guaranteed to be in source_panel
source_panel.objview.clearSelection()
for obj in existing_objs:
source_panel.objview.set_current_item_id(get_uuid(obj), extend=True)
# Show info if some sources were deleted
missing_count = len(source_uuids) - len(existing_objs)
if missing_count > 0 and not execenv.unattended:
if len(existing_objs) == 1:
msg = _("Selected a single source object")
else:
msg = _("Selected %d source objects") % len(existing_objs)
msg += " ("
if missing_count == 1:
msg += _("1 source object no longer exists")
else:
msg += _("%d source objects no longer exist") % missing_count
msg += ")"
QW.QMessageBox.warning(self, _("Select source objects"), msg)
# ------Plotting data in modal dialogs----------------------------------------------
[docs]
def add_plot_items_to_dialog(self, dlg: PlotDialog, oids: list[str]) -> None:
"""Add plot items to dialog
Args:
dlg: Dialog
oids: Object IDs
"""
objs = self.objmodel.get_objects(oids)
plot = dlg.get_plot()
with create_progress_bar(
self, _("Creating plot items"), max_=len(objs)
) as progress:
for index, obj in enumerate(objs):
progress.setValue(index + 1)
QW.QApplication.processEvents()
if progress.wasCanceled():
return None
existing_item = self.plothandler.get(get_uuid(obj))
item = create_adapter_from_object(obj).make_item(
update_from=existing_item
)
item.param.label = self.mainwindow.render_object_title(obj.title)
item.param.update_item(item)
item.set_readonly(True)
plot.add_item(item, z=0)
plot.set_active_item(item)
item.unselect()
plot.replot()
return dlg
[docs]
def open_separate_view(
self, oids: list[str] | None = None, edit_annotations: bool = False
) -> PlotDialog | None:
"""
Open separate view for visualizing selected objects
Args:
oids: Object IDs (default: None)
edit_annotations: Edit annotations (default: False)
Returns:
Instance of PlotDialog
"""
if oids is None:
oids = self.objview.get_sel_object_uuids(include_groups=True)
obj = self.objmodel[oids[-1]] # last selected object
# Create a new dialog and add plot items to it
dlg = self.create_new_dialog(
title=(
self.mainwindow.render_object_title(obj.title)
if len(oids) == 1
else None
),
edit=True,
name=f"{obj.PREFIX}_new_window",
)
if dlg is None:
return None
self.add_plot_items_to_dialog(dlg, oids)
mgr = dlg.get_manager()
toolbar = QW.QToolBar(_("Annotations"), self)
dlg.button_layout.insertWidget(0, toolbar)
mgr.add_toolbar(toolbar, id(toolbar))
toolbar.setToolButtonStyle(QC.Qt.ToolButtonTextUnderIcon)
for tool in self.ANNOTATION_TOOLS:
mgr.add_tool(tool, toolbar_id=id(toolbar))
def toggle_annotations(enabled: bool):
"""Toggle annotation tools / edit buttons visibility"""
for widget in (dlg.button_box, toolbar, mgr.get_itemlist_panel()):
if enabled:
widget.show()
else:
widget.hide()
edit_ann_action = create_action(
dlg,
_("Annotations"),
toggled=toggle_annotations,
icon=get_icon("annotations.svg"),
)
mgr.add_tool(ActionTool, edit_ann_action)
default_toolbar = mgr.get_default_toolbar()
action_btn = default_toolbar.widgetForAction(edit_ann_action)
action_btn.setToolButtonStyle(QC.Qt.ToolButtonTextBesideIcon)
plot = dlg.get_plot()
for item in plot.items:
item.set_selectable(False)
adapter = create_adapter_from_object(obj)
for item in adapter.iterate_shape_items(editable=True):
plot.add_item(item)
adapter.annotation_adapter.capture_item_reference(item)
self.__separate_views[dlg] = obj
toggle_annotations(edit_annotations)
if len(oids) > 1:
# If multiple objects are displayed, show the item list panel
# (otherwise, it is hidden by default to lighten the dialog, except
# if `edit_annotations` is True):
plot.manager.get_itemlist_panel().show()
if edit_annotations:
edit_ann_action.setChecked(True)
dlg.show()
dlg.finished.connect(self.__separate_view_finished)
return dlg
def __separate_view_finished(self, result: int) -> None:
"""Separate view was closed
Args:
result: result
"""
dlg: PlotDialog = self.sender()
if result == QW.QDialog.DialogCode.Accepted:
obj = self.__separate_views[dlg]
adapter = create_adapter_from_object(obj)
rw_items = []
for item in dlg.get_plot().get_items():
if adapter.annotation_adapter.is_annotation_item(
item
) and is_plot_item_serializable(item):
rw_items.append(item)
adapter.set_annotations_from_items(rw_items)
self.selection_changed(update_items=True)
self.__separate_views.pop(dlg)
dlg.deleteLater()
[docs]
def view_images_side_by_side(self, oids: list[str] | None = None) -> None:
"""
View selected images side-by-side in a grid layout
Args:
oids: Object IDs (default: None, uses selected objects)
"""
if oids is None:
oids = self.objview.get_sel_object_uuids(include_groups=True)
if len(oids) < 2:
return
objs = self.objmodel.get_objects(oids)
# Compute grid dimensions
max_cols = 4
num_cols = min(len(objs), max_cols)
num_rows = (len(objs) + num_cols - 1) // num_cols
# Create dialog with synchronized plots
dlg = SyncPlotDialog(title=_("View images side-by-side"), toolbar=False)
dlg.setObjectName(f"{self.PANEL_STR_ID}_side_by_side")
# Add each image to the grid
for idx, obj in enumerate(objs):
row = idx // num_cols
col = idx % num_cols
# Create plot with title
rendered_title = self.mainwindow.render_object_title(obj.title)
plot = BasePlot(options=BasePlotOptions(title=rendered_title, type="image"))
# Create plot item from object
adapter = create_adapter_from_object(obj)
item = adapter.make_item()
item.param.label = rendered_title
item.param.update_item(item)
item.set_readonly(True)
plot.add_item(item, z=0)
# Add ROI items if available
if obj.roi is not None:
for roi_item in adapter.iterate_roi_items(editable=False):
plot.add_item(roi_item)
# Add to synchronized dialog
dlg.add_plot(row, col, plot, sync=True)
# Finalize and show dialog
dlg.finalize_configuration()
# Set explicit size for proper display
dlg.resize(20 + 440 * num_cols, 20 + 400 * num_rows)
exec_dialog(dlg)
[docs]
def get_dialog_size(self) -> tuple[int, int]:
"""Get dialog size (minimum and maximum)"""
# Resize the dialog so that it's at least MINDIALOGSIZE (absolute values),
# and at most MAXDIALOGSIZE (% of the main window size):
minwidth, minheight = self.MINDIALOGSIZE
maxwidth = int(self.mainwindow.width() * self.MAXDIALOGSIZE)
maxheight = int(self.mainwindow.height() * self.MAXDIALOGSIZE)
size = min(minwidth, maxwidth), min(minheight, maxheight)
return size
[docs]
def create_new_dialog(
self,
edit: bool = False,
toolbar: bool = True,
title: str | None = None,
name: str | None = None,
options: dict[str, Any] | None = None,
) -> PlotDialog | None:
"""Create new pop-up signal/image plot dialog.
Args:
edit: Edit mode
toolbar: Show toolbar
title: Dialog title
name: Dialog object name
options: Plot options
Returns:
Plot dialog instance
"""
plot_options = self.plothandler.get_plot_options()
if options is not None:
plot_options = plot_options.copy(options)
# pylint: disable=not-callable
dlg = PlotDialog(
parent=self.parentWidget(),
title=APP_NAME if title is None else f"{title} - {APP_NAME}",
options=plot_options,
toolbar=toolbar,
icon="DataLab.svg",
edit=edit,
size=self.get_dialog_size(),
)
dlg.setObjectName(name)
return dlg
[docs]
def get_roi_editor_output(
self, mode: Literal["apply", "extract", "define"] = "apply"
) -> tuple[TypeROI, bool] | None:
"""Get ROI data (array) from specific dialog box.
Args:
mode: Mode of operation, either "apply" (define ROI, then apply it to
selected objects), "extract" (define ROI, then extract data from it),
or "define" (define ROI without applying or extracting).
Returns:
A tuple containing the ROI object and a boolean indicating whether the
dialog was accepted or not.
"""
obj = self.objview.get_sel_objects(include_groups=True)[-1]
# Use get() instead of [] to avoid KeyError if the plot item doesn't exist
# (can happen when "auto refresh" is disabled or in "show first only" mode
# where not all objects have plot items created yet)
existing_item = self.plothandler.get(get_uuid(obj))
item = create_adapter_from_object(obj).make_item(update_from=existing_item)
roi_editor_class = self.get_roieditor_class() # pylint: disable=not-callable
roi_editor = roi_editor_class(
parent=self.parentWidget(),
obj=obj,
mode=mode,
item=item,
options=self.plothandler.get_plot_options(),
source_panel=self,
size=self.get_dialog_size(),
)
# Propagate the source plot axis scales (e.g. log) to the ROI editor
# so that signals/images displayed with non-linear scales keep the same
# representation in the ROI editor dialog (see issue: ROI editor was
# always opening with default lin-lin scales).
src_plot = self.plothandler.plot
roi_editor.get_plot().set_scales(
src_plot.get_axis_scale("bottom"),
src_plot.get_axis_scale("left"),
)
if exec_dialog(roi_editor):
return roi_editor.get_roieditor_results()
return None
[docs]
def get_objects_with_dialog(
self,
title: str,
comment: str = "",
nb_objects: int = 1,
parent: QW.QWidget | None = None,
) -> TypeObj | None:
"""Get object with dialog box.
Args:
title: Dialog title
comment: Optional dialog comment
nb_objects: Number of objects to select
parent: Parent widget
Returns:
Object(s) (signal(s) or image(s), or None if dialog was canceled)
"""
parent = self if parent is None else parent
dlg = objectview.GetObjectsDialog(parent, self, title, comment, nb_objects)
if exec_dialog(dlg):
return dlg.get_selected_objects()
return None
def __show_no_result_warning(self):
"""Show no result warning"""
msg = "<br>".join(
[
_("No result currently available for this object."),
"",
_(
"This feature leverages the results of previous analysis "
"performed on the selected object(s).<br><br>"
"To compute results, select one or more objects and choose "
"a feature in the <u>Analysis</u> menu."
),
]
)
if not execenv.unattended:
QW.QMessageBox.information(self, APP_NAME, msg)
[docs]
def show_results(self) -> None:
"""Show results"""
objs = self.objview.get_sel_objects(include_groups=True)
rdatadict = create_resultdata_dict(objs)
if rdatadict:
for rdata in rdatadict.values():
show_resultdata(self.parentWidget(), rdata, f"{objs[0].PREFIX}_results")
else:
self.__show_no_result_warning()
[docs]
def toggle_result_label_visibility(self, state: bool) -> None:
"""Toggle the visibility of the merged result label on the plot.
Args:
state: True to show the label, False to hide it
"""
show_label = state
# Update the configuration
Conf.show_result_label.set(show_label)
# Synchronize the other panel's action state
for panel in (self.mainwindow.signalpanel, self.mainwindow.imagepanel):
if panel is not self and panel.acthandler.show_label_action is not None:
panel.acthandler.show_label_action.blockSignals(True)
panel.acthandler.show_label_action.setChecked(show_label)
panel.acthandler.show_label_action.blockSignals(False)
# Refresh the plot to show/hide the label
self.plothandler.toggle_result_label_visibility(show_label)
def __add_result_signal(
self,
x: np.ndarray | list[float],
y: np.ndarray | list[float],
title: str,
xaxis: str,
yaxis: str,
group_id: str | None = None,
) -> None:
"""Add result signal
Args:
x: X data
y: Y data
title: Signal title
xaxis: X axis label
yaxis: Y axis label
group_id: UUID of the group to add the signal to. If None, add to
current group.
"""
xdata = np.array(x, dtype=float)
ydata = np.array(y, dtype=float)
obj = create_signal(
title=f"{title}: {yaxis} = f({xaxis})",
x=xdata,
y=ydata,
labels=[xaxis, yaxis],
)
self.mainwindow.signalpanel.add_object(obj, group_id=group_id)
def __create_plot_result_param_class(self, rdata: ResultData) -> type[gds.DataSet]:
"""Create PlotResultParam class dynamically based on result data.
Args:
rdata: Result data
Returns:
PlotResultParam class
"""
# Build X and Y choices from result data headers
xchoices = (("indices", _("Indices")),)
for xlabel in rdata.headers:
# If this column data is not numeric, we skip it:
if not isinstance(
rdata.results[0].get_column_values(xlabel)[0], (int, float, np.number)
):
continue
xchoices += ((xlabel, xlabel),)
ychoices = xchoices[1:]
# Determine default plot kind based on result data
default_kind = (
"one_curve_per_object"
if any(len(result.to_dataframe()) > 1 for result in rdata.results)
else "one_curve_per_title"
)
class PlotResultParam(gds.DataSet):
"""Plot results parameters"""
kind = gds.ChoiceItem(
_("Plot kind"),
(
(
"one_curve_per_object",
_("One curve per object (or ROI) and per result title"),
),
("one_curve_per_title", _("One curve per result title")),
),
default=default_kind,
)
xaxis = gds.ChoiceItem(_("X axis"), xchoices, default="indices")
yaxis = gds.ChoiceItem(_("Y axis"), ychoices, default=ychoices[0][0])
return PlotResultParam
def __plot_result(
self,
category: str,
rdata: ResultData,
objs: list[SignalObj | ImageObj],
param: gds.DataSet | None = None,
result_group_id: str | None = None,
) -> None:
"""Plot results for a specific category
Args:
category: Result category
rdata: Result data
objs: List of objects
param: Plot result parameters. If None, show dialog to get parameters.
result_group_id: UUID of the group to add result signals to. If None,
add to current group.
"""
# Regrouping ResultShape results by their `title` attribute:
grouped_results: dict[str, list[GeometryAdapter | TableAdapter]] = {}
for result in rdata.results:
grouped_results.setdefault(result.title, []).append(result)
# From here, results are already grouped by their `category` attribute,
# and then by their `title` attribute. We can now plot them.
#
# Now, we have two common use cases:
# 1. Plotting one curve per object (signal/image) and per `title`
# attribute, if each selected object contains a result object
# with multiple values (e.g. from a blob detection feature).
# 2. Plotting one curve per `title` attribute, if each selected object
# contains a result object with a single value (e.g. from a FHWM
# feature) - in that case, we select only the first value of each
# result object.
if param is None:
# Create parameter class and show dialog
PlotResultParam = self.__create_plot_result_param_class(rdata)
comment = (
_(
"Plot results obtained from previous analyses.<br><br>"
"This plot is based on results associated with '%s' prefix."
)
% category
)
param = PlotResultParam(_("Plot results"), comment=comment)
if not param.edit(parent=self.parentWidget()):
return
if param.kind == "one_curve_per_title":
# One curve per ROI (if any) and per result title
# ------------------------------------------------------------------
# Begin by checking if all results have the same number of ROIs:
# for simplicity, let's check the number of unique ROI indices.
all_roi_indexes = [
result.get_unique_roi_indices() for result in rdata.results
]
# Check if all roi_indexes are the same:
if len(set(map(tuple, all_roi_indexes))) > 1:
if not execenv.unattended:
QW.QMessageBox.warning(
self,
_("Plot results"),
_(
"All objects associated with results must have the "
"same regions of interest (ROIs) to plot results "
"together."
),
)
return
obj = objs[0]
# Build a string with source object references (max 3, then use "...")
max_ids_to_show = 3
source_refs = [get_title_reference(obj) for obj in objs]
if len(source_refs) <= max_ids_to_show:
source_ids = ", ".join(source_refs)
else:
# Show first 2, "...", then the last reference.
source_ids = (
", ".join(source_refs[: max_ids_to_show - 1])
+ ", ..., "
+ source_refs[-1]
)
for i_roi in all_roi_indexes[0]:
roi_suffix = f"|ROI{int(i_roi + 1)}" if i_roi >= 0 else ""
for title, results in grouped_results.items(): # title
x, y = [], []
for index, result in enumerate(results):
if param.xaxis == "indices":
x.append(index)
else:
x.append(result.get_column_values(param.xaxis, i_roi)[0])
y.append(result.get_column_values(param.yaxis, i_roi)[0])
if i_roi >= 0:
roi_suffix = f"|{obj.roi.get_single_roi_title(int(i_roi))}"
self.__add_result_signal(
x,
y,
f"{title} ({source_ids}){roi_suffix}",
param.xaxis,
param.yaxis,
result_group_id,
)
else:
# One curve per result title, per object and per ROI
# ------------------------------------------------------------------
for title, results in grouped_results.items(): # title
for index, result in enumerate(results): # object
obj = objs[index]
roi_indices = result.get_unique_roi_indices()
for i_roi in roi_indices: # ROI
roi_suffix = ""
if i_roi >= 0:
roi_suffix = f"|{obj.roi.get_single_roi_title(int(i_roi))}"
roi_data = result.get_roi_data(i_roi)
if param.xaxis == "indices":
x = list(range(len(roi_data)))
else:
x = roi_data[param.xaxis].values
y = roi_data[param.yaxis].values
source_ref = get_title_reference(objs[index])
stitle = f"{title} ({source_ref}){roi_suffix}"
self.__add_result_signal(
x, y, stitle, param.xaxis, param.yaxis, result_group_id
)
[docs]
def plot_results(
self,
kind: str | None = None,
xaxis: str | None = None,
yaxis: str | None = None,
) -> None:
"""Plot results
Args:
kind: Plot kind. Either "one_curve_per_object" or "one_curve_per_title".
If None, show dialog to get parameters.
xaxis: X axis column name. If None, show dialog to get parameters.
yaxis: Y axis column name. If None, show dialog to get parameters.
"""
objs = self.objview.get_sel_objects(include_groups=True)
rdatadict = create_resultdata_dict(objs)
if rdatadict:
# Always use or create a "Results" group for all plot results
rgroup_title = _("Results")
target_panel = self.mainwindow.signalpanel
try:
# Check if a "Results" group already exists in the signal panel
rgroup = target_panel.objmodel.get_group_from_title(rgroup_title)
except KeyError:
# Create the group if it doesn't exist
rgroup = target_panel.add_group(rgroup_title)
result_group_id = get_uuid(rgroup)
for category, rdata in rdatadict.items():
param = None
if kind is not None and xaxis is not None and yaxis is not None:
# Create parameter object programmatically
PlotResultParam = self.__create_plot_result_param_class(rdata)
param = PlotResultParam()
param.kind = kind
param.xaxis = xaxis
param.yaxis = yaxis
self.__plot_result(category, rdata, objs, param, result_group_id)
else:
self.__show_no_result_warning()
[docs]
def delete_results(self) -> None:
"""Delete results"""
self.mainwindow.historypanel.add_ui_entry(
_("Delete results"),
target=self.PANEL_STR_ID + "panel",
method_name="delete_results",
save_state=False,
)
objs = self.objview.get_sel_objects(include_groups=True)
rdatadict = create_resultdata_dict(objs)
if rdatadict:
if execenv.unattended:
confirmed = True
else:
answer = QW.QMessageBox.warning(
self,
_("Delete results"),
_(
"Are you sure you want to delete all results "
"of the selected object(s)?"
),
QW.QMessageBox.Yes | QW.QMessageBox.No,
)
confirmed = answer == QW.QMessageBox.Yes
if confirmed:
objs = self.objview.get_sel_objects(include_groups=True)
for obj in objs:
# Remove all table and geometry results using adapter methods
TableAdapter.remove_all_from(obj)
GeometryAdapter.remove_all_from(obj)
# Clear analysis parameters to prevent a manual recompute
# from attempting to recompute deleted analyses
clear_analysis_parameters(obj)
if obj is self.objview.get_current_object():
self.objprop.update_properties_from(obj)
# Update action states to reflect the removal
selected_groups = self.objview.get_sel_groups()
self.acthandler.selected_objects_changed(selected_groups, objs)
self.refresh_plot("selected", True, False)
else:
self.__show_no_result_warning()
[docs]
def add_label_with_title(
self, title: str | None = None, ignore_msg: bool = True
) -> None:
"""Add a label with object title on the associated plot
Args:
title: Label title. Defaults to None.
If None, the title is the object title.
ignore_msg: If True, do not show the information message. Defaults to True.
If False, show a message box to inform the user that the label has been
added as an annotation, and that it can be edited or removed using the
annotation editing window.
"""
if title is None:
action_title = _("Add object title to plot")
else:
action_title = _("Add label with title")
self.mainwindow.historypanel.add_ui_entry(
action_title,
target=self.PANEL_STR_ID + "panel",
method_name="add_label_with_title",
save_state=False,
title=title,
)
objs = self.objview.get_sel_objects(include_groups=True)
for obj in objs:
label = (
self.mainwindow.render_object_title(obj.title)
if title is None
else title
)
create_adapter_from_object(obj).add_label_with_title(title=label)
if (
not Conf.ignore_title_insertion_msg.get(False)
and not ignore_msg
and not execenv.unattended
):
answer = QW.QMessageBox.information(
self,
_("Annotation added"),
_(
"The label has been added as an annotation. "
"You can edit or remove it using the annotation editing window."
"<br><br>"
"Choosing to ignore this message will prevent it "
"from being displayed again."
),
QW.QMessageBox.Ok | QW.QMessageBox.Ignore,
)
if answer == QW.QMessageBox.Ignore:
Conf.ignore_title_insertion_msg.set(True)
self.refresh_plot("selected", True, False)