Skip to content

Commit 1fec42d

Browse files
committed
Lint and format with ruff and black
1 parent cf11834 commit 1fec42d

File tree

6 files changed

+655
-467
lines changed

6 files changed

+655
-467
lines changed

tests.py

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from viscm.gui import *
2-
from viscm.bezierbuilder import *
31
import numpy as np
4-
import matplotlib as mpl
5-
from matplotlib.backends.qt_compat import QtGui, QtCore
6-
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
72

8-
cms = {"viscm/examples/sample_linear.jscm",
9-
"viscm/examples/sample_diverging.jscm",
10-
"viscm/examples/sample_diverging_continuous.jscm"}
3+
from viscm.bezierbuilder import json
4+
from viscm.gui import Colormap, viscm_editor
5+
6+
cms = {
7+
"viscm/examples/sample_linear.jscm",
8+
"viscm/examples/sample_diverging.jscm",
9+
"viscm/examples/sample_diverging_continuous.jscm",
10+
}
1111

1212

1313
def test_editor_loads_native():
@@ -16,8 +16,13 @@ def test_editor_loads_native():
1616
data = json.loads(f.read())
1717
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
1818
cm.load(k)
19-
viscm = viscm_editor(uniform_space=cm.uniform_space, cmtype=cm.cmtype, method=cm.method, **cm.params)
20-
assert viscm.name == data["name"]
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"]
2126

2227
extensions = data["extensions"]["https://matplotlib.org/viscm"]
2328
xp, yp, fixed = viscm.control_point_model.get_control_points()
@@ -26,7 +31,7 @@ def test_editor_loads_native():
2631
assert len(extensions["xp"]) == len(xp)
2732
assert len(extensions["yp"]) == len(yp)
2833
assert len(xp) == len(yp)
29-
for i in range(len(xp)):
34+
for i in range(len(xp)):
3035
assert extensions["xp"][i] == xp[i]
3136
assert extensions["yp"][i] == yp[i]
3237
assert extensions["min_Jp"] == viscm.min_Jp
@@ -35,19 +40,34 @@ def test_editor_loads_native():
3540
assert extensions["cmtype"] == viscm.cmtype
3641

3742
colors = data["colors"]
38-
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)]]
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+
]
3947
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
4048
for i in range(len(colors)):
4149
for z in range(3):
4250
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)
4351

52+
53+
# import matplotlib as mpl
54+
# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
55+
# from matplotlib.backends.qt_compat import QtCore, QtGui
56+
#
4457
# def test_editor_add_point():
4558
# # Testing linear
46-
59+
#
4760
# fig = plt.figure()
4861
# figure_canvas = FigureCanvas(fig)
49-
# linear = viscm_editor(min_Jp=40, max_Jp=60, xp=[-10, 10], yp=[0,0], figure=fig, cmtype="linear")
50-
62+
# linear = viscm_editor(
63+
# min_Jp=40,
64+
# max_Jp=60,
65+
# xp=[-10, 10],
66+
# yp=[0,0],
67+
# figure=fig,
68+
# cmtype="linear",
69+
# )
70+
#
5171
# Jp, ap, bp = linear.cmap_model.get_Jpapbp(3)
5272
# eJp, eap, ebp = [40, 50, 60], [-10, 0, 10], [0, 0, 0]
5373
# for i in range(3):
@@ -61,12 +81,24 @@ def test_editor_loads_native():
6181
# for i in range(3):
6282
# for z in range(3):
6383
# assert approxeq(rgb[i][z], ergb[i][z])
64-
84+
6585

