source: sasmodels/example/sesansfit.py @ bdd08df

core_shell_microgelscostrafo411magnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since bdd08df was 9217ef8, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

add data saving to sesansfit; nicer errors when parameters don't exist

  • Property mode set to 100644
File size: 3.0 KB
Line 
1from bumps.names import *
2from sasmodels import core, bumps_model, sesans
3from sas.sascalc.dataloader.loader import Loader
4
5def get_bumps_model(model_name):
6    kernel = core.load_model(model_name)
7    model = bumps_model.Model(kernel)
8    return model
9
10def sesans_fit(file, model, initial_vals={}, custom_params={}, param_range=[], acceptance_angle=None):
11    """
12
13    @param file: SESANS file location
14    @param model: Bumps model object or model name - can be model, model_1 * model_2, and/or model_1 + model_2
15    @param initial_vals: dictionary of {param_name : initial_value}
16    @param custom_params: dictionary of {custom_parameter_name : Parameter() object}
17    @param param_range: dictionary of {parameter_name : [minimum, maximum]}
18    @param constraints: dictionary of {parameter_name : constraint}
19    @return: FitProblem for Bumps usage
20    """
21    initial_vals['background'] = 0.0
22    try:
23        loader = Loader()
24        data = loader.load(file)
25        if data is None: raise IOError("Could not load file %r"%(file))
26
27    except:
28        # If no loadable data file, generate random data
29        SElength = np.linspace(0, 2400, 61) # [A]
30        data = np.ones_like(SElength)
31        err_data = np.ones_like(SElength)*0.03
32
33        class Sample:
34            zacceptance = 0.1 # [A^-1]
35            thickness = 0.2 # [cm]
36
37        class SESANSData1D:
38            #q_zmax = 0.23 # [A^-1]
39            lam = 0.2 # [nm]
40            x = SElength
41            y = data
42            dy = err_data
43            sample = Sample()
44            has_no_finite_acceptance = acceptance_angle is not None
45        data = SESANSData1D()
46        data.acceptance_angle = acceptance_angle
47
48    data.has_no_finite_acceptance = acceptance_angle is not None
49    radius = initial_vals.get("radius", 1000)
50    data.Rmax = 30*radius # [A]
51
52    if isinstance(model, basestring):
53        model = get_bumps_model(model)
54
55    # Load custom parameters, initial values and parameter ranges
56    pars = model.parameters().copy()
57    pars.update(custom_params)
58    for k, v in custom_params.items():
59        if k not in pars:
60            par_error("Can't set parameter %r in model"%k, pars)
61        setattr(model, k, v)
62        model._parameter_names.append(k)
63    for k, v in initial_vals.items():
64        if k not in pars:
65            par_error("Can't get parameter %r from model"%k, pars)
66        param = pars[k]
67        param.value = v
68    for k, v in param_range.items():
69        if k not in pars:
70            par_error("Can't set range on parameter %r in model"%k, pars)
71        param = pars[k]
72        param.range(*v)
73
74    if False: # for future implementation
75        M_sesans = bumps_model.Experiment(data=data, model=model)
76        M_sans = bumps_model.Experiment(data=sans_data, model=model)
77        problem = FitProblem([M_sesans, M_sans])
78    else:
79        M_sesans = bumps_model.Experiment(data=data, model=model)
80        problem = FitProblem(M_sesans)
81    return problem
82
83
84def par_error(msg, pars):
85    raise ValueError(msg+"\nAvailable parameters: %s"%", ".join(sorted(pars.keys())))
Note: See TracBrowser for help on using the repository browser.