Files
Jyotisha/scripts/flexible_birth_time_profile.py
T
ClaudeandClaude Opus 5.5 6dacd779d2
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m51s
Independent Staging Quality Gate / validate-python (push) Successful in 7m50s
Independent Staging Quality Gate / publish (push) Successful in 3m39s
fix(report): astrologer 11-25 site-vs-local comparison fixes (BUG-2364–2376)
- 2364 KP sub-lord in the Nakshatra Reference: proportional, not equal ninths (upstream calc_sub_lord)
- 2365 birth-time sensitivity KP cusps: Placidus + Krishnamurti, same source as the formal KP table
- 2366 expression paths: D2/D11/D24/D4 house counted from the varga's own ascendant
- 2367 Bhavesh table: PD-only lord labelled, not "Yes", when the PD range is not stable
- 2368 KP cusp table: very strong significator is a lead, not a promise
- 2369 yoga wording: no blanket "cancellation not checked"; detector match is not "conditions hold"
- 2370 Neecha self-reference marked per condition
- 2371 reading guide: feedback only in rectification; all applicable systems, two dashas minimum
- 2372 audit table: progress/index rows are observation only
- 2373 Saptavargaja by chart
- 2374 Manahoo interferer never one of the pair (upstream 322e2f30)
- 2375 KP monthly table seeded under the KP profile (owner-approved frozen-file edit) + profile line
- 2376 annual continuous motion: full event list per year in the appendix (owner-approved)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXp3RzgZVEpT1PXxkwHj
2026-11-25 16:54:52 +08:00

