Ejemplo n.º 1
0
# Instantiate the mask and visualize atlas
#
from nilearn.input_data import NiftiLabelsMasker

# Instantiate the masker with label image and label values
masker = NiftiLabelsMasker(atlas.maps, labels=atlas.labels, standardize=True)

# Visualize the atlas
# Note that we need to call fit prior to generating the mask
masker.fit()

# At this point, no functional image has been provided to the masker.
# We can still generate a report which can be displayed in a Jupyter
# Notebook, opened in a browser using the .open_in_browser() method,
# or saved to a file using the .save_as_html(output_filepath) mathod.
report = masker.generate_report()
report

##########################################################################
# Fitting the mask and generating a report
masker.fit(func_filename)

# We can again generate a report, but this time, the provided functional
# image is displayed with the ROI of the atlas.
# The report also contains a summary table giving the region sizes in mm3
report = masker.generate_report()
report

###########################################################################
# Process the data with the NiftiLablesMasker
#
Ejemplo n.º 2
0
def test_nifti_labels_masker_report(data_img_3d, mask):
    shape = (13, 11, 12)
    affine = np.diag([2, 2, 2, 1])
    n_regions = 9
    labels = ['background'
              ] + ['region_{}'.format(i) for i in range(1, n_regions + 1)]
    EXPECTED_COLUMNS = [
        'label value', 'region name', 'size (in mm^3)', 'relative size (in %)'
    ]
    labels_img = 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 = NiftiLabelsMasker(labels_img_floats, labels=labels)
    masker.fit()
    masker.generate_report()

    # Check that providing incorrect labels raises an error
    masker = NiftiLabelsMasker(labels_img, labels=labels[:-1])
    masker.fit()
    with pytest.raises(ValueError,
                       match="Mismatch between the number of provided labels"):
        masker.generate_report()
    masker = NiftiLabelsMasker(labels_img, labels=labels)
    masker.fit()
    # Check that a warning is given when generating the report
    # since no image was provided to fit
    with pytest.warns(UserWarning,
                      match="No image provided to fit in NiftiLabelsMasker"):
        masker.generate_report()

    # No image was provided to fit, regions are plotted using
    # plot_roi such that no contour should be in the image
    display = masker._reporting()
    for d in ['x', 'y', 'z']:
        assert len(display[0].axes[d].ax.collections) == 0

    masker = NiftiLabelsMasker(labels_img, labels=labels)
    masker.fit(data_img_3d)

    display = masker._reporting()
    for d in ['x', 'y', 'z']:
        assert len(display[0].axes[d].ax.collections) > 0
        assert len(display[0].axes[d].ax.collections) <= n_regions

    masker = NiftiLabelsMasker(labels_img, labels=labels, mask_img=mask)
    masker.fit(data_img_3d)
    report = masker.generate_report()
    assert masker._reporting_data is not None
    # Check that background label was left as default
    assert masker.background_label == 0
    assert masker._report_content['description'] == (
        'This reports shows the regions defined by the labels of the mask.')
    # Check that the number of regions is correct
    assert masker._report_content['number_of_regions'] == n_regions
    # Check that all expected columns are present with the right size
    for col in EXPECTED_COLUMNS:
        assert col in masker._report_content['summary']
        assert len(masker._report_content['summary'][col]) == n_regions
    # Check that labels match
    assert masker._report_content['summary']['region name'] == labels[1:]
    # Relative sizes of regions should sum to 100%
    assert_almost_equal(
        sum(masker._report_content['summary']['relative size (in %)']), 100)
    _check_html(report)
    assert "Regions summary" in str(report)
    # Check region sizes calculations
    expected_region_sizes = Counter(get_data(labels_img).ravel())
    for r in range(1, n_regions + 1):
        assert_almost_equal(
            masker._report_content['summary']['size (in mm^3)'][r - 1],
            expected_region_sizes[r] * np.abs(np.linalg.det(affine[:3, :3])))

    # Check that region labels are no displayed in the report
    # when they were not provided by the user.
    masker = NiftiLabelsMasker(labels_img)
    masker.fit()
    report = masker.generate_report()
    for col in EXPECTED_COLUMNS:
        if col == "region name":
            assert col not in masker._report_content["summary"]
        else:
            assert col in masker._report_content["summary"]
            assert len(masker._report_content['summary'][col]) == n_regions