Legendarium Usage Guide
Legendarium is a Python library for storing experiment parameters and metrics in a compact, reloadable format. It provides a Legendarium writer and functions for loading experiments into pandas dataframes.
Installation
Install the published package with pip:
python -m pip install sylegendarium
For a local checkout, install the package in editable mode from the repository root:
python -m pip install -e .
The package requires Python, pandas, NumPy, tqdm, and rapidfuzz. Parquet support also requires a pandas-compatible Parquet engine, such as pyarrow:
python -m pip install pyarrow
Core Concepts
An experiment has:
- Parameters: values that describe the experiment and are repeated for its rows, such as the algorithm name or a configuration value.
- Metrics: values recorded for each
(run, step)pair. - Metadata: type, description, unit, and storage information written to the experiment metadata file.
Metric storage is selected from its declared type:
| Declared type | Storage | Recommended for |
|---|---|---|
int, float, str, bool |
Parquet | Scalar values and values that are efficient in columns |
numpy.ndarray |
zstd-compressed .npy (one per metric) |
Arrays with the same shape for every row in that metric |
list, dict, tuples, or other non-Parquet-safe values |
Heavy metrics | Variable-size or complex Python objects |
NumPy metrics accept np_dtype to reduce precision and memory, e.g. create_metric("map", np.ndarray, "Mean map", "%", np_dtype=np.float32). Rows are cast when written, so they are also kept at that precision in RAM.
A NumPy metric whose arrays do not all have the same shape is automatically moved to heavy storage during dataframe conversion. This preserves the values but is slower and uses more disk space.
Creating and Writing an Experiment
Declare parameters and metrics before writing rows:
from sylegendarium import Legendarium
import numpy as np
experiment = Legendarium(
experiment_name="navigation_run",
experiment_description="Navigation policy comparison",
path="experiments",
allow_overwrite=True,
verbose=True,
show_progress=True,
)
experiment.create_parameter("algorithm", "Greedy")
experiment.create_parameter("max_distance", 100.0)
experiment.create_metric(
"reward",
float,
"Reward obtained by the agent",
"points",
)
experiment.create_metric(
"map",
np.ndarray,
"Environment map",
"pixels",
)
experiment.create_metric(
"objects",
list,
"Objects detected at this step",
"objects",
)
with experiment:
for run in range(3):
for step in range(100):
experiment.write(
run=run,
step=step,
reward=float(np.random.rand()),
map=np.random.rand(100, 100),
objects=[f"object_{i}" for i in range(np.random.randint(1, 5))],
)
with experiment calls save() automatically when the block exits. This also happens if the block exits because of an exception. Call experiment.save() explicitly when using the object without a context manager.
Each call to write() must provide integer run and step values and one value for every declared metric.
Converting an Existing DataFrame
pd_to_experiment() is intended for dataframes loaded from older or external pipelines. The dataframe must contain run and step columns.
from sylegendarium import pd_to_experiment
pd_to_experiment(
data_reader,
experiment_name="newprocessing_experiment",
experiment_description="Converted final BGA dataset",
path="test",
verbose=True,
show_progress=True,
)
When the dataframe has no metadata attribute, Legendarium infers the metric type from the first non-null value in each column. In this case, columns other than run and step are treated as metrics.
For large dataframes, this function uses batch dataframe construction instead of appending one pandas row at a time. Progress output reports metric preparation, heavy-row preparation, and save phases.
To run silently:
pd_to_experiment(
data_reader,
experiment_name="quiet_conversion",
path="test",
show_progress=False,
)
Loading Experiments
Load one experiment:
from sylegendarium import load_experiment_pd
df = load_experiment_pd("navigation_run", "experiments")
print(df.head())
Load every experiment found in a directory:
from sylegendarium import load_experiments
all_runs = load_experiments("experiments")
print(all_runs.shape)
print(all_runs.columns)
Load only some metrics and/or cast floating-point NumPy metrics while loading. run, step and the parameters are always loaded:
df = load_experiment_pd("navigation_run", "experiments", columns=["map"], np_dtype=np.float32)
all_runs = load_experiments("experiments", columns=["reward"])
Only the requested NumPy and heavy metrics are decompressed, so this reduces both load time and RAM. np_dtype can also be a dict {metric_name: dtype}.
Experiments saved before version 1.1.0 (tar.gz with an NPZ file) are still loaded, but zlib decompression makes them slower. To convert one to the current format:
df = load_experiment_pd("navigation_run", "experiments", np_dtype=np.float32)
pd_to_experiment(df, "navigation_run", path="experiments_converted")
The returned dataframe includes the experiment metadata in its metadata attribute:
print(all_runs.metadata)
Older experiment files are detected and delegated to the legacy loader when possible.
Monitoring Progress and Time
Enable progress when creating a writer:
experiment = Legendarium(
"large_experiment",
"Large experiment",
"experiments",
show_progress=True,
verbose=True,
)
For pd_to_experiment(), use show_progress=True. The automatic save reports phases such as:
[Legendarium]: save [0.1s]: writing metadata
[Legendarium]: save [1.8s]: writing parquet
[Legendarium]: save [7.4s]: writing heavy metrics
[Legendarium]: save [42.0s]: creating tar archive
The progress bars cover metric preparation, heavy rows, categorical columns, NumPy metrics, and heavy rows. Parquet writing and archive compression are single library calls, so they report start and completion rather than a percentage.
Files Created
For an experiment named navigation_run, the output directory contains:
navigation_run.meta.yaml: experiment metadata and storage indexes.navigation_run.metrics.tar: archive containing the stored data (each member is already compressed, so the tar itself is not).- A Parquet file inside the archive for scalar and columnar data.
- One zstd-compressed
.npyfile per NumPy metric inside the archive (navigation_run.np_metric_<i>.npy.zst). Ifzstandardis not installed, a single NPZ file is written instead. - An LZMA-compressed pickle stream inside the archive when heavy metrics are present.
The archive is the primary data file. Do not delete the metadata file or archive independently.
Practical Recommendations
- Declare all parameters and metrics before entering the writing context.
- Use scalar metrics for values that can be represented as columns.
- Use NumPy metrics only when every row of that metric has the same array shape.
- Use heavy metrics for variable-size arrays, lists, dictionaries, and custom Python objects.
- For large legacy dataframes, prefer
pd_to_experiment()withshow_progress=Trueand allow the batch conversion to finish before profiling compression. - Restart a notebook kernel after changing the installed Legendarium source so the notebook does not keep an older imported module in memory.
Troubleshooting
Length of values does not match length of index
This usually indicates that heavy data was assigned one serialized row at a time instead of being accumulated for the whole dataframe. Use the current loader and regenerate the experiment if it was created by an older conversion implementation.
all input arrays must have the same shape
A NumPy metric contains arrays with incompatible shapes. Declare it as a heavy metric, or regenerate it with the current dataframe conversion, which automatically moves only that variable-shape metric to heavy storage.
Metadata
Release files for sylegendarium 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sylegendarium-1.1.0.tar.gz | 23.2 kB | Without provenance | Details for sylegendarium-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sylegendarium-1.1.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for sylegendarium-1.1.0-py2.py3-none-any.whl |
Total release size: 47.5 kB
Release files / sylegendarium-1.1.0.tar.gz
| Download URL | sylegendarium-1.1.0.tar.gz |
|---|---|
| Size | 23.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e861f627ca3a2895d4e90780f58714537c0555ad33c420db09d1ebfc904f540
|
|
BLAKE2b-256 checksum How to use checksums |
ee11b5734e99a954cc3f871faa3d4c3ff7e368369fb5c9077d91b966ca235cdd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.12
|
Release files / sylegendarium-1.1.0-py2.py3-none-any.whl
| Download URL | sylegendarium-1.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 24.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c10b407b5e651d1538b4d37554d0235495acccba64bc3524b2be90249db4dc1b
|
|
BLAKE2b-256 checksum How to use checksums |
19f73280d217c22cb96fc51ad16627f1976ffa46d788b4bdef2c17629ae1f098
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.12
|
Security for sylegendarium 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.