Skip to content

Commit 9842208

Browse files
committed
Fix augmenter regression
1 parent d3a60bc commit 9842208

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎src/damast/data_handling/transformers/augmenters.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ def update_balltree(self, x: npt.NDArray[np.float64]):
172172
def __call__(self, x: npt.NDArray[np.float64], y: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
173173
"""
174174
Compute distances between the Balltree and each entry in `x`"""
175+
# polars may pass empty batches, which BallTree.query rejects
176+
if len(x) == 0:
177+
return np.empty(0, dtype=np.float64)
175178
return self._tree.query(np.vstack([x, y]).T, return_distance=True)[
176179
0].reshape(-1)
177180

‎tests/damast/domains/maritime/ais/test_augmenters.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,28 @@ def test_add_distance_closest_anchorage(tmp_path):
292292
assert np.isclose(np.min(distances), closest_anchorages[idx])
293293

294294

295+
def test_add_distance_closest_anchorage_empty_input(tmp_path):
296+
dataset = polars.DataFrame({"latitude": [34.84, 35.14], "longitude": [128.42, 128.60]})
297+
df = polars.DataFrame({ColumnName.LATITUDE: [], ColumnName.LONGITUDE: []},
298+
schema={ColumnName.LATITUDE: pl.Float64, ColumnName.LONGITUDE: pl.Float64})
299+
metadata = damast.core.MetaData(
300+
columns=[damast.core.DataSpecification(ColumnName.LATITUDE, unit=units.deg,
301+
representation_type=float),
302+
damast.core.DataSpecification(ColumnName.LONGITUDE, unit=units.deg,
303+
representation_type=float)])
304+
adf = damast.core.AnnotatedDataFrame(df, metadata)
305+
306+
pipeline = damast.core.DataProcessingPipeline(name="Compute closest anchorage",
307+
base_dir=tmp_path)
308+
pipeline.add("Add distance to anchorage", ComputeClosestAnchorage(dataset, ["latitude", "longitude"]),
309+
name_mappings={"x": ColumnName.LATITUDE,
310+
"y": ColumnName.LONGITUDE,
311+
"distance": ColumnName.DISTANCE_CLOSEST_ANCHORAGE})
312+
new_adf = pipeline.transform(adf)
313+
314+
assert new_adf.collect()[ColumnName.DISTANCE_CLOSEST_ANCHORAGE].len() == 0
315+
316+
295317
def test_message_index(tmp_path):
296318
mmsi_a = 400000000
297319
mmsi_b = 500000000

0 commit comments

Comments
 (0)