pfdha 0.3.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.
Files changed (143) hide show
  1. openquake/pfdha/__init__.py +31 -0
  2. openquake/pfdha/__main__.py +15 -0
  3. openquake/pfdha/applicability.py +202 -0
  4. openquake/pfdha/calc/calculators.py +660 -0
  5. openquake/pfdha/calc/config_loader.py +939 -0
  6. openquake/pfdha/calc/contexts.py +874 -0
  7. openquake/pfdha/calc/geometry/__init__.py +58 -0
  8. openquake/pfdha/calc/geometry/coordinates.py +223 -0
  9. openquake/pfdha/calc/geometry/generalized_coordinate_integral.py +100 -0
  10. openquake/pfdha/calc/geometry/physical_cache.py +101 -0
  11. openquake/pfdha/calc/geometry/physical_trace.py +468 -0
  12. openquake/pfdha/calc/geometry/reference_manifest.py +139 -0
  13. openquake/pfdha/calc/geometry/routing.py +199 -0
  14. openquake/pfdha/calc/geometry/straight_reference.py +91 -0
  15. openquake/pfdha/calc/geometry/supplied_reference.py +144 -0
  16. openquake/pfdha/calc/geometry/types.py +194 -0
  17. openquake/pfdha/calc/geometry/visini_geospatial.py +213 -0
  18. openquake/pfdha/calc/geometry/visini_slice.py +100 -0
  19. openquake/pfdha/calc/hazard.py +1306 -0
  20. openquake/pfdha/calc/location_uncertainty.py +220 -0
  21. openquake/pfdha/calc/model_adapter.py +915 -0
  22. openquake/pfdha/calc/plotting.py +381 -0
  23. openquake/pfdha/calc/utils/__init__.py +15 -0
  24. openquake/pfdha/calc/utils/geometry.py +14 -0
  25. openquake/pfdha/calc/utils/interpolation.py +292 -0
  26. openquake/pfdha/calc/utils/parsing.py +523 -0
  27. openquake/pfdha/calc/utils/probability.py +432 -0
  28. openquake/pfdha/calc/utils/rupture_distance.py +825 -0
  29. openquake/pfdha/calc/utils/support_geometry.py +191 -0
  30. openquake/pfdha/calc/utils/surface_filter.py +376 -0
  31. openquake/pfdha/cli.py +195 -0
  32. openquake/pfdha/disaggregation/__init__.py +28 -0
  33. openquake/pfdha/disaggregation/accumulator.py +920 -0
  34. openquake/pfdha/disaggregation/common.py +197 -0
  35. openquake/pfdha/disaggregation/curve_output.py +480 -0
  36. openquake/pfdha/disaggregation/dominant.py +712 -0
  37. openquake/pfdha/disaggregation/epsilon.py +123 -0
  38. openquake/pfdha/disaggregation/epsilon_outputs.py +205 -0
  39. openquake/pfdha/disaggregation/map_output.py +926 -0
  40. openquake/pfdha/displacement.py +550 -0
  41. openquake/pfdha/faulting_style.py +64 -0
  42. openquake/pfdha/forecast_diagnostics.py +194 -0
  43. openquake/pfdha/gis_adapter.py +161 -0
  44. openquake/pfdha/input_templates/source_surface_expression.template.csv +1 -0
  45. openquake/pfdha/logic_tree/__init__.py +41 -0
  46. openquake/pfdha/logic_tree/aggregation.py +487 -0
  47. openquake/pfdha/logic_tree/branch_execution.py +342 -0
  48. openquake/pfdha/logic_tree/config_builder.py +73 -0
  49. openquake/pfdha/logic_tree/coupling_rules.py +496 -0
  50. openquake/pfdha/logic_tree/curve_runner.py +458 -0
  51. openquake/pfdha/logic_tree/driver.py +736 -0
  52. openquake/pfdha/logic_tree/driver_support.py +772 -0
  53. openquake/pfdha/logic_tree/enumerator.py +97 -0
  54. openquake/pfdha/logic_tree/export.py +913 -0
  55. openquake/pfdha/logic_tree/io.py +504 -0
  56. openquake/pfdha/logic_tree/job_config.py +1417 -0
  57. openquake/pfdha/logic_tree/map_runner.py +1172 -0
  58. openquake/pfdha/logic_tree/model_metadata.yaml +286 -0
  59. openquake/pfdha/logic_tree/nrml_reader.py +89 -0
  60. openquake/pfdha/logic_tree/output_retention.py +32 -0
  61. openquake/pfdha/logic_tree/outputs.py +268 -0
  62. openquake/pfdha/logic_tree/param_parser.py +109 -0
  63. openquake/pfdha/logic_tree/provenance.py +284 -0
  64. openquake/pfdha/logic_tree/run_reports.py +314 -0
  65. openquake/pfdha/logic_tree/sampling.py +46 -0
  66. openquake/pfdha/logic_tree/sampling_workflow.py +366 -0
  67. openquake/pfdha/logic_tree/site_builder.py +399 -0
  68. openquake/pfdha/logic_tree/source_model_lt.py +464 -0
  69. openquake/pfdha/logic_tree/types.py +193 -0
  70. openquake/pfdha/logic_tree/validators.py +372 -0
  71. openquake/pfdha/logic_tree/xml_utils.py +105 -0
  72. openquake/pfdha/model_registry.py +690 -0
  73. openquake/pfdha/primary_surf_displ/__init__.py +46 -0
  74. openquake/pfdha/primary_surf_displ/_beta_md.py +51 -0
  75. openquake/pfdha/primary_surf_displ/base.py +101 -0
  76. openquake/pfdha/primary_surf_displ/chiou_et_al_2025_fd.py +236 -0
  77. openquake/pfdha/primary_surf_displ/kuehn_coeffs.npz +0 -0
  78. openquake/pfdha/primary_surf_displ/kuehn_et_al_2024_fd.py +363 -0
  79. openquake/pfdha/primary_surf_displ/lavrentiadis_abrahamson_2023_fd.py +437 -0
  80. openquake/pfdha/primary_surf_displ/moss_et_al_2024_fd.py +300 -0
  81. openquake/pfdha/primary_surf_displ/moss_ross_2011_fd.py +262 -0
  82. openquake/pfdha/primary_surf_displ/petersen_et_al_2011_fd.py +246 -0
  83. openquake/pfdha/primary_surf_displ/quigley_et_al_2025_fd.py +256 -0
  84. openquake/pfdha/primary_surf_displ/takao_et_al_2013_fd.py +369 -0
  85. openquake/pfdha/primary_surf_displ/todorovska_et_al_2007_fd.py +322 -0
  86. openquake/pfdha/primary_surf_displ/vertical_component_fd.py +215 -0
  87. openquake/pfdha/primary_surf_displ/youngs_et_al_2003_fd.py +239 -0
  88. openquake/pfdha/primary_surf_rup/__init__.py +44 -0
  89. openquake/pfdha/primary_surf_rup/base.py +62 -0
  90. openquake/pfdha/primary_surf_rup/fixed_sr.py +83 -0
  91. openquake/pfdha/primary_surf_rup/mammarella_et_al_2024_sr.py +454 -0
  92. openquake/pfdha/primary_surf_rup/moss_et_al_2013_sr.py +89 -0
  93. openquake/pfdha/primary_surf_rup/moss_ross_2011_sr.py +43 -0
  94. openquake/pfdha/primary_surf_rup/pizza_et_al_2023_sr.py +47 -0
  95. openquake/pfdha/primary_surf_rup/takao_et_al_2013_sr.py +37 -0
  96. openquake/pfdha/primary_surf_rup/wells_coppersmith_1993_sr.py +38 -0
  97. openquake/pfdha/primary_surf_rup/yang_et_al_2021_sr.py +69 -0
  98. openquake/pfdha/primary_surf_rup/youngs_et_al_2003_sr.py +59 -0
  99. openquake/pfdha/renewal.py +165 -0
  100. openquake/pfdha/scalerel/__init__.py +47 -0
  101. openquake/pfdha/scalerel/base.py +259 -0
  102. openquake/pfdha/scalerel/leonard2010.py +152 -0
  103. openquake/pfdha/scalerel/leonard2014.py +268 -0
  104. openquake/pfdha/scalerel/thingbaijam2017.py +167 -0
  105. openquake/pfdha/scalerel/wc1994.py +305 -0
  106. openquake/pfdha/secondary_surf_displ/__init__.py +42 -0
  107. openquake/pfdha/secondary_surf_displ/mea22_workflow_sfd.py +116 -0
  108. openquake/pfdha/secondary_surf_displ/moss_et_al_2024_sfd.py +273 -0
  109. openquake/pfdha/secondary_surf_displ/nurminen_et_al_2020_sfd.py +463 -0
  110. openquake/pfdha/secondary_surf_displ/petersen_et_al_2011_sfd.py +207 -0
  111. openquake/pfdha/secondary_surf_displ/rodriguezpadilla_oskin_2023_sfd.py +91 -0
  112. openquake/pfdha/secondary_surf_displ/takao_et_al_2013_sfd.py +296 -0
  113. openquake/pfdha/secondary_surf_displ/takao_et_al_2018_sfd.py +27 -0
  114. openquake/pfdha/secondary_surf_displ/visini.py +1223 -0
  115. openquake/pfdha/secondary_surf_displ/visini_et_al_2025_sfd.py +379 -0
  116. openquake/pfdha/secondary_surf_displ/youngs_et_al_2003_sfd.py +283 -0
  117. openquake/pfdha/secondary_surf_rup/__init__.py +47 -0
  118. openquake/pfdha/secondary_surf_rup/base.py +60 -0
  119. openquake/pfdha/secondary_surf_rup/ferrario_livio_2021_ssr.py +140 -0
  120. openquake/pfdha/secondary_surf_rup/fixed_ssr.py +85 -0
  121. openquake/pfdha/secondary_surf_rup/mea22_support_transfer_ssr.py +193 -0
  122. openquake/pfdha/secondary_surf_rup/moss_et_al_2024_ssr.py +232 -0
  123. openquake/pfdha/secondary_surf_rup/nurminen_et_al_2020_ssr.py +734 -0
  124. openquake/pfdha/secondary_surf_rup/petersen_et_al_2011_ssr.py +204 -0
  125. openquake/pfdha/secondary_surf_rup/rodriguezpadilla_oskin_2023_ssr.py +152 -0
  126. openquake/pfdha/secondary_surf_rup/takao_et_al_2013_ssr.py +80 -0
  127. openquake/pfdha/secondary_surf_rup/takao_et_al_2018_ssr.py +92 -0
  128. openquake/pfdha/secondary_surf_rup/visini_et_al_2025_ssr.py +1087 -0
  129. openquake/pfdha/secondary_surf_rup/youngs_et_al_2003_ssr.py +133 -0
  130. openquake/pfdha/source_builder.py +491 -0
  131. openquake/pfdha/temporal.py +112 -0
  132. openquake/pfdha/temporal_disaggregation.py +419 -0
  133. openquake/pfdha/temporal_driver.py +337 -0
  134. openquake/pfdha/temporal_hazard.py +411 -0
  135. openquake/pfdha/temporal_maps.py +95 -0
  136. openquake/pfdha/temporal_storage.py +130 -0
  137. pfdha-0.3.0.dist-info/METADATA +247 -0
  138. pfdha-0.3.0.dist-info/RECORD +143 -0
  139. pfdha-0.3.0.dist-info/WHEEL +5 -0
  140. pfdha-0.3.0.dist-info/entry_points.txt +2 -0
  141. pfdha-0.3.0.dist-info/licenses/LICENSE +661 -0
  142. pfdha-0.3.0.dist-info/licenses/NOTICE +41 -0
  143. pfdha-0.3.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,31 @@
