Files
Jyotisha/scripts/report_render/birth_precision_window.py
T
jesse-ux f914571ed6
Independent Staging Quality Gate / validate-python (pull_request) Successful in 10m19s
Independent Staging Quality Gate / validate-frontend (pull_request) Successful in 18m3s
Independent Staging Quality Gate / publish (pull_request) Skipped
Independent Staging Quality Gate / validate-python (push) Successful in 16m57s
Independent Staging Quality Gate / validate-frontend (push) Failing after 28m43s
Independent Staging Quality Gate / publish (push) Skipped
Clarify default birth time trials in reports (#280)
2026-11-15 13:29:05 +08:00

108 lines
5.1 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.
"""Birth-time tolerance used by the personal report's evidence grading."""
from datetime import datetime
from math import isfinite
import re
CONFIRMED_TOLERANCE_MINUTES = 1.0
DEFAULT_TOLERANCE_MINUTES = 30.0
_WINDOW_EDGE_TOLERANCE_MINUTES = 0.01
WINDOW_POLICY = "birth_record_window_v1"
DEFAULT_WINDOW_SOURCE = "default_unknown_30"
DEFAULT_WINDOW_SCENARIO = "default_window_scenario"
DEFAULT_WINDOW_DISCLOSURE_ZH = "没有填写误差,这只是试算,不能确认稳定"
DEFAULT_WINDOW_DISCLOSURE_EN = "No margin was supplied. This trial does not establish stability."
_DECLARED_SOURCES = {"user_stated", "shichen", "period", "rectified_range"}
_MAX_SIDE_MINUTES = 720
_LOCAL_AT = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2})?\Z")
def _valid_side(value: object) -> bool:
return type(value) in (float, int) and 0 <= value <= _MAX_SIDE_MINUTES and isfinite(value)
def _local_at(value: object) -> bool:
if not isinstance(value, str) or not _LOCAL_AT.fullmatch(value):
return False
try:
datetime.fromisoformat(value)
except ValueError:
return False
return True
def _window_metadata(value: dict) -> dict:
metadata = {}
if _local_at(value.get("start_at")) and _local_at(value.get("end_at")):
metadata.update(start_at=value["start_at"], end_at=value["end_at"])
intervals = value.get("intervals")
if isinstance(intervals, list) and all(
isinstance(part, dict) and set(part) == {"start_at", "end_at"}
and _local_at(part["start_at"]) and _local_at(part["end_at"])
for part in intervals
):
metadata["intervals"] = intervals
for key in ("resolved_line_charts", "both_versions_domains"):
entries = value.get(key)
if isinstance(entries, list) and all(isinstance(entry, str) for entry in entries):
metadata[key] = entries
return metadata
def report_birth_window(value: object, *, minute_confirmed: bool) -> dict:
supplied = value if isinstance(value, dict) else {}
if minute_confirmed:
tolerance = CONFIRMED_TOLERANCE_MINUTES
metadata = _window_metadata(supplied) if supplied.get("source") in ("rectified_range", "confirmed_minute") else {}
return {"before_minutes": tolerance, "after_minutes": tolerance, "source": "confirmed_minute", **metadata}
before, after, source = supplied.get("before_minutes"), supplied.get("after_minutes"), supplied.get("source")
if not (isinstance(source, str) and source in _DECLARED_SOURCES
and _valid_side(before) and _valid_side(after) and (before or after)):
before = after = DEFAULT_TOLERANCE_MINUTES
source = "default_unknown_30"
result = {"before_minutes": before, "after_minutes": after, "source": source}
if source == "rectified_range":
result.update(_window_metadata(supplied))
if source in ("user_stated", "default_unknown_30") and supplied.get("rectified_range_rejected") == "clock_outside_chosen_side":
result["rectified_range_rejected"] = "clock_outside_chosen_side"
return result
def crosses_birth_window(before: float | None, after: float | None, window: dict) -> bool:
"""Check for a crossing in either direction, keeping the confirmed-minute rule inclusive."""
before_limit, after_limit = window["before_minutes"], window["after_minutes"]
if window.get("source") == "confirmed_minute":
return (before is not None and before <= before_limit
or after is not None and after <= after_limit)
return (before is not None and before < before_limit - _WINDOW_EDGE_TOLERANCE_MINUTES
or after is not None and after < after_limit - _WINDOW_EDGE_TOLERANCE_MINUTES)
def birth_window_label_zh(window: dict) -> str:
source = window["source"]
before, after = window["before_minutes"], window["after_minutes"]
if source == "confirmed_minute":
return f"已确认到分钟,按前后各 {before:g} 分钟核对"
if source == "default_unknown_30":
return f"没有填写误差,按前后各 {before:g} 分钟试算"
label = "按填写时给的范围"
if source == "rectified_range":
label = "按生时纠正选定的区间"
if "start_at" in window and "end_at" in window:
label += f" {window['start_at'][11:16]}–{window['end_at'][11:16]}"
return f"{label}(前 {round(before, 3):g} 分钟、后 {round(after, 3):g} 分钟)"
def birth_window_disclosure(window: dict, language: str = "zh") -> str:
if language == "en":
before, after = window["before_minutes"], window["after_minutes"]
if window["source"] == DEFAULT_WINDOW_SOURCE:
return f"Trial calculation from {before:g} minutes earlier to {after:g} minutes later. {DEFAULT_WINDOW_DISCLOSURE_EN}"
source = {"rectified_range": "Rectified interval", "confirmed_minute": "Confirmed minute"}.get(
window["source"], "Declared birth-time range"
)
return f"{source}: {before:g} minutes earlier to {after:g} minutes later."
label = birth_window_label_zh(window)
return f"{label}。{DEFAULT_WINDOW_DISCLOSURE_ZH}。" if window["source"] == DEFAULT_WINDOW_SOURCE else label