Skip to content

Commit 66bebe7

Browse files
committed
add docstrings and assertions
1 parent e859939 commit 66bebe7

4 files changed

Lines changed: 36 additions & 11 deletions

File tree

.idea/dictionaries/haroldmartin.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphviz2drawio/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def main():
1111

1212
client = Client(
1313
dsn="https://f1fce21b51864819a26ea116ff4e5b7f:[email protected]/1266157",
14-
release=__version__
14+
release=__version__,
1515
)
1616
except BaseException:
1717
pass

graphviz2drawio/mx/Curve.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,57 @@
11
from . import LinearRegression
22

3+
linear_min_r2 = 0.9
4+
35

46
class Curve:
57
def __init__(self, start, end, cb):
8+
"""Takes complex numbers for start, end, and list of 4 Bezier control points"""
69
super(Curve, self).__init__()
710
self.start = start
811
self.end = end
12+
assert cb is None or len(cb) == 4
913
self.cb = cb
1014

1115
def __str__(self):
1216
control = "[" + (str(self.cb) if self.cb is not None else "None") + "]"
1317
return str(self.start) + ", " + control + ", " + str(self.end)
1418

1519
@staticmethod
16-
def is_linear(points):
20+
def is_linear(points, threshold=linear_min_r2):
21+
"""
22+
Returns a boolean indicating whether a list of complex points is linear.
23+
24+
Takes a list of complex points and optional minimum R**2 threshold for linear regression.
25+
"""
1726
r2 = LinearRegression.coefficients(points)[2]
18-
return r2 > 0.9
27+
return r2 > threshold
1928

2029
def cubic_bezier_coordinates(self, t):
21-
x = Curve.cubic_bezier(
22-
[self.cb[0].real, self.cb[1].real, self.cb[2].real, self.cb[3].real], t
23-
)
24-
y = Curve.cubic_bezier(
25-
[self.cb[0].imag, self.cb[1].imag, self.cb[2].imag, self.cb[3].imag], t
26-
)
30+
"""
31+
Returns a complex number representing the point along the cubic bezier curve.
32+
33+
Takes parametric parameter t where 0 <= t <= 1
34+
"""
35+
x = Curve._cubic_bezier(self._cb("real"), t)
36+
y = Curve._cubic_bezier(self._cb("imag"), t)
2737
return complex(x, y)
2838

39+
def _cb(self, prop):
40+
return [getattr(x, prop) for x in self.cb]
41+
2942
@staticmethod
30-
def cubic_bezier(p, t):
43+
def _cubic_bezier(p, t):
44+
"""
45+
Returns a float representing the point along the cubic bezier curve in the given dimension.
46+
47+
Takes ordered list of 4 control points [P0, P1, P2, P3] and parametric parameter t where 0 <= t <= 1
48+
49+
implements explicit form of https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B%C3%A9zier_curves
50+
"""
51+
assert 0 <= t <= 1
3152
return (
3253
(((1.0 - t) ** 3) * p[0])
33-
+ (3 * t * ((1.0 - t) ** 2) * p[1])
54+
+ (3.0 * t * ((1.0 - t) ** 2) * p[1])
3455
+ (3.0 * (t ** 2) * (1.0 - t) * p[2])
3556
+ ((t ** 3) * p[3])
3657
)

graphviz2drawio/mx/LinearRegression.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ def variance(points, m):
77

88

99
def covariance(points, m):
10+
"""Returns the covariance of a list of complex points"""
1011
covar = 0.0
1112
for point in points:
1213
covar += (point.real - m.real) * (point.imag - m.imag)
1314
return covar
1415

1516

1617
def determination(points, y_mean, b0, b1):
18+
"""Returns the coefficient of determination (R**2) for linear equation and list of complex points"""
1719
sse = []
1820
sst = []
1921
for point in points:
@@ -29,6 +31,7 @@ def determination(points, y_mean, b0, b1):
2931

3032

3133
def coefficients(points):
34+
"""Returns the coefficents of a linear equation and R**2 as a list for a list of complex points"""
3235
x = [point.real for point in points]
3336
m = mean(points)
3437
var = variance(x, m.real)

0 commit comments

Comments
 (0)