Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix color bar handling with color map with only 1 level #4256

Merged
merged 8 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/01_plotting/plot_demo_glass_brain_extensive.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
threshold=2,
symmetric_cbar=False,
cmap="viridis",
title="only plot positive values",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those plots were added in the #3993
just adding some title so the figures are easier to identify when running the example dumps > 20 figures on you

)

# %%
Expand All @@ -116,6 +117,7 @@
threshold=2,
symmetric_cbar=False,
cmap="viridis",
title="vmin == threshold",
)

# %%
Expand Down
10 changes: 10 additions & 0 deletions nilearn/plotting/displays/_slicers.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,16 @@
threshold = float(threshold) if threshold is not None else None

affine = img.affine
if threshold is not None:
data = safe_get_data(img, ensure_finite=True)

Check warning on line 400 in nilearn/plotting/displays/_slicers.py

View check run for this annotation

Codecov / codecov/patch

nilearn/plotting/displays/_slicers.py#L400

Added line #L400 was not covered by tests
if threshold == 0:
data = np.ma.masked_equal(data, 0, copy=False)

Check warning on line 402 in nilearn/plotting/displays/_slicers.py

View check run for this annotation

Codecov / codecov/patch

nilearn/plotting/displays/_slicers.py#L402

Added line #L402 was not covered by tests
else:
data = np.ma.masked_inside(

Check warning on line 404 in nilearn/plotting/displays/_slicers.py

View check run for this annotation

Codecov / codecov/patch

nilearn/plotting/displays/_slicers.py#L404

Added line #L404 was not covered by tests
data, -threshold, threshold, copy=False
)
img = new_img_like(img, data, affine)

Check warning on line 407 in nilearn/plotting/displays/_slicers.py

View check run for this annotation

Codecov / codecov/patch

nilearn/plotting/displays/_slicers.py#L407

Added line #L407 was not covered by tests

data = safe_get_data(img, ensure_finite=True)
data_bounds = get_bounds(data.shape, affine)
(xmin, xmax), (ymin, ymax), (zmin, zmax) = data_bounds
Expand Down
22 changes: 22 additions & 0 deletions nilearn/plotting/tests/test_img_plotting/test_plot_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,25 @@ def test_demo_plot_roi_output_file(tmp_path):
with open(filename, "wb") as fp:
out = demo_plot_roi(output_file=fp)
assert out is None


def test_cmap_with_one_level(shape_3d_default, affine_eye):
"""Test we can handle cmap with only 1 level

Regression test for
https://github.com/nilearn/nilearn/issues/4255
"""
array_data = np.zeros(shape_3d_default)
array_data[0, 1, 1] = 1

img = Nifti1Image(array_data, affine_eye)

clust_ids = list(np.unique(img.get_fdata())[1:])

try:
cmap = plt.colormaps["tab20"].resampled(len(clust_ids))
except TypeError:
# for older versions of matplotlib
cmap = plt.cm.get_cmap("tab20", len(clust_ids))

plot_roi(img, alpha=0.8, colorbar=True, cmap=cmap)