source: sasmodels/sasmodels/model_test.py @ 17bbadd

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 17bbadd was 17bbadd, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

refactor so all model defintion queries use model_info; better documentation of model_info structure; initial implementation of product model (broken)

  • Property mode set to 100644
File size: 9.6 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
50import numpy as np
51
52from .core import list_models, load_model_info, build_model, HAVE_OPENCL
53from .core import make_kernel, call_kernel, call_ER, call_VR
54from .exception import annotate_exception
55
56#TODO: rename to tests so that tab completion works better for models directory
57
58def make_suite(loaders, models):
59    """
60    Construct the pyunit test suite.
61
62    *loaders* is the list of kernel drivers to use, which is one of
63    *["dll", "opencl"]*, *["dll"]* or *["opencl"]*.  For python models,
64    the python driver is always used.
65
66    *models* is the list of models to test, or *["all"]* to test all models.
67    """
68
69    ModelTestCase = _hide_model_case_from_nosetests()
70    suite = unittest.TestSuite()
71
72    if models[0] == 'all':
73        skip = models[1:]
74        models = list_models()
75    else:
76        skip = []
77    for model_name in models:
78        if model_name in skip: continue
79        model_info = load_model_info(model_name)
80
81        #print('------')
82        #print('found tests in', model_name)
83        #print('------')
84
85        # if ispy then use the dll loader to call pykernel
86        # don't try to call cl kernel since it will not be
87        # available in some environmentes.
88        is_py = callable(model_info['Iq'])
89
90        if is_py:  # kernel implemented in python
91            test_name = "Model: %s, Kernel: python"%model_name
92            test_method_name = "test_%s_python" % model_name
93            test = ModelTestCase(test_name, model_info,
94                                 test_method_name,
95                                 platform="dll",  # so that
96                                 dtype="double")
97            suite.addTest(test)
98        else:   # kernel implemented in C
99            # test using opencl if desired and available
100            if 'opencl' in loaders and HAVE_OPENCL:
101                test_name = "Model: %s, Kernel: OpenCL"%model_name
102                test_method_name = "test_%s_opencl" % model_name
103                # Using dtype=None so that the models that are only
104                # correct for double precision are not tested using
105                # single precision.  The choice is determined by the
106                # presence of *single=False* in the model file.
107                test = ModelTestCase(test_name, model_info,
108                                     test_method_name,
109                                     platform="ocl", dtype=None)
110                #print("defining", test_name)
111                suite.addTest(test)
112
113            # test using dll if desired
114            if 'dll' in loaders:
115                test_name = "Model: %s, Kernel: dll"%model_name
116                test_method_name = "test_%s_dll" % model_name
117                test = ModelTestCase(test_name, model_info,
118                                     test_method_name,
119                                     platform="dll",
120                                     dtype="double")
121                suite.addTest(test)
122
123    return suite
124
125
126def _hide_model_case_from_nosetests():
127    class ModelTestCase(unittest.TestCase):
128        """
129        Test suit for a particular model with a particular kernel driver.
130
131        The test suite runs a simple smoke test to make sure the model
132        functions, then runs the list of tests at the bottom of the model
133        description file.
134        """
135        def __init__(self, test_name, model_info, test_method_name,
136                     platform, dtype):
137            self.test_name = test_name
138            self.info = model_info
139            self.platform = platform
140            self.dtype = dtype
141
142            setattr(self, test_method_name, self._runTest)
143            unittest.TestCase.__init__(self, test_method_name)
144
145        def _runTest(self):
146            smoke_tests = [
147                [{}, 0.1, None],
148                [{}, (0.1, 0.1), None],
149                [{}, 'ER', None],
150                [{}, 'VR', None],
151                ]
152
153            tests = self.info['tests']
154            try:
155                model = build_model(self.info, dtype=self.dtype,
156                                    platform=self.platform)
157                for test in smoke_tests + tests:
158                    self._run_one_test(model, test)
159
160                if not tests and self.platform == "dll":
161                    ## Uncomment the following to make forgetting the test
162                    ## values an error.  Only do so for the "dll" tests
163                    ## to reduce noise from both opencl and dll, and because
164                    ## python kernels use platform="dll".
165                    #raise Exception("No test cases provided")
166                    pass
167
168            except Exception as exc:
169                annotate_exception(exc, self.test_name)
170                raise
171
172        def _run_one_test(self, model, test):
173            pars, x, y = test
174
175            if not isinstance(y, list):
176                y = [y]
177            if not isinstance(x, list):
178                x = [x]
179
180            self.assertEqual(len(y), len(x))
181
182            if x[0] == 'ER':
183                actual = [call_ER(model.info, pars)]
184            elif x[0] == 'VR':
185                actual = [call_VR(model.info, pars)]
186            elif isinstance(x[0], tuple):
187                Qx, Qy = zip(*x)
188                q_vectors = [np.array(Qx), np.array(Qy)]
189                kernel = make_kernel(model, q_vectors)
190                actual = call_kernel(kernel, pars)
191            else:
192                q_vectors = [np.array(x)]
193                kernel = make_kernel(model, q_vectors)
194                actual = call_kernel(kernel, pars)
195
196            self.assertGreater(len(actual), 0)
197            self.assertEqual(len(y), len(actual))
198
199            for xi, yi, actual_yi in zip(x, y, actual):
200                if yi is None:
201                    # smoke test --- make sure it runs and produces a value
202                    self.assertTrue(np.isfinite(actual_yi),
203                                    'invalid f(%s): %s' % (xi, actual_yi))
204                else:
205                    self.assertTrue(is_near(yi, actual_yi, 5),
206                                    'f(%s); expected:%s; actual:%s'
207                                    % (xi, yi, actual_yi))
208
209    return ModelTestCase
210
211def is_near(target, actual, digits=5):
212    """
213    Returns true if *actual* is within *digits* significant digits of *target*.
214    """
215    import math
216    shift = 10**math.ceil(math.log10(abs(target)))
217    return abs(target-actual)/shift < 1.5*10**-digits
218
219def main():
220    """
221    Run tests given is sys.argv.
222
223    Returns 0 if success or 1 if any tests fail.
224    """
225    import xmlrunner
226
227    models = sys.argv[1:]
228    if models and models[0] == '-v':
229        verbosity = 2
230        models = models[1:]
231    else:
232        verbosity = 1
233    if models and models[0] == 'opencl':
234        if not HAVE_OPENCL:
235            print("opencl is not available")
236            return 1
237        loaders = ['opencl']
238        models = models[1:]
239    elif models and models[0] == 'dll':
240        # TODO: test if compiler is available?
241        loaders = ['dll']
242        models = models[1:]
243    elif models and models[0] == 'opencl_and_dll':
244        loaders = ['opencl', 'dll']
245        models = models[1:]
246    else:
247        loaders = ['opencl', 'dll']
248    if not models:
249        print("""\
250usage:
251  python -m sasmodels.model_test [-v] [opencl|dll] model1 model2 ...
252
253If -v is included on the
254If neither opencl nor dll is specified, then models will be tested with
255both opencl and dll; the compute target is ignored for pure python models.
256
257If model1 is 'all', then all except the remaining models will be tested.
258
259""")
260
261        return 1
262
263    #runner = unittest.TextTestRunner()
264    runner = xmlrunner.XMLTestRunner(output='logs', verbosity=verbosity)
265    result = runner.run(make_suite(loaders, models))
266    return 1 if result.failures or result.errors else 0
267
268
269def model_tests():
270    """
271    Test runner visible to nosetests.
272
273    Run "nosetests sasmodels" on the command line to invoke it.
274    """
275    tests = make_suite(['opencl', 'dll'], ['all'])
276    for test_i in tests:
277        yield test_i._runTest
278
279
280if __name__ == "__main__":
281    sys.exit(main())
Note: See TracBrowser for help on using the repository browser.