Fix cmap=ListedColormap raising 'unhashable type' and add regression tests - #162
Merged
Merged
Conversation
`build_plotter` previously called `cm in colormaps` directly where `colormaps` is a dict. Passing a matplotlib `ListedColormap` / `LinearSegmentedColormap` instance therefore raised `TypeError: unhashable type: 'ListedColormap'` — the same failure reported by hippomaps users calling `surfplot_canonical_foldunfold` with `cmap=cmc.devon`. Master already guarded the membership check with `isinstance(cm, str)`, but still relied on `plt.get_cmap(cm)` to coerce the non-string branch. Make the path explicit: - accept `matplotlib.colors.Colormap` instances directly - only call `plt.get_cmap` for string inputs - raise a clear `TypeError` for any other type Also broaden the `cmap` parameter docstrings on `build_plotter`, `plot_surf`, and `plot_hemispheres` to document Colormap-instance support. Tests in `brainspace/tests/test_plotting.py` now cover: - `ListedColormap` and `LinearSegmentedColormap` instances - matplotlib registered cmap names - brainspace-registered cmap names (`BuGyRd`) - mixed string/Colormap entries across layout cells - invalid cmap types fail with a useful error - end-to-end `plot_surf` smoke test with a Colormap instance Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
build_plotterdoescm in colormapswherecolormapsis a dict. Passing a matplotlibListedColormap/LinearSegmentedColormapinstance therefore raisedTypeError: unhashable type: 'ListedColormap'.surfplot_canonical_foldunfold(..., cmap=cmc.devon, ...)after reinstalling brainspace; works around with a string cmap name only.isinstance(cm, str), but still leaned onplt.get_cmap(cm)to coerce the non-string branch. This PR makes the path explicit (Colormap instance vs. string vs. unsupported type) and pins the contract with regression tests so the bug can't slip back in.Changes
brainspace/plotting/surface_plotting.pymatplotlib.colors.Colormap.build_plotter's LUT block: acceptColormapinstances directly, route strings throughplt.get_cmap, and raise a clearTypeErrorfor anything else.cmapdocstrings onbuild_plotter,plot_surf, andplot_hemispheresto document Colormap-instance support.brainspace/tests/test_plotting.py— 7 new regression tests:ListedColormapandLinearSegmentedColormapinstance cases (the original bug)BuGyRd) continue to resolve to the precomputed lookupTypeErrorplot_surfsmoke test with a Colormap instanceTest plan
pytest brainspace/tests/test_plotting.py brainspace/tests/test_colormaps.py— 19 passed locally on macOS / Python 3.10 / matplotlib 3.10.ListedColormap in colormaps-> unhashable) and confirmedbuild_plotter(..., cmap=ListedColormap(...))now succeeds.