Skip to content

Commit

Permalink
FIX register_cmap for matplotlib > 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdoc committed May 16, 2024
1 parent c8a9c6c commit 318646c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
25 changes: 17 additions & 8 deletions cortex/dataset/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
9 changes: 8 additions & 1 deletion cortex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 318646c

Please sign in to comment.