-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement associations between Data layers #2761
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
488c09b
implement associations between Data layers
bmorris3 fe40115
support for Imviz blinking, glue 'bad' masks
bmorris3 5afa49f
prevent children of children in data associations
bmorris3 55d2e68
ensure requested parent in data associations exists
bmorris3 00785d7
more efficient layer icon updates
bmorris3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,6 +356,9 @@ | |
self.hub.subscribe(self, SubsetUpdateMessage, | ||
handler=lambda msg: self._clear_object_cache(msg.subset.label)) | ||
|
||
# Store for associations between Data entries: | ||
self._data_associations = self._init_data_associations() | ||
|
||
# Subscribe to messages that result in changes to the layers | ||
self.hub.subscribe(self, AddDataMessage, | ||
handler=self._on_layers_changed) | ||
|
@@ -473,9 +476,11 @@ | |
if hasattr(msg, 'data'): | ||
layer_name = msg.data.label | ||
is_wcs_only = msg.data.meta.get(_wcs_only_label, False) | ||
is_not_child = self._get_assoc_data_parent(layer_name) is None | ||
elif hasattr(msg, 'subset'): | ||
layer_name = msg.subset.label | ||
is_wcs_only = False | ||
is_not_child = True | ||
else: | ||
raise NotImplementedError(f"cannot recognize new layer from {msg}") | ||
|
||
|
@@ -490,13 +495,25 @@ | |
self.state.layer_icons = {**self.state.layer_icons, | ||
layer_name: orientation_icons.get(layer_name, | ||
wcs_only_refdata_icon)} | ||
else: | ||
elif is_not_child: | ||
self.state.layer_icons = { | ||
**self.state.layer_icons, | ||
layer_name: alpha_index(len([ln for ln, ic in self.state.layer_icons.items() | ||
if not ic.startswith('mdi-')])) | ||
if not ic.startswith('mdi-') and | ||
self._get_assoc_data_parent(ln) is None])) | ||
} | ||
|
||
# all remaining layers at this point have a parent: | ||
for layer_name in self.state.layer_icons: | ||
children_layers = self._get_assoc_data_children(layer_name) | ||
if children_layers is not None: | ||
parent_icon = self.state.layer_icons[layer_name] | ||
for i, child_layer in enumerate(children_layers, start=1): | ||
self.state.layer_icons = { | ||
**self.state.layer_icons, | ||
child_layer: f'{parent_icon}{i}' | ||
} | ||
|
||
def _change_reference_data(self, new_refdata_label, viewer_id=None): | ||
""" | ||
Change reference data to Data with ``data_label``. | ||
|
@@ -1251,7 +1268,7 @@ | |
|
||
return new_state | ||
|
||
def add_data(self, data, data_label=None, notify_done=True): | ||
def add_data(self, data, data_label=None, notify_done=True, parent=None): | ||
""" | ||
Add data to the Glue ``DataCollection``. | ||
|
||
|
@@ -1266,10 +1283,12 @@ | |
The name associated with this data. If none is given, label is pulled | ||
from the input data (if `~glue.core.data.Data`) or a generic name is | ||
generated. | ||
notify_done: bool | ||
notify_done : bool | ||
Flag controlling whether a snackbar message is set when the data is | ||
added to the app. Set to False to avoid overwhelming the user if | ||
lots of data is getting loaded at once. | ||
parent : str, optional | ||
Associate the added Data entry as the child of layer ``parent``. | ||
""" | ||
|
||
if not data_label and hasattr(data, "label"): | ||
|
@@ -1280,6 +1299,18 @@ | |
|
||
self.data_collection[data_label] = data | ||
|
||
# manage associated Data entries: | ||
self._add_assoc_data_as_parent(data_label) | ||
if parent is not None: | ||
# Does the parent Data have a parent? If so, raise error: | ||
parent_of_parent = self._get_assoc_data_parent(parent) | ||
if parent_of_parent is not None: | ||
raise NotImplementedError('Data associations are currently supported ' | ||
'between root layers (without parents) and their ' | ||
f'children, but the proposed parent "{parent}" has ' | ||
f'parent "{parent_of_parent}".') | ||
self._set_assoc_data_as_child(data_label, new_parent_label=parent) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we also need to validate that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing, done in 55d2e68. |
||
|
||
# Send out a toast message | ||
if notify_done: | ||
snackbar_message = SnackbarMessage( | ||
|
@@ -1998,6 +2029,17 @@ | |
if layer.layer.data.label != data_label: | ||
layer.visible = False | ||
|
||
# if Data has children, update their visibilities to match Data: | ||
assoc_children = self._get_assoc_data_children(data_label) | ||
for layer in viewer.layers: | ||
for data_label in assoc_children: | ||
if layer.layer.data.label == data_label: | ||
if visible and not layer.visible: | ||
layer.visible = True | ||
layer.update() | ||
else: | ||
layer.visible = visible | ||
|
||
# update data menu - selected_data_items should be READ ONLY, not modified by the user/UI | ||
selected_items = viewer_item['selected_data_items'] | ||
data_id = self._data_id_from_label(data_label) | ||
|
@@ -2577,3 +2619,27 @@ | |
raise KeyError(f'{name} not found in app.state.tray_items') | ||
|
||
return tray_item | ||
|
||
def _init_data_associations(self): | ||
# assume all Data are parents: | ||
data_associations = { | ||
data.label: {'parent': None, 'children': []} | ||
for data in self.data_collection | ||
} | ||
return data_associations | ||
|
||
def _add_assoc_data_as_parent(self, data_label): | ||
self._data_associations[data_label] = {'parent': None, 'children': []} | ||
|
||
def _set_assoc_data_as_child(self, data_label, new_parent_label): | ||
# Data has a new parent: | ||
self._data_associations[data_label]['parent'] = new_parent_label | ||
# parent has a new child: | ||
self._data_associations[new_parent_label]['children'].append(data_label) | ||
|
||
def _get_assoc_data_children(self, data_label): | ||
# intentionally not recursive for now, just one generation: | ||
return self._data_associations.get(data_label, {}).get('children', []) | ||
|
||
def _get_assoc_data_parent(self, data_label): | ||
return self._data_associations.get(data_label, {}).get('parent') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will update the state a bunch of times. Can we instead either build this logic into above or use a dictionary-comprehension to build all the new entries and then just apply to the state once?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. In 00785d7, I instead gather the child layer icon updates in a different dict, and update
self.state.layer_icons
once at the end.