From 5cdf6331c4c9380ad9d6cc0c1eb1c323ce616453 Mon Sep 17 00:00:00 2001 From: Utz Ermel Date: Thu, 29 Aug 2024 10:57:39 -0400 Subject: [PATCH] Fix color choice based on object ID (#32) The data portal now also supports UniProtKB accessions as `Annotation.object_id`. UniProtKB accessions can contain letters, so the ID can't be directly converted to an integers anymore. This PR instead uses `hash(Annotation.object_id.split(':')[-1])` to determine consistent colors for annotations of the same objects. --- src/napari_cryoet_data_portal/_reader.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/napari_cryoet_data_portal/_reader.py b/src/napari_cryoet_data_portal/_reader.py index 70ca7d5..c5eec4e 100644 --- a/src/napari_cryoet_data_portal/_reader.py +++ b/src/napari_cryoet_data_portal/_reader.py @@ -24,9 +24,8 @@ def _annotation_color(annotation: Annotation) -> np.ndarray: """Maps an annotation to a color based on its object_id.""" try: object_id = int(annotation.object_id.split(":")[-1]) - except RuntimeError as e: - logger.error("Failed to parse integer from object_id: %s\%s", annotation.object_id, e) - return DEFAULT_OBJECT_COLOR + except ValueError as e: + object_id = hash(annotation.object_id.split(":")[-1]) color = OBJECT_COLORMAP(object_id % len(OBJECT_COLORMAP.color_stops)) return np.array(color.rgba)