362 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Build a read-only sensitivity profile for an unresolved birth-time window."""
from __future__ import annotations
from copy import deepcopy
from datetime import datetime
from hashlib import sha256
import json
import re
from typing import Any, Mapping, Sequence
SCHEMA_VERSION = "jyotish.flexible_birth_time_profile.v1"
MAX_CANDIDATE_MINUTES = 31
_CLOCK_RE = re.compile(r"(?:[01]\d|2[0-3]):[0-5]\d")
_PROHIBITED_AUTHORITY_FIELDS = frozenset({
"approved_birth_time",
"final_birth_time",
"winner",
"approval",
"approval_authority",
})
_PROHIBITED_SOURCE_STATUSES = frozenset({"approved", "confirmed"})
class FlexibleBirthTimeProfileError(ValueError):
"""Raised when a candidate window cannot be represented safely."""
def build_flexible_birth_time_profile(
candidates: Sequence[Mapping[str, Any]],
*,
source_reference: Mapping[str, Any],
) -> dict[str, Any]:
_reject_candidate_window_authority(candidates)
rows = _normalize_candidates(candidates)
source = _normalize_source_reference(source_reference)
stable: dict[str, Any] = {}
sensitive: dict[str, dict[str, Any]] = {}
for key in sorted({key for row in rows for key in row["evidence"]}):
values = {row["candidate_time"]: row["evidence"].get(key) for row in rows}
if len({_canonical(value) for value in values.values()}) == 1:
stable[key] = deepcopy(next(iter(values.values())))
else:
sensitive[key] = deepcopy(values)
times = [row["candidate_time"] for row in rows]
profile_id = _profile_id(times, source["review_id"])
return {
"schema_version": SCHEMA_VERSION,
"flexible_profile_id": profile_id,
"birth_time_window": {
"start_time": times[0],
"end_time": times[-1],
"candidate_count": len(times),
"candidate_times": times,
},
"candidate_references": [
{"candidate_id": row["candidate_id"], "candidate_time": row["candidate_time"]}
for row in rows
],
"stable_evidence": stable,
"sensitive_evidence": sensitive,
"source_reference": source,
"trace": [
{"kind": "flexible_birth_time_profile", "reference": profile_id},
{"kind": "candidate_window", "reference": source["review_id"]},
*(
{"kind": "candidate", "reference": f"candidate://{row['candidate_id']}"}
for row in rows
),
],
"status": "candidate_window_only",
"claim_boundary": (
"Read-only candidate-window comparison. It cannot select or confirm a birth minute, "
"replace chart identity, or grant authority to a candidate chart."
),
}
def build_flexible_birth_time_profile_from_window(
*,
birth_date: str,
start_time: str,
end_time: str,
candidate_times: Sequence[str],
lat: float,
lon: float,
tz: float,
ayanamsa: str,
node_mode: str,
source_reference: Mapping[str, Any],
candidate_intervals: list[dict[str, str]] | None = None,
) -> dict[str, Any]:
"""Issue #165: ``ayanamsa`` and ``node_mode`` are the profile's and are required.
There is no Raman / mean default here any more, so a window comparison can
never run on settings different from the chart it describes.
"""
ayanamsa, node_mode = _calculation_settings(ayanamsa, node_mode)
if candidate_intervals is None:
start, end = _parse_window(birth_date, start_time, end_time)
normalized_times = _normalize_candidate_times(birth_date, start, end, candidate_times)
else:
from scripts.rectification.candidate_window import enumerate_candidate_window
moments = enumerate_candidate_window({"birth_date": birth_date, "start_time": start_time,
"end_time": end_time, "candidate_intervals": candidate_intervals})
by_clock = {value.strftime("%H:%M"): value for value in moments}
if not 2 <= len(candidate_times) <= MAX_CANDIDATE_MINUTES or len(set(candidate_times)) != len(candidate_times):
raise FlexibleBirthTimeProfileError("candidate_count_must_be_two_to_thirty_one")
if any(value not in by_clock for value in candidate_times):
raise FlexibleBirthTimeProfileError("candidate_time_outside_window")
normalized_times = sorted(by_clock[value] for value in candidate_times)
candidates = [
_candidate_from_recast(
candidate_at=value,
recast=_recast_candidate_layers(
value, lat=lat, lon=lon, tz=tz, ayanamsa=ayanamsa, node_mode=node_mode,
),
ayanamsa=ayanamsa,
node_mode=node_mode,
)
for value in normalized_times
]
profile = build_flexible_birth_time_profile(candidates, source_reference=source_reference)
if candidate_intervals is not None:
# Keep civil chronology and declared gaps instead of the legacy clock-sorted envelope.
clocks = [value.strftime("%H:%M") for value in normalized_times]
profile["birth_time_window"].update({"start_time": start_time, "end_time": end_time,
"candidate_times": clocks, "candidate_intervals": deepcopy(candidate_intervals),
"candidate_datetimes": [value.isoformat(timespec="minutes") for value in normalized_times]})
refs = {row["candidate_time"]: row for row in profile["candidate_references"]}
profile["candidate_references"] = [{**refs[value.strftime("%H:%M")], "candidate_date": value.date().isoformat()} for value in normalized_times]
profile["calculation_profile"] = {
"ayanamsa": ayanamsa,
"node_mode": node_mode,
"coordinate_mode": "explicit_lat_lon_tz",
"candidate_recast": "native_domain_calculation_service",
}
return profile
def _calculation_settings(ayanamsa: Any, node_mode: Any) -> tuple[str, str]:
if not isinstance(ayanamsa, str) or not ayanamsa.strip():
raise FlexibleBirthTimeProfileError("ayanamsa_required")
if not isinstance(node_mode, str) or node_mode.strip().lower() not in {"mean", "true"}:
raise FlexibleBirthTimeProfileError("node_mode_required")
return ayanamsa.strip().lower(), node_mode.strip().lower()
def _recast_candidate_layers(
candidate: datetime,
*,
lat: float,
lon: float,
tz: float,
ayanamsa: str,
node_mode: str,
) -> dict[str, Any]:
try:
import domain_calculation_service
import jaimini
import varga
except ModuleNotFoundError: # pragma: no cover - package import
from scripts import domain_calculation_service, jaimini, varga
chart = domain_calculation_service.compute_chart({
"year": candidate.year,
"month": candidate.month,
"day": candidate.day,
"hour": candidate.hour,
"minute": candidate.minute,
"second": candidate.second,
"lat": lat,
"lon": lon,
"tz": tz,
"ayanamsa": ayanamsa,
"node_mode": node_mode,
})
planets = {
name: row["lon"]
for name, row in (chart.get("planets") or {}).items()
if name in {"Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn", "Rahu", "Ketu"}
}
ascendant = chart.get("ascendant") or {}
asc_lon = float(ascendant["lon"])
divisions = [2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 16, 24, 30, 40, 45, 60]
vargas = varga.calc_all_vargas(planets, asc_lon, divisions=divisions)
arudha = jaimini.calc_arudha_padas(int(asc_lon // 30), planets)
padas = arudha.get("padas") or {}
# BUG-2365:KP 宫头取 Placidus + Krishnamurti(与正式 KP 表同源),不再读 Raman 整宫上升度数。
try:
from scripts.rectification.kp_cusp_observation import observe_kp_cusps
except ModuleNotFoundError: # pragma: no cover - script import
from rectification.kp_cusp_observation import observe_kp_cusps
observation = observe_kp_cusps(((chart.get("birth_info") or {}).get("julian_day")), lat, lon)
kp_cusps: dict[str, Any] = {}
if observation.get("status") == "executed":
for number in (1, 4, 7, 10):
row = (observation.get("houses") or {}).get(str(number)) or {}
if row:
kp_cusps[f"house_{number}"] = {
"sign": row.get("sign"),
"nakshatra_lord": row.get("nakshatra_lord"),
"sub_lord": row.get("sub_lord"),
}
return {
"ascendant": ascendant,
"varga_lagna": {key: value.get("Ascendant") or {} for key, value in vargas.items()},
"arudha": {"A7": padas.get("A7") or {}, "A10": padas.get("A10") or {}, "UL": arudha.get("upapada") or {}},
"kp_cusps": kp_cusps,
}
def _candidate_from_recast(
*,
candidate_at: datetime,
recast: Mapping[str, Any],
ayanamsa: str,
node_mode: str,
) -> dict[str, Any]:
evidence: dict[str, Any] = {"D1.ascendant": (recast.get("ascendant") or {}).get("sign")}
for key, value in (recast.get("varga_lagna") or {}).items():
if isinstance(key, str) and key.startswith("D") and isinstance(value, Mapping):
evidence[f"{key.split('_', 1)[0]}.ascendant"] = value.get("sign")
for key in ("A7", "A10", "UL"):
value = (recast.get("arudha") or {}).get(key)
if isinstance(value, Mapping):
evidence[f"arudha.{key}"] = value.get("sign")
evidence["KP.cusp_observation"] = recast.get("kp_cusps") or {}
candidate_time = candidate_at.strftime("%H:%M")
candidate_id = f"candidate-{candidate_time.replace(':', '')}"
return {
"candidate_id": candidate_id,
"candidate_time": candidate_time,
"evidence": evidence,
"trace": [
{"kind": "candidate_chart_recast", "reference": f"candidate-chart://{candidate_id}"},
{"kind": "calculation_profile", "reference": f"ayanamsa://{ayanamsa}/node/{node_mode}"},
],
}
def _parse_window(birth_date: str, start_time: str, end_time: str) -> tuple[datetime, datetime]:
if not _is_hh_mm(start_time) or not _is_hh_mm(end_time):
raise FlexibleBirthTimeProfileError("birth_date_or_candidate_time_invalid")
try:
start = datetime.strptime(f"{birth_date} {start_time}", "%Y-%m-%d %H:%M")
end = datetime.strptime(f"{birth_date} {end_time}", "%Y-%m-%d %H:%M")
except (TypeError, ValueError) as exc:
raise FlexibleBirthTimeProfileError("birth_date_or_candidate_time_invalid") from exc
if end < start:
raise FlexibleBirthTimeProfileError("candidate_window_must_not_cross_midnight")
return start, end
def _normalize_candidate_times(
birth_date: str,
start: datetime,
end: datetime,
candidate_times: Sequence[str],
) -> list[datetime]:
if isinstance(candidate_times, (str, bytes)) or not isinstance(candidate_times, Sequence):
raise FlexibleBirthTimeProfileError("candidate_times_required")
if len(candidate_times) < 2 or len(candidate_times) > MAX_CANDIDATE_MINUTES:
raise FlexibleBirthTimeProfileError("candidate_count_must_be_two_to_thirty_one")
values: list[datetime] = []
for raw in candidate_times:
if not _is_hh_mm(raw):
raise FlexibleBirthTimeProfileError("candidate_time_invalid")
try:
value = datetime.strptime(f"{birth_date} {raw}", "%Y-%m-%d %H:%M")
except (TypeError, ValueError) as exc:
raise FlexibleBirthTimeProfileError("candidate_time_invalid") from exc
if value < start or value > end:
raise FlexibleBirthTimeProfileError("candidate_time_outside_window")
values.append(value)
if len(set(values)) != len(values):
raise FlexibleBirthTimeProfileError("candidate_times_must_be_unique")
return sorted(values)
def _normalize_candidates(candidates: Sequence[Mapping[str, Any]]) -> list[dict[str, Any]]:
if isinstance(candidates, (str, bytes)) or not isinstance(candidates, Sequence):
raise FlexibleBirthTimeProfileError("candidates_required")
if len(candidates) < 2 or len(candidates) > MAX_CANDIDATE_MINUTES:
raise FlexibleBirthTimeProfileError("candidate_count_must_be_two_to_thirty_one")
rows: list[dict[str, Any]] = []
for candidate in candidates:
if not isinstance(candidate, Mapping):
raise FlexibleBirthTimeProfileError("candidate_must_be_mapping")
candidate_id = candidate.get("candidate_id")
candidate_time = candidate.get("candidate_time")
evidence = candidate.get("evidence")
trace = candidate.get("trace")
if not isinstance(candidate_id, str) or not candidate_id:
raise FlexibleBirthTimeProfileError("candidate_id_required")
if not _is_hh_mm(candidate_time):
raise FlexibleBirthTimeProfileError("candidate_time_required")
if not isinstance(evidence, Mapping) or not evidence:
raise FlexibleBirthTimeProfileError("candidate_evidence_required")
if not isinstance(trace, list) or not trace:
raise FlexibleBirthTimeProfileError("candidate_trace_required")
rows.append({"candidate_id": candidate_id, "candidate_time": candidate_time, "evidence": dict(evidence), "trace": trace})
if len({row["candidate_id"] for row in rows}) != len(rows):
raise FlexibleBirthTimeProfileError("candidate_ids_must_be_unique")
if len({row["candidate_time"] for row in rows}) != len(rows):
raise FlexibleBirthTimeProfileError("candidate_times_must_be_unique")
return sorted(rows, key=lambda row: row["candidate_time"])
def _normalize_source_reference(value: Mapping[str, Any]) -> dict[str, str]:
if not isinstance(value, Mapping):
raise FlexibleBirthTimeProfileError("source_reference_must_be_mapping")
_reject_candidate_window_authority(value)
status = value.get("status")
if isinstance(status, str) and status.strip().lower() in _PROHIBITED_SOURCE_STATUSES:
raise FlexibleBirthTimeProfileError("approved_or_confirmed_source_reference_forbidden")
review_id = value.get("review_id")
if not isinstance(review_id, str) or not review_id:
raise FlexibleBirthTimeProfileError("source_review_id_required")
return {"review_id": review_id, "status": "review_required"}
def _profile_id(candidate_times: Sequence[str], review_id: str) -> str:
digest = sha256(repr((tuple(candidate_times), review_id)).encode("utf-8")).hexdigest()[:24]
return f"flexible-birth-time://{digest}"
def _canonical(value: Any) -> str:
return json.dumps(value, ensure_ascii=True, sort_keys=True, separators=(",", ":"), default=str)
def _is_hh_mm(value: Any) -> bool:
return isinstance(value, str) and _CLOCK_RE.fullmatch(value) is not None
def _candidate_window_authority_violation(value: Any, path: str = "$") -> str | None:
if isinstance(value, Mapping):
for raw_key, item in value.items():
key = str(raw_key).strip().lower()
child_path = f"{path}.{raw_key}"
if key in _PROHIBITED_AUTHORITY_FIELDS:
return child_path
if key == "source_reference" and isinstance(item, Mapping):
status = item.get("status")
if isinstance(status, str) and status.strip().lower() in _PROHIBITED_SOURCE_STATUSES:
return f"{child_path}.status"
violation = _candidate_window_authority_violation(item, child_path)
if violation:
return violation
elif isinstance(value, (list, tuple)):
for index, item in enumerate(value):
violation = _candidate_window_authority_violation(item, f"{path}[{index}]")
if violation:
return violation
return None
def _reject_candidate_window_authority(value: Any) -> None:
violation = _candidate_window_authority_violation(value)
if violation:
raise FlexibleBirthTimeProfileError(f"candidate_window_authority_forbidden:{violation}")