Skip to content

Commit aebe236

Browse files
authored
MNT,FIX test code on python 3.11, fix matplotlib register cmap (#536)
1 parent e8dfe56 commit aebe236

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

.github/workflows/install_from_wheel.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
sudo apt-get update
3636
sudo apt-get install -y inkscape
3737
pip install --upgrade pip
38-
pip install wheel numpy "cython<3.0"
38+
pip install build wheel numpy "cython<3.0"
3939
4040
- name: Create the wheel
4141
run: python setup.py bdist_wheel

.github/workflows/run_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
python-version: [3.7, 3.8, 3.9, "3.10"]
16+
python-version: [3.7, 3.8, 3.9, "3.10", "3.11"]
1717
max-parallel: 5
1818

1919
steps:
@@ -35,7 +35,7 @@ jobs:
3535
sudo apt-get update
3636
sudo apt-get install -y inkscape
3737
pip install --upgrade pip
38-
pip install wheel numpy "cython<3.0"
38+
pip install wheel setuptools numpy "cython<3.0"
3939
# force using latest nibabel
4040
pip install -U nibabel
4141
pip install -e . --no-build-isolation

cortex/dataset/views.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
import os
21
import glob
32
import json
3+
import os
4+
45
import h5py
56
import numpy as np
67

78
from .. import options
8-
from .braindata import BrainData, VolumeData, VertexData
9+
from .braindata import BrainData, VertexData, VolumeData
910

1011
default_cmap = options.config.get("basic", "default_cmap")
1112

13+
# register_cmap is deprecated in matplotlib > 3.7.0 and replaced by colormaps.register
14+
try:
15+
from matplotlib import colormaps as cm
16+
def register_cmap(cmap):
17+
return cm.register(cmap)
18+
except ImportError:
19+
from matplotlib.cm import register_cmap
20+
1221

1322
def normalize(data):
1423
if isinstance(data, tuple):
@@ -193,7 +202,8 @@ def _write_hdf(self, h5, name="data", data=None, xfmname=None):
193202
def get_cmapdict(self):
194203
"""Returns a dictionary with cmap information."""
195204

196-
from matplotlib import colors, pyplot as plt
205+
from matplotlib import colors
206+
from matplotlib import pyplot as plt
197207

198208
try:
199209
# plt.get_cmap accepts:
@@ -206,18 +216,19 @@ def get_cmapdict(self):
206216
cmapdir = options.config.get('webgl', 'colormaps')
207217
colormaps = glob.glob(os.path.join(cmapdir, "*.png"))
208218
colormaps = dict(((os.path.split(c)[1][:-4], c) for c in colormaps))
209-
if not self.cmap in colormaps:
219+
if self.cmap not in colormaps:
210220
raise ValueError('Unkown color map %s' % self.cmap)
211221
I = plt.imread(colormaps[self.cmap])
212-
cmap = colors.ListedColormap(np.squeeze(I))
222+
name = self.cmap if isinstance(self.cmap, str) else self.cmap.name
223+
cmap = colors.ListedColormap(np.squeeze(I), name=name)
213224
# Register colormap to matplotlib to avoid loading it again
214-
plt.register_cmap(self.cmap, cmap)
225+
register_cmap(cmap)
215226

216227
return dict(cmap=cmap, vmin=self.vmin, vmax=self.vmax)
217228

218229
@property
219230
def raw(self):
220-
from matplotlib import colors, cm
231+
from matplotlib import cm, colors
221232

222233
cmap = self.get_cmapdict()['cmap']
223234
# Normalize colors according to vmin, vmax
@@ -398,5 +409,5 @@ def u(s, encoding='utf8'):
398409
return s
399410

400411

401-
from .viewRGB import VolumeRGB, VertexRGB, Colors
402-
from .view2D import Volume2D, Vertex2D
412+
from .viewRGB import Colors, VertexRGB, VolumeRGB
413+
from .view2D import Vertex2D, Volume2D

cortex/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
from .testing_utils import INKSCAPE_VERSION
2424
from .volume import anat2epispace
2525

26+
# register_cmap is deprecated in matplotlib > 3.7.0 and replaced by colormaps.register
27+
try:
28+
from matplotlib import colormaps as cm
29+
def register_cmap(cmap):
30+
return cm.register(cmap)
31+
except ImportError:
32+
from matplotlib.cm import register_cmap
33+
2634

2735
class DocLoader(object):
2836
def __init__(self, func, mod, package):
@@ -1000,9 +1008,9 @@ def get_cmap(name):
10001008
colormaps = dict((c[:-4], os.path.join(cmapdir, c)) for c in colormaps)
10011009
if name in colormaps:
10021010
I = plt.imread(colormaps[name])
1003-
cmap = colors.ListedColormap(np.squeeze(I))
1011+
cmap = colors.ListedColormap(np.squeeze(I), name=name)
10041012
try:
1005-
plt.cm.register_cmap(name,cmap)
1013+
register_cmap(cmap)
10061014
except:
10071015
print(f"Color map {name} is already registered.")
10081016
else:

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[build-system]
22
# Minimum requirements for the build system to execute, according to PEP518
33
# specification.
4-
requires = ["numpy", "cython<3.0", "setuptools", "wheel"]
4+
requires = ["setuptools", "build", "numpy", "cython<3.0", "wheel"]
5+
build-backend = "setuptools.build_meta"
56

67
[tool.codespell]
78
skip = '.git,*.pdf,*.svg,*.css,*.min.*,*.gii,resources,OpenCTM-1.0.3,filestore,build,_build'

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
setuptools
12
future
23
numpy
34
scipy

0 commit comments

Comments
 (0)