11import logging
2+ import warnings
23from time import sleep
34from typing import Union , Optional , Tuple
45from multiprocessing import Pool
1011from shapely .geometry import Point , Polygon
1112from tqdm .autonotebook import tqdm
1213
14+ warnings .filterwarnings (
15+ "ignore" ,
16+ message = "Modules under `h3.unstable` are experimental, and may change at any time." ,
17+ )
18+
19+ from h3 .unstable import vect as h3_vect # name in 3.x
1320
1421from openquake .hazardlib .source .rupture import (
1522 NonParametricProbabilisticRupture ,
2431 flatten_list ,
2532 get_nonparametric_rupture_occurrence_rate ,
2633 _get_class_name ,
27- breakpoint
34+ breakpoint ,
2835)
2936
3037
@@ -401,7 +408,8 @@ def rupture_dict_from_logic_tree_dict(
401408 logic_tree_dict .items ()
402409 ):
403410 logging .info (
404- f"processing { branch_name } ({ i + 1 } /{ len (logic_tree_dict .keys ())} )"
411+ f"processing { branch_name } ({ i + 1 } /"
412+ + f"{ len (logic_tree_dict .keys ())} )"
405413 )
406414 rup_dict [branch_name ] = rupture_list_from_source_list_parallel (
407415 source_list ,
@@ -417,7 +425,8 @@ def rupture_dict_from_logic_tree_dict(
417425 logic_tree_dict .items ()
418426 ):
419427 logging .info (
420- f"processing { branch_name } ({ i + 1 } /{ len (logic_tree_dict .keys ())} )"
428+ f"processing { branch_name } ({ i + 1 } /"
429+ + f"{ len (logic_tree_dict .keys ())} )"
421430 )
422431 rup_dict [branch_name ] = rupture_df_from_source_list (
423432 source_list ,
@@ -452,10 +461,11 @@ def rupture_dict_to_gdf(
452461 if df .occurrence_rate .min () <= 0.0 :
453462 logging .info ("trimming zero-rate ruptures" )
454463 df_len = len (df )
455- df = df [df .occurrence_rate > 0. ]
464+ df = df [df .occurrence_rate > 0.0 ]
456465 df_trim_len = len (df )
457466 logging .info (
458- f"{ df_trim_len } ruptures remaining, { df_len - df_trim_len } removed"
467+ f"{ df_trim_len } ruptures remaining, "
468+ + f"{ df_len - df_trim_len } removed"
459469 )
460470
461471 if return_gdf :
@@ -472,35 +482,17 @@ def parse_geometry(row, x="longitude", y="latitude", z="depth"):
472482 return df
473483
474484
475- def _get_h3_cell_for_rupture_df (rupture_df , h3_res ):
476- logging .info ("getting H3 cells" )
477- cell_ids = list (
478- tqdm (
479- (
480- h3 .geo_to_h3 (row .latitude , row .longitude , h3_res )
481- for i , row in rupture_df .iterrows ()
482- ),
483- total = len (rupture_df ),
484- )
485- )
486- rupture_df ["cell_id" ] = cell_ids
487-
488-
489485def _get_h3_cell (args ):
490486 return h3 .geo_to_h3 (* args )
491487
492488
493- def _get_h3_cell_for_rupture_df_parallel (rupture_df , h3_res ):
494- logging .info ("getting H3 cells in parallel" )
495-
496- lons = rupture_df .longitude .values
497- lats = rupture_df .latitude .values
498-
499- args = ((lat , lons [i ], h3_res ) for i , lat in enumerate (lats ))
500-
501- with Pool (_n_procs ) as pool :
502- cell_ids = pool .map (_get_h3_cell , args )
489+ def _get_h3_cell_for_rupture_df (rupture_df , h3_res ):
490+ lats = rupture_df ["latitude" ].to_numpy ()
491+ lons = rupture_df ["longitude" ].to_numpy ()
503492
493+ cell_ints = h3_vect .geo_to_h3 (lats , lons , h3_res )
494+ h3_to_string = np .frompyfunc (h3 .h3_to_string , 1 , 1 )
495+ cell_ids = h3_to_string (cell_ints )
504496 rupture_df ["cell_id" ] = cell_ids
505497
506498
0 commit comments