6686
# # Testing adding a point to linear
6787
# linear.bezier_builder.mode = "add"
68-
# qtEvent = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, QtCore.QPoint(), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.ShiftModifier)
69-
# event = mpl.backend_bases.MouseEvent("button_press_event", figure_canvas, 0, 10, guiEvent=qtEvent)
88+
# qtEvent = QtGui.QMouseEvent(
89+
# QtCore.QEvent.MouseButtonPress,
90+
# QtCore.QPoint(),
91+
# QtCore.Qt.LeftButton,
92+
# QtCore.Qt.LeftButton,
93+
# QtCore.Qt.ShiftModifier,
94+
# )
95+
# event = mpl.backend_bases.MouseEvent(
96+
# "button_press_event",
97+
# figure_canvas,
98+
# 0,
99+
# 10,
100+
# guiEvent=qtEvent,
101+
# )
70102
# event.xdata = 0
71103
# event.ydata = 10
72104
# event.inaxes = linear.bezier_builder.ax
@@ -87,8 +119,20 @@ def test_editor_loads_native():
87119

88120
# # Removing a point from linear
89121
# linear.bezier_builder.mode = "remove"
90-
# qtEvent = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, QtCore.QPoint(), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.ControlModifier)
91-
# event = mpl.backend_bases.MouseEvent("button_press_event", figure_canvas, 0, 10, guiEvent=qtEvent)
122+
# qtEvent = QtGui.QMouseEvent(
123+
# QtCore.QEvent.MouseButtonPress,
124+
# QtCore.QPoint(),
125+
# QtCore.Qt.LeftButton,
126+
# QtCore.Qt.LeftButton,
127+
# QtCore.Qt.ControlModifier,
128+
# )
129+
# event = mpl.backend_bases.MouseEvent(
130+
# "button_press_event",
131+
# figure_canvas,
132+
# 0,
133+
# 10,
134+
# guiEvent=qtEvent,
135+
# )
92136
# event.xdata = 0
93137
# event.ydata = 10
94138
# event.inaxes = linear.bezier_builder.ax
@@ -102,7 +146,5 @@ def test_editor_loads_native():
102146
# # print(linear.cmap_model.get_Jpapbp(3))
103147

104148

105-
106149
def approxeq(x, y, err=0.0001):
107150
return abs(y - x) < err
108-

viscm/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
# See file LICENSE.txt for license information.
55

66
from .cli import cli
7+
78
cli()

