Skip to content

Add validation for detector module/gap geometry inference #53

@coderabbitai

Description

@coderabbitai

Context

During review of PR #45 (#45 (comment)), a potential issue was identified in mdx2/dxtbx_machinery.py where detector module and gap calculations could silently produce incorrect geometry.

Requested by: @spmeisburger
Related PR: #45
File: mdx2/dxtbx_machinery.py (lines ~21-75)

Problem

The current code computes num_modules_* and gap_* using integer division (//), which can silently hide remainders and produce plausible-but-wrong geometry values. This could lead to incorrect downstream binning/tiling without any error indication.

Requested Solution

Phase 1: Unit Tests (Required First)

Create comprehensive unit tests for the detector geometry validation in Experiment.__init__:

  • Test cases with valid, evenly-divisible detector layouts
  • Test cases with invalid layouts (non-divisible gaps, wrong module sizes)
  • Edge cases (single module, zero gaps, etc.)
  • Boundary conditions

Phase 2: Implementation

Add explicit validation checks to ensure:

  1. Module sizes (self._module_size_fast and self._module_size_slow) are > 0
  2. num_modules_fast and num_modules_slow (computed as image_size[axis] // module_size) are >= 1
  3. total_gap_* (computed as image_size[axis] - (num_modules_* * module_size)) is >= 0
  4. When num_modules_* > 1, verify that total_gap_* % (num_modules_* - 1) == 0 (gaps must be evenly divisible)
  5. Raise clear RuntimeError messages when validation fails

This will make geometry failures explicit instead of producing silently wrong values.

Acceptance Criteria

  • Unit tests created covering valid and invalid detector geometries
  • All tests initially demonstrate current behavior
  • Validation logic added to Experiment.__init__
  • Tests pass with new validation
  • Clear error messages guide users when geometry is invalid

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions