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

Wrong intensity when RandomMotion is applied after ZNormalization #37

Closed
fepegar opened this issue Jan 9, 2020 · 5 comments
Closed
Labels
enhancement New feature or request

Comments

@fepegar
Copy link
Owner

fepegar commented Jan 9, 2020

To reproduce:

from torchio import Image, ImagesDataset, INTENSITY
from torchio.transforms import (
    RandomAffine,
    RandomFlip,
    RandomNoise,
    RandomBiasField,
    RandomElasticDeformation,
    RandomMotion,
    ZNormalization,
)

paths_list = [[
    Image('image', '~/Dropbox/MRI/t1.nii.gz', INTENSITY),
]]

seed = None
verbose = True

transforms = [
    ZNormalization(verbose=verbose),
    RandomMotion(proportion_to_augment=1, seed=seed, verbose=verbose),
    # RandomNoise(seed=seed, verbose=verbose),
    # RandomBiasField(seed=seed, verbose=verbose),
    # RandomFlip(axes=(0,), seed=seed, verbose=verbose),
    # RandomAffine(seed=seed, verbose=verbose),
    # RandomElasticDeformation(proportion_to_augment=0.1, seed=seed, verbose=verbose),
]

dataset = ImagesDataset(paths_list)

sample = dataset[0]
for i, transform in enumerate(transforms):
    transformed = transform(sample)
    name = transform.__class__.__name__
    path = f'/tmp/{i}_{name}.nii.gz'
    dataset.save_sample(transformed, dict(image=path))

@romainVala do you know why this could be? I'm probably missing some theory about the Fourier transform.

@fepegar fepegar added the bug Something isn't working label Jan 9, 2020
@fepegar fepegar changed the title Intensity is inverted if RandomMotion is applied after ZNormalization Wrong intensity when RandomMotion is applied after ZNormalization Jan 9, 2020
@fepegar
Copy link
Owner Author

fepegar commented Jan 9, 2020

Actually this is expected because of this line: https://github.com/fepegar/torchio/blob/master/torchio/transforms/random_motion.py#L216.

I'm not sure it's the best approach.

@romainVala
Copy link
Contributor

Hi
Ok I see, MRI is a complex number but we always take the abs value.
I am not sure why this is so, but with normal acquisition we do not have negative number. So here after the ZNormalization you end up with negative number which are turn to positive after the abs operation donne after the inverse fft

You could change this line L216 to take the real part, but it is not the way the acquisition works ...

I just check to perform random motion with the same seed, and compare the 2 cases (real and abs value after inv fft) this induce changes especially in the background we you get negative value if you take the real part

So I do not thing it is a good idea to change abs to real ... (since this is not what you will get from the scanner after motion ... (the scanner is always returning the abs))

May be you just need to invert the order of the transforms ... (starting with random motion ... ?)

@romainVala
Copy link
Contributor

Not realated, but I look closer to the znormalization and I do not understand the rational to compute the mean on the mask build for pixel above the mean ...

@fepegar
Copy link
Owner Author

fepegar commented Jan 10, 2020 via email

@romainVala
Copy link
Contributor

yes good idea, since it can easily happen as you find out ...

add a warning, something like that :
Warning input volume has negative value. Using motion simulation will end up with only positive value, due to the abs value use after inverse fft

@fepegar fepegar added documentation Improvements or additions to documentation and removed bug Something isn't working labels Jan 10, 2020
@fepegar fepegar added enhancement New feature or request and removed documentation Improvements or additions to documentation labels Jan 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants