Skip to content

Commit

Permalink
[FIX] threshold images right before ploting in GLM reports (#4258)
Browse files Browse the repository at this point in the history
* fix thresholding in GLM report

* update doc string

* add comment

* fix tests

* fix and pass cut coord to mask

* update changelog
  • Loading branch information
Remi-Gau committed Feb 13, 2024
1 parent 58bad49 commit 0beaaf8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
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`_).

Enhancements
------------
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 @@ -224,8 +224,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 @@ -629,7 +628,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 @@ -656,6 +655,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 @@ -763,30 +763,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 @@ -889,6 +899,7 @@ def _clustering_params_to_dataframe(
@fill_doc
def _stat_map_to_svg(
stat_img,
threshold,
bg_img,
cut_coords,
display_mode,
Expand All @@ -905,6 +916,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 @@ -945,13 +959,15 @@ def _stat_map_to_svg(
bg_img=bg_img,
cut_coords=cut_coords,
display_mode=display_mode,
threshold=threshold,
)
elif plot_type == "glass":
stat_map_plot = plot_glass_brain(
stat_img,
display_mode=display_mode,
colorbar=True,
plot_abs=False,
threshold=threshold,
)
else:
raise ValueError(
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

0 comments on commit 0beaaf8

Please sign in to comment.