Changeset 9890053 in sasmodels


Ignore:
Timestamp:
Mar 3, 2015 4:31:38 PM (9 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
59c4c4e
Parents:
49f92c1
Message:

add smoke tests for ER/VR; check that smoke test results are valid floats

Location:
sasmodels
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/bumps_model.py

    r5134b2c r9890053  
    437437    data = load_data('DEC07086.DAT') 
    438438    set_beam_stop(data, 0.004) 
    439     plot_data(data) 
     439    plot_data(data, data.data) 
    440440    import matplotlib.pyplot as plt; plt.show() 
    441441 
  • sasmodels/core.py

    r84e01d2 r9890053  
    5353    return v,w/np.sum(w) 
    5454 
     55def dispersion_mesh(pars): 
     56    """ 
     57    Create a mesh grid of dispersion parameters and weights. 
     58 
     59    Returns [p1,p2,...],w where pj is a vector of values for parameter j 
     60    and w is a vector containing the products for weights for each 
     61    parameter set in the vector. 
     62    """ 
     63    values, weights = zip(*pars) 
     64    if len(values) > 1: 
     65        values = [v.flatten() for v in np.meshgrid(*values)] 
     66        weights = np.vstack([v.flatten() for v in np.meshgrid(*weights)]) 
     67        weights = np.prod(weights, axis=0) 
     68    return values, weights 
     69 
    5570def call_kernel(kernel, pars, cutoff=1e-5): 
    5671    fixed_pars = [pars.get(name, kernel.info['defaults'][name]) 
     
    5974    return kernel(fixed_pars, pd_pars, cutoff=cutoff) 
    6075 
     76def call_ER(kernel, pars): 
     77    ER = kernel.info.get('ER', None) 
     78    if ER is None: 
     79        return 1.0 
     80    else: 
     81        vol_pars = [get_weights(kernel, pars, name) 
     82                    for name in kernel.info['partype']['volume']] 
     83        values, weights = dispersion_mesh(vol_pars) 
     84        fv = ER(*values) 
     85        #print values[0].shape, weights.shape, fv.shape 
     86        return np.sum(weights*fv) / np.sum(weights) 
     87 
     88def call_VR(kernel, pars): 
     89    VR = kernel.info.get('VR', None) 
     90    if VR is None: 
     91        return 1.0 
     92    else: 
     93        vol_pars = [get_weights(kernel, pars, name) 
     94                    for name in kernel.info['partype']['volume']] 
     95        values, weights = dispersion_mesh(vol_pars) 
     96        whole,part = VR(*values) 
     97        return np.sum(weights*part)/np.sum(weights*whole) 
     98 
  • sasmodels/model_test.py

    r84e01d2 r9890053  
    11# -*- coding: utf-8 -*- 
    22""" 
    3 Created on Tue Feb 17 11:43:56 2015 
    4  
    5 @author: David 
     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), ...], [I(qx1,qy1), I(qx2,qy2), ...], 
     32 
     33        [ {parameters}, 'ER', ER(pars) ], 
     34        [ {parameters}, 'VR', VR(pars) ], 
     35        ... 
     36    ] 
     37 
     38Parameters are *key:value* pairs, where key is one of the parameters of the 
     39model and value is the value to use for the test.  Any parameters not given 
     40in the parameter list will take on the default parameter value. 
     41 
     42Precision defaults to 5 digits (relative). 
    643""" 
    744 
     
    1350from .core import list_models, load_model_definition 
    1451from .core import load_model_cl, load_model_dll 
    15 from .core import make_kernel, call_kernel 
     52from .core import make_kernel, call_kernel, call_ER, call_VR 
    1653 
    1754def annotate_exception(exc, msg): 
     
    5491        model_definition = load_model_definition(model_name) 
    5592 
    56         smoke_tests = [[{},0.1,None],[{},(0.1,0.1),None]] 
     93        smoke_tests = [ 
     94            [{},0.1,None], 
     95            [{},(0.1,0.1),None], 
     96            [{},'ER',None], 
     97            [{},'VR',None], 
     98            ] 
    5799        tests = smoke_tests + getattr(model_definition, 'tests', []) 
    58100         
     
    102144                pars, Q, I = test 
    103145 
     146                if not isinstance(I, list): 
     147                    I = [I] 
    104148                if not isinstance(Q, list): 
    105149                    Q = [Q] 
    106                 if not isinstance(I, list): 
    107                     I = [I] 
    108                      
    109                 if isinstance(Q[0], tuple): 
     150 
     151                self.assertEqual(len(I), len(Q)) 
     152 
     153                if Q[0] == 'ER': 
     154                    Iq = [call_ER(kernel, pars)] 
     155                elif Q[0] == 'VR': 
     156                    Iq = [call_VR(kernel, pars)] 
     157                elif isinstance(Q[0], tuple): 
    110158                    Qx,Qy = zip(*Q) 
    111159                    Q_vectors = [np.array(Qx), np.array(Qy)] 
     160                    kernel = make_kernel(model, Q_vectors) 
     161                    Iq = call_kernel(kernel, pars) 
    112162                else: 
    113163                    Q_vectors = [np.array(Q)] 
    114  
    115                 self.assertEqual(len(I), len(Q)) 
    116  
    117                 kernel = make_kernel(model, Q_vectors) 
    118                 Iq = call_kernel(kernel, pars) 
     164                    kernel = make_kernel(model, Q_vectors) 
     165                    Iq = call_kernel(kernel, pars) 
    119166             
    120167                self.assertGreater(len(Iq), 0)     
     
    122169                 
    123170                for q, i, iq in zip(Q, I, Iq): 
    124                     if i is None: continue # smoke test --- make sure it runs 
    125                     err = abs(i - iq) 
    126                     nrm = abs(i) 
    127              
    128                     self.assertLess(err * 10**5, nrm, 'q:%s; expected:%s; actual:%s' % (q, i, iq)) 
     171                    if i is None: 
     172                        # smoke test --- make sure it runs and produces a value 
     173                        self.assertTrue(np.isfinite(iq), 'q:%s; not finite; actual:%s' % (q, iq)) 
     174                    else: 
     175                        err = abs(i - iq) 
     176                        nrm = abs(i) 
     177                        self.assertLess(err * 10**5, nrm, 'q:%s; expected:%s; actual:%s' % (q, i, iq)) 
    129178                     
    130179        except Exception,exc:  
  • sasmodels/models/barbell.py

    ra5d0d00 r9890053  
    127127source = [ "lib/J1.c", "lib/gauss76.c", "barbell.c" ] 
    128128 
    129 def ER(radius, length): 
    130     return 1.0 
    131  
    132129# parameters for demo 
    133130demo = dict( 
  • sasmodels/models/bcc.py

    r6272968 r9890053  
    128128source = [ "lib/J1.c", "lib/gauss150.c", "bcc.c" ] 
    129129 
    130 def ER(radius, length): 
    131     return 0 
    132  
    133130# parameters for demo 
    134131demo = dict( 
  • sasmodels/models/cylinder.py

    ra5d0d00 r9890053  
    172172 
    173173 
    174 # test values: 
    175 # [ 
    176 #   [ {parameters}, q, I(q)], 
    177 #   [ {parameters}, [q], [I(q)] ], 
    178 #   [ {parameters}, [q1, q2, ...], [I(q1), I(q2), ...]], 
    179  
    180 #   [ {parameters}, (qx, qy), I(qx, Iqy)], 
    181 #   [ {parameters}, [(qx1, qy1), (qx2, qy2), ...], [I(qx1,qy1), I(qx2,qy2), ...], 
    182 #   ... 
    183 # ] 
    184 # Precision defaults to 7 digits (relative) for single, 14 for double 
    185 # May want a limited precision version that checks for 8-n or 15-n digits respectively 
    186174qx,qy = 0.2*np.cos(2.5), 0.2*np.sin(2.5) 
    187175tests = [ 
  • sasmodels/models/fcc.py

    ra5d0d00 r9890053  
    103103source = [ "lib/J1.c", "lib/gauss150.c", "fcc.c" ] 
    104104 
    105 def ER(radius, length): 
    106     return 0 
    107  
    108105# parameters for demo 
    109106demo = dict( 
  • sasmodels/models/parallelepiped.py

    ra5d0d00 r9890053  
    101101""" 
    102102 
    103 from numpy import pi, inf 
     103from numpy import pi, inf, sqrt 
    104104 
    105105name = "parallelepiped" 
Note: See TracChangeset for help on using the changeset viewer.