Changeset cd3dba0 in sasmodels for sasmodels/compare.py


Ignore:
Timestamp:
Nov 23, 2015 7:14:54 PM (7 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:
3a45c2c
Parents:
0e9048f
Message:

improve compare.py so that parameters can be constrained to valid values

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/compare.py

    r73a3e22 rcd3dba0  
    1616from . import core 
    1717from . import kerneldll 
    18 from . import models 
     18from . import generate 
    1919from .data import plot_theory, empty_data1D, empty_data2D 
    2020from .direct_model import DirectModel 
     
    116116        return 2*np.random.rand()*(v if v != 0 else 1) 
    117117 
    118 def randomize_model(name, pars, seed=None): 
     118def randomize_model(pars, seed=None): 
    119119    if seed is None: 
    120120        seed = np.random.randint(1e9) 
     
    122122    # Note: the sort guarantees order of calls to random number generator 
    123123    pars = dict((p,randomize(p,v)) for p,v in sorted(pars.items())) 
    124     # The capped cylinder model has a constraint on its parameters 
     124 
     125    return pars, seed 
     126 
     127def constrain_pars(model_definition, pars): 
     128    name = model_definition.name 
    125129    if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']: 
    126130        pars['radius'],pars['cap_radius'] = pars['cap_radius'],pars['radius'] 
    127     return pars, seed 
     131 
     132    # These constraints are only needed for comparison to sasview 
     133    if name in ('teubner_strey','broad_peak'): 
     134        del pars['scale'] 
     135    if name in ('guinier',): 
     136        del pars['background'] 
     137    if getattr(model_definition, 'category', None) == 'structure-factor': 
     138        del pars['scale'], pars['background'] 
     139 
    128140 
    129141def parlist(pars): 
     
    209221 
    210222def compare(name, pars, Ncpu, Nocl, opts, set_pars): 
     223    model_definition = core.load_model_definition(name) 
     224 
    211225    view = 'linear' if '-linear' in opts else 'log' if '-log' in opts else 'q4' if '-q4' in opts else 'log' 
    212226 
     
    231245    if '-random' in opts or '-random' in opt_values: 
    232246        seed = int(opt_values['-random']) if '-random' in opt_values else None 
    233         pars, seed = randomize_model(name, pars, seed=seed) 
     247        pars, seed = randomize_model(pars, seed=seed) 
     248        constrain_pars(model_definition, pars) 
    234249        print "Randomize using -random=%i"%seed 
    235250    pars.update(set_pars)  # set value after random to control value 
     
    241256        print "pars",parlist(pars) 
    242257 
    243     model_definition = core.load_model_definition(name) 
    244258    # OpenCl calculation 
    245259    if Nocl > 0: 
     
    247261                                    dtype=dtype, cutoff=cutoff, Nevals=Nocl) 
    248262        print "opencl t=%.1f ms, intensity=%.0f"%(ocl_time, sum(ocl)) 
     263        #print "ocl", ocl 
    249264        #print max(ocl), min(ocl) 
    250265 
     
    261276            #print "ocl/sasview", (ocl-pars['background'])/(cpu-pars['background']) 
    262277            print "sasview t=%.1f ms, intensity=%.0f"%(cpu_time, sum(cpu)) 
     278            #print "sasview",cpu 
    263279        except ImportError: 
    264280            Ncpu = 0 
     
    396412 
    397413 
    398 def get_demo_pars(name): 
    399     __import__('sasmodels.models.'+name) 
    400     model = getattr(models, name) 
    401     pars = getattr(model, 'demo', None) 
    402     if pars is None: pars = dict((p[0],p[2]) for p in model.parameters) 
     414def get_demo_pars(model_definition): 
     415    info = generate.make_info(model_definition) 
     416    pars = dict((p[0],p[2]) for p in info['parameters']) 
     417    pars.update(info['demo']) 
    403418    return pars 
    404419 
     
    425440    # if model does not define demo parameters 
    426441    name = args[0] 
    427     pars = get_demo_pars(name) 
     442    model_definition = core.load_model_definition(name) 
     443    pars = get_demo_pars(model_definition) 
    428444 
    429445    Nopencl = int(args[1]) if len(args) > 1 else 5 
Note: See TracChangeset for help on using the changeset viewer.