Skip to content

Commit cb57e41

Browse files
committed
Updated docs to new interfaces and created doc tests #104
1 parent 19b7de6 commit cb57e41

File tree

8 files changed

+262
-20
lines changed

8 files changed

+262
-20
lines changed

docs/blob_factory.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ We chose the defualt distributions for all other blob parameters. We would set u
5757
5858
from blobmodel import Model, DefaultBlobFactory
5959
60-
my_blob_factory = DefaultBlobFactory(A_dist="normal", A_parameter=5)
60+
my_blob_factory = DefaultBlobFactory(A_dist=DistributionEnum.normal, A_parameter=5)
6161
6262
bm = Model(
6363
Nx=200,
@@ -66,7 +66,7 @@ We chose the defualt distributions for all other blob parameters. We would set u
6666
Ly=10,
6767
dt=0.1,
6868
T=20,
69-
blob_shape="gauss",
69+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
7070
blob_factory=my_blob_factory,
7171
t_drain=100,
7272
num_blobs=100,
@@ -148,7 +148,7 @@ In this case you can use the ``CustomBlobFactory`` class to define all blob para
148148
Ly=2,
149149
dt=0.1,
150150
T=10,
151-
blob_shape="gauss",
151+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
152152
t_drain=2,
153153
periodic_y=True,
154154
num_blobs=1000,

docs/blob_labels.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Let's take a look at an example: Let's say we want to calculate the individual b
2626
dt=0.1,
2727
T=20,
2828
periodic_y=True,
29-
blob_shape="gauss",
29+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
3030
num_blobs=10,
3131
t_drain=1e10,
3232
labels="individual",

docs/blob_shapes.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ An example would look like this:
6161
dt=0.1,
6262
T=10,
6363
num_blobs=10,
64-
blob_shape=BlobShapeImpl("exp", "lorentz"),
64+
blob_shape=BlobShapeImpl(BlobShapeEnum.exp, BlobShapeEnum.lorentz),
6565
periodic_y=True,
6666
t_drain=1e10,
6767
)
@@ -87,13 +87,13 @@ We specify the asymmetry parameter when defining the ``blob_factory``. An exampl
8787
.. code-block:: python
8888
8989
bf = DefaultBlobFactory(
90-
A_dist="deg",
91-
wx_dist="deg",
92-
spx_dist="deg",
93-
spy_dist="deg",
90+
A_dist=DistributionEnum.deg,
91+
wx_dist=DistributionEnum.deg,
92+
spx_dist=DistributionEnum.deg,
93+
spy_dist=DistributionEnum.deg,
9494
shape_param_x_parameter=0.5,
9595
shape_param_y_parameter=0.5,
96-
)
96+
)
9797
9898
bm = Model(
9999
Nx=100,
@@ -103,7 +103,7 @@ We specify the asymmetry parameter when defining the ``blob_factory``. An exampl
103103
dt=0.1,
104104
T=10,
105105
num_blobs=10,
106-
blob_shape=BlobShapeImpl("2-exp", "2-exp"),
106+
blob_shape=BlobShapeImpl(BlobShapeEnum.double_exp, BlobShapeEnum.double_exp),
107107
t_drain=1e10,
108108
blob_factory=bf,
109109
)

docs/blob_tilt.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ Alternatively, we can force force any tilt angle by setting blob_alignment = Fal
2020

