MacroLens / code /macrolens /__init__.py
itouchz's picture
Duplicate from macrolens/MacroLens
ff4becd
Raw
History Blame Contribute Delete
5.54 kB
"""MacroLens — public unified API (v0.2).
The 10-line workflow::
import macrolens as ml
X_train, y_train, meta_train = ml.load("T1", "train", granularity="daily")
X_test, y_test, meta_test = ml.load("T1", "test")
model = ml.methods.LightGBMRegressor(task="T1")
model.fit(X_train, y_train, seed=42)
y_pred = model.predict(X_test)
metrics = ml.score("T1", y_test, y_pred,
cluster_keys=meta_test["ticker"].values)
print(metrics["mse"].value, metrics["mse"].ci_lo, metrics["mse"].ci_hi)
Public surface
--------------
* :func:`load` — sklearn-style ``(X, y, meta)`` data layer.
* :func:`score` / :func:`compare_methods` — eval layer.
* :func:`info` / :func:`features` — benchmark metadata.
* :func:`list_methods` — registered method names (filterable by family / task).
* :data:`methods` — sub-namespace; ``ml.methods.<ClassName>(task=...)``.
* :class:`LoadedData`, :class:`MetricValue`, :class:`RunRecord` — types.
Legacy v0.1 entry points (``load_tsf``, ``to_arrays``, ``evaluate``,
``ask_lumina``, ...) remain importable during the v0.1 → v0.2 transition;
they will be removed in Phase 7.
"""
from __future__ import annotations
# ── v0.2 unified API (primary surface) ────────────────────────────────────
from . import methods # noqa: F401 (sub-namespace; ml.methods.<Name>)
from ._types import LoadedData, MetricValue, RunRecord
from .data import load
from .eval import compare_methods, score
from .meta import BENCHMARK_NAME, __version__, features, info
from .methods import ALL_METHODS, list_methods
# ── Legacy v0.1 entry points (transitional) ───────────────────────────────
# These are imported lazily below so the new public surface stays usable
# even when the legacy modules grow new dependencies. Failures during the
# transitional period are captured and surfaced as ImportError on first
# attribute access (rather than crashing every ``import macrolens`` call).
def _import_legacy() -> dict[str, object]:
out: dict[str, object] = {}
try:
from ._evaluate import evaluate, format_submission
out["evaluate"] = evaluate
out["format_submission"] = format_submission
except Exception: # pragma: no cover -- legacy module surface drift
pass
try:
from ._fast import TSFTorchDataset, load_torch, to_arrays
out["TSFTorchDataset"] = TSFTorchDataset
out["load_torch"] = load_torch
out["to_arrays"] = to_arrays
# legacy `features` function on _fast shadowed by meta.features in
# the v0.2 surface; expose under a private alias for back-compat.
from ._fast import features as _legacy_features
out["_legacy_features"] = _legacy_features
except Exception: # pragma: no cover
pass
try:
from ._loaders import load_panel, load_scenarios, load_task, load_tsf
out["load_panel"] = load_panel
out["load_scenarios"] = load_scenarios
out["load_task"] = load_task
out["load_tsf"] = load_tsf
except Exception: # pragma: no cover
pass
try:
from ._meta import BENCHMARK_VERSION
out["BENCHMARK_VERSION"] = BENCHMARK_VERSION
except Exception: # pragma: no cover
pass
try:
from ._types import (
BenchmarkInfo,
GenerationMetrics,
REValuationMetrics,
ScenarioMetrics,
TaskSample,
TSFMetrics,
TSFSample,
ValuationMetrics,
)
out["BenchmarkInfo"] = BenchmarkInfo
out["GenerationMetrics"] = GenerationMetrics
out["REValuationMetrics"] = REValuationMetrics
out["ScenarioMetrics"] = ScenarioMetrics
out["TaskSample"] = TaskSample
out["TSFMetrics"] = TSFMetrics
out["TSFSample"] = TSFSample
out["ValuationMetrics"] = ValuationMetrics
except Exception: # pragma: no cover
pass
return out
_LEGACY = _import_legacy()
def __getattr__(name: str):
"""Resolve legacy attributes lazily (and ``ask_lumina`` even more so)."""
if name in _LEGACY:
return _LEGACY[name]
if name == "ask_lumina":
# The lumina agent imports openrouter / vector store deps that may
# not be installed in CPU-only paper-scope environments. Defer the
# import to first call.
from ..agents.lumina import ask as ask_lumina
return ask_lumina
if name == "lakehouse":
def _lakehouse(tag: str = "macrolens-v1.0"):
from ..lakehouse import Client
return Client.from_release(tag)
return _lakehouse
raise AttributeError(f"module 'macrolens' has no attribute {name!r}")
__all__ = [
# v0.2 unified API
"load",
"score",
"compare_methods",
"info",
"features",
"list_methods",
"methods",
"ALL_METHODS",
"LoadedData",
"MetricValue",
"RunRecord",
"BENCHMARK_NAME",
"__version__",
# legacy (lazy)
"evaluate",
"format_submission",
"TSFTorchDataset",
"load_torch",
"to_arrays",
"load_panel",
"load_scenarios",
"load_task",
"load_tsf",
"ask_lumina",
"lakehouse",
"BENCHMARK_VERSION",
"BenchmarkInfo",
"GenerationMetrics",
"REValuationMetrics",
"ScenarioMetrics",
"TaskSample",
"TSFMetrics",
"TSFSample",
"ValuationMetrics",
]