Skip to content

fix: handle illumination sources without a hardware source code#518

Merged
Alpaca233 merged 2 commits into
Cephla-Lab:masterfrom
Alpaca233:bugfix/illumination-source-code-handling
Mar 22, 2026
Merged

fix: handle illumination sources without a hardware source code#518
Alpaca233 merged 2 commits into
Cephla-Lab:masterfrom
Alpaca233:bugfix/illumination-source-code-handling

Conversation

@Alpaca233
Copy link
Copy Markdown
Collaborator

Summary

  • get_illumination_source_code() now returns -1 (instead of 0) when a channel exists but has no hardware source code (e.g. software-only illumination sources not using the microcontroller)
  • _is_led_matrix() now checks 0 <= source < 10 so that -1 correctly evaluates to False instead of being misidentified as LED matrix

Test plan

  • Verify software-only illumination channels (no source code) no longer trigger LED matrix logic
  • Verify LED matrix channels (source code 0-9) still work correctly

🤖 Generated with Claude Code

- get_illumination_source_code() returns -1 when channel has no source
  code (e.g. software-only illumination not using the controller)
- _is_led_matrix() now checks 0 <= source < 10, so -1 correctly
  evaluates to False instead of being treated as LED matrix

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent software-only illumination channels (with no microcontroller “source code”) from being treated as LED-matrix sources by introducing a -1 sentinel value and tightening the LED-matrix detection logic in LiveController.

Changes:

  • Update AcquisitionChannel.get_illumination_source_code() documentation and return value when the illumination channel cannot be resolved.
  • Update LiveController._is_led_matrix() to treat only source codes in the 0–9 range as LED matrix sources (excluding -1).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
software/control/models/acquisition_config.py Adjusts illumination source code resolution to use -1 when the primary illumination channel cannot be found.
software/control/core/live_controller.py Narrows LED-matrix detection to 0 <= source < 10 to ensure -1 is not misclassified as LED matrix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"""
ill_channel_name = self.primary_illumination_channel
if not ill_channel_name:
return 0
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_illumination_source_code() docstring says it returns -1 when not found, but the if not ill_channel_name: branch still returns 0. Since 0 is a valid LED-matrix source code, this can incorrectly classify “no primary channel configured” as LED matrix. Consider returning -1 here (or otherwise making the sentinel consistent).

Suggested change
return 0
return -1

Copilot uses AI. Check for mistakes.
Comment on lines 223 to 226
ill_channel = illumination_config.get_channel_by_name(ill_channel_name)
if not ill_channel:
return 0
return -1
return illumination_config.get_source_code(ill_channel)
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn’t yet handle the case described in the PR summary (“channel exists but has no hardware source code”). IlluminationChannelConfig.get_source_code() falls back to 0 when controller_port_mapping has no entry, so software-only/N/A channels will still appear as source code 0. Consider detecting a missing mapping (and no legacy source_code/led_matrix_pattern) and returning -1 in that case.

Copilot uses AI. Check for mistakes.
Comment on lines 211 to 226
def get_illumination_source_code(self, illumination_config: "IlluminationChannelConfig") -> int:
"""Get the illumination source code for the primary illumination channel.

Args:
illumination_config: The machine's illumination channel configuration.

Returns:
Source code (int) for the primary illumination channel, or 0 if not found.
Source code (int) for the primary illumination channel, or -1 if not found.
"""
ill_channel_name = self.primary_illumination_channel
if not ill_channel_name:
return 0
ill_channel = illumination_config.get_channel_by_name(ill_channel_name)
if not ill_channel:
return 0
return -1
return illumination_config.get_source_code(ill_channel)
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add/adjust tests for the new -1 sentinel behavior (e.g., missing primary channel, missing channel by name, and existing channel with no controller_port_mapping entry) to ensure software-only channels don’t trigger LED-matrix logic and that 0–9 source codes still do.

Copilot uses AI. Check for mistakes.
@@ -85,7 +85,7 @@ def _get_illumination_wavelength(self) -> Optional[int]:

def _is_led_matrix(self) -> bool:
"""Check if current configuration is LED matrix (source code < 10)."""
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring says “source code < 10”, but the logic now specifically treats 0–9 as LED matrix and excludes -1. Update the docstring to match the actual range and sentinel behavior to avoid confusion for future changes.

Suggested change
"""Check if current configuration is LED matrix (source code < 10)."""
"""Return True if the current configuration uses LED-matrix illumination.
LED-matrix sources are encoded with integer IDs in the range 09 inclusive.
Negative values (e.g. -1) are reserved forno source/ invalid and are
not treated as LED matrix.
"""

Copilot uses AI. Check for mistakes.
def _is_led_matrix(self) -> bool:
"""Check if current configuration is LED matrix (source code < 10)."""
return self._get_illumination_source() < 10
return 0 <= self._get_illumination_source() < 10
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get_illumination_source() returns 0 when currentConfiguration or ill_config is missing, and 0 is treated as LED matrix here. If the intent is “unknown/unconfigured source => not LED matrix”, consider having _get_illumination_source() return -1 for those cases (or make _is_led_matrix() return False when config is unavailable).

Suggested change
return 0 <= self._get_illumination_source() < 10
# Treat missing configuration as "not LED matrix" instead of assuming source code 0.
if not self.currentConfiguration:
return False
ill_config = self._get_illumination_config()
if not ill_config:
return False
source_code = self.currentConfiguration.get_illumination_source_code(ill_config)
return 0 <= source_code < 10

Copilot uses AI. Check for mistakes.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Alpaca233 Alpaca233 merged commit cc677e2 into Cephla-Lab:master Mar 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants