-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_handling_tests.py
executable file
·208 lines (168 loc) · 6.77 KB
/
data_handling_tests.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test Run examples:
ex.
$ python2 -m unittest -v data_handling_tests.TestDataLoad
$ python2 -m unittest -v data_handling_tests.TestNewData
"""
import data_handling
import unittest
import numpy
def test_data_type(self, data, d_key, data_type=str):
"""
Test that the loaded data is the expected object type.
Parameters
----------
data : class
Contains the loaded data.
d_key : d_key contains the string to test.
data_type : object type to test for
"""
if data_type is list:
self.assertIs(type(self.p.m[d_key]), data_type)
if data_type is float:
self.assertIs(type(self.p.m[d_key][1]), data_type)
def test_container_string(data_keys, expected_string):
"""
Tests that the correct string for the components are specified.
Parameters
----------
data : class
Contains the loaded data.
"""
return numpy.array_equal(data_keys, expected_string)
class TestDataLoad(unittest.TestCase):
"""
Known data loading as expected
"""
import numpy
data = data_handling.ImportData()
data.comps = ['carbon_dioxide', 'ethane']
data.eos = 'DWPM'
data.model = 'Adachi-Lu'
data.phases = ['x','y']
data.r = None
data.s = None
data.k_params = None
data.load_pure_data()
data.load()
data.c[0]['a_c (Pa m6 mol-2)'][0] = '' # Test param caculation
data.c[1]['b_c (m3 mol-1)'][0] = ''
crit_ans = numpy.array([0.34985866655, # data.c[0]['a_c (Pa m6 mol-2)'][0]
6.36225762119e-05 # data.c[1]['b_c (m3 mol-1)'][0]
])
p = data_handling.MixParameters()
p.mixture_parameters(data.VLE, data)
p.m['n'] = len(data.comps) # Define system size
for i in range(p.m['n']): # Set params for all compounds
p.parameters(data.c[i]) # Defines p.c[i]
points_ans = numpy.array([216.59, # p.c[1]['T'][0]
304.59, # p.c[1]['T'][88]
517278.8, # p.c[1]['P'][0]
7381661.9, # p.c[1]['P'][88]
7381661.9, # p.c[1]['P_c']
90.356, # p.c[2]['T'][0]
279.56, # p.c[2]['T'][88]
1.1198, # p.c[2]['P'][0]
2781354.5, # p.c[2]['P'][88]
305.3577, # p.c[2]['T_c']
])
points_ans_vle = numpy.array([1.0, # p.m['x'][1][0]
0.926, # p.m['x'][1][1]
1.0, # p.m['y'][1][0]
0.822 # p.m['y'][1][1]
])
def test_d1(self):
"""Pure data load"""
points_return = numpy.array([self.p.c[1]['T'][0],
self.p.c[1]['T'][88],
self.p.c[1]['P'][0],
self.p.c[1]['P'][88],
self.p.c[1]['P_c'],
self.p.c[2]['T'][0],
self.p.c[2]['T'][88],
self.p.c[2]['P'][0],
self.p.c[2]['P'][88],
self.p.c[2]['T_c']
])
print self.points_ans - points_return
numpy.testing.assert_array_equal(self.points_ans, points_return)
def test_d2(self):
"""VLE data load"""
points_return_vle = numpy.array([self.p.m['x'][1][0],
self.p.m['x'][1][1],
self.p.m['y'][1][0],
self.p.m['y'][1][1]
])
numpy.testing.assert_array_equal(self.points_ans_vle,
points_return_vle)
def test_d3(self):
"""Critical parameter calculation"""
crit_calc = numpy.array([self.p.c[1]['a_c'],
self.p.c[2]['b_c']
])
numpy.testing.assert_allclose(crit_calc, self.crit_ans, rtol=1e-03)
class TestNewData(unittest.TestCase):
"""
Test if newly added data is loading as expected.
TODO: Add simple argparse/config options to test new data
"""
import numpy
data = data_handling.ImportData()
# TODO: Allow for a system to test argument passes
data.comps = ['carbon_dioxide', 'ethane']
data.eos = 'DWPM'
data.model = 'Adachi-Lu'
data.phases = ['x','y']
data.r = None
data.s = None
data.k_params = None
data.load_pure_data() # Using data.comps
def test_nd1(self):
"""
New dataset key strings
"""
expected_string = numpy.array(['P_c (Pa)', 'b_c (m3 mol-1)', 'Z_c',
'V_c (m3 mol-1)', 'R (m3 Pa K-1 mol-1)', 'm (Soave)',
'virialT', 'T (K)','m (Adachi-Lu)', 'P (Pa)', 'w',
'virialB', 'T_c (K)', 'model','a_c (Pa m6 mol-2)',
'name'])
passlist = []
for i in range(len(self.data.c)):
passlist.append(test_container_string(self.data.c[i].keys(),
expected_string))
self.assertTrue(passlist)
if len(data.comps) > 1: # pure component simulation.
# Load VLE and mixture parameter data
data.load()
p = data_handling.MixParameters()
p.mixture_parameters(data.VLE, data)
p.m['n'] = len(data.comps) # Define system size
print p.m.keys()
for i in range(p.m['n']): # Set params for all compounds
p.parameters(data.c[i]) # Defines p.c[i]
def test_nd3(self):
"""
New dataset VLE object types
"""
for d_key in ['T', 'P', 'x']:
test_data_type(self, self.p.m, d_key, data_type=list)
for d_key in ['T', 'P']:
test_data_type(self, self.p.m, d_key, data_type=float)
for d_key in self.p.m['Valid phases']:
self.assertIs(type(self.p.m[d_key][1][0]), float)
self.assertIs(type(self.p.m[d_key][2][0]), float)
def data_handling_suite():
"""
Gather all the data_handling tests from this module in a test suite.
"""
TestData = unittest.TestSuite()
data_handling_suite1 = unittest.makeSuite(TestDataLoad)
#data_handling_suite2 = unittest.makeSuite(TestNewData)
TestData.addTest(data_handling_suite1)
#TestData.addTest(data_handling_suite2)
return TestData
if __name__ == '__main__':
TestData = data_handling_suite()
unittest.TextTestRunner(verbosity=2).run(TestData)