Skip to content

Commit 43b52e4

Browse files
Merge pull request #1858 from OceanParcels/remove_ParcelsRandom
Removing ParcelsRandom and rng for v4
2 parents 870b4f3 + 04bb031 commit 43b52e4

File tree

12 files changed

+144
-430
lines changed

12 files changed

+144
-430
lines changed

docs/documentation/index.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ Documentation and Tutorials
33

44
Parcels has several documentation and tutorial Jupyter notebooks and scripts which go through various aspects of Parcels. Static versions of the notebooks are available below via the gallery in the site, with the interactive notebooks being available either completely online at the following `Binder link <https://mybinder.org/v2/gh/OceanParcels/parcels/main?labpath=docs%2Fexamples%2Fparcels_tutorial.ipynb>`_. Following the gallery of notebooks is a list of scripts which provide additional examples to users. You can work with the example notebooks and scripts locally by downloading :download:`parcels_tutorials.zip </_downloads/parcels_tutorials.zip>` and running with your own Parcels installation.
55

6-
.. warning::
7-
In v3.1.0 we updated kernels in the tutorials to use ``parcels.ParcelsRandom`` instead of ``from parcels import ParcelsRandom``. Due to our C-conversion code, using ``parcels.ParcelsRandom`` only works with v3.1.0+. When browsing/downloading the tutorials, it's important that you are using the documentation corresponding to the version of Parcels that you have installed. You can find which parcels version you have installed by doing ``import parcels`` followed by ``print(parcels.__version__)``. If you don't want to use the latest version of Parcels, you can browse prior versions of the documentation by using the version switcher in the bottom right of this page.
8-
96
.. nbgallery::
107
:caption: Overview
118
:name: tutorial-overview

docs/examples/example_brownian.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import random
12
from datetime import timedelta
23

34
import numpy as np
@@ -27,11 +28,11 @@ def test_brownian_example(mesh, npart=3000):
2728
)
2829

2930
# Set random seed
30-
parcels.ParcelsRandom.seed(123456)
31+
random.seed(123456)
3132

3233
runtime = timedelta(days=1)
3334

