Repository: nilearn/nilearn

Issue:
NiftiLabelsMasker generate report gives error 

I am trying to use the following code to load the neuroparc glasser atlas into NiftiLabelsMasker and QC it:

```
from nilearn import input_data

masker = input_data.NiftiLabelsMasker('./Glasser_space-MNI152NLin6_res-1x1x1.nii.gz',
                        labels=['Background', 'Primary_visual_cortex', 'Medial_superior_temporal_area', 'Sixth_Visual_Area', 'Second_Visual_Area', 'Third_Visual_Area', 'Forth_Visual_Area', 'Eighth_Visual_Area', 'Primary_Motor_Cortex', 'Primary_Sensory_Cortex', 'Frontal_Eye_Field', 'Premotor_Eye_Field', 'Area_55b', 'Area_V3A', 'RetroSplenial_Complex', 'Parieto-Occipital_Sulcus_Area_2', 'Seventh_Visual_Area', 'IntraParietal_Sulcus_Area_1', 'Fusiform_Face_Complex', 'Area_V3B', 'Lateral_Occipital_Area_1', 'Lateral_Occipital_Area_2', 'Posterior_InferoTemporal_Complex', 'Middle_Temporal_Area', 'Primary_Auditory_Cortex', 'PeriSylvian_Language_Area', 'Superior_Frontal_Language_Area', 'PreCuneus_Visual_Area', 'Superior_Temporal_Visual_Area', 'Medial_Area_7P', 'Area_7m', 'Parieto-Occipital_Sulcus_Area_1', 'Area_23d', 'Area_Ventral_23_a+b', 'Area_Dorsal_23_a+b', 'Area_31p_Ventral', 'Area_5m', 'Area_5m_ventral', 'Area_23c', 'Area_5L', 'Dorsal_Area_24d', 'Lateral_Area_7A', 'Supplementary_and_Cingulate_Eye_Field', 'Area_6m_Anterior', 'Medial_6m_anterior', 'Medial_Area_7A', 'Lateral_Area_7P', 'Area_7PC', 'Area_Lateral_IntraParietal_ventral', 'Ventral_IntraParietal_Complex', 'Medial_IntraParietal_Area', 'Area_1', 'Area_2', 'Area_3a', 'Dorsal_area_6', 'Area_6mp', 'Ventral_Area_6', 'Area_Posterior_24', 'Area_33_prime', 'Anterior_24_prime', 'Area_p32_prime', 'Area_a24', 'Area_dorsal_32', 'Area_8BM', 'Area_p32', 'Area_10r', 'Area_47m', 'Area_8Av', 'Area_8Ad', 'Area_9_Middle', 'Area_8B_Lateral', 'Area_9_Posterior', 'Area_10d', 'Area_8C', 'Area_44', 'Area_45', 'Area_47L', 'Area_anterior_47R', 'Rostral_Area_6', 'Area_IFJa', 'Area_IFJp', 'Area_IFSp', 'Area_IFSa', 'Area_posterior_9-46v', 'Area_46', 'Area_anterior_9-46v', 'Area_9-46d', 'Area_9_anterior', 'Area_10v', 'Area_anterior_10p', 'Polar_10p', 'Area_11', 'Area_13', 'Orbital_Frontal_Complex', 'Area_47s', 'Area_Lateral_IntraParietal_dorsal', 'Area_6_anterior', 'Inferior_6-8_Transitional_Area', 'Superior_6-8_Transitional_Area', 'Area_43', 'Area_OP4/PV', 'Area_OP1/SII', 'Area_OP2-3/VS', 'Area_52', 'RetroInsular_Cortex', 'Area_PFcm', 'Posterior_Insular_Area_2', 'Area_TA2', 'Frontal_OPercular_Area_4', 'Middle_Insular_Area', 'Pirform_Cortex', 'Anterior_Ventral_Insular_Area', 'Anterior_Agranular_Insula_Complex', 'Frontal_OPercular_Area_1', 'Frontal_OPercular_Area_3', 'Frontal_OPercular_Area_2', 'Area_PFt', 'Anterior_IntraParietal_Area', 'Entorhinal_Cortex', 'PreSubiculum', 'Hippocampus', 'ProStraite_Area', 'Perirhinal_Ectorhinal_Cortex', 'Area_STGa', 'ParaBelt_Complex', 'Auditory_5_Complex', 'ParaHippocampal_Area_1', 'ParaHippocampal_Area_3', 'Area_STSd_Anterior', 'Area_STSd_Posterior', 'Area_STSv_Posterior', 'Area_TG_Dorsal', 'Area_TE1_Anterior', 'Area_TE1_Posterior', 'Area_TE2_Anterior', 'Area_TF', 'Area_TE2_Posterior', 'Area_PHT', 'Area_PH', 'Area_TemporoParietoOccipital_Junction_1', 'Area_TemporoParietoOccipital_Junction_2', 'Area_TemporoParietoOccipital_Junction_3', 'Dorsal_Transitional_Visual_Area', 'Area_PGp', 'Area_IntraParietal_2', 'Area_IntraParietal_1', 'Area_IntraParietal_0', 'Area_PF_Opercular', 'Area_PF_Complex', 'Area_PFm_Complex', 'Area_PGi', 'Area_PGs', 'Area_V6A', 'VentroMedial_Visual_Area_1', 'VentroMedial_Visual_Area_3', 'ParaHippocampal_Area_2', 'Area_V4t', 'Area_FST', 'Area_V3CD', 'Area_Lateral_Occipital_3', 'VentroMedial_Visual_Area_2', 'Area_31pd', 'Area_31a', 'Ventral_Visual_Complex', 'Area_25', 'Area_s32', 'Posterior_OFC_Complex', 'Area_Posterior_Insular_1', 'Insular_Granular_Complex', 'Area_Frontal_Opercular_5', 'Area_Posterior_10p', 'Area_Posterior_47r', 'Area_TG_ventral', 'Medial_Belt_Complex', 'Lateral_Belt_Complex', 'Auditory_4_Complex', 'Area_STSv_Anterior', 'Area_TE1_Middle', 'Para-Insular_Area', 'Area_Anterior_32_Prime', 'Area_Posterior_24'],
                        standardize=False,
                        memory='nilearn_cache',
                        verbose=1,
                        detrend=False)
masker.fit()
masker.generate_report()
```

