Skip to content

Commit d0c8fb4

Browse files
committed
GeospatialScenario instance kdtree
1 parent c1883fb commit d0c8fb4

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

pvdeg/scenario.py

+46-23
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ def __init__(
11101110
self.hpc = hpc
11111111
self.func = func
11121112
self.template = template
1113+
self.kdtree = None # sklearn kdtree
11131114

11141115
def __eq__(self, other):
11151116
raise NotImplementedError("""
@@ -1285,14 +1286,22 @@ def location_bounding_box(
12851286

12861287
self.meta_data = self.meta_data.loc[bbox_gids]
12871288

1289+
def set_kdtree(self, kdtree = None) -> None:
1290+
"""Initialize a kidtree and save it to the GeospatialScenario"""
1291+
if kdtree is None:
1292+
self.kdtree = pvdeg.geospatial.meta_KDtree(meta_df=self.meta_data)
1293+
else:
1294+
self.kdtree = kdtree
1295+
1296+
12881297
def classify_mountains_radii(
12891298
self,
1290-
kdtree,
12911299
rad_1: Union[float, int] = 12,
12921300
rad_2: Union[float, int] = 1,
12931301
threshold_factor: Union[float, int] = 1.25,
12941302
elevation_floor: Union[float, int] = 0,
12951303
bbox_kwarg: Optional[dict] = {},
1304+
kdtree = None,
12961305
):
12971306
"""
12981307
Find mountains from elevation metadata using sklearn kdtree for fast lookup.
@@ -1306,9 +1315,6 @@ def classify_mountains_radii(
13061315
-----------
13071316
meta_df : pd.DataFrame
13081317
Dataframe of metadata as generated by pvdeg.weather.get for geospatial
1309-
kdtree : sklearn.neighbors.KDTree
1310-
kdtree containing latitude-longitude pairs for quick lookups
1311-
Generate using ``pvdeg.geospatial.meta_KDTree``
13121318
rad_1 : float
13131319
radius of the larger search area whose elevations are compared against
13141320
the smaller search area. controls the kdtree query region.
@@ -1325,15 +1331,21 @@ def classify_mountains_radii(
13251331
elevation_floor : int
13261332
minimum inclusive elevation in meters. If a point has smaller location
13271333
it will be clipped from result.
1334+
kdtree : sklearn.neighbors.KDTree
1335+
Generated automatically but can be provided externally.
1336+
kdtree containing latitude-longitude pairs for quick lookups
1337+
Generate using ``pvdeg.geospatial.meta_KDTree``
13281338
13291339
Returns:
13301340
--------
13311341
None, strictly updates meta_data attribute of geospatial scenari instance.
13321342
"""
13331343

1344+
self.set_kdtree(kdtree=kdtree)
1345+
13341346
gids = pvdeg.geospatial.identify_mountains_radii(
13351347
meta_df=self.meta_data,
1336-
kdtree=kdtree,
1348+
kdtree=self.kdtree,
13371349
rad_1=rad_1,
13381350
rad_2=rad_2,
13391351
threshold_factor=threshold_factor,
@@ -1346,12 +1358,12 @@ def classify_mountains_radii(
13461358

13471359
def classify_mountains_weights(
13481360
self,
1349-
kdtree,
13501361
threshold: int = 0,
13511362
percentile: int = 75,
13521363
k_neighbors: int = 3,
13531364
method: str = "mean",
13541365
normalization: str = "linear",
1366+
kdtree = None,
13551367
):
13561368
"""
13571369
Add a column to the scenario meta_data dataframe containing a boolean
@@ -1360,10 +1372,6 @@ def classify_mountains_weights(
13601372
13611373
Parameters:
13621374
-----------
1363-
kdtree : sklearn.neighbors.KDTree or str
1364-
kdtree containing latitude-longitude pairs for quick lookups
1365-
Generate using ``pvdeg.geospatial.meta_KDTree``. Can take a pickled
1366-
kdtree as a path to the .pkl file.
13671375
threshold : float
13681376
minimum weight that a mountain can be identifed.
13691377
value between `[0,1]` (inclusive)
@@ -1379,6 +1387,11 @@ def classify_mountains_weights(
13791387
normalization : str, (default = 'linear')
13801388
function to apply when normalizing weights. Logarithmic uses log_e/ln
13811389
options : `'linear'`, `'logarithmic'`, '`exponential'`
1390+
kdtree : sklearn.neighbors.KDTree or str
1391+
Generated automatically but can be provided externally.
1392+
kdtree containing latitude-longitude pairs for quick lookups
1393+
Generate using ``pvdeg.geospatial.meta_KDTree``. Can take a pickled
1394+
kdtree as a path to the .pkl file.
13821395
13831396
Returns:
13841397
--------
@@ -1388,9 +1401,12 @@ def classify_mountains_weights(
13881401
---------
13891402
`pvdeg.geospatial.identify_mountains_weights`
13901403
"""
1404+
1405+
self.set_kdtree(kdtree=kdtree)
1406+
13911407
gids = pvdeg.geospatial.identify_mountains_weights(
13921408
meta_df=self.meta_data,
1393-
kdtree=kdtree,
1409+
kdtree=self.kdtree,
13941410
threshold=threshold,
13951411
percentile=percentile,
13961412
k_neighbors=k_neighbors,
@@ -1403,17 +1419,13 @@ def classify_mountains_weights(
14031419

14041420
def classify_feature(
14051421
self,
1406-
kdtree=None,
14071422
feature_name=None,
14081423
resolution="10m",
14091424
radius=None,
1425+
kdtree=None,
14101426
bbox_kwarg={},
14111427
):
14121428
"""
1413-
kdtree : sklearn.neighbors.KDTree or str
1414-
kdtree containing latitude-longitude pairs for quick lookups
1415-
Generate using ``pvdeg.geospatial.meta_KDTree``. Can take a pickled
1416-
kdtree as a path to the .pkl file.
14171429
feature_name : str
14181430
cartopy.feature.NaturalEarthFeature feature key.
14191431
Options: ``'lakes'``, ``'rivers_lake_centerlines'``, ``'coastline'``
@@ -1424,6 +1436,11 @@ def classify_feature(
14241436
Area around feature coordinates to include in the downsampled result.
14251437
Bigger area means larger radius and more samples included.
14261438
pass
1439+
kdtree : sklearn.neighbors.KDTree or str
1440+
Generated automatically but can be provided externally.
1441+
kdtree containing latitude-longitude pairs for quick lookups
1442+
Generate using ``pvdeg.geospatial.meta_KDTree``. Can take a pickled
1443+
kdtree as a path to the .pkl file.
14271444
14281445
Returns:
14291446
--------
@@ -1434,9 +1451,11 @@ def classify_feature(
14341451
`pvdeg.geospatial.feature_downselect`
14351452
"""
14361453

1454+
self.set_kdtree(kdtree=kdtree)
1455+
14371456
feature_gids = pvdeg.geospatial.feature_downselect(
14381457
meta_df=self.meta_data,
1439-
kdtree=kdtree,
1458+
kdtree=self.kdtree,
14401459
feature_name=feature_name,
14411460
resolution=resolution,
14421461
radius=radius,
@@ -1448,22 +1467,18 @@ def classify_feature(
14481467

14491468
def downselect_elevation_stochastic(
14501469
self,
1451-
kdtree,
14521470
downselect_prop,
14531471
k_neighbors=3,
14541472
method="mean",
14551473
normalization="linear",
1474+
kdtree = None,
14561475
):
14571476
"""
14581477
Prefenetially downselect data points based on elevation and update
14591478
scenario metadata.
14601479
14611480
Parameters:
14621481
-----------
1463-
kdtree : sklearn.neighbors.KDTree or str
1464-
kdtree containing latitude-longitude pairs for quick lookups
1465-
Generate using ``pvdeg.geospatial.meta_KDTree``. Can take a pickled
1466-
kdtree as a path to the .pkl file.
14671482
downselect_prop : float
14681483
proportion of original datapoints to keep in output gids list
14691484
k_neighbors : int, (default = 3)
@@ -1474,6 +1489,11 @@ def downselect_elevation_stochastic(
14741489
normalization : str, (default = 'linear')
14751490
function to apply when normalizing weights. Logarithmic uses $log_e$, $ln$
14761491
options : `'linear'`, `'log'`, '`exp'`, `'invert-linear'`
1492+
kdtree : sklearn.neighbors.KDTree or str
1493+
Generated automatically but can be provided externally.
1494+
kdtree containing latitude-longitude pairs for quick lookups
1495+
Generate using ``pvdeg.geospatial.meta_KDTree``. Can take a pickled
1496+
kdtree as a path to the .pkl file.
14771497
14781498
Returns:
14791499
--------
@@ -1483,9 +1503,12 @@ def downselect_elevation_stochastic(
14831503
---------
14841504
`pvdeg.geospatial.elevation_stochastic_downselect` for more info/docs
14851505
"""
1506+
1507+
self.set_kdtree(kdtree=kdtree)
1508+
14861509
gids = pvdeg.geospatial.elevation_stochastic_downselect(
14871510
meta_df=self.meta_data,
1488-
kdtree=kdtree,
1511+
kdtree=self.kdtree,
14891512
downselect_prop=downselect_prop,
14901513
k_neighbors=k_neighbors,
14911514
method=method,

0 commit comments

Comments
 (0)