8000 [MAINT] ensure transform on 3D volume (1D surface) images returns 1D arrays by Remi-Gau · Pull Request #5381 · nilearn/nilearn · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[MAINT] ensure transform on 3D volume (1D surface) images returns 1D arrays #5381

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

Merged
merged 45 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
dcdfa06
[MAINT] ensure transform 3D images returns 1D arrays
Remi-Gau May 15, 2025
fdd8f0d
fix nifti label masker
Remi-Gau May 15, 2025
1f900dd
fix nifti maps maskers
Remi-Gau May 15, 2025
a7bb6ec
update doc strings
Remi-Gau May 15, 2025
853edec
docstrings
Remi-Gau May 15, 2025
cdd9b52
docstrings
Remi-Gau May 15, 2025
747492c
fix
Remi-Gau May 15, 2025
5bef1b0
fix
Remi-Gau May 15, 2025
0504756
fix
Remi-Gau May 15, 2025
1d526f0
fix
Remi-Gau May 15, 2025
8000 d44e3b2
Merge remote-tracking branch 'upstream/main' into 3D1D
Remi-Gau May 19, 2025
eddca40
fix
Remi-Gau May 19, 2025
60a7b1a
check_arrays
Remi-Gau May 19, 2025
f14b703
fix
Remi-Gau May 19, 2025
417776b
fix
Remi-Gau May 19, 2025
427b481
fix
Remi-Gau May 19, 2025
ff8b482
Merge branch 'main' into 3D1D
Remi-Gau May 20, 2025
87b93f3
Merge branch 'main' into 3D1D
Remi-Gau May 20, 2025
0510ad9
sty
Remi-Gau May 20, 2025
185c829
fix
Remi-Gau May 20, 2025
f1748ee
fix
Remi-Gau May 20, 2025
ef5fe78
fix
Remi-Gau May 20, 2025
8f5e92b
ref
Remi-Gau May 20, 2025
ceeb4e9
simp
Remi-Gau May 20, 2025
8b53a88
ref
Remi-Gau May 20, 2025
cb46ad9
ref
Remi-Gau May 20, 2025
ba065d1
rm deadcode
Remi-Gau May 20, 2025
58ec000
surface masker transform 1D returns 1D
Remi-Gau May 20, 2025
e69134c
doc
Remi-Gau May 20, 2025
86dfee8
log
Remi-Gau May 20, 2025
68c7eae
doc
Remi-Gau May 20, 2025
1d820e2
doc
Remi-Gau May 20, 2025
f15c15f
inv trans 1D array give 1D surf img
Remi-Gau May 20, 2025
252cf1b
type
Remi-Gau May 20, 2025
dd2901f
Merge remote-tracking branch 'upstream/main' into 3D1D
Remi-Gau May 20, 2025
0c28d4e
Merge remote-tracking branch 'upstream/main' into 3D1D
Remi-Gau May 20, 2025
6f8c5f6
Update nilearn/_utils/estimator_checks.py
Remi-Gau May 21, 2025
402f902
Update nilearn/_utils/estimator_checks.py
Remi-Gau May 21, 2025
e1d52de
doc
Remi-Gau May 21, 2025
e552e3f
doc
Remi-Gau May 21, 2025
02f55a7
doc
Remi-Gau May 21, 2025
4508c96
doc
Remi-Gau May 21, 2025
6af0509
doc
Remi-Gau May 21, 2025
8e7275d
doc
Remi-Gau May 21, 2025
60685d8
doc
Remi-Gau May 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ Changes

- :bdg-danger:`Deprecation` For version >=0.13.2 :func:`~nilearn.interfaces.bids.parse_bids_filename` will return a dictionary whose keys correspond to valid BIDS terms. (:gh:`5320` by `Rémi Gau`_).

- :bdg-danger:`Deprecation` Using the transform method of nifti maskers on 3D images now returns 1D arrays. Similarly, using the transform method of surface maskers on 1D surface images now returns 1D arrays. (:gh:`5381` by `Rémi Gau`_).

- :bdg-danger:`Deprecation` Using the inverse_transform method of surface maskers on 1D arrays now returns surface images (:gh:`5381` by `Rémi Gau`_).

- :bdg-danger:`Deprecation` The default for ``slice_time_ref`` in :func:`~nilearn.glm.first_level.first_level_from_bids` was changed to ``None`` (:gh:`5382` by `Rémi Gau`_).
16 changes: 8 additions & 8 deletions doc/manipulating_images/manipulating_images.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.. _data_manipulation:

=====================================================================
=============================================================
Manipulating images: resampling, smoothing, masking, ROIs...
=====================================================================
=============================================================

This chapter discusses how nilearn can be used to do simple operations on
brain images.
Expand All @@ -11,7 +11,7 @@ brain images.
.. _preprocessing_functions:

Functions for data preparation and image transformation
=========================================================
=======================================================

Nilearn comes with many simple functions for simple data preparation and
transformation. Note that if you want to perform these operations while
Expand Down Expand Up @@ -106,7 +106,7 @@ of the transformation matrix (i.e., affine).
:ref:`An example illustrating affine transforms on data and bounding boxes <sphx_glr_auto_examples_06_manipulating_images_plot_affine_transformation.py>`

Accessing individual volumes in 4D images
===========================================
=========================================

* :func:`nilearn.image.index_img`: selects one or more volumes in a 4D
image.
Expand Down Expand Up @@ -139,7 +139,7 @@ Accessing individual volumes in 4D images
.. _computing_and_applying_mask:

Computing and applying spatial masks
=====================================
====================================

Relevant functions:

Expand All @@ -157,7 +157,7 @@ Relevant functions:


Extracting a brain mask
------------------------
-----------------------

If we do not have a spatial mask of the target regions, a brain mask
can be computed from the data:
Expand All @@ -183,7 +183,7 @@ can be computed from the data:
.. _mask_4d_2_3d:

Masking data: from 4D Nifti images to 2D data arrays
---------------------------------------------------------------
----------------------------------------------------

:term:`fMRI` data is usually represented as a 4D block of data: 3 spatial
dimensions and one time dimension. In practice, we are usually
Expand Down Expand Up @@ -211,7 +211,7 @@ do it manually below:


Image operations: creating a ROI mask manually
===============================================
==============================================

A region of interest (ROI) mask can be computed for instance with a
statistical test. This requires a chain of image
Expand Down
60 changes: 46 additions & 14 deletions doc/manipulating_images/masker_objects.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.. _masker_objects:

=====================================================================
================================================================
From neuroimaging volumes to data matrices: the masker objects
=====================================================================
================================================================

This chapter introduces the maskers: objects that go from
neuroimaging volumes, on the disk or in memory, to data matrices, eg of
Expand All @@ -24,10 +24,10 @@ the raw neuroimaging data in 3D space into the units of observation
relevant for the research questions at hand.

.. tip::
Masker objects can transform both 3D and 4D image objects.
Transforming a 4D image produces a 2D (samples x features) matrix.
Currently, transforming a 3D image also produces a 2D (1 x features) matrix,
but starting in version 0.12, it will produce a 1D (features) array.
Masker objects can transform both 3D and 4D image objects :

- transforming a 3D image produces a 1D (features,) array,
- transforming a 4D image produces a 2D (samples, features) array.


.. |niimgs| image:: ../images/niimgs.jpg
Expand Down Expand Up @@ -294,8 +294,6 @@ properties, before conversion to :term:`voxel` signals.
:func:`nilearn.signal.clean`




Resampling: resizing and changing resolutions of images
.......................................................

