-
Notifications
You must be signed in to change notification settings - Fork 359
Fixed #1153, Fixed #707 Track Random Seed #1154
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2df389e
Initial commit
seanlaw 0171f9c
Changed dtype
seanlaw 6fd085d
Added scraamp-related files
seanlaw 6b25f9a
Completed up to test_aampi.py
seanlaw 35a3cad
Completed seed tracking for all modules
seanlaw c6378c4
Force histogram distribution to use custom RNG
seanlaw 81b13ab
Added no cover
seanlaw 7561ecc
Addressed comments
seanlaw 86ebd7b
Added STUMPY_SEED support
seanlaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import os | ||
| from contextlib import contextmanager | ||
|
|
||
| import numpy as np | ||
|
|
||
| # Note that an initial SEED = 0 is disallowed | ||
| # in order to account for unit testing | ||
| if os.getenv("STUMPY_SEED") is not None: # pragma: no cover | ||
| SEED = int(os.getenv("STUMPY_SEED")) | ||
| else: | ||
| SEED = np.random.randint(1, 4_294_967_296, dtype=np.uint32) | ||
| RNG = np.random.RandomState(seed=SEED) | ||
|
|
||
|
|
||
| def set_seed(seed): | ||
| """ | ||
| Permanently set the RNG seed to a different value | ||
|
|
||
| Parameters | ||
| ---------- | ||
| seed : int | ||
| The random seed for (permanently) setting the random number generator to | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| global SEED | ||
| global RNG | ||
| SEED = seed | ||
| RNG = np.random.RandomState(seed=SEED) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def fix_seed(seed): | ||
| """ | ||
| A context manager for setting the RNG seed to a fixed, hardcoded, safe seed | ||
| and then returning the RNG back to its previous state prior to the seed change | ||
|
|
||
| This is typically used when you want to generate a specific random sequence once. | ||
| To repeat the same random sequence, use `fix_state` instead. If you are picking | ||
| a random seed directly before calling `fix_seed` then you probably want to use | ||
| `fix_state` instead! | ||
|
|
||
| Parameters | ||
| ---------- | ||
| seed : int | ||
| The random seed for (temporarily) setting the random number generator to | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| curr_state = RNG.get_state() | ||
| RNG.seed(seed) | ||
|
NimaSarajpoor marked this conversation as resolved.
|
||
| try: | ||
| yield | ||
| finally: | ||
| RNG.set_state(curr_state) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def fix_state(): | ||
| """ | ||
| A context manager for setting the RNG state to a fixed, hardcoded, safe state | ||
| and then returning the RNG back to its previous state prior to the state change | ||
|
|
||
| This is typically used when you want to repeat the same random sequence more than | ||
| once. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| None | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| curr_state = RNG.get_state() | ||
| try: | ||
| yield | ||
| finally: | ||
| RNG.set_state(curr_state) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.