source: sasmodels/example/sesansfit.py @ 8898d0f

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

Updated sesans scripting to be more command line focused AND modified hardsphere to effect_radius.

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