Skip to content

Commit 1776f37

Browse files
committed
fixed geodetic distances so that floats are always passed
1 parent 8b1dbac commit 1776f37

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

openquake/hme/model_test_frameworks/gem/gem_test_functions.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,21 @@ def get_distances(eq, rup_gdf):
280280
"""
281281
# this assumes we want 3d distance instead of separate treatment
282282
# of h, v dists
283+
# `openquake.hazardlib.geo.geodetic.distance` expects NumPy numeric
284+
# arrays. Pandas Series can arrive as object-typed inputs on newer
285+
# openquake/numba combinations, which then fail inside the JIT dispatcher.
286+
# Converting explicitly keeps the call stable across engine versions.
287+
rup_lons = np.asarray(rup_gdf["longitude"], dtype=np.float64)
288+
rup_lats = np.asarray(rup_gdf["latitude"], dtype=np.float64)
289+
rup_depths = np.asarray(rup_gdf["depth"], dtype=np.float64)
290+
283291
dists = distance(
284292
eq.longitude,
285293
eq.latitude,
286294
eq.depth,
287-
rup_gdf["longitude"],
288-
rup_gdf["latitude"],
289-
rup_gdf["depth"],
295+
rup_lons,
296+
rup_lats,
297+
rup_depths,
290298
)
291299
return dists
292300

openquake/hme/model_test_frameworks/gem/unit_tests/test_gem_test_functions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from unittest.mock import patch
23

34
import numpy as np
45
import pandas as pd
@@ -336,6 +337,36 @@ def test_get_distances(self):
336337

337338
np.testing.assert_allclose(distances, distances_expected)
338339

340+
def test_get_distances_coerces_rupture_coordinates(self):
341+
eq = self.eq_gdf.iloc[0]
342+
343+
rups = self.rupture_gdf.iloc[:3].copy()
344+
for col in ["longitude", "latitude", "depth"]:
345+
rups[col] = rups[col].astype(object)
346+
347+
def fake_distance(lon1, lat1, depth1, lons2, lats2, depths2):
348+
assert np.isscalar(lon1)
349+
assert np.isscalar(lat1)
350+
assert np.isscalar(depth1)
351+
assert isinstance(lons2, np.ndarray)
352+
assert isinstance(lats2, np.ndarray)
353+
assert isinstance(depths2, np.ndarray)
354+
assert lons2.dtype == np.float64
355+
assert lats2.dtype == np.float64
356+
assert depths2.dtype == np.float64
357+
return np.array([50.22082, 48.373561, 46.53774], dtype=np.float64)
358+
359+
with patch(
360+
"openquake.hme.model_test_frameworks.gem.gem_test_functions.distance",
361+
side_effect=fake_distance,
362+
):
363+
distances = get_distances(eq, rups)
364+
365+
np.testing.assert_allclose(
366+
distances,
367+
np.array([50.22082, 48.373561, 46.53774], dtype=np.float64),
368+
)
369+
339370
def test_get_rups_in_mag_range(self):
340371
eq = self.eq_gdf.loc[8]
341372
mag_range = 1.0

0 commit comments

Comments
 (0)