feat(rectification): open on the nearest chart boundary (BUG-1307)

This commit is contained in:
jesse-ux
2026-10-12 15:36:43 +08:00
committed by Jesse_Chen
parent 805932addd
commit 74949d0475
22 changed files with 1066 additions and 30 deletions
+65 -5
View File
@@ -13,6 +13,7 @@ from typing import Any
from scripts import domain_calculation_service, varga
TARGETS = ("D1", "D9", "D10")
BOUNDARY_CHARTS = ("D1", "D2", "D4", "D5", "D7", "D9", "D10", "D11", "D12", "D24", "D30")
VERSION = "rectification-varga-segments-v1"
@@ -45,12 +46,16 @@ def scan_varga_segments(body: dict[str, Any], *, cache_dir: Path | None = None)
or any(not isinstance(target, str) or target not in TARGETS for target in targets)
or len(set(targets)) != len(targets)):
raise ValueError("invalid_target_charts")
boundary_charts, recorded_time = _boundary_request(body)
profile = {
"lat": _number(body, "latitude", -90, 90),
"lon": _number(body, "longitude", -180, 180),
"tz": _number(body, "timezone_offset", -14, 14),
"ayanamsa": "raman", "node_mode": "mean",
}
if boundary_charts:
profile["boundary_charts"] = list(boundary_charts)
profile["recorded_time"] = recorded_time
key = hashlib.sha256(json.dumps({"version": VERSION, "start": str(start), "end": str(end), "targets": targets, **profile}, sort_keys=True).encode()).hexdigest()
root = cache_dir if cache_dir is not None else Path(os.environ.get("JYOTISH_CACHE_DIR", str(Path(__file__).resolve().parents[1] / "scratch" / "local"))) / "varga_segments"
path = root / f"{key}.json"
@@ -63,22 +68,34 @@ def scan_varga_segments(body: dict[str, Any], *, cache_dir: Path | None = None)
pass
minutes = []
segments = {target: [] for target in targets}
boundary_signs: list[dict[str, int]] = []
wanted = list(dict.fromkeys([*targets, *boundary_charts]))
divisions = [int(chart[1:]) for chart in wanted if chart not in {"D1", "D11"}]
d11_chart = None
if "D11" in wanted:
from scripts.active_rectification_event_engine import _d11_chart as d11_chart
for offset in range(width):
moment = start + timedelta(minutes=offset)
chart = domain_calculation_service.compute_chart({
**profile, "year": moment.year, "month": moment.month, "day": moment.day,
"lat": profile["lat"], "lon": profile["lon"], "tz": profile["tz"],
"ayanamsa": "raman", "node_mode": "mean",
"year": moment.year, "month": moment.month, "day": moment.day,
"hour": moment.hour, "minute": moment.minute,
})
ascendant = float(chart["ascendant"]["lon"])
signs = {"D1": int(ascendant // 30) % 12} if "D1" in targets else {}
divisions = [int(target[1:]) for target in targets if target != "D1"]
planets = {name: float(item["lon"]) for name, item in chart.get("planets", {}).items() if isinstance(item, dict) and isinstance(item.get("lon"), (int, float))}
all_signs = {"D1": int(ascendant // 30) % 12} if "D1" in wanted else {}
if divisions:
planets = {name: float(item["lon"]) for name, item in chart.get("planets", {}).items() if isinstance(item, dict) and isinstance(item.get("lon"), (int, float))}
computed = varga.calc_all_vargas(planets, ascendant, divisions=divisions)
for value in computed.values():
signs[f"D{value['_meta']['div']}"] = int(value["Ascendant"]["sign_idx"])
all_signs[f"D{value['_meta']['div']}"] = int(value["Ascendant"]["sign_idx"])
if "D11" in wanted:
all_signs["D11"] = int(d11_chart(planets, ascendant)["Ascendant"]["sign_idx"])
signs = {target: all_signs[target] for target in targets}
stamp = moment.strftime("%Y-%m-%dT%H:%M")
minutes.append({"offset": offset, "time": stamp[11:], "date": stamp[:10], "signs": signs})
if boundary_charts:
boundary_signs.append({chart: all_signs[chart] for chart in boundary_charts if chart in all_signs})
for target in targets:
rows = segments[target]
sign = signs[target]
@@ -90,6 +107,8 @@ def scan_varga_segments(body: dict[str, Any], *, cache_dir: Path | None = None)
"window_minutes": width, "target_charts": targets, "minutes": minutes,
"segments": segments, "full_window_unique": {target: len({row['sign'] for row in rows}) == 1 for target, rows in segments.items()},
"ayanamsa": "raman", "node_mode": "mean", "step_minutes": 1}
if boundary_charts:
payload["nearest_boundary"] = _nearest_from_signs(boundary_signs, [row["time"] for row in minutes], recorded_time)
root.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=root, delete=False) as handle:
json.dump(payload, handle, ensure_ascii=False, sort_keys=True)
@@ -101,6 +120,47 @@ def scan_varga_segments(body: dict[str, Any], *, cache_dir: Path | None = None)
return payload
def _boundary_request(body: dict[str, Any]) -> tuple[tuple[str, ...], str | None]:
if "boundary_charts" not in body:
return (), None
charts = body.get("boundary_charts")
if (not isinstance(charts, list) or not charts
or any(not isinstance(chart, str) or chart not in BOUNDARY_CHARTS for chart in charts)
or len(set(charts)) != len(charts)):
raise ValueError("invalid_boundary_charts")
recorded = body.get("recorded_time")
if not isinstance(recorded, str) or len(recorded) < 5 or recorded[2] != ":":
raise ValueError("invalid_recorded_time")
hour, minute = recorded[:2], recorded[3:5]
if not (hour.isdigit() and minute.isdigit()):
raise ValueError("invalid_recorded_time")
hour_value, minute_value = int(hour), int(minute)
if hour_value > 23 or minute_value > 59:
raise ValueError("invalid_recorded_time")
return tuple(charts), f"{hour_value:02d}:{minute_value:02d}"
def _nearest_from_signs(signs_by_minute: list[dict[str, int]], times: list[str], recorded_time: str | None) -> dict[str, Any]:
from scripts.jyotish_engine import SIGNS, SIGNS_CN
from scripts.rectification.nearest_boundary import nearest_chart_boundary
transitions: list[dict[str, str]] = []
previous: dict[str, int] | None = None
for signs, clock in zip(signs_by_minute, times):
if previous is not None:
for chart, sign in signs.items():
before = previous.get(chart)
if isinstance(before, int) and isinstance(sign, int) and before != sign and 0 <= before <= 11 and 0 <= sign <= 11:
transitions.append({
"layer": chart.lower(),
"at": clock,
"from_sign": SIGNS_CN[SIGNS[before]],
"to_sign": SIGNS_CN[SIGNS[sign]],
})
previous = signs
return nearest_chart_boundary({"transitions": transitions}, recorded_time)
def handle_varga_segments(handler, body):
from scripts.offline_compute_mixins import BadRequest
try: