Skip to content

Conversation

@j-c-c
Copy link
Collaborator

@j-c-c j-c-c commented Mar 28, 2025

Validates downsample against Matlab to address #1244

This PR creates a legacy flag for Image.downsample/Volume.downsample that reproduces the Matlab downsample output up to np.allclose tolerances.

I will upload notebooks, with matlab invocations documented, that demonstrate these results. I still need add a legacy flag test and check whether some desired properties hold.

@j-c-c j-c-c self-assigned this Mar 28, 2025
@j-c-c j-c-c linked an issue Mar 28, 2025 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Mar 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.61%. Comparing base (9613a2b) to head (2f05e8b).
Report is 6 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1252   +/-   ##
========================================
  Coverage    90.61%   90.61%           
========================================
  Files          132      132           
  Lines        14143    14153   +10     
========================================
+ Hits         12815    12825   +10     
  Misses        1328     1328           

☔ View full report in Codecov by Sentry.
📢 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.

@j-c-c
Copy link
Collaborator Author

j-c-c commented Mar 28, 2025

Attaching the notebooks that validate Image and Volume downsample (with legacy=True) against Matlab downsample.
validate_downsample-legacy.txt
vol_ds.txt

@j-c-c j-c-c force-pushed the validate_ds branch 2 times, most recently from 65c7c04 to 927ee68 Compare April 2, 2025 14:12
@j-c-c j-c-c marked this pull request as ready for review April 8, 2025 13:57
Copy link
Collaborator

@janden janden left a comment

Choose a reason for hiding this comment

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

Looks great! Just one thing.

ims_ds_legacy = ims.downsample(res_ds, legacy=True)

# ASPIRE-Python downsample with centering adjustments for odd resolution images.
shifts = 0.5 * np.ones((n_img, 2), dtype=dtype)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we replace this with the formula that we found for the shifts? I'm worried that this is only half-pixel in certain special cases like this one.

Copy link
Collaborator Author

@j-c-c j-c-c Apr 22, 2025

Choose a reason for hiding this comment

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

My notes from our meeting are a little incomplete, but what I have working locally for each case is this.

even -> even: no shift needed
even -> odd: shift by 1/2
odd -> odd: shift by 1/2 - res_ds/(2 * res_og)
odd -> even: shift by -res_ds/(2 * res_og)

I'm not getting allclose on the odd -> even case, so maybe I have something wrong here. For what it's worth, the original test works for different downsample ratios since the shifting occurs before and after downsampling depending on where the odd resolution is.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But shouldn't the amount of shifting required differ depending on the downsampling ratio (per the formulas you give above)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[Edit Above: I had a typo in the formulas above... was missing a factor of 2 in the denominators.]

Yes, but the 1/2 pixel shift (on line 205) which happens prior to downsampling is converted in the downsampled image into a shift of res_ds/(2 * res_og) pixels by the act of downsampling. So the ratio ends up being accounted for. I believe this is correct and have confirmed it for several different downsample ratios, but it's possible I'm misunderstanding something.

I suspect that I am not getting allclose for the last case due to some difference in our Image.shift method on even vs odd images.

@j-c-c j-c-c merged commit 5e7413a into develop Apr 24, 2025
35 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.

Validate downsampling against MATLAB.

3 participants