|
| 1 | +# API: Load |
| 2 | + |
| 3 | +This page covers the export reader API for loading files produced by [`export_batch`](api_export.md). |
| 4 | + |
| 5 | +Related pages: [API: Specs and Data Structures](api_specs.md), [API: Embedding](api_embedding.md), and [API: Export](api_export.md). |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## load_export (primary / recommended) { #load_export } |
| 10 | + |
| 11 | +### Signature |
| 12 | + |
| 13 | +```python |
| 14 | +load_export( |
| 15 | + path: Union[str, os.PathLike], |
| 16 | +) -> ExportResult |
| 17 | +``` |
| 18 | + |
| 19 | +Use `load_export(...)` to read any export produced by [`export_batch`](api_export.md) — both **combined** (single file) and **per-item** (directory) layouts are supported. The layout is detected automatically. |
| 20 | + |
| 21 | +### Mental Model |
| 22 | + |
| 23 | +`load_export(...)` answers one question: *where is the export?* |
| 24 | + |
| 25 | +- Pass a **file** (`.npz`, `.nc`, or `.json`) to load a **combined** export. |
| 26 | +- Pass a **directory** to load a **per-item** export. |
| 27 | + |
| 28 | +Everything else — layout detection, key parsing, NaN-fill for partial failures — is handled automatically. |
| 29 | + |
| 30 | +### Default Pattern |
| 31 | + |
| 32 | +```python |
| 33 | +from rs_embed import load_export |
| 34 | + |
| 35 | +# Combined export (single file) |
| 36 | +result = load_export("exports/run.npz") |
| 37 | + |
| 38 | +# Per-item export (directory of p00000.npz, p00001.npz, ...) |
| 39 | +result = load_export("exports/per_item_run/") |
| 40 | +``` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Parameters |
| 45 | + |
| 46 | +| Parameter | Meaning | |
| 47 | +| --------- | --------------------------------------------------------------------------------- | |
| 48 | +| `path` | Path to a `.npz`/`.nc`/`.json` file (combined) or a directory (per-item export). | |
| 49 | + |
| 50 | +### Raises |
| 51 | + |
| 52 | +| Exception | When | |
| 53 | +| ----------------- | --------------------------------------------------------------------- | |
| 54 | +| `FileNotFoundError` | Path does not exist. | |
| 55 | +| `ValueError` | Path exists but cannot be interpreted as an rs-embed export. | |
| 56 | +| `ImportError` | NetCDF export requested but `xarray` is not installed. | |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Return Value: ExportResult { #ExportResult } |
| 61 | + |
| 62 | +`load_export(...)` always returns an `ExportResult`. |
| 63 | + |
| 64 | +```python |
| 65 | +@dataclass |
| 66 | +class ExportResult: |
| 67 | + layout: str # "combined" or "per_item" |
| 68 | + spatials: list[dict] # one dict per spatial point |
| 69 | + temporal: dict | None # temporal spec used at export time |
| 70 | + n_items: int # number of spatial points |
| 71 | + status: str # "ok" | "partial" | "failed" |
| 72 | + models: dict[str, ModelResult] # keyed by model name |
| 73 | + manifest: dict # raw manifest for advanced use |
| 74 | +``` |
| 75 | + |
| 76 | +### Convenience Methods |
| 77 | + |
| 78 | +```python |
| 79 | +result.embedding("remoteclip") # → np.ndarray, shape (n_items, dim) |
| 80 | +result.ok_models # → list[str] — models with status "ok" |
| 81 | +result.failed_models # → list[str] — models with status "failed" |
| 82 | +``` |
| 83 | + |
| 84 | +`embedding(model)` raises `KeyError` if the model was not part of the export and `ValueError` if the model failed for every point. |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## ModelResult { #ModelResult } |
| 89 | + |
| 90 | +Each entry in `result.models` is a `ModelResult`: |
| 91 | + |
| 92 | +```python |
| 93 | +@dataclass |
| 94 | +class ModelResult: |
| 95 | + name: str # canonical model identifier |
| 96 | + status: str # "ok" | "partial" | "failed" |
| 97 | + embeddings: np.ndarray | None # (n_items, dim) or (n_items, C, H, W) |
| 98 | + inputs: np.ndarray | None # (n_items, C, H, W) — None if not saved |
| 99 | + meta: list[dict] # per-point embedding metadata |
| 100 | + error: str | None # error string for fully-failed models |
| 101 | +``` |
| 102 | + |
| 103 | +**Status values:** |
| 104 | + |
| 105 | +| Status | Meaning | |
| 106 | +| --------- | ------------------------------------------------ | |
| 107 | +| `"ok"` | All points succeeded. | |
| 108 | +| `"partial"` | Some points succeeded; failed points are NaN-filled in `embeddings`. | |
| 109 | +| `"failed"` | All points failed; `embeddings` is `None`. | |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Common Patterns |
| 114 | + |
| 115 | +### Load and inspect a combined export |
| 116 | + |
| 117 | +```python |
| 118 | +from rs_embed import load_export |
| 119 | + |
| 120 | +result = load_export("exports/combined_run.npz") |
| 121 | + |
| 122 | +print(result.n_items) # number of spatial points |
| 123 | +print(result.ok_models) # models that succeeded |
| 124 | +print(result.temporal) # {'start': '2022-06-01', 'end': '2022-09-01'} |
| 125 | + |
| 126 | +emb = result.embedding("remoteclip") # shape (n_items, dim) |
| 127 | +``` |
| 128 | + |
| 129 | +### Access inputs when save_inputs=True |
| 130 | + |
| 131 | +```python |
| 132 | +result = load_export("exports/combined_run.npz") |
| 133 | +mr = result.models["prithvi"] |
| 134 | +if mr.inputs is not None: |
| 135 | + print(mr.inputs.shape) # (n_items, C, H, W) |
| 136 | +``` |
| 137 | + |
| 138 | +### Load a per-item export directory |
| 139 | + |
| 140 | +```python |
| 141 | +result = load_export("exports/per_item_run/") |
| 142 | +print(result.layout) # "per_item" |
| 143 | +print(result.n_items) # number of files found |
| 144 | + |
| 145 | +emb = result.embedding("remoteclip") # (n_items, dim) — stacked from per-file arrays |
| 146 | +``` |
| 147 | + |
| 148 | +### Handle partial failures |
| 149 | + |
| 150 | +```python |
| 151 | +result = load_export("exports/combined_run.npz") |
| 152 | + |
| 153 | +if result.failed_models: |
| 154 | + print("Failed:", result.failed_models) |
| 155 | + |
| 156 | +for name in result.ok_models: |
| 157 | + emb = result.embedding(name) |
| 158 | + print(f"{name}: {emb.shape}") |
| 159 | +``` |
| 160 | + |
| 161 | +### Load via the manifest JSON |
| 162 | + |
| 163 | +Pass the `.json` manifest path if that is what you have — `load_export` finds the paired array file automatically: |
| 164 | + |
| 165 | +```python |
| 166 | +result = load_export("exports/combined_run.json") |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Relationship to export_batch |
| 172 | + |
| 173 | +`load_export` is the read-side counterpart to `export_batch`. Every file produced by `export_batch` can be read back with `load_export` without manually parsing NPZ keys or manifest JSON. |
| 174 | + |
| 175 | +```python |
| 176 | +from rs_embed import export_batch, load_export, ExportTarget, ExportConfig, PointBuffer, TemporalSpec |
| 177 | + |
| 178 | +# Write |
| 179 | +export_batch( |
| 180 | + spatials=[PointBuffer(121.5, 31.2, 2048)], |
| 181 | + temporal=TemporalSpec.range("2022-06-01", "2022-09-01"), |
| 182 | + models=["remoteclip"], |
| 183 | + target=ExportTarget.combined("exports/run"), |
| 184 | + config=ExportConfig(save_inputs=True), |
| 185 | +) |
| 186 | + |
| 187 | +# Read back |
| 188 | +result = load_export("exports/run.npz") |
| 189 | +emb = result.embedding("remoteclip") # shape (1, dim) |
| 190 | +``` |
| 191 | + |
| 192 | +!!! tip "Simple rule" |
| 193 | + Pass a file path for combined exports, a directory path for per-item exports. |
| 194 | + Everything else is automatic. |
0 commit comments