34-
parcels.ParcelsRandom.seed(1234)
35+
random.seed(1234)
3536
pset = parcels.ParticleSet(
3637
fieldset=fieldset,
3738
pclass=parcels.ScipyParticle,

docs/examples/tutorial_delaystart.ipynb

Lines changed: 85 additions & 84 deletions
Large diffs are not rendered by default.

docs/examples/tutorial_diffusion.ipynb

Lines changed: 38 additions & 22 deletions
Large diffs are not rendered by default.

docs/reference/misc.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
Miscellaneous
22
=============
33

4-
parcels.rng module
5-
------------------
6-
7-
.. automodule:: parcels.rng
8-
:members:
9-
:undoc-members:
10-
114
parcels.tools.statuscodes module
125
--------------------------------
136

parcels/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
__version__ = version
44

5-
import parcels.rng as ParcelsRandom # noqa: F401
65
from parcels.application_kernels import *
76
from parcels.field import *
87
from parcels.fieldset import *

parcels/application_kernels/advectiondiffusion.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"""
55

66
import math
7-
8-
import parcels
7+
import random
98

109
__all__ = ["AdvectionDiffusionEM", "AdvectionDiffusionM1", "DiffusionUniformKh"]
1110

@@ -26,8 +25,8 @@ def AdvectionDiffusionM1(particle, fieldset, time): # pragma: no cover
2625
mean and a standard deviation of sqrt(dt).
2726
"""
2827
# Wiener increment with zero mean and std of sqrt(dt)
29-
dWx = parcels.rng.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
30-
dWy = parcels.rng.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
28+
dWx = random.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
29+
dWy = random.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
3130

3231
Kxp1 = fieldset.Kh_zonal[time, particle.depth, particle.lat, particle.lon + fieldset.dres]
3332
Kxm1 = fieldset.Kh_zonal[time, particle.depth, particle.lat, particle.lon - fieldset.dres]
@@ -61,8 +60,8 @@ def AdvectionDiffusionEM(particle, fieldset, time): # pragma: no cover
6160
mean and a standard deviation of sqrt(dt).
6261
"""
6362
# Wiener increment with zero mean and std of sqrt(dt)
64-
dWx = parcels.rng.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
65-
dWy = parcels.rng.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
63+
dWx = random.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
64+
dWy = random.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
6665

6766
u, v = fieldset.UV[time, particle.depth, particle.lat, particle.lon]
6867

@@ -102,8 +101,8 @@ def DiffusionUniformKh(particle, fieldset, time): # pragma: no cover
102101
mean and a standard deviation of sqrt(dt).
103102
"""
104103
# Wiener increment with zero mean and std of sqrt(dt)
105-
dWx = parcels.rng.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
106-
dWy = parcels.rng.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
104+
dWx = random.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
105+
dWy = random.normalvariate(0, math.sqrt(math.fabs(particle.dt)))
107106

108107
bx = math.sqrt(2 * fieldset.Kh_zonal[particle])
109108
by = math.sqrt(2 * fieldset.Kh_meridional[particle])

parcels/kernel.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
import numpy as np
1212

13-
import parcels.rng as ParcelsRandom # noqa: F401
14-
from parcels import rng # noqa: F401
1513
from parcels.application_kernels.advection import (
1614
AdvectionAnalytical,
1715
AdvectionRK4_3D,
@@ -137,9 +135,7 @@ def __init__(
137135
self.funcvars = None
138136
self.funccode = funccode or inspect.getsource(pyfunc.__code__)
139137
self.funccode = ( # Remove parcels. prefix (see #1608)
140-
self.funccode.replace("parcels.rng", "rng")
141-
.replace("parcels.ParcelsRandom", "ParcelsRandom")
142-
.replace("parcels.StatusCode", "StatusCode")
138+
self.funccode.replace("parcels.StatusCode", "StatusCode")
143139
)
144140

145141
# Parse AST if it is not provided explicitly
@@ -152,8 +148,6 @@ def __init__(
152148
try:
153149
user_ctx = stack[-1][0].f_globals
154150
user_ctx["math"] = globals()["math"]
155-
user_ctx["ParcelsRandom"] = globals()["ParcelsRandom"]
156-
user_ctx["rng"] = globals()["rng"]
157151
user_ctx["random"] = globals()["random"]
158152
user_ctx["StatusCode"] = globals()["StatusCode"]
159153
except:

parcels/rng.py

Lines changed: 0 additions & 194 deletions
This file was deleted.

tests/test_diffusion.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import random
12
from datetime import timedelta
23

34
import numpy as np
@@ -9,7 +10,6 @@
910
AdvectionDiffusionM1,
1011
DiffusionUniformKh,
1112
Field,
12-
ParcelsRandom,
1313
ParticleSet,
1414
RectilinearZGrid,
1515
ScipyParticle,
@@ -33,7 +33,7 @@ def test_fieldKh_Brownian(mesh):
3333
npart = 1000
3434
runtime = timedelta(days=1)
3535

36-
ParcelsRandom.seed(1234)
36+
random.seed(1234)
3737
pset = ParticleSet(fieldset=fieldset, pclass=ScipyParticle, lon=np.zeros(npart), lat=np.zeros(npart))
3838
pset.execute(pset.Kernel(DiffusionUniformKh), runtime=runtime, dt=timedelta(hours=1))
3939

@@ -43,7 +43,7 @@ def test_fieldKh_Brownian(mesh):
4343
lats = pset.lat
4444
lons = pset.lon
4545

46-
tol = 200 * mesh_conversion # effectively 200 m errors
46+
tol = 500 * mesh_conversion # effectively 500 m errors
4747
assert np.allclose(np.std(lats), expected_std_lat, atol=tol)
4848
assert np.allclose(np.std(lons), expected_std_lon, atol=tol)
4949
assert np.allclose(np.mean(lons), 0, atol=tol)
@@ -71,7 +71,7 @@ def test_fieldKh_SpatiallyVaryingDiffusion(mesh, kernel):
7171
npart = 100
7272
runtime = timedelta(days=1)
7373

74-
ParcelsRandom.seed(1636)
74+
random.seed(1636)
7575
pset = ParticleSet(fieldset=fieldset, pclass=ScipyParticle, lon=np.zeros(npart), lat=np.zeros(npart))
7676
pset.execute(pset.Kernel(kernel), runtime=runtime, dt=timedelta(hours=1))
7777

@@ -92,15 +92,15 @@ def test_randomexponential(lambd):
9292
fieldset.lambd = lambd
9393

9494
# Set random seed
95-
ParcelsRandom.seed(1234)
95+
random.seed(1234)
9696

9797
pset = ParticleSet(
9898
fieldset=fieldset, pclass=ScipyParticle, lon=np.zeros(npart), lat=np.zeros(npart), depth=np.zeros(npart)
9999
)
100100

101101
def vertical_randomexponential(particle, fieldset, time): # pragma: no cover
102102
# Kernel for random exponential variable in depth direction
103-
particle.depth = ParcelsRandom.expovariate(fieldset.lambd)
103+
particle.depth = random.expovariate(fieldset.lambd)
104104

105105
pset.execute(vertical_randomexponential, runtime=1, dt=1)
106106

@@ -120,15 +120,15 @@ def test_randomvonmises(mu, kappa):
120120
fieldset.kappa = kappa
121121

122122
# Set random seed
123-
ParcelsRandom.seed(1234)
123+
random.seed(1234)
124124

125125
AngleParticle = ScipyParticle.add_variable("angle")
126126
pset = ParticleSet(
127127
fieldset=fieldset, pclass=AngleParticle, lon=np.zeros(npart), lat=np.zeros(npart), depth=np.zeros(npart)
128128
)
129129

130130
def vonmises(particle, fieldset, time): # pragma: no cover
131-
particle.angle = ParcelsRandom.vonmisesvariate(fieldset.mu, fieldset.kappa)
131+
particle.angle = random.vonmisesvariate(fieldset.mu, fieldset.kappa)
132132

133133
pset.execute(vonmises, runtime=1, dt=1)
134134

0 commit comments

Comments
 (0)