source: sasmodels/example/sesansfit.py @ 89ab393

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 89ab393 was 89ab393, checked in by krzywon, 9 years ago

Moving unit conversion to sesans_reader instead of script

  • Property mode set to 100644
File size: 2.6 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    try:
22        loader = Loader()
23        data = loader.load(file)
24        if data is None: raise IOError("Could not load file %r"%(file))
25
26    except:
27        # If no loadable data file, generate random data
28        SElength = np.linspace(0, 2400, 61) # [A]
29        data = np.ones_like(SElength)
30        err_data = np.ones_like(SElength)*0.03
31
32        class Sample:
33            zacceptance = 0.1 # [A^-1]
34            thickness = 0.2 # [cm]
35
36        class SESANSData1D:
37            #q_zmax = 0.23 # [A^-1]
38            lam = 0.2 # [nm]
39            x = SElength
40            y = data
41            dy = err_data
42            sample = Sample()
43            needs_all_q = acceptance_angle is not None
44        data = SESANSData1D()
45        data.acceptance_angle = acceptance_angle
46
47    data.needs_all_q = acceptance_angle is not None
48    if "radius" in initial_vals:
49        radius = initial_vals.get("radius")
50    else:
51        radius = 1000
52    data.Rmax = 3*radius # [A]
53
54    if isinstance(model, basestring):
55        model = get_bumps_model(model)
56
57    # Load custom parameters, initial values and parameter ranges
58    for k, v in custom_params.items():
59        setattr(model, k, v)
60        model._parameter_names.append(k)
61    for k, v in initial_vals.items():
62        param = model.parameters().get(k)
63        setattr(param, "value", v)
64    for k, v in param_range.items():
65        param = model.parameters().get(k)
66        if param is not None:
67            setattr(param.bounds, "limits", v)
68
69    if False: # for future implementation
70        M_sesans = bumps_model.Experiment(data=data, model=model)
71        M_sans = bumps_model.Experiment(data=sans_data, model=model)
72        problem = FitProblem([M_sesans, M_sans])
73    else:
74        M_sesans = bumps_model.Experiment(data=data, model=model)
75        problem = FitProblem(M_sesans)
76    return problem
Note: See TracBrowser for help on using the repository browser.