multipolePDM 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.
Files changed (43) hide show
  1. multipolepdm/__init__.py +3 -0
  2. multipolepdm/__main__.py +32 -0
  3. multipolepdm/cli.py +127 -0
  4. multipolepdm/config/common.py +7 -0
  5. multipolepdm/config/p_ado.py +3 -0
  6. multipolepdm/config/p_dco.py +10 -0
  7. multipolepdm/config/pdco.py +13 -0
  8. multipolepdm/contracts.py +222 -0
  9. multipolepdm/examples/README.md +56 -0
  10. multipolepdm/examples/example.json +45 -0
  11. multipolepdm/examples/example_PDCO.json +55 -0
  12. multipolepdm/examples/example_P_DCO.json +55 -0
  13. multipolepdm/examples/example_batch.json +159 -0
  14. multipolepdm/gui/results.py +191 -0
  15. multipolepdm/gui/self_test.py +89 -0
  16. multipolepdm/gui/window.py +560 -0
  17. multipolepdm/io/export.py +255 -0
  18. multipolepdm/io/input.py +115 -0
  19. multipolepdm/jobs/manager.py +186 -0
  20. multipolepdm/jobs/workers.py +26 -0
  21. multipolepdm/models/p_ado.py +5 -0
  22. multipolepdm/models/p_dco.py +5 -0
  23. multipolepdm/models/pdco.py +5 -0
  24. multipolepdm/models/registry.py +88 -0
  25. multipolepdm/pdm/mapping.py +200 -0
  26. multipolepdm/pdm/observations.py +63 -0
  27. multipolepdm/pdm/statistics.py +229 -0
  28. multipolepdm/physics/amplitudes.py +99 -0
  29. multipolepdm/physics/joint.py +131 -0
  30. multipolepdm/physics/populations.py +52 -0
  31. multipolepdm/physics/propagation.py +43 -0
  32. multipolepdm/physics/symmetries.py +34 -0
  33. multipolepdm/plotting/__init__.py +16 -0
  34. multipolepdm/plotting/export.py +86 -0
  35. multipolepdm/plotting/intervals.py +199 -0
  36. multipolepdm/plotting/overview.py +229 -0
  37. multipolepdm/plotting/style.py +94 -0
  38. multipolepdm-0.1.0.dist-info/METADATA +40 -0
  39. multipolepdm-0.1.0.dist-info/RECORD +43 -0
  40. multipolepdm-0.1.0.dist-info/WHEEL +5 -0
  41. multipolepdm-0.1.0.dist-info/entry_points.txt +3 -0
  42. multipolepdm-0.1.0.dist-info/licenses/LICENSE +21 -0
  43. multipolepdm-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,255 @@
