Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: "Unit test"
on:
- "push"
- "pull_request"

jobs:

unit-test-with-pytest:
runs-on: "ubuntu-latest"
strategy:
matrix:
python-version:
- "3.11"
- "3.10"
- "3.9"
- "3.8"
pyqt-dependency:
- "PyQt5"
# - "PyQt6"

steps:
- uses: "actions/checkout@v3"

- name: "Set up Python ${{ matrix.python-version }}"
uses: "actions/setup-python@v4"
with:
python-version: "${{ matrix.python-version }}"

- name: "Install dependencies in pyproject.toml"
run: |
pip install .
pip install pytest pytest-cov ${{ matrix.pyqt-dependency }}
- name: "Run tests"
run: "make test"
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
.PHONY: test
test:
python -m pytest --version
python -m pytest test/


.PHONY: lint
lint:
pre-commit --version
pre-commit run --all-files --show-diff-on-failure --color always


.PHONY: ci
ci: lint test
4 changes: 4 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ dependencies:

# Development
- "pre-commit"
- "pytest"
- "pytest-cov"
- "pytest-qt"
- "pytest-xvfb"

- pip:
- "build ~=0.10"
124 changes: 73 additions & 51 deletions tests.py → test/test_editor_loads_native.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,83 @@
import json

import numpy as np
import pytest

from viscm.bezierbuilder import json
from viscm.gui import Colormap, viscm_editor

cms = {
"viscm/examples/sample_linear.jscm",
"viscm/examples/sample_diverging.jscm",
"viscm/examples/sample_diverging_continuous.jscm",
}


def test_editor_loads_native():
for k in cms:
with open(k) as f:
data = json.loads(f.read())
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
cm.load(k)
viscm = viscm_editor(
uniform_space=cm.uniform_space,
cmtype=cm.cmtype,
method=cm.method,
**cm.params,
)
assert viscm.name == data["name"]

extensions = data["extensions"]["https://matplotlib.org/viscm"]
xp, yp, fixed = viscm.control_point_model.get_control_points()

assert extensions["fixed"] == fixed
assert len(extensions["xp"]) == len(xp)
assert len(extensions["yp"]) == len(yp)
assert len(xp) == len(yp)
for i in range(len(xp)):
assert extensions["xp"][i] == xp[i]
assert extensions["yp"][i] == yp[i]
assert extensions["min_Jp"] == viscm.min_Jp
assert extensions["max_Jp"] == viscm.max_Jp
assert extensions["filter_k"] == viscm.filter_k
assert extensions["cmtype"] == viscm.cmtype

colors = data["colors"]
colors = [
[int(c[i : i + 2], 16) / 256 for i in range(0, 6, 2)]
for c in [colors[i : i + 6] for i in range(0, len(colors), 6)]
]
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
for i in range(len(colors)):
for z in range(3):
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)

def approxeq(x, y, *, err=0.0001):
return abs(y - x) < err


@pytest.mark.parametrize(
"colormap_file",
[
"viscm/examples/sample_linear.jscm",
"viscm/examples/sample_diverging.jscm",
"viscm/examples/sample_diverging_continuous.jscm",
],
)
@pytest.mark.xfail(reason="Test very old; intent unclear")
def test_editor_loads_native(colormap_file):
with open(colormap_file) as f:
data = json.loads(f.read())
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
cm.load(colormap_file)
viscm = viscm_editor(
uniform_space=cm.uniform_space,
cmtype=cm.cmtype,
method=cm.method,
**cm.params,
)
assert viscm.name == data["name"]

extensions = data["extensions"]["https://matplotlib.org/viscm"]
xp, yp, fixed = viscm.control_point_model.get_control_points()

assert extensions["fixed"] == fixed
assert len(extensions["xp"]) == len(xp)
assert len(extensions["yp"]) == len(yp)
assert len(xp) == len(yp)
for i in range(len(xp)):
assert extensions["xp"][i] == xp[i]
assert extensions["yp"][i] == yp[i]
assert extensions["min_Jp"] == viscm.min_Jp
assert extensions["max_Jp"] == viscm.max_Jp
assert extensions["filter_k"] == viscm.cmap_model.filter_k
assert extensions["cmtype"] == viscm.cmtype

# Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of
# two-character (0-255) values) to 3-tuples of floats (0-1).
colors_hex = data["colors"]
colors_hex = [colors_hex[i : i + 6] for i in range(0, len(colors_hex), 6)]
colors = [
[int(c[i : i + 2], 16) / 255 for i in range(0, len(c), 2)] for c in colors_hex
]

editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()

for i in range(len(colors)):
for z in range(3):
# FIXME: The right-hand side of this comparison will always be 0.
# https://github.com/matplotlib/viscm/pull/66#discussion_r1213818015
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)
# Should the test look more like this?
# assert approxeq(colors[i][z], editor_colors[i][z], err=0.005)


# import matplotlib as mpl
# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
# try:
# from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
# except ImportError:
# try:
# from matplotlib.backends.backend_qt5agg import (
# FigureCanvasQTAgg as FigureCanvas
# )
# except ImportError:
# from matplotlib.backends.backend_qt4agg import (
# FigureCanvasQTAgg as FigureCanvas
# )
# from matplotlib.backends.qt_compat import QtCore, QtGui
#
# def test_editor_add_point():
Expand Down Expand Up @@ -144,7 +170,3 @@ def test_editor_loads_native():

# print(linear.control_point_model.get_control_points())
# # print(linear.cmap_model.get_Jpapbp(3))


def approxeq(x, y, err=0.0001):
return abs(y - x) < err