AllenNeuralDynamics / spim-core

common features shared across Allen Institute SPIM systems
MIT License
0 stars 1 forks source link

YAML compatibility #23

Closed Poofjunior closed 1 year ago

Poofjunior commented 1 year ago

We can now use configs written as either YAML or TOML files.

I found a yaml reader/writer that:

Given that we have this working, I suggest we switch over to using YAMLs for our configs. To make that easy, this code can actually load TOMLs and save them as YAMLs if you rune config.save("config.yaml") where the file saved has a ".yaml" file extension--even if it was loaded from a TOML file. Otherwise, you can use this script (pasted at the end) to convert our existing TOML configs to a YAML config.

No changes are needed in the other repos to merge this request as-is.

Testing

I added a test file that you run with pytest. It tests loading/saving/member-access.

I can also confirm that the exa-spim-control code will simulate when running a yaml-style config.

TOML to YAML conversion script:

#!/usr/bin/env python3

import toml
from pathlib import Path
from ruamel.yaml import YAML
import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("toml_path", type=str, default=None,
                        help="input path to a toml file.")
    parser.add_argument("--yaml_path", type=str, default=None,
                        help="output path of the yaml file. " +
                             "Defaults to same folder as the toml file.")
    args = parser.parse_args()

    toml_path = Path(args.toml_path)
    if not toml_path.is_file():
        raise RuntimeError("Error: toml path must point to a toml file.")
    if args.yaml_path is None:
        yaml_path = Path(toml_path.parent /
                         Path(str(toml_path.name).rstrip("toml") + "yaml"))
    else:
        yaml_path = Path(args.yaml_path)
        if not yaml_path.is_file():
            raise RuntimeError("Error: yaml path must end with the yaml file "
                               f"to be created.\r\nPath is: {str(yaml_path)}")

    # Do the conversion.
    with open(str(toml_path), 'r') as toml_file:
        cfg_file = toml.load(toml_file)
        with open(str(yaml_path), 'w') as yaml_file:
            yaml = YAML()
            yaml.dump(cfg_file, yaml_file)

if __name__ == "__main__":
    main()
adamkglaser commented 1 year ago

Got it!

adamkglaser commented 1 year ago

Fine by me to merge