File size: 3,839 Bytes
10e7c2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MagBridge-Battery v1.0 — minimal loader example.

Run from the bundle root:
    python load_example.py

Requires: pandas, pyarrow.
Licensed under Apache-2.0 (see LICENSE-CODE).
"""
from __future__ import annotations

import json
from pathlib import Path

import numpy as np
import pandas as pd


def load_bundle(base: Path) -> tuple[pd.DataFrame, dict, dict, dict]:
    """Load all shards, both split files, and the manifest.

    Returns
    -------
    df : pd.DataFrame
        Concatenated shards. One row per sample. Signal columns hold length-100
        arrays.
    primary_split : dict
        Cell-disjoint, leakage-free split. Keys include 'train_samples',
        'val_samples', 'test_samples', and 'split_guarantee'.
    optimistic_split : dict
        Intentionally leaky baseline. Do not use for reporting; see its
        'warning' field.
    manifest : dict
        Provenance, hashes, bridge config.
    """
    shards = sorted((base / "data").glob("shard_*.parquet"))
    if not shards:
        raise FileNotFoundError(f"No shards found under {base / 'data'}")
    df = pd.concat([pd.read_parquet(s) for s in shards], ignore_index=True)

    primary_split = json.loads((base / "splits" / "by_cell_primary.json").read_text())
    optimistic_split = json.loads((base / "splits" / "by_record_optimistic_baseline.json").read_text())
    manifest = json.loads((base / "manifest.json").read_text())

    return df, primary_split, optimistic_split, manifest


def apply_split(df: pd.DataFrame, split: dict) -> dict[str, pd.DataFrame]:
    """Slice df into train/val/test using a split dict."""
    by_id = df.set_index("sample_id", drop=False)
    out = {}
    for subset in ("train", "val", "test"):
        ids = split[f"{subset}_samples"]
        out[subset] = by_id.loc[by_id.index.intersection(ids)].reset_index(drop=True)
    return out


def stack_signals(df: pd.DataFrame, channels: list[str] | None = None) -> np.ndarray:
    """Stack signal columns into a (N, T, C) numpy array.

    Default channels: the six signal channels. ``time_norm`` is omitted because
    it is constant across samples (a fixed reference grid) and adds no
    per-sample information.
    """
    if channels is None:
        channels = ["B_s1Y", "B_s1Z", "B_s2Y", "B_s2Z", "B_s1C5", "B_s2C6"]
    arrays = [np.stack(df[c].values) for c in channels]  # each (N, T)
    return np.stack(arrays, axis=-1)  # (N, T, C)


def main() -> None:
    base = Path(__file__).parent.resolve()

    df, primary, optimistic, manifest = load_bundle(base)

    print(f"Dataset: {manifest['dataset_name']} v{manifest['dataset_version']}")
    print(f"Schema: {manifest['schema_version']}")
    print(f"Generated: {manifest['generated_at_utc']}")
    print(f"Total samples loaded: {len(df)}")
    print()

    splits = apply_split(df, primary)
    print("Primary (cell-disjoint) split:")
    for name, sub in splits.items():
        print(f"  {name:5s}: {len(sub):5d} samples")
    print()
    print("Split guarantee:")
    print(f"  {primary['split_guarantee'][:120]}...")
    print()

    # Show that the optimistic split is shipped with a clear warning
    print("Optimistic split warning (first 120 chars):")
    print(f"  {optimistic['warning'][:120]}...")
    print(f"  leakage_stats: {optimistic.get('leakage_stats', {})}")
    print()

    # Stack train signals into a tensor
    X_train = stack_signals(splits["train"])
    print(f"Train signal tensor shape: {X_train.shape}  (N, T, C)")
    print(f"  channels = ['B_s1Y','B_s1Z','B_s2Y','B_s2Z','B_s1C5','B_s2C6']")

    # Show that Regime-B has missing SOH by design
    regime_b = df[df["anomaly_subtype"] == "low_voltage_regime_B"]
    print()
    print(f"Regime-B samples: {len(regime_b)}; SOH missing: {regime_b['soh'].isna().sum()} (expected = all)")


if __name__ == "__main__":
    main()