YAML configuration

LiR experiments are configured in YAML files. This page explains the structure of a YAML configuration, the types of values that may be used, and the extra features that the confidence package – on which LiR relies for parsing YAML files – provides. The Setting up an experiment page shows how these elements are used to define an experiment.

Sections and lists

A YAML file consists of keys and values. A key whose value is again a set of keys and values is called a section; a key whose value is an ordered collection is called a list.

Sections correspond to Python dictionaries, and lists correspond to Python lists. For example, in the minimal experiment setup below, experiments is a list of experiment definitions, and each experiment definition is a section with the subsections data, lrsystem and output:

output_path: ./output
experiments:
  - name: my_experiment
    strategy: single_run
    data:
      provider:
        method: glass
      splits:
        strategy: train_test_sources
        test_size: 0.5
    lrsystem:
      architecture: score_based
      preprocessing: standard_scaler
      pairing: source_pairs
      comparing: manhattan_distance
    output:
      - method: metrics
        columns:
          - cllr
          - cllr_min

Values and types

Scalar values in YAML are converted to their Python counterparts:

  • integers and floats, e.g. 3 and 0.5;

  • booleans, written as true or false (converted to True and False in Python);

  • strings, e.g. my_experiment (quoting is only needed when the string would otherwise be ambiguous);

  • null: a key without a value, or a key with the value null or ~, is converted to None in Python.

The null value is useful to explicitly leave an optional field empty, for example:

experiments:
  - name: architecture_evaluation
    strategy: grid
    lrsystem:               # no base LR system; it is substituted by the grid search
    lrsystem_parameters:
      ...

Comments may be added on any line with a # (at the start of the line, or preceded by a space): everything from the # to the end of the line is ignored.

Dots in declarations

Nested sections may also be declared with a dot in the key, as an alternative to nesting with indentation. The following two declarations are equivalent:

data:
  provider:
    method: glass
data.provider:
  method: glass

Dotted keys are expanded into nested sections anywhere in the configuration, except for keys inside a list, where they are kept as-is. In particular, keys of an experiment definition in the experiments list may not contain dots, so the nested form with indentation should be used there.

Only keys are affected by this: dots in values are not special. This is why a value such as sklearn.impute.SimpleImputer in a method field, or a path such as comparing.steps.calibration.bandwidth, is left as-is.

Anchors and aliases

YAML itself supports anchors and aliases for reusing a part of the file. An anchor names a value with &name; an alias *name inserts the anchored value at another location:

data: &data
  provider:
    method: glass

experiments:
  - name: validation
    data: *data

Anchors and aliases are resolved by the YAML parser, before LiR reads the file: an alias is fixed to the anchored value as it appears in the file. Unlike a reference (see below), an alias does not pick up later changes to the anchored section, such as an override with --set on the command line.

References

The confidence package supports references: a value enclosed in ${...} refers to another part of the configuration. References are always resolved from the root of the configuration, so any section may be referred to from anywhere in the file. This is useful to define a section once and reuse it in multiple experiments:

data:
  provider:
    method: glass

score_based_lrsystem:
  architecture: score_based
  ...

experiments:
  - name: validation
    strategy: single_run
    data: ${data}
    lrsystem: ${score_based_lrsystem}

The value of a reference is the value of the referenced section itself, including its type: a reference to the integer 5 evaluates to 5, not to the string '5'.

A reference may also point to a nested section by using a dot in the reference, e.g. ${section_name.subsection_name}:

data:
  provider:
    method: glass

short_name: ${data.provider.method}

A reference may be embedded in a larger value, in which case the result is always a string:

name: my_experiment
output_path: output/${name}_run

If a reference cannot be resolved, an error is raised that mentions the referenced key. A value that refers to itself, directly or indirectly (e.g. a: ${b} with b: ${a}), is rejected as well.

Built-in variables

LiR provides a predefined variable that may be referred to in any string value:

  • ${timestamp}: the date and time at which the experiment was started, in the format YYYY-MM-DD HH-MM-SS.

For example:

output_path: output/${timestamp}_glass_example

Overriding values on the command line

Values from the configuration file may be overridden on the command line with the --set option:

lir my_setup.yaml --set output_path=/tmp/results

Keys in --set may be dotted, e.g. --set data.provider.method=csv: the override is applied to the top-level sections of the configuration file, so it affects the experiments that refer to such a section. Note that values inside a list, such as the fields of an experiment definition in the experiments list, may not be overridden this way, and that command-line values are read as strings: --set data.splits.test_size=0.3 passes the string '0.3' to the field rather than the number 0.3, so the experiment fails when the field is used.

Combining multiple YAML files

The lir command also accepts a folder for an experiment setup argument, instead of a YAML file. In that case it reads and merges all YAML files in the folder. This makes it possible to separate common, reusable logic from variable parts; for example when you’re building a pipeline to be executed for a series of different datasets, requiring a different data setup.

### Example of use with symlinks (not working on Windows)

The directory structure might look as follows:

config/
   shared_pipeline.yaml
   experiment_a/
      data_setup.yaml
   experiment_b/
      data_setup.yaml

By creating a symlink of shared_pipeline.yaml in experiment_a and experiment_b, the shared pipeline logic does not have to be duplicated. The shared_pipeline.yaml can depend on (YAML) keys defined in data_setup.yaml.

A symlink can be created as follows, in this scenario:

  • for experiment_a: ln -s ../shared_pipeline.yaml config/experiment_a/shared_pipeline.yaml

  • for experiment_b: ln -s ../shared_pipeline.yaml config/experiment_b/shared_pipeline.yaml

The lir command can be executed as follows:

  • for experiment_a: lir config/experiment_a

  • for experiment_b: lir config/experiment_b