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

Fixes a bug in NiftiImageData.cpp causing segfault #1226

Merged
merged 5 commits into from
Jan 8, 2024

Conversation

evgueni-ovtchinnikov
Copy link
Contributor

@evgueni-ovtchinnikov evgueni-ovtchinnikov commented Dec 11, 2023

Changes in this pull request

Trying to fix a bug in NiftiImageData.cpp that causes segfault.

Testing performed

Tested on the code provided by @paskino in #1225.

Related issues

Fixes #1225.

Checklist before requesting a review

  • I have performed a self-review of my code
  • I have added docstrings/doxygen in line with the guidance in the developer guide
  • I have implemented unit tests that cover any new or modified functionality
  • The code builds and runs on my machine
  • CHANGES.md has been updated with any functionality change

Contribution Notes

Please read and adhere to the contribution guidelines.

Please tick the following:

  • The content of this Pull Request (the Contribution) is intentionally submitted for inclusion in SIRF (the Work) under the terms and conditions of the Apache-2.0 License.

@evgueni-ovtchinnikov evgueni-ovtchinnikov added this to the v3.6 milestone Dec 11, 2023
@evgueni-ovtchinnikov evgueni-ovtchinnikov changed the title Fixed a bug in NiftiImageData.cpp causing segfault Fix a bug in NiftiImageData.cpp causing segfault Dec 12, 2023
@evgueni-ovtchinnikov evgueni-ovtchinnikov marked this pull request as draft December 12, 2023 12:35
@evgueni-ovtchinnikov evgueni-ovtchinnikov marked this pull request as ready for review December 12, 2023 15:17
@evgueni-ovtchinnikov evgueni-ovtchinnikov changed the title Fix a bug in NiftiImageData.cpp causing segfault Fixes a bug in NiftiImageData.cpp causing segfault Dec 12, 2023
@@ -220,7 +220,7 @@ void NiftiImageData<dataType>::construct_NiftiImageData_from_complex_im_real_com

auto &it_in = in_sptr->begin();
auto &it_out = out_sptr->begin();
for (; it_in!=in_sptr->end(); ++it_in, ++it_out)
for (; it_out != out_sptr->end(); ++it_in, ++it_out)
Copy link
Member

Choose a reason for hiding this comment

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

not sure why this is. Aren't they the same size?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, the input image dimensions are (2, 256, 256) and the output (1, 256, 256) because the first dimension of a NiftiImageData object is z-dimension, which is 1 for both input and output images, but the input image is reconstructed from MR acquisition data with two repetitions.

Copy link
Member

Choose a reason for hiding this comment

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

Are we (silently) constructing a single volume from a repeated volume? That sounds wrong. I would suggest that we have a chechk that dimensions are identical (e.g. via get_geometrical_info() ) and if not throw an error.

Where is the call then that you're trying to fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Throwing an error to avoid segfault is not very helpful, I believe - I would rather issue a warning that the complex data contains more than one image, and only the first one is going to be used. @paskino which way would you prefer?

Where is the call then that you're trying to fix?

In Reg.NiftiImageData.construct_from_complex_image, if I understand your question correctly

Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to understand if this is a "conceptual" problem (i.e. what should we do with weird data) or a bug (i.e. we know how to handle the data, but the code doesn't do it).

What data do we stick into Reg.NiftiImageData.construct_from_complex_image where it throws this error? Is it a normal complex image, and If it is complex data with 2 repetitions, then I would argue that the output should be a real image with 2 repetitions, not silently throw away the 2nd repetition.

Unfortunately, this might be getting this into our problem of encoding MRI dimensions. From your description, I understand that data of a single slice with 2 repetitions is encoded as a 3D NiftiImageData. That in itself would be wrong (it should be a 4D image, with a single slice). And as I stated above, the construct_from functions really should check that all args have the same geometry. If they don't, an error should be thrown as it is invalid usage. Otherwise other crashes are unavoidable.

I might misunderstand this completely of course...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it a normal complex image, and If it is complex data with 2 repetitions, then I would argue that the output should be a real image with 2 repetitions, not silently throw away the 2nd repetition.

That would require NiftiImageData to handle multiple images, and efficient implementation of such a functionality may take time. I would make a separate issue/PR for that when the need for it arises - the current quick fix just allows the computation of the resampler norm.

From your description, I understand that data of a single slice with 2 repetitions is encoded as a 3D NiftiImageData.

No, that was what my quick fix tried to avoid, as it would be wrong indeed.

@KrisThielemans
Copy link
Member

@evgueni-ovtchinnikov you will have to merge master here, as I've updated STIR master, and that needed small SIRF changes. So, BUILD_DEVEL=ON tests will fail.

@KrisThielemans
Copy link
Member

so... the tests worked. Can't say I understand that as I needed #1228. oh well.

@evgueni-ovtchinnikov evgueni-ovtchinnikov merged commit e650e29 into master Jan 8, 2024
6 checks passed
@evgueni-ovtchinnikov evgueni-ovtchinnikov deleted the fix-mr2nifti branch January 8, 2024 16:47
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.

NiftyResampler norm crashes on MR data with multiple repetitions
2 participants