2121
.. code-block:: python
2222
23+
vx, vy = 1, 0
24+
wx, wy = 1, 1
2325
bf = DefaultBlobFactory(
24-
A_dist="deg",
26+
A_dist=DistributionEnum.deg,
2527
vy_parameter=vy,
2628
vx_parameter=vx,
2729
wx_parameter=wx,

docs/drainage_time.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Let's take a look at a quick example. Let's assume we want ``t_drain`` to decrea
1313
from blobmodel import Model, DefaultBlobFactory
1414
import numpy as np
1515
16-
bf = DefaultBlobFactory(A_dist="deg", wx_dist="deg", vx_dist="deg", vy_dist="zeros")
17-
1816
t_drain = np.linspace(2, 1, 100)
1917
2018
tmp = Model(
@@ -24,11 +22,10 @@ Let's take a look at a quick example. Let's assume we want ``t_drain`` to decrea
2422
Ly=0,
2523
dt=1,
2624
T=1000,
27-
blob_shape="exp",
25+
blob_shape=BlobShapeImpl(BlobShapeEnum.exp),
2826
t_drain=t_drain,
2927
periodic_y=False,
30-
num_blobs=10000,
31-
blob_factory=bf,
28+
num_blobs=100,
3229
)
3330
3431
ds_changing_t_drain = tmp.make_realization()

docs/getting_started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ In addition, we can specify the blob shape, drainage time and the number of blob
2323
dt=0.1,
2424
T=20,
2525
periodic_y=True,
26-
blob_shape="gauss",
26+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
2727
num_blobs=100,
2828
t_drain=1e10,
2929
t_init=10,

docs/one_dim.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ This is achieved by setting the flag ``one_dimensional`` to True. Additionally,
1010
1111
from blobmodel import Model, show_model, DefaultBlobFactory
1212
13-
bf = DefaultBlobFactory(A_dist="exp", wx_dist="deg", vx_dist="deg", vy_dist="zeros")
13+
bf = DefaultBlobFactory(
14+
A_dist=DistributionEnum.exp,
15+
wx_dist=DistributionEnum.deg,
16+
vx_dist=DistributionEnum.deg,
17+
vy_dist=DistributionEnum.zeros,
18+
)
1419
1520
bm = Model(
1621
Nx=100,
@@ -20,7 +25,7 @@ This is achieved by setting the flag ``one_dimensional`` to True. Additionally,
2025
dt=0.1,
2126
T=10,
2227
periodic_y=False,
23-
blob_shape="exp",
28+
blob_shape=BlobShapeImpl(BlobShapeEnum.exp),
2429
num_blobs=20,
2530
t_drain=10,
2631
blob_factory=bf,

tests/test_docs.py

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
from blobmodel import (
2+
Model,
3+
DefaultBlobFactory,
4+
Blob,
5+
BlobShapeImpl,
6+
DistributionEnum,
7+
BlobShapeEnum,
8+
)
9+
import numpy as np
10+
from unittest.mock import MagicMock
11+
import matplotlib.pyplot as plt
12+
13+
# The following tests check that the docs examples run, if they fail make sure to change the docs too!
14+
15+
16+
def test_getting_started():
17+
bm = Model(
18+
Nx=10,
19+
Ny=10,
20+
Lx=10,
21+
Ly=10,
22+
dt=0.1,
23+
T=20,
24+
periodic_y=True,
25+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
26+
num_blobs=100,
27+
t_drain=1e10,
28+
t_init=10,
29+
)
30+
ds = bm.make_realization(file_name="example.nc")
31+
ds["n"].isel(y=0).mean(dim=("t")).plot()
32+
33+
34+
def test_drainage_time():
35+
t_drain = np.linspace(2, 1, 100)
36+
37+
tmp = Model(
38+
Nx=100,
39+
Ny=1,
40+
Lx=10,
41+
Ly=0,
42+
dt=1,
43+
T=1000,
44+
blob_shape=BlobShapeImpl(BlobShapeEnum.exp),
45+
t_drain=t_drain,
46+
periodic_y=False,
47+
num_blobs=10,
48+
)
49+
tmp.make_realization()
50+
51+
52+
def test_one_dim():
53+
bf = DefaultBlobFactory(
54+
A_dist=DistributionEnum.exp,
55+
wx_dist=DistributionEnum.deg,
56+
vx_dist=DistributionEnum.deg,
57+
vy_dist=DistributionEnum.zeros,
58+
)
59+
60+
bm = Model(
61+
Nx=100,
62+
Ny=1,
63+
Lx=10,
64+
Ly=0,
65+
dt=0.1,
66+
T=10,
67+
periodic_y=False,
68+
blob_shape=BlobShapeImpl(BlobShapeEnum.exp),
69+
num_blobs=20,
70+
t_drain=10,
71+
blob_factory=bf,
72+
one_dimensional=True,
73+
)
74+
bm.make_realization(speed_up=True, error=1e-2)
75+
76+
77+
def test_blob_shapes():
78+
from blobmodel import Model, BlobShapeImpl
79+
80+
bm = Model(
81+
Nx=100,
82+
Ny=100,
83+
Lx=10,
84+
Ly=10,
85+
dt=0.1,
86+
T=10,
87+
num_blobs=10,
88+
blob_shape=BlobShapeImpl(BlobShapeEnum.exp, BlobShapeEnum.lorentz),
89+
periodic_y=True,
90+
t_drain=1e10,
91+
)
92+
93+
bf = DefaultBlobFactory(
94+
A_dist=DistributionEnum.deg,
95+
wx_dist=DistributionEnum.deg,
96+
spx_dist=DistributionEnum.deg,
97+
spy_dist=DistributionEnum.deg,
98+
shape_param_x_parameter=0.5,
99+
shape_param_y_parameter=0.5,
100+
)
101+
bm = Model(
102+
Nx=100,
103+
Ny=100,
104+
Lx=10,
105+
Ly=10,
106+
dt=0.1,
107+
T=10,
108+
num_blobs=10,
109+
blob_shape=BlobShapeImpl(BlobShapeEnum.double_exp, BlobShapeEnum.double_exp),
110+
t_drain=1e10,
111+
blob_factory=bf,
112+
)
113+
114+
115+
def test_blob_tilt():
116+
vx, vy = 1, 0
117+
wx, wy = 1, 1
118+
119+
bf = DefaultBlobFactory(
120+
A_dist=DistributionEnum.deg,
121+
vy_parameter=vy,
122+
vx_parameter=vx,
123+
wx_parameter=wx,
124+
wy_parameter=wy,
125+
blob_alignment=False,
126+
)
127+
128+
theta = np.pi / 2
129+
bf.set_theta_setter(lambda: theta)
130+
131+
132+
def test_labels():
133+
bm = Model(
134+
Nx=10,
135+
Ny=10,
136+
Lx=20,
137+
Ly=20,
138+
dt=0.1,
139+
T=20,
140+
periodic_y=True,
141+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
142+
num_blobs=10,
143+
t_drain=1e10,
144+
labels="individual",
145+
label_border=0.75,
146+
)
147+
148+
ds = bm.make_realization(speed_up=True, error=1e-2)
149+
150+
ds["n"].isel(t=-1).plot()
151+
plt.figure()
152+
ds["blob_labels"].isel(t=-1).plot()
153+
154+
155+
def test_blob_factory():
156+
my_blob_factory = DefaultBlobFactory(A_dist=DistributionEnum.normal, A_parameter=5)
157+
158+
bm = Model(
159+
Nx=10,
160+
Ny=10,
161+
Lx=10,
162+
Ly=10,
163+
dt=0.1,
164+
T=20,
165+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
166+
blob_factory=my_blob_factory,
167+
t_drain=100,
168+
num_blobs=100,
169+
)
170+
171+
ds = bm.make_realization()
172+
173+
174+
def test_custom_blob_factory():
175+
from blobmodel import BlobFactory, AbstractBlobShape
176+
177+
class CustomBlobFactory(BlobFactory):
178+
def __init__(self) -> None:
179+
pass
180+
181+
def sample_blobs(
182+
self,
183+
Ly: float,
184+
T: float,
185+
num_blobs: int,
186+
blob_shape: AbstractBlobShape,
187+
t_drain: float,
188+
) -> list[Blob]:
189+
190+
# set custom parameter distributions
191+
amp = np.linspace(0.01, 1, num=num_blobs)
192+
width = np.linspace(0.01, 1, num=num_blobs)
193+
vx = np.linspace(0.01, 1, num=num_blobs)
194+
vy = np.linspace(0.01, 1, num=num_blobs)
195+
196+
posx = np.zeros(num_blobs)
197+
posy = np.random.uniform(low=0.0, high=Ly, size=num_blobs)
198+
t_init = np.random.uniform(low=0, high=T, size=num_blobs)
199+
200+
# sort blobs by _t_init
201+
t_init = np.sort(t_init)
202+
203+
return [
204+
Blob(
205+
blob_id=i,
206+
blob_shape=blob_shape,
207+
amplitude=amp[i],
208+
width_prop=width[i],
209+
width_perp=width[i],
210+
v_x=vx[i],
211+
v_y=vy[i],
212+
pos_x=posx[i],
213+
pos_y=posy[i],
214+
t_init=t_init[i],
215+
t_drain=t_drain,
216+
)
217+
for i in range(num_blobs)
218+
]
219+
220+
def is_one_dimensional(self) -> bool:
221+
return False
222+
223+
bf = CustomBlobFactory()
224+
tmp = Model(
225+
Nx=10,
226+
Ny=10,
227+
Lx=2,
228+
Ly=2,
229+
dt=0.1,
230+
T=10,
231+
blob_shape=BlobShapeImpl(BlobShapeEnum.gaussian),
232+
t_drain=2,
233+
periodic_y=True,
234+
num_blobs=10,
235+
blob_factory=bf,
236+
)
237+
238+
ds = tmp.make_realization()

0 commit comments

Comments
 (0)