Skip to content

Commit

Permalink
Fix color choice based on object ID (#32)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
uermel committed Aug 29, 2024
1 parent ce940d2 commit 5cdf633
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/napari_cryoet_data_portal/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 5cdf633

Please sign in to comment.