Skip to content

Commit 90602b1

Browse files
author
Chahan Kropf
committed
Replace default threshold by dynamic one and apply black
1 parent 5eb7753 commit 90602b1

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

climada/entity/exposures/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def assign_centroids(
591591
self,
592592
hazard,
593593
distance="euclidean",
594-
threshold=u_coord.NEAREST_NEIGHBOR_THRESHOLD,
594+
threshold=None,
595595
overwrite=True,
596596
):
597597
"""Assign for each exposure coordinate closest hazard coordinate.

climada/util/coordinates.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ def match_coordinates(
10661066
coords_to_assign,
10671067
distance="euclidean",
10681068
unit="degree",
1069-
threshold=NEAREST_NEIGHBOR_THRESHOLD,
1069+
threshold=None,
10701070
**kwargs,
10711071
):
10721072
"""To each coordinate in `coords`, assign a matching coordinate in `coords_to_assign`
@@ -1147,10 +1147,13 @@ def match_coordinates(
11471147
if np.array_equal(coords, coords_to_assign):
11481148
assigned_idx = np.arange(coords.shape[0])
11491149
else:
1150+
if threshold is None:
1151+
threshold = 2 * max(abs(r) for r in get_resolution(coords_to_assign.T))
11501152
LOGGER.info(
11511153
"No exact centroid match found. Reprojecting coordinates "
1152-
"to nearest neighbor closer than the threshold = %s",
1154+
"to nearest neighbor closer than the threshold = %s %s",
11531155
threshold,
1156+
unit,
11541157
)
11551158
# pairs of floats can be sorted (lexicographically) in NumPy
11561159
coords_view = coords.view(dtype="float64,float64").reshape(-1)
@@ -1180,18 +1183,8 @@ def match_coordinates(
11801183
# convert to proper units before proceeding to nearest neighbor search
11811184
if unit == "degree":
11821185
# check that coords are indeed geographic before converting
1183-
11841186
check_if_geo_coords(coords[:, 0], coords[:, 1])
11851187
check_if_geo_coords(coords_to_assign[:, 0], coords_to_assign[:, 1])
1186-
elif unit != "km":
1187-
LOGGER.warning(
1188-
"You are using coordinates systems defined in %s. "
1189-
"The following distance threshold will be used for coordinates "
1190-
"matching: %i %s. Please adapt the distance threshold if needed. ",
1191-
unit,
1192-
threshold,
1193-
unit,
1194-
)
11951188

11961189
not_assigned_idx_mask = assigned_idx == -1
11971190
assigned_idx[not_assigned_idx_mask] = nearest_neighbor_funcs[distance](
@@ -1208,7 +1201,7 @@ def match_centroids(
12081201
coord_gdf,
12091202
centroids,
12101203
distance="euclidean",
1211-
threshold=NEAREST_NEIGHBOR_THRESHOLD,
1204+
threshold=None,
12121205
):
12131206
"""Assign to each gdf coordinate point its closest centroids's coordinate.
12141207
If distances > threshold in points' distances, -1 is returned.
@@ -1352,7 +1345,7 @@ def _nearest_neighbor_approx(
13521345
min_idx = dist.argmin()
13531346
# Raise a warning if the minimum distance is greater than the
13541347
# threshold and set an unvalid index -1
1355-
if np.sqrt(dist.min()) * ONE_LAT_KM > threshold:
1348+
if np.sqrt(dist.min()) > threshold:
13561349
num_warn += 1
13571350
min_idx = -1
13581351

@@ -1422,7 +1415,7 @@ def _nearest_neighbor_haversine(centroids, coordinates, unit, threshold):
14221415

14231416
# Raise a warning if the minimum distance is greater than the
14241417
# threshold and set an unvalid index -1
1425-
dist = dist * EARTH_RADIUS_KM
1418+
dist = np.rad2deg(dist)
14261419
num_warn = np.sum(dist > threshold)
14271420
if num_warn:
14281421
LOGGER.warning(
@@ -1483,12 +1476,8 @@ def _nearest_neighbor_euclidean(
14831476
# convert back to degree for check antimeridian and convert distance
14841477
centroids = np.rad2deg(centroids)
14851478
coordinates = np.rad2deg(coordinates)
1486-
dist = dist * EARTH_RADIUS_KM
1487-
threshold_unit = "km"
1479+
dist = np.rad2deg(dist)
14881480
else:
1489-
threshold_unit = (
1490-
unit # the unit of the threshold is considered to be in input unit
1491-
)
14921481
if check_antimeridian:
14931482
# if unit is not in degree, check_antimeridian is forced to False
14941483
check_antimeridian = False
@@ -1503,9 +1492,9 @@ def _nearest_neighbor_euclidean(
15031492
num_warn = np.sum(dist > threshold)
15041493
if num_warn:
15051494
LOGGER.warning(
1506-
"Distance to closest centroid is greater than %i %s for %i coordinates.",
1495+
"Distance to closest centroid is greater than %f %s for %i coordinates.",
15071496
threshold,
1508-
threshold_unit,
1497+
unit,
15091498
num_warn,
15101499
)
15111500
assigned[dist > threshold] = -1
@@ -1531,7 +1520,7 @@ def _nearest_neighbor_antimeridian(centroids, coordinates, threshold, assigned):
15311520
First column contains latitude, second column contains longitude. Each
15321521
row is a geographic point
15331522
threshold : float
1534-
distance threshold in km over which no neighbor will be found. Those
1523+
distance threshold in degree over which no neighbor will be found. Those
15351524
are assigned with a -1 index
15361525
assigned : 1d array
15371526
coordinates that have assigned so far
@@ -1551,7 +1540,7 @@ def _nearest_neighbor_antimeridian(centroids, coordinates, threshold, assigned):
15511540
mid_lon = 0.5 * (lon_max + lon_min)
15521541
antimeridian = mid_lon + 180
15531542

1554-
thres_deg = np.degrees(threshold / EARTH_RADIUS_KM)
1543+
thres_deg = threshold
15551544
coord_strip_bool = coordinates[:, 1] + antimeridian < 1.5 * thres_deg
15561545
coord_strip_bool |= coordinates[:, 1] - antimeridian > -1.5 * thres_deg
15571546
if np.any(coord_strip_bool):

0 commit comments

Comments
 (0)