Skip to content

Commit

Permalink
Warn in nifti masker report when no image provided to fit (#2694)
Browse files Browse the repository at this point in the history
* Add warning and warning message to report when no image provided to fit

* Add tests
  • Loading branch information
NicolasGensollen committed Feb 16, 2021
1 parent 29f64ef commit e239f40
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 5 additions & 1 deletion nilearn/input_data/nifti_masker.py
Expand Up @@ -249,7 +249,7 @@ def __init__(self, mask_img=None, sessions=None, smoothing_fwhm=None,
'mask and its input image. ')
self._overlay_text = ('\n To see the input Nifti image before '
'resampling, hover over the displayed image.')

self._warning_message = ""
self._shelving = False

def generate_report(self):
Expand Down Expand Up @@ -283,6 +283,10 @@ def _reporting(self):
# compute middle image from 4D series for plotting
img = image.index_img(img, dim[-1] // 2)
else: # images were not provided to fit
msg = ("No image provided to fit in NiftiMasker. "
"Setting image to mask for reporting.")
warnings.warn(msg)
self._warning_message = msg
img = mask

# create display of retained input mask, image
Expand Down
6 changes: 6 additions & 0 deletions nilearn/reporting/data/html/report_body_template.html
Expand Up @@ -179,6 +179,11 @@
opacity: 1;
}

.elem-warn {
color: #FF0000;
font-weight: bold;
}

</style>
<div class="nilearn_report">
<h1 class="withtooltip">
Expand All @@ -197,6 +202,7 @@ <h1 class="withtooltip">
</div>
</div>
<div class="pure-u-1 pure-u-md-1-3 raise">
<p class="elem-warn">{{warning_message}}</p>
<p class="elem-desc">{{description}}</p>
<p></p>
<details>
Expand Down
14 changes: 11 additions & 3 deletions nilearn/reporting/html_report.py
Expand Up @@ -46,7 +46,7 @@ def _str_params(params):


def _update_template(title, docstring, content, overlay,
parameters, description=None):
parameters, description=None, warning_message=None):
"""Populate a report with content.
Parameters
Expand All @@ -69,6 +69,11 @@ def _update_template(title, docstring, content, overlay,
description : str, optional
An optional description of the content.
warning_message : str, optional
An optional warning message to be displayed in red.
This is used for example when no image was provided
to the estimator when fitting.
Returns
-------
report : HTMLReport
Expand All @@ -85,7 +90,8 @@ def _update_template(title, docstring, content, overlay,
overlay=overlay,
docstring=docstring,
parameters=parameters,
description=description)
description=description,
warning_message=warning_message)

head_template_name = 'report_head_template.html'
head_template_path = resource_path.joinpath(head_template_name)
Expand Down Expand Up @@ -148,6 +154,7 @@ def generate_report(estimator):
else: # We can create a report
overlay, image = _define_overlay(estimator)
description = estimator._report_description
warning_message = estimator._warning_message
parameters = _str_params(estimator.get_params())
docstring = estimator.__doc__
snippet = docstring.partition('Parameters\n ----------\n')[0]
Expand All @@ -156,7 +163,8 @@ def generate_report(estimator):
content=_embed_img(image),
overlay=_embed_img(overlay),
parameters=parameters,
description=description)
description=description,
warning_message=warning_message)
return report


Expand Down
10 changes: 9 additions & 1 deletion nilearn/reporting/tests/test_html_report.py
Expand Up @@ -37,13 +37,19 @@ def test_3d_reports():
# check providing mask to init
masker = input_data.NiftiMasker(mask_img=mask_img_3d)
masker.fit(data_img_3d)
assert mask._warning_message == ""
html = masker.generate_report()
_check_html(html)

# check providing mask to init and no images to .fit
masker = input_data.NiftiMasker(mask_img=mask_img_3d)
assert masker._warning_message == ""
masker.fit()
html = masker.generate_report()
warn_message = ("No image provided to fit in NiftiMasker. "
"Setting image to mask for reporting.")
with pytest.warns(UserWarning, match=warn_message):
html = masker.generate_report()
assert masker._warning_message == warn_message
_check_html(html)


Expand All @@ -63,12 +69,14 @@ def test_4d_reports():
# test .fit method
mask = input_data.NiftiMasker(mask_strategy='epi')
mask.fit(data_img_4d)
assert mask._warning_message == ""
html = mask.generate_report()
_check_html(html)

# test .fit_transform method
masker = input_data.NiftiMasker(mask_img=mask_img, standardize=True)
masker.fit_transform(data_img_4d)
assert mask._warning_message == ""
html = masker.generate_report()
_check_html(html)

Expand Down

0 comments on commit e239f40

Please sign in to comment.