Skip to content

Commit 8c3b76b

Browse files
committed
smtk cleanup
1 parent 2457312 commit 8c3b76b

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

egsim/smtk/registry.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def gsim(model: Union[str, GMPE], raise_deprecated=True) -> GMPE:
3232
if model.startswith('GMPETable'):
3333
# GMPETable. raises: TypeError, ValueError, FileNotFoundError, OSError,
3434
# AttributeError
35-
filepath = re.match(r'^GMPETable\(([^)]+?)\)$', model).\
36-
group(1).split("=")[1] # get table filename
35+
filepath = (
36+
re.match(r'^GMPETable\(([^)]+?)\)$', model).group(1).split("=")[1]
37+
)
3738
return GMPETable(gmpe_table=filepath)
3839
else:
3940
# "normal" str case, calls valid_gsim which raises:
@@ -122,9 +123,7 @@ def sa_period(obj: Union[float, str, IMT]) -> Union[float, None]:
122123
return float(period) if np.isfinite(period) else None
123124

124125

125-
def get_sa_limits(
126-
model: Union[str, GMPE]
127-
) -> Union[tuple[float, float], None]:
126+
def get_sa_limits(model: Union[str, GMPE]) -> Union[tuple[float, float], None]:
128127
"""Return the SA period limits defined for the given gsim, or None"""
129128

130129
if isinstance(model, str):

egsim/smtk/residuals.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ def get_observed_motions(flatfile: pd.DataFrame, imts: Container[str], log=True)
176176

177177

178178
def yield_event_contexts(flatfile: pd.DataFrame) -> Iterable[EventContext]:
179-
"""Group the flatfile by events, and yield `EventContext`s objects, one for each event"""
180-
179+
"""
180+
Group the flatfile by events, and yield `EventContext`s objects, one for each event
181+
"""
181182
# check event id column or use the event location to group events:
182183
# group flatfile by events. Use ev. id (_EVENT_COLUMNS[0]) or, when
183184
# no ID found, event spatio-temporal coordinates (_EVENT_COLUMNS[1:])
@@ -270,8 +271,11 @@ def get_expected_motions(
270271
columns.extend((i, Clabel.intra_ev_std, gsim_name) for i in imt_names)
271272
data.append(intra)
272273

273-
return pd.DataFrame(columns=pd.MultiIndex.from_tuples(columns),
274-
data=np.hstack(data), index=ctx.sids)
274+
return pd.DataFrame(
275+
columns=pd.MultiIndex.from_tuples(columns),
276+
data=np.hstack(data),
277+
index=ctx.sids
278+
)
275279

276280

277281
def get_residuals_from_expected_and_observed_motions(
@@ -332,8 +336,9 @@ def _get_random_effects_residuals(obs, mean, inter, intra, normalise=True):
332336
# TODO this is the only part where grouping by event is relevant: maybe
333337
# move groupby here?
334338
nvals = float(len(mean))
335-
inter_res = ((inter ** 2.) * sum(obs - mean)) /\
336-
(nvals * (inter ** 2.) + (intra ** 2.))
339+
inter_res = (
340+
((inter ** 2.) * sum(obs - mean)) / (nvals * (inter ** 2.) + (intra ** 2.))
341+
)
337342
intra_res = obs - (mean + inter_res)
338343
if normalise:
339344
return inter_res / inter, intra_res / intra
@@ -370,7 +375,8 @@ def get_residuals_likelihood(residuals: pd.DataFrame, inplace=True) -> pd.DataFr
370375

371376
def get_likelihood(values: Union[np.ndarray, pd.Series]) -> Union[np.ndarray, pd.Series]:
372377
"""
373-
Return the likelihood of the given values according to Equation 9 of Scherbaum et al. (2004)
378+
Return the likelihood of the given values according to
379+
Equation 9 of Scherbaum et al. (2004)
374380
"""
375381
zvals = np.fabs(values)
376382
return 1.0 - erf(zvals / sqrt(2.))
@@ -554,7 +560,7 @@ def get_required_ground_motion_properties(
554560
for p in required_props:
555561
try:
556562
# https://stackoverflow.com/a/29706954
557-
# `required_props_flatfile` is a dataframe with a specific index (see above).
563+
# `required_props_flatfile` is a dataframe with a specific index.
558564
# Adding a Series to it might result in NaNs where the Series index
559565
# does not match the DataFrame index. As such, assign the series.values to
560566
# the DataFrame (see test_residuals.test_assign_series to assure this is ok)

egsim/smtk/scenarios.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ class RuptureProperties)
140140

141141
# sort columns (maybe we could use reindex but let's be more explicit):
142142
computed_cols = set(output.columns)
143-
expected_cols = list(product(imts, [Clabel.median, Clabel.std], gsims)) + meta_columns
143+
expected_cols = (
144+
list(product(imts, [Clabel.median, Clabel.std], gsims)) + meta_columns
145+
)
144146
output = output[[c for c in expected_cols if c in computed_cols]].copy()
145147
if header_sep:
146148
output.columns = [header_sep.join(c) for c in output.columns]
@@ -239,7 +241,8 @@ def create_planar_surface(
239241
:param aspect: Aspect ratio of rupture
240242
:param ztor: top of rupture depth, in km
241243
242-
:return: Rupture as instance of :class:`openquake.hazardlib.geo.surface.planar.PlanarSurface`
244+
:return: Rupture as instance of
245+
:class:`openquake.hazardlib.geo.surface.planar.PlanarSurface`
243246
"""
244247
# If the top of rupture depth in the initial
245248
if fabs(top_centroid.depth - ztor) > 1E-9:

egsim/smtk/validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def init_context_maker(
165165

166166

167167
def get_ground_motion_values(
168-
model: GMPE, imts: list[IMT], ctx: np.recarray, *, model_name: Optional[str] = None
168+
model: GMPE, imts: list[IMT], ctx: np.recarray, *, model_name: Optional[str] = None
169169
):
170170
"""
171171
Compute the ground motion values from the arguments returning 4 arrays each

0 commit comments

Comments
 (0)