source: sasmodels/example/sesansfit.py @ 02e70ff

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

Beginnings of handling acceptance angle in sesans

  • Property mode set to 100644
File size: 3.1 KB
Line 
1from bumps.names import *
2from sasmodels import core, bumps_model, sesans
3
4HAS_CONVERTER = True
5try:
6    from sas.sascalc.data_util.nxsunit import Converter
7except ImportError:
8    HAS_CONVERTER = False
9
10
11def get_bumps_model(model_name):
12    kernel = core.load_model(model_name)
13    model = bumps_model.Model(kernel)
14    return model
15
16def sesans_fit(file, model_name, initial_vals={}, custom_params={}, param_range=[], acceptance_angle=None):
17    """
18
19def sesans_fit(file, model, initial_vals={}, custom_params={}, param_range=[]):
20    """
21    @param file: SESANS file location
22    @param model: Bumps model object or model name - can be model, model_1 * model_2, and/or model_1 + model_2
23    @param initial_vals: dictionary of {param_name : initial_value}
24    @param custom_params: dictionary of {custom_parameter_name : Parameter() object}
25    @param param_range: dictionary of {parameter_name : [minimum, maximum]}
26    @param constraints: dictionary of {parameter_name : constraint}
27    @return: FitProblem for Bumps usage
28    """
29    try:
30        from sas.sascalc.dataloader.loader import Loader
31        loader = Loader()
32        data = loader.load(file)
33        if data is None: raise IOError("Could not load file %r"%(file))
34        if HAS_CONVERTER == True:
35            default_unit = "A"
36            data_conv_q = Converter(data._xunit)
37            for x in data.x:
38                print x
39            data.x = data_conv_q(data.x, units=default_unit)
40            for x in data.x:
41                print x
42            data._xunit = default_unit
43
44    except:
45        # If no loadable data file, generate random data
46        SElength = np.linspace(0, 2400, 61) # [A]
47        data = np.ones_like(SElength)
48        err_data = np.ones_like(SElength)*0.03
49
50        class Sample:
51            zacceptance = 0.1 # [A^-1]
52            thickness = 0.2 # [cm]
53
54        class SESANSData1D:
55            #q_zmax = 0.23 # [A^-1]
56            lam = 0.2 # [nm]
57            x = SElength
58            y = data
59            dy = err_data
60            sample = Sample()
61            acceptance_angle = acceptance_angle
62            needs_all_q = acceptance_angle is not None
63        data = SESANSData1D()
64
65    if "radius" in initial_vals:
66        radius = initial_vals.get("radius")
67    else:
68        radius = 1000
69    data.Rmax = 3*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.