File size: 7,016 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 | import os
import json
import pytest
import numpy as np
import openpnm as op
from pathlib import Path
class JSONGraphTest:
def setup_class(self):
ws = op.Workspace()
ws.settings['local_data'] = True
self.net = op.network.Cubic(shape=[2, 2, 2])
self.net['pore.diameter'] = 2.0 * np.ones(self.net.Np)
self.net['throat.diameter'] = 2.0 * np.ones(self.net.Nt)
self.net.add_model(propname='throat.length',
model=op.models.network.pore_to_pore_distance)
def teardown_class(self):
ws = op.Workspace()
ws.clear()
# def test_save_failure(self, tmpdir):
# path = Path(os.path.realpath(tmpdir),
# '../../../fixtures/JSONGraphFormat')
# filename = Path(path.resolve(), 'save_failure.json')
# # Create a deep copy of network with one required property missing
# net = copy.deepcopy(self.net)
# net.pop('pore.diameter')
# # Ensure an exception was thrown
# with pytest.raises(Exception) as e_info:
# op.io.network_to_jsongraph(net.project, filename=filename)
# expected_error = 'Error - network is missing one of:'
# assert expected_error in str(e_info.value)
def test_save_success(self):
path = Path(os.path.realpath(__file__),
'../../../fixtures/JSONGraphFormat')
filename = Path(path.resolve(), 'save_success.json')
op.io.network_to_jsongraph(self.net, filename=filename)
# Read newly created file
with open(filename, 'r') as file:
json_file = json.load(file)
# Ensure correctnes of overal network properties
assert json_file['graph']['metadata']['number_of_nodes'] == self.net.Np
assert json_file['graph']['metadata']['number_of_links'] == self.net.Nt
# Ensure correctnes of node list properties
nodes = sorted(json_file['graph']['nodes'], key=lambda node: int(node['id']))
assert len(nodes) == self.net.Np
assert isinstance(nodes, list)
# Sweep all nodes in the list
for node in nodes:
assert isinstance(node, dict)
# Ensure correctnes of node property types
assert 'id' in node
assert 'metadata' in node
assert isinstance(node['id'], str)
assert isinstance(node['metadata'], dict)
# Ensure correctnes of node property values
assert int(node['id']) < self.net.Np
# Ensure correctnes of node metadata types
assert 'node_coordinates' in node['metadata']
assert 'node_squared_radius' in node['metadata']
assert 'x' in node['metadata']['node_coordinates']
assert 'y' in node['metadata']['node_coordinates']
assert 'z' in node['metadata']['node_coordinates']
assert isinstance(node['metadata']['node_coordinates'], dict)
assert isinstance(node['metadata']['node_squared_radius'], int)
assert isinstance(node['metadata']['node_coordinates']['x'], int)
assert isinstance(node['metadata']['node_coordinates']['y'], int)
assert isinstance(node['metadata']['node_coordinates']['z'], int)
# Ensure correctness of node metadata values
assert node['metadata']['node_squared_radius'] == 1
assert node['metadata']['node_coordinates']['x'] * 2 % 2 == 0
assert node['metadata']['node_coordinates']['y'] * 2 % 2 == 0
assert node['metadata']['node_coordinates']['z'] * 2 % 2 == 0
# Ensure correctnes of edge list properties
edges = sorted(json_file['graph']['edges'], key=lambda edge: int(edge['id']))
assert len(edges) == self.net.Nt
assert isinstance(edges, list)
# Sweep all edges in the list
for edge in edges:
assert isinstance(edge, dict)
# Ensure correctnes of edge property types
assert 'id' in edge
assert 'source' in edge
assert 'target' in edge
assert 'metadata' in edge
assert isinstance(edge['id'], str)
assert isinstance(edge['source'], str)
assert isinstance(edge['target'], str)
assert isinstance(edge['metadata'], dict)
# Ensure correctnes of edge property values
assert int(edge['id']) < self.net.Nt
assert int(edge['source']) < self.net.Np
assert int(edge['target']) < self.net.Np
# Ensure correctnes of edge metadata types
assert 'link_length' in edge['metadata']
assert 'link_squared_radius' in edge['metadata']
assert isinstance(edge['metadata']['link_length'], float)
assert isinstance(edge['metadata']['link_squared_radius'], float)
# Ensure correctnes of edge metadata values
assert edge['metadata']['link_length'] == 1.0
assert edge['metadata']['link_squared_radius'] == 1.0
# Remove test file after completion
os.remove(filename)
def test_load_failure(self):
path = Path(os.path.realpath(__file__),
'../../../fixtures/JSONGraphFormat')
filename = Path(path.resolve(), 'invalid.json')
# Ensure an exception was thrown
with pytest.raises(Exception):
op.io.from_jsongraph(filename)
def test_load_success(self):
# Load JSON file and ensure project integrity
path = Path(os.path.realpath(__file__),
'../../../fixtures/JSONGraphFormat')
filename = Path(path.resolve(), 'valid.json')
net = op.io.network_from_jsongraph(filename)
assert hasattr(net, 'conns')
# Ensure overal network properties
assert net.Np == 2
assert net.Nt == 1
# Ensure correctness of pore properties
assert np.array_equal(net['pore.index'], np.array([0, 1]))
assert np.array_equal(net['pore.diameter'], np.array([0, 0]))
assert np.array_equal(net['pore.coords'][0], np.array([0, 0, 0]))
assert np.array_equal(net['pore.coords'][1], np.array([1, 1, 1]))
# Ensure correctness of throat properties
length = 1.73205080757
squared_radius = 5.169298742047715
assert net['throat.length'] == length
assert np.array_equal(net['throat.conns'], np.array([[0, 1]]))
assert net['throat.diameter'] == 2.0 * np.sqrt(squared_radius)
if __name__ == '__main__':
import py
# All the tests in this file can be run with 'playing' this file
t = JSONGraphTest()
self = t # For interacting with the tests at the command line
tmpdir = py.path.local()
t.setup_class()
for item in t.__dir__():
if item.startswith('test'):
print(f'Running test: {item}')
try:
t.__getattribute__(item)()
except TypeError:
t.__getattribute__(item)()
|