[5428233] | 1 | # -*- coding: utf-8 -*- |
---|
| 2 | """ |
---|
[9890053] | 3 | Run model unit tests. |
---|
[5428233] | 4 | |
---|
[9890053] | 5 | Usage:: |
---|
| 6 | |
---|
[3c56da87] | 7 | python -m sasmodels.model_test [opencl|dll|opencl_and_dll] model1 model2 ... |
---|
[9890053] | 8 | |
---|
[3c56da87] | 9 | if model1 is 'all', then all except the remaining models will be tested |
---|
[9890053] | 10 | |
---|
[823e620] | 11 | Each model is tested using the default parameters at q=0.1, (qx, qy)=(0.1, 0.1), |
---|
[9890053] | 12 | and the ER and VR are computed. The return values at these points are not |
---|
| 13 | considered. The test is only to verify that the models run to completion, |
---|
| 14 | and do not produce inf or NaN. |
---|
| 15 | |
---|
| 16 | Tests are defined with the *tests* attribute in the model.py file. *tests* |
---|
| 17 | is a list of individual tests to run, where each test consists of the |
---|
| 18 | parameter values for the test, the q-values and the expected results. For |
---|
| 19 | the effective radius test, the q-value should be 'ER'. For the VR test, |
---|
| 20 | the q-value should be 'VR'. For 1-D tests, either specify the q value or |
---|
| 21 | a list of q-values, and the corresponding I(q) value, or list of I(q) values. |
---|
| 22 | |
---|
| 23 | That is:: |
---|
| 24 | |
---|
| 25 | tests = [ |
---|
| 26 | [ {parameters}, q, I(q)], |
---|
| 27 | [ {parameters}, [q], [I(q)] ], |
---|
| 28 | [ {parameters}, [q1, q2, ...], [I(q1), I(q2), ...]], |
---|
| 29 | |
---|
| 30 | [ {parameters}, (qx, qy), I(qx, Iqy)], |
---|
[d6adfbe] | 31 | [ {parameters}, [(qx1, qy1), (qx2, qy2), ...], |
---|
[823e620] | 32 | [I(qx1, qy1), I(qx2, qy2), ...]], |
---|
[9890053] | 33 | |
---|
| 34 | [ {parameters}, 'ER', ER(pars) ], |
---|
| 35 | [ {parameters}, 'VR', VR(pars) ], |
---|
| 36 | ... |
---|
| 37 | ] |
---|
| 38 | |
---|
| 39 | Parameters are *key:value* pairs, where key is one of the parameters of the |
---|
| 40 | model and value is the value to use for the test. Any parameters not given |
---|
| 41 | in the parameter list will take on the default parameter value. |
---|
| 42 | |
---|
| 43 | Precision defaults to 5 digits (relative). |
---|
[5428233] | 44 | """ |
---|
[5c962df] | 45 | from __future__ import print_function |
---|
[5428233] | 46 | |
---|
[f734e7d] | 47 | import sys |
---|
[5428233] | 48 | import unittest |
---|
| 49 | |
---|
[f734e7d] | 50 | import numpy as np |
---|
[5428233] | 51 | |
---|
[aa4946b] | 52 | from .core import list_models, load_model_definition, load_model, HAVE_OPENCL |
---|
[9890053] | 53 | from .core import make_kernel, call_kernel, call_ER, call_VR |
---|
[9e430d0] | 54 | from .exception import annotate_exception |
---|
[3c56da87] | 55 | |
---|
[aa4946b] | 56 | |
---|
[3c56da87] | 57 | def make_suite(loaders, models): |
---|
[5c962df] | 58 | """ |
---|
| 59 | Construct the pyunit test suite. |
---|
| 60 | |
---|
| 61 | *loaders* is the list of kernel drivers to use, which is one of |
---|
| 62 | *["dll", "opencl"]*, *["dll"]* or *["opencl"]*. For python models, |
---|
| 63 | the python driver is always used. |
---|
| 64 | |
---|
| 65 | *models* is the list of models to test, or *["all"]* to test all models. |
---|
| 66 | """ |
---|
[f734e7d] | 67 | |
---|
[d6adfbe] | 68 | ModelTestCase = _hide_model_case_from_nosetests() |
---|
[5428233] | 69 | suite = unittest.TestSuite() |
---|
| 70 | |
---|
[f734e7d] | 71 | if models[0] == 'all': |
---|
| 72 | skip = models[1:] |
---|
| 73 | models = list_models() |
---|
| 74 | else: |
---|
| 75 | skip = [] |
---|
| 76 | for model_name in models: |
---|
| 77 | if model_name in skip: continue |
---|
| 78 | model_definition = load_model_definition(model_name) |
---|
| 79 | |
---|
[9404dd3] | 80 | #print('------') |
---|
| 81 | #print('found tests in', model_name) |
---|
| 82 | #print('------') |
---|
[503527e] | 83 | |
---|
| 84 | # if ispy then use the dll loader to call pykernel |
---|
| 85 | # don't try to call cl kernel since it will not be |
---|
| 86 | # available in some environmentes. |
---|
[823e620] | 87 | is_py = callable(getattr(model_definition, 'Iq', None)) |
---|
[503527e] | 88 | |
---|
| 89 | if is_py: # kernel implemented in python |
---|
| 90 | test_name = "Model: %s, Kernel: python"%model_name |
---|
| 91 | test_method_name = "test_%s_python" % model_name |
---|
| 92 | test = ModelTestCase(test_name, model_definition, |
---|
| 93 | test_method_name, |
---|
| 94 | platform="dll", # so that |
---|
| 95 | dtype="double") |
---|
| 96 | suite.addTest(test) |
---|
| 97 | else: # kernel implemented in C |
---|
| 98 | # test using opencl if desired and available |
---|
| 99 | if 'opencl' in loaders and HAVE_OPENCL: |
---|
[f734e7d] | 100 | test_name = "Model: %s, Kernel: OpenCL"%model_name |
---|
[503527e] | 101 | test_method_name = "test_%s_opencl" % model_name |
---|
[13ed84c] | 102 | # Using dtype=None so that the models that are only |
---|
| 103 | # correct for double precision are not tested using |
---|
| 104 | # single precision. The choice is determined by the |
---|
| 105 | # presence of *single=False* in the model file. |
---|
[f734e7d] | 106 | test = ModelTestCase(test_name, model_definition, |
---|
[503527e] | 107 | test_method_name, |
---|
[13ed84c] | 108 | platform="ocl", dtype=None) |
---|
[9404dd3] | 109 | #print("defining", test_name) |
---|
[f734e7d] | 110 | suite.addTest(test) |
---|
| 111 | |
---|
| 112 | # test using dll if desired |
---|
[503527e] | 113 | if 'dll' in loaders: |
---|
[f734e7d] | 114 | test_name = "Model: %s, Kernel: dll"%model_name |
---|
[503527e] | 115 | test_method_name = "test_%s_dll" % model_name |
---|
[f734e7d] | 116 | test = ModelTestCase(test_name, model_definition, |
---|
[503527e] | 117 | test_method_name, |
---|
| 118 | platform="dll", |
---|
| 119 | dtype="double") |
---|
[f734e7d] | 120 | suite.addTest(test) |
---|
[5428233] | 121 | |
---|
| 122 | return suite |
---|
| 123 | |
---|
[aa4946b] | 124 | |
---|
[d6adfbe] | 125 | def _hide_model_case_from_nosetests(): |
---|
| 126 | class ModelTestCase(unittest.TestCase): |
---|
[5c962df] | 127 | """ |
---|
| 128 | Test suit for a particular model with a particular kernel driver. |
---|
| 129 | |
---|
| 130 | The test suite runs a simple smoke test to make sure the model |
---|
| 131 | functions, then runs the list of tests at the bottom of the model |
---|
| 132 | description file. |
---|
| 133 | """ |
---|
[503527e] | 134 | def __init__(self, test_name, definition, test_method_name, |
---|
[aa4946b] | 135 | platform, dtype): |
---|
[d6adfbe] | 136 | self.test_name = test_name |
---|
| 137 | self.definition = definition |
---|
[aa4946b] | 138 | self.platform = platform |
---|
| 139 | self.dtype = dtype |
---|
[d6adfbe] | 140 | |
---|
[503527e] | 141 | setattr(self, test_method_name, self._runTest) |
---|
| 142 | unittest.TestCase.__init__(self, test_method_name) |
---|
[1b50776] | 143 | |
---|
| 144 | def _runTest(self): |
---|
[503527e] | 145 | smoke_tests = [ |
---|
[823e620] | 146 | [{}, 0.1, None], |
---|
| 147 | [{}, (0.1, 0.1), None], |
---|
| 148 | [{}, 'ER', None], |
---|
| 149 | [{}, 'VR', None], |
---|
[503527e] | 150 | ] |
---|
| 151 | |
---|
| 152 | tests = getattr(self.definition, 'tests', []) |
---|
[d6adfbe] | 153 | try: |
---|
[aa4946b] | 154 | model = load_model(self.definition, dtype=self.dtype, |
---|
| 155 | platform=self.platform) |
---|
[503527e] | 156 | for test in smoke_tests + tests: |
---|
[3c56da87] | 157 | self._run_one_test(model, test) |
---|
[d6adfbe] | 158 | |
---|
[503527e] | 159 | if not tests and self.platform == "dll": |
---|
| 160 | ## Uncomment the following to make forgetting the test |
---|
| 161 | ## values an error. Only do so for the "dll" tests |
---|
| 162 | ## to reduce noise from both opencl and dll, and because |
---|
[13ed84c] | 163 | ## python kernels use platform="dll". |
---|
[2781b2e] | 164 | #raise Exception("No test cases provided") |
---|
[503527e] | 165 | pass |
---|
| 166 | |
---|
[9404dd3] | 167 | except Exception as exc: |
---|
[d6adfbe] | 168 | annotate_exception(exc, self.test_name) |
---|
| 169 | raise |
---|
[f734e7d] | 170 | |
---|
[3c56da87] | 171 | def _run_one_test(self, model, test): |
---|
| 172 | pars, x, y = test |
---|
| 173 | |
---|
| 174 | if not isinstance(y, list): |
---|
| 175 | y = [y] |
---|
| 176 | if not isinstance(x, list): |
---|
| 177 | x = [x] |
---|
| 178 | |
---|
| 179 | self.assertEqual(len(y), len(x)) |
---|
| 180 | |
---|
| 181 | if x[0] == 'ER': |
---|
| 182 | actual = [call_ER(model.info, pars)] |
---|
| 183 | elif x[0] == 'VR': |
---|
| 184 | actual = [call_VR(model.info, pars)] |
---|
| 185 | elif isinstance(x[0], tuple): |
---|
[823e620] | 186 | Qx, Qy = zip(*x) |
---|
[3c56da87] | 187 | q_vectors = [np.array(Qx), np.array(Qy)] |
---|
| 188 | kernel = make_kernel(model, q_vectors) |
---|
| 189 | actual = call_kernel(kernel, pars) |
---|
| 190 | else: |
---|
| 191 | q_vectors = [np.array(x)] |
---|
| 192 | kernel = make_kernel(model, q_vectors) |
---|
| 193 | actual = call_kernel(kernel, pars) |
---|
| 194 | |
---|
| 195 | self.assertGreater(len(actual), 0) |
---|
| 196 | self.assertEqual(len(y), len(actual)) |
---|
| 197 | |
---|
| 198 | for xi, yi, actual_yi in zip(x, y, actual): |
---|
| 199 | if yi is None: |
---|
| 200 | # smoke test --- make sure it runs and produces a value |
---|
| 201 | self.assertTrue(np.isfinite(actual_yi), |
---|
[823e620] | 202 | 'invalid f(%s): %s' % (xi, actual_yi)) |
---|
[3c56da87] | 203 | else: |
---|
[13ed84c] | 204 | self.assertTrue(is_near(yi, actual_yi, 5), |
---|
[823e620] | 205 | 'f(%s); expected:%s; actual:%s' |
---|
| 206 | % (xi, yi, actual_yi)) |
---|
[3c56da87] | 207 | |
---|
[d6adfbe] | 208 | return ModelTestCase |
---|
| 209 | |
---|
[13ed84c] | 210 | def is_near(target, actual, digits=5): |
---|
| 211 | """ |
---|
| 212 | Returns true if *actual* is within *digits* significant digits of *target*. |
---|
| 213 | """ |
---|
| 214 | import math |
---|
| 215 | shift = 10**math.ceil(math.log10(abs(target))) |
---|
| 216 | return abs(target-actual)/shift < 1.5*10**-digits |
---|
[d6adfbe] | 217 | |
---|
| 218 | def main(): |
---|
[aa4946b] | 219 | """ |
---|
| 220 | Run tests given is sys.argv. |
---|
| 221 | |
---|
| 222 | Returns 0 if success or 1 if any tests fail. |
---|
| 223 | """ |
---|
[db8756e] | 224 | import xmlrunner |
---|
| 225 | |
---|
[f734e7d] | 226 | models = sys.argv[1:] |
---|
[13ed84c] | 227 | if models and models[0] == '-v': |
---|
| 228 | verbosity = 2 |
---|
| 229 | models = models[1:] |
---|
| 230 | else: |
---|
| 231 | verbosity = 1 |
---|
[f734e7d] | 232 | if models and models[0] == 'opencl': |
---|
[aa4946b] | 233 | if not HAVE_OPENCL: |
---|
[9404dd3] | 234 | print("opencl is not available") |
---|
[d6adfbe] | 235 | return 1 |
---|
[f734e7d] | 236 | loaders = ['opencl'] |
---|
| 237 | models = models[1:] |
---|
| 238 | elif models and models[0] == 'dll': |
---|
| 239 | # TODO: test if compiler is available? |
---|
| 240 | loaders = ['dll'] |
---|
| 241 | models = models[1:] |
---|
[84e01d2] | 242 | elif models and models[0] == 'opencl_and_dll': |
---|
| 243 | loaders = ['opencl', 'dll'] |
---|
| 244 | models = models[1:] |
---|
[f734e7d] | 245 | else: |
---|
| 246 | loaders = ['opencl', 'dll'] |
---|
[d6adfbe] | 247 | if not models: |
---|
[9404dd3] | 248 | print("""\ |
---|
[503527e] | 249 | usage: |
---|
[13ed84c] | 250 | python -m sasmodels.model_test [-v] [opencl|dll] model1 model2 ... |
---|
| 251 | |
---|
| 252 | If -v is included on the |
---|
| 253 | If neither opencl nor dll is specified, then models will be tested with |
---|
| 254 | both opencl and dll; the compute target is ignored for pure python models. |
---|
[503527e] | 255 | |
---|
| 256 | If model1 is 'all', then all except the remaining models will be tested. |
---|
[13ed84c] | 257 | |
---|
| 258 | """) |
---|
[503527e] | 259 | |
---|
[d6adfbe] | 260 | return 1 |
---|
| 261 | |
---|
[ddfe69c] | 262 | #runner = unittest.TextTestRunner() |
---|
[13ed84c] | 263 | runner = xmlrunner.XMLTestRunner(output='logs', verbosity=verbosity) |
---|
[3c56da87] | 264 | result = runner.run(make_suite(loaders, models)) |
---|
[d6adfbe] | 265 | return 1 if result.failures or result.errors else 0 |
---|
[5428233] | 266 | |
---|
[a217f7d] | 267 | |
---|
| 268 | def model_tests(): |
---|
[aa4946b] | 269 | """ |
---|
| 270 | Test runner visible to nosetests. |
---|
| 271 | |
---|
| 272 | Run "nosetests sasmodels" on the command line to invoke it. |
---|
| 273 | """ |
---|
[823e620] | 274 | tests = make_suite(['opencl', 'dll'], ['all']) |
---|
[a217f7d] | 275 | for test_i in tests: |
---|
| 276 | yield test_i._runTest |
---|
| 277 | |
---|
[aa4946b] | 278 | |
---|
[5428233] | 279 | if __name__ == "__main__": |
---|
[d6adfbe] | 280 | sys.exit(main()) |
---|