File size: 4,544 Bytes
f421c2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import pytest
import numpy as np
import openpnm as op
import matplotlib.pyplot as plt
from numpy.testing import assert_allclose
class PlotToolsTest:
def setup_class(self):
self.ws = op.Workspace()
def test_plot_tutorial(self):
pn = op.network.Cubic(shape=[4, 4, 1])
# This runs locally, but fails on the CI due to a missing argument 's'
# coming from within the networkx function, not ours.
# op.visualization.plot_tutorial(pn)
# plt.close()
def test_plot_networkx_var_spacing(self):
for i in range(3):
shape = np.ones(3, dtype=int)
shape[np.arange(3) != i] = [5, 8]
spacing = np.ones(3, dtype=float)
spacing[np.arange(3) != i] = [0.01, 0.6]
pn = op.network.Cubic(shape=shape)
dims = op.topotools.dimensionality(pn)
x, y = pn["pore.coords"].T[dims]
fig, ax = plt.subplots()
m = op.visualization.plot_networkx(pn, ax=ax)
x_plot, y_plot = np.array(m.get_offsets()).T
np.testing.assert_allclose(x_plot, x)
np.testing.assert_allclose(y_plot, y)
plt.close()
def test_plot_networkx(self):
# 2D networks in XY, YZ, XZ planes
for i in range(3):
shape = np.ones(3, dtype=int)
shape[np.arange(3) != i] = [5, 8]
pn = op.network.Cubic(shape=shape)
x, y = pn["pore.coords"].T[op.topotools.dimensionality(pn)]
fig, ax = plt.subplots()
m = op.visualization.plot_networkx(pn, ax=ax)
x_plot, y_plot = np.array(m.get_offsets()).T
np.testing.assert_allclose(x_plot, x)
np.testing.assert_allclose(y_plot, y)
plt.close()
# 1D networks in XY, YZ, XZ planes
for i in range(3):
shape = np.ones(3, dtype=int)
shape[np.arange(3) == i] = [5]
pn = op.network.Cubic(shape=shape)
x, = pn["pore.coords"].T[op.topotools.dimensionality(pn)]
fig, ax = plt.subplots()
m = op.visualization.plot_networkx(pn, ax=ax)
x_plot, y_plot = np.array(m.get_offsets()).T
np.testing.assert_allclose(x_plot, x)
plt.close()
def test_plot_networkx_3d(self):
pn = op.network.Cubic(shape=[5, 8, 3])
with pytest.raises(Exception):
op.visualization.plot_networkx(pn)
def test_plot_connections_color_by(self):
pn = op.network.Cubic(shape=[5, 5, 1])
np.random.seed(10)
Ts = np.array([0, 4, 6, 18])
_ = op.visualization.plot_connections(
pn,
throats=Ts,
color_by=np.random.rand(pn.Nt),
)
def test_plot_coordinates_color_by(self):
pn = op.network.Cubic(shape=[5, 5, 1])
np.random.seed(10)
_ = op.visualization.plot_coordinates(
pn,
color_by=np.random.rand(pn.Np),
)
def test_plot_connections_and_coordinates_color_by_3D(self):
pn = op.network.Cubic(shape=[5, 5, 5])
np.random.seed(10)
ax = op.visualization.plot_connections(
pn,
color_by=np.random.rand(pn.Nt),
)
ax = op.visualization.plot_coordinates(
pn,
color_by=np.random.rand(pn.Np),
ax=ax,
)
def test_plot_connections_and_coordinates_color(self):
pn = op.network.Cubic(shape=[5, 5, 1])
ax = op.visualization.plot_connections(
network=pn,
color='b',
)
ax = op.visualization.plot_coordinates(
network=pn,
color='b',
ax=ax,
)
def test_3d(self):
pn = op.network.Cubic(shape=[10, 10, 3])
pn['pore.internal'] = True
pn.add_boundary_pores()
Ps = pn.pores('internal') # find internal pores
fig, ax = plt.subplots() # create empty figure
ax = op.visualization.plot_coordinates(
network=pn,
pores=Ps,
color='g',
ax=ax,
)
Ps = pn.pores('*boundary') # find boundary pores
ax = op.visualization.plot_coordinates(
network=pn,
pores=Ps,
color='r',
ax=ax,
)
if __name__ == '__main__':
t = PlotToolsTest()
self = t
t.setup_class()
for item in t.__dir__():
if item.startswith('test'):
print(f'Running test: {item}')
t.__getattribute__(item)()
|