Files
ruflo/separability_analysis.py
T

131 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Detailed analysis of the separability_matrix bug for nested CompoundModels.
"""
def analyze_bug():
"""Analyze the bug in separability_matrix for nested CompoundModels."""
print("=== SEPARABILITY MATRIX BUG ANALYSIS ===")
print()
print("## Problem Case")
print("```python")
print("cm = ((m.Shift(1) & m.Shift(2)) | m.Mapping([0, 1, 0, 1])) * (m.Polynomial1D(1) & m.Polynomial1D(2))")
print("result = separability_matrix(cm)")
print("```")
print()
print("## Model Structure Breakdown")
print()
print("Let's trace through the model structure:")
print()
print("1. **Base Components:**")
print(" - Shift(1): 1 input → 1 output")
print(" - Shift(2): 1 input → 1 output")
print(" - Mapping([0,1,0,1]): 2 inputs → 4 outputs")
print(" - Polynomial1D(1): 1 input → 1 output")
print(" - Polynomial1D(2): 1 input → 1 output")
print()
print("2. **Step 1: Shift(1) & Shift(2) (parallel combination)**")
print(" - Result: 2 inputs → 2 outputs")
print(" - Separability matrix: 2×2 identity matrix")
print(" ```")
print(" [[True, False],")
print(" [False, True ]]")
print(" ```")
print()
print("3. **Step 2: (Shift(1) & Shift(2)) | Mapping([0,1,0,1]) (serial combination)**")
print(" - Input: 2×2 matrix from step 1")
print(" - Mapping([0,1,0,1]): takes 2 inputs, produces 4 outputs")
print(" - Result: 2 inputs → 4 outputs (INCORRECT: should be 4 outputs!)")
print(" - This is where the first issue occurs!")
print()
print("4. **Step 3: Polynomial1D(1) & Polynomial1D(2) (parallel combination)**")
print(" - Result: 2 inputs → 2 outputs")
print(" - Separability matrix: 2×2 identity matrix")
print()
print("5. **Step 4: Left * Right (arithmetic combination)**")
print(" - Left side: 4×2 matrix (4 outputs, 2 inputs)")
print(" - Right side: 2×2 matrix (2 outputs, 2 inputs)")
print(" - **FAILURE**: Arithmetic operations require same dimensions!")
print(" - Expected: 4×2 matrix (final result should be 4 outputs, 2 inputs)")
print()
print("## Root Cause Analysis")
print()
print("### Issue 1: Incorrect n_outputs calculation in serial composition")
print()
print("In `MockCompoundModel.__init__` when `op == '|'` (serial):")
print("```python")
print("elif op == '|': # serial combination")
print(" n_inputs = right.n_inputs")
print(" n_outputs = left.n_outputs # BUG: Should be left.n_outputs!")
print("```")
print()
print("The issue is in the model dimension calculation. When we have:")
print("`(Shift(1) & Shift(2)) | Mapping([0, 1, 0, 1])`")
print()
print("- Left side (Shift(1) & Shift(2)): 2 inputs → 2 outputs")
print("- Right side (Mapping): 2 inputs → 4 outputs")
print("- Serial composition should result in: 2 inputs → 4 outputs")
print(" (The number of inputs comes from the right side,")
print(" the number of outputs comes from the left side)")
print()
print("But the current logic incorrectly sets:")
print("```python")
print("n_outputs = left.n_outputs # = 2, should be 4!")
print("```")
print()
print("### Issue 2: Separability matrix computation for serial composition")
print()
print("The `_cdot` function (handling `|` operator) in separable.py:")
print("1. Swaps left and right operands")
print("2. Computes coordinate matrices")
print("3. Performs matrix multiplication")
print()
print("However, the coordinate matrix computation may not properly handle")
print("the case where the left operand has more outputs than the compound")
print("model thinks it should have.")
print()
print("## Expected vs Actual Results")
print()
print("**Expected final result:**")
print("- Shape: (4, 2) - 4 outputs, 2 inputs")
print("- Matrix showing which inputs affect which outputs")
print()
print("**Actual result:**")
print("- Error: 'Models must have same dimensions for arithmetic'")
print("- Because left side is computed as 4×2 but right side is 2×2")
print()
print("## The Fix")
print()
print("The core issue is in the compound model dimension calculation.")
print("For serial composition (A | B), the correct dimensions are:")
print("- `n_inputs = B.n_inputs` (inputs to the pipeline)")
print("- `n_outputs = A.n_outputs` (outputs from the pipeline)")
print()
print("However, there may be additional issues in the separability matrix")
print("computation logic that need to be addressed once the dimensions")
print("are correct.")
print()
print("## Specific Code Location Needing Fix")
print()
print("**File: `/workspaces/claude-code-flow/astropy_fix/astropy/modeling/core.py`**")
print("Look for the `CompoundModel.__init__` method or similar logic")
print("that computes `n_inputs` and `n_outputs` for serial composition.")
print()
print("**File: `/workspaces/claude-code-flow/astropy_fix/astropy/modeling/separable.py`**")
print("The `_cdot` function may need review to ensure coordinate matrices")
print("are computed correctly for nested compound models.")
if __name__ == "__main__":
analyze_bug()