Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to invert RescaleIntensity transform #998

Merged
merged 7 commits into from
Mar 4, 2023
Merged

Add support to invert RescaleIntensity transform #998

merged 7 commits into from
Mar 4, 2023

Conversation

nicoloesch
Copy link
Contributor

Fixes #993 .

Description
The RescaleIntensity Tranasform constitutes a min-max transformation, which is easily reversible. As a result, the class attribute self.invert_transform is added and the function rescale is touched slightly in order to revert the transformation.
As it would be helpful for the user to have access to the class attribute self.in_min_max (and the respective in_min, in_max), the respective attributes are added.

Known Issues

I checked the reversibility of the transform. If the resulting torch.Tensor after applying transform and transform.inverse() to a random input vector, a difference between input and output tensor results. I assume this originates from rounding issues between int and float. If the output tensor is kept as a float, the difference is 6.2028e-07 per pixel (max: 3.8147e-06 min: 3.8147e-06) for a 4x64x64x64 tensor. If the output tensor is casted to int, the difference becomes 0.2465 (max: 1. min: 0.)

in_tensor = torch.randint(256, (4, 64, 64, 64))

transform = torchio.transforms.preprocessing.RescaleIntensity(out_min_max=(-1,1))

transformed = transform(in_tensor)
reverted = transform.inverse()(transformed)

diff = a-reverted.int()  # or without casting to int

mean_ = torch.mean(diff.float())
max_ = torch.max(diff.float())
min_ = torch.min(diff.float())

Checklist

  • I have read the CONTRIBUTING docs and have a developer setup (especially important are pre-commitand pytest)
  • Non-breaking change (would not break existing functionality)
  • Breaking change (would cause existing functionality to change)
  • Tests added or modified to cover the changes
  • Integration tests passed locally by running pytest
    • I had a lot of warnings (510 passed, 3846 warnings), which I assume are not originating from my Code (only minor changes) but most likely from other parts of the projects
  • In-line docstrings updated
  • Documentation updated, tested running make html inside the docs/ folder
  • This pull request is ready to be reviewed
  • If the PR is ready and there are multiple commits, I have squashed them and force-pushed

@codecov
Copy link

codecov bot commented Mar 4, 2023

Codecov Report

Merging #998 (0850c0b) into main (821d176) will increase coverage by 0.03%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #998      +/-   ##
==========================================
+ Coverage   86.56%   86.59%   +0.03%     
==========================================
  Files          91       91              
  Lines        5774     5787      +13     
==========================================
+ Hits         4998     5011      +13     
  Misses        776      776              
Impacted Files Coverage Δ
...chio/transforms/preprocessing/intensity/rescale.py 94.73% <100.00%> (+1.55%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@fepegar fepegar changed the title Implemented reversible RescaleIntensity transform Add support to invert RescaleIntensity transform Mar 4, 2023
@fepegar fepegar merged commit 3a492b0 into fepegar:main Mar 4, 2023
@fepegar
Copy link
Owner

fepegar commented Mar 4, 2023

Excellent! Thanks a lot for your contribution, @nicoloesch!

@all-contributors please add @nicoloesch for code

@allcontributors
Copy link
Contributor

@fepegar

I couldn't determine any contributions to add, did you specify any contributions?
Please make sure to use valid contribution names.

I've put up a pull request to add @nicoloesch! 🎉

@fepegar fepegar mentioned this pull request Oct 24, 2023
1 task
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.

(Potential) Reversible RescaleIntensity Transform
2 participants