Skip to content

Commit

Permalink
fix: move random number generation into numba
Browse files Browse the repository at this point in the history
Copy of what @amoodie did in DeltaRCM#27
  • Loading branch information
ericbarefoot committed May 22, 2020
1 parent acde2dc commit 2726176
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyDeltaRCM/init_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def import_files(self):
if self.seed is not None:
if self.verbose >= 2:
print("setting random seed to %s " % str(self.seed))
np.random.seed(self.seed)
shared_tools.set_random_seed(self.seed)

def set_constants(self):

Expand Down
8 changes: 8 additions & 0 deletions pyDeltaRCM/shared_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def get_jwalk():
[0, 0, 0],
[1, 1, 1]])

@njit
def set_random_seed(_seed):
np.random.seed(_seed)


@njit
def get_random_uniform(N):
return np.random.uniform(0, 1, N)

@njit
def get_flux_wt(qx, qy, ivec, jvec):
Expand Down
11 changes: 6 additions & 5 deletions tests/test_yaml_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pyDeltaRCM.deltaRCM_driver import pyDeltaRCM

from pyDeltaRCM.shared_tools import set_random_seed, get_random_uniform

# utilities for file writing
def create_temporary_file(tmp_path, file_name):
Expand Down Expand Up @@ -105,13 +106,13 @@ def test_random_seed_settings_value(tmp_path):
p, f = create_temporary_file(tmp_path, file_name)
write_parameter_to_file(f, 'seed', 9999)
f.close()
np.random.seed(9999)
_preval_same = np.random.uniform()
np.random.seed(5)
_preval_diff = np.random.uniform(1000)
set_random_seed(9999)
_preval_same = get_random_uniform(1)
set_random_seed(5)
_preval_diff = get_random_uniform(1000)
delta = pyDeltaRCM(input_file=p)
assert delta.seed == 9999
_postval_same = np.random.uniform()
_postval_same = get_random_uniform(1)
assert _preval_same == _postval_same


Expand Down

0 comments on commit 2726176

Please sign in to comment.