Skip to content

Commit

Permalink
Merge pull request #360 from GEMScienceTools/fix_aftershocks
Browse files Browse the repository at this point in the history
Adding paths relative to file - test now should run on remote
  • Loading branch information
cossatot authored Oct 26, 2023
2 parents 8880f35 + d854c31 commit bf3a809
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 22 deletions.
33 changes: 29 additions & 4 deletions openquake/aft/aftershock_probabilities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
# ------------------- The OpenQuake Model Building Toolkit --------------------
# Copyright (C) 2022-2023 GEM Foundation
# _______ _______ __ __ _______ _______ ___ _
# | || | | |_| || _ || || | | |
# | _ || _ | ____ | || |_| ||_ _|| |_| |
# | | | || | | ||____|| || | | | | _|
# | |_| || |_| | | || _ | | | | |_
# | || | | ||_|| || |_| | | | | _ |
# |_______||____||_| |_| |_||_______| |___| |___| |_|
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# coding: utf-8

import time
import logging
from typing import Optional, Sequence, Tuple

import h5py
import numpy as np
import pandas as pd
from tqdm import tqdm
Expand Down Expand Up @@ -85,7 +110,7 @@ def get_aftershock_rup_rates(
return

if not rup_id:
rup_id = rup.rup_id
rup_id = rup.ruid

mfd = get_aftershock_grmfd(
rup,
Expand Down Expand Up @@ -345,9 +370,9 @@ def get_aftershock_rupture_rates(

logging.info("Concatenating results")
rr = [r for r in rup_adjustments if len(r) != 0]
t7 = time.time()
# t7 = time.time() # TODO not used
rup_adj_df = pd.concat([pd.DataFrame(r) for r in rr], axis=1).fillna(0.0)
t8 = time.time()
# t8 = time.time() # TODO not used

rup_adjustments = rup_adj_df.sum(axis=1)
oq_rup_index = rup_df.loc[rup_adjustments.index, "oq_rup_ind"]
Expand Down
31 changes: 28 additions & 3 deletions openquake/aft/rupture_distances.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# ------------------- The OpenQuake Model Building Toolkit --------------------
# Copyright (C) 2022-2023 GEM Foundation
# _______ _______ __ __ _______ _______ ___ _
# | || | | |_| || _ || || | | |
# | _ || _ | ____ | || |_| ||_ _|| |_| |
# | | | || | | ||____|| || | | | | _|
# | |_| || |_| | | || _ | | | | |_
# | || | | ||_|| || |_| | | | | _ |
# |_______||____||_| |_| |_||_______| |___| |___| |_|
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# coding: utf-8

import logging
from multiprocessing import Pool
from typing import Dict, Sequence, Optional, Tuple, Union

import h5py
Expand Down Expand Up @@ -251,7 +276,7 @@ def split_rows(
block_idx = row_ids[start:stop]

this["array_stack"] = stacked_array[
row_ids[start] : row_ids[stop], :
row_ids[start]: row_ids[stop], :
]

this["row_idxs"] = block_idx - block_idx[0]
Expand All @@ -260,7 +285,7 @@ def split_rows(
start = closest_first_inds[i]
block_idx = row_ids[start:]

this["array_stack"] = stacked_array[row_ids[start] :, :]
this["array_stack"] = stacked_array[row_ids[start]:, :]
this["row_idxs"] = block_idx - block_idx[0]

return data_splits
Expand Down
25 changes: 25 additions & 0 deletions openquake/aft/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ------------------- The OpenQuake Model Building Toolkit --------------------
# Copyright (C) 2022-2023 GEM Foundation
# _______ _______ __ __ _______ _______ ___ _
# | || | | |_| || _ || || | | |
# | _ || _ | ____ | || |_| ||_ _|| |_| |
# | | | || | | ||____|| || | | | | _|
# | |_| || |_| | | || _ | | | | |_
# | || | | ||_|| || |_| | | | | _ |
# |_______||____||_| |_| |_||_______| |___| |___| |_|
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# coding: utf-8
58 changes: 43 additions & 15 deletions openquake/aft/tests/test_aftershock_probabilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
import os
# ------------------- The OpenQuake Model Building Toolkit --------------------
# Copyright (C) 2022-2023 GEM Foundation
# _______ _______ __ __ _______ _______ ___ _
# | || | | |_| || _ || || | | |
# | _ || _ | ____ | || |_| ||_ _|| |_| |
# | | | || | | ||____|| || | | | | _|
# | |_| || |_| | | || _ | | | | |_
# | || | | ||_|| || |_| | | | | _ |
# |_______||____||_| |_| |_||_______| |___| |___| |_|
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# coding: utf-8

import pathlib

import pandas as pd
import numpy as np
Expand All @@ -18,19 +44,22 @@
)

from openquake.aft.aftershock_probabilities import (
#get_aftershock_grmfd,
#num_aftershocks,
#get_a,
# get_aftershock_grmfd,
# num_aftershocks,
# get_a,
get_source_counts,
#get_aftershock_rup_rates,
#get_rup,
#RupDist2,
#make_source_dist_df,
#fetch_rup_from_source_dist_groups,
# get_aftershock_rup_rates,
# get_rup,
# RupDist2,
# make_source_dist_df,
# fetch_rup_from_source_dist_groups,
rupture_aftershock_rates_per_source,
prep_source_data,
)

HERE = pathlib.Path(__file__).parent.absolute()


area_source_1 = AreaSource(
source_id=0,
name="s1",
Expand Down Expand Up @@ -165,11 +194,12 @@ def test_get_aftershock_rup_adjustments():
oq_rup_index = rup_df.loc[rup_adjustments.index, "oq_rup_ind"]
rup_adjustments.index = oq_rup_index

rup_adj_df_path = os.path.join(".", "test_data", "test_get_aftershock_rup_adjustments_results.csv")
rup_adj_df_path = (HERE / "test_data" /
"test_get_aftershock_rup_adjustments_results.csv")
rup_adjustment_df = pd.read_csv(rup_adj_df_path, index_col=0)

np.testing.assert_array_almost_equal(rup_adjustments.values,
rup_adjustment_df['rates'].values)
rup_adjustment_df['rates'].values)

return rup_adjustments

Expand All @@ -184,6 +214,7 @@ def mag_to_mo(mag: float, c: float = 9.05):


def plot_mfds():

sources = [area_source_1, area_source_2, area_source_3]
rup_df, source_groups = prep_source_data(sources)

Expand Down Expand Up @@ -223,13 +254,10 @@ def look_at_aftershock_rup_rates():
rup_df, source_groups = prep_source_data(sources)
source_pairs = get_close_source_pairs(sources)

rup_dists = calc_rupture_adjacence_dict_all_sources(
_ = calc_rupture_adjacence_dict_all_sources(
source_pairs, rup_df, source_groups
)

source_counts, source_cum_counts, source_count_starts = get_source_counts(
sources
)


#plot_mfds()

0 comments on commit bf3a809

Please sign in to comment.