viscm/bezierbuilder.py

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf8
21
# BézierBuilder
32
#
43
# Copyright (c) 2013, Juan Luis Cano Rodríguez <juanlu001@gmail.com>
@@ -32,17 +31,17 @@
3231
$ python bezier_builder.py
3332
3433
"""
35-
from __future__ import division, print_function, absolute_import
3634

37-
import numpy as np
3835
from math import factorial
39-
from scipy import signal
4036

37+
import numpy as np
38+
from matplotlib.backends.qt_compat import QtCore
4139
from matplotlib.lines import Line2D
42-
from matplotlib.backends.qt_compat import QtGui, QtCore
40+
4341
from .minimvc import Trigger
4442

45-
class ControlPointModel(object):
43+
44+
class ControlPointModel:
4645
def __init__(self, xp, yp, fixed=None):
4746
# fixed is either None (if no point is fixed) or and index of a fixed
4847
# point
@@ -84,22 +83,22 @@ def set_control_points(self, xp, yp, fixed=None):
8483
self.trigger.fire()
8584

8685

87-
class ControlPointBuilder(object):
86+
class ControlPointBuilder:
8887
def __init__(self, ax, control_point_model):
8988
self.ax = ax
9089
self.control_point_model = control_point_model
9190

9291
self.canvas = self.ax.figure.canvas
9392
xp, yp, _ = self.control_point_model.get_control_points()
94-
self.control_polygon = Line2D(xp, yp,
95-
ls="--", c="#666666", marker="x",
96-
mew=2, mec="#204a87")
93+
self.control_polygon = Line2D(
94+
xp, yp, ls="--", c="#666666", marker="x", mew=2, mec="#204a87"
95+
)
9796
self.ax.add_line(self.control_polygon)
9897

9998
# Event handler for mouse clicking
100-
self.canvas.mpl_connect('button_press_event', self.on_button_press)
101-
self.canvas.mpl_connect('button_release_event', self.on_button_release)
102-
self.canvas.mpl_connect('motion_notify_event', self.on_motion_notify)
99+
self.canvas.mpl_connect("button_press_event", self.on_button_press)
100+
self.canvas.mpl_connect("button_release_event", self.on_button_release)
101+
self.canvas.mpl_connect("motion_notify_event", self.on_motion_notify)
103102

104103
self._index = None # Active vertex
105104

@@ -109,7 +108,7 @@ def __init__(self, ax, control_point_model):
109108

110109
def on_button_press(self, event):
111110
modkey = event.guiEvent.modifiers()
112-
# Ignore clicks outside axes
111+
# Ignore clicks outside axes
113112
if event.inaxes != self.ax:
114113
return
115114
res, ind = self.control_polygon.contains(event)
@@ -118,8 +117,7 @@ def on_button_press(self, event):
118117
if res and (modkey == QtCore.Qt.ControlModifier or self.mode == "remove"):
119118
# Control-click deletes
120119
self.control_point_model.remove_point(ind["ind"][0])
121-
if (modkey == QtCore.Qt.ShiftModifier or self.mode == "add"):
122-
120+
if modkey == QtCore.Qt.ShiftModifier or self.mode == "add":
123121
# Adding a new point. Find the two closest points and insert it in
124122
# between them.
125123
total_squared_dists = []
@@ -132,10 +130,7 @@ def on_button_press(self, event):
132130
total_squared_dists.append(dist)
133131
best = np.argmin(total_squared_dists)
134132

135-
self.control_point_model.add_point(best + 1,
136-
event.xdata,
137-
event.ydata)
138-
133+
self.control_point_model.add_point(best + 1, event.xdata, event.ydata)
139134

140135
def on_button_release(self, event):
141136
if event.button != 1:
@@ -170,7 +165,7 @@ def compute_bezier_points(xp, yp, at, method, grid=256):
170165
# arclength(t), and then invert it.
171166
t = np.linspace(0, 1, grid)
172167

173-
arclength = compute_arc_length(xp, yp, method, t=t)
168+
arclength = compute_arc_length(xp, yp, method, t=t)
174169
arclength /= arclength[-1]
175170
# Now (t, arclength) is a lookup table describing the t -> arclength
176171
# mapping. Invert it to get at -> t
@@ -181,6 +176,7 @@ def compute_bezier_points(xp, yp, at, method, grid=256):
181176

182177
return method(list(zip(xp, yp)), at_t).T
183178

179+
184180
def compute_arc_length(xp, yp, method, t=None, grid=256):
185181
if t is None:
186182
t = np.linspace(0, 1, grid)
@@ -194,7 +190,8 @@ def compute_arc_length(xp, yp, method, t=None, grid=256):
194190
np.hypot(x_deltas, y_deltas, out=arclength_deltas[1:])
195191
return np.cumsum(arclength_deltas)
196192

197-
class SingleBezierCurveModel(object):
193+
194+
class SingleBezierCurveModel:
198195
def __init__(self, control_point_model, method="CatmulClark"):
199196
self.method = eval(method)
200197
self.control_point_model = control_point_model
@@ -216,15 +213,14 @@ def _refresh(self):
216213
# self.canvas.draw()
217214

218215

219-
class TwoBezierCurveModel(object):
216+
class TwoBezierCurveModel:
220217
def __init__(self, control_point_model, method="CatmulClark"):
221218
self.method = eval(method)
222219
self.control_point_model = control_point_model
223220
x, y = self.get_bezier_points()
224221
self.bezier_curve = Line2D(x, y)
225222
self.trigger = self.control_point_model.trigger
226223
self.trigger.add_callback(self._refresh)
227-
228224

229225
def get_bezier_points(self, num=200):
230226
return self.get_bezier_points_at(np.linspace(0, 1, num))
@@ -233,15 +229,15 @@ def get_bezier_points_at(self, at, grid=256):
233229
at = np.asarray(at)
234230
if at.ndim == 0:
235231
at = np.array([at])
236-
237-
low_mask = (at < 0.5)
238-
high_mask = (at >= 0.5)
232+
233+
low_mask = at < 0.5
234+
high_mask = at >= 0.5
239235

240236
xp, yp, fixed = self.control_point_model.get_control_points()
241237
assert fixed is not None
242238

243-
low_xp = xp[:fixed + 1]
244-
low_yp = yp[:fixed + 1]
239+
low_xp = xp[: fixed + 1]
240+
low_yp = yp[: fixed + 1]
245241
high_xp = xp[fixed:]
246242
high_yp = yp[fixed:]
247243

@@ -257,21 +253,23 @@ def get_bezier_points_at(self, at, grid=256):
257253
low_at = (0.5 - (0.5 - low_at) * sf) * 2
258254
else:
259255
high_at = (0.5 + (high_at - 0.5) * sf) * 2 - 1
260-
low_at = low_at * 2
261-
262-
low_points = compute_bezier_points(low_xp, low_yp,
263-
low_at, self.method, grid=grid)
264-
high_points = compute_bezier_points(high_xp, high_yp,
265-
high_at, self.method, grid=grid)
266-
out = np.concatenate([low_points,high_points], 1)
256+
low_at = low_at * 2
257+
258+
low_points = compute_bezier_points(
259+
low_xp, low_yp, low_at, self.method, grid=grid
260+
)
261+
high_points = compute_bezier_points(
262+
high_xp, high_yp, high_at, self.method, grid=grid
263+
)
264+
out = np.concatenate([low_points, high_points], 1)
267265
return out
268266

269267
def _refresh(self):
270268
x, y = self.get_bezier_points()
271269
self.bezier_curve.set_data(x, y)
272270

273271

274-
class BezierCurveView(object):
272+
class BezierCurveView:
275273
def __init__(self, ax, bezier_curve_model):
276274
self.ax = ax
277275
self.bezier_curve_model = bezier_curve_model
@@ -291,19 +289,18 @@ def _refresh(self):
291289

292290

293291
# We used to use scipy.special.binom here,
294-
# but reimplementing it ourself lets us avoid pulling in a dependency
292+
# but reimplementing it ourself lets us avoid pulling in a dependency
295293
# scipy just for that one function.
296294
def binom(n, k):
297295
return factorial(n) * 1.0 / (factorial(k) * factorial(n - k))
298296

299-
def Bernstein(n, k):
300-
"""Bernstein polynomial.
301297

