Skip to content

Commit

Permalink
Merge branch 'main' into 2405
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Feb 26, 2024
2 parents 27485a0 + a01664f commit 7c28265
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ repos:
- id: flynt

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
rev: v3.15.1
hooks:
- id: pyupgrade
args: [--py38-plus]
Expand All @@ -62,7 +62,7 @@ repos:
- id: rst-inline-touching-normal

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.1.1
rev: 24.2.0
hooks:
- id: black
args: [--config, pyproject.toml]
Expand Down
5 changes: 4 additions & 1 deletion doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
Fixes
-----

- :bdg-dark:`Code` Fix errant warning when using ``stat_type`` in :func:`nilearn.glm.compute_contrast` (:gh:`4257` by `Eric Larson`_)
- :bdg-dark:`Code` Fix errant warning when using ``stat_type`` in :func:`nilearn.glm.compute_contrast` (:gh:`4257` by `Eric Larson`_).
- :bdg-dark:`Code` Fix when thresholding is applied to images by GLM reports (:gh:`4258` by `Rémi Gau`_).
- :bdg-dark:`Code` Make sure that :class:`nilearn.maskers.NiftiSpheresMasker` reports displays properly when it contains only 1 sphere (:gh:`4269` by `Rémi Gau`_).

Enhancements
------------
Expand All @@ -17,3 +19,4 @@ Changes
-------

