pickagm 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pickagm/__init__.py ADDED
File without changes
pickagm/avgSA.py ADDED
@@ -0,0 +1,164 @@
1
+ import numpy as np
2
+ from openquake.hazardlib.imt import IMT, SA, PGA
3
+ from openquake.hazardlib.gsim.base import GMPE
4
+
5
+ def compute_ln_AvgSA(
6
+ ln_SA_data: np.ndarray,
7
+ weights: np.ndarray|None=None
8
+ ) -> float|np.ndarray:
9
+ # ln_SA_data: is a 1D or 2D numpy array containing the natural log of the
10
+ # SA values required to calculate the AvgSA. If 2D the average is
11
+ # calculated along a row. i.e. each column represents a different period. If
12
+ # it is 1D then the average is performed on all values in the vector
13
+
14
+ # weights: is a 1D array containing the weights associated with each SA(T1)
15
+ # returns ln_AvgSA
16
+
17
+ if len(ln_SA_data.shape) == 1:
18
+ n_values = len(ln_SA_data)
19
+ elif len(ln_SA_data.shape) == 2:
20
+ n_values = ln_SA_data.shape[1] # number of columns is the number of periods
21
+
22
+ if not weights is None:
23
+ if len(weights) != n_values:
24
+ raise ValueError(f"length of 'weights' ({len(weights)}) does not "
25
+ f"match 'n' ({n_values})")
26
+
27
+ raise NotImplementedError("weighting of periods is not yet implemented")
28
+
29
+ ln_avgSA = (1 / n_values) * ln_SA_data.sum(axis=1)
30
+ return ln_avgSA
31
+
32
+
33
+ def compute_std_dev_ln_AvgSA(
34
+ std_dev_ln_SA: np.ndarray,
35
+ correlation_matrix: np.ndarray,
36
+ weights: np.ndarray|None=None) -> np.ndarray:
37
+ # std_dev_ln_SA: is a 1D or 2D numpy array containing the std. dev of
38
+ # the natural log of the SA values used to calculate the AvgSA.
39
+ # std_dev_ln_SA: can be either the total, between-event or within-event
40
+ # std. deviation.
41
+ # If 2D each column represents a different period and each row a different
42
+ # rupture. If 1D then each values represent the values associated with a
43
+ # single rupture
44
+
45
+ # Calculation according to Kohrangi et al. (2018) "Ground-motion models for
46
+ # average spectral acceleration in a period range: direct and indirect
47
+ # methods", in Bulletin of Earthquake Engineering, Eq. 3
48
+
49
+ if len(std_dev_ln_SA.shape) == 1:
50
+ n_values = len(std_dev_ln_SA)
51
+ elif len(std_dev_ln_SA.shape) == 2:
52
+ n_values = std_dev_ln_SA.shape[1] # number of cols is the no. of periods
53
+ else:
54
+ raise ValueError(f"Dimension = {len(std_dev_ln_SA.shape)}. Must be 1 or 2")
55
+
56
+ if not weights is None:
57
+ if len(weights) != n_values:
58
+ raise ValueError(f"length of 'weights' ({len(weights)}) does not "
59
+ f"match 'n' ({n_values})")
60
+
61
+ raise NotImplementedError("weighting of periods is not yet implemented")
62
+
63
+ # create an nxmxm data cube where each layer contains the products of
64
+ # sigma_i, sigma_j, and rho for that rupture. Uses numpy broadcasting
65
+ # sigma_ln_AvgSA[:, :, np.newaxis] has shape (n, m, 1)
66
+ # sigma_ln_AvgSA[:, np.newaxis, :] has shape (n, 1, m)
67
+ ssr = std_dev_ln_SA[:, :, np.newaxis] * \
68
+ std_dev_ln_SA[:, np.newaxis, :] * \
69
+ correlation_matrix
70
+
71
+ std_dev_ln_SA = np.sqrt((1 / n_values) ** 2 * np.sum(ssr, axis=(1, 2)))
72
+ return std_dev_ln_SA
73
+
74
+
75
+ class indirect_AvgSA_GMPE():
76
+ def __init__(
77
+ self,
78
+ gmm: GMPE|list[GMPE],
79
+ rho_total: np.ndarray|None = None,
80
+ rho_deltaB: np.ndarray|None = None,
81
+ rho_deltaW: np.ndarray|None = None,
82
+ gmm_weights: np.ndarray|None = None,
83
+ mean_only: bool = False,
84
+ avg_periods: list[float]|None=None,):
85
+
86
+
87
+ # either rho_total needs to be specified or
88
+ # both rho_deltaB and rho_deltaW need to be specified
89
+ if not mean_only:
90
+ if (rho_deltaB is not None and rho_deltaW is not None):
91
+ self.use_dB_dW = True
92
+ else:
93
+ self.use_dB_dW = False
94
+
95
+ if rho_total is None and not self.use_dB_dW:
96
+ raise ValueError("Either 'rho_total' or both 'rho_deltaB' and "
97
+ "'rho_deltaW must not be None")
98
+
99
+ if not isinstance(gmm, list):
100
+ gmm = [gmm]
101
+
102
+ if gmm_weights:
103
+ raise NotImplementedError("weighting of GMMs is not implemented")
104
+
105
+ self.gmm = gmm
106
+ self.rho = rho_total
107
+ self.rho_dB = rho_deltaB
108
+ self.rho_dW = rho_deltaW
109
+ self.gmm_weights = gmm_weights
110
+ self.mean_only = mean_only
111
+
112
+ if avg_periods is not None:
113
+ self.imts = [SA(t) if t > 0 else PGA() for t in avg_periods]
114
+ else:
115
+ self.imts = None
116
+
117
+
118
+ def compute(
119
+ self,
120
+ ctx: np.recarray,
121
+ imts: IMT|list[IMT]|None,
122
+ mean: np.ndarray,
123
+ sigma: np.ndarray|None=None,
124
+ tau: np.ndarray|None=None,
125
+ phi: np.ndarray|None=None) -> tuple[np.ndarray, np.ndarray]:
126
+
127
+ # mean sigma tau and phi are all 1 x len(ctx) arrays instead of the
128
+ # standard len(imts) x len(ctx) because the compute function should
129
+ # always return None and the imts are averaged to produce a single value
130
+
131
+ if self.imts is not None:
132
+ # the stored imts are used
133
+ imts = self.imts
134
+
135
+ # the standard arrays of shape len(imts) x len(ctx) still need to be
136
+ # to the openquake GMPE to calculate all the im values for the ctx
137
+ sa_mean = np.zeros((len(imts), len(ctx)))
138
+ sa_sigma = np.zeros_like(sa_mean)
139
+ sa_tau = np.zeros_like(sa_mean)
140
+ sa_phi = np.zeros_like(sa_mean)
141
+
142
+ # for each GMM get the mean and std. dev for all ruptures
143
+ for g in self.gmm:
144
+ g.compute(ctx, imts, sa_mean, sa_sigma, sa_tau, sa_phi)
145
+
146
+ # calculate the mean and std. dev of the ln_AvgSA
147
+ mu_ln_AvgSA = compute_ln_AvgSA(sa_mean.T) # transpose so columns are for each T
148
+ mean[:,:] = mu_ln_AvgSA.T
149
+
150
+ if not self.mean_only:
151
+ if self.use_dB_dW:
152
+ tau_ln_AvgSA = compute_std_dev_ln_AvgSA(sa_tau.T, self.rho_dB)
153
+ phi_ln_AvgSA = compute_std_dev_ln_AvgSA(sa_phi.T, self.rho_dW)
154
+ sigma_ln_AvgSA = np.sqrt(tau_ln_AvgSA ** 2 + phi_ln_AvgSA ** 2)
155
+ tau[:,:] = tau_ln_AvgSA.T
156
+ phi[:,:] = phi_ln_AvgSA.T
157
+ else:
158
+ sigma_ln_AvgSA = compute_std_dev_ln_AvgSA(sa_sigma.T, self.rho)
159
+
160
+ sigma[:,:] = sigma_ln_AvgSA.T
161
+ return
162
+
163
+ def __str__(self):
164
+ return f"[indirect_AvgSA_GMPE with {self.gmm}]"
pickagm/contexts.py ADDED
@@ -0,0 +1,51 @@
1
+ from typing import Protocol
2
+
3
+ import numpy as np
4
+ import pandas as pd
5
+
6
+
7
+ class SiteRupContextBuilder(Protocol):
8
+
9
+ def __init__(self, n_ctxs: int, site_params: dict, *args, **kwargs) -> None:
10
+ ...
11
+
12
+ def ctx(self, rup_params: dict) -> np.recarray:
13
+ ...
14
+ ...
15
+
16
+ def get_rup_ctxs(disagg_type: str, rup_samples: pd.DataFrame) -> list[dict]:
17
+
18
+ disagg_mappping = {
19
+ "Mag": "mag",
20
+ "Dist": "rjb",
21
+ "Lon": "lon",
22
+ "Lat": "lat",
23
+ "TRT": "trt",
24
+ "Eps": "eps"
25
+ }
26
+
27
+ disagg_params = disagg_type.split("_")
28
+ for param in disagg_params:
29
+ if param not in disagg_mappping:
30
+ raise ValueError(f"Disagg type {disagg_type} is not supported")
31
+
32
+ rup_ctxs = []
33
+ for s in rup_samples[disagg_params].itertuples(index=False):
34
+ rup_ctx = {}
35
+ for param in disagg_params:
36
+ rup_ctx[disagg_mappping[param]] = getattr(s, param)
37
+
38
+ rup_ctxs.append(rup_ctx)
39
+
40
+ return rup_ctxs
41
+
42
+
43
+ def get_site_rup_ctxs(disagg_type: str,
44
+ disagg_dist: pd.DataFrame,
45
+ ctx_builder: SiteRupContextBuilder):
46
+
47
+ rup_ctxs = get_rup_ctxs(disagg_type, disagg_dist) # do them all!
48
+ site_rup_ctxs = [ctx_builder.ctx(rup_ctx) for rup_ctx in rup_ctxs]
49
+ site_rup_ctxs = np.concatenate(site_rup_ctxs).view(np.recarray)
50
+ return site_rup_ctxs
51
+
pickagm/corrmodels.py ADDED
@@ -0,0 +1,455 @@
1
+ import json
2
+ from pathlib import Path
3
+ from typing import Protocol
4
+
5
+ import h5py
6
+ import numpy as np
7
+ import pandas as pd
8
+ from scipy.interpolate import RegularGridInterpolator
9
+
10
+ CORR_COEFFS_FOLDER = Path(__file__).parent / "im_corr_data"
11
+
12
+ ## This code is based on the correlation models in OpenQuake, but it is adapted
13
+ ## to allow consider all IMs instead of just SA.
14
+ class CorrelationModel(Protocol):
15
+
16
+ rho: dict[str, pd.DataFrame] # dict of correlation matrices for each residual type
17
+ model: str
18
+
19
+ def get_correlation(self, imt1: tuple, imt2: tuple, residual_type) -> float:
20
+ ...
21
+
22
+ def build_correlation_matrices(self,
23
+ incl_nonSA_imts: list[str]|None=None,
24
+ incl_SA_Ts: list[float]|None=None,
25
+ excl_SA_T_gt: float|None=None,
26
+ excl_SA_T_lt: float|None=None) -> dict[str, pd.DataFrame]:
27
+ ...
28
+
29
+
30
+ class EmpiricalCorrelationModel:
31
+ """
32
+ Intermediate class to handle the interpolation of empirical correlation
33
+ data and the construction of the correlation matrices.
34
+ """
35
+ def __init__(self, rho_arrays, rho_imts,
36
+ incl_nonSA_imts: list[str]|None=None,
37
+ incl_SA_Ts: list[float]|None=None):
38
+ """
39
+ # TODO: add more details to docstring
40
+ :param np.ndarray avg_periods :
41
+ The periods used for calculating the spectral acceleration
42
+
43
+ :param dict[str, np.ndarray] rho_arrays:
44
+ The correlation data. The keys rho_arrays are strings that represent
45
+ the type of residual that the associated array corresponds to.
46
+
47
+ :param np.ndarray rho_periods:
48
+ A 1D array of the spectral periods that correspond to the rows and
49
+ columns of the correlation data
50
+
51
+ Notes:
52
+ 1) It is assumed that the same periods apply to all correlation
53
+ matrices in the same correlation model.
54
+
55
+ 2) The keys for rho_arrays follow the notation of Al Atik et al (2010).
56
+ Each key maps to a method that returns the appropriate
57
+ correlation coefficient. Currently the this is:
58
+ "total" -> get_correlation()
59
+ "dB_e" -> get_between_event_correlation()
60
+ "dS2S_s" -> get_between_site_correlation()
61
+ "dWS_es" -> get_within_event_correlation()
62
+ The class can be instantiated using a dict that doesn't contain all
63
+ the keys, however if the mapped function is called then a ValueError
64
+ will be raised. The provided dict must have as a minimum the key
65
+ "total".
66
+ """
67
+ # rho_arrays is dictionary of the arrays of corr coeffs that are part
68
+ # of the correlation model. The keys of this dictionary indicate what
69
+ # correlation functions can be called without raising an error.
70
+
71
+ if not isinstance(rho_arrays, dict):
72
+ raise TypeError("rho_arrays must be of type dict")
73
+ if "total" not in rho_arrays.keys():
74
+ raise ValueError("rho_arrays must contain the key 'total'")
75
+
76
+ # store the raw correlation data
77
+ self.raw_rhos = rho_arrays
78
+ self.raw_imts = rho_imts
79
+ self.raw_Ts = np.array([imt[1] for ii, imt in enumerate(self.raw_imts)
80
+ if imt[0] == "SA"])
81
+ self.valid_residual_types = rho_arrays.keys()
82
+
83
+ MIN_PERIOD = min(self.raw_Ts)
84
+ MAX_PERIOD = max(self.raw_Ts)
85
+
86
+ # set some attributes for the requested IMTs and periods.
87
+ # self.included_nonSA_imts = incl_nonSA_imts
88
+ # self.desired_SA_Ts = incl_SA_Ts
89
+ # self.exclude_SA_T_gt = exclude_SA_T_gt
90
+ # self.exclude_SA_T_lt = exclude_SA_T_lt
91
+ self._nonsa_idxs = [ii for ii, imt in enumerate(self.raw_imts)
92
+ if not imt[0] == "SA"]
93
+ self._sa_idxs = [ii for ii, imt in enumerate(self.raw_imts)
94
+ if imt[0] == "SA"]
95
+
96
+ # create the interpolation functions for each residual type / array
97
+ self.sa_interpers = {}
98
+
99
+ # Do some checks to make sure the requested IMTs are in the model or
100
+ # can be interpolated from the available data.
101
+
102
+ # check that non-SA IMTs are in the model
103
+ if incl_nonSA_imts is not None:
104
+ invalid_imts = [imt for imt in incl_nonSA_imts
105
+ if imt not in [ii[0] for ii in self.raw_imts]]
106
+ if invalid_imts != []:
107
+ raise ValueError(f"Requested IMT(s) {invalid_imts} "
108
+ f"are not in the model")
109
+
110
+
111
+ # check that SA IMTs are in the model or can be interpolated from the
112
+ # available data
113
+ if incl_SA_Ts is not None:
114
+ if any(incl_SA_Ts < MIN_PERIOD) and ["PGA", "None"] not in rho_imts: # then we can't interpolate -> raise error
115
+ raise ValueError(f"Period ({min(incl_SA_Ts):.3f}) is less than "
116
+ f"the minimum allowable period for the "
117
+ f"correlation model ({MIN_PERIOD:.3f}).")
118
+ if any(incl_SA_Ts > MAX_PERIOD):
119
+ raise ValueError(f"Period ({max(incl_SA_Ts):.3f}) is greater than "
120
+ f"the maximum allowable period for the "
121
+ f"correlation model ({MAX_PERIOD:.3f}).")
122
+
123
+ # if we get to this point then the model is valid and we can build the
124
+ # desired correlation matrices for the requested IMTs.
125
+
126
+ self.rho = self.build_correlation_matrices(
127
+ incl_nonSA_imts, incl_SA_Ts)
128
+
129
+
130
+ def build_correlation_matrices(self,
131
+ incl_nonSA_imts: list[str]|None=None,
132
+ incl_SA_Ts: list[float]|None=None):
133
+ """
134
+ Interpolate the raw correlation data for required periods (avg_periods).
135
+ Returns a dict with keys indicating the type of residual and the values
136
+ being 2D numpy arrays of interpolated corr coeffs.
137
+ """
138
+ # interpolates the raw coeff data for all the provided matrices and
139
+ # returns a dictionary with the keys indicating the type of residual
140
+ # and the values being 2D numpy arrays of interpolated corr coeffs.
141
+
142
+ corr_mats = {}
143
+ for residual_type in self.valid_residual_types:
144
+ corr_mats[residual_type] = self._build_correlation_matrix(
145
+ residual_type, incl_nonSA_imts, incl_SA_Ts)
146
+ return corr_mats
147
+
148
+
149
+ def _build_correlation_matrix(self,
150
+ residual_type,
151
+ incl_nonSA_imts,
152
+ incl_SA_Ts):
153
+
154
+ # get the raw data for the specified residual type
155
+ raw_rhos = self.raw_rhos[residual_type]
156
+
157
+ #? if the period 0.0s is included we need to get the PGA
158
+ # get the desired nonSA imts
159
+ nonsa_idx = []
160
+ if incl_nonSA_imts is not None:
161
+ nonsa_idx += [ii for ii, imt in enumerate(self.raw_imts)
162
+ if imt[0] in incl_nonSA_imts]
163
+
164
+ elif ((any(incl_SA_Ts < min(self.raw_Ts)) and (incl_nonSA_imts is None)) or
165
+ (any(incl_SA_Ts < min(self.raw_Ts)) and ("PGA" not in incl_nonSA_imts))):
166
+ # we need to include PGA for interpolation
167
+ incl_nonSA_imts = ["PGA"]
168
+ nonsa_idx += [ii for ii, imt in enumerate(self.raw_imts)
169
+ if imt[0] == "PGA"]
170
+ else:
171
+ nonsa_idx = self._nonsa_idxs # the non SA imts will be removed later
172
+
173
+ # get the SA-SA correlations for the desired periods
174
+ if incl_SA_Ts is None:
175
+ periods_to_interp = self.raw_Ts
176
+
177
+ elif all(incl_SA_Ts >= min(self.raw_Ts)):
178
+ periods_to_interp = incl_SA_Ts
179
+ idxs_for_sa_data = self._sa_idxs
180
+ original_periods = self.raw_Ts
181
+
182
+ else:
183
+ # Remove the 0 period to interp because we already have the PGA
184
+ periods_to_interp = np.array(incl_SA_Ts)
185
+ periods_to_interp = periods_to_interp[periods_to_interp != 0]
186
+ idx_pga = [ii for ii, imt in enumerate(self.raw_imts)
187
+ if imt[0] == "PGA"]
188
+ # Add the PGA to the orignal data
189
+ idxs_for_sa_data = idx_pga + self._sa_idxs
190
+ original_periods = np.append(np.array(0), self.raw_Ts)
191
+
192
+ interper = self._create_SA_interper(
193
+ #? PGA needs to be included here if a period less than the smallest
194
+ #? SA(T) in the correlation data
195
+ raw_rhos[np.ix_(idxs_for_sa_data, idxs_for_sa_data)],
196
+ original_periods,
197
+ residual_type)
198
+ rho_SA_SA = self._interpolate_matrix(interper, periods_to_interp)
199
+
200
+ # get the nonSA-SA correlations for the desired periods
201
+ rho_nonSA_SA = []
202
+ for row in raw_rhos[nonsa_idx,:]:
203
+ rho_nonSA_SA.append(
204
+ np.interp(periods_to_interp, original_periods, row[idxs_for_sa_data]))
205
+ rho_nonSA_SA = np.array(rho_nonSA_SA) # shape (n_nonSA, n_SA)
206
+
207
+ # get the nonSA-nonSA correlations (no interpolation necessary)
208
+ rho_nonSA_nonSA = raw_rhos[np.ix_(nonsa_idx, nonsa_idx)]
209
+
210
+ # combine the nonSA-nonSA, nonSA-SA and SA-SA correlations into a single matrix
211
+ n_nonSA = len(nonsa_idx)
212
+ n_SA = len(periods_to_interp)
213
+ rho_mat = np.zeros((n_nonSA + n_SA, n_nonSA + n_SA))
214
+ rho_mat[:n_nonSA, :n_nonSA] = rho_nonSA_nonSA
215
+ rho_mat[:n_nonSA, n_nonSA:] = rho_nonSA_SA
216
+ rho_mat[n_nonSA:, :n_nonSA] = rho_nonSA_SA.T
217
+ rho_mat[n_nonSA:, n_nonSA:] = rho_SA_SA
218
+
219
+ # create a dataframe with the appropriate row and column labels
220
+ imt_labels = [imt[0] for ii, imt in enumerate(self.raw_imts)
221
+ if ii in nonsa_idx] +\
222
+ [f"SA({np.round(T,3)})" for T in periods_to_interp]
223
+ corr_df = pd.DataFrame(rho_mat,
224
+ index=imt_labels,
225
+ columns=imt_labels)
226
+
227
+ if incl_nonSA_imts is None: # remove nonSAimts
228
+ drop_imts = [imt for ii, imt in enumerate(corr_df.index) if ii in nonsa_idx]
229
+ corr_df = corr_df.drop(columns=drop_imts, index=drop_imts)
230
+
231
+ return corr_df
232
+
233
+
234
+ def _create_SA_interper(self, corr_data, periods, residual_type):
235
+ interper = RegularGridInterpolator(
236
+ points=(periods, periods),
237
+ values=corr_data,
238
+ method="linear"
239
+ )
240
+ self.sa_interpers[residual_type] = interper
241
+ return interper
242
+
243
+
244
+ def _interpolate_matrix(self, interper, new_Ts):
245
+ """
246
+ 2D interpolation of the correlation matrix.
247
+ Returns a symmetric nxn matrix with 1s on the diagonal
248
+
249
+ :param RegularGridInterpolator interper:
250
+ interpolation function to be used
251
+
252
+ :param np.ndarray new_Ts:
253
+ array of periods at which the matrix should be interpolated
254
+ """
255
+ # create an array of interp points with shape (N*N, 2)
256
+ t1, t2 = np.meshgrid(new_Ts, new_Ts, indexing="ij") # t1 and t2 have shape (N, N)
257
+ interp_pts = np.column_stack([t1.ravel(), t2.ravel()])
258
+ interped_rhos = interper(interp_pts).reshape(len(new_Ts), -1)
259
+
260
+ # make sure the diagonal terms are equal to 1.0
261
+ np.fill_diagonal(interped_rhos, 1.0)
262
+
263
+ return interped_rhos
264
+
265
+
266
+ def _check_valid_residual_type(self, residual_type):
267
+ if not residual_type in self.valid_residual_types:
268
+ raise ValueError(f"Invalid type of residual ({residual_type}) for "
269
+ f"correlation model.")
270
+
271
+ def get_correlation(self, imt1: tuple, imt2: tuple, residual_type) -> float:
272
+ """
273
+ gets the correlation coefficient for the specified imts and residual type.
274
+ Doesn't allow for interpolation, so the imts must be in the calculated
275
+ correlation matrix already.
276
+ # TODO: update docstring to be more specific about the imt format
277
+
278
+ :param float t1:
279
+ First period of interest.
280
+
281
+ :param float t2:
282
+ Second period of interest.
283
+
284
+ :return float rho:
285
+ The estimated correlation coefficient.
286
+ """
287
+ # check if the residual type is valid for this model
288
+ self._check_valid_residual_type(residual_type)
289
+
290
+ rho_df = self.corr_mats[residual_type]
291
+
292
+ # check that the imts are valid
293
+ self._check_valid_imt(imt1, rho_df)
294
+ self._check_valid_imt(imt2, rho_df)
295
+
296
+ return rho_df.loc[imt1, imt2]
297
+
298
+ def _check_valid_imt(self, imt, corr_df):
299
+ if imt not in corr_df.index:
300
+ raise ValueError(f"IMT {imt} is not in the correlation matrix.")
301
+
302
+
303
+ def __str__(self):
304
+ return f"{self.model}"
305
+
306
+
307
+ def load_corr_coeff_data_from_hdf5(fp, group_name):
308
+ """
309
+ Loads an HDF5 group into a dictionary of numpy arrays.
310
+ """
311
+ with h5py.File(fp, "r") as f:
312
+ if group_name not in f:
313
+ raise KeyError(f"Group '{group_name}' not found.")
314
+
315
+ group = f[group_name]
316
+
317
+ # Dictionary comprehension:
318
+ # item[()] loads the dataset into a numpy array
319
+ rho_dict = {
320
+ key: item[()]
321
+ for key, item in group.items()
322
+ if isinstance(item, h5py.Dataset)
323
+ }
324
+
325
+ imt_dict = {
326
+ key: json.loads(item.attrs["imts"])
327
+ for key, item in group.items()
328
+ if isinstance(item, h5py.Dataset)
329
+ }
330
+
331
+ return rho_dict, list(imt_dict.values())[0] # assuming the same imts for all datasets in the group
332
+
333
+
334
+ class ClemettCorrelationModelAsc(EmpiricalCorrelationModel):
335
+ """
336
+ Correlation model for the active shallow crust regions in Europe
337
+ Clemett and Gündel (2026) - Paper in preparation
338
+ """
339
+ def __init__(self,
340
+ incl_nonSA_imts: list[str]|None=None,
341
+ incl_SA_Ts: list[float]|None=None):
342
+
343
+ self.model = "Clemett26ASC"
344
+
345
+ fp = CORR_COEFFS_FOLDER / "clemett_corr_coeffs.hdf5"
346
+ rho_arrays, rho_periods = load_corr_coeff_data_from_hdf5(fp, "asc")
347
+ super().__init__(rho_arrays, rho_periods,
348
+ incl_nonSA_imts, incl_SA_Ts)
349
+
350
+
351
+ class ClemettCorrelationModelSInter(EmpiricalCorrelationModel):
352
+ """
353
+ Correlation model for subduction interface regions in Europe
354
+ Clemett and Gündel (2026) - Paper in preparation
355
+ """
356
+ def __init__(self,
357
+ incl_nonSA_imts: list[str]|None=None,
358
+ incl_SA_Ts: list[float]|None=None):
359
+
360
+ self.model = "Clemett26SInter"
361
+
362
+ fp = CORR_COEFFS_FOLDER / "clemett_corr_coeffs.hdf5"
363
+ rho_arrays, rho_periods = load_corr_coeff_data_from_hdf5(fp, "sinter")
364
+ super().__init__(rho_arrays, rho_periods,
365
+ incl_nonSA_imts, incl_SA_Ts)
366
+
367
+
368
+ class ClemettCorrelationModelSSlab(EmpiricalCorrelationModel):
369
+ """
370
+ Correlation model for the subduction slab regions in Europe
371
+ Clemett and Gündel (2026) - Paper in preparation
372
+ """
373
+ def __init__(self,
374
+ incl_nonSA_imts: list[str]|None=None,
375
+ incl_SA_Ts: list[float]|None=None):
376
+
377
+ self.model = "Clemett26SSlab"
378
+
379
+ fp = CORR_COEFFS_FOLDER / "clemett_corr_coeffs.hdf5"
380
+ rho_arrays, rho_periods = load_corr_coeff_data_from_hdf5(fp, "sslab")
381
+ super().__init__(rho_arrays, rho_periods,
382
+ incl_nonSA_imts, incl_SA_Ts)
383
+
384
+
385
+ class ClemettCorrelationModelVrancea(EmpiricalCorrelationModel):
386
+ """
387
+ Correlation model for the Vrancea deep seismicity region in Europe
388
+ Clemett and Gündel (2026) - Paper in preparation
389
+ """
390
+ def __init__(self,
391
+ incl_nonSA_imts: list[str]|None=None,
392
+ incl_SA_Ts: list[float]|None=None):
393
+
394
+ self.model = "Clemett26Vrancea"
395
+
396
+ fp = CORR_COEFFS_FOLDER / "clemett_corr_coeffs.hdf5"
397
+ rho_arrays, rho_periods = load_corr_coeff_data_from_hdf5(fp, "vrancea")
398
+ super().__init__(rho_arrays, rho_periods,
399
+ incl_nonSA_imts, incl_SA_Ts)
400
+
401
+
402
+ def conditional_correlation_matrix(
403
+ corr_df: pd.DataFrame, conditioning_imt: str) -> pd.DataFrame:
404
+
405
+ # check if conditioning imt is in the correlation matrix
406
+ if conditioning_imt not in corr_df.index:
407
+ raise ValueError(f"Conditioning IMT {conditioning_imt} is not in the "
408
+ f"correlation matrix.")
409
+
410
+ # convert to numpy array for calculations / remove conditionin imt
411
+ rho = corr_df.drop(index=conditioning_imt, columns=conditioning_imt) \
412
+ .to_numpy()
413
+
414
+ # get vector of conditional correlations between imt and conditioning imt
415
+ rho_v = corr_df.loc[conditioning_imt, :].drop(conditioning_imt) \
416
+ .to_numpy() \
417
+ .reshape(-1,1)
418
+
419
+ numerator = rho - rho_v @ rho_v.T
420
+ denominator = np.sqrt((1 - rho_v ** 2)) @ np.sqrt((1 - rho_v ** 2)).T
421
+ rho_cond = numerator / denominator
422
+
423
+ # force 1.0 on diagonal (should be the case already but just to be safe)
424
+ np.fill_diagonal(rho_cond, 1.0)
425
+
426
+ # convert back to dataframe with appropriate labels
427
+ new_index = corr_df.index.drop(conditioning_imt)
428
+ corr_cond_df = pd.DataFrame(rho_cond, index=new_index, columns=new_index)
429
+
430
+ return corr_cond_df
431
+
432
+
433
+ # register the correlation models in a dictionary for easy access
434
+ CORR_MODELS = {
435
+ "clemett_asc": ClemettCorrelationModelAsc,
436
+ "clemett_sinter": ClemettCorrelationModelSInter,
437
+ "clemett_sslab": ClemettCorrelationModelSSlab,
438
+ "clemett_vrancea": ClemettCorrelationModelVrancea,
439
+ }
440
+
441
+
442
+ if __name__ == "__main__":
443
+ from openquake.hazardlib.imt import PGA, RSD595, SA, AvgSA
444
+ sa_periods = np.round(np.logspace(np.log10(0.025), np.log10(8), num=30), 3)
445
+ nonSA_imts = [AvgSA([0,3]).string, RSD595().string, PGA().string] # strings match the correlation matrix
446
+ # cond_imt = AvgSA([0,3]) # strings match the correlation matrix
447
+ # selection_imts = [RSD595(), PGA()] + [SA(period) for period in sa_periods] # these need to be imt instances to work with GMMs
448
+
449
+ corr_model = ClemettCorrelationModelAsc(
450
+ incl_nonSA_imts=nonSA_imts,
451
+ incl_SA_Ts=sa_periods,
452
+ )
453
+
454
+ # rho_cond = conditional_correlation_matrix(corr_model.rho["total"], cond_imt.string)
455
+ pass
@@ -0,0 +1,4 @@
1
+ [diffend] Oversized file quarantined before diffing.
2
+ name: pickagm/data/ESM_flatfile_SA.csv
3
+ size: 61228757 bytes
4
+ sha256: ddef5efc9c115fea348fb7386e6dfa4e61f7b6d1f221afc769a4644ee1982f9e