Skip to content

Commit

Permalink
Fix issue with labels for subsets that appear multiple times in a vie…
Browse files Browse the repository at this point in the history
…wer.
  • Loading branch information
Carifio24 committed Aug 6, 2024
1 parent 2da4cc1 commit cc9bffc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
4 changes: 2 additions & 2 deletions glue_ar/common/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from glue_ar.common.export_options import ar_layer_export
from glue_ar.common.gltf_builder import GLTFBuilder
from glue_ar.common.usd_builder import USDBuilder
from glue_ar.utils import Bounds, BoundsWithResolution
from glue_ar.utils import Bounds, BoundsWithResolution, export_label_for_layer

from typing import List, Tuple, Union

Expand Down Expand Up @@ -41,7 +41,7 @@ def export_viewer(viewer_state: Vispy3DViewerState,
layer_groups = defaultdict(list)
export_groups = defaultdict(list)
for layer_state in layer_states:
name, export_state = state_dictionary[layer_state.layer.label]
name, export_state = state_dictionary[export_label_for_layer(layer_state)]
key = (type(layer_state), name)
layer_groups[key].append(layer_state)
export_groups[key].append(export_state)
Expand Down
12 changes: 10 additions & 2 deletions glue_ar/common/export_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from echo import SelectionCallbackProperty
from glue.core import BaseData
from glue.core.data_combo_helper import ComboHelper
from glue.core.state_objects import State
from glue.viewers.common.viewer import LayerArtist
from glue_vispy_viewers.common.layer_state import LayerState

from glue_ar.utils import data_count, export_label_for_layer

from typing import Iterable, Union


__all__ = ["ARExportDialogState"]
Expand All @@ -13,7 +20,7 @@ class ARExportDialogState(State):
compression = SelectionCallbackProperty()
method = SelectionCallbackProperty()

def __init__(self, layers):
def __init__(self, layers: Iterable[LayerState]):

super(ARExportDialogState, self).__init__()

Expand All @@ -27,4 +34,5 @@ def __init__(self, layers):

self.layers = layers
self.layer_helper = ComboHelper(self, 'layer')
self.layer_helper.choices = [state.layer.label for state in self.layers]
add_data_label = data_count(self.layers) > 1
self.layer_helper.choices = [export_label_for_layer(layer_state, add_data_label) for layer_state in layers]
9 changes: 4 additions & 5 deletions glue_ar/qt/export_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from glue_ar.common.export_options import ar_layer_export
from glue_ar.common.export_state import ARExportDialogState
from glue_ar.utils import export_label_for_layer

from qtpy.QtWidgets import QCheckBox, QDialog, QHBoxLayout, QLabel, QLayout, QLineEdit, QWidget
from qtpy.QtGui import QIntValidator, QDoubleValidator
Expand All @@ -34,15 +35,15 @@ def __init__(self, parent=None, viewer=None):
self.ui = load_ui('export_dialog.ui', self, directory=os.path.dirname(__file__))

self._layer_export_states: Dict[str, Dict[str, State]] = {
self.state.label_for_layer(layer): {}
export_label_for_layer(layer): {}
for layer in layers
}

self.state_dictionary: Dict[str, Tuple[str, State]] = {}
self._on_layer_change(self.state.layer)
for layer in layers:
method = self.state.method
label = self.state.label_for_layer(layer)
label = export_label_for_layer(layer)
if label in self.state_dictionary:
_, state = self.state_dictionary[label]
else:
Expand All @@ -67,7 +68,7 @@ def __init__(self, parent=None, viewer=None):
self.state.add_callback('method', self._on_method_change)

def _layer_for_label(self, label: str) -> VispyLayerArtist:
return next(layer for layer in self.state.layers if self.state.label_for_layer(layer) == label)
return next(layer for layer in self.state.layers if export_label_for_layer(layer) == label)

def _widgets_for_property(self,
instance: HasCallbackProperties,
Expand Down Expand Up @@ -118,8 +119,6 @@ def _on_layer_change(self, layer_name: str):
state = ar_layer_export.options_class(layer_state_cls, method)()
self.state_dictionary[layer_name] = (method, state)

print(method_names)
print(method)
with delay_callback(self.state, 'method'):
self.state.method_helper.choices = method_names
method_change = method != self.state.method
Expand Down
29 changes: 18 additions & 11 deletions glue_ar/qt/export_tool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os.path import splitext
from glue_vispy_viewers.volume import viewer_state
from glue_vispy_viewers.volume.volume_viewer import VispyVolumeViewerMixin

from qtpy import compat
Expand Down Expand Up @@ -59,14 +60,20 @@ def activate(self):
layer.enabled and layer.state.visible]
bounds = xyz_bounds(self.viewer.state, with_resolution=isinstance(self.viewer, VispyVolumeViewerMixin))

worker = Worker(export_viewer,
viewer_state=self.viewer.state,
layer_states=layer_states,
bounds=bounds,
state_dictionary=dialog.state_dictionary,
filepath=export_path)
exporting_dialog = ExportingDialog(parent=self.viewer, filetype=filetype)
worker.result.connect(exporting_dialog.close)
worker.error.connect(exporting_dialog.close)
worker.start()
exporting_dialog.exec_()
# worker = Worker(export_viewer,
# viewer_state=self.viewer.state,
# layer_states=layer_states,
# bounds=bounds,
# state_dictionary=dialog.state_dictionary,
# filepath=export_path)
# exporting_dialog = ExportingDialog(parent=self.viewer, filetype=filetype)
# worker.result.connect(exporting_dialog.close)
# worker.error.connect(exporting_dialog.close)
# worker.start()
# exporting_dialog.exec_()

export_viewer(viewer_state=self.viewer.state,
layer_states=layer_states,
bounds=bounds,
state_dictionary=dialog.state_dictionary,
filepath=export_path)
11 changes: 10 additions & 1 deletion glue_ar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from glue_vispy_viewers.common.layer_state import LayerState, VispyLayerState
from glue_vispy_viewers.volume.layer_state import VolumeLayerState
from glue_vispy_viewers.volume.viewer_state import Vispy3DViewerState
from numpy import array, inf, isnan, ndarray
from numpy import array, inf, isnan, logical_and, ndarray

from typing import Literal, overload, Iterable, List, Optional, Tuple, Union

Expand All @@ -27,6 +27,15 @@ def data_count(layers: Iterable[Union[LayerArtist, LayerState]]) -> int:
return len(data)


def export_label_for_layer(layer: Union[LayerArtist, LayerState],
add_data_label: bool = True) -> str:
if (not add_data_label) or isinstance(layer.layer, BaseData):
return layer.layer.label
else:
data = layer.layer.data
return f"{layer.layer.label} ({data.label})"


def layers_to_export(viewer: Viewer) -> List[LayerArtist]:
return list(filter(lambda artist: artist.enabled and artist.visible, viewer.layers))

Expand Down

0 comments on commit cc9bffc

Please sign in to comment.