Skip to content

Commit 339bc85

Browse files
authored
Use clearer names in query signature (#18)
* DOC/API: rename transform kwarg Due to a conversation with someone at pydata the name "transform" was confused from "axes to data coordinate transform" to "data-aggregation-pipeline". This is fairly a "transform" but is not the transform that is meant. * API: make the query inputs positional only This way implementers can pick a shorter name than "coord_transform".
1 parent 3eccc70 commit 339bc85

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

data_prototype/containers.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import pandas as pd
99

1010

11-
class _Transform(Protocol):
11+
class _MatplotlibTransform(Protocol):
1212
def transform(self, verts):
1313
...
1414

15-
def __sub__(self, other) -> "_Transform":
15+
def __sub__(self, other) -> "_MatplotlibTransform":
1616
...
1717

1818

@@ -35,8 +35,9 @@ class DataContainer(Protocol):
3535
def query(
3636
self,
3737
# TODO 3D?!!
38-
transform: _Transform,
38+
coord_transform: _MatplotlibTransform,
3939
size: Tuple[int, int],
40+
/,
4041
) -> Tuple[Dict[str, Any], Union[str, int]]:
4142
"""
4243
Query the data container for data.
@@ -46,7 +47,7 @@ def query(
4647
4748
Parameters
4849
----------
49-
transform : matplotlib.transform.Transform
50+
coord_transform : matplotlib.transform.Transform
5051
Must go from axes fraction space -> data space
5152
5253
size : 2 integers
@@ -88,7 +89,7 @@ def __init__(self, **data):
8889

8990
def query(
9091
self,
91-
transform: _Transform,
92+
coord_transform: _MatplotlibTransform,
9293
size: Tuple[int, int],
9394
) -> Tuple[Dict[str, Any], Union[str, int]]:
9495
return dict(self._data), self._cache_key
@@ -113,7 +114,7 @@ def __init__(self, **shapes):
113114

114115
def query(
115116
self,
116-
transform: _Transform,
117+
coord_transform: _MatplotlibTransform,
117118
size: Tuple[int, int],
118119
) -> Tuple[Dict[str, Any], Union[str, int]]:
119120
return {k: np.random.randn(*d.shape) for k, d in self._desc.items()}, str(uuid.uuid4())
@@ -166,26 +167,26 @@ def _split(input_dict):
166167

167168
def query(
168169
self,
169-
transform: _Transform,
170+
coord_transform: _MatplotlibTransform,
170171
size: Tuple[int, int],
171172
) -> Tuple[Dict[str, Any], Union[str, int]]:
172173
# TODO find a better way to compute the hash key, this is not sentative to
173174
# scale changes, only limit changes
174-
data_bounds = tuple(transform.transform([[0, 0], [1, 1]]).flatten())
175+
data_bounds = tuple(coord_transform.transform([[0, 0], [1, 1]]).flatten())
175176
hash_key = hash((data_bounds, size))
176177
if hash_key in self._cache:
177178
return self._cache[hash_key], hash_key
178179

179180
xpix, ypix = size
180-
x_data, _ = transform.transform(
181+
x_data, _ = coord_transform.transform(
181182
np.vstack(
182183
[
183184
np.linspace(0, 1, int(xpix) * 2),
184185
np.zeros(int(xpix) * 2),
185186
]
186187
).T
187188
).T
188-
_, y_data = transform.transform(
189+
_, y_data = coord_transform.transform(
189190
np.vstack(
190191
[
191192
np.zeros(int(ypix) * 2),
@@ -218,11 +219,11 @@ def __init__(self, raw_data, num_bins: int):
218219

219220
def query(
220221
self,
221-
transform: _Transform,
222+
coord_transform: _MatplotlibTransform,
222223
size: Tuple[int, int],
223224
) -> Tuple[Dict[str, Any], Union[str, int]]:
224225
dmin, dmax = self._full_range
225-
xmin, ymin, xmax, ymax = transform.transform([[0, 0], [1, 1]]).flatten()
226+
xmin, ymin, xmax, ymax = coord_transform.transform([[0, 0], [1, 1]]).flatten()
226227

227228
xmin, xmax = np.clip([xmin, xmax], dmin, dmax)
228229
hash_key = hash((xmin, xmax))
@@ -266,7 +267,7 @@ def __init__(self, series: pd.Series, *, index_name: str, col_name: str):
266267

267268
def query(
268269
self,
269-
transform: _Transform,
270+
coord_transform: _MatplotlibTransform,
270271
size: Tuple[int, int],
271272
) -> Tuple[Dict[str, Any], Union[str, int]]:
272273
return {self._index_name: self._data.index.values, self._col_name: self._data.values}, self._hash_key
@@ -305,7 +306,7 @@ def __init__(
305306

306307
def query(
307308
self,
308-
transform: _Transform,
309+
coord_transform: _MatplotlibTransform,
309310
size: Tuple[int, int],
310311
) -> Tuple[Dict[str, Any], Union[str, int]]:
311312
ret = {}
@@ -328,10 +329,10 @@ def __init__(self, data: DataContainer, mapping: Dict[str, str]):
328329

329330
def query(
330331
self,
331-
transform: _Transform,
332+
coord_transform: _MatplotlibTransform,
332333
size: Tuple[int, int],
333334
) -> Tuple[Dict[str, Any], Union[str, int]]:
334-
base, cache_key = self._data.query(transform, size)
335+
base, cache_key = self._data.query(coord_transform, size)
335336
return {v: base[k] for k, v in self._mapping.items()}, cache_key
336337

337338
def describe(self):
@@ -346,13 +347,13 @@ def __init__(self, *data: DataContainer):
346347

347348
def query(
348349
self,
349-
transform: _Transform,
350+
coord_transform: _MatplotlibTransform,
350351
size: Tuple[int, int],
351352
) -> Tuple[Dict[str, Any], Union[str, int]]:
352353
cache_keys = []
353354
ret = {}
354355
for data in self._datas:
355-
base, cache_key = data.query(transform, size)
356+
base, cache_key = data.query(coord_transform, size)
356357
ret.update(base)
357358
cache_keys.append(cache_key)
358359
return ret, hash(tuple(cache_keys))
@@ -364,7 +365,7 @@ def describe(self):
364365
class WebServiceContainer:
365366
def query(
366367
self,
367-
transform: _Transform,
368+
coord_transform: _MatplotlibTransform,
368369
size: Tuple[int, int],
369370
) -> Tuple[Dict[str, Any], Union[str, int]]:
370371
def hit_some_database():

data_prototype/wrappers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from matplotlib.collections import LineCollection as _LineCollection
1515
from matplotlib.artist import Artist as _Artist
1616

17-
from data_prototype.containers import DataContainer, _Transform
17+
from data_prototype.containers import DataContainer, _MatplotlibTransform
1818

1919

2020
class _BBox(Protocol):
@@ -30,8 +30,8 @@ class _Axes(Protocol):
3030
xaxis: _Axis
3131
yaxis: _Axis
3232

33-
transData: _Transform
34-
transAxes: _Transform
33+
transData: _MatplotlibTransform
34+
transAxes: _MatplotlibTransform
3535

3636
def get_xlim(self) -> Tuple[float, float]:
3737
...

examples/animation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import matplotlib.pyplot as plt
1515
from matplotlib.animation import FuncAnimation
1616

17-
from data_prototype.containers import _Transform, Desc
17+
from data_prototype.containers import _MatplotlibTransform, Desc
1818

1919
from data_prototype.wrappers import LineWrapper, FormatedText
2020

@@ -34,7 +34,7 @@ def describe(self):
3434

3535
def query(
3636
self,
37-
transform: _Transform,
37+
coord_transformtransform: _MatplotlibTransform,
3838
size: Tuple[int, int],
3939
) -> Tuple[Dict[str, Any], Union[str, int]]:
4040
th = np.linspace(0, 2 * np.pi, self.N)

examples/subsample.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import numpy as np
2222

2323
from data_prototype.wrappers import ImageWrapper
24-
from data_prototype.containers import _Transform
24+
from data_prototype.containers import _MatplotlibTransform, Desc
2525

2626
from skimage.transform import downscale_local_mean
2727

@@ -45,10 +45,10 @@ def describe(self):
4545

4646
def query(
4747
self,
48-
transform: _Transform,
48+
coord_transform: _MatplotlibTransform,
4949
size: Tuple[int, int],
5050
) -> Tuple[Dict[str, Any], Union[str, int]]:
51-
(x1, y1), (x2, y2) = transform.transform([[0, 0], [1, 1]])
51+
(x1, y1), (x2, y2) = coord_transform.transform([[0, 0], [1, 1]])
5252

5353
xi1 = np.argmin(np.abs(x - x1))
5454
yi1 = np.argmin(np.abs(y - y1))

0 commit comments

Comments
 (0)