Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions surfaces_tools/widgets/auto_representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ def _make_representation(
atom_show_threshold=1,
):
representation = awb_viewers.NglViewerRepresentation(
style_id=style_id or f"{viewer.REPRESENTATION_PREFIX}{name}",
style_id=style_id
or awb_viewers.encode_representation_style_id(
viewer.REPRESENTATION_PREFIX,
representation_type=representation_type,
size=3,
color="element",
token=name,
),
indices=indices,
deletable=deletable,
atom_show_threshold=atom_show_threshold,
Expand Down Expand Up @@ -142,7 +149,12 @@ def __init__(self, viewer, **kwargs):
self.observe(self._observe_structure, names="structure")

def _observe_structure(self, _=None):
if isinstance(getattr(self.viewer, "structure", None), Atoms):
structure = getattr(self.viewer, "structure", None)
has_stored_representations = isinstance(structure, Atoms) and any(
array.startswith(self.viewer.REPRESENTATION_PREFIX)
for array in structure.arrays
)
if isinstance(structure, Atoms) and not has_stored_representations:
reset_default_representation(self.viewer)
self.status.value = ""

Expand Down
8 changes: 8 additions & 0 deletions tests/test_auto_representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def test_apply_auto_representations(self):
self.assertEqual(viewer._all_representations[0].selection.value, "3..4")
self.assertEqual(viewer._all_representations[1].type.value, "spacefill")
self.assertEqual(viewer._all_representations[1].selection.value, "1..2")
expected_style_id = viewers.encode_representation_style_id(
viewer.REPRESENTATION_PREFIX,
representation_type="spacefill",
size=3,
color="element",
token="non_molecule",
)
self.assertEqual(viewer._all_representations[1].style_id, expected_style_id)

def test_structure_change_resets_stale_auto_representations(self):
first_structure = Atoms(
Expand Down