- :bdg-primary:`Doc` Render the description of the templates, atlases and datasets of the :mod:`nilearn.datasets` as part of the documentation (:gh:`4232` by `Rémi Gau`_).
- :bdg-dark:`Code` Change the colormap to ``gray`` for the background image in the :class:`nilearn.maskers.NiftiSpheresMasker` (:gh:`4269` by `Rémi Gau`_).
18 changes: 10 additions & 8 deletions nilearn/maskers/nifti_spheres_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,13 @@ def __init__(
# Parameters for reporting
self.reports = reports
self.report_id = -1
self._report_content = dict()
self._report_content["description"] = (
"This reports shows the regions "
"defined by the spheres of the masker."
)
self._report_content["warning_message"] = None

self._report_content = {
"description": (
"This reports shows the regions defined "
"by the spheres of the masker."
),
"warning_message": None,
}
self.verbose = verbose

def generate_report(self, displayed_spheres="all"):
Expand Down Expand Up @@ -506,7 +506,9 @@ def _reporting(self):
)
regions_summary["relative size (in %)"].append("not implemented")
if idx in spheres_to_be_displayed:
display = plotting.plot_img(img, cut_coords=positions[idx])
display = plotting.plot_img(
img, cut_coords=positions[idx], cmap="gray"
)
display.add_markers(
marker_coords=[positions[idx]],
marker_color="g",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
This directory contains files required for nilearn reporting.

html/ : templates for HTML files
css/ : styling sheets
2 changes: 1 addition & 1 deletion nilearn/reporting/data/html/report_body_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
font-size: 12pt;
font-weight: 200;
border-radius: 6px;
padding: 5px;css=resource_path.joinpath('css'),
padding: 5px;

margin-left: 3pt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@
font-size: 12pt;
font-weight: 200;
border-radius: 6px;
padding: 5px;css=resource_path.joinpath('css'),

padding: 5px;
margin-left: 3pt;

/* Position the tooltip text - see examples below! */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@
font-size: 12pt;
font-weight: 200;
border-radius: 6px;
padding: 5px;css=resource_path.joinpath('css'),

padding: 5px;
margin-left: 3pt;

/* Position the tooltip text - see examples below! */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@
font-size: 12pt;
font-weight: 200;
border-radius: 6px;
padding: 5px;css=resource_path.joinpath('css'),

padding: 5px;
margin-left: 3pt;

/* Position the tooltip text - see examples below! */
Expand Down
28 changes: 22 additions & 6 deletions nilearn/reporting/glm_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ def make_glm_report(
mask_img = model.masker_.mask_img_

mask_plot_html_code = _mask_to_svg(
mask_img=mask_img,
bg_img=bg_img,
mask_img=mask_img, bg_img=bg_img, cut_coords=cut_coords
)
all_components = _make_stat_maps_contrast_clusters(
stat_img=statistical_maps,
Expand Down Expand Up @@ -635,7 +634,7 @@ def _resize_plot_inches(plot, width_change=0, height_change=0):
return plot


def _mask_to_svg(mask_img, bg_img):
def _mask_to_svg(mask_img, bg_img, cut_coords=None):
"""Plot cuts of an mask image and creates SVG code of it.
Parameters
Expand All @@ -662,6 +661,7 @@ def _mask_to_svg(mask_img, bg_img):
bg_img=bg_img,
display_mode="z",
cmap="Set1",
cut_coords=cut_coords,
)
mask_plot_svg = _plot_to_svg(plt.gcf())
# prevents sphinx-gallery & jupyter from scraping & inserting plots
Expand Down Expand Up @@ -769,30 +769,40 @@ def _make_stat_maps_contrast_clusters(
components_template_text = html_template_obj.read()
for contrast_name, stat_map_img in stat_img.items():
component_text_ = string.Template(components_template_text)
thresholded_stat_map, threshold = threshold_stats_img(

# Only use threshold_stats_img to adjust the threshold
# that we will pass to _clustering_params_to_dataframe
# and _stat_map_to_svg
# Necessary to avoid :
# https://github.com/nilearn/nilearn/issues/4192
thresholded_img, threshold = threshold_stats_img(
stat_img=stat_map_img,
threshold=threshold,
alpha=alpha,
cluster_threshold=cluster_threshold,
height_control=height_control,
)

table_details = _clustering_params_to_dataframe(
threshold,
cluster_threshold,
min_distance,
height_control,
alpha,
)

stat_map_svg = _stat_map_to_svg(
stat_img=thresholded_stat_map,
stat_img=thresholded_img,
threshold=threshold,
bg_img=bg_img,
cut_coords=cut_coords,
display_mode=display_mode,
plot_type=plot_type,
table_details=table_details,
)

cluster_table = get_clusters_table(
stat_map_img,
thresholded_img,
stat_threshold=threshold,
cluster_threshold=cluster_threshold,
min_distance=min_distance,
Expand Down Expand Up @@ -897,6 +907,7 @@ def _clustering_params_to_dataframe(
@fill_doc
def _stat_map_to_svg(
stat_img,
threshold,
bg_img,
cut_coords,
display_mode,
Expand All @@ -913,6 +924,9 @@ def _stat_map_to_svg(
to be plotted as slices or glass brain.
Does not perform any thresholding.
threshold : float
Desired threshold in z-scale.
bg_img : Niimg-like object
Only used when plot_type is 'slice'.
See :ref:`extracting_data`.
Expand Down Expand Up @@ -971,6 +985,7 @@ def _stat_map_to_svg(
colorbar=True,
cmap=cmap,
symmetric_cbar=symmetric_cbar,
threshold=threshold,
)
elif plot_type == "glass":
stat_map_plot = plot_glass_brain(
Expand All @@ -980,6 +995,7 @@ def _stat_map_to_svg(
plot_abs=False,
symmetric_cbar=True,
cmap=cmap,
threshold=threshold,
)
else:
raise ValueError(
Expand Down
4 changes: 4 additions & 0 deletions nilearn/reporting/html_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from string import Template

from nilearn.externals import tempita
from nilearn.maskers import NiftiSpheresMasker
from nilearn.plotting.html_document import HTMLDocument
from nilearn.reporting.utils import figure_to_svg_base64

Expand Down Expand Up @@ -163,6 +164,9 @@ def _define_overlay(estimator):
if len(displays) == 1: # set overlay to None
overlay, image = None, displays[0]

elif isinstance(estimator, NiftiSpheresMasker):
overlay, image = None, displays

elif len(displays) == 2:
overlay, image = displays[0], displays[1]

Expand Down
3 changes: 3 additions & 0 deletions nilearn/reporting/tests/test_glm_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def test_stat_map_to_svg_slice_z(img_3d_mni, cut_coords):
display_mode="ortho",
plot_type="slice",
table_details=table_details,
threshold=2.76,
)


Expand All @@ -247,6 +248,7 @@ def test_stat_map_to_svg_glass_z(img_3d_mni, cut_coords):
display_mode="z",
plot_type="glass",
table_details=table_details,
threshold=2.76,
)


Expand All @@ -264,6 +266,7 @@ def test_stat_map_to_svg_invalid_plot_type(img_3d_mni, cut_coords):
display_mode="z",
plot_type="junk",
table_details={"junk": 0},
threshold=2.76,
)


Expand Down
15 changes: 15 additions & 0 deletions nilearn/reporting/tests/test_html_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ def test_nifti_spheres_masker_report_displayed_spheres_list_more_than_seeds():
masker.generate_report(displayed_spheres=displayed_spheres)


def test_nifti_spheres_masker_report_1_sphere():
"""Check the report for sphere actually works for one sphere.
See https://github.com/nilearn/nilearn/issues/4268
"""
report = NiftiSpheresMasker([(1, 1, 1)]).fit().generate_report()

empty_div = """
<img id="map1" class="pure-img" width="100%"
src="data:image/svg+xml;base64,D"
style="display:none;" alt="image"/>"""

assert empty_div not in report.body


def test_nifti_labels_masker_report_smoke_test(labels, labels_img):
"""Smoke test."""
labels_img_floats = new_img_like(
Expand Down

0 comments on commit 7c28265

Please sign in to comment.