Skip to content

Commit d995bb5

Browse files
committed
style: apply black formatting to coorx/linear.py
1 parent ad5472e commit d995bb5

File tree

1 file changed

+83
-45
lines changed

1 file changed

+83
-45
lines changed

coorx/linear.py

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def _imap(self, coords):
3939

4040
def as_affine(self):
4141
return AffineTransform(
42-
matrix=np.eye(self.dims[0]), offset=np.zeros(self.dims[0]), from_cs=self.systems[0], to_cs=self.systems[1]
42+
matrix=np.eye(self.dims[0]),
43+
offset=np.zeros(self.dims[0]),
44+
from_cs=self.systems[0],
45+
to_cs=self.systems[1],
4346
)
4447

4548
@property
@@ -117,7 +120,9 @@ def __rmul__(self, tr):
117120
if isinstance(tr, TransposeTransform):
118121
tr.validate_transform_for_mul(self)
119122
return TransposeTransform(
120-
axis_order=tuple(self._map(np.array(tr.axis_order))), from_cs=self.systems[0], to_cs=tr.systems[1]
123+
axis_order=tuple(self._map(np.array(tr.axis_order))),
124+
from_cs=self.systems[0],
125+
to_cs=tr.systems[1],
121126
)
122127
return super().__rmul__(tr)
123128

@@ -210,7 +215,9 @@ def offset(self):
210215
def offset(self, t):
211216
t = np.asarray(t)
212217
if t.shape != (self.dims[0],):
213-
raise TypeError("Offset must have length equal to transform dimensionality (%d)" % self.dims[0])
218+
raise TypeError(
219+
"Offset must have length equal to transform dimensionality (%d)" % self.dims[0]
220+
)
214221
if np.all(t == self._offset):
215222
return
216223

@@ -235,7 +242,10 @@ def as_affine(self):
235242

236243
def as_st(self):
237244
return STTransform(
238-
offset=self.offset, scale=(1,) * self.dims[0], from_cs=self.systems[0], to_cs=self.systems[1]
245+
offset=self.offset,
246+
scale=(1,) * self.dims[0],
247+
from_cs=self.systems[0],
248+
to_cs=self.systems[1],
239249
)
240250

241251
def __mul__(self, tr):
@@ -359,15 +369,19 @@ def set_params(self, scale=None, offset=None):
359369
if scale is not None:
360370
scale = np.asarray(scale)
361371
if scale.shape != (self.dims[0],):
362-
raise TypeError("Scale must have length equal to transform dimensionality (%d)" % self.dims[0])
372+
raise TypeError(
373+
"Scale must have length equal to transform dimensionality (%d)" % self.dims[0]
374+
)
363375
if not np.all(scale == self._scale):
364376
self._scale[:] = scale
365377
need_update = True
366378

367379
if offset is not None and not np.all(offset == self._offset):
368380
offset = np.asarray(offset)
369381
if offset.shape != (self.dims[0],):
370-
raise TypeError("Offset must have length equal to transform dimensionality (%d)" % self.dims[0])
382+
raise TypeError(
383+
"Offset must have length equal to transform dimensionality (%d)" % self.dims[0]
384+
)
371385
if not np.all(offset == self._offset):
372386
self._offset[:] = offset
373387
need_update = True
@@ -621,7 +635,10 @@ def set_params(self, matrix=None, offset=None):
621635
if offset is not None:
622636
o = np.asarray(offset)
623637
if o.ndim != 1 or len(o) != self.dims[1]:
624-
raise Exception("Offset length must be the same as transform output dimension (%d)" % self.dims[1])
638+
raise Exception(
639+
"Offset length must be the same as transform output dimension (%d)"
640+
% self.dims[1]
641+
)
625642
if np.any(o != self._offset):
626643
self._offset = o
627644
self._inv_matrix = None
@@ -642,7 +659,10 @@ def inv_offset(self):
642659