1
+ """PFDHA package metadata."""
2
+
3
+ from pathlib import Path
4
+
5
+
6
+ def _source_tree_version():
7
+ """Read the source-tree version when pyproject.toml is available.
8
+
9
+ This avoids a stale hard-coded fallback and also prevents an older globally
10
+ installed ``pfdha`` distribution from masking the version of the source tree
11
+ currently being executed.
12
+ """
13
+ pyproject = Path(__file__).resolve().parents[2] / "pyproject.toml"
14
+ if not pyproject.is_file():
15
+ return None
16
+ try:
17
+ import tomllib
18
+ with pyproject.open("rb") as f:
19
+ data = tomllib.load(f)
20
+ return str(data["project"]["version"])
21
+ except Exception:
22
+ return None
23
+
24
+
25
+ __version__ = _source_tree_version()
26
+ if __version__ is None:
27
+ try:
28
+ from importlib.metadata import version as _dist_version
29
+ __version__ = _dist_version("pfdha")
30
+ except Exception: # uninstalled source fragment / unusual embedding
31
+ __version__ = "0+unknown"
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Allow ``python -m openquake.pfdha`` to invoke the CLI.
4
+
5
+ Usage::
6
+
7
+ python -m openquake.pfdha run job.ini
8
+ python -m openquake.pfdha validate job.ini
9
+ """
10
+ import sys
11
+
12
+ from openquake.pfdha.cli import main
13
+
14
+ if __name__ == "__main__":
15
+ sys.exit(main())
@@ -0,0 +1,202 @@
1
+ """Non-intrusive model-domain reporting, counted per rupture-site evaluation."""
2
+ from __future__ import annotations
3
+
4
+ from dataclasses import asdict, dataclass
5
+ import json
6
+ from typing import Iterable
7
+
8
+ import numpy as np
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class ApplicabilityRule:
13
+ metric: str
14
+ units: str
15
+ lower: float | None
16
+ upper: float | None
17
+ kind: str
18
+ reference: str | None
19
+ faulting_style: str | None = None
20
+
21
+ def __post_init__(self):
22
+ units = {"mag": "Mw", "r": "km", "rx": "km", "L": "km", "x_L": "ratio"}
23
+ if units.get(self.metric) != self.units:
24
+ raise ValueError("Applicability metric and units do not match")
25
+ if self.kind not in {"calibration", "scientific_support", "computational"}:
26
+ raise ValueError("Unknown applicability rule kind")
27
+ bounds = [v for v in (self.lower, self.upper) if v is not None]
28
+ if any(not np.isfinite(v) for v in bounds):
29
+ raise ValueError("Applicability bounds must be finite")
30
+ if self.lower is not None and self.upper is not None and self.lower > self.upper:
31
+ raise ValueError("Applicability lower bound exceeds upper bound")
32
+ if self.faulting_style not in {None, "normal", "reverse", "strike-slip"}:
33
+ raise ValueError("Unknown applicability faulting style")
34
+ if bounds and not self.reference:
35
+ raise ValueError("Known applicability bounds require a reference")
36
+
37
+
38
+ def _record_key(row):
39
+ return json.dumps({key: row[key] for key in
40
+ ("model", "domain", "source_scope", "branch_id", "rule")},
41
+ sort_keys=True, separators=(",", ":"))
42
+
43
+
44
+ def merge_applicability_summaries(summaries: Iterable[dict | None]) -> dict:
45
+ records = {}
46
+ for summary in summaries:
47
+ for row in (summary or {}).get("records", []):
48
+ key = _record_key(row)
49
+ if key not in records:
50
+ records[key] = dict(row)
51
+ continue
52
+ target = records[key]
53
+ for count in ("evaluated", "active", "outside_evaluated", "outside_active",
54
+ "nonfinite_evaluated", "nonfinite_active"):
55
+ target[count] = target.get(count, 0) + row.get(count, 0)
56
+ for field, reducer in (("minimum", min), ("maximum", max)):
57
+ values = [v for v in (target[field], row[field]) if v is not None]
58
+ target[field] = reducer(values) if values else None
59
+ rows = [records[key] for key in sorted(records)]
60
+ status = "reported" if rows else "not_evaluated"
61
+ if any(row["coverage"] != "complete" for row in rows):
62
+ status = "unknown"
63
+ if any(row["outside_active"] or row.get("nonfinite_evaluated", 0) for row in rows):
64
+ status = "warning"
65
+ return {"schema_version": 1, "status": status,
66
+ "count_unit": "rupture_site_evaluation",
67
+ "weighting": "unweighted; source-model alternatives remain separate",
68
+ "active_semantics": "positive occurrence rate, domain allocation, and available conditioning gates; exceedance tails are not eligibility gates",
69
+ "coverage_counts": {coverage: sum(row["coverage"] == coverage for row in rows)
70
+ for coverage in ("complete", "partial", "unknown")},
71
+ "records": rows}
72
+
73
+
74
+ class ApplicabilityTracker:
75
+ def __init__(self):
76
+ self._records = {}
77
+
78
+ def observe(self, *, model, domain, source_scope, branch_id, rule, values, active_mask):
79
+ values = np.asarray(values, dtype=float)
80
+ active = np.asarray(active_mask, dtype=bool)
81
+ if values.ndim != 1 or values.shape != active.shape:
82
+ raise ValueError("Applicability values and active mask must have the same site shape")
83
+ finite = np.isfinite(values)
84
+ outside = np.zeros(values.shape, dtype=bool)
85
+ if rule.lower is not None:
86
+ outside |= values < rule.lower
87
+ if rule.upper is not None:
88
+ outside |= values > rule.upper
89
+ row = {"model": model, "domain": domain, "source_scope": source_scope,
90
+ "branch_id": branch_id, "rule": asdict(rule),
91
+ "coverage": ("unknown" if rule.lower is None and rule.upper is None else
92
+ "partial" if rule.lower is None or rule.upper is None else "complete"),
93
+ "evaluated": int(values.size), "active": int(active.sum()),
94
+ "outside_evaluated": int((finite & outside).sum()),
95
+ "outside_active": int((active & finite & outside).sum()),
96
+ "nonfinite_evaluated": int((~finite).sum()),
97
+ "nonfinite_active": int((active & ~finite).sum()),
98
+ "minimum": float(values[finite].min()) if finite.any() else None,
99
+ "maximum": float(values[finite].max()) if finite.any() else None}
100
+ key = _record_key(row)
101
+ if key in self._records:
102
+ row = merge_applicability_summaries([
103
+ {"records": [self._records[key]]}, {"records": [row]}])["records"][0]
104
+ self._records[key] = row
105
+
106
+ def summary(self):
107
+ return merge_applicability_summaries([{"records": list(self._records.values())}])
108
+
109
+
110
+ def model_rules(model_name, *, is_fd, model_params=None):
111
+ """Use traceable model-specific evidence; absent bounds remain unknown."""
112
+ known = []
113
+ if is_fd:
114
+ from openquake.pfdha.displacement import get_fd_model_metadata
115
+ try:
116
+ metadata = get_fd_model_metadata(model_name)
117
+ except ValueError:
118
+ # Lightweight adapters can be exercised independently of the strict
119
+ # displacement configuration. Reporting cannot invent their bounds.
120
+ metadata = {}
121
+ known = metadata.get("applicability", {}).get("rules", [])
122
+ rules = [ApplicabilityRule(**rule) for rule in known]
123
+ if model_name in {"VisiniEtAl2025SecondarySR", "VisiniEtAl2025SecondaryFD"}:
124
+ # The authors explicitly identify these mechanism-specific ranges
125
+ # as the applicability of their equations, not a hard exclusion.
126
+ for style, lower, upper in (("normal", 5.6, 7.5), ("reverse", 4.9, 7.9)):
127
+ rules.append(ApplicabilityRule(
128
+ "mag", "Mw", lower, upper, "scientific_support",
129
+ "Visini et al. (2025), Data and surface rupture ranking, p. 4 and Table 1",
130
+ faulting_style=style,
131
+ ))
132
+ if model_name in {"NurminenEtAl2020SecondarySR", "NurminenEtAl2020SecondaryFD"}:
133
+ reference = ("Nurminen et al. (2020), Frontiers in Earth Science 8:581605, "
134
+ "Table 1 and Analysis of the Distributed Rupture Displacements, "
135
+ "doi:10.3389/feart.2020.581605")
136
+ rules.append(ApplicabilityRule("mag", "Mw", 4.9, 7.9, "calibration", reference))
137
+ if model_name == "PizzaEtAl2023PrimarySR":
138
+ # Table 2 describes the fitted dataset ranges, not numerical limits.
139
+ reference = "Pizza et al. (2023), BSSA 113, Table 2, p. 2113, doi:10.1785/0120230019"
140
+ from openquake.pfdha.faulting_style import resolve_style
141
+
142
+ requested = (model_params or {}).get("style")
143
+ if resolve_style(requested) == "all":
144
+ rules.append(ApplicabilityRule("mag", "Mw", 5.5, 7.9, "calibration", reference))
145
+ else:
146
+ for style, upper in (("normal", 7.1), ("reverse", 7.9), ("strike-slip", 7.8)):
147
+ rules.append(ApplicabilityRule(
148
+ "mag", "Mw", 5.5, upper, "calibration", reference,
149
+ faulting_style=style,
150
+ ))
151
+ if model_name == "YangEtAl2021PrimarySR":
152
+ rules.append(ApplicabilityRule(
153
+ "mag", "Mw", 4.0, 6.6, "scientific_support",
154
+ "Yang et al. (2021), GSA Bulletin 133, Fig. 11A, p. 1926, doi:10.1130/B35809.1",
155
+ ))
156
+ if model_name == "MossRoss2011PrimarySR":
157
+ rules.append(ApplicabilityRule(
158
+ "mag", "Mw", 5.5, 8.0, "scientific_support",
159
+ "Moss and Ross (2011), Fig. 1, p. 1546; see also Yang et al. (2021), Fig. 11A",
160
+ ))
161
+ if not any(rule.metric == "mag" and
162
+ (rule.lower is not None or rule.upper is not None) for rule in rules):
163
+ rules.insert(0, ApplicabilityRule("mag", "Mw", None, None, "calibration", None))
164
+ return rules
165
+
166
+
167
+ def observe_context(calculator, adapters, ctx, source_id, *, primary_mask, distributed_mask,
168
+ primary_gate, secondary_gate=None):
169
+ """Observe already-built contexts without invoking probability models again."""
170
+ tracker = getattr(calculator, "_applicability_tracker", None)
171
+ if tracker is None:
172
+ return
173
+ scope = getattr(calculator, "config", {}).get("reporting_scope", {})
174
+ source_scope = json.dumps([scope.get("source_model_branch_id", "sm0"),
175
+ scope.get("source_group"), str(source_id)], separators=(",", ":"))
176
+ positive_rate = np.asarray(ctx.occurrence_rate) > 0
177
+ for slot, adapter in adapters.items():
178
+ if slot not in {"primary_sr", "primary_fd", "secondary_sr", "secondary_fd"}:
179
+ continue
180
+ principal = slot.startswith("primary")
181
+ mask = primary_mask if principal else distributed_mask
182
+ if slot == "primary_sr":
183
+ # Principal surface rupture also conditions distributed hazard.
184
+ mask = primary_mask | distributed_mask
185
+ active = positive_rate & mask
186
+ if slot != "primary_sr":
187
+ active &= np.asarray(primary_gate) > 0
188
+ if slot == "secondary_fd" and secondary_gate is not None:
189
+ active &= np.asarray(secondary_gate) > 0
190
+ for rule in calculator._applicability_rules[slot]:
191
+ values = getattr(ctx, rule.metric)
192
+ rule_active = active
193
+ if rule.faulting_style is not None:
194
+ selected = np.asarray(ctx.style) == rule.faulting_style
195
+ if not np.any(selected):
196
+ continue
197
+ values = np.asarray(values)[selected]
198
+ rule_active = active[selected]
199
+ tracker.observe(model=adapter.model.__class__.__name__,
200
+ domain="principal" if principal else "distributed",
201
+ source_scope=source_scope, branch_id=scope.get("branch_id", "direct"),
202
+ rule=rule, values=values, active_mask=rule_active)