Added unit test for cylinders_porosity
@@ -13,8 +13,7 @@ class GeneratorTest():
np.random.seed(10)
def test_cylinders(self):
- X = 100
- Y = 100
+ X = Y = 100
# Fibers don't work in 2D
with pytest.raises(Exception):
im = ps.generators.cylinders(shape=[X, Y], radius=4, ncylinders=20)
@@ -22,10 +21,23 @@ class GeneratorTest():
im = ps.generators.cylinders(shape=[1, X, Y], radius=1, ncylinders=20)
assert im.dtype == bool
assert np.shape(im.squeeze()) == (X, Y)
- im = ps.generators.cylinders(shape=[50, 50, 50], radius=1,
- ncylinders=20)
+ im = ps.generators.cylinders(shape=[50, 50, 50], radius=1, ncylinders=20)
assert np.shape(im.squeeze()) == (50, 50, 50)
+ def test_cylinders_porosity(self):
+ # max_iter must at least be 3
+ with pytest.raises(Exception):
+ im = ps.generators.cylinders_porosity(
+ shape=[50, 50, 50], radius=4, porosity=0.5, max_iter=2
+ )
+ im = ps.generators.cylinders_porosity(
+ shape=[50, 50, 50], radius=3, porosity=0.5, max_iter=10
+ )
+ assert im.dtype == bool
+ assert np.shape(im.squeeze()) == (50, 50, 50)
+ porosity = im.sum() / im.size
+ np.testing.assert_allclose(porosity, 0.5, rtol=1e-2)
+
def test_insert_shape_center_defaults(self):
im = np.zeros([11, 11])
shape = np.ones([3, 3])
@@ -281,5 +293,5 @@ if __name__ == '__main__':
t.setup_class()
for item in t.__dir__():
if item.startswith('test'):
- print('running test: '+item)
+ print(f"Running test: {item}")
t.__getattribute__(item)()
