-
Notifications
You must be signed in to change notification settings - Fork 11
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
Numba for speed up #27
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b35069a
shift random choices out to numba funcs
3261170
use utils version
7683df3
shift new_cells ops out to standalone jitted functions
3cdcc5d
switch one random call in sed
0de5452
add a consistency check
4a463ce
add timer
01bfe2b
add numba dep
9fda2c2
set rng in numba, and make all calls to random in numba jitted functions
5edba66
addtl changes to defaults and plotting and timing
1c38f66
disable jitting for coverage tests
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 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 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 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 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 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 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,70 @@ | ||
|
||
import numpy as np | ||
|
||
import time | ||
import numba | ||
|
||
# utilities used in various places in the model and docs | ||
|
||
|
||
@numba.njit | ||
def set_random_seed(_seed): | ||
np.random.seed(_seed) | ||
|
||
|
||
@numba.njit | ||
def get_random_uniform(N): | ||
return np.random.uniform(0, 1, N) | ||
|
||
|
||
@numba.njit | ||
def random_pick(probs): | ||
""" | ||
Randomly pick a number weighted by array probs (len 8) | ||
Return the index of the selected weight in array probs | ||
""" | ||
|
||
# catch bad prob arrays | ||
if np.nansum(probs) == 0: | ||
probs[1, 1] = 0 | ||
for i, prob in np.ndenumerate(probs): | ||
if np.isnan(prob): | ||
probs[i] = 1 | ||
|
||
# prep array | ||
for i, prob in np.ndenumerate(probs): | ||
if np.isnan(prob): | ||
probs[i] = 0 | ||
|
||
cutoffs = np.cumsum(probs) | ||
v = np.random.uniform(0, cutoffs[-1]) | ||
idx = np.searchsorted(cutoffs, v) | ||
|
||
return int(idx) | ||
|
||
|
||
@numba.njit | ||
def random_pick_inlet(choices, probs=None): | ||
""" | ||
Randomly pick a number from array choices weighted by array probs | ||
Values in choices are column indices | ||
|
||
Return a tuple of the randomly picked index for row 0 | ||
""" | ||
|
||
if probs is None: | ||
probs = np.array([1. for i in range(len(choices))]) | ||
|
||
cutoffs = np.cumsum(probs) | ||
v = np.random.uniform(0, cutoffs[-1]) | ||
idx = np.searchsorted(cutoffs, v) | ||
|
||
return choices[idx] | ||
|
||
|
||
def _get_version(): | ||
""" | ||
Extract version number from single file, and make it availabe everywhere. | ||
""" | ||
from . import _version | ||
return _version.__version__() |
This file contains 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 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ scipy | |
netCDF4 | ||
basic_modeling_interface | ||
pyyaml | ||
numba |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems very similar in intent to the
shared_tools.py
file we have. Any reason they can't be combined? (I have no attachment to either name)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah this would eliminate the shared_tools file. I have this done in another commit, but haven't pushed it. I'll do some of the jitting again, on top of Eric's changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that numba doesn't support jitting classes in the way that we would like to use them (for example, they don't import inheritance) and so the way you've done it seems like the simplest way forward for us.