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

[ENH] support fitting first levels to numpy arrays directly, in addition to niimgs #4110

Open
1 task done
henrymj opened this issue Nov 14, 2023 · 4 comments · May be fixed by #4112
Open
1 task done

[ENH] support fitting first levels to numpy arrays directly, in addition to niimgs #4110

henrymj opened this issue Nov 14, 2023 · 4 comments · May be fixed by #4112
Labels
Enhancement for feature requests GLM Issues/PRs related to the nilearn.glm module.

Comments

@henrymj
Copy link

henrymj commented Nov 14, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe your proposed enhancement in detail.

We recently fit GLMs to edge timeseries (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC10369861/) in order to look at fluctuations of attention at the network level. We believe it's an exciting approach that offers novel information and which is really easy to implement.

Almost all of the code was based off of nilearn - it mostly involved just changing FirstLevelModel().fit to take numpy arrays, rather than niimgs, so that we could give it the 2D arrays (n_features x n_scans).

It would be really great to bake this feature directly into nilearn, so that it can be easily kept up to date with other nilearn features and bug-fixes. I believe the changes to the code would be minimal, and add little-to-no maintenance, and I would be happy to submit the pull request myself.

I have some example pieces of potential changes below.

Benefits to the change

This would add flexibility to the nilearn code for fitting first-level models - letting it fit edges, or even just data that is in a significantly different format (e.g., open data that was already preprocessed and parcellated with some atlas). It would cost very little to maintain.

Pseudocode for the new behavior, if applicable

# insert your code below

def fit(...):

if (self.mask_img is None) & (type(run_imgs[0])==np.ndarray):
    assert run_imgs[0].shape==2  # nFeatures x nScans
...

if type(run_img)==np.ndarray:
    Y = run_img
else:
    run_img = check_niimg(run_img, ensure_ndim=4)
    Y = self.masker_.transform(run_img, sample_mask=sample_mask)

if design_matrices is None:
     n_scans = Y.shape[-1]
     
     
def compute_contrast(...):

...

if self.masker_ is None:
    output = estimate_
else:
    output = self.masker_.inverse_transform(estimate_)

@henrymj henrymj added the Enhancement for feature requests label Nov 14, 2023
@bthirion
Copy link
Member

Given that the GLM is natively implemented on arrays, it is indeed easy to do. The only that has to be done is to create a high-level API that encapsulates the function. So the cost is minimal.
I don't think we should advertise much these functions, but I think it is OK to have them.
Any other opinion ?

@Remi-Gau
Copy link
Collaborator

Any other opinion ?

I think that sounds like a sensible approach.

@henrymj would you be willing to start a pull request to start implementing this?

@henrymj
Copy link
Author

henrymj commented Nov 15, 2023

Absolutely!

To clarify: @bthirion are you suggesting making a new function specifically designed to take lists of numpy arrays? My initial idea was to just modify FirstLevelModel.fit and .compute_contrast to be able to handle numpy arrays, in addition to niimgs.

@bthirion
Copy link
Member

I think it would increase a lot the complexity of the object. It also makes debugging difficult, when you hit an error in a complex pipeline and are not sure whether a given argument is an image, an array or whatever.

henrymj added a commit to henrymj/nilearn that referenced this issue Nov 16, 2023
@Remi-Gau Remi-Gau added the GLM Issues/PRs related to the nilearn.glm module. label Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement for feature requests GLM Issues/PRs related to the nilearn.glm module.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants