Changeset aa8c6e0 in sasmodels for sasmodels/model_test.py
- Timestamp:
- Oct 30, 2018 8:43:35 AM (5 years ago)
- Branches:
- master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 765d025, 23df833
- Parents:
- 153f8f6 (diff), c6084f1 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/model_test.py
r81751c2 raa8c6e0 47 47 import sys 48 48 import unittest 49 import traceback 49 50 50 51 try: … … 76 77 # pylint: enable=unused-import 77 78 78 79 79 def make_suite(loaders, models): 80 80 # type: (List[str], List[str]) -> unittest.TestSuite … … 87 87 *models* is the list of models to test, or *["all"]* to test all models. 88 88 """ 89 ModelTestCase = _hide_model_case_from_nose()90 89 suite = unittest.TestSuite() 91 90 … … 96 95 skip = [] 97 96 for model_name in models: 98 if model_name in skip: 99 continue 100 model_info = load_model_info(model_name) 101 102 #print('------') 103 #print('found tests in', model_name) 104 #print('------') 105 106 # if ispy then use the dll loader to call pykernel 107 # don't try to call cl kernel since it will not be 108 # available in some environmentes. 109 is_py = callable(model_info.Iq) 110 111 # Some OpenCL drivers seem to be flaky, and are not producing the 112 # expected result. Since we don't have known test values yet for 113 # all of our models, we are instead going to compare the results 114 # for the 'smoke test' (that is, evaluation at q=0.1 for the default 115 # parameters just to see that the model runs to completion) between 116 # the OpenCL and the DLL. To do this, we define a 'stash' which is 117 # shared between OpenCL and DLL tests. This is just a list. If the 118 # list is empty (which it will be when DLL runs, if the DLL runs 119 # first), then the results are appended to the list. If the list 120 # is not empty (which it will be when OpenCL runs second), the results 121 # are compared to the results stored in the first element of the list. 122 # This is a horrible stateful hack which only makes sense because the 123 # test suite is thrown away after being run once. 124 stash = [] 125 126 if is_py: # kernel implemented in python 127 test_name = "%s-python"%model_name 128 test_method_name = "test_%s_python" % model_info.id 97 if model_name not in skip: 98 model_info = load_model_info(model_name) 99 _add_model_to_suite(loaders, suite, model_info) 100 101 return suite 102 103 def _add_model_to_suite(loaders, suite, model_info): 104 ModelTestCase = _hide_model_case_from_nose() 105 106 #print('------') 107 #print('found tests in', model_name) 108 #print('------') 109 110 # if ispy then use the dll loader to call pykernel 111 # don't try to call cl kernel since it will not be 112 # available in some environmentes. 113 is_py = callable(model_info.Iq) 114 115 # Some OpenCL drivers seem to be flaky, and are not producing the 116 # expected result. Since we don't have known test values yet for 117 # all of our models, we are instead going to compare the results 118 # for the 'smoke test' (that is, evaluation at q=0.1 for the default 119 # parameters just to see that the model runs to completion) between 120 # the OpenCL and the DLL. To do this, we define a 'stash' which is 121 # shared between OpenCL and DLL tests. This is just a list. If the 122 # list is empty (which it will be when DLL runs, if the DLL runs 123 # first), then the results are appended to the list. If the list 124 # is not empty (which it will be when OpenCL runs second), the results 125 # are compared to the results stored in the first element of the list. 126 # This is a horrible stateful hack which only makes sense because the 127 # test suite is thrown away after being run once. 128 stash = [] 129 130 if is_py: # kernel implemented in python 131 test_name = "%s-python"%model_info.name 132 test_method_name = "test_%s_python" % model_info.id 133 test = ModelTestCase(test_name, model_info, 134 test_method_name, 135 platform="dll", # so that 136 dtype="double", 137 stash=stash) 138 suite.addTest(test) 139 else: # kernel implemented in C 140 141 # test using dll if desired 142 if 'dll' in loaders or not use_opencl(): 143 test_name = "%s-dll"%model_info.name 144 test_method_name = "test_%s_dll" % model_info.id 129 145 test = ModelTestCase(test_name, model_info, 130 test_method_name,131 platform="dll", # so that132 dtype="double",133 stash=stash)146 test_method_name, 147 platform="dll", 148 dtype="double", 149 stash=stash) 134 150 suite.addTest(test) 135 else: # kernel implemented in C 136 137 # test using dll if desired 138 if 'dll' in loaders: 139 test_name = "%s-dll"%model_name 140 test_method_name = "test_%s_dll" % model_info.id 141 test = ModelTestCase(test_name, model_info, 142 test_method_name, 143 platform="dll", 144 dtype="double", 145 stash=stash) 146 suite.addTest(test) 147 148 # test using opencl if desired and available 149 if 'opencl' in loaders and use_opencl(): 150 test_name = "%s-opencl"%model_name 151 test_method_name = "test_%s_opencl" % model_info.id 152 # Using dtype=None so that the models that are only 153 # correct for double precision are not tested using 154 # single precision. The choice is determined by the 155 # presence of *single=False* in the model file. 156 test = ModelTestCase(test_name, model_info, 157 test_method_name, 158 platform="ocl", dtype=None, 159 stash=stash) 160 #print("defining", test_name) 161 suite.addTest(test) 162 163 # test using cuda if desired and available 164 if 'cuda' in loaders and use_cuda(): 165 test_name = "%s-cuda"%model_name 166 test_method_name = "test_%s_cuda" % model_info.id 167 # Using dtype=None so that the models that are only 168 # correct for double precision are not tested using 169 # single precision. The choice is determined by the 170 # presence of *single=False* in the model file. 171 test = ModelTestCase(test_name, model_info, 172 test_method_name, 173 platform="cuda", dtype=None, 174 stash=stash) 175 #print("defining", test_name) 176 suite.addTest(test) 177 178 return suite 151 152 # test using opencl if desired and available 153 if 'opencl' in loaders and use_opencl(): 154 test_name = "%s-opencl"%model_info.name 155 test_method_name = "test_%s_opencl" % model_info.id 156 # Using dtype=None so that the models that are only 157 # correct for double precision are not tested using 158 # single precision. The choice is determined by the 159 # presence of *single=False* in the model file. 160 test = ModelTestCase(test_name, model_info, 161 test_method_name, 162 platform="ocl", dtype=None, 163 stash=stash) 164 #print("defining", test_name) 165 suite.addTest(test) 166 167 # test using cuda if desired and available 168 if 'cuda' in loaders and use_cuda(): 169 test_name = "%s-cuda"%model_name 170 test_method_name = "test_%s_cuda" % model_info.id 171 # Using dtype=None so that the models that are only 172 # correct for double precision are not tested using 173 # single precision. The choice is determined by the 174 # presence of *single=False* in the model file. 175 test = ModelTestCase(test_name, model_info, 176 test_method_name, 177 platform="cuda", dtype=None, 178 stash=stash) 179 #print("defining", test_name) 180 suite.addTest(test) 181 179 182 180 183 def _hide_model_case_from_nose(): … … 403 406 return abs(target-actual)/shift < 1.5*10**-digits 404 407 405 def run_one(model): 406 # type: (str) -> str 407 """ 408 Run the tests for a single model, printing the results to stdout. 409 410 *model* can by a python file, which is handy for checking user defined 411 plugin models. 408 # CRUFT: old interface; should be deprecated and removed 409 def run_one(model_name): 410 # msg = "use check_model(model_info) rather than run_one(model_name)" 411 # warnings.warn(msg, category=DeprecationWarning, stacklevel=2) 412 try: 413 model_info = load_model_info(model_name) 414 except Exception: 415 output = traceback.format_exc() 416 return output 417 418 success, output = check_model(model_info) 419 return output 420 421 def check_model(model_info): 422 # type: (ModelInfo) -> str 423 """ 424 Run the tests for a single model, capturing the output. 425 426 Returns success status and the output string. 412 427 """ 413 428 # Note that running main() directly did not work from within the … … 423 438 424 439 # Build a test suite containing just the model 425 loader = 'opencl' if use_opencl() else 'cuda' if use_cuda() else 'dll' 426 models = [model] 427 try: 428 suite = make_suite([loader], models) 429 except Exception: 430 import traceback 431 stream.writeln(traceback.format_exc()) 432 return 440 loaders = ['opencl' if use_opencl() else 'cuda' if use_cuda() else 'dll'] 441 suite = unittest.TestSuite() 442 _add_model_to_suite(loaders, suite, model_info) 433 443 434 444 # Warn if there are no user defined tests. … … 445 455 for test in suite: 446 456 if not test.info.tests: 447 stream.writeln("Note: %s has no user defined tests."%model )457 stream.writeln("Note: %s has no user defined tests."%model_info.name) 448 458 break 449 459 else: … … 461 471 output = stream.getvalue() 462 472 stream.close() 463 return output473 return result.wasSuccessful(), output 464 474 465 475
Note: See TracChangeset
for help on using the changeset viewer.