Skip to content

Commit

Permalink
MNT,FIX test code on python 3.11, fix matplotlib register cmap (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdoc authored May 21, 2024
1 parent e8dfe56 commit aebe236
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/install_from_wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y inkscape
pip install --upgrade pip
pip install wheel numpy "cython<3.0"
pip install build wheel numpy "cython<3.0"
- name: Create the wheel
run: python setup.py bdist_wheel
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, "3.10"]
python-version: [3.7, 3.8, 3.9, "3.10", "3.11"]
max-parallel: 5

steps:
Expand All @@ -35,7 +35,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y inkscape
pip install --upgrade pip
pip install wheel numpy "cython<3.0"
pip install wheel setuptools numpy "cython<3.0"
# force using latest nibabel
pip install -U nibabel
pip install -e . --no-build-isolation
Expand Down
29 changes: 20 additions & 9 deletions cortex/dataset/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
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")

# register_cmap is deprecated in matplotlib > 3.7.0 and replaced by colormaps.register
try:
from matplotlib import colormaps as cm
def register_cmap(cmap):
return cm.register(cmap)
except ImportError:
from matplotlib.cm import register_cmap


def normalize(data):
if isinstance(data, tuple):
Expand Down Expand Up @@ -193,7 +202,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 +216,19 @@ 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))
name = self.cmap if isinstance(self.cmap, str) else self.cmap.name
cmap = colors.ListedColormap(np.squeeze(I), name=name)
# Register colormap to matplotlib to avoid loading it again
plt.register_cmap(self.cmap, cmap)
register_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 +409,5 @@ def u(s, encoding='utf8'):
return s


from .viewRGB import VolumeRGB, VertexRGB, Colors
from .view2D import Volume2D, Vertex2D
from .viewRGB import Colors, VertexRGB, VolumeRGB
from .view2D import Vertex2D, Volume2D
12 changes: 10 additions & 2 deletions cortex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
from .testing_utils import INKSCAPE_VERSION
from .volume import anat2epispace

# register_cmap is deprecated in matplotlib > 3.7.0 and replaced by colormaps.register
try:
from matplotlib import colormaps as cm
def register_cmap(cmap):
return cm.register(cmap)
except ImportError:
from matplotlib.cm import register_cmap


class DocLoader(object):
def __init__(self, func, mod, package):
Expand Down Expand Up @@ -1000,9 +1008,9 @@ def get_cmap(name):
colormaps = dict((c[:-4], os.path.join(cmapdir, c)) for c in colormaps)
if name in colormaps:
I = plt.imread(colormaps[name])
cmap = colors.ListedColormap(np.squeeze(I))
cmap = colors.ListedColormap(np.squeeze(I), name=name)
try:
plt.cm.register_cmap(name,cmap)
register_cmap(cmap)
except:
print(f"Color map {name} is already registered.")
else:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[build-system]
# Minimum requirements for the build system to execute, according to PEP518
# specification.
requires = ["numpy", "cython<3.0", "setuptools", "wheel"]
requires = ["setuptools", "build", "numpy", "cython<3.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.codespell]
skip = '.git,*.pdf,*.svg,*.css,*.min.*,*.gii,resources,OpenCTM-1.0.3,filestore,build,_build'
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
setuptools
future
numpy
scipy
Expand Down

0 comments on commit aebe236

Please sign in to comment.