fix: fallback to 'track_id' if preferred color_by is missing in Tracks (#573). #625
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.



What is this PR
Why is this PR needed?
This PR addresses issue #573, where setting layer.color_by to a missing feature (e.g., “individual_factorized”) in the Napari Tracks layer raises an error, which can break the user experience during GUI usage.
A fallback mechanism is required to ensure robust, user-friendly behavior, especially when input data or properties are dynamically generated and may not always include the expected column.
What does this PR do?
• Introduces a utility function set_tracks_color_by() in movement/utils/napari_utils.py that:
• Tries to set layer.color_by to the desired feature (e.g., “individual_factorized”)
• Falls back to a safe default ("track_id") if the preferred feature is missing
• Optionally shows a message in the Napari overlay (if viewer is passed) to inform the user
• Replaces the direct layer.color_by = ... call in DataLoader._add_tracks_layer() with this new utility for safe assignment
• Adds a unit test test_set_tracks_color_by() under tests/test_unit/test_napari_plugin/test_napari_utils.py to verify:
• Preferred feature is set when available
• Fallback feature is used when preferred is missing
• Function behaves without error in both cases
⸻
🔗 References
• Fixes: #573
• Related discussions: napari color_by PR thread
⸻
🧪 How has this PR been tested?
• ✅ Manually tested through GUI (movement launch) using different datasets and missing features
• ✅ Ran unit tests via pytest and verified correct behavior
• ✅ Confirmed with pre-commit that code style, docstrings, and static checks pass
• ✅ Verified fallback message appears in Napari when needed
• ✅ Added tests simulate both success and fallback scenarios
⸻
• No
• Yes (please explain):
This is a non-breaking enhancement to improve fault-tolerance when assigning track color styles.
⸻
📚 Does this PR require an update to the documentation?
• No
• Yes (please explain):
The new utility function is internal and used within the plugin — it’s fully documented via docstrings, but doesn’t require user-facing documentation updates.
⸻
✅ Checklist