Skip to content

Commit bc0f0b8

Browse files
author
Chahan Kropf
committed
Improve docstrings
1 parent f3300b0 commit bc0f0b8

File tree

3 files changed

+79
-45
lines changed

3 files changed

+79
-45
lines changed

climada/engine/impact_calc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def impact(
122122
>>> haz = Hazard.from_hdf5(HAZ_DEMO_H5) # Set hazard
123123
>>> impfset = ImpactFuncSet.from_excel(ENT_TEMPLATE_XLS)
124124
>>> exp = Exposures(pd.read_excel(ENT_TEMPLATE_XLS))
125+
>>> #Adjust threshold and distance to centroids/exposure resolution
125126
>>> exp.assign_centroids(hazard, threshold=1.5, distance='haversine')
126127
>>> impcalc = ImpactCal(exp, impfset, haz)
127128
>>> imp = impcalc.impact(insured=True, assign_centroids=False)
@@ -131,7 +132,7 @@ def impact(
131132
--------
132133
apply_deductible_to_mat : apply deductible to impact matrix
133134
apply_cover_to_mat : apply cover to impact matrix
134-
climada.exposures.assign_centroids : assign centroids to exposures explicitly
135+
climada.entity.exposures.assign_centroids : assign centroids to exposures explicitly
135136
"""
136137
# TODO: consider refactoring, making use of Exposures.hazard_impf
137138
# check for compatibility of exposures and hazard type

climada/entity/exposures/base.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ def assign_centroids(
594594
threshold=None,
595595
overwrite=True,
596596
):
597-
"""Assign for each exposure coordinate closest hazard coordinate.
597+
"""Assign for each exposure coordinate the closest hazard coordinate.
598598
The Exposures ``gdf`` will be altered by this method. It will have an additional
599599
(or modified) column named ``centr_[hazard.HAZ_TYPE]`` after the call.
600600
@@ -606,13 +606,15 @@ def assign_centroids(
606606
Parameters
607607
----------
608608
hazard : Hazard
609-
Hazard to match (with raster or vector centroids).
609+
Hazard to match. The hazard centroids must be in the same coordinate
610+
reference system (crs) as the exposures.
610611
distance : str, optional
611-
Distance to use in case of vector centroids.
612+
Distance to use for matching exposures points to hazard centroids
612613
Possible values are "euclidean", "haversine" and "approx".
613614
Default: "euclidean"
614615
threshold : float
615-
If the distance (in the exposure's crs) to the nearest neighbor exceeds `threshold`,
616+
Assignement threshold in units of the exposure's crs.
617+
If the distance to the nearest neighbor exceeds `threshold`,
616618
the index `-1` is assigned.
617619
Set `threshold` to 0, to disable nearest neighbor matching and enforce
618620
exact matching.
@@ -625,6 +627,13 @@ def assign_centroids(
625627
--------
626628
climada.util.coordinates.match_coordinates:
627629
method to associate centroids to exposure points
630+
climada.util.coordinates.estimate_matching_threshold:
631+
method to calculate the default threshold
632+
climada.util.coordinates.km_to_degree:
633+
method to approximately convert kilometers to degrees
634+
climada.util.coordinates.degree_to_km:
635+
method to approximately convert degrees to kilometers
636+
628637
Notes
629638
-----
630639
For coordinates in lat/lon coordinates distances in degrees differ from
@@ -639,6 +648,11 @@ def assign_centroids(
639648
to systematically wrong assignements.
640649
- hazard centroids covering larger areas than exposures may lead
641650
to sub-optimal matching if the threshold is too large
651+
- projected crs often diverge at the anti-meridian and close points
652+
on either side will be at a large distance. For proper handling
653+
of the anti-meridian please use degree coordinates in EPSG:4326.
654+
This might be relevant for countries like the Fidji or the US that
655+
cross the anti-meridian.
642656
643657
Users are free to implement their own matching alrogithm and save the
644658
matching centroid index in the appropriate column ``centr_[hazard.HAZ_TYPE]``.

climada/util/coordinates.py

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ def estimate_matching_threshold(coords_to_assign):
11231123

11241124

11251125
def degree_to_km(degree):
1126-
"""
1126+
r"""
11271127
Convert an angle from degrees to kilometers.
11281128
11291129
This function converts a given angle in degrees to its equivalent distance in
@@ -1146,8 +1146,9 @@ def degree_to_km(degree):
11461146
11471147
Notes
11481148
-----
1149-
The conversion is based on the formula: $distance = angle_{radians} \\times R$,
1150-
where $R$ is the Earth's radius.
1149+
The conversion is based on the formula:
1150+
.. math:: $distance = angle_{radians} \\times R$
1151+
where R is the Earth's radius in km.
11511152
11521153
Examples
11531154
--------
@@ -1158,7 +1159,7 @@ def degree_to_km(degree):
11581159

11591160

11601161
def km_to_degree(km):
1161-
"""
1162+
r"""
11621163
Convert a distance from kilometers to degrees.
11631164
11641165
This function converts a given distance in kilometers on the Earth's surface
@@ -1181,8 +1182,9 @@ def km_to_degree(km):
11811182
11821183
Notes
11831184
-----
1184-
The conversion is based on the formula: $angle_{radians} = distance / R$,
1185-
where $R$ is the Earth's radius.
1185+
The conversion is based on the formula:
1186+
.. math::$angle_{radians} = distance / R$
1187+
where R is the Earth's radius in km.
11861188
11871189
Examples
11881190
--------
@@ -1225,10 +1227,12 @@ def match_coordinates(
12251227
"approx". Default: "euclidean"
12261228
crs : str or pyproj crs, optinal
12271229
Coordinate reference system (crs) of the input coordinates.
1228-
Default: EPSG:4326
1230+
Default: EPSG:4326 (degrees lat/lon)
12291231
threshold : float, optional
1232+
Assignement threshold in units of the provided crs.
12301233
If the distance to the nearest neighbor exceeds `threshold`, the index `-1` is assigned.
1231-
Set `threshold` to 0 to disable nearest neighbor matching. Default: 100 (km or crs.unit)
1234+
Set `threshold` to 0 to disable nearest neighbor matching.
1235+
Default: twice the highest resolution of the coords_to_assign
12321236
kwargs: dict, optional
12331237
Keyword arguments to be passed on to nearest-neighbor finding functions in case of
12341238
non-exact matching with the specified `distance`.
@@ -1251,13 +1255,21 @@ def match_coordinates(
12511255
The nearest neighbor matching works with lat/lon coordinates (in degrees), and projected coordinates
12521256
(e.g. meters, kilometers). However, projected coordinates, require "euclidean" distance to be used
12531257
for nearest neighbor matching. Furthermore, the handling of antimeridian crossing is disabled
1254-
(check_antimeridian=False) and a warning is raised when the user attempts to use this option with
1255-
a non-degree coordinates system.
1258+
(check_antimeridian=False).
12561259
12571260
Make sure that all coordinates are according to the same coordinate reference system. In case
12581261
of lat/lon coordinates, the "haversine" distance is able to correctly compute the distance
1259-
across the antimeridian. However, when exact matches are enforced with `threshold=0`, lat/lon
1262+
across the antimeridian. When exact matches are enforced with `threshold=0`, lat/lon
12601263
coordinates need to be given in the same longitudinal range (such as (-180, 180)).
1264+
1265+
See Also
1266+
--------
1267+
climada.util.coordinates.estimate_matching_threshold:
1268+
method to calculate the default threshold
1269+
climada.util.coordinates.km_to_degree:
1270+
method to approximately convert kilometers to degrees
1271+
climada.util.coordinates.degree_to_km:
1272+
method to approximately convert degrees to kilometers
12611273
"""
12621274
crs = PCRS.from_user_input(crs)
12631275

@@ -1335,8 +1347,14 @@ def match_centroids(
13351347
threshold=None,
13361348
):
13371349
"""Assign to each gdf coordinate point its closest centroids's coordinate.
1338-
If distances > threshold in points' distances, -1 is returned.
1339-
If centroids are in a raster and coordinate point is outside of it ``-1`` is assigned
1350+
If there is no exact match for some entry, an attempt is made to assign the geographically
1351+
nearest neighbor. If the distance to the nearest neighbor exceeds `threshold`, the index `-1`
1352+
is assigned.
1353+
1354+
Make sure that all coordinates are according to the same coordinate reference system (crs).
1355+
1356+
You can disable nearest neighbor matching by setting `threshold` to 0, in which case
1357+
only exactly matching coordinates are assigned to each other.
13401358
13411359
Parameters
13421360
----------
@@ -1352,41 +1370,42 @@ def match_centroids(
13521370
for geographic crs differs from the true distance.
13531371
Default: "euclidean"
13541372
threshold : float, optional
1355-
If the distance (in the unit of the crs otherwise) to the nearest
1356-
neighbor exceeds `threshold`, the index `-1` is assigned.
1357-
Set `threshold` to 0, to enforce exact coordinate matching only.
1358-
Default: 2 times the minimal grid spacing assuming centroids to ge
1359-
on a grid
1373+
Assignement threshold in units of the provided crs.
1374+
If the distance to the nearest neighbor exceeds `threshold`, the index `-1` is assigned.
1375+
Set `threshold` to 0 to disable nearest neighbor matching.
1376+
Default: twice the highest resolution of the coords_to_assign
13601377
13611378
See Also
13621379
--------
1363-
climada.util.coordinates.match_grid_points: method to associate centroids to
1364-
coordinate points when centroids is a raster
1365-
climada.util.coordinates.match_coordinates: method to associate centroids to
1366-
coordinate points
1380+
climada.util.coordinates.match_coordinates: method to match coordinates
13671381
climada.util.coordinates.estimate_matching_threshold: method to estimate the
13681382
default threshold
1383+
climada.util.coordinates.km_to_degree:
1384+
method to approximately convert kilometers to degrees
1385+
climada.util.coordinates.degree_to_km:
1386+
method to approximately convert degrees to kilometers
1387+
climada.util.coordinates.match_grid_points: method to associate centroids to
1388+
coordinate points when centroids is a raster
13691389
13701390
Notes
13711391
-----
1372-
The default order of use is:
1373-
1374-
1. if centroid raster is defined, assign the closest raster point
1375-
to each gdf coordinate point.
1376-
2. if no raster, assign centroids to the nearest neighbor using
1377-
euclidian metric
1378-
1379-
Both cases can introduce innacuracies for coordinates in lat/lon
1380-
coordinates as distances in degrees differ from distances in meters
1381-
on the Earth surface, in particular for higher latitude and distances
1382-
larger than 100km. If more accuracy is needed, please use 'haversine'
1383-
distance metric. This however is slower for (quasi-)gridded data,
1384-
and works only for non-gridded data.
1385-
1386-
Projected coordinates (defined in units other than degrees) are supported.
1387-
However, nearest neighbor matching for projected coordinates is only possible
1388-
using 'euclidean' distance, and handling of coordinates crossing the antimeridian
1389-
is not implemented.
1392+
For coordinates in lat/lon coordinates distances in degrees differ from
1393+
distances in meters on the Earth surface, in particular for higher
1394+
latitude and distances larger than 100km. If more accuracy for degree
1395+
coordinates is needed, please use 'haversine' distance metric,
1396+
which however is slower.
1397+
1398+
Caution: nearest neighbourg matching can introduce serious artefacts
1399+
such as:
1400+
- coordinates centroids with shifted grids can lead
1401+
to systematically wrong assignements.
1402+
- centroids covering larger areas than coordinates may lead
1403+
to sub-optimal matching if the threshold is too large
1404+
- projected crs often diverge at the anti-meridian and close points
1405+
on either side will be at a large distance. For proper handling
1406+
of the anti-meridian please use degree coordinates in EPSG:4326.
1407+
This might be relevant for countries like the Fidji or the US that
1408+
cross the anti-meridian.
13901409
"""
13911410

13921411
try:

0 commit comments

Comments
 (0)