source: sasmodels/example/sesansfit.py @ 5bb9c79

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

Small fix to sesansfit script.

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