1 | import logging |
---|
2 | |
---|
3 | from bumps.names import * |
---|
4 | from sasmodels import core, bumps_model, sesans |
---|
5 | from sas.sascalc.dataloader.loader import Loader |
---|
6 | |
---|
7 | def get_bumps_model(model_name): |
---|
8 | kernel = core.load_model(model_name) |
---|
9 | model = bumps_model.Model(kernel) |
---|
10 | return model |
---|
11 | |
---|
12 | def sesans_fit(file, model, initial_vals={}, custom_params={}, param_range=[], |
---|
13 | acceptance_angle=None): |
---|
14 | """ |
---|
15 | |
---|
16 | @param file: SESANS file location |
---|
17 | @param model: Bumps model object or model name - 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 | @param constraints: dictionary of {parameter_name : constraint} |
---|
22 | @return: FitProblem for Bumps usage |
---|
23 | """ |
---|
24 | logging.basicConfig() |
---|
25 | |
---|
26 | initial_vals['background'] = 0.0 |
---|
27 | try: |
---|
28 | loader = Loader() |
---|
29 | data = loader.load(file)[0] |
---|
30 | if data is None: |
---|
31 | raise IOError("Could not load file %r"%(file)) |
---|
32 | |
---|
33 | except Exception: |
---|
34 | raise |
---|
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 | has_no_finite_acceptance = acceptance_angle is not None |
---|
52 | data = SESANSData1D() |
---|
53 | data.acceptance_angle = acceptance_angle |
---|
54 | |
---|
55 | data.has_no_finite_acceptance = acceptance_angle is not None |
---|
56 | radius = initial_vals.get("radius", 1000) |
---|
57 | data.Rmax = 30*radius # [A] |
---|
58 | |
---|
59 | if isinstance(model, str): |
---|
60 | model = get_bumps_model(model) |
---|
61 | |
---|
62 | # Load custom parameters, initial values and parameter ranges |
---|
63 | pars = model.parameters().copy() |
---|
64 | pars.update(custom_params) |
---|
65 | for k, v in custom_params.items(): |
---|
66 | if k not in pars: |
---|
67 | par_error("Can't set parameter %r in model"%k, pars) |
---|
68 | setattr(model, k, v) |
---|
69 | model._parameter_names.append(k) |
---|
70 | for k, v in initial_vals.items(): |
---|
71 | if k not in pars: |
---|
72 | par_error("Can't get parameter %r from model"%k, pars) |
---|
73 | param = pars[k] |
---|
74 | param.value = v |
---|
75 | for k, v in param_range.items(): |
---|
76 | if k not in pars: |
---|
77 | par_error("Can't set range on parameter %r in model"%k, pars) |
---|
78 | param = pars[k] |
---|
79 | param.range(*v) |
---|
80 | |
---|
81 | if False: # for future implementation |
---|
82 | M_sesans = bumps_model.Experiment(data=data, model=model) |
---|
83 | M_sans = bumps_model.Experiment(data=sans_data, model=model) |
---|
84 | problem = FitProblem([M_sesans, M_sans]) |
---|
85 | else: |
---|
86 | M_sesans = bumps_model.Experiment(data=data, model=model) |
---|
87 | problem = FitProblem(M_sesans) |
---|
88 | return problem |
---|
89 | |
---|
90 | |
---|
91 | def par_error(msg, pars): |
---|
92 | raise ValueError(msg+"\nAvailable parameters: %s"%", ".join(sorted(pars.keys()))) |
---|