It appears to load, but when I try to check the results with generate_report, I get the following error:

```
/autofs/space/uriline_003/users/projects/OCD_NM/walker_pedersen/scripts/rest/conda/OCDNMrest/lib/python3.9/site-packages/nilearn/datasets/__init__.py:86: FutureWarning: Fetchers from the nilearn.datasets module will be updated in version 0.9 to return python strings instead of bytes and Pandas dataframes instead of Numpy arrays.
  warn("Fetchers from the nilearn.datasets module will be "
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/autofs/space/uriline_003/users/projects/OCD_NM/walker_pedersen/scripts/rest/conda/OCDNMrest/lib/python3.9/site-packages/nilearn/input_data/nifti_labels_masker.py", line 213, in generate_report
    return generate_report(self)
  File "/autofs/space/uriline_003/users/projects/OCD_NM/walker_pedersen/scripts/rest/conda/OCDNMrest/lib/python3.9/site-packages/nilearn/reporting/html_report.py", line 211, in generate_report
    overlay, image = _define_overlay(estimator)
  File "/autofs/space/uriline_003/users/projects/OCD_NM/walker_pedersen/scripts/rest/conda/OCDNMrest/lib/python3.9/site-packages/nilearn/reporting/html_report.py", line 153, in _define_overlay
    displays = estimator._reporting()
  File "/autofs/space/uriline_003/users/projects/OCD_NM/walker_pedersen/scripts/rest/conda/OCDNMrest/lib/python3.9/site-packages/nilearn/input_data/nifti_labels_masker.py", line 269, in _reporting
    regions_summary['region name'].append(self.labels[label])
TypeError: list indices must be integers or slices, not numpy.float32
```
Attached is text file with the code and a copy of the glasser atlas in question
[atlasExample.tar.gz](https://github.com/nilearn/nilearn/files/7289767/atlasExample.tar.gz)




Discussion:
Hi @walkerped 

Thanks for opening this issue. You get this error because the label image contains floats instead of integers.
This is clearly a bug since the masker should be able to handle this situation. I'll work on a PR to fix this.
In the mean time, a way around would be to cast your labels into integers. 
Here is a code snippet which should compute the report without error:

```python
from nilearn.image import load_img
from nilearn.input_data import NiftiLabelsMasker

atlas_path = "/home/nicolas/Downloads/atlasExample/Glasser_space-MNI152NLin6_res-1x1x1.nii.gz"
labels = ['Background', 'Primary_visual_cortex', 'Medial_superior_temporal_area', 'Sixth_Visual_Area',
          'Second_Visual_Area', 'Third_Visual_Area', 'Forth_Visual_Area', 'Eighth_Visual_Area',
          'Primary_Motor_Cortex', 'Primary_Sensory_Cortex', 'Frontal_Eye_Field', 'Premotor_Eye_Field',
          'Area_55b', 'Area_V3A', 'RetroSplenial_Complex', 'Parieto-Occipital_Sulcus_Area_2',
          'Seventh_Visual_Area', 'IntraParietal_Sulcus_Area_1', 'Fusiform_Face_Complex', 'Area_V3B',
          'Lateral_Occipital_Area_1', 'Lateral_Occipital_Area_2', 'Posterior_InferoTemporal_Complex',
          'Middle_Temporal_Area', 'Primary_Auditory_Cortex', 'PeriSylvian_Language_Area',
          'Superior_Frontal_Language_Area', 'PreCuneus_Visual_Area', 'Superior_Temporal_Visual_Area',
          'Medial_Area_7P', 'Area_7m', 'Parieto-Occipital_Sulcus_Area_1', 'Area_23d', 'Area_Ventral_23_a+b',
          'Area_Dorsal_23_a+b', 'Area_31p_Ventral', 'Area_5m', 'Area_5m_ventral', 'Area_23c', 'Area_5L',
          'Dorsal_Area_24d', 'Lateral_Area_7A', 'Supplementary_and_Cingulate_Eye_Field', 'Area_6m_Anterior',
          'Medial_6m_anterior', 'Medial_Area_7A', 'Lateral_Area_7P', 'Area_7PC', 'Area_Lateral_IntraParietal_ventral',
          'Ventral_IntraParietal_Complex', 'Medial_IntraParietal_Area', 'Area_1', 'Area_2', 'Area_3a', 'Dorsal_area_6',
          'Area_6mp', 'Ventral_Area_6', 'Area_Posterior_24', 'Area_33_prime', 'Anterior_24_prime', 'Area_p32_prime',
          'Area_a24', 'Area_dorsal_32', 'Area_8BM', 'Area_p32', 'Area_10r', 'Area_47m', 'Area_8Av', 'Area_8Ad',
          'Area_9_Middle', 'Area_8B_Lateral', 'Area_9_Posterior', 'Area_10d', 'Area_8C', 'Area_44', 'Area_45', 'Area_47L',
          'Area_anterior_47R', 'Rostral_Area_6', 'Area_IFJa', 'Area_IFJp', 'Area_IFSp', 'Area_IFSa', 'Area_posterior_9-46v',
          'Area_46', 'Area_anterior_9-46v', 'Area_9-46d', 'Area_9_anterior', 'Area_10v', 'Area_anterior_10p', 'Polar_10p',
          'Area_11', 'Area_13', 'Orbital_Frontal_Complex', 'Area_47s', 'Area_Lateral_IntraParietal_dorsal',
          'Area_6_anterior', 'Inferior_6-8_Transitional_Area', 'Superior_6-8_Transitional_Area', 'Area_43', 'Area_OP4/PV',
          'Area_OP1/SII', 'Area_OP2-3/VS', 'Area_52', 'RetroInsular_Cortex', 'Area_PFcm', 'Posterior_Insular_Area_2',
          'Area_TA2', 'Frontal_OPercular_Area_4', 'Middle_Insular_Area', 'Pirform_Cortex', 'Anterior_Ventral_Insular_Area',
          'Anterior_Agranular_Insula_Complex', 'Frontal_OPercular_Area_1', 'Frontal_OPercular_Area_3',
          'Frontal_OPercular_Area_2', 'Area_PFt', 'Anterior_IntraParietal_Area', 'Entorhinal_Cortex', 'PreSubiculum',
          'Hippocampus', 'ProStraite_Area', 'Perirhinal_Ectorhinal_Cortex', 'Area_STGa', 'ParaBelt_Complex',
          'Auditory_5_Complex', 'ParaHippocampal_Area_1', 'ParaHippocampal_Area_3', 'Area_STSd_Anterior',
          'Area_STSd_Posterior', 'Area_STSv_Posterior', 'Area_TG_Dorsal', 'Area_TE1_Anterior', 'Area_TE1_Posterior',
          'Area_TE2_Anterior', 'Area_TF', 'Area_TE2_Posterior', 'Area_PHT', 'Area_PH',
          'Area_TemporoParietoOccipital_Junction_1', 'Area_TemporoParietoOccipital_Junction_2',
          'Area_TemporoParietoOccipital_Junction_3', 'Dorsal_Transitional_Visual_Area', 'Area_PGp', 'Area_IntraParietal_2',
          'Area_IntraParietal_1', 'Area_IntraParietal_0', 'Area_PF_Opercular', 'Area_PF_Complex', 'Area_PFm_Complex',
          'Area_PGi', 'Area_PGs', 'Area_V6A', 'VentroMedial_Visual_Area_1', 'VentroMedial_Visual_Area_3',
          'ParaHippocampal_Area_2', 'Area_V4t', 'Area_FST', 'Area_V3CD', 'Area_Lateral_Occipital_3',
          'VentroMedial_Visual_Area_2', 'Area_31pd', 'Area_31a', 'Ventral_Visual_Complex', 'Area_25', 'Area_s32',
          'Posterior_OFC_Complex', 'Area_Posterior_Insular_1', 'Insular_Granular_Complex', 'Area_Frontal_Opercular_5',
          'Area_Posterior_10p', 'Area_Posterior_47r', 'Area_TG_ventral', 'Medial_Belt_Complex', 'Lateral_Belt_Complex',
          'Auditory_4_Complex', 'Area_STSv_Anterior', 'Area_TE1_Middle', 'Para-Insular_Area', 'Area_Anterior_32_Prime',
          'Area_Posterior_24']
label_image = load_img(atlas_path, dtype=int)
masker = NiftiLabelsMasker(label_image,
                          labels=labels,
                           standardize=False,
                           memory='nilearn_cache',
                           verbose=1,
                           detrend=False)
masker.fit()
report = masker.generate_report()
report
```

Resolving patch:
diff --git a/nilearn/input_data/nifti_labels_masker.py b/nilearn/input_data/nifti_labels_masker.py
--- a/nilearn/input_data/nifti_labels_masker.py
+++ b/nilearn/input_data/nifti_labels_masker.py
@@ -242,8 +242,9 @@ def _reporting(self):
             # previously fitted with no func image and is re-fitted
             if 'warning_message' in self._report_content:
                 self._report_content['warning_message'] = None
+            labels_image = image.load_img(labels_image, dtype=int)
             labels_image_data = image.get_data(labels_image)
-            labels_image_affine = image.load_img(labels_image).affine
+            labels_image_affine = labels_image.affine
             # Number of regions excluding the background
             number_of_regions = np.sum(np.unique(labels_image_data)
                                        != self.background_label)


Test patch:
diff --git a/nilearn/reporting/tests/test_html_report.py b/nilearn/reporting/tests/test_html_report.py
--- a/nilearn/reporting/tests/test_html_report.py
+++ b/nilearn/reporting/tests/test_html_report.py
@@ -4,7 +4,7 @@
 import numpy as np
 from nilearn import input_data
 from nilearn._utils import data_gen
-from nilearn.image import get_data
+from nilearn.image import get_data, new_img_like
 from numpy.testing import assert_almost_equal
 
 # Note: html output by nilearn view_* functions
@@ -72,6 +72,14 @@ def test_nifti_labels_masker_report():
     labels_img = data_gen.generate_labeled_regions(shape,
                                                    affine=affine,
                                                    n_regions=n_regions)
+    labels_img_floats = new_img_like(
+        labels_img, get_data(labels_img).astype(float)
+    )
+    masker = input_data.NiftiLabelsMasker(labels_img_floats,
+                                          labels=labels)
+    masker.fit()
+    masker.generate_report()
+
     # Check that providing incorrect labels raises an error
     masker = input_data.NiftiLabelsMasker(labels_img,
                                           labels=labels[:-1])