Store API¶
Persistence layer for activations, models, and runs.
mi_crow.store ¶
LocalStore ¶
LocalStore(
base_path="",
runs_prefix="runs",
dataset_prefix="datasets",
model_prefix="models",
)
Bases: Store
Local filesystem implementation of Store interface.
Initialize LocalStore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
Path | str
|
Base directory path for the store |
''
|
runs_prefix
|
str
|
Prefix for runs directory |
'runs'
|
dataset_prefix
|
str
|
Prefix for datasets directory |
'datasets'
|
model_prefix
|
str
|
Prefix for models directory |
'models'
|
Source code in src/mi_crow/store/local_store.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
Store ¶
Store(
base_path="",
runs_prefix="runs",
dataset_prefix="datasets",
model_prefix="models",
)
Bases: ABC
Abstract store optimized for tensor batches grouped by run_id.
This interface intentionally excludes generic bytes/JSON APIs. Implementations should focus on efficient safetensors-backed IO.
The store organizes data hierarchically: - Runs: Top-level grouping by run_id - Batches: Within each run, data is organized by batch_index - Layers: Within each batch, tensors are organized by layer_signature - Keys: Within each layer, tensors are identified by key (e.g., "activations")
Initialize Store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
Path | str
|
Base directory path for the store |
''
|
runs_prefix
|
str
|
Prefix for runs directory (default: "runs") |
'runs'
|
dataset_prefix
|
str
|
Prefix for datasets directory (default: "datasets") |
'datasets'
|
model_prefix
|
str
|
Prefix for models directory (default: "models") |
'models'
|
Source code in src/mi_crow/store/store.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
get_detector_metadata
abstractmethod
¶
get_detector_metadata(run_id, batch_index)
Load detector metadata with separate JSON and tensor store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Run identifier |
required |
batch_index
|
int
|
Batch index |
required |
Returns:
| Type | Description |
|---|---|
tuple[Dict[str, Any], TensorMetadata]
|
Tuple of (metadata dict, tensor_metadata dict). Returns empty dicts if not found. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid or metadata format is invalid |
JSONDecodeError
|
If metadata file exists but contains invalid JSON |
OSError
|
If tensor files exist but cannot be loaded |
Source code in src/mi_crow/store/store.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
get_detector_metadata_by_layer_by_key
abstractmethod
¶
get_detector_metadata_by_layer_by_key(
run_id, batch_index, layer, key
)
Get a specific tensor from detector metadata by layer and key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Run identifier |
required |
batch_index
|
int
|
Batch index |
required |
layer
|
str
|
Layer signature |
required |
key
|
str
|
Tensor key (e.g., "activations") |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The requested tensor |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid |
FileNotFoundError
|
If the tensor doesn't exist |
OSError
|
If tensor file exists but cannot be loaded |
Source code in src/mi_crow/store/store.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
get_run_metadata
abstractmethod
¶
get_run_metadata(run_id)
Load metadata for a run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Run identifier |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Metadata dictionary, or empty dict if not found |
Raises:
| Type | Description |
|---|---|
ValueError
|
If run_id is invalid |
JSONDecodeError
|
If metadata file exists but contains invalid JSON |
Source code in src/mi_crow/store/store.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
iter_run_batch_range ¶
iter_run_batch_range(run_id, *, start=0, stop=None, step=1)
Iterate run batches for indices in range(start, stop, step).
If stop is None, it will be set to max(list_run_batches(run_id)) + 1 (or 0 if none). Raises ValueError if step == 0 or start < 0.
Source code in src/mi_crow/store/store.py
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 | |
put_detector_metadata
abstractmethod
¶
put_detector_metadata(
run_id, batch_index, metadata, tensor_metadata
)
Save detector metadata with separate JSON and tensor store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Run identifier |
required |
batch_index
|
int
|
Batch index (must be non-negative) |
required |
metadata
|
Dict[str, Any]
|
JSON-serializable metadata dictionary (aggregated from all detectors) |
required |
tensor_metadata
|
TensorMetadata
|
Dictionary mapping layer_signature to dict of tensor_key -> tensor (from all detectors) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Full path key used for store (e.g., "runs/{run_id}/batch_{batch_index}") |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid or metadata is not JSON-serializable |
OSError
|
If file system operations fail |
Source code in src/mi_crow/store/store.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
put_run_detector_metadata
abstractmethod
¶
put_run_detector_metadata(
run_id, metadata, tensor_metadata
)
Save detector metadata for a whole run in a unified location.
This differs from put_detector_metadata which organises data
per-batch under runs/{run_id}/batch_{batch_index}.
put_run_detector_metadata instead stores everything under
runs/{run_id}/detectors. Implementations are expected to
support being called multiple times for the same run_id and
append / aggregate new metadata rather than overwrite it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Run identifier |
required |
metadata
|
Dict[str, Any]
|
JSON-serialisable metadata dictionary aggregated from all detectors for the current chunk / batch. |
required |
tensor_metadata
|
TensorMetadata
|
Dictionary mapping layer_signature to dict of tensor_key -> tensor (from all detectors). |
required |
Returns:
| Type | Description |
|---|---|
str
|
String path/key where metadata was stored |
str
|
(e.g. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid or metadata is not JSON‑serialisable. |
OSError
|
If file system operations fail. |
Source code in src/mi_crow/store/store.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
put_run_metadata
abstractmethod
¶
put_run_metadata(run_id, meta)
Persist metadata for a run (e.g., dataset/model identifiers).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Run identifier |
required |
meta
|
Dict[str, Any]
|
Metadata dictionary to save (must be JSON-serializable) |
required |
Returns:
| Type | Description |
|---|---|
str
|
String path/key where metadata was stored (e.g., "runs/{run_id}/meta.json") |
Raises:
| Type | Description |
|---|---|
ValueError
|
If run_id is invalid or meta is not JSON-serializable |
OSError
|
If file system operations fail |
Note
Implementations should store JSON at a stable location, e.g., runs/{run_id}/meta.json.
Source code in src/mi_crow/store/store.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |