Skip to content

Fix NaN values in inverse Box-Cox transform for Lambda > 0 - #577

Merged
dnerini merged 1 commit into
pySTEPS:masterfrom
raashish1601:fix/boxcox-inverse-nan
Oct 8, 2026
Merged

dnerini merged 1 commit into
pySTEPS:masterfrom
raashish1601:fix/boxcox-inverse-nan

Conversation

@raashish1601

Copy link
Copy Markdown
Contributor

The inverse of boxcox_transform returns NaN instead of 0 for dry pixels when Lambda > 0.

The forward transform sets values below the threshold to zerovalue = threshold - 1 in transformed space. For Lambda > 0 that value is usually below -1 / Lambda. For example, with threshold 0.1 and Lambda = 0.5 it is about -2.37, so Lambda * R + 1 is negative and np.log gives NaN. The inverse then runs R[R < threshold] = zerovalue, but NaN < threshold is False, so these pixels stay NaN.

R = np.array([0.0, 0.05, 1.0])
md = {"transform": None, "unit": "mm/h", "threshold": 0.1, "zerovalue": 0.0, "accutime": 5}
Rt, md = boxcox_transform(R.copy(), md, Lambda=0.5)
boxcox_transform(Rt, md, inverse=True)[0]
# array([nan, nan,  1.])   expected [0, 0, 1]

The fix finds the zeros in transformed space, before the back-transformation. The transform is monotonic, so for finite values this gives the same pixels as before. Lambda = 0 and the dB/sqrt transforms are not affected.

I added a round trip test with zeros for Lambda in 0, 0.5 and 1. It fails on master for 0.5 and 1. test_utils_transformation.py and test_utils_conversion.py pass, and black is clean.

@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.76%. Comparing base (19e5f0d) to head (219a966).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #577      +/-   ##
==========================================
+ Coverage   84.75%   84.76%   +0.01%     
==========================================
  Files         170      170              
  Lines       15104    15114      +10     
==========================================
+ Hits        12801    12811      +10     
  Misses       2303     2303              
Flag Coverage Δ
unit_tests 84.76% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dnerini

dnerini commented Oct 8, 2026

Copy link
Copy Markdown
Member

hi @raashish1601, thanks for the fix, looks good to me!

@dnerini
dnerini self-requested a review October 8, 2026 20:04
@dnerini
dnerini merged commit f4ebd1d into pySTEPS:master Oct 8, 2026
10 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