Expand Down Expand Up @@ -343,9 +341,10 @@ an excerpt of :ref:`the example performing Anova-SVM on the Haxby data
|

.. tip::
Masker objects can inverse-transform both 1D and 2D arrays.
Inverse-transforming a 2D array produces a 4D (X x Y x Z x samples) image,
while inverse-transforming a 1D array produces a 3D (X x Y x Z) image.
Masker objects can inverse-transform both 1D and 2D arrays :

- inverse-transforming a 2D array produces a 4D (X x Y x Z x samples) image,
- inverse-transforming a 1D array produces a 3D (X x Y x Z) image.

.. topic:: **Examples to better understand the NiftiMasker**

Expand Down Expand Up @@ -445,8 +444,17 @@ as to facilitate the computation of :term:`voxel` signals in multi-subjects sett
While :class:`NiftiMasker`, :class:`NiftiLabelsMasker` and
:class:`NiftiMapsMasker` work with 3D inputs (single brain volume) or 4D inputs
(sequence of brain volumes in time for one subject), :class:`MultiNiftiMasker`,
:class:`MultiNiftiLabelsMasker` and :class:`MultiNiftiMapsMasker` expect 5D
inputs (list of sequences of brain volumes).
:class:`MultiNiftiLabelsMasker` and :class:`MultiNiftiMapsMasker`
can also handle list of 3D or 4D image objects.

.. tip::
MultiMasker objects can transform both 3D, 4D,
as well as list of 3D or 4D image objects :

- transforming a 3D image produces a 1D (features,) array,
- transforming a 4D image produces a 2D (samples, features) array,
- transforming a list of 3D image produces a list of 1D (features,) array,
- transforming a list of 4D image produces a list of 2D (samples, features) array.

:class:`MultiNiftiMasker` Usage
-------------------------------
Expand All @@ -470,7 +478,7 @@ for each subject.
* :ref:`sphx_glr_auto_examples_03_connectivity_plot_atlas_comparison.py`

:class:`MultiNiftiMapsMasker` Usage
-------------------------------------
-----------------------------------

:class:`MultiNiftiMapsMasker` extracts signals regions defined by maps
for each subject.
Expand Down Expand Up @@ -504,3 +512,27 @@ seed position is used.
.. topic:: **Examples**

* :ref:`sphx_glr_auto_examples_03_connectivity_plot_sphere_based_connectome.py`


Extraction of signals from surface images\ :class:`SurfaceMasker`, :class:`SurfaceLabelsMasker`, :class:`SurfaceMapsMasker`
============================================================================================================================

The purpose of :class:`SurfaceMasker`, :class:`SurfaceLabelsMasker`, :class:`SurfaceMapsMasker`
is to mirror the capabilities of
:class:`NiftiMasker`, :class:`NiftiLabelsMasker` and :class:`NiftiMapsMasker`
but to extract data from :class:`~nilearn.surface.SurfaceImage`.

They can perform data extraction from 1D surface data (n_vertices),
2D surface data (n_vertices x samples)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
2D surface data (n_vertices x samples)
2D surface data (n_vertices, samples)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Apparently I am late :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oops sorry

I need to do another pass on this so I can integrate those suggestions

as well as list of 1D or 2D surface data with the same underlying mesh.

.. tip::
Surface masker objects can transform both 1D, 2D,
as well as list of 1D or 2D surface image objects.
Transforming a 1D image produces a 1D (features,) array.
All other input will produce a 1D (samples, features) array..
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
All other input will produce a 1D (samples, features) array..
All other input will produce a 1D (samples, features) array.


Surface masker objects can inverse-transform both 1D and 2D arrays :

- inverse-transforming a 1D array produces a 1D (n_vertices,) image,
- inverse-transforming a 2D array produces a 2D (n_vertices, samples) image.
79 changes: 78 additions & 1 deletion nilearn/_utils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,19 @@ def custom_function(vertices):
"mask_img :", "mask :"
)

# signals for inverse transform
docdict["signals_inv_transform"] = """
signals : 1D/2D :obj:`numpy.ndarray`
Extracted signal.
If a 1D array is provided,
then the shape should be (number of elements,).
If a 2D array is provided,
then the shape should be (number of scans, number of elements).
"""
docdict["region_signals_inv_transform"] = docdict["signals_inv_transform"]
docdict["x_inv_transform"] = docdict["signals_inv_transform"]


# smoothing_fwhm
docdict["smoothing_fwhm"] = """
smoothing_fwhm : :obj:`float` or :obj:`int` or None, optional.
Expand Down Expand Up @@ -1239,7 +1252,7 @@ def custom_function(vertices):

##############################################################################
#
# Other values definitions
# Other values definitions: return values, attributes...
#

# atlas_type
Expand Down Expand Up @@ -1358,6 +1371,29 @@ def custom_function(vertices):

"""

# image returned Nifti maskers by inverse_transform
docdict["img_inv_transform_nifti"] = """img : :obj:`nibabel.nifti1.Nifti1Image`
Transformed image in brain space.
Output shape for :

- 1D array : 3D :obj:`nibabel.nifti1.Nifti1Image` will be returned.
- 2D array : 4D :obj:`nibabel.nifti1.Nifti1Image` will be returned.

See :ref:`extracting_data`.
"""
# image returned surface maskers by inverse_transform
docdict[
"img_inv_transform_surface"
] = """img : :obj:`~nilearn.surface.SurfaceImage`
Signal for each vertex projected on the mesh.
Output shape for :

- 1D array : 1D :obj:`~nilearn.surface.SurfaceImage` will be returned.
- 2D array : 2D :obj:`~nilearn.surface.SurfaceImage` will be returned.

See :ref:`extracting_data`.
"""

# atlas labels
docdict["labels"] = """'labels' : :obj:`list` of :obj:`str`
List of the names of the regions."""
Expand All @@ -1380,6 +1416,47 @@ def custom_function(vertices):
Formatted according to 'dseg.tsv' format from
`BIDS <https://bids-specification.readthedocs.io/en/latest/derivatives/imaging.html#common-image-derived-labels>`_."""

# signals returned Nifti maskers by transform, fit_transform...
docdict["signals_transform_nifti"] = """signals : :obj:`numpy.ndarray`
Signal for each :term:`voxel`.
Output shape for :

- 3D images: (number of elements,) array
- 4D images: (number of scans, number of elements) array
"""
# signals returned Mulit Nifti maskers by transform, fit_transform...
docdict[
"signals_transform_multi_nifti"
] = """signals : :obj:`list` of :obj:`numpy.ndarray` or :obj:`numpy.ndarray`
Signal for each :term:`voxel`.
Output shape for :

- 3D images: (number of elements,) array
- 4D images: (number of scans, number of elements) array
- list of 3D images: list of (number of elements,) array
- list of 4D images: list of (number of scans, number of elements)
array
"""
# signals returned Mulit Nifti maskers by transform, fit_transform...
docdict[
"signals_transform_imgs_multi_nifti"
] = """signals : :obj:`list` of :obj:`numpy.ndarray`
Signal for each :term:`voxel`.
Output shape for :

- list of 3D images: list of (number of elements,) array
- list of 4D images: list of (number of scans, number of elements)
array
"""
# signals returned surface maskers by transform, fit_transform...
docdict["signals_transform_surface"] = """signals : :obj:`numpy.ndarray`
Signal for each element.
Output shape for :

- 1D images: (number of elements,) array
- 2D images: (number of scans, number of elements) array
"""

# template
docdict["template"] = """'template' : :obj:`str`
The standardized space of analysis
Expand Down
Loading
Loading
0