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

Resample template and save a temporary copy #11

Open
htwangtw opened this issue Mar 25, 2022 · 8 comments
Open

Resample template and save a temporary copy #11

htwangtw opened this issue Mar 25, 2022 · 8 comments

Comments

@htwangtw
Copy link
Collaborator

I recently find out if the atlas and the data are in the same space, it takes a split second for LabelsMasker to run. I think it is worth resampling the template before hand.

@ltetrel
Copy link
Member

ltetrel commented Apr 1, 2022

but data and atlas should not already be on a standardized space (like MNI152NLin2009cAsym) ? if that is not the case, what you are suggesting is saving the preprocessed data also into the atlas space, before running extraction right ?

But what is the actual impact in term of performance between:

  • registered data to atlas -> LabelsMasker -> check if register -> don't register because already in atlas space -> timeserie (proposition)
    VS
  • non-registered data to atlas -> LabelsMasker -> check if register -> register to atlas -> timeserie (now)

It may be actually more "dumb" and always run a registration in any case, which mitigates even more the proposal (overall takes 2 registration instead of one: registered data to atlas -> LabelsMasker -> register but fast because already in atlas space -> timeserie)

@htwangtw
Copy link
Collaborator Author

htwangtw commented Apr 4, 2022

Some clarifications:

but data and atlas should not already be on a standardized space (like MNI152NLin2009cAsym) ?

I am using some interchangeable jargon without realising it. MNI152NLin2009cAsym is a template it's registered to. It can still have different resolution, resulting in different shape for the volume and different affine matrix. What's needed is not registration in this case. It's resampling. It's simply matching the shape of volumes and the affine matrix.

See this tutorial to explain resampling

if that is not the case, what you are suggesting is saving the preprocessed data also into the atlas space, before running extraction right ?

No. We are resampling the atlas to the data, not the other way round. It's far more efficient to do it on one file than all the subjects prerpocessed data. By default in nilearn maskers, all the mask and atlas are resampled to match the data, unless specified. Again, we are not doing a full registration here, just resampling.

But what is the actual impact in term of performance between:

* registered data to atlas -> LabelsMasker -> check if register -> don't register because already in atlas space -> timeserie (proposition)
  VS

* non-registered data to atlas -> LabelsMasker -> check if register -> register to atlas -> timeserie (now)

It may be actually more "dumb" and always run a registration in any case, which mitigates even more the proposal (overall takes 2 registration instead of one: registered data to atlas -> LabelsMasker -> register but fast because already in atlas space -> timeserie)

If we are doing the thing I described, this comparison and comment don't apply.

Also, nilearn is not checking the registration, it's only checking if the shape of the atlas matches a volume from the functional data, and if the affine matrix match.

@ltetrel
Copy link
Member

ltetrel commented Apr 4, 2022

So resampling is actually two steps: registration and interpolation.
You mean interpolation in that case, and I clearly imagine that nilearn will try to interpolate each image to atlas if not the same shape.
So what you are proposing totally makes sense indeed.

@pbellec
Copy link

pbellec commented Apr 4, 2022

fyi resampling means that you are changing the sampling rate (here spatial). This is typically done through interpolation. In general the term is used interchangeably, even though strictly speaking a complex transform is not just a change in sampling rate.

In fMRI people use the term registration for the estimation of the transformation, which is separate from resampling itself. The reason is that sometimes you will chain multiple transformations and resample once, in order to avoid accumulating interpolation errors. So resampling and registration are often separate functions.

@htwangtw
Copy link
Collaborator Author

htwangtw commented Apr 4, 2022

That's a great explanation!

So here's a proposed workflow:

  • Get the atlas from the closest resolution and registration template to the data
  • Get the target shape and target affine from the preprocessed output
  • Resample the atlas with nilearn.image.resample_img, save a temporary copy
  • Create the masker using the new copy, fit the data, save the timeseries
  • Delete the temporary copy

This is not urgent btw, just logging some relevant insight from working on the denoising project

@ltetrel
Copy link
Member

ltetrel commented Apr 4, 2022

@pbelled

resampling means that you are changing the sampling rate (here spatial)

Just realized that I meant resampling is: transformation (not registration, transformation is already estimated) + interpolation! At least using ITK convention (which is the standard): https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAnImage/Documentation.html.

@htwangtw

By default in nilearn maskers, all the mask and atlas are resampled to match the data

So I don't get why you want to resample yourself, if nilearn already does it by default ? Will it take less time in total to resample ourself first, then run nilearn. Rather than letting nilearn resample it himself ?

  • resampled atlas to data -> LabelsMasker -> nilearn check if resampled (check affines) -> don't resample because already in atlas space -> timeserie (proposition)
    VS
  • non-resampled data to atlas -> LabelsMasker -> nilearn check if resampled (check affines) -> atlas resample to img -> timeserie (now)

@htwangtw
Copy link
Collaborator Author

htwangtw commented Apr 4, 2022

So I don't get why you want to resample yourself, if nilearn already does it by default ? Will it take less time in total to resample ourself first, then run nilearn. Rather than letting nilearn resample it himself ?

A dataset is usually collected with the same protocol, so all the preprocessed data, registered to the same template, with the same shape and affine matrix. If you let nilearn check it itself, it will redo the resampling on the altas every time you run fit_transform. If you have 30 subjects, it will run thirty times. If you make sure the atlas is has the same shape as the data, you are only doing it once.

@ltetrel
Copy link
Member

ltetrel commented Apr 4, 2022

Ooooooh I get it now ^^'
I though nilearn masker fit_transform would take a list of images, resample the mask once, and then do the resampling on all the images! But it is indeed taking just one image as input: https://nilearn.github.io/modules/generated/nilearn.input_data.NiftiMasker.html#nilearn.input_data.NiftiMasker.fit_transform

thanks for the explanation! This could save some time :)

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

No branches or pull requests

3 participants