Repository navigation
Expand file tree
/
Copy pathreturn_path_geometry_response.py
More file actions
320 lines (284 loc) · 11.2 KB
/
Copy pathreturn_path_geometry_response.py
File metadata and controls
320 lines (284 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""Freeze geometry, predict a nodal tangent response, then evaluate it.
This is a declared known-source mathematical control. The geometry-only
predictor never receives the source coefficient or the reserved response.
Write-once records and source archives reuse the established evidence owner;
hashes bind bytes, not independent provenance or authenticated chronology.
"""
from __future__ import annotations
import argparse
import hashlib
from fractions import Fraction as Q
from pathlib import Path
import mpmath as mp
import networkx as nx
from benchmarks import relational_seeded_response as evidence
from tnfr.dynamics.relational import RelationalExchangeModel
from tnfr.physics.phase_cycle_geometry import (
_return_path_storage_residual,
assess_return_path_geometry_response,
)
from tnfr.physics.relational_sine_comparison import bound_relational_sine_exchange
from tnfr.research.relational_acquisition import _verify_archive
from tnfr.sdk import relational_report_to_dict
from tnfr.utils.io import json_loads
ROOT = Path(__file__).resolve().parents[1]
DECLARATION = ROOT / "docs/assets/return_path_geometry_response/declaration.json"
DEFAULT_OUTPUT = (
ROOT / "artifacts/research/return_path_geometry_response/response-v1.json"
)
_MAX_BYTES = 32 * 1024**2
def _read(path):
evidence._require(path.stat().st_size <= _MAX_BYTES, "record exceeds byte budget")
data = path.read_bytes()
evidence._require(len(data) <= _MAX_BYTES, "record exceeds byte budget")
return data, json_loads(data)
def _digest(data):
return hashlib.sha256(data).hexdigest()
def _source_files():
files = evidence._source_files()
for path in (Path(__file__).resolve(), DECLARATION):
files[path.relative_to(ROOT).as_posix()] = path.read_bytes()
return files
def _runtime():
return {**evidence._runtime(), "mpmath": mp.__version__, "networkx": nx.__version__}
def _graph(declaration):
graph = nx.Graph()
graph.add_nodes_from(declaration["nodes"])
for key in ("left_cycle", "right_cycle"):
cycle = declaration[key]
graph.add_edges_from(zip(cycle, cycle[1:] + cycle[:1]), weight=1)
graph.add_edges_from(declaration["additional_edges"], weight=1)
return graph
def _source_root(declaration):
coefficient = declaration["source_coefficient"]
def current(angle):
sine = mp.sin(angle)
return sine + coefficient * sine**3
return mp.findroot(
lambda angle: current(mp.pi / 2 - angle / 4)
- current(angle)
- current(2 * angle / 3),
(mp.pi / 6, 2 * mp.pi / 5),
)
def prepare_observation(declaration):
"""Quantize a known-source geometry, certifying its enclosing root signs."""
settings = declaration["geometry_observation"]
with mp.workdps(settings["decimal_places"]):
scaled = (
_source_root(declaration) / (2 * mp.pi) * settings["turn_grid_denominator"]
)
lower = Q(int(mp.floor(scaled)), settings["turn_grid_denominator"])
upper = Q(int(mp.ceil(scaled)), settings["turn_grid_denominator"])
coefficient = Q(declaration["source_coefficient"])
signs = (
_return_path_storage_residual(lower, coefficient),
_return_path_storage_residual(upper, coefficient),
)
evidence._require(
lower < upper and signs[0].lo > 0 > signs[1].hi,
"quantized source geometry has unresolved strict root signs",
)
return {
"special_turn_bounds": [str(lower), str(upper)],
"origin": settings["origin"],
}
def predict_response(declaration, observation):
"""Use geometry and independent scales; do not consume the source coefficient."""
graph = _graph(declaration)
graph.graph["GAMMA"] = {"type": "none"}
for node in graph:
graph.nodes[node].update(EPI=0, theta=0, nu_f=1)
source = bound_relational_sine_exchange(
graph,
reference_model=RelationalExchangeModel(
1, epi_weight=0, phase_weight=1, phase_domain="regular"
),
)
return assess_return_path_geometry_response(
source,
left_cycle=declaration["left_cycle"],
right_cycle=declaration["right_cycle"],
mediator=declaration["mediator"],
special_turn_bounds=tuple(
Q(value) for value in observation["special_turn_bounds"]
),
form_direction=declaration["form_direction"],
observation_origin=observation["origin"],
)
def _prediction_input(declaration):
"""Expose only graph preparation and the declared perturbation to inference."""
return {
key: declaration[key]
for key in (
"nodes",
"left_cycle",
"right_cycle",
"mediator",
"additional_edges",
"form_direction",
)
}
def evaluate_response(declaration):
"""Differentiate full neighbor sums independently of the predictor's Hessian."""
graph = _graph(declaration)
settings = declaration["geometry_observation"]
with mp.workdps(settings["decimal_places"]):
v = _source_root(declaration)
bulk, join = mp.pi / 2 - v / 4, 2 * v / 3
angles = (
0,
v,
v + bulk,
v + 2 * bulk,
v + 3 * bulk,
2 * join,
2 * join - v,
2 * join - v - bulk,
2 * join - v - 2 * bulk,
2 * join - v - 3 * bulk,
join,
)
impulse = declaration["form_direction"]
phase_velocity = [
sum(mp.mpf(impulse[i] - impulse[j]) for j in graph[i]) / graph.degree[i]
for i in graph
]
coefficient = declaration["source_coefficient"]
def current(angle):
sine = mp.sin(angle)
return sine + coefficient * sine**3
values = [
mp.diff(
lambda time: sum(
current(
angles[j]
- angles[i]
+ time * (phase_velocity[j] - phase_velocity[i])
)
for j in graph[i]
)
/ graph.degree[i],
0,
)
for i in graph
]
return [
mp.nstr(value, declaration["response_record_digits"]) for value in values
]
def _fraction(record):
evidence._require(
isinstance(record, dict)
and set(record) == {"numerator", "denominator"}
and type(record["numerator"]) is int
and type(record["denominator"]) is int
and record["denominator"] > 0,
"invalid projected exact fraction",
)
return Q(record["numerator"], record["denominator"])
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("stage", choices=("prepare", "predict", "evaluate"))
parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT)
args = parser.parse_args(argv)
output = args.output
protocol_path = output.with_suffix(".protocol.json")
archive = output.with_suffix(".source.zip")
prediction_path = output.with_suffix(".prediction.json")
if output.exists():
raise FileExistsError(f"retain existing response: {output}")
evidence._verify_runtime_source()
_, declaration = _read(DECLARATION)
if args.stage == "prepare":
if any(path.exists() for path in (protocol_path, archive, prediction_path)):
raise FileExistsError("retain existing preparation or prediction")
observation = prepare_observation(declaration)
files = _source_files()
evidence._archive(archive, files)
_verify_archive(archive, evidence._manifest(files))
evidence._write(
protocol_path,
{
"schema": "tnfr.return-path-geometry-response-protocol.v1",
"declaration": declaration,
"observation": observation,
"runtime": _runtime(),
"source_sha256": evidence._manifest(files),
"archive_scope": "all_tnfr_python_both_producers_pyproject_and_declaration_no_dependency_binaries",
},
)
print(f"Prepared geometry: {protocol_path}")
return 0
protocol_bytes, protocol = _read(protocol_path)
evidence._require(protocol["declaration"] == declaration, "declaration changed")
evidence._require(protocol["runtime"] == _runtime(), "runtime changed")
evidence._require(
protocol["source_sha256"] == evidence._manifest(_source_files()),
"source changed",
)
_verify_archive(archive, protocol["source_sha256"])
provenance = {
"protocol_sha256": _digest(protocol_bytes),
"source_archive_sha256": _digest(archive.read_bytes()),
}
if args.stage == "predict":
if prediction_path.exists():
raise FileExistsError(
"retain existing prediction, including unavailable results"
)
report = predict_response(
_prediction_input(declaration), protocol["observation"]
)
evidence._write(
prediction_path, {**provenance, **relational_report_to_dict(report)}
)
print(f"Recorded prediction: {prediction_path}")
return 0
prediction_bytes, prediction = _read(prediction_path)
evidence._require(
all(prediction.get(key) == value for key, value in provenance.items()),
"prediction provenance mismatch",
)
# Re-admit the frozen geometric premises before consuming projected bounds.
# This checks the saved prediction without replacing it or using a response.
rebuilt = relational_report_to_dict(
predict_response(_prediction_input(declaration), protocol["observation"])
)
evidence._require(
evidence._encoded(prediction) == evidence._encoded({**provenance, **rebuilt}),
"frozen prediction differs from admitted geometric premises",
)
report = prediction["report"]
evidence._require(
report["coefficient_status"] == "bounded",
"prediction unavailable; do not evaluate response",
)
values = evaluate_response(declaration)
bounds = report["response_acceleration_bounds"]
evidence._require(len(bounds) == len(values) == 11, "response node order mismatch")
contained = [
_fraction(bound["lo"]) <= Q(value) <= _fraction(bound["hi"])
for value, bound in zip(values, bounds)
]
lower, upper = _fraction(report["coefficient_lower"]), _fraction(
report["coefficient_upper"]
)
coefficient_contained = lower <= declaration["source_coefficient"] <= upper
passed = coefficient_contained and lower > 0 and all(contained)
evidence._write(
output,
{
"schema": "tnfr.return-path-geometry-response-record.v1",
**provenance,
"prediction_sha256": _digest(prediction_bytes),
"response_acceleration_decimal": values,
"component_containment": contained,
"coefficient_containment": coefficient_contained,
"sine_baseline_excluded": lower > 0,
"passed": passed,
"scope": declaration["limitations"],
},
)
print(f"Recorded response: {output}; passed={passed}")
return 0 if passed else 1
if __name__ == "__main__":
raise SystemExit(main())