Menrva
Menrva is a small yet effective library to manage everything you need about configuration! It enables modular, composable and scriptable yaml configuration files. This allows to move the complexity of configuration management directly in the configuration itself and narrows the code logic that deals with it, leading to readable configuration and code.
Basic Usage
The most simple way to use Menrva is to load configuration from a .yaml file. Configuration is a folder containing a config file in yaml
# config/ folder contaning this file: config.yaml
# our configuration for a model
model:
name: unetmodel
args:
in_channels: 3
out_channels: 1
encoder: resnet18
# the training configuration
train:
n_epochs: 20
From code it can be loaded as follows
>>> from menrva.config import load_config
>>> cfg = load_config("config")
>>> print(cfg.model.args)
{'in_channels': 3, 'out_channels': 1, 'encoder': 'resnet18'}
Configuration can be overwritten by command line passing to the load_config function
the updates that can be easily obtained from the command line
>>> cfg = load_config(
"config",
from_cli=[
"model.args.encoder=resnet34",
"model.args.in_channels=1",
]
)
>>> print(cfg.model.args)
{'in_channels': 1, 'out_channels': 1, 'encoder': 'resnet34'}
Variable Interpolation
Yaml configuration allows for variables interpolation: dynamic variables whose value is evaluated when at runtime that specific parameter is retrieved. This allows to define in the config dependencies between variables that are automatically resolved. As an example let's say that we need to configure the preprocessing steps of a model depending from the currently executed step
step: train
model:
name: unetmodel
args:
in_channels: 3
out_channels: 1
encoder: resnet18
preprocessing: ${ _preprocess.${step} }
# the two different preprocessing steps
_preprocess:
train:
- resize
- random_crop
- noise
test:
- resize
>>> cfg = load_config("config")
>>> cfg.model.preprocessing
['resize', 'random_crop', 'noise']
>>> cfg.step = "test"
>>> cfg.model.preprocessing
['resize']
This approach simplifies the code that uses the configuration since it does not have to actively modify the configuration file that is always completely readable and not hidden in the code logic. In our example it allows to seamlessly switch between train and test configuration (note that you can set step=train or step=test from command line) In our example it allows to seamlessly switch between train and test configuration (note that you can set step=train or step=test from command line).
Modular Configuration
However, having the configuration in an unique file is not handy. Menrva allows to split the
configuration in multiple files and to freely compose them. Suppose that we have a model, and
we support testing on different scenes of different datasets. Each dataset however has its own set of required preprocessing operations. We can divide the the configuration as follows: a
root config.yaml file, a folder containing specific overrides for each supported dataset
and a folder scenes containing the specific scenes we want to test.
config/
config.yaml
overrides/
replica.yaml
tum.yaml
scenes/
tum/
xyz.yaml
replica/
room0.yaml
#### config.yaml
model:
name: unetmodel
args:
in_channels: 3
out_channels: 1
encoder: resnet18
dataset:
_root: datasets
# (searches for the configuration file under <key>/<value_path>
# and adds the founc config under the scope <key>). Fields are
# evaluated in order, thus `dataset.name` must be set by the
# chosen scene
$extend_config:
scene: tum/xyz
overrides: ${ dataset.name }
####
#### scene/tum/xyz.yaml
# this field will be in the cfg.scene scope
name: tum-xyz
# everything in in $override_config overrides fields starting
# from the root scope, thus cfg.dataset
$override_config:
dataset:
name: tum
seq_name: xyz
####
#### overrides/tum.yaml
$override_config:
dataset:
basedir: ${ dataset.root }/tum
args:
undistort: true
Following the example, dividing the configuration this way allows to factorize the configuration of all the scenes of the same dataset.
>>> cfg = menrva.config.load_config("config")
>>> cfg.scene.name
"tum-xyz"
>>> cfg.dataset.basedir, cfg.dataset.args.undistort
('datasets/tum', True)
Note that overrides are evaluated when the config is loaded, thus you can not change
cfg.scene to another scene directly from code. However you can do that when you
load it.
>>> cfg = menrva.config.load_config("config", from_cli=["scene=replica/room0"])
>>> cfg.scene.name
"replica-room0"
>>> cfg.dataset.basedir, cfg.dataset.args.undistort
('datasets/replica', False)
Save Configuration
Since the configuration can be changed at runtime or when loaded, for logging purposes Menrva allows to dump the loaded config on disk.
# save the config overwritten by the cli in the new replica-config folder
# (if loaded back it will be the same config, but without variable interpolation available, use it only for logging purposes)
>>> cfg = menrva.config.load_config("config", from_cli=["scene=replica/room0"], dump_cfg="replica-config")
>>> cfg.scene.name
"replica-room0"
>>> cfg.dataset.basedir, cfg.dataset.args.undistort
('datasets/replica', False)
Metadata
Release files for menrva-config 0.0.1
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 |
|---|---|---|---|---|
| menrva_config-0.0.1.tar.gz | 26.6 kB | Without provenance | Details for menrva_config-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| menrva_config-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for menrva_config-0.0.1-py3-none-any.whl |
Total release size: 53.5 kB
Release files / menrva_config-0.0.1.tar.gz
| Download URL | menrva_config-0.0.1.tar.gz |
|---|---|
| Size | 26.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
876541eb0a1f5a6a7af7aa3c6cc9501fdc052e299aee7103b68a8313186e88a5
|
|
BLAKE2b-256 checksum How to use checksums |
8406f728e2830503fec593a0612cd286040d028c3977d08edabc73bcb424ced7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / menrva_config-0.0.1-py3-none-any.whl
| Download URL | menrva_config-0.0.1-py3-none-any.whl |
|---|---|
| Size | 26.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a1100c553fb35a509a51d85a6487de95a83683d086298c5a970990e0ab5b36d6
|
|
BLAKE2b-256 checksum How to use checksums |
6b9ce84d679e5527c41194b29287d9d877e19daaded377c5a0d1a859c5f49bff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Security for menrva-config 0.0.1
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.