From 318646c3bd92a5589a463d4be1db08b0625338c9 Mon Sep 17 00:00:00 2001 From: Matteo Visconti di Oleggio Castello Date: Wed, 15 May 2024 18:01:10 -0700 Subject: [PATCH] FIX register_cmap for matplotlib > 3.7 --- cortex/dataset/views.py | 25 +++++++++++++++++-------- cortex/utils.py | 9 ++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cortex/dataset/views.py b/cortex/dataset/views.py index 8a74a468..6ce68ab4 100644 --- a/cortex/dataset/views.py +++ b/cortex/dataset/views.py @@ -1,14 +1,22 @@ -import os import glob import json +import os + import h5py import numpy as np from .. import options -from .braindata import BrainData, VolumeData, VertexData +from .braindata import BrainData, VertexData, VolumeData default_cmap = options.config.get("basic", "default_cmap") +try: + from matplotlib.cm import register_cmap +except ImportError: + from matplotlib import colormaps + def register_cmap(*args, **kwargs): + return colormaps.register(*args, **kwargs) + def normalize(data): if isinstance(data, tuple): @@ -193,7 +201,8 @@ def _write_hdf(self, h5, name="data", data=None, xfmname=None): def get_cmapdict(self): """Returns a dictionary with cmap information.""" - from matplotlib import colors, pyplot as plt + from matplotlib import colors + from matplotlib import pyplot as plt try: # plt.get_cmap accepts: @@ -206,18 +215,18 @@ def get_cmapdict(self): cmapdir = options.config.get('webgl', 'colormaps') colormaps = glob.glob(os.path.join(cmapdir, "*.png")) colormaps = dict(((os.path.split(c)[1][:-4], c) for c in colormaps)) - if not self.cmap in colormaps: + if self.cmap not in colormaps: raise ValueError('Unkown color map %s' % self.cmap) I = plt.imread(colormaps[self.cmap]) cmap = colors.ListedColormap(np.squeeze(I)) # Register colormap to matplotlib to avoid loading it again - plt.register_cmap(self.cmap, cmap) + register_cmap(self.cmap, cmap) return dict(cmap=cmap, vmin=self.vmin, vmax=self.vmax) @property def raw(self): - from matplotlib import colors, cm + from matplotlib import cm, colors cmap = self.get_cmapdict()['cmap'] # Normalize colors according to vmin, vmax @@ -398,5 +407,5 @@ def u(s, encoding='utf8'): return s -from .viewRGB import VolumeRGB, VertexRGB, Colors -from .view2D import Volume2D, Vertex2D +from .view2D import Vertex2D, Volume2D +from .viewRGB import Colors, VertexRGB, VolumeRGB diff --git a/cortex/utils.py b/cortex/utils.py index edcd3aa7..642ae652 100644 --- a/cortex/utils.py +++ b/cortex/utils.py @@ -23,6 +23,13 @@ from .testing_utils import INKSCAPE_VERSION from .volume import anat2epispace +try: + from matplotlib.cm import register_cmap +except ImportError: + from matplotlib import colormaps + def register_cmap(*args, **kwargs): + return colormaps.register(*args, **kwargs) + class DocLoader(object): def __init__(self, func, mod, package): @@ -1001,7 +1008,7 @@ def get_cmap(name): if name in colormaps: I = plt.imread(colormaps[name]) cmap = colors.ListedColormap(np.squeeze(I)) - plt.cm.register_cmap(name,cmap) + register_cmap(name, cmap) else: try: cmap = plt.cm.get_cmap(name)