Skip to content

Commit 4769524

Browse files
committed
Fixup unit test
The unit test appears to have fallen out of sync with the code long ago (e.g. it seems to previously expect the values from `get_sRGB` to range from 0-255). I did my best to restore it, but at this point I'm not 100% sure why the colormaps only match approximately after applying these fixes.
1 parent f9dd86d commit 4769524

File tree

5 files changed

+121
-72
lines changed

5 files changed

+121
-72
lines changed

.github/workflows/test.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: "Unit test"
2+
on:
3+
- "push"
4+
- "pull_request"
5+
6+
jobs:
7+
8+
unit-test-with-pytest:
9+
runs-on: "ubuntu-latest"
10+
strategy:
11+
matrix:
12+
python-version:
13+
- "3.11"
14+
- "3.10"
15+
- "3.9"
16+
- "3.8"
17+
pyqt-dependency:
18+
- "PyQt5"
19+
# - "PyQt6"
20+
21+
steps:
22+
- uses: "actions/checkout@v3"
23+
24+
- name: "Set up Python ${{ matrix.python-version }}"
25+
uses: "actions/setup-python@v4"
26+
with:
27+
python-version: "${{ matrix.python-version }}"
28+
29+
- name: "Install dependencies in pyproject.toml"
30+
run: |
31+
pip install .
32+
pip install pytest pytest-cov ${{ matrix.pyqt-dependency }}
33+
34+
- name: "Run tests"
35+
run: "make test"

.travis.yml

-20
This file was deleted.

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
.PHONY: test
2+
test:
3+
python -m pytest --version
4+
python -m pytest test/
5+
6+
17
.PHONY: lint
28
lint:
9+
pre-commit --version
310
pre-commit run --all-files --show-diff-on-failure --color always
11+
12+
13+
.PHONY: ci
14+
ci: precommit test

environment.yml

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ dependencies:
1212

1313
# Development
1414
- "pre-commit"
15+
- "pytest"
16+
- "pytest-cov"
17+
- "pytest-qt"
18+
- "pytest-xvfb"
1519

1620
- pip:
1721
- "build ~=0.10"

tests.py test/test_editor_loads_native.py

+71-52
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,80 @@
1-
import numpy as np
1+
import json
2+
3+
import pytest
24

3-
from viscm.bezierbuilder import json
45
from viscm.gui import Colormap, viscm_editor
56

6-
cms = {
7-
"viscm/examples/sample_linear.jscm",
8-
"viscm/examples/sample_diverging.jscm",
9-
"viscm/examples/sample_diverging_continuous.jscm",
10-
}
11-
12-
13-
def test_editor_loads_native():
14-
for k in cms:
15-
with open(k) as f:
16-
data = json.loads(f.read())
17-
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
18-
cm.load(k)
19-
viscm = viscm_editor(
20-
uniform_space=cm.uniform_space,
21-
cmtype=cm.cmtype,
22-
method=cm.method,
23-
**cm.params,
24-
)
25-
assert viscm.name == data["name"]
26-
27-
extensions = data["extensions"]["https://matplotlib.org/viscm"]
28-
xp, yp, fixed = viscm.control_point_model.get_control_points()
29-
30-
assert extensions["fixed"] == fixed
31-
assert len(extensions["xp"]) == len(xp)
32-
assert len(extensions["yp"]) == len(yp)
33-
assert len(xp) == len(yp)
34-
for i in range(len(xp)):
35-
assert extensions["xp"][i] == xp[i]
36-
assert extensions["yp"][i] == yp[i]
37-
assert extensions["min_Jp"] == viscm.min_Jp
38-
assert extensions["max_Jp"] == viscm.max_Jp
39-
assert extensions["filter_k"] == viscm.filter_k
40-
assert extensions["cmtype"] == viscm.cmtype
41-
42-
colors = data["colors"]
43-
colors = [
44-
[int(c[i : i + 2], 16) / 256 for i in range(0, 6, 2)]
45-
for c in [colors[i : i + 6] for i in range(0, len(colors), 6)]
46-
]
47-
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
48-
for i in range(len(colors)):
49-
for z in range(3):
50-
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)
7+
8+
def approxeq(x, y, *, err=0.0001):
9+
return abs(y - x) < err
10+
11+
12+
@pytest.mark.parametrize(
13+
"colormap_file",
14+
[
15+
"viscm/examples/sample_linear.jscm",
16+
"viscm/examples/sample_diverging.jscm",
17+
"viscm/examples/sample_diverging_continuous.jscm",
18+
],
19+
)
20+
def test_editor_loads_native(colormap_file):
21+
with open(colormap_file) as f:
22+
data = json.loads(f.read())
23+
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
24+
cm.load(colormap_file)
25+
viscm = viscm_editor(
26+
uniform_space=cm.uniform_space,
27+
cmtype=cm.cmtype,
28+
method=cm.method,
29+
**cm.params,
30+
)
31+
assert viscm.name == data["name"]
32+
33+
extensions = data["extensions"]["https://matplotlib.org/viscm"]
34+
xp, yp, fixed = viscm.control_point_model.get_control_points()
35+
36+
assert extensions["fixed"] == fixed
37+
assert len(extensions["xp"]) == len(xp)
38+
assert len(extensions["yp"]) == len(yp)
39+
assert len(xp) == len(yp)
40+
for i in range(len(xp)):
41+
assert extensions["xp"][i] == xp[i]
42+
assert extensions["yp"][i] == yp[i]
43+
assert extensions["min_Jp"] == viscm.min_Jp
44+
assert extensions["max_Jp"] == viscm.max_Jp
45+
assert extensions["filter_k"] == viscm.cmap_model.filter_k
46+
assert extensions["cmtype"] == viscm.cmtype
47+
48+
# Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of
49+
# two-character (0-255) values) to 3-tuples of floats (0-1).
50+
colors_hex = data["colors"]
51+
colors_hex = [colors_hex[i : i + 6] for i in range(0, len(colors_hex), 6)]
52+
colors = [
53+
# TODO: Should we divide by 255 here instead of 256? The tests pass with a
54+
# lower value for `err` if we do.
55+
[int(c[i : i + 2], 16) / 256 for i in range(0, len(c), 2)]
56+
for c in colors_hex
57+
]
58+
59+
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
60+
61+
for i in range(len(colors)):
62+
for z in range(3):
63+
assert approxeq(colors[i][z], editor_colors[i][z], err=0.01)
5164

5265

5366
# import matplotlib as mpl
54-
# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
67+
# try:
68+
# from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
69+
# except ImportError:
70+
# try:
71+
# from matplotlib.backends.backend_qt5agg import (
72+
# FigureCanvasQTAgg as FigureCanvas
73+
# )
74+
# except ImportError:
75+
# from matplotlib.backends.backend_qt4agg import (
76+
# FigureCanvasQTAgg as FigureCanvas
77+
# )
5578
# from matplotlib.backends.qt_compat import QtCore, QtGui
5679
#
5780
# def test_editor_add_point():
@@ -144,7 +167,3 @@ def test_editor_loads_native():
144167

145168
# print(linear.control_point_model.get_control_points())
146169
# # print(linear.cmap_model.get_Jpapbp(3))
147-
148-
149-
def approxeq(x, y, err=0.0001):
150-
return abs(y - x) < err

0 commit comments

Comments
 (0)