Storage¶
Stores persist run records and artifacts. metalab supports file-based storage for local development and PostgreSQL for production deployments.
FileStore¶
metalab.FileStore ¶
Filesystem-based storage backend.
Provides atomic writes and per-run locking for concurrent access. Uses FileStoreLayout for all path construction.
Create via FileStoreConfig:
Initialize from config.
Use FileStoreConfig(...).connect() to create instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileStoreConfig
|
The FileStoreConfig for this store. |
required |
delete_run ¶
Delete a run and all its artifacts/logs/derived metrics/results.
get_derived ¶
Retrieve derived metrics for a run.
get_experiment_manifest ¶
Retrieve experiment manifest by ID (most recent version).
get_log ¶
Retrieve a log file.
Searches for logs matching the run_id. Handles legacy formats.
get_log_path ¶
Get the path where a log file should be written.
Enables streaming loggers to write directly to the store.
get_result ¶
Retrieve structured result data.
get_working_directory ¶
Return the root directory of this store.
Implements SupportsWorkingDirectory capability.
list_logs ¶
List available log names for a run.
Returns a list of log names (e.g., ["run", "stdout", "stderr"]).
list_run_records ¶
List run records, optionally filtered by experiment.
open_artifact ¶
Open an artifact for reading.
Implements SupportsArtifactOpen capability.
put_artifact ¶
Store an artifact.
put_derived ¶
Persist derived metrics for a run.
put_experiment_manifest ¶
put_experiment_manifest(experiment_id: str, manifest: dict[str, Any], timestamp: str | None = None) -> None
Store an experiment manifest.
put_result ¶
put_result(run_id: str, name: str, data: Any, dtype: str | None = None, shape: list[int] | None = None, metadata: dict[str, Any] | None = None) -> None
Store structured result data for a run.
Results are stored in JSON format at results/{run_id}/{name}.json.
scoped ¶
Return a store scoped to the given experiment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_id
|
str
|
The experiment identifier. |
required |
Returns:
| Type | Description |
|---|---|
'FileStore'
|
A new FileStore scoped to the experiment. |
metalab.FileStoreConfig
dataclass
¶
Bases: StoreConfig
Configuration for FileStore.
Example:
config = FileStoreConfig(root="./experiments")
scoped = config.scoped("my_exp:1.0")
store = scoped.connect()
# Or let runner handle scoping:
metalab.run(exp, store=config) # auto-scopes to experiment
for_experiment ¶
Get a scoped config for a specific experiment.
Alias for scoped() with a clearer name for loading/browsing context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_id
|
str
|
The experiment ID to scope to. |
required |
Returns:
| Type | Description |
|---|---|
'FileStoreConfig'
|
A new FileStoreConfig scoped to the experiment. |
Example:
from_locator
classmethod
¶
Parse file:// locator into config.
list_experiments ¶
List all experiment IDs in this collection.
Discovers experiments by scanning subdirectories for _meta.json files that contain experiment_id. Only works on unscoped configs.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of experiment IDs found in this collection. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called on a scoped config. |
Example:
PostgresStore¶
For query-accelerated storage. Requires the postgres extra (uv add metalab[postgres]).
metalab.store.postgres.PostgresStoreConfig
dataclass
¶
PostgresStoreConfig(*, experiment_id: str | None = None, connection_string: str, file_root: str, schema: str = 'public', auto_migrate: bool = True, connect_timeout: float = 10.0, pool_min_size: int = 1, pool_max_size: int = 2)
Bases: StoreConfig
Configuration for PostgresStore.
Example:
config = PostgresStoreConfig(
connection_string="postgresql://localhost/metalab",
file_root="/data/experiments",
)
scoped = config.scoped("my_exp:1.0")
store = scoped.connect()
# Or let runner handle scoping:
metalab.run(exp, store=config) # auto-scopes to experiment
connect ¶
Create a PostgresStore from this config.
If the connection_string lacks credentials, attempts to discover them from service.json at {file_root}/services/postgres/service.json.
for_experiment ¶
Get a scoped config for a specific experiment.
Alias for scoped() with a clearer name for loading/browsing context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_id
|
str
|
The experiment ID to scope to. |
required |
Returns:
| Type | Description |
|---|---|
'PostgresStoreConfig'
|
A new PostgresStoreConfig scoped to the experiment. |
from_locator
classmethod
¶
Parse postgresql:// locator into config.
list_experiments ¶
List all experiment IDs in this collection.
Discovers experiments by scanning subdirectories of file_root for _meta.json files that contain experiment_id. Only works on unscoped configs.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of experiment IDs found in this collection. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If called on a scoped config. |