-
Notifications
You must be signed in to change notification settings - Fork 23
Fix: Prevent negative indices in downsample_fft when image dimension is less than MIN_IMG_SIZE
#44
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
Open
leonardodtang
wants to merge
7
commits into
rohitrango:main
Choose a base branch
from
leonardodtang:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8ea103
Added self.min_dim in AbstractRegistration to replace MIN_IMG_SIZE de…
leonardodtang f000854
moved min_dim logic to utils, reverted interpolation change in abstra…
leonardodtang 07b219a
removed some unused imports and whitespace
leonardodtang cdf6d68
added test for registration on synthetic images with low z dimension …
leonardodtang 21d2b6c
Merge branch 'main' into main
leonardodtang 3770f0c
Added registration runs for comprehensive test in test_lowdim.py
leonardodtang ea2eaa3
fixed expected_dims in test_lowdim.py
leonardodtang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import pytest | ||
| import torch | ||
| import numpy as np | ||
| from pathlib import Path | ||
| import SimpleITK as sitk | ||
|
|
||
| from fireants.registration.affine import AffineRegistration | ||
| from fireants.registration.greedy import GreedyRegistration | ||
| from fireants.registration.syn import SyNRegistration | ||
| from fireants.io.image import Image, BatchedImages | ||
| from fireants.io import FakeBatchedImages | ||
| from .test_moments_registration import generate_3d_ellipse | ||
| try: | ||
| from .conftest import dice_loss | ||
| except ImportError: | ||
| from conftest import dice_loss | ||
|
|
||
| def create_synthetic_data_np(size): | ||
| rng = np.random.RandomState(42) | ||
|
|
||
| # Generate random axes lengths | ||
| a = rng.uniform(size // 4, 3*size // 8) | ||
| b = rng.uniform(3*size // 16, a-5) | ||
| c = rng.uniform(2*size // 16, b-5) | ||
| axes = (a, b, c) | ||
|
|
||
| # Generate fixed image | ||
| fixed_center = (2 * size // 32, -3*size // 32, size // 32) | ||
| fixed_angles = (np.pi/6, np.pi/4, -np.pi/3) | ||
| fixed_arr = generate_3d_ellipse(size=size, axes=axes, center=fixed_center, | ||
| angles=fixed_angles, rng=rng) | ||
|
|
||
| # Generate moving image with different center and rotation | ||
| moving_center = (-4*size // 32, size // 32, -2*size // 32) | ||
| moving_angles = (-np.pi/3, -np.pi/6, np.pi/2) | ||
| moving_arr = generate_3d_ellipse(size=size, axes=axes, center=moving_center, | ||
| angles=moving_angles, rng=rng) | ||
|
|
||
| return fixed_arr, moving_arr | ||
|
|
||
|
|
||
| def test_lowdim(): | ||
| test_data_dir = Path(__file__).parent / "test_data" | ||
| fixed_image_path = str(test_data_dir / "deformable_image_1.nii.gz") | ||
| moving_image_path = str(test_data_dir / "deformable_image_2.nii.gz") | ||
|
|
||
| downscales = [24, 12, 6] | ||
| expected_zdims = [128 // (128 // zdim) for zdim in downscales] | ||
| for downscale, expected_zdim in zip(downscales, expected_zdims): | ||
| if any(not Path(f).exists() for f in [fixed_image_path, moving_image_path]): | ||
| fixed_np, moving_np = create_synthetic_data_np(128) | ||
| else: | ||
| fixed_img = Image.load_file(fixed_image_path) | ||
| moving_img = Image.load_file(moving_image_path) | ||
| fixed_np = sitk.GetArrayFromImage(fixed_img.itk_image) | ||
| moving_np = sitk.GetArrayFromImage(moving_img.itk_image) | ||
| expected_zdim = fixed_np.shape[2] // (fixed_np.shape[2] // downscale) | ||
|
|
||
| # Scale down | ||
| fixed_np = fixed_np[:,:,::128//downscale] | ||
| moving_np = moving_np[:,:,::128//downscale] | ||
|
|
||
| fixed_dims = fixed_np.shape | ||
| moving_dims = moving_np.shape | ||
|
|
||
| fixed_itk = sitk.GetImageFromArray(fixed_np) | ||
| moving_itk = sitk.GetImageFromArray(moving_np) | ||
|
|
||
| fixed_itk.SetSpacing((1.0, 1.0, 128//downscale)) | ||
| moving_itk.SetSpacing((1.0, 1.0, 128//downscale)) | ||
|
|
||
| fixed_img = Image(fixed_itk, device='cuda') | ||
| moving_img = Image(moving_itk, device='cuda') | ||
|
|
||
| fixed_batch = BatchedImages([fixed_img]) | ||
| moving_batch = BatchedImages([moving_img]) | ||
|
|
||
| # Test AffineRegistration | ||
| reg = AffineRegistration( | ||
| scales=[4, 2, 1], | ||
| iterations=[200, 100, 50], | ||
| fixed_images=fixed_batch, | ||
| moving_images=moving_batch, | ||
| loss_type='mse', | ||
| optimizer='Adam', | ||
| optimizer_lr=3e-2, | ||
| ) | ||
| assert reg.min_dim <= min(fixed_dims), f"Min dimension is {reg.min_dim}, expected {min(fixed_dims)}" | ||
| reg.optimize() | ||
|
|
||
| # Test GreedyRegistration | ||
| reg = GreedyRegistration( | ||
| scales=[4, 2, 1], | ||
| iterations=[200, 100, 50], | ||
| fixed_images=fixed_batch, | ||
| moving_images=moving_batch, | ||
| loss_type='cc', | ||
| optimizer='Adam', | ||
| optimizer_lr=0.2, | ||
| smooth_warp_sigma=0.25, | ||
| smooth_grad_sigma=0.5 | ||
| ) | ||
| assert reg.min_dim <= min(fixed_dims), f"Min dimension is {reg.min_dim}, expected {min(fixed_dims)}" | ||
| reg.optimize() | ||
|
|
||
| # Test SynRegistration | ||
| reg = SyNRegistration( | ||
| scales=[4, 2, 1], | ||
| iterations=[200, 100, 50], | ||
| fixed_images=fixed_batch, | ||
| moving_images=moving_batch, | ||
| loss_type='cc', | ||
| optimizer='Adam', | ||
| optimizer_lr=0.2, | ||
| smooth_warp_sigma=0.25, | ||
| smooth_grad_sigma=0.5 | ||
| ) | ||
| assert reg.min_dim <= min(fixed_dims), f"Min dimension is {reg.min_dim}, expected {min(fixed_dims)}" | ||
| reg.optimize() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to get
fixed_arraysandmoving_arrays? There is ashapeattribute of theBatchedImagesobject that you can use directly