[3330bb4] | 1 | # -*- coding: utf-8 -*- |
---|
| 2 | """ |
---|
| 3 | Run model unit tests. |
---|
| 4 | |
---|
| 5 | Usage:: |
---|
| 6 | |
---|
[d92182f] | 7 | python -m sasmodels.model_test [opencl|cuda|dll|all] model1 model2 ... |
---|
[3330bb4] | 8 | |
---|
[d92182f] | 9 | If model1 is 'all', then all except the remaining models will be tested. |
---|
| 10 | Subgroups are also possible, such as 'py', 'single' or '1d'. See |
---|
| 11 | :func:`core.list_models` for details. |
---|
[3330bb4] | 12 | |
---|
| 13 | Each model is tested using the default parameters at q=0.1, (qx, qy)=(0.1, 0.1), |
---|
[39a06c9] | 14 | and Fq is called to make sure R_eff, volume and volume ratio are computed. |
---|
| 15 | The return values at these points are not considered. The test is only to |
---|
| 16 | verify that the models run to completion, and do not produce inf or NaN. |
---|
[3330bb4] | 17 | |
---|
| 18 | Tests are defined with the *tests* attribute in the model.py file. *tests* |
---|
| 19 | is a list of individual tests to run, where each test consists of the |
---|
| 20 | parameter values for the test, the q-values and the expected results. For |
---|
[39a06c9] | 21 | the effective radius test and volume ratio tests, use the extended output |
---|
| 22 | form, which checks each output of kernel.Fq. For 1-D tests, either specify |
---|
| 23 | the q value or a list of q-values, and the corresponding I(q) value, or |
---|
| 24 | list of I(q) values. |
---|
[3330bb4] | 25 | |
---|
| 26 | That is:: |
---|
| 27 | |
---|
| 28 | tests = [ |
---|
| 29 | [ {parameters}, q, I(q)], |
---|
| 30 | [ {parameters}, [q], [I(q)] ], |
---|
| 31 | [ {parameters}, [q1, q2, ...], [I(q1), I(q2), ...]], |
---|
| 32 | |
---|
| 33 | [ {parameters}, (qx, qy), I(qx, Iqy)], |
---|
| 34 | [ {parameters}, [(qx1, qy1), (qx2, qy2), ...], |
---|
| 35 | [I(qx1, qy1), I(qx2, qy2), ...]], |
---|
| 36 | |
---|
[39a06c9] | 37 | [ {parameters}, q, F(q), F^2(q), R_eff, V, V_r ], |
---|
[3330bb4] | 38 | ... |
---|
| 39 | ] |
---|
| 40 | |
---|
| 41 | Parameters are *key:value* pairs, where key is one of the parameters of the |
---|
| 42 | model and value is the value to use for the test. Any parameters not given |
---|
| 43 | in the parameter list will take on the default parameter value. |
---|
| 44 | |
---|
| 45 | Precision defaults to 5 digits (relative). |
---|
| 46 | """ |
---|
| 47 | from __future__ import print_function |
---|
| 48 | |
---|
[7b5898f] | 49 | import argparse |
---|
[3330bb4] | 50 | import sys |
---|
| 51 | import unittest |
---|
[12eec1e] | 52 | import traceback |
---|
[3330bb4] | 53 | |
---|
[9826f82] | 54 | try: |
---|
| 55 | from StringIO import StringIO |
---|
[bedb9b0] | 56 | except ImportError: |
---|
| 57 | # StringIO.StringIO renamed to io.StringIO in Python 3 |
---|
| 58 | # Note: io.StringIO exists in python 2, but using unicode instead of str |
---|
[9826f82] | 59 | from io import StringIO |
---|
[3330bb4] | 60 | |
---|
| 61 | import numpy as np # type: ignore |
---|
| 62 | |
---|
| 63 | from .core import list_models, load_model_info, build_model |
---|
[304c775] | 64 | from .direct_model import call_kernel, call_Fq |
---|
[3330bb4] | 65 | from .exception import annotate_exception |
---|
| 66 | from .modelinfo import expand_pars |
---|
[3221de0] | 67 | from .kernelcl import use_opencl |
---|
[74e9b5f] | 68 | from .kernelcuda import use_cuda |
---|
[81751c2] | 69 | from . import product |
---|
[3330bb4] | 70 | |
---|
[2d81cfe] | 71 | # pylint: disable=unused-import |
---|
[3330bb4] | 72 | try: |
---|
| 73 | from typing import List, Iterator, Callable |
---|
| 74 | except ImportError: |
---|
| 75 | pass |
---|
| 76 | else: |
---|
| 77 | from .modelinfo import ParameterTable, ParameterSet, TestCondition, ModelInfo |
---|
| 78 | from .kernel import KernelModel |
---|
[2d81cfe] | 79 | # pylint: enable=unused-import |
---|
[3330bb4] | 80 | |
---|
| 81 | def make_suite(loaders, models): |
---|
| 82 | # type: (List[str], List[str]) -> unittest.TestSuite |
---|
| 83 | """ |
---|
| 84 | Construct the pyunit test suite. |
---|
| 85 | |
---|
[74e9b5f] | 86 | *loaders* is the list of kernel drivers to use (dll, opencl or cuda). |
---|
| 87 | For python model the python driver is always used. |
---|
[3330bb4] | 88 | |
---|
| 89 | *models* is the list of models to test, or *["all"]* to test all models. |
---|
| 90 | """ |
---|
| 91 | suite = unittest.TestSuite() |
---|
| 92 | |
---|
[d92182f] | 93 | try: |
---|
| 94 | # See if the first model parses as a model group |
---|
| 95 | group = list_models(models[0]) |
---|
[3330bb4] | 96 | skip = models[1:] |
---|
[d92182f] | 97 | models = group |
---|
| 98 | except Exception: |
---|
[3330bb4] | 99 | skip = [] |
---|
| 100 | for model_name in models: |
---|
[12eec1e] | 101 | if model_name not in skip: |
---|
| 102 | model_info = load_model_info(model_name) |
---|
| 103 | _add_model_to_suite(loaders, suite, model_info) |
---|
[3330bb4] | 104 | |
---|
[12eec1e] | 105 | return suite |
---|
[3330bb4] | 106 | |
---|
[12eec1e] | 107 | def _add_model_to_suite(loaders, suite, model_info): |
---|
| 108 | ModelTestCase = _hide_model_case_from_nose() |
---|
| 109 | |
---|
| 110 | #print('------') |
---|
| 111 | #print('found tests in', model_name) |
---|
| 112 | #print('------') |
---|
| 113 | |
---|
| 114 | # if ispy then use the dll loader to call pykernel |
---|
| 115 | # don't try to call cl kernel since it will not be |
---|
| 116 | # available in some environmentes. |
---|
| 117 | is_py = callable(model_info.Iq) |
---|
| 118 | |
---|
| 119 | # Some OpenCL drivers seem to be flaky, and are not producing the |
---|
| 120 | # expected result. Since we don't have known test values yet for |
---|
| 121 | # all of our models, we are instead going to compare the results |
---|
| 122 | # for the 'smoke test' (that is, evaluation at q=0.1 for the default |
---|
| 123 | # parameters just to see that the model runs to completion) between |
---|
| 124 | # the OpenCL and the DLL. To do this, we define a 'stash' which is |
---|
| 125 | # shared between OpenCL and DLL tests. This is just a list. If the |
---|
| 126 | # list is empty (which it will be when DLL runs, if the DLL runs |
---|
| 127 | # first), then the results are appended to the list. If the list |
---|
| 128 | # is not empty (which it will be when OpenCL runs second), the results |
---|
| 129 | # are compared to the results stored in the first element of the list. |
---|
| 130 | # This is a horrible stateful hack which only makes sense because the |
---|
| 131 | # test suite is thrown away after being run once. |
---|
| 132 | stash = [] |
---|
| 133 | |
---|
| 134 | if is_py: # kernel implemented in python |
---|
| 135 | test_name = "%s-python"%model_info.name |
---|
| 136 | test_method_name = "test_%s_python" % model_info.id |
---|
| 137 | test = ModelTestCase(test_name, model_info, |
---|
[b297ba9] | 138 | test_method_name, |
---|
| 139 | platform="dll", # so that |
---|
| 140 | dtype="double", |
---|
| 141 | stash=stash) |
---|
[12eec1e] | 142 | suite.addTest(test) |
---|
| 143 | else: # kernel implemented in C |
---|
| 144 | |
---|
| 145 | # test using dll if desired |
---|
| 146 | if 'dll' in loaders or not use_opencl(): |
---|
| 147 | test_name = "%s-dll"%model_info.name |
---|
| 148 | test_method_name = "test_%s_dll" % model_info.id |
---|
[3330bb4] | 149 | test = ModelTestCase(test_name, model_info, |
---|
[b297ba9] | 150 | test_method_name, |
---|
| 151 | platform="dll", |
---|
| 152 | dtype="double", |
---|
| 153 | stash=stash) |
---|
[12eec1e] | 154 | suite.addTest(test) |
---|
| 155 | |
---|
| 156 | # test using opencl if desired and available |
---|
| 157 | if 'opencl' in loaders and use_opencl(): |
---|
| 158 | test_name = "%s-opencl"%model_info.name |
---|
| 159 | test_method_name = "test_%s_opencl" % model_info.id |
---|
| 160 | # Using dtype=None so that the models that are only |
---|
| 161 | # correct for double precision are not tested using |
---|
| 162 | # single precision. The choice is determined by the |
---|
| 163 | # presence of *single=False* in the model file. |
---|
| 164 | test = ModelTestCase(test_name, model_info, |
---|
[b297ba9] | 165 | test_method_name, |
---|
| 166 | platform="ocl", dtype=None, |
---|
| 167 | stash=stash) |
---|
[12eec1e] | 168 | #print("defining", test_name) |
---|
[3330bb4] | 169 | suite.addTest(test) |
---|
| 170 | |
---|
[aa8c6e0] | 171 | # test using cuda if desired and available |
---|
| 172 | if 'cuda' in loaders and use_cuda(): |
---|
[00afc15] | 173 | test_name = "%s-cuda" % model_info.id |
---|
[aa8c6e0] | 174 | test_method_name = "test_%s_cuda" % model_info.id |
---|
| 175 | # Using dtype=None so that the models that are only |
---|
| 176 | # correct for double precision are not tested using |
---|
| 177 | # single precision. The choice is determined by the |
---|
| 178 | # presence of *single=False* in the model file. |
---|
| 179 | test = ModelTestCase(test_name, model_info, |
---|
[b297ba9] | 180 | test_method_name, |
---|
| 181 | platform="cuda", dtype=None, |
---|
| 182 | stash=stash) |
---|
[aa8c6e0] | 183 | #print("defining", test_name) |
---|
[3330bb4] | 184 | suite.addTest(test) |
---|
[74e9b5f] | 185 | |
---|
[3330bb4] | 186 | |
---|
| 187 | def _hide_model_case_from_nose(): |
---|
| 188 | # type: () -> type |
---|
| 189 | class ModelTestCase(unittest.TestCase): |
---|
| 190 | """ |
---|
| 191 | Test suit for a particular model with a particular kernel driver. |
---|
| 192 | |
---|
| 193 | The test suite runs a simple smoke test to make sure the model |
---|
| 194 | functions, then runs the list of tests at the bottom of the model |
---|
| 195 | description file. |
---|
| 196 | """ |
---|
| 197 | def __init__(self, test_name, model_info, test_method_name, |
---|
| 198 | platform, dtype, stash): |
---|
| 199 | # type: (str, ModelInfo, str, str, DType, List[Any]) -> None |
---|
| 200 | self.test_name = test_name |
---|
| 201 | self.info = model_info |
---|
| 202 | self.platform = platform |
---|
| 203 | self.dtype = dtype |
---|
| 204 | self.stash = stash # container for the results of the first run |
---|
| 205 | |
---|
| 206 | setattr(self, test_method_name, self.run_all) |
---|
| 207 | unittest.TestCase.__init__(self, test_method_name) |
---|
| 208 | |
---|
| 209 | def run_all(self): |
---|
| 210 | # type: () -> None |
---|
| 211 | """ |
---|
| 212 | Run all the tests in the test suite, including smoke tests. |
---|
| 213 | """ |
---|
| 214 | smoke_tests = [ |
---|
| 215 | # test validity at reasonable values |
---|
| 216 | ({}, 0.1, None), |
---|
| 217 | ({}, (0.1, 0.1), None), |
---|
| 218 | # test validity at q = 0 |
---|
| 219 | #({}, 0.0, None), |
---|
| 220 | #({}, (0.0, 0.0), None), |
---|
| 221 | # test vector form |
---|
| 222 | ({}, [0.001, 0.01, 0.1], [None]*3), |
---|
| 223 | ({}, [(0.1, 0.1)]*2, [None]*2), |
---|
[81751c2] | 224 | # test that Fq will run, and return R_eff, V, V_r |
---|
[304c775] | 225 | ({}, 0.1, None, None, None, None, None), |
---|
[3330bb4] | 226 | ] |
---|
[65314f7] | 227 | tests = smoke_tests |
---|
[20fe0cd] | 228 | #tests = [] |
---|
[65314f7] | 229 | if self.info.tests is not None: |
---|
| 230 | tests += self.info.tests |
---|
[81751c2] | 231 | S_tests = [test for test in tests if '@S' in test[0]] |
---|
| 232 | P_tests = [test for test in tests if '@S' not in test[0]] |
---|
[3330bb4] | 233 | try: |
---|
| 234 | model = build_model(self.info, dtype=self.dtype, |
---|
| 235 | platform=self.platform) |
---|
[81751c2] | 236 | results = [self.run_one(model, test) for test in P_tests] |
---|
| 237 | for test in S_tests: |
---|
| 238 | # pull the S model name out of the test defn |
---|
| 239 | pars = test[0].copy() |
---|
| 240 | s_name = pars.pop('@S') |
---|
| 241 | ps_test = [pars] + list(test[1:]) |
---|
[a7db39fa] | 242 | #print("PS TEST PARAMS!!!",ps_test) |
---|
[81751c2] | 243 | # build the P@S model |
---|
| 244 | s_info = load_model_info(s_name) |
---|
| 245 | ps_info = product.make_product_info(self.info, s_info) |
---|
| 246 | ps_model = build_model(ps_info, dtype=self.dtype, |
---|
| 247 | platform=self.platform) |
---|
| 248 | # run the tests |
---|
[b39bf3b] | 249 | #self.info = ps_model.info |
---|
[a7db39fa] | 250 | #print("SELF.INFO PARAMS!!!",[p.id for p in self.info.parameters.call_parameters]) |
---|
| 251 | #print("PS MODEL PARAMETERS:",[p.id for p in ps_model.info.parameters.call_parameters]) |
---|
[81751c2] | 252 | results.append(self.run_one(ps_model, ps_test)) |
---|
| 253 | |
---|
[3330bb4] | 254 | if self.stash: |
---|
| 255 | for test, target, actual in zip(tests, self.stash[0], results): |
---|
[2d81cfe] | 256 | assert np.all(abs(target-actual) < 5e-5*abs(actual)), \ |
---|
| 257 | ("GPU/CPU comparison expected %s but got %s for %s" |
---|
| 258 | % (target, actual, test[0])) |
---|
[3330bb4] | 259 | else: |
---|
| 260 | self.stash.append(results) |
---|
| 261 | |
---|
| 262 | # Check for missing tests. Only do so for the "dll" tests |
---|
[74e9b5f] | 263 | # to reduce noise from both opencl and cuda, and because |
---|
[3330bb4] | 264 | # python kernels use platform="dll". |
---|
| 265 | if self.platform == "dll": |
---|
| 266 | missing = [] |
---|
| 267 | ## Uncomment the following to require test cases |
---|
| 268 | #missing = self._find_missing_tests() |
---|
| 269 | if missing: |
---|
| 270 | raise ValueError("Missing tests for "+", ".join(missing)) |
---|
| 271 | |
---|
| 272 | except: |
---|
| 273 | annotate_exception(self.test_name) |
---|
| 274 | raise |
---|
| 275 | |
---|
| 276 | def _find_missing_tests(self): |
---|
| 277 | # type: () -> None |
---|
[39a06c9] | 278 | """make sure there are 1D and 2D tests as appropriate""" |
---|
[3330bb4] | 279 | model_has_1D = True |
---|
| 280 | model_has_2D = any(p.type == 'orientation' |
---|
| 281 | for p in self.info.parameters.kernel_parameters) |
---|
| 282 | |
---|
| 283 | # Lists of tests that have a result that is not None |
---|
| 284 | single = [test for test in self.info.tests |
---|
| 285 | if not isinstance(test[2], list) and test[2] is not None] |
---|
| 286 | tests_has_1D_single = any(isinstance(test[1], float) for test in single) |
---|
| 287 | tests_has_2D_single = any(isinstance(test[1], tuple) for test in single) |
---|
| 288 | |
---|
| 289 | multiple = [test for test in self.info.tests |
---|
| 290 | if isinstance(test[2], list) |
---|
[bb4b509] | 291 | and not all(result is None for result in test[2])] |
---|
[3330bb4] | 292 | tests_has_1D_multiple = any(isinstance(test[1][0], float) |
---|
| 293 | for test in multiple) |
---|
| 294 | tests_has_2D_multiple = any(isinstance(test[1][0], tuple) |
---|
| 295 | for test in multiple) |
---|
| 296 | |
---|
| 297 | missing = [] |
---|
| 298 | if model_has_1D and not (tests_has_1D_single or tests_has_1D_multiple): |
---|
| 299 | missing.append("1D") |
---|
| 300 | if model_has_2D and not (tests_has_2D_single or tests_has_2D_multiple): |
---|
| 301 | missing.append("2D") |
---|
| 302 | |
---|
| 303 | return missing |
---|
| 304 | |
---|
| 305 | def run_one(self, model, test): |
---|
| 306 | # type: (KernelModel, TestCondition) -> None |
---|
| 307 | """Run a single test case.""" |
---|
[304c775] | 308 | user_pars, x, y = test[:3] |
---|
[b39bf3b] | 309 | print("PS MODEL PARAMETERS:",[p.id for p in model.info.parameters.call_parameters]) |
---|
| 310 | pars = expand_pars(model.info.parameters, user_pars) |
---|
| 311 | invalid = invalid_pars(model.info.parameters, pars) |
---|
[bb4b509] | 312 | if invalid: |
---|
| 313 | raise ValueError("Unknown parameters in test: " + ", ".join(invalid)) |
---|
[3330bb4] | 314 | |
---|
| 315 | if not isinstance(y, list): |
---|
| 316 | y = [y] |
---|
| 317 | if not isinstance(x, list): |
---|
| 318 | x = [x] |
---|
| 319 | |
---|
| 320 | self.assertEqual(len(y), len(x)) |
---|
| 321 | |
---|
[304c775] | 322 | if isinstance(x[0], tuple): |
---|
[3330bb4] | 323 | qx, qy = zip(*x) |
---|
| 324 | q_vectors = [np.array(qx), np.array(qy)] |
---|
| 325 | else: |
---|
| 326 | q_vectors = [np.array(x)] |
---|
| 327 | |
---|
[304c775] | 328 | kernel = model.make_kernel(q_vectors) |
---|
| 329 | if len(test) == 3: |
---|
| 330 | actual = call_kernel(kernel, pars) |
---|
| 331 | self._check_vectors(x, y, actual, 'I') |
---|
| 332 | return actual |
---|
| 333 | else: |
---|
| 334 | y1 = y |
---|
[6652522] | 335 | y2 = test[3] if isinstance(test[3], list) else [test[3]] |
---|
| 336 | F, Fsq, R_eff, volume, volume_ratio = call_Fq(kernel, pars) |
---|
| 337 | if F is not None: # F is none for models with Iq instead of Fq |
---|
| 338 | self._check_vectors(x, y1, F, 'F') |
---|
| 339 | self._check_vectors(x, y2, Fsq, 'F^2') |
---|
[304c775] | 340 | self._check_scalar(test[4], R_eff, 'R_eff') |
---|
| 341 | self._check_scalar(test[5], volume, 'volume') |
---|
| 342 | self._check_scalar(test[6], volume_ratio, 'form:shell ratio') |
---|
[6652522] | 343 | return Fsq |
---|
[304c775] | 344 | |
---|
| 345 | def _check_scalar(self, target, actual, name): |
---|
| 346 | if target is None: |
---|
| 347 | # smoke test --- make sure it runs and produces a value |
---|
| 348 | self.assertTrue(not np.isnan(actual), |
---|
| 349 | 'invalid %s: %s' % (name, actual)) |
---|
| 350 | elif np.isnan(target): |
---|
| 351 | # make sure nans match |
---|
| 352 | self.assertTrue(np.isnan(actual), |
---|
| 353 | '%s: expected:%s; actual:%s' |
---|
| 354 | % (name, target, actual)) |
---|
| 355 | else: |
---|
| 356 | # is_near does not work for infinite values, so also test |
---|
| 357 | # for exact values. |
---|
| 358 | self.assertTrue(target == actual or is_near(target, actual, 5), |
---|
| 359 | '%s: expected:%s; actual:%s' |
---|
| 360 | % (name, target, actual)) |
---|
| 361 | |
---|
| 362 | def _check_vectors(self, x, target, actual, name='I'): |
---|
| 363 | self.assertTrue(len(actual) > 0, |
---|
| 364 | '%s(...) expected return'%name) |
---|
| 365 | if target is None: |
---|
| 366 | return |
---|
| 367 | self.assertEqual(len(target), len(actual), |
---|
| 368 | '%s(...) returned wrong length'%name) |
---|
| 369 | for xi, yi, actual_yi in zip(x, target, actual): |
---|
[3330bb4] | 370 | if yi is None: |
---|
| 371 | # smoke test --- make sure it runs and produces a value |
---|
| 372 | self.assertTrue(not np.isnan(actual_yi), |
---|
[304c775] | 373 | 'invalid %s(%s): %s' % (name, xi, actual_yi)) |
---|
[3330bb4] | 374 | elif np.isnan(yi): |
---|
[304c775] | 375 | # make sure nans match |
---|
[3330bb4] | 376 | self.assertTrue(np.isnan(actual_yi), |
---|
[304c775] | 377 | '%s(%s): expected:%s; actual:%s' |
---|
| 378 | % (name, xi, yi, actual_yi)) |
---|
[3330bb4] | 379 | else: |
---|
| 380 | # is_near does not work for infinite values, so also test |
---|
[304c775] | 381 | # for exact values. |
---|
[3330bb4] | 382 | self.assertTrue(yi == actual_yi or is_near(yi, actual_yi, 5), |
---|
[304c775] | 383 | '%s(%s); expected:%s; actual:%s' |
---|
| 384 | % (name, xi, yi, actual_yi)) |
---|
[3330bb4] | 385 | |
---|
| 386 | return ModelTestCase |
---|
| 387 | |
---|
[bb4b509] | 388 | def invalid_pars(partable, pars): |
---|
| 389 | # type: (ParameterTable, Dict[str, float]) |
---|
| 390 | """ |
---|
| 391 | Return a list of parameter names that are not part of the model. |
---|
| 392 | """ |
---|
| 393 | names = set(p.id for p in partable.call_parameters) |
---|
| 394 | invalid = [] |
---|
| 395 | for par in sorted(pars.keys()): |
---|
[304c775] | 396 | # special handling of R_eff mode, which is not a usual parameter |
---|
[5024a56] | 397 | if par == product.RADIUS_MODE_ID: |
---|
[304c775] | 398 | continue |
---|
[0b8a1fc] | 399 | if par == product.RADIUS_TYPE_ID: |
---|
| 400 | continue |
---|
[265c657] | 401 | if par == product.STRUCTURE_MODE_ID: |
---|
| 402 | continue |
---|
[bb4b509] | 403 | parts = par.split('_pd') |
---|
| 404 | if len(parts) > 1 and parts[1] not in ("", "_n", "nsigma", "type"): |
---|
| 405 | invalid.append(par) |
---|
| 406 | continue |
---|
| 407 | if parts[0] not in names: |
---|
| 408 | invalid.append(par) |
---|
| 409 | return invalid |
---|
| 410 | |
---|
| 411 | |
---|
[3330bb4] | 412 | def is_near(target, actual, digits=5): |
---|
| 413 | # type: (float, float, int) -> bool |
---|
| 414 | """ |
---|
| 415 | Returns true if *actual* is within *digits* significant digits of *target*. |
---|
| 416 | """ |
---|
| 417 | import math |
---|
[6652522] | 418 | if target == 0.: |
---|
| 419 | return actual == 0. |
---|
[a430f5f] | 420 | shift = 10**math.ceil(math.log10(np.abs(target))) |
---|
| 421 | return np.abs(target-actual)/shift < 1.5*10**-digits |
---|
[3330bb4] | 422 | |
---|
[12eec1e] | 423 | # CRUFT: old interface; should be deprecated and removed |
---|
| 424 | def run_one(model_name): |
---|
[b297ba9] | 425 | # type: (str) -> str |
---|
| 426 | """ |
---|
| 427 | [Deprecated] Run the tests associated with *model_name*. |
---|
| 428 | |
---|
| 429 | Use the following instead:: |
---|
| 430 | |
---|
| 431 | succss, output = check_model(load_model_info(model_name)) |
---|
| 432 | """ |
---|
[12eec1e] | 433 | # msg = "use check_model(model_info) rather than run_one(model_name)" |
---|
| 434 | # warnings.warn(msg, category=DeprecationWarning, stacklevel=2) |
---|
| 435 | try: |
---|
| 436 | model_info = load_model_info(model_name) |
---|
| 437 | except Exception: |
---|
| 438 | output = traceback.format_exc() |
---|
| 439 | return output |
---|
| 440 | |
---|
[b297ba9] | 441 | _, output = check_model(model_info) |
---|
[12eec1e] | 442 | return output |
---|
| 443 | |
---|
| 444 | def check_model(model_info): |
---|
[b297ba9] | 445 | # type: (ModelInfo) -> Tuple[bool, str] |
---|
[3330bb4] | 446 | """ |
---|
[12eec1e] | 447 | Run the tests for a single model, capturing the output. |
---|
[3330bb4] | 448 | |
---|
[12eec1e] | 449 | Returns success status and the output string. |
---|
[3330bb4] | 450 | """ |
---|
| 451 | # Note that running main() directly did not work from within the |
---|
| 452 | # wxPython pycrust console. Instead of the results appearing in the |
---|
| 453 | # window they were printed to the underlying console. |
---|
| 454 | from unittest.runner import TextTestResult, _WritelnDecorator |
---|
| 455 | |
---|
| 456 | # Build a object to capture and print the test results |
---|
[f2cbeb7] | 457 | stream = _WritelnDecorator(StringIO()) # Add writeln() method to stream |
---|
[3330bb4] | 458 | verbosity = 2 |
---|
| 459 | descriptions = True |
---|
| 460 | result = TextTestResult(stream, descriptions, verbosity) |
---|
| 461 | |
---|
| 462 | # Build a test suite containing just the model |
---|
[aa8c6e0] | 463 | loaders = ['opencl' if use_opencl() else 'cuda' if use_cuda() else 'dll'] |
---|
[12eec1e] | 464 | suite = unittest.TestSuite() |
---|
| 465 | _add_model_to_suite(loaders, suite, model_info) |
---|
[3330bb4] | 466 | |
---|
| 467 | # Warn if there are no user defined tests. |
---|
| 468 | # Note: the test suite constructed above only has one test in it, which |
---|
| 469 | # runs through some smoke tests to make sure the model runs, then runs |
---|
| 470 | # through the input-output pairs given in the model definition file. To |
---|
| 471 | # check if any such pairs are defined, therefore, we just need to check if |
---|
| 472 | # they are in the first test of the test suite. We do this with an |
---|
| 473 | # iterator since we don't have direct access to the list of tests in the |
---|
| 474 | # test suite. |
---|
[012cd34] | 475 | # In Qt5 suite.run() will clear all tests in the suite after running |
---|
| 476 | # with no way of retaining them for the test below, so let's check |
---|
| 477 | # for user tests before running the suite. |
---|
[3330bb4] | 478 | for test in suite: |
---|
| 479 | if not test.info.tests: |
---|
[12eec1e] | 480 | stream.writeln("Note: %s has no user defined tests."%model_info.name) |
---|
[3330bb4] | 481 | break |
---|
| 482 | else: |
---|
| 483 | stream.writeln("Note: no test suite created --- this should never happen") |
---|
| 484 | |
---|
[012cd34] | 485 | # Run the test suite |
---|
| 486 | suite.run(result) |
---|
| 487 | |
---|
| 488 | # Print the failures and errors |
---|
| 489 | for _, tb in result.errors: |
---|
| 490 | stream.writeln(tb) |
---|
| 491 | for _, tb in result.failures: |
---|
| 492 | stream.writeln(tb) |
---|
| 493 | |
---|
[f2cbeb7] | 494 | output = stream.getvalue() |
---|
| 495 | stream.close() |
---|
[12eec1e] | 496 | return result.wasSuccessful(), output |
---|
[f2cbeb7] | 497 | |
---|
[3330bb4] | 498 | |
---|
[d92182f] | 499 | def model_tests(): |
---|
| 500 | # type: () -> Iterator[Callable[[], None]] |
---|
| 501 | """ |
---|
| 502 | Test runner visible to nosetests. |
---|
| 503 | |
---|
| 504 | Run "nosetests sasmodels" on the command line to invoke it. |
---|
| 505 | """ |
---|
| 506 | loaders = ['dll'] |
---|
| 507 | if use_opencl(): |
---|
| 508 | loaders.append('opencl') |
---|
| 509 | if use_cuda(): |
---|
| 510 | loaders.append('cuda') |
---|
| 511 | tests = make_suite(loaders, ['all']) |
---|
[b297ba9] | 512 | def _build_test(test): |
---|
[d92182f] | 513 | # In order for nosetest to show the test name, wrap the test.run_all |
---|
| 514 | # instance in function that takes the test name as a parameter which |
---|
| 515 | # will be displayed when the test is run. Do this as a function so |
---|
| 516 | # that it properly captures the context for tests that captured and |
---|
| 517 | # run later. If done directly in the for loop, then the looping |
---|
| 518 | # variable test will be shared amongst all the tests, and we will be |
---|
| 519 | # repeatedly testing vesicle. |
---|
| 520 | |
---|
| 521 | # Note: in sasview sas.sasgui.perspectives.fitting.gpu_options |
---|
| 522 | # requires that the test.description field be set. |
---|
| 523 | wrap = lambda: test.run_all() |
---|
| 524 | wrap.description = test.test_name |
---|
| 525 | return wrap |
---|
| 526 | # The following would work with nosetests and pytest: |
---|
| 527 | # return lambda name: test.run_all(), test.test_name |
---|
| 528 | |
---|
| 529 | for test in tests: |
---|
[b297ba9] | 530 | yield _build_test(test) |
---|
[d92182f] | 531 | |
---|
| 532 | |
---|
[7b5898f] | 533 | def main(): |
---|
[3330bb4] | 534 | # type: (*str) -> int |
---|
| 535 | """ |
---|
| 536 | Run tests given is models. |
---|
| 537 | |
---|
| 538 | Returns 0 if success or 1 if any tests fail. |
---|
| 539 | """ |
---|
| 540 | try: |
---|
| 541 | from xmlrunner import XMLTestRunner as TestRunner |
---|
| 542 | test_args = {'output': 'logs'} |
---|
| 543 | except ImportError: |
---|
| 544 | from unittest import TextTestRunner as TestRunner |
---|
| 545 | test_args = {} |
---|
| 546 | |
---|
[7b5898f] | 547 | parser = argparse.ArgumentParser(description="Test SasModels Models") |
---|
| 548 | parser.add_argument("-v", "--verbose", action="store_const", |
---|
| 549 | default=1, const=2, help="Use verbose output") |
---|
[d92182f] | 550 | parser.add_argument("-e", "--engine", default="all", |
---|
[7b5898f] | 551 | help="Engines on which to run the test. " |
---|
[d92182f] | 552 | "Valid values are opencl, cuda, dll, and all. " |
---|
| 553 | "Defaults to all if no value is given") |
---|
[7b5898f] | 554 | parser.add_argument("models", nargs="*", |
---|
[d92182f] | 555 | help="The names of the models to be tested. " |
---|
| 556 | "If the first model is 'all', then all but the listed " |
---|
| 557 | "models will be tested. See core.list_models() for " |
---|
| 558 | "names of other groups, such as 'py' or 'single'.") |
---|
[b297ba9] | 559 | opts = parser.parse_args() |
---|
[7b5898f] | 560 | |
---|
[b297ba9] | 561 | if opts.engine == "opencl": |
---|
[3221de0] | 562 | if not use_opencl(): |
---|
[3330bb4] | 563 | print("opencl is not available") |
---|
| 564 | return 1 |
---|
| 565 | loaders = ['opencl'] |
---|
[b297ba9] | 566 | elif opts.engine == "dll": |
---|
[7b5898f] | 567 | loaders = ["dll"] |
---|
[b297ba9] | 568 | elif opts.engine == "cuda": |
---|
[74e9b5f] | 569 | if not use_cuda(): |
---|
| 570 | print("cuda is not available") |
---|
| 571 | return 1 |
---|
| 572 | loaders = ['cuda'] |
---|
[b297ba9] | 573 | elif opts.engine == "all": |
---|
[74e9b5f] | 574 | loaders = ['dll'] |
---|
| 575 | if use_opencl(): |
---|
| 576 | loaders.append('opencl') |
---|
| 577 | if use_cuda(): |
---|
| 578 | loaders.append('cuda') |
---|
[d92182f] | 579 | else: |
---|
[b297ba9] | 580 | print("unknown engine " + opts.engine) |
---|
[d92182f] | 581 | return 1 |
---|
[3330bb4] | 582 | |
---|
[b297ba9] | 583 | runner = TestRunner(verbosity=opts.verbose, **test_args) |
---|
| 584 | result = runner.run(make_suite(loaders, opts.models)) |
---|
[3330bb4] | 585 | return 1 if result.failures or result.errors else 0 |
---|
| 586 | |
---|
| 587 | |
---|
| 588 | if __name__ == "__main__": |
---|
[7b5898f] | 589 | sys.exit(main()) |
---|