Reputation: 1516
I use hydra in a Python project. My yaml configuration file contains these lines:
now_dir: ${now:%Y-%m-%d}/${now:%H-%M-%S}
hydra:
run:
dir: ./hydra_outputs/${now_dir}
In my code I want to get the date and time used by hydra to create its output folder structure. It would be perfect if hydra had a built-in function that could return a str
or a Datetime
object containing the date generated from ${now:%Y-%m-%d}/${now:%H-%M-%S}
.
If not, I can still parse the current path set by hydra (which I can get with os.getcwd()
) or use the information in the Dictconfig
created by hydra from the yaml above, but it would be less convenient.
Upvotes: 2
Views: 2441
Reputation: 7639
See the docs on Accessing the Hydra config.
As of Hydra version 1.1, your options include:
output_dir: ${hydra:run.dir}
Note the colon inside the brackets in the above yaml code: this is a call to a custom resolver named "hydra".
In your python code, you'd be able to access this value via the output_dir
key on your main function's config:
@hydra.main()
def my_app(cfg: DictConfig) -> None:
print(cfg.output_dir)
from hydra.core.hydra_config import HydraConfig
@hydra.main()
def my_app(cfg: DictConfig) -> None:
print(HydraConfig.get().run.dir)
For consistent results, make sure that your access to the HydraConfig singleton happens within the bounds of your @hydra.main
-decorated function.
Upvotes: 1
Reputation: 1516
So the best way I found is to get the date from the path that hydra created with a regex:
>>> import re
>>> from datetime import datetime
>>> path
'hydra_outputs/2021-07-09/16-25-37/.hydra'
>>> match = re.search(r'\d{4}-\d{2}-\d{2}/\d{2}-\d{2}-\d{2}', path)
>>> datetime.strptime(match.group(), '%Y-%m-%d/%H-%M-%S')
datetime.datetime(2021, 7, 9, 16, 25, 37)
I leave the question open in case someone have a better solution.
Upvotes: 0
Reputation: 2300
What about using just a plain yaml anchor?
now_dir: &nowdir ${now:%Y-%m-%d}/${now:%H-%M-%S}
You will able to use the value *nowdir
later in the .yaml file.
Upvotes: 1