File size: 5,536 Bytes
ff4becd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""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",
]