Skip to content

Commit 7824875

Browse files
author
Chahan Kropf
committed
Update test to use crs as input
1 parent 15c5d0e commit 7824875

File tree

1 file changed

+11
-78
lines changed

1 file changed

+11
-78
lines changed

climada/util/test/test_coordinates.py

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ def normal_warning(self, dist):
12311231
centroids, exposures = self.data_input_values()
12321232

12331233
# Interpolate with lower threshold to raise warnings
1234-
threshold = 40 / EARTH_RADIUS_KM
1234+
threshold = 0.361
12351235
with self.assertLogs("climada.util.coordinates", level="INFO") as cm:
12361236
neighbors = u_coord.match_coordinates(
12371237
exposures, centroids, distance=dist, threshold=threshold
@@ -1264,7 +1264,7 @@ def distance_threshold_warning_antimeridian(self, dist):
12641264
centroids, exposures = self.data_antimeridian_values()
12651265

12661266
# Interpolate with lower threshold to raise warnings
1267-
threshold = 100 / EARTH_RADIUS_KM
1267+
threshold = 0.1
12681268
with self.assertLogs("climada.util.coordinates", level="INFO") as cm:
12691269
neighbors = u_coord.match_coordinates(
12701270
exposures, centroids, distance=dist, threshold=threshold
@@ -1354,48 +1354,33 @@ def setUp_match_coordinates(self):
13541354
# Set up mock data for tests
13551355
# note that the base coordinates are in lat/lon
13561356
self.coords = np.array(
1357-
[(0.2, 2), (0, 0), (0, 2), (2.1, 3), (1, 1), (-1, 1), (0, 179.9)]
1357+
[(0.2, 2), (0, 0), (0, 2), (2.1, 3), (0.95, 1), (-1, 1), (0, 179.9)]
13581358
)
13591359
self.coords_to_assign = np.array(
13601360
[(2.1, 3), (0, 0), (0, 2), (0.9, 1.0), (0, -179.9)]
13611361
)
13621362
# test with different unit
1363-
self.expected_results = {
1364-
"degree": [
1365-
# test with different thresholds (in degree (converted to km))
1366-
(2, [2, 1, 2, 0, 3, 1, 4]),
1367-
(1.5, [-1, 1, 2, 0, 3, -1, -1]),
1368-
(0, [-1, 1, 2, 0, -1, -1, -1]),
1369-
],
1370-
"m": [
1371-
# test with different thresholds (in m)
1372-
(100e3, [2, 1, 2, 0, 3, -1, -1]),
1373-
(20e3, [-1, 1, 2, 0, 3, -1, -1]),
1374-
(0, [-1, 1, 2, 0, -1, -1, -1]),
1375-
],
1376-
"km": [
1377-
# test with different thresholds (in km)
1378-
(100, [2, 1, 2, 0, 3, -1, -1]),
1379-
(20, [-1, 1, 2, 0, 3, -1, -1]),
1380-
(0, [-1, 1, 2, 0, -1, -1, -1]),
1381-
],
1382-
}
1363+
self.expected_results = [
1364+
# test with different thresholds (in degree)
1365+
(2, [2, 1, 2, 0, 3, 1, 4]),
1366+
(0.1, [-1, 1, 2, 0, 3, -1, -1]),
1367+
(0, [-1, 1, 2, 0, -1, -1, -1]),
1368+
]
13831369

13841370
def test_match_coordinates(self):
13851371
"""Test match_coordinates function"""
13861372
self.setUp_match_coordinates()
1387-
unit = "degree" # first only check for degrees
13881373
for distance in ["euclidean", "haversine", "approx"]:
13891374
# make sure that it works for both float32 and float64
13901375
for test_dtype in [np.float64, np.float32]:
13911376
coords_typed = self.coords.astype(test_dtype)
13921377
coords_to_assign_typed = self.coords_to_assign.astype(test_dtype)
1393-
for thresh, result in self.expected_results[unit]:
1378+
for thresh, result in self.expected_results:
13941379
assigned_idx = u_coord.match_coordinates(
13951380
coords_typed,
13961381
coords_to_assign_typed,
13971382
distance=distance,
1398-
unit=unit,
1383+
crs=DEF_CRS,
13991384
threshold=thresh,
14001385
)
14011386
np.testing.assert_array_equal(assigned_idx, result)
@@ -1416,58 +1401,6 @@ def test_match_coordinates(self):
14161401
)
14171402
np.testing.assert_array_equal(assigned_idx, result)
14181403

1419-
def test_match_coordinates_different_unit(self):
1420-
"""Test match_coordinates function in cases of different units"""
1421-
self.setUp_match_coordinates()
1422-
unit_conv_funcs = {
1423-
"degree": lambda x: x,
1424-
"km": lambda x: np.deg2rad(x) * EARTH_RADIUS_KM,
1425-
"m": lambda x: np.deg2rad(x) * EARTH_RADIUS_KM * 1e3,
1426-
}
1427-
distance = "euclidean"
1428-
for unit in ["degree", "km", "m"]:
1429-
results = self.expected_results[unit]
1430-
# do not check antimeridian if units are not in degree
1431-
check_antimeridian = False if unit != "degree" else True
1432-
# make sure that it works for both float32 and float64
1433-
for test_dtype in [np.float64, np.float32]:
1434-
coords_typed = unit_conv_funcs[unit](self.coords.astype(test_dtype))
1435-
coords_to_assign_typed = unit_conv_funcs[unit](
1436-
self.coords_to_assign.astype(test_dtype)
1437-
)
1438-
for thresh, result in results:
1439-
assigned_idx = u_coord.match_coordinates(
1440-
coords_typed,
1441-
coords_to_assign_typed,
1442-
distance=distance,
1443-
unit=unit,
1444-
threshold=thresh,
1445-
check_antimeridian=check_antimeridian,
1446-
)
1447-
np.testing.assert_array_equal(assigned_idx, result)
1448-
# test empty coords_to_assign
1449-
coords_to_assign_empty = np.array([])
1450-
result = [-1, -1, -1, -1, -1, -1, -1]
1451-
assigned_idx = u_coord.match_coordinates(
1452-
self.coords,
1453-
coords_to_assign_empty,
1454-
distance=distance,
1455-
unit=unit,
1456-
threshold=thresh,
1457-
)
1458-
np.testing.assert_array_equal(assigned_idx, result)
1459-
# test empty coords
1460-
coords_empty = np.array([])
1461-
result = np.array([])
1462-
assigned_idx = u_coord.match_coordinates(
1463-
coords_empty,
1464-
self.coords_to_assign,
1465-
distance=distance,
1466-
unit=unit,
1467-
threshold=thresh,
1468-
)
1469-
np.testing.assert_array_equal(assigned_idx, result)
1470-
14711404
def test_match_coordinates_invalid_unit(self):
14721405
"""Test match_coordinates with invalid unit"""
14731406
self.setUp_match_coordinates()

0 commit comments

Comments
 (0)