source: sasmodels/sasmodels/model_test.py @ 65314f7

core_shell_microgelscostrafo411magnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 65314f7 was 65314f7, checked in by lewis, 7 years ago

Ensure tests for new sum/multi models pass

  • Property mode set to 100644
File size: 18.1 KB
Line 
1# -*- coding: utf-8 -*-
2"""
3Run model unit tests.
4
5Usage::
6
7    python -m sasmodels.model_test [opencl|dll|opencl_and_dll] model1 model2 ...
8
9    if model1 is 'all', then all except the remaining models will be tested
10
11Each model is tested using the default parameters at q=0.1, (qx, qy)=(0.1, 0.1),
12and the ER and VR are computed.  The return values at these points are not
13considered.  The test is only to verify that the models run to completion,
14and do not produce inf or NaN.
15
16Tests are defined with the *tests* attribute in the model.py file.  *tests*
17is a list of individual tests to run, where each test consists of the
18parameter values for the test, the q-values and the expected results.  For
19the effective radius test, the q-value should be 'ER'.  For the VR test,
20the q-value should be 'VR'.  For 1-D tests, either specify the q value or
21a list of q-values, and the corresponding I(q) value, or list of I(q) values.
22
23That 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)],
31        [ {parameters}, [(qx1, qy1), (qx2, qy2), ...],
32                        [I(qx1, qy1), I(qx2, qy2), ...]],
33
34        [ {parameters}, 'ER', ER(pars) ],
35        [ {parameters}, 'VR', VR(pars) ],
36        ...
37    ]
38
39Parameters are *key:value* pairs, where key is one of the parameters of the
40model and value is the value to use for the test.  Any parameters not given
41in the parameter list will take on the default parameter value.
42
43Precision defaults to 5 digits (relative).
44"""
45from __future__ import print_function
46
47import sys
48import unittest
49
50try:
51    from StringIO import StringIO
52except ImportError:
53    # StringIO.StringIO renamed to io.StringIO in Python 3
54    # Note: io.StringIO exists in python 2, but using unicode instead of str
55    from io import StringIO
56
57import numpy as np  # type: ignore
58
59from . import core
60from .core import list_models, load_model_info, build_model
61from .direct_model import call_kernel, call_ER, call_VR
62from .exception import annotate_exception
63from .modelinfo import expand_pars
64
65try:
66    from typing import List, Iterator, Callable
67except ImportError:
68    pass
69else:
70    from .modelinfo import ParameterTable, ParameterSet, TestCondition, ModelInfo
71    from .kernel import KernelModel
72
73
74def make_suite(loaders, models):
75    # type: (List[str], List[str]) -> unittest.TestSuite
76    """
77    Construct the pyunit test suite.
78
79    *loaders* is the list of kernel drivers to use, which is one of
80    *["dll", "opencl"]*, *["dll"]* or *["opencl"]*.  For python models,
81    the python driver is always used.
82
83    *models* is the list of models to test, or *["all"]* to test all models.
84    """
85    ModelTestCase = _hide_model_case_from_nose()
86    suite = unittest.TestSuite()
87
88    if models[0] == 'all':
89        skip = models[1:]
90        models = list_models()
91    else:
92        skip = []
93    for model_name in models:
94        if model_name in skip:
95            continue
96        model_info = load_model_info(model_name)
97
98        #print('------')
99        #print('found tests in', model_name)
100        #print('------')
101
102        # if ispy then use the dll loader to call pykernel
103        # don't try to call cl kernel since it will not be
104        # available in some environmentes.
105        is_py = callable(model_info.Iq)
106
107        # Some OpenCL drivers seem to be flaky, and are not producing the
108        # expected result.  Since we don't have known test values yet for
109        # all of our models, we are instead going to compare the results
110        # for the 'smoke test' (that is, evaluation at q=0.1 for the default
111        # parameters just to see that the model runs to completion) between
112        # the OpenCL and the DLL.  To do this, we define a 'stash' which is
113        # shared between OpenCL and DLL tests.  This is just a list.  If the
114        # list is empty (which it will be when DLL runs, if the DLL runs
115        # first), then the results are appended to the list.  If the list
116        # is not empty (which it will be when OpenCL runs second), the results
117        # are compared to the results stored in the first element of the list.
118        # This is a horrible stateful hack which only makes sense because the
119        # test suite is thrown away after being run once.
120        stash = []
121
122        if is_py:  # kernel implemented in python
123            test_name = "Model: %s, Kernel: python"%model_name
124            test_method_name = "test_%s_python" % model_info.id
125            test = ModelTestCase(test_name, model_info,
126                                 test_method_name,
127                                 platform="dll",  # so that
128                                 dtype="double",
129                                 stash=stash)
130            suite.addTest(test)
131        else:   # kernel implemented in C
132
133            # test using dll if desired
134            if 'dll' in loaders or not core.HAVE_OPENCL:
135                test_name = "Model: %s, Kernel: dll"%model_name
136                test_method_name = "test_%s_dll" % model_info.id
137                test = ModelTestCase(test_name, model_info,
138                                     test_method_name,
139                                     platform="dll",
140                                     dtype="double",
141                                     stash=stash)
142                suite.addTest(test)
143
144            # test using opencl if desired and available
145            if 'opencl' in loaders and core.HAVE_OPENCL:
146                test_name = "Model: %s, Kernel: OpenCL"%model_name
147                test_method_name = "test_%s_opencl" % model_info.id
148                # Using dtype=None so that the models that are only
149                # correct for double precision are not tested using
150                # single precision.  The choice is determined by the
151                # presence of *single=False* in the model file.
152                test = ModelTestCase(test_name, model_info,
153                                     test_method_name,
154                                     platform="ocl", dtype=None,
155                                     stash=stash)
156                #print("defining", test_name)
157                suite.addTest(test)
158
159    return suite
160
161
162def _hide_model_case_from_nose():
163    # type: () -> type
164    class ModelTestCase(unittest.TestCase):
165        """
166        Test suit for a particular model with a particular kernel driver.
167
168        The test suite runs a simple smoke test to make sure the model
169        functions, then runs the list of tests at the bottom of the model
170        description file.
171        """
172        def __init__(self, test_name, model_info, test_method_name,
173                     platform, dtype, stash):
174            # type: (str, ModelInfo, str, str, DType, List[Any]) -> None
175            self.test_name = test_name
176            self.info = model_info
177            self.platform = platform
178            self.dtype = dtype
179            self.stash = stash  # container for the results of the first run
180
181            setattr(self, test_method_name, self.run_all)
182            unittest.TestCase.__init__(self, test_method_name)
183
184        def run_all(self):
185            # type: () -> None
186            """
187            Run all the tests in the test suite, including smoke tests.
188            """
189            smoke_tests = [
190                # test validity at reasonable values
191                ({}, 0.1, None),
192                ({}, (0.1, 0.1), None),
193                # test validity at q = 0
194                #({}, 0.0, None),
195                #({}, (0.0, 0.0), None),
196                # test vector form
197                ({}, [0.001, 0.01, 0.1], [None]*3),
198                ({}, [(0.1, 0.1)]*2, [None]*2),
199                # test that ER/VR will run if they exist
200                ({}, 'ER', None),
201                ({}, 'VR', None),
202                ]
203            tests = smoke_tests
204            if self.info.tests is not None:
205                tests += self.info.tests
206            try:
207                model = build_model(self.info, dtype=self.dtype,
208                                    platform=self.platform)
209                results = [self.run_one(model, test) for test in tests]
210                if self.stash:
211                    for test, target, actual in zip(tests, self.stash[0], results):
212                        assert np.all(abs(target-actual) < 5e-5*abs(actual)),\
213                            "GPU/CPU comparison expected %s but got %s for %s"%(target, actual, test[0])
214                else:
215                    self.stash.append(results)
216
217                # Check for missing tests.  Only do so for the "dll" tests
218                # to reduce noise from both opencl and dll, and because
219                # python kernels use platform="dll".
220                if self.platform == "dll":
221                    missing = []
222                    ## Uncomment the following to require test cases
223                    #missing = self._find_missing_tests()
224                    if missing:
225                        raise ValueError("Missing tests for "+", ".join(missing))
226
227            except:
228                annotate_exception(self.test_name)
229                raise
230
231        def _find_missing_tests(self):
232            # type: () -> None
233            """make sure there are 1D, 2D, ER and VR tests as appropriate"""
234            model_has_VR = callable(self.info.VR)
235            model_has_ER = callable(self.info.ER)
236            model_has_1D = True
237            model_has_2D = any(p.type == 'orientation'
238                               for p in self.info.parameters.kernel_parameters)
239
240            # Lists of tests that have a result that is not None
241            single = [test for test in self.info.tests
242                      if not isinstance(test[2], list) and test[2] is not None]
243            tests_has_VR = any(test[1] == 'VR' for test in single)
244            tests_has_ER = any(test[1] == 'ER' for test in single)
245            tests_has_1D_single = any(isinstance(test[1], float) for test in single)
246            tests_has_2D_single = any(isinstance(test[1], tuple) for test in single)
247
248            multiple = [test for test in self.info.tests
249                        if isinstance(test[2], list)
250                        and not all(result is None for result in test[2])]
251            tests_has_1D_multiple = any(isinstance(test[1][0], float)
252                                        for test in multiple)
253            tests_has_2D_multiple = any(isinstance(test[1][0], tuple)
254                                        for test in multiple)
255
256            missing = []
257            if model_has_VR and not tests_has_VR:
258                missing.append("VR")
259            if model_has_ER and not tests_has_ER:
260                missing.append("ER")
261            if model_has_1D and not (tests_has_1D_single or tests_has_1D_multiple):
262                missing.append("1D")
263            if model_has_2D and not (tests_has_2D_single or tests_has_2D_multiple):
264                missing.append("2D")
265
266            return missing
267
268        def run_one(self, model, test):
269            # type: (KernelModel, TestCondition) -> None
270            """Run a single test case."""
271            user_pars, x, y = test
272            pars = expand_pars(self.info.parameters, user_pars)
273            invalid = invalid_pars(self.info.parameters, pars)
274            if invalid:
275                raise ValueError("Unknown parameters in test: " + ", ".join(invalid))
276
277            if not isinstance(y, list):
278                y = [y]
279            if not isinstance(x, list):
280                x = [x]
281
282            self.assertEqual(len(y), len(x))
283
284            if x[0] == 'ER':
285                actual = np.array([call_ER(model.info, pars)])
286            elif x[0] == 'VR':
287                actual = np.array([call_VR(model.info, pars)])
288            elif isinstance(x[0], tuple):
289                qx, qy = zip(*x)
290                q_vectors = [np.array(qx), np.array(qy)]
291                kernel = model.make_kernel(q_vectors)
292                actual = call_kernel(kernel, pars)
293            else:
294                q_vectors = [np.array(x)]
295                kernel = model.make_kernel(q_vectors)
296                actual = call_kernel(kernel, pars)
297
298            self.assertTrue(len(actual) > 0)
299            self.assertEqual(len(y), len(actual))
300
301            for xi, yi, actual_yi in zip(x, y, actual):
302                if yi is None:
303                    # smoke test --- make sure it runs and produces a value
304                    self.assertTrue(not np.isnan(actual_yi),
305                                    'invalid f(%s): %s' % (xi, actual_yi))
306                elif np.isnan(yi):
307                    self.assertTrue(np.isnan(actual_yi),
308                                    'f(%s): expected:%s; actual:%s'
309                                    % (xi, yi, actual_yi))
310                else:
311                    # is_near does not work for infinite values, so also test
312                    # for exact values.  Note that this will not
313                    self.assertTrue(yi == actual_yi or is_near(yi, actual_yi, 5),
314                                    'f(%s); expected:%s; actual:%s'
315                                    % (xi, yi, actual_yi))
316            return actual
317
318    return ModelTestCase
319
320def invalid_pars(partable, pars):
321    # type: (ParameterTable, Dict[str, float])
322    """
323    Return a list of parameter names that are not part of the model.
324    """
325    names = set(p.id for p in partable.call_parameters)
326    invalid = []
327    for par in sorted(pars.keys()):
328        parts = par.split('_pd')
329        if len(parts) > 1 and parts[1] not in ("", "_n", "nsigma", "type"):
330            invalid.append(par)
331            continue
332        if parts[0] not in names:
333            invalid.append(par)
334    return invalid
335
336
337def is_near(target, actual, digits=5):
338    # type: (float, float, int) -> bool
339    """
340    Returns true if *actual* is within *digits* significant digits of *target*.
341    """
342    import math
343    shift = 10**math.ceil(math.log10(abs(target)))
344    return abs(target-actual)/shift < 1.5*10**-digits
345
346def run_one(model):
347    # type: (str) -> str
348    """
349    Run the tests for a single model, printing the results to stdout.
350
351    *model* can by a python file, which is handy for checking user defined
352    plugin models.
353    """
354    # Note that running main() directly did not work from within the
355    # wxPython pycrust console.  Instead of the results appearing in the
356    # window they were printed to the underlying console.
357    from unittest.runner import TextTestResult, _WritelnDecorator
358
359    # Build a object to capture and print the test results
360    stream = _WritelnDecorator(StringIO())  # Add writeln() method to stream
361    verbosity = 2
362    descriptions = True
363    result = TextTestResult(stream, descriptions, verbosity)
364
365    # Build a test suite containing just the model
366    loaders = ['opencl'] if core.HAVE_OPENCL else ['dll']
367    models = [model]
368    try:
369        suite = make_suite(loaders, models)
370    except Exception:
371        import traceback
372        stream.writeln(traceback.format_exc())
373        return
374    # Run the test suite
375    suite.run(result)
376
377    # Print the failures and errors
378    for _, tb in result.errors:
379        stream.writeln(tb)
380    for _, tb in result.failures:
381        stream.writeln(tb)
382
383    # Warn if there are no user defined tests.
384    # Note: the test suite constructed above only has one test in it, which
385    # runs through some smoke tests to make sure the model runs, then runs
386    # through the input-output pairs given in the model definition file.  To
387    # check if any such pairs are defined, therefore, we just need to check if
388    # they are in the first test of the test suite.  We do this with an
389    # iterator since we don't have direct access to the list of tests in the
390    # test suite.
391    for test in suite:
392        if not test.info.tests:
393            stream.writeln("Note: %s has no user defined tests."%model)
394        break
395    else:
396        stream.writeln("Note: no test suite created --- this should never happen")
397
398    output = stream.getvalue()
399    stream.close()
400    return output
401
402
403def main(*models):
404    # type: (*str) -> int
405    """
406    Run tests given is models.
407
408    Returns 0 if success or 1 if any tests fail.
409    """
410    try:
411        from xmlrunner import XMLTestRunner as TestRunner
412        test_args = {'output': 'logs'}
413    except ImportError:
414        from unittest import TextTestRunner as TestRunner
415        test_args = {}
416
417    if models and models[0] == '-v':
418        verbosity = 2
419        models = models[1:]
420    else:
421        verbosity = 1
422    if models and models[0] == 'opencl':
423        if not core.HAVE_OPENCL:
424            print("opencl is not available")
425            return 1
426        loaders = ['opencl']
427        models = models[1:]
428    elif models and models[0] == 'dll':
429        # TODO: test if compiler is available?
430        loaders = ['dll']
431        models = models[1:]
432    elif models and models[0] == 'opencl_and_dll':
433        loaders = ['opencl', 'dll'] if core.HAVE_OPENCL else ['dll']
434        models = models[1:]
435    else:
436        loaders = ['opencl', 'dll'] if core.HAVE_OPENCL else ['dll']
437    if not models:
438        print("""\
439usage:
440  python -m sasmodels.model_test [-v] [opencl|dll] model1 model2 ...
441
442If -v is included on the command line, then use verbose output.
443
444If neither opencl nor dll is specified, then models will be tested with
445both OpenCL and dll; the compute target is ignored for pure python models.
446
447If model1 is 'all', then all except the remaining models will be tested.
448
449""")
450
451        return 1
452
453    runner = TestRunner(verbosity=verbosity, **test_args)
454    result = runner.run(make_suite(loaders, models))
455    return 1 if result.failures or result.errors else 0
456
457
458def model_tests():
459    # type: () -> Iterator[Callable[[], None]]
460    """
461    Test runner visible to nosetests.
462
463    Run "nosetests sasmodels" on the command line to invoke it.
464    """
465    loaders = ['opencl', 'dll'] if core.HAVE_OPENCL else ['dll']
466    tests = make_suite(loaders, ['all'])
467    for test_i in tests:
468        # In order for nosetest to see the correct test name, need to set
469        # the description attribute of the returned function.  Since we
470        # can't do this for the returned instance, wrap it in a lambda and
471        # set the description on the lambda.  Otherwise we could just do:
472        #    yield test_i.run_all
473        L = lambda: test_i.run_all()
474        L.description = test_i.test_name
475        yield L
476
477
478if __name__ == "__main__":
479    sys.exit(main(*sys.argv[1:]))
Note: See TracBrowser for help on using the repository browser.