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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: test
test:
python -m pytest --version
python -m pytest test/
python -m pytest -v test/


.PHONY: lint
Expand Down
114 changes: 68 additions & 46 deletions test/test_editor_loads_native.py → test/test_editor_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,74 @@ def approxeq(x, y, *, err=0.0001):
"viscm/examples/sample_diverging_continuous.jscm",
],
)
@pytest.mark.xfail(reason="Test very old; intent unclear")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a test that ensures that the editor can load a .jscm data file and interpret its values correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactoring looks good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a test that ensures that the editor can load a .jscm data file and interpret its values correctly.

Got that; I'm referring to this assertion here when I say "intent unclear":

assert actual_colors[i][z] == np.rint(expected_colors[i][z] / 256)

Right now I'm not sure why this was ever working :)

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)
class TestEditorLoad:
def expected(self, colormap_file):
with open(colormap_file) as f:
exp = json.loads(f.read())
return exp

def actual(self, colormap_file):
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
cm.load(colormap_file)
act = viscm_editor(
uniform_space=cm.uniform_space,
cmtype=cm.cmtype,
method=cm.method,
**cm.params,
)
return act

def test_editor_loads_jscm_parameters_match(self, colormap_file):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives us a set of 3 passing tests which just check attributes are as expected, and 3 xfail tests which check actual values loaded from a jscm file.

expected = self.expected(colormap_file)
actual = self.actual(colormap_file)

assert actual.name == expected["name"]

extensions = expected["extensions"]["https://matplotlib.org/viscm"]
xp, yp, fixed = actual.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"] == actual.min_Jp
assert extensions["max_Jp"] == actual.max_Jp
assert extensions["filter_k"] == actual.cmap_model.filter_k
assert extensions["cmtype"] == actual.cmtype

@pytest.mark.xfail(reason="Test very old; intent unclear")
def test_editor_loads_jscm_data_match(self, colormap_file):
expected = self.expected(colormap_file)
actual = self.actual(colormap_file)

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

actual_colors = actual.cmap_model.get_sRGB(num=256)[0].tolist()

for i in range(len(expected_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 actual_colors[i][z] == np.rint(expected_colors[i][z] / 256)
# Should the test look more like this?
# assert approxeq(
# expected_colors[i][z],
# actual_colors[i][z],
# err=0.005,
# )


# import matplotlib as mpl
Expand Down