302-
"""
298+
def Bernstein(n, k):
299+
"""Bernstein polynomial."""
303300
coeff = binom(n, k)
304301

305302
def _bpoly(x):
306-
return coeff * x ** k * (1 - x) ** (n - k)
303+
return coeff * x**k * (1 - x) ** (n - k)
307304

308305
return _bpoly
309306

@@ -318,7 +315,8 @@ def Bezier(points, at):
318315
curve = np.zeros((at_flat.shape[0], 2))
319316
for ii in range(N):
320317
curve += np.outer(Bernstein(N - 1, ii)(at_flat), points[ii])
321-
return curve.reshape(at.shape + (2,))
318+
return curve.reshape((*at.shape, 2))
319+
322320

323321
def CatmulClark(points, at):
324322
points = np.asarray(points)
@@ -327,19 +325,10 @@ def CatmulClark(points, at):
327325
new_p = np.zeros((2 * len(points), 2))
328326
new_p[0] = points[0]
329327
new_p[-1] = points[-1]
330-
new_p[1:-2:2] = 3/4. * points[:-1] + 1/4. * points[1:]
331-
new_p[2:-1:2] = 1/4. * points[:-1] + 3/4. * points[1:]
328+
new_p[1:-2:2] = 3 / 4.0 * points[:-1] + 1 / 4.0 * points[1:]
329+
new_p[2:-1:2] = 1 / 4.0 * points[:-1] + 3 / 4.0 * points[1:]
332330
points = new_p
333331
xp, yp = zip(*points)
334332
xp = np.interp(at, np.linspace(0, 1, len(xp)), xp)
335333
yp = np.interp(at, np.linspace(0, 1, len(yp)), yp)
336334
return np.asarray(list(zip(xp, yp)))
337-
338-
339-
340-
341-
342-
343-
344-
345-

0 commit comments

Comments
 (0)