source: sasmodels/example/sesansfit.py @ 4c1bfb3

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

refactor fit_sesans/sesansfit to simplify startup

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