Source code for pycsamt.api.ordering
"""Package-wide site ordering policy.
Configure once and every loader/processor that normalizes through
``ensure_sites`` uses the same policy::
from pycsamt.api import configure_ordering
configure_ordering(mode="auto")
Per-call ``order_by=...`` arguments remain authoritative overrides.
"""
from __future__ import annotations
import copy
from collections.abc import Generator
from contextlib import contextmanager
from dataclasses import dataclass, fields
from typing import Any
__all__ = [
"SiteOrderingConfig",
"PYCSAMT_ORDERING",
"configure_ordering",
"reset_ordering",
]
_ALIASES = {
"name": "station",
"natural": "station",
"lat": "latitude",
"lon": "longitude",
"profile": "chainage",
"spatial": "chainage",
"none": "input",
"preserve": "input",
}
_MODES = {"auto", "chainage", "input", "station", "latitude", "longitude"}
[docs]
@dataclass
class SiteOrderingConfig:
"""Global site-ordering strategy and automatic-line thresholds."""
mode: str = "auto"
min_linearity: float = 0.95
max_cross_track_ratio: float = 0.15
min_coordinate_fraction: float = 0.60
[docs]
@contextmanager
def context(self, **kw: Any) -> Generator[SiteOrderingConfig, None, None]:
"""Temporarily override ordering settings, then restore them."""
snapshot = self.clone()
try:
self.configure(**kw)
yield self
finally:
for field in fields(self):
setattr(self, field.name, getattr(snapshot, field.name))
[docs]
def reset(self) -> None:
"""Restore package defaults."""
defaults = SiteOrderingConfig()
for field in fields(self):
setattr(self, field.name, getattr(defaults, field.name))
[docs]
def clone(self) -> SiteOrderingConfig:
"""Return an independent copy."""
return copy.deepcopy(self)
PYCSAMT_ORDERING = SiteOrderingConfig()
[docs]
def reset_ordering() -> None:
"""Reset the global ordering policy to ``mode='auto'``."""
PYCSAMT_ORDERING.reset()