643660
def as_affine(self):
644661
return AffineTransform(
645-
matrix=self.matrix.copy(), offset=self.offset.copy(), from_cs=self.systems[0], to_cs=self.systems[1]
662+
matrix=self.matrix.copy(),
663+
offset=self.offset.copy(),
664+
from_cs=self.systems[0],
665+
to_cs=self.systems[1],
646666
)
647667

648668
@property
@@ -750,7 +770,9 @@ def __mul__(self, tr):
750770
self.validate_transform_for_mul(tr)
751771
if isinstance(tr, AffineTransform):
752772
m = np.dot(self.full_matrix, tr.full_matrix)
753-
return AffineTransform(matrix=m[:-1, :-1], offset=m[:-1, -1], from_cs=tr.systems[0], to_cs=self.systems[1])
773+
return AffineTransform(
774+
matrix=m[:-1, :-1], offset=m[:-1, -1], from_cs=tr.systems[0], to_cs=self.systems[1]
775+
)
754776
return tr.__rmul__(self)
755777

756778
def __eq__(self, tr):
@@ -770,13 +792,17 @@ def from_qmatrix4x4(cls, matrix, *init_args, **init_kwargs):
770792
# Create an AffineTransform from the matrix
771793
return cls(
772794
matrix=matrix_array[:-1, :-1], # 3x3 transformation matrix
773-
offset=matrix_array[:-1, -1], # 3-element translation vector
774-
*init_args, **init_kwargs
795+
offset=matrix_array[:-1, -1], # 3-element translation vector
796+
*init_args,
797+
**init_kwargs,
775798
)
776799

777800
def copy(self, from_cs=None, to_cs=None):
778801
return AffineTransform(
779-
matrix=self.matrix, offset=self.offset, from_cs=from_cs or self.systems[0], to_cs=to_cs or self.systems[1]
802+
matrix=self.matrix,
803+
offset=self.offset,
804+
from_cs=from_cs or self.systems[0],
805+
to_cs=to_cs or self.systems[1],
780806
)
781807

782808

