Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable %f pattern in now resolver #1287

Merged
merged 3 commits into from Jan 10, 2021
Merged

Enable %f pattern in now resolver #1287

merged 3 commits into from Jan 10, 2021

Conversation

forcecore
Copy link
Contributor

@forcecore forcecore commented Jan 10, 2021

Motivation

I sometimes manually run multiple instances of Hydra configured programs within a fraction of a second. In that case, their output directory's timestamp collide.

The user manual points to python2 strftime document which supports %f pattern (micro seconds), which would be one way of solving this issue.
https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
However, in Python 3,
https://docs.python.org/3/library/time.html?highlight=strftime#time.strftime
time.strftime does not implement %f format and datetime.stftime does.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

(How should this PR be tested? Do you require special setup to run the test or repro the fixed bug?)

Simple tutorial codes in the user manual would do the job

main.py:

#!/usr/bin/env python

import logging
import hydra
from omegaconf import OmegaConf

log = logging.getLogger(__name__)


@hydra.main(config_name='config')
def main(cfg):
    log.info(OmegaConf.to_yaml(cfg))
    log.debug("hello world!")


if __name__ == "__main__":
    main()

config.yaml:

db:
  driver: mysql
  user: omry
  password: secret

hydra:
  run:
    dir: ./outputs/${now:%Y%m%d_%H%M%S_%f}

Related Issues and PRs

(Is this PR part of a group of changes? Link the other relevant PRs and Issues here. Use https://help.github.com/en/articles/closing-issues-using-keywords for help on GitHub syntax)

None related

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 10, 2021
@forcecore forcecore changed the title Enable %f pattern for strftime Enable %f pattern in now resolver Jan 10, 2021
@shagunsodhani
Copy link
Contributor

Thanks @forcecore Could you add a test for checking that %f is correctly resolved? Maybe we could add this to the docs at https://hydra.cc/docs/next/configure_hydra/workdir

@forcecore
Copy link
Contributor Author

forcecore commented Jan 10, 2021

@shagunsodhani
I think I might write a test that uses %f and assert %f is not found in the actual run dir path name.
However, I'm unsure where to put the test. Could you recommend me where to put the test code?

Maybe along with
test_hydra.py: get_run_output(["tests/test_apps/run_dir_test/my_app.py"])
and write a new app like this?
"tests/test_apps/run_dir_test_microseconds/my_app.py"

@shagunsodhani
Copy link
Contributor

]
print(" ".join(cmd))
out, _err = get_run_output(cmd)
print(out)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the prints please.

Comment on lines 77 to 81
rundir = str(tmpdir / rundir)
cmd = [
"tests/test_apps/run_dir_test/my_app_microseconds.py",
f"hydra.run.dir={rundir}",
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use integration_test() instead of this decicated app.
Take a look at how it's being used in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to expect something exact, but with microseconds I'm unsure how timestamp will go?

Copy link
Collaborator

@omry omry Jan 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could print something like:
'%f' not in os.getcwd() and ensures that it's "True".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I might try assert_regex_match, then it seems to only have use cases with dedicated programs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could print something like:
'%f' not in os.getcwd() and ensures that it's "True".

OK I see

@forcecore
Copy link
Contributor Author

I think the new test works. It fails with the old code and passes with the new one.

Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please update the doc at https://hydra.cc/docs/configure_hydra/intro
  2. Please add a news fragment file. See this.

tests/test_hydra.py Show resolved Hide resolved
@forcecore
Copy link
Contributor Author

  • Document updated to point to Python 3's datetime.strftime
  • Added "1287.bugfix" with content Fixed hydra.job.id and hydra.job.num not getting passed to jobs in multirun

@omry
Copy link
Collaborator

omry commented Jan 10, 2021

omry and others added 2 commits January 10, 2021 02:10
	modified:   website/docs/configure_hydra/Intro.md
@forcecore
Copy link
Contributor Author

Doc link updated

Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

@omry omry merged commit 4d7c953 into facebookresearch:master Jan 10, 2021
@forcecore forcecore deleted the strftime_fix branch January 10, 2021 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants