File size: 8,789 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | import numpy as np
import pytest
import openpnm as op
class NetworkTest:
def setup_class(self):
self.net = op.network.Cubic(shape=[10, 10, 10])
def teardown_class(self):
ws = op.Workspace()
ws.clear()
def test_find_connected_pores_numeric_not_flattend(self):
a = self.net.find_connected_pores(throats=[0, 1])
assert np.all(a.flatten() == [0, 1, 1, 2])
def test_find_connected_pores_numeric_flattend(self):
a = self.net.find_connected_pores(throats=[0, 1], flatten=True)
assert np.all(a == [0, 1, 2])
def test_find_connected_pores_boolean_flattend(self):
Tind = np.zeros((self.net.Nt,), dtype=bool)
Tind[[0, 1]] = True
a = self.net.find_connected_pores(throats=Tind, flatten=True)
assert np.all(a == [0, 1, 2])
def test_find_connected_pores_empty_flattend(self):
a = self.net.find_connected_pores(throats=[], flatten=True)
assert np.shape(a) == (0, )
def test_find_neighbor_pores_numeric(self):
a = self.net.find_neighbor_pores(pores=[])
assert np.size(a) == 0
def test_find_neighbor_pores_boolean(self):
Pind = np.zeros((self.net.Np,), dtype=bool)
Pind[[0, 1]] = True
a = self.net.find_neighbor_pores(pores=Pind)
assert np.all(a == [2, 10, 11, 100, 101])
def test_find_neighbor_pores_numeric_union(self):
a = self.net.find_neighbor_pores(pores=[0, 2], mode='union')
assert np.all(a == [1, 3, 10, 12, 100, 102])
def test_find_neighbor_pores_numeric_intersection(self):
a = self.net.find_neighbor_pores(pores=[0, 2], mode='xnor')
assert np.all(a == [1])
def test_find_neighbor_pores_numeric_exclusive_or(self):
a = self.net.find_neighbor_pores(pores=[0, 2], mode='exclusive_or')
assert np.all(a == [3, 10, 12, 100, 102])
def test_find_neighbor_pores_numeric_union_include_input(self):
a = self.net.find_neighbor_pores(pores=[0, 2], mode='or',
include_input=True)
assert np.all(a == [1, 3, 10, 12, 100, 102])
a = self.net.find_neighbor_pores(pores=[0, 1], mode='or',
include_input=True)
assert np.all(a == [0, 1, 2, 10, 11, 100, 101])
def test_find_neighbor_pores_asmask(self):
a = self.net.find_neighbor_pores(pores=[0, 2], flatten=True, asmask=True)
assert sum(a) == 6
assert a.dtype == 'bool'
def tes_find_neighbor_pores_asmask_unflattened(self):
with pytest.raises():
a = self.net.find_neighbor_pores(pores=[0, 2], flatten=False, asmask=True)
def test_find_neighbor_pores_numeric_intersection_include_input(self):
a = self.net.find_neighbor_pores(pores=[0, 2], mode='and',
include_input=True)
assert np.all(a == [1])
a = self.net.find_neighbor_pores(pores=[0, 1], mode='and',
include_input=True)
assert np.all(a == [])
def test_find_neighbor_pores_numeric_intersection_exclude_input(self):
a = self.net.find_neighbor_pores(pores=[0, 2], mode='and',
include_input=False)
assert np.all(a == [1])
def test_find_neighbor_pores_numeric_exclusive_or_include_input(self):
a = self.net.find_neighbor_pores(pores=[0, 2], mode='exclusive_or',
include_input=True)
assert np.all(a == [3, 10, 12, 100, 102])
a = self.net.find_neighbor_pores(pores=[0, 1], mode='exclusive_or',
include_input=True)
assert np.all(a == [0, 1, 2, 10, 11, 100, 101])
def test_find_neighbor_throats_on_pores_wo_throats(self):
net = op.network.Cubic(shape=[10, 10, 1])
ts = net.find_neighbor_throats(pores=net.Ps[-1])
op.topotools.trim(net, throats=ts)
ts2 = net.find_neighbor_throats(pores=99)
assert ts2.size == 0
def test_find_neighbor_throats_empty(self):
a = self.net.find_neighbor_throats(pores=[])
assert np.size(a) == 0
def test_find_neighbor_throats_boolean(self):
Pind = np.zeros((self.net.Np,), dtype=bool)
Pind[[0, 1]] = True
a = self.net.find_neighbor_throats(pores=Pind)
assert np.all(a == [0, 1, 900, 901, 1800, 1801])
def test_find_neighbor_throats_numeric_union(self):
a = self.net.find_neighbor_throats(pores=[0, 2], mode='union')
assert np.all(a == [0, 1, 2, 900, 902, 1800, 1802])
def test_find_neighbor_throats_numeric_intersection(self):
a = self.net.find_neighbor_throats(pores=[0, 2], mode='xnor')
assert np.size(a) == 0
def test_find_neighbor_throats_numeric_exclusive_or(self):
a = self.net.find_neighbor_throats(pores=[0, 2],
mode='exclusive_or')
assert np.all(a == [0, 1, 2, 900, 902, 1800, 1802])
def test_num_neighbors_empty(self):
a = self.net.num_neighbors(pores=[])
assert np.size(a) == 0
def test_num_neighbors_pores_flattened(self):
a = self.net.num_neighbors(pores=0, flatten=True)
assert a == 3
assert isinstance(a, int)
a = self.net.num_neighbors(pores=[0, 2], flatten=True)
assert a == 6
assert isinstance(a, int)
def test_num_neighbors_pores_with_modes(self):
a = self.net.num_neighbors(pores=[0, 2], mode='union', flatten=True)
assert a == 6
a = self.net.num_neighbors(pores=[0, 2], mode='xnor',
flatten=True)
assert a == 1
a = self.net.num_neighbors(pores=[0, 2], mode='exclusive_or',
flatten=True)
assert a == 5
def test_num_neighbors_pores_not_flattened(self):
a = self.net.num_neighbors(pores=[0, 2], flatten=False)
assert np.all(a == [3, 4])
a = self.net.num_neighbors(pores=0, flatten=False)
assert np.all(a == [3])
assert isinstance(a, np.ndarray)
def test_find_nearby_pores_distance_1(self):
a = self.net.find_nearby_pores(pores=[0, 1], r=1, flatten=False,
include_input=True)
b = self.net.find_neighbor_pores(pores=[0, 1], flatten=False,
include_input=True)
assert np.all([np.all(a[i] == b[i]) for i in range(0, len(a))])
def test_find_nearby_pores_distance_2(self):
a = self.net.find_nearby_pores(pores=[0, 1], r=2)
assert np.all([np.size(a[i]) for i in [0, 1]] == [9, 13])
def test_find_nearby_pores_distance_0(self):
a = self.net.find_nearby_pores(pores=[0, 1], r=1e-9, flatten=False)
assert np.shape(a) == (2, 0)
a = self.net.find_nearby_pores(pores=[0, 1], r=1e-9, flatten=True)
assert a.shape == (0,)
def test_find_nearby_pores_distance_1_flattened(self):
a = self.net.find_nearby_pores(pores=[0, 1], r=1, flatten=True)
b = self.net.find_neighbor_pores(pores=[0, 1])
assert np.all(a == b)
def test_find_nearby_pores_distance_2_flattened(self):
a = self.net.find_nearby_pores(pores=[0, 1], r=2, flatten=True)
assert np.size(a) == 15
def test_find_nearby_pores_distance_2_flattened_include_input(self):
a = self.net.find_nearby_pores(pores=[0, 1], r=2,
flatten=True, include_input=True)
assert np.size(a) == 17
assert np.all(np.isin([0, 1], a))
def test_get_incidence_matrix(self):
net = op.network.Demo([4, 4, 1])
assert net._im == {}
im = net.get_incidence_matrix(fmt='coo')
assert im.shape == (16, 24)
assert im.data.shape == (48,)
assert len(net._im) == 1
im = net.get_incidence_matrix(fmt='dok')
assert len(im.keys()) == 48
assert len(net._im) == 2
def test_get_adjacency_matrix(self):
net = op.network.Demo([4, 4, 1])
assert net._am == {}
im = net.get_adjacency_matrix(fmt='coo')
assert im.shape == (16, 16)
assert im.data.shape == (48,)
assert len(net._am) == 1
am = net.get_adjacency_matrix(fmt='dok')
assert len(am.keys()) == 48
assert len(net._am) == 2
def test_into(self):
net = op.network.Demo([4, 4, 1])
# This test is lame, but just to keep the code cov counter happy
net.info
if __name__ == '__main__':
t = NetworkTest()
t.setup_class()
self = t
for item in t.__dir__():
if item.startswith('test'):
print(f"Running test: {item}")
t.__getattribute__(item)()
|