IDSIA / sacred

Sacred is a tool to help you configure, organize, log and reproduce experiments developed at IDSIA.
MIT License
4.21k stars 380 forks source link

Print comments if config from YAML file #229

Open timmeinhardt opened 6 years ago

timmeinhardt commented 6 years ago

Is there a way to print the comments in a YAML file when executing the print_config command? Currently this YAML file:

env_name: 'nn-train-v0'   # environment to train on (default: nn-train-v0)
algo: 'a2c'               # algorithm to use (default: a2c) ['a2c', 'ppo', 'acktr']

will produce an output like this:

Configuration (modified, added, typechanged, doc):
  algo = 'a2c'
  env_name = 'nn-train-v0'

Which unfortunately omits the comments.

Qwlouse commented 6 years ago

That would be pretty cool, I agree. Unfortunately, it seems like PyYAML discards all comments. It seems like ruamel might be able to handle comments, but I currently don't have time to check. If you figure out a way to load/save YAML with comments, then I'd be willing to extend config file handling to include them.

timmeinhardt commented 6 years ago

I just checked out the ruamel package and it indeed supports loading/dumping YAML comments. Here is a small example of how to load a YAML configuration and access its comments:

from ruamel.yaml import YAML

# could also be loaded from a file with `open("config.yaml")`
yaml_string = """\
# example
name:
    # details
    family: Smith   # very common
    given: Alice    # one of the siblings
"""

yaml = YAML()
code = yaml.load(yaml_string)
code['name']['given'] = 'Bob'

print(code.ca)
print(code.ca.items['name'][3][0].value)
print(code["name"].ca)

Output:

Comment(comment=[None, [CommentToken('# example\n', line: 0)]],
  items={'name': [None, None, None, [CommentToken('# details\n', line: 2)]]})
# details
Comment(comment=[None, [CommentToken('# details\n', line: 2)]],
  items={'family': [None, None, CommentToken('# very common\n', line: 3), None], 'given': [None, None, CommentToken('# one of the siblings\n', line: 4), None]})

The regular YAML entries are accessed as nested directories and the corresponding comments have to be retrieved with the ca attribute. Each CommentToken can be in either of 4 different places depending on the position of the comment in the YAML file.

More details on how to use the comments can be found here. Unfortunately the general documentation is not very thorough.