Skip to content

Commit

Permalink
added tests, update notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
jgostick committed Feb 5, 2024
1 parent 9d7a9fe commit 51549de
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 58 deletions.
47 changes: 0 additions & 47 deletions examples/generators/reference/cylindrical_pillars_mesh.ipynb

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions porespy/generators/_micromodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ def _extract(im, shape, spacing, truncate, lattice):
a, b = (new_shape/2).astype(int)
s = (slice(a, a+1, None), slice(b, b+1, None))
if truncate:
a, b = (shape/2).astype(int)
a, b = np.around(shape/2).astype(int)
sx = extend_slice(slices=s, shape=im.shape, pad=[a, b])
im = im[sx]
im = im[:shape[0], :shape[1]]
else:
diag = np.around((spacing**2 + spacing**2)**0.5).astype(int)
a, b = (np.ceil(shape/diag)*diag/2).astype(int)
sx = extend_slice(slices=s, shape=im.shape, pad=[a, b])
im = im[sx]
sx = extend_slice(slices=s, shape=im.shape, pad=[a, b])
im = im[sx]
return im


Expand Down Expand Up @@ -106,6 +109,8 @@ def rectangular_pillars_array(
<https://porespy.org/examples/generators/reference/rectangular_pillars_array.html>`_
to view online example.
"""
if len(shape) != 2:
raise Exception('shape must be 2D for this function')
if seed is not None:
np.random.seed(seed)
if isinstance(dist, str):
Expand Down Expand Up @@ -133,7 +138,6 @@ def rectangular_pillars_array(
)
tmp[sx] = True
tmp = _extract(tmp, shape, spacing, truncate, lattice)
pts = _extract(pts, shape, spacing, truncate, lattice)
return tmp


Expand Down Expand Up @@ -202,6 +206,8 @@ def cylindrical_pillars_array(
<https://porespy.org/examples/generators/reference/cylindrical_pillars_array.html>`_
to view online example.
"""
if len(shape) != 2:
raise Exception('shape must be 2D for this function')
if seed is not None:
np.random.seed(seed)
if isinstance(dist, str):
Expand Down Expand Up @@ -235,7 +241,6 @@ def cylindrical_pillars_mesh(
a: int = 1000,
n: int = None,
truncate : bool = True,
seed: int = None,
):
r"""
A 2D micromodel with randomly located cylindrical pillars of random radius
Expand Down Expand Up @@ -263,9 +268,15 @@ def cylindrical_pillars_mesh(
A flag to indicate if the output should be truncated to the given `shape`
or if the returned image should be expanded to include the full boundary
pillars. The default is `True`.
seed : int
The value to initialize numpy's random number generator. The default is
`None` which results in a new realization each time this function is called.
Returns
-------
im : ndarray
A ndarray with pillars locations determined by generating a triangular mesh
of the specified domain size and putting pillars at each vertex. Note that
this process is deterministic for a given set of input arguments so the
apparent randomness of the pillar locations is actually determined by the
underlaying mesh package used (`nanomesh`).
Examples
--------
Expand All @@ -274,10 +285,8 @@ def cylindrical_pillars_mesh(
to view online example.
"""
if seed is not None:
np.random.seed(seed)
if len(shape) != 2:
raise Exception("Shape must be 2D")
raise Exception('shape must be 2D for this function')
if n is None:
n = a**0.5/f
im = np.ones(shape, dtype=float)
Expand Down
75 changes: 75 additions & 0 deletions test/unit/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,81 @@ def test_polydisperse_cylinders(self):
eps = fibers.sum()/fibers.size
assert eps == 0.759302

def test_rectangular_pillars_array(self):
im1 = ps.generators.rectangular_pillars_array(shape=[190, 190])
assert im1.shape == (190, 190)
im2 = ps.generators.rectangular_pillars_array(
shape=[190, 190],
truncate=False,)
assert im2.shape == (201, 201)
im3 = ps.generators.rectangular_pillars_array(shape=[190, 190], seed=0)
im4 = ps.generators.rectangular_pillars_array(shape=[190, 190], seed=0)
im5 = ps.generators.rectangular_pillars_array(shape=[190, 190], seed=None)
assert np.all(im3 == im4)
assert ~np.all(im3 == im5)
im6 = ps.generators.rectangular_pillars_array(
shape=[190, 190],
lattice='triangular',
)
assert ~np.all(im1 == im6)
im7 = ps.generators.rectangular_pillars_array(
shape=[190, 190],
dist='uniform',
dist_kwargs=dict(loc=1, scale=2))
im8 = ps.generators.rectangular_pillars_array(
shape=[190, 190],
dist='uniform',
dist_kwargs=dict(loc=5, scale=5))
assert np.sum(im7) < np.sum(im8)

def test_cylindrical_pillars_array(self):
im1 = ps.generators.cylindrical_pillars_array(shape=[190, 190])
assert im1.shape == (190, 190)
im2 = ps.generators.cylindrical_pillars_array(
shape=[190, 190],
truncate=False,)
assert im2.shape == (201, 201)
im3 = ps.generators.cylindrical_pillars_array(shape=[190, 190], seed=0)
im4 = ps.generators.cylindrical_pillars_array(shape=[190, 190], seed=0)
im5 = ps.generators.cylindrical_pillars_array(shape=[190, 190], seed=None)
assert np.all(im3 == im4)
assert ~np.all(im3 == im5)
im6 = ps.generators.cylindrical_pillars_array(
shape=[190, 190],
lattice='triangular',
)
assert ~np.all(im1 == im6)
im7 = ps.generators.cylindrical_pillars_array(
shape=[190, 190],
dist='uniform',
dist_kwargs=dict(loc=1, scale=2))
im8 = ps.generators.cylindrical_pillars_array(
shape=[190, 190],
dist='uniform',
dist_kwargs=dict(loc=5, scale=5))
assert np.sum(im8) < np.sum(im7)

def test_cylindrical_pillars_mesh(self):
im1 = ps.generators.cylindrical_pillars_mesh(
shape=[190, 190],
truncate=True,
)
assert im1.shape == (190, 190)
im2 = ps.generators.cylindrical_pillars_mesh(
shape=[190, 190],
truncate=False,
)
assert im2.shape == (224, 224)
im3 = ps.generators.cylindrical_pillars_mesh(
shape=[190, 190],
f=.5,
)
im4 = ps.generators.cylindrical_pillars_mesh(
shape=[190, 190],
f=.85,
)
assert im3.sum() > im4.sum()


if __name__ == '__main__':
t = GeneratorTest()
Expand Down

0 comments on commit 51549de

Please sign in to comment.