@@ -807,7 +833,12 @@ def __init__(self, offset=None, scale=None, angle=None, axis=None, init=None, **
807833
kwds.setdefault("dims", (3, 3))
808834
super().__init__(**kwds)
809835
assert self.dims == (3, 3), "SRT3DTransform can only map 3D coordinates"
810-
self._state = {"offset": np.zeros(3), "scale": np.ones(3), "angle": 0, "axis": np.array([0.0, 0.0, 1.0])}
836+
self._state = {
837+
"offset": np.zeros(3),
838+
"scale": np.ones(3),
839+
"angle": 0,
840+
"axis": np.array([0.0, 0.0, 1.0]),
841+
}
811842
self._affine = None
812843
if all(p is None for p in (offset, scale, angle, axis)):
813844
if init is not None:
@@ -1001,8 +1032,15 @@ def set_mapping(self, points1, points2):
10011032
# else:
10021033
# params_flat.extend(v)
10031034
# param_len[k] = len(v)
1004-
params_flat = list(params["offset"]) + [params["scale"][0]] + list(params["axis"]) + [params["angle"]]
1005-
params_flat = list(params["offset"]) + list(params["scale"]) + list(params["axis"]) + [params["angle"]]
1035+
params_flat = (
1036+
list(params["offset"]) + [params["scale"][0]] + list(params["axis"]) + [params["angle"]]
1037+
)
1038+
params_flat = (
1039+
list(params["offset"])
1040+
+ list(params["scale"])
1041+
+ list(params["axis"])
1042+
+ [params["angle"]]
1043+
)
10061044
x0 = np.array(params_flat)
10071045

10081046
def unflatten_params(x):
@@ -1128,7 +1166,9 @@ def __init__(self, **kwds):
11281166
assert kwds["dims"] == (3, 3)
11291167
affine_params = kwds.pop("affine", {})
11301168
super().__init__(**kwds)
1131-
self.affine = AffineTransform(dims=(4, 4), **affine_params, from_cs=self.systems[0], to_cs=self.systems[1])
1169+
self.affine = AffineTransform(
1170+
dims=(4, 4), **affine_params, from_cs=self.systems[0], to_cs=self.systems[1]
1171+
)
11321172

11331173
def _map(self, arr):
11341174
arr4 = np.empty((arr.shape[0], 4), dtype=arr.dtype)
@@ -1193,9 +1233,8 @@ def set_params(self, affine=None):
11931233

11941234

11951235
class BilinearTransform(Transform):
1196-
"""2D bilinear transform.
1236+
"""2D bilinear transform."""
11971237

1198-
"""
11991238
state_keys = ["_matrix", "_inv_matrix"]
12001239

12011240
def __init__(self, **kwds):
@@ -1205,7 +1244,7 @@ def __init__(self, **kwds):
12051244

12061245
kwds.setdefault("dims", (2, 2))
12071246
assert kwds["dims"] == (2, 2)
1208-
super().__init__(**kwds)
1247+
super().__init__(**kwds)
12091248

12101249
def set_mapping(self, points1, points2):
12111250
"""Set to a transformation matrix that maps points1 onto points2.
@@ -1215,16 +1254,18 @@ def set_mapping(self, points1, points2):
12151254
# convert inputs to (4, 2) arrays
12161255
points1 = self._prepare_arg_for_mapping(points1)[0]
12171256
points2 = self._prepare_arg_for_mapping(points2)[0]
1218-
assert points1.shape == points2.shape == (4, 2), f"Input arrays must have shape (4, 2); got ({points1.shape}, {points2.shape})"
1219-
1257+
assert (
1258+
points1.shape == points2.shape == (4, 2)
1259+
), f"Input arrays must have shape (4, 2); got ({points1.shape}, {points2.shape})"
1260+
12201261
self._inv_matrix = self._solve_matrix(points2, points1)
12211262
self._matrix = self._solve_matrix(points1, points2)
12221263

12231264
@staticmethod
12241265
def _solve_matrix(a, b):
12251266
# solve 2 sets of linear equations to determine transformation matrix elements
12261267
c = BilinearTransform._prepare_for_mapping(a)
1227-
matrix = np.zeros((2,4))
1268+
matrix = np.zeros((2, 4))
12281269
for i in range(2):
12291270
# solve Ax = B; x is one row of the desired transformation matrix
12301271
matrix[i] = numpy.linalg.solve(c, b[:, i])
@@ -1244,35 +1285,34 @@ def _prepare_for_mapping(points):
12441285
@property
12451286
def matrix(self):
12461287
return self._matrix.copy()
1247-
1288+
12481289
@property
12491290
def inv_matrix(self):
12501291
return self._inv_matrix.copy()
12511292

1252-
def _map(self, arr):
1293+
def _map(self, arr):
12531294
arr4 = BilinearTransform._prepare_for_mapping(arr)
12541295
out = np.dot(arr4, self._matrix.T)
12551296
return out[:, :2]
12561297

1257-
def _imap(self, arr):
1298+
def _imap(self, arr):
12581299
arr4 = BilinearTransform._prepare_for_mapping(arr)
12591300
out = np.dot(arr4, self._inv_matrix.T)
12601301
return out[:, :2]
12611302

12621303
@property
12631304
def params(self):
12641305
return {"matrix": self.matrix, "inv_matrix": self.inv_matrix}
1265-
1306+
12661307
def set_params(self, matrix, inv_matrix):
12671308
self._matrix = matrix
12681309
self._inv_matrix = inv_matrix
12691310
self._update()
12701311

12711312

12721313
class Homography2DTransform(Transform):
1273-
"""2D homography transform.
1314+
"""2D homography transform."""
12741315

1275-
"""
12761316
state_keys = ["_matrix", "_inv_matrix"]
12771317

12781318
def __init__(self, **kwds):
@@ -1282,7 +1322,7 @@ def __init__(self, **kwds):
12821322

12831323
kwds.setdefault("dims", (2, 2))
12841324
assert kwds["dims"] == (2, 2)
1285-
super().__init__(**kwds)
1325+
super().__init__(**kwds)
12861326

12871327
def set_mapping(self, points1, points2):
12881328
"""Set to a transformation matrix that maps points1 onto points2.
@@ -1292,36 +1332,34 @@ def set_mapping(self, points1, points2):
12921332
# convert inputs to (4, 2) arrays
12931333
points1 = self._prepare_arg_for_mapping(points1)[0]
12941334
points2 = self._prepare_arg_for_mapping(points2)[0]
1295-
assert points1.shape == points2.shape == (4, 2), f"Input arrays must have shape (4, 2); got ({points1.shape}, {points2.shape})"
1296-
1335+
assert (
1336+
points1.shape == points2.shape == (4, 2)
1337+
), f"Input arrays must have shape (4, 2); got ({points1.shape}, {points2.shape})"
1338+
12971339
self._inv_matrix = self._solve_matrix(points2, points1)
12981340
self._matrix = self._solve_matrix(points1, points2)
12991341

13001342
@staticmethod
13011343
def _solve_matrix(a, b):
13021344
assert a.shape == (4, 2) and b.shape == (4, 2), "a and B must be of shape (4, 2)"
1303-
1345+
13041346
# Constructing the matrix to solve ah = b
13051347
a_matrix = []
13061348
b_matrix = []
13071349
for (x, y), (x_prime, y_prime) in zip(a, b):
13081350
a_matrix.append([x, y, 1, 0, 0, 0, -x * x_prime, -y * x_prime])
13091351
a_matrix.append([0, 0, 0, x, y, 1, -x * y_prime, -y * y_prime])
13101352
b_matrix.extend([x_prime, y_prime])
1311-
1353+
13121354
a_matrix = np.array(a_matrix)
13131355
b_matrix = np.array(b_matrix)
1314-
1356+
13151357
# Solving for h (the homography parameters)
13161358
h = np.linalg.lstsq(a_matrix, b_matrix, rcond=None)[0]
1317-
1359+
13181360
# Construct the homography matrix
1319-
H = np.array([
1320-
[h[0], h[1], h[2]],
1321-
[h[3], h[4], h[5]],
1322-
[h[6], h[7], 1]
1323-
])
1324-
1361+
H = np.array([[h[0], h[1], h[2]], [h[3], h[4], h[5]], [h[6], h[7], 1]])
1362+
13251363
return H
13261364

13271365
@staticmethod
@@ -1337,25 +1375,25 @@ def _prepare_for_mapping(points):
13371375
@property
13381376
def matrix(self):
13391377
return self._matrix.copy()
1340-
1378+
13411379
@property
13421380
def inv_matrix(self):
13431381
return self._inv_matrix.copy()
13441382

1345-
def _map(self, arr):
1383+
def _map(self, arr):
13461384
arr4 = self._prepare_for_mapping(arr)
13471385
out = np.dot(arr4, self._matrix.T)
13481386
return out[:, :2] / out[:, 2:3]
13491387

1350-
def _imap(self, arr):
1388+
def _imap(self, arr):
13511389
arr4 = self._prepare_for_mapping(arr)
13521390
out = np.dot(arr4, self._inv_matrix.T)
13531391
return out[:, :2] / out[:, 2:3]
13541392

13551393
@property
13561394
def params(self):
13571395
return {"matrix": self.matrix, "inv_matrix": self.inv_matrix}
1358-
1396+
13591397
def set_params(self, matrix, inv_matrix):
13601398
self._matrix = matrix
13611399
self._inv_matrix = inv_matrix

0 commit comments

Comments
 (0)