1 | from bumps.names import * |
---|
2 | from sasmodels import core, bumps_model, sesans |
---|
3 | |
---|
4 | HAS_CONVERTER = True |
---|
5 | try: |
---|
6 | from sas.sascalc.data_util.nxsunit import Converter |
---|
7 | except ImportError: |
---|
8 | HAS_CONVERTER = False |
---|
9 | |
---|
10 | def get_bumps_model(model_name): |
---|
11 | kernel = core.load_model(model_name) |
---|
12 | model = bumps_model.Model(kernel) |
---|
13 | return model |
---|
14 | |
---|
15 | def 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 |
---|