1
+ """Portable output snapshots; large text export is optional and streamed."""
2
+
3
+ from dataclasses import asdict
4
+ from datetime import datetime, timezone
5
+ import hashlib
6
+ from importlib.metadata import version
7
+ import json
8
+ from pathlib import Path
9
+ import re
10
+ import uuid
11
+ import sys
12
+
13
+ import numpy as np
14
+
15
+ from multipolepdm import __version__
16
+ from multipolepdm.contracts import (
17
+ Interval,
18
+ MappingResult,
19
+ MarginalSummary,
20
+ ResponseGrid,
21
+ Summary,
22
+ )
23
+ from multipolepdm.io.input import job_from_dict, job_to_dict
24
+ from multipolepdm.pdm.observations import observation_mesh
25
+
26
+
27
+ def reserve_result_directory(job):
28
+ label = re.sub(r"[^A-Za-z0-9_.-]+", "-", job.label).strip(".-")[:60] or "transition"
29
+ stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%S")
30
+ return Path(job.output.directory) / f"{stamp}-{label}-{uuid.uuid4().hex[:10]}"
31
+
32
+
33
+ def result_directory(job):
34
+ path = reserve_result_directory(job).expanduser()
35
+ path.mkdir(parents=True, exist_ok=False)
36
+ return path
37
+
38
+
39
+ def _jsonable(value):
40
+ if isinstance(value, np.ndarray):
41
+ return value.tolist()
42
+ if isinstance(value, np.generic):
43
+ return value.item()
44
+ raise TypeError(type(value).__name__)
45
+
46
+
47
+ def export_result(
48
+ result, summary, directory, cancelled=lambda: False, progress=lambda value: None
49
+ ):
50
+ """Write the snapshot and return the terminal status recorded on disk."""
51
+ directory = Path(directory)
52
+ directory.mkdir(parents=True, exist_ok=True)
53
+ r = result.response
54
+ package_root = Path(__file__).resolve().parents[1]
55
+ code_digest = hashlib.sha256()
56
+ for source in sorted(package_root.rglob("*.py")):
57
+ code_digest.update(source.relative_to(package_root).as_posix().encode())
58
+ code_digest.update(source.read_bytes())
59
+ manifest = {
60
+ "schema": "multipolepdm-result-v1",
61
+ "version": __version__,
62
+ "implementation_sha256": code_digest.hexdigest(),
63
+ "runtime": {
64
+ "python": sys.version.split()[0],
65
+ **{
66
+ name: version(name)
67
+ for name in ("numpy", "scipy", "sympy", "matplotlib")
68
+ },
69
+ },
70
+ "status": "saving",
71
+ "input": job_to_dict(result.job),
72
+ "response_metadata": r.metadata,
73
+ "normalization": result.normalization,
74
+ "warnings": list(result.warnings),
75
+ "reference_spin": r.reference_spin,
76
+ "ratio_name": r.ratio_name,
77
+ "units": {
78
+ "xi_rad": "rad",
79
+ "sigma": "hbar units",
80
+ "sigma_over_i": "dimensionless",
81
+ },
82
+ "grid_interpretation": "Pointwise density samples on the native xi/sigma grid, not histogram heights. For probability use parameter_node_probability or native trapezoidal weights. Coarse tan(xi) resampling is not mass preserving; use marginal_delta.csv for the refined delta PDF.",
83
+ "independent_observation_errors": True,
84
+ }
85
+
86
+ def record(status, error=None):
87
+ manifest["status"] = status
88
+ if error is not None:
89
+ manifest["error"] = str(error)
90
+ (directory / "manifest.json").write_text(
91
+ json.dumps(manifest, indent=2, allow_nan=False) + "\n", encoding="utf-8"
92
+ )
93
+
94
+ record("saving")
95
+ try:
96
+ if cancelled():
97
+ record("cancelled")
98
+ return "cancelled"
99
+ observed_p, observed_ratio, observed_pdf = observation_mesh(
100
+ result.job.polarization, result.job.angular_ratio
101
+ )
102
+ np.savez_compressed(
103
+ directory / "density.npz",
104
+ xi_rad=r.xi_rad,
105
+ sigma=r.sigma,
106
+ polarization=r.polarization,
107
+ angular_ratio=r.angular_ratio,
108
+ valid=r.valid,
109
+ observation_density=result.observation_density,
110
+ jacobian=result.jacobian,
111
+ parameter_density=result.parameter_density,
112
+ observed_p=observed_p,
113
+ observed_ratio=observed_ratio,
114
+ observed_pdf=observed_pdf,
115
+ )
116
+ (directory / "summary.json").write_text(
117
+ json.dumps(asdict(summary), default=_jsonable, indent=2, allow_nan=False)
118
+ + "\n",
119
+ encoding="utf-8",
120
+ )
121
+ for name, marginal in (
122
+ ("delta", summary.delta),
123
+ ("sigma_over_i", summary.sigma_over_i),
124
+ ):
125
+ np.savetxt(
126
+ directory / f"marginal_{name}.csv",
127
+ np.column_stack((marginal.coordinate, marginal.density)),
128
+ delimiter=",",
129
+ header=f"{name},probability_density_per_{name}",
130
+ comments="",
131
+ )
132
+ progress(25)
133
+ if cancelled():
134
+ record("cancelled")
135
+ return "cancelled"
136
+ if result.job.output.full_csv:
137
+ ratio_mesh, p_mesh = np.meshgrid(observed_ratio, observed_p)
138
+ np.savetxt(
139
+ directory / "observation_density.csv",
140
+ np.column_stack(
141
+ (ratio_mesh.ravel(), p_mesh.ravel(), observed_pdf.ravel())
142
+ ),
143
+ delimiter=",",
144
+ header=f"{r.ratio_name},polarization_P,observation_density",
145
+ comments="",
146
+ )
147
+ with (directory / "parameter_density.csv").open(
148
+ "w", encoding="utf-8"
149
+ ) as stream:
150
+ stream.write(
151
+ f"mixing_angle_xi_rad,mixing_ratio_delta,alignment_sigma,alignment_sigma_over_i,polarization_P,{r.ratio_name},observation_density,jacobian_abs_xi_sigma,parameter_density_per_rad_sigma,parameter_density_per_delta_sigma,parameter_density_per_rad_sigma_over_i,native_quadrature_weight,parameter_node_probability\n"
152
+ )
153
+
154
+ def weights(axis):
155
+ widths = np.diff(axis)
156
+ return np.concatenate(
157
+ (
158
+ [widths[0] / 2],
159
+ (widths[:-1] + widths[1:]) / 2,
160
+ [widths[-1] / 2],
161
+ )
162
+ )
163
+
164
+ wx, ws = weights(r.xi_rad), weights(r.sigma)
165
+ for n, sigma in enumerate(r.sigma):
166
+ if cancelled():
167
+ break
168
+ delta = np.tan(r.xi_rad)
169
+ weight = wx * ws[n]
170
+ columns = (
171
+ r.xi_rad,
172
+ delta,
173
+ np.full(delta.shape, sigma),
174
+ np.full(delta.shape, sigma / r.reference_spin),
175
+ r.polarization[n],
176
+ r.angular_ratio[n],
177
+ result.observation_density[n],
178
+ result.jacobian[n],
179
+ result.parameter_density[n],
180
+ result.parameter_density[n] / (1 + delta**2),
181
+ r.reference_spin * result.parameter_density[n],
182
+ weight,
183
+ result.parameter_density[n] * weight,
184
+ )
185
+ np.savetxt(
186
+ stream, np.column_stack(columns), delimiter=",", fmt="%.12g"
187
+ )
188
+ progress(25 + 74 * (n + 1) / len(r.sigma))
189
+ if cancelled():
190
+ record("cancelled")
191
+ return "cancelled"
192
+ record("saved")
193
+ except Exception as exc:
194
+ record("save_failed", exc)
195
+ raise
196
+ progress(100)
197
+ return "saved"
198
+
199
+
200
+ def load_result(directory, allow_partial=False):
201
+ directory = Path(directory)
202
+ meta = json.loads((directory / "manifest.json").read_text(encoding="utf-8"))
203
+ if meta["schema"] != "multipolepdm-result-v1":
204
+ raise ValueError("Unsupported output schema.")
205
+ if meta["status"] != "saved" and not allow_partial:
206
+ raise ValueError(
207
+ "Output is not fully saved. Use allow_partial=True only to recover a complete snapshot knowingly."
208
+ )
209
+ with np.load(directory / "density.npz", allow_pickle=False) as data:
210
+ r = ResponseGrid(
211
+ data["xi_rad"],
212
+ data["sigma"],
213
+ data["polarization"],
214
+ data["angular_ratio"],
215
+ data["valid"],
216
+ meta["reference_spin"],
217
+ meta["ratio_name"],
218
+ meta["response_metadata"],
219
+ )
220
+ return MappingResult(
221
+ job_from_dict(meta["input"]),
222
+ r,
223
+ data["observation_density"],
224
+ data["jacobian"],
225
+ data["parameter_density"],
226
+ meta["normalization"],
227
+ tuple(meta["warnings"]),
228
+ )
229
+
230
+
231
+ def load_snapshot(directory):
232
+ directory = Path(directory)
233
+ result = load_result(directory)
234
+ summary = json.loads((directory / "summary.json").read_text(encoding="utf-8"))
235
+
236
+ def marginal(name):
237
+ entry = summary[name]
238
+ intervals = tuple(
239
+ Interval(
240
+ interval["mass"],
241
+ tuple(interval["eti"]),
242
+ tuple(tuple(pair) for pair in interval["hdi"]),
243
+ interval["hdi_threshold"],
244
+ )
245
+ for interval in entry["intervals"]
246
+ )
247
+ return MarginalSummary(
248
+ np.asarray(entry["coordinate"], dtype=float),
249
+ np.asarray(entry["density"], dtype=float),
250
+ entry["median"],
251
+ entry["mode"],
252
+ intervals,
253
+ )
254
+
255
+ return result, Summary(marginal("delta"), marginal("sigma_over_i"))
@@ -0,0 +1,115 @@
1
+ """JSON-only input, shared by the GUI, CLI and notebooks."""
2
+
3
+ from dataclasses import asdict
4
+ from importlib import resources
5
+ import json
6
+ from pathlib import Path
7
+
8
+ from multipolepdm.contracts import (
9
+ Geometry,
10
+ GridSpec,
11
+ JobRequest,
12
+ Measurement,
13
+ OutputSpec,
14
+ Transition,
15
+ )
16
+
17
+
18
+ def _transition(data):
19
+ return Transition(**data)
20
+
21
+
22
+ def job_from_dict(data):
23
+ allowed = {
24
+ "mode",
25
+ "label",
26
+ "target",
27
+ "gate",
28
+ "target_first",
29
+ "intermediates",
30
+ "observations",
31
+ "geometry",
32
+ "grid",
33
+ "output",
34
+ }
35
+ unknown = set(data) - allowed
36
+ if unknown:
37
+ raise ValueError(f"Unknown input fields: {', '.join(sorted(unknown))}")
38
+ mode = data["mode"]
39
+ ratio = "R_ADO" if mode == "P_ADO" else "R_DCO"
40
+ observations = data["observations"]
41
+ if set(observations) != {"P", ratio}:
42
+ raise ValueError(f"{mode} requires observations P and {ratio}.")
43
+ return JobRequest(
44
+ mode=mode,
45
+ target=_transition(data["target"]),
46
+ polarization=Measurement(**observations["P"]),
47
+ angular_ratio=Measurement(**observations[ratio]),
48
+ label=str(data.get("label", "Transition")),
49
+ gate=_transition(data["gate"]) if data.get("gate") is not None else None,
50
+ target_first=data.get("target_first", True),
51
+ intermediates=tuple(_transition(t) for t in data.get("intermediates", [])),
52
+ geometry=Geometry(**data.get("geometry", {})),
53
+ grid=GridSpec(**data.get("grid", {})),
54
+ output=OutputSpec(**data.get("output", {})),
55
+ )
56
+
57
+
58
+ def job_to_dict(job):
59
+ data = {
60
+ "mode": job.mode,
61
+ "label": job.label,
62
+ "target": asdict(job.target),
63
+ "observations": {
64
+ "P": asdict(job.polarization),
65
+ job.ratio_name: asdict(job.angular_ratio),
66
+ },
67
+ "geometry": asdict(job.geometry),
68
+ "grid": asdict(job.grid),
69
+ "output": asdict(job.output),
70
+ }
71
+ data["target"].pop("delta")
72
+ if job.mode != "P_ADO":
73
+ data.update(
74
+ gate=asdict(job.gate),
75
+ target_first=job.target_first,
76
+ intermediates=[asdict(t) for t in job.intermediates],
77
+ )
78
+ return data
79
+
80
+
81
+ def load_jobs(path):
82
+ data = json.loads(Path(path).read_text(encoding="utf-8"))
83
+ if isinstance(data, dict) and set(data) == {"jobs"}:
84
+ records = data["jobs"]
85
+ if not isinstance(records, list) or not records:
86
+ raise ValueError("Batch jobs must be a nonempty array.")
87
+ else:
88
+ records = [data]
89
+ return tuple(job_from_dict(record) for record in records)
90
+
91
+
92
+ def save_input(job, path):
93
+ Path(path).write_text(
94
+ json.dumps(job_to_dict(job), indent=2, allow_nan=False) + "\n", encoding="utf-8"
95
+ )
96
+
97
+
98
+ def example_data(mode="P_ADO"):
99
+ name = {
100
+ "P_ADO": "example.json",
101
+ "P_DCO": "example_P_DCO.json",
102
+ "PDCO": "example_PDCO.json",
103
+ "batch": "example_batch.json",
104
+ }[mode]
105
+ return json.loads(
106
+ resources.files("multipolepdm")
107
+ .joinpath("examples", name)
108
+ .read_text(encoding="utf-8")
109
+ )
110
+
111
+
112
+ def export_example(mode, path):
113
+ Path(path).write_text(
114
+ json.dumps(example_data(mode), indent=2) + "\n", encoding="utf-8"
115
+ )
@@ -0,0 +1,186 @@
1
+ """Bounded calculation and disk queues; ready always precedes save completion."""
2
+
3
+ from concurrent.futures import ThreadPoolExecutor
4
+ from dataclasses import dataclass, field, fields, is_dataclass, replace
5
+ from queue import Queue
6
+ from threading import BoundedSemaphore, Event, Lock
7
+ import uuid
8
+
9
+ import numpy as np
10
+
11
+ from multipolepdm.io.export import export_result, load_snapshot, reserve_result_directory
12
+ from multipolepdm.jobs.workers import Cancelled, compute_job
13
+
14
+
15
+ @dataclass
16
+ class TaskHandle:
17
+ job_id: str
18
+ job: object
19
+ directory: object
20
+ cancellation: Event = field(default_factory=Event)
21
+ state: str = "queued"
22
+
23
+
24
+ class QueueFull(RuntimeError):
25
+ pass
26
+
27
+
28
+ def freeze_arrays(value):
29
+ """Consumers can plot shared snapshots but cannot edit an in-flight export."""
30
+ if isinstance(value, np.ndarray):
31
+ value.setflags(write=False)
32
+ elif is_dataclass(value):
33
+ for item in fields(value):
34
+ freeze_arrays(getattr(value, item.name))
35
+ elif isinstance(value, (tuple, list)):
36
+ for item in value:
37
+ freeze_arrays(item)
38
+
39
+
40
+ class JobManager:
41
+ def __init__(self, compute=compute_job, writer=export_result, max_pending=8):
42
+ self.events = Queue()
43
+ self.handles = {}
44
+ self._lock = Lock()
45
+ self._slots = BoundedSemaphore(max_pending)
46
+ self._results = BoundedSemaphore(2)
47
+ self._compute = compute
48
+ self._writer = writer
49
+ self._calculator = ThreadPoolExecutor(
50
+ max_workers=1, thread_name_prefix="pdm-calculate"
51
+ )
52
+ self._disk = ThreadPoolExecutor(max_workers=1, thread_name_prefix="pdm-save")
53
+ self._closed = False
54
+
55
+ def _notify(self, handle, state, **payload):
56
+ self.events.put(
57
+ {
58
+ "job_id": handle.job_id,
59
+ "state": state,
60
+ "directory": str(handle.directory),
61
+ **payload,
62
+ }
63
+ )
64
+
65
+ def _event(self, handle, state, **payload):
66
+ handle.state = state
67
+ self._notify(handle, state, **payload)
68
+
69
+ def _acquire_slot(self):
70
+ if self._closed:
71
+ raise RuntimeError("Job manager is closed.")
72
+ if not self._slots.acquire(blocking=False):
73
+ raise QueueFull("Queue is full. Wait for a task to finish or cancel one.")
74
+
75
+ def submit(self, job):
76
+ with self._lock:
77
+ self._acquire_slot()
78
+ try:
79
+ handle = TaskHandle(uuid.uuid4().hex, job, reserve_result_directory(job))
80
+ self.handles[handle.job_id] = handle
81
+ self._event(handle, "queued")
82
+ self._calculator.submit(self._run, handle)
83
+ return handle.job_id
84
+ except Exception:
85
+ self._slots.release()
86
+ raise
87
+
88
+ def retry_save(self, job_id, result, summary, destination):
89
+ with self._lock:
90
+ handle = self.handles[job_id]
91
+ if handle.state != "save_failed":
92
+ raise RuntimeError("Only a failed data save can be retried.")
93
+ output = replace(handle.job.output, directory=destination)
94
+ job = replace(handle.job, output=output)
95
+ directory = reserve_result_directory(job)
96
+ snapshot = replace(result, job=job)
97
+ self._acquire_slot()
98
+ if not self._results.acquire(blocking=False):
99
+ self._slots.release()
100
+ raise QueueFull("Disk queue is busy. Retry when a save finishes.")
101
+ previous_job, previous_directory = handle.job, handle.directory
102
+ try:
103
+ handle.job, handle.directory = job, directory
104
+ handle.cancellation.clear()
105
+ self._event(handle, "queued")
106
+ self._disk.submit(self._save, handle, snapshot, summary)
107
+ except Exception as exc:
108
+ handle.job, handle.directory = previous_job, previous_directory
109
+ self._event(handle, "save_failed", error=str(exc))
110
+ self._results.release()
111
+ self._slots.release()
112
+ raise
113
+
114
+ def open_saved(self, job_id):
115
+ with self._lock:
116
+ handle = self.handles[job_id]
117
+ if self._closed or handle.state != "saved":
118
+ raise RuntimeError("This task has no saved result to open.")
119
+ self._disk.submit(self._open, handle)
120
+
121
+ def _open(self, handle):
122
+ try:
123
+ result, summary = load_snapshot(handle.directory)
124
+ freeze_arrays(result)
125
+ freeze_arrays(summary)
126
+ self._notify(handle, "opened", result=result, summary=summary)
127
+ except Exception as exc:
128
+ self._notify(handle, "open_failed", error=str(exc))
129
+
130
+ def cancel(self, job_id):
131
+ self.handles[job_id].cancellation.set()
132
+
133
+ def _run(self, handle):
134
+ self._results.acquire()
135
+ try:
136
+ if handle.cancellation.is_set():
137
+ raise Cancelled("Cancelled before computation.")
138
+ self._event(handle, "calculating", percent=0)
139
+ result, summary = self._compute(
140
+ handle.job,
141
+ handle.cancellation.is_set,
142
+ lambda p: self._event(handle, "calculating", percent=p),
143
+ )
144
+ freeze_arrays(result)
145
+ freeze_arrays(summary)
146
+ self._event(handle, "ready", result=result, summary=summary, percent=100)
147
+ self._disk.submit(self._save, handle, result, summary)
148
+ except Exception as exc:
149
+ self._event(
150
+ handle,
151
+ "cancelled" if isinstance(exc, Cancelled) else "calculation_failed",
152
+ error=str(exc),
153
+ )
154
+ self._results.release()
155
+ self._slots.release()
156
+
157
+ def _save(self, handle, result, summary):
158
+ try:
159
+ handle.directory = handle.directory.expanduser()
160
+ self._event(handle, "saving", percent=0)
161
+ handle.directory.mkdir(parents=True, exist_ok=False)
162
+ outcome = self._writer(
163
+ result,
164
+ summary,
165
+ handle.directory,
166
+ handle.cancellation.is_set,
167
+ lambda p: self._event(handle, "saving", percent=p),
168
+ )
169
+ if outcome not in ("saved", "cancelled"):
170
+ raise ValueError("Data writer must return 'saved' or 'cancelled'.")
171
+ self._event(handle, outcome, percent=100)
172
+ except Exception as exc:
173
+ self._event(handle, "save_failed", error=str(exc))
174
+ finally:
175
+ self._results.release()
176
+ self._slots.release()
177
+
178
+ def shutdown(self, wait=True, cancel=False):
179
+ with self._lock:
180
+ self._closed = True
181
+ if cancel:
182
+ for handle in self.handles.values():
183
+ handle.cancellation.set()
184
+ # Calculator must finish enqueueing its writes before disk shutdown.
185
+ self._calculator.shutdown(wait=True)
186
+ self._disk.shutdown(wait=wait)
@@ -0,0 +1,26 @@
1
+ """Scientific worker functions without a Qt dependency."""
2
+
3
+ from multipolepdm.models.registry import evaluate_response
4
+
5
+
6
+ class Cancelled(Exception):
7
+ pass
8
+
9
+
10
+ def compute_job(job, cancelled=lambda: False, progress=lambda value: None):
11
+ from multipolepdm.pdm.mapping import map_density
12
+ from multipolepdm.pdm.statistics import summarize_density
13
+
14
+ def checkpoint(percent):
15
+ if cancelled():
16
+ raise Cancelled("Cancelled by user.")
17
+ progress(percent)
18
+
19
+ checkpoint(5)
20
+ response = evaluate_response(job)
21
+ checkpoint(50)
22
+ result = map_density(job, response)
23
+ checkpoint(80)
24
+ summary = summarize_density(result)
25
+ checkpoint(100)
26
+ return result, summary
@@ -0,0 +1,5 @@
1
+ from multipolepdm.models.registry import build_response
2
+
3
+
4
+ def evaluate(job, xi_rad, sigma):
5
+ return build_response(job, xi_rad, sigma)
@@ -0,0 +1,5 @@
1
+ from multipolepdm.models.registry import build_response
2
+
3
+
4
+ def evaluate(job, xi_rad, sigma):
5
+ return build_response(job, xi_rad, sigma)
@@ -0,0 +1,5 @@
1
+ from multipolepdm.models.registry import build_response
2
+
3
+
4
+ def evaluate(job, xi_rad, sigma):
5
+ return build_response(job, xi_rad, sigma, conditional=True)
@@ -0,0 +1,88 @@
1
+ """Three observation definitions feeding one PDM core."""
2
+
3
+ import numpy as np
4
+
5
+ from multipolepdm.config import p_ado, p_dco, pdco
6
+ from multipolepdm.contracts import ResponseGrid
7
+
8
+ MODE_CONFIGS = {c.MODE: c for c in (p_ado, p_dco, pdco)}
9
+
10
+
11
+ def build_response(job, xi_rad, sigma, conditional=False):
12
+ from multipolepdm.physics.joint import response_channels
13
+
14
+ xi_rad, sigma = np.asarray(xi_rad, dtype=float), np.asarray(sigma, dtype=float)
15
+ if xi_rad.ndim != 1 or sigma.ndim != 1 or not np.all(np.isfinite(xi_rad)):
16
+ raise ValueError("Response coordinates must be finite one-dimensional arrays.")
17
+ pc, rc = response_channels(job, xi_rad, sigma, conditional)
18
+ den = pc.sum(axis=0)
19
+ valid = (den > 0) & (rc[1] > 0)
20
+ polarization = np.full(den.shape, np.nan)
21
+ angular_ratio = np.full(den.shape, np.nan)
22
+ np.divide(pc[0] - pc[1], den, out=polarization, where=valid)
23
+ np.divide(rc[0], rc[1], out=angular_ratio, where=valid)
24
+ valid &= np.isfinite(polarization) & np.isfinite(angular_ratio)
25
+ from multipolepdm.physics.symmetries import observable_identities
26
+
27
+ unit_ratio, zero_polarization = observable_identities(job)
28
+ # Preserve analytic identities instead of turning roundoff into a PDF.
29
+ if unit_ratio:
30
+ angular_ratio[valid] = 1.0
31
+ if zero_polarization:
32
+ polarization[valid] = 0.0
33
+ roundoff = 1e-12
34
+ valid &= (np.abs(polarization) <= 1 + roundoff) & (angular_ratio >= -roundoff)
35
+ polarization[valid] = np.clip(polarization[valid], -1, 1)
36
+ angular_ratio[valid] = np.maximum(angular_ratio[valid], 0)
37
+ polarization[~valid] = np.nan
38
+ angular_ratio[~valid] = np.nan
39
+ return ResponseGrid(
40
+ xi_rad,
41
+ sigma,
42
+ polarization,
43
+ angular_ratio,
44
+ valid,
45
+ job.reference_spin,
46
+ job.ratio_name,
47
+ {
48
+ "polarization_definition": "direction-gated"
49
+ if conditional
50
+ else "single-line",
51
+ "target_first": job.target_first,
52
+ "reference_spin": job.reference_spin,
53
+ "xi_unit": "rad",
54
+ "core_alignment": "sigma",
55
+ "ratio_azimuth_average": job.mode != "P_ADO"
56
+ and job.geometry.ratio_azimuth_average,
57
+ "polarization_gate_azimuth_average": conditional
58
+ and job.geometry.polarization_gate_azimuth_average,
59
+ "azimuth_integration": (
60
+ "Uniform nine-node intensity mean; polar angles remain fixed."
61
+ if (job.mode != "P_ADO" and job.geometry.ratio_azimuth_average)
62
+ or (conditional and job.geometry.polarization_gate_azimuth_average)
63
+ else "Point directions; no azimuthal averaging."
64
+ ),
65
+ "undefined_response_points": int(np.count_nonzero(~valid)),
66
+ "exact_observable_identities": {
67
+ "unit_ratio": unit_ratio,
68
+ "zero_linear_polarization": zero_polarization,
69
+ },
70
+ "amplitude_convention": "KS/Droste: (-1)^L CG(Jf mf L q | Ji mi) D*; electric helicity factor lambda; e_theta parallel",
71
+ },
72
+ )
73
+
74
+
75
+ def evaluate_response(job, xi_rad=None, sigma=None):
76
+ from multipolepdm.models import (
77
+ p_ado as ado_model,
78
+ p_dco as dco_model,
79
+ pdco as pdco_model,
80
+ )
81
+
82
+ if xi_rad is None and sigma is None:
83
+ xi_rad, sigma = job.grid.axes(job.reference_spin)
84
+ elif xi_rad is None or sigma is None:
85
+ raise ValueError("Provide both xi_rad and sigma axes, or neither.")
86
+ return {"P_ADO": ado_model, "P_DCO": dco_model, "PDCO": pdco_model}[
87
+ job.mode
88
+ ].evaluate(job, xi_rad, sigma)