Skip to content

Fix unit handling and restore deleted logic in ScreenGalHalo image methods - #56

Open
mandarmulherkar wants to merge 12 commits into
eblur:mainfrom
mandarmulherkar:fix-galhalo-variable-image
Open

mandarmulherkar wants to merge 12 commits into
eblur:mainfrom
mandarmulherkar:fix-galhalo-variable-image

Conversation

@mandarmulherkar

Copy link
Copy Markdown

Note on stacking: this branch is built on top of #55, so it contains those commits too. Either approach works for me:

Happy to split it differently if you'd prefer.

What is broken

ScreenGalHalo.fake_variable_image, ScreenGalHalo.variable_profile, and the module-level time_delay all raise on every call. Six distinct defects, from two unrelated causes:

From the astropy-Quantity migration (949a0b6, June 2022):

  • time_delay: d_cm = dkpc * 1.e3 * u.pc.to('cm') is a bare float, because Unit.to() returns a conversion factor rather than a Quantity. Dividing by c.c yields s/m, so .to('s') raises UnitConversionError for all inputs. time_delay is exported in all, so this is broken public API.
  • variable_profile: passes self.theta (a Quantity) to time_delay, which documents alpha as a bare float in arcsec; the comparison in _is_small_angle then fails.
  • variable_profile: the intensity accumulator is a plain ndarray while the values written into it carry 1/arcsec², raising TypeError.
  • fake_variable_image: reads self.lam_unit, removed in that migration (same as Fix unit handling in Halo.fake_image #55 ).
  • fake_variable_image: lmin/lmax compared against self.lam without units (same as Fix unit handling in Halo.fake_image #55 ).

From a docstring-only commit (fe4c562, June 2026):

  • fake_variable_image: the line computing var_profile was deleted, leaving the loop below referencing an undefined name. Its trailing comment (# intensity cube (NE x NTH)...) was left behind, orphaned.

Why it went unnoticed

No test or notebook exercises any of these methods. 949a0b6 also deleted the only assertions that referenced lam_unit, so the suite stayed green while the attribute disappeared.

The fix

self.lam and self.theta carry their own units, so the unit-string branching is unnecessary. One .to(...) with the appropriate equivalency handles any input unit, and .value strips to a plain array where scipy needs one. lmin/lmax are given lam's unit before comparison. time_delay lets astropy do the kpc→s conversion instead of hand-rolling it. The var_profile line is restored; the time_now block deleted alongside it is not needed, since variable_profile already defaults tnow to time[-1].

variable_profile now returns a Quantity; its docstring is updated to match.

Tests

First coverage for all of these. 159 → 165 passing. Reverting any individual fix turns its corresponding test red. Verified one at a time, not just in aggregate.

Noticed but deliberately not addressed

  • path_diff and time_delay are public and would be friendlier accepting Quantities, since halo.theta is one. That's an API change rather than a bug fix, happy to follow up.
  • theta_rad (galhalo.py:171) and lctm (:175) are assigned and never read.
  • deltat is computed inside a loop it doesn't depend on.
  • iend = max(...) looks like it drops the bin at lmax, in both fake_image and fake_variable_image.
  • The intensity accumulator assumes self.fabs is unitless; a Quantity flux passed to calculate_intensity would still fail.

Co-authored by Claude: Claude explained the physics and did the code reviews; I wrote the tests and code fixes.

fake_image raised AttributeError on every call: it branched on
self.lam_unit, an attribute removed in 949a0b6 when Halo migrated
to astropy Quantities. Two further errors were masked behind it:
self.lam was multiplied by u.angstrom despite already carrying a
unit, and lmin/lmax were compared against unitless floats.

self.lam carries its own unit, so the unit-string branching is
unnecessary. Convert explicitly with the spectral equivalency
instead, which handles any input unit rather than just two.

Adds the first test coverage for fake_image.
time_delay documents alpha as a bare float in arcsec, but
variable_profile passed self.theta, a Quantity, which broke the
comparison in _is_small_angle. Convert at the call site.

The intensity accumulator was a plain ndarray while the values
written into it carry 1/arcsec^2, so give it norm_int's unit.
Return type is now a Quantity; docstring updated to match.
Commit fe4c562 ("first try of remaking docstrings with claude")
removed the line that computes var_profile, leaving the loop below
referencing an undefined name. fake_variable_image raised
NameError on every call.

variable_profile already defaults tnow to time[-1], so the
separate time_now block that commit also removed is unnecessary.
Same lam_unit branching as Halo.fake_image: the attribute was
removed in 949a0b6 during the astropy Quantity migration, but the
read survived here. self.lam carries its own unit, so convert with
the spectral equivalency and strip to a plain array for the spline.
lmin and lmax are documented as bare floats in halo.lam units but
were compared directly against self.lam, an astropy Quantity,
raising UnitConversionError whenever either was passed. Give them
lam's unit before comparing, as in Halo.fake_image.
Assert the linearity of variable_profile in lc, which the previous
version computed but never checked. pytest.approx does not handle
astropy Quantities, so compare .value.

This branch has not been deployed

No deployments
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.

1 participant