source: sasmodels/example/sesansfit.py @ d6850fa

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

Good progress on SESANS scripting.

  • Property mode set to 100644
File size: 2.6 KB
Line 
1#TODO: Convert units properly (nm -> A)
2#TODO: Implement constraints
3
4from bumps.names import *
5from sasmodels import core, bumps_model
6
7HAS_CONVERTER = True
8try:
9    from sas.sascalc.data_util.nxsunit import Converter
10except ImportError:
11    HAS_CONVERTER = False
12
13def sesans_fit(file, model_name, initial_vals={}, custom_params={}, param_range=[]):
14    """
15
16    @param file: SESANS file location
17    @param model_name: model name string - 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    @return: FitProblem for Bumps usage
22    """
23    try:
24        from sas.sascalc.dataloader.loader import Loader
25        loader = Loader()
26        data = loader.load(file)
27        if data is None: raise IOError("Could not load file %r"%(file))
28        if HAS_CONVERTER == True:
29            default_unit = "A"
30            data_conv_q = Converter(data._xunit)
31            data.x = data_conv_q(data.x, units=default_unit)
32            data._xunit = default_unit
33
34    except:
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        data = SESANSData1D()
52
53    radius = 1000
54    data.Rmax = 3*radius # [A]
55
56    kernel = core.load_model(model_name)
57    model = bumps_model.Model(kernel)
58
59    # Load custom parameters, initial values and parameter constraints
60    for k, v in custom_params.items():
61        setattr(model, k, v)
62        model._parameter_names.append(k)
63    for k, v in initial_vals.items():
64        param = model.parameters().get(k)
65        setattr(param, "value", v)
66    for k, v in param_range.items():
67        param = model.parameters().get(k)
68        if param is not None:
69            setattr(param.bounds, "limits", v)
70
71    if False: # have sans data
72        M_sesans = bumps_model.Experiment(data=data, model=model)
73        M_sans = bumps_model.Experiment(data=sans_data, model=model)
74        problem = FitProblem([M_sesans, M_sans])
75    else:
76        M_sesans = bumps_model.Experiment(data=data, model=model)
77        problem = FitProblem(M_sesans)
78    return problem
Note: See TracBrowser for help on using the repository browser.