source: sasmodels/example/sesansfit.py @ e589e9a

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since e589e9a was fa79f5c, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

restore working sesans example using direct model

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