@@ -1110,6 +1110,7 @@ def __init__(
1110
1110
self .hpc = hpc
1111
1111
self .func = func
1112
1112
self .template = template
1113
+ self .kdtree = None # sklearn kdtree
1113
1114
1114
1115
def __eq__ (self , other ):
1115
1116
raise NotImplementedError ("""
@@ -1285,14 +1286,22 @@ def location_bounding_box(
1285
1286
1286
1287
self .meta_data = self .meta_data .loc [bbox_gids ]
1287
1288
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
+
1288
1297
def classify_mountains_radii (
1289
1298
self ,
1290
- kdtree ,
1291
1299
rad_1 : Union [float , int ] = 12 ,
1292
1300
rad_2 : Union [float , int ] = 1 ,
1293
1301
threshold_factor : Union [float , int ] = 1.25 ,
1294
1302
elevation_floor : Union [float , int ] = 0 ,
1295
1303
bbox_kwarg : Optional [dict ] = {},
1304
+ kdtree = None ,
1296
1305
):
1297
1306
"""
1298
1307
Find mountains from elevation metadata using sklearn kdtree for fast lookup.
@@ -1306,9 +1315,6 @@ def classify_mountains_radii(
1306
1315
-----------
1307
1316
meta_df : pd.DataFrame
1308
1317
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``
1312
1318
rad_1 : float
1313
1319
radius of the larger search area whose elevations are compared against
1314
1320
the smaller search area. controls the kdtree query region.
@@ -1325,15 +1331,21 @@ def classify_mountains_radii(
1325
1331
elevation_floor : int
1326
1332
minimum inclusive elevation in meters. If a point has smaller location
1327
1333
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``
1328
1338
1329
1339
Returns:
1330
1340
--------
1331
1341
None, strictly updates meta_data attribute of geospatial scenari instance.
1332
1342
"""
1333
1343
1344
+ self .set_kdtree (kdtree = kdtree )
1345
+
1334
1346
gids = pvdeg .geospatial .identify_mountains_radii (
1335
1347
meta_df = self .meta_data ,
1336
- kdtree = kdtree ,
1348
+ kdtree = self . kdtree ,
1337
1349
rad_1 = rad_1 ,
1338
1350
rad_2 = rad_2 ,
1339
1351
threshold_factor = threshold_factor ,
@@ -1346,12 +1358,12 @@ def classify_mountains_radii(
1346
1358
1347
1359
def classify_mountains_weights (
1348
1360
self ,
1349
- kdtree ,
1350
1361
threshold : int = 0 ,
1351
1362
percentile : int = 75 ,
1352
1363
k_neighbors : int = 3 ,
1353
1364
method : str = "mean" ,
1354
1365
normalization : str = "linear" ,
1366
+ kdtree = None ,
1355
1367
):
1356
1368
"""
1357
1369
Add a column to the scenario meta_data dataframe containing a boolean
@@ -1360,10 +1372,6 @@ def classify_mountains_weights(
1360
1372
1361
1373
Parameters:
1362
1374
-----------
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.
1367
1375
threshold : float
1368
1376
minimum weight that a mountain can be identifed.
1369
1377
value between `[0,1]` (inclusive)
@@ -1379,6 +1387,11 @@ def classify_mountains_weights(
1379
1387
normalization : str, (default = 'linear')
1380
1388
function to apply when normalizing weights. Logarithmic uses log_e/ln
1381
1389
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.
1382
1395
1383
1396
Returns:
1384
1397
--------
@@ -1388,9 +1401,12 @@ def classify_mountains_weights(
1388
1401
---------
1389
1402
`pvdeg.geospatial.identify_mountains_weights`
1390
1403
"""
1404
+
1405
+ self .set_kdtree (kdtree = kdtree )
1406
+
1391
1407
gids = pvdeg .geospatial .identify_mountains_weights (
1392
1408
meta_df = self .meta_data ,
1393
- kdtree = kdtree ,
1409
+ kdtree = self . kdtree ,
1394
1410
threshold = threshold ,
1395
1411
percentile = percentile ,
1396
1412
k_neighbors = k_neighbors ,
@@ -1403,17 +1419,13 @@ def classify_mountains_weights(
1403
1419
1404
1420
def classify_feature (
1405
1421
self ,
1406
- kdtree = None ,
1407
1422
feature_name = None ,
1408
1423
resolution = "10m" ,
1409
1424
radius = None ,
1425
+ kdtree = None ,
1410
1426
bbox_kwarg = {},
1411
1427
):
1412
1428
"""
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.
1417
1429
feature_name : str
1418
1430
cartopy.feature.NaturalEarthFeature feature key.
1419
1431
Options: ``'lakes'``, ``'rivers_lake_centerlines'``, ``'coastline'``
@@ -1424,6 +1436,11 @@ def classify_feature(
1424
1436
Area around feature coordinates to include in the downsampled result.
1425
1437
Bigger area means larger radius and more samples included.
1426
1438
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.
1427
1444
1428
1445
Returns:
1429
1446
--------
@@ -1434,9 +1451,11 @@ def classify_feature(
1434
1451
`pvdeg.geospatial.feature_downselect`
1435
1452
"""
1436
1453
1454
+ self .set_kdtree (kdtree = kdtree )
1455
+
1437
1456
feature_gids = pvdeg .geospatial .feature_downselect (
1438
1457
meta_df = self .meta_data ,
1439
- kdtree = kdtree ,
1458
+ kdtree = self . kdtree ,
1440
1459
feature_name = feature_name ,
1441
1460
resolution = resolution ,
1442
1461
radius = radius ,
@@ -1448,22 +1467,18 @@ def classify_feature(
1448
1467
1449
1468
def downselect_elevation_stochastic (
1450
1469
self ,
1451
- kdtree ,
1452
1470
downselect_prop ,
1453
1471
k_neighbors = 3 ,
1454
1472
method = "mean" ,
1455
1473
normalization = "linear" ,
1474
+ kdtree = None ,
1456
1475
):
1457
1476
"""
1458
1477
Prefenetially downselect data points based on elevation and update
1459
1478
scenario metadata.
1460
1479
1461
1480
Parameters:
1462
1481
-----------
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.
1467
1482
downselect_prop : float
1468
1483
proportion of original datapoints to keep in output gids list
1469
1484
k_neighbors : int, (default = 3)
@@ -1474,6 +1489,11 @@ def downselect_elevation_stochastic(
1474
1489
normalization : str, (default = 'linear')
1475
1490
function to apply when normalizing weights. Logarithmic uses $log_e$, $ln$
1476
1491
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.
1477
1497
1478
1498
Returns:
1479
1499
--------
@@ -1483,9 +1503,12 @@ def downselect_elevation_stochastic(
1483
1503
---------
1484
1504
`pvdeg.geospatial.elevation_stochastic_downselect` for more info/docs
1485
1505
"""
1506
+
1507
+ self .set_kdtree (kdtree = kdtree )
1508
+
1486
1509
gids = pvdeg .geospatial .elevation_stochastic_downselect (
1487
1510
meta_df = self .meta_data ,
1488
- kdtree = kdtree ,
1511
+ kdtree = self . kdtree ,
1489
1512
downselect_prop = downselect_prop ,
1490
1513
k_neighbors = k_neighbors ,
1491
1514
method = method ,
0 commit comments