[a98958b] | 1 | from bumps.names import * |
---|
[a06430c] | 2 | from sasmodels import core, bumps_model, sesans |
---|
[170ea69] | 3 | from sas.sascalc.dataloader.loader import Loader |
---|
[0a33675] | 4 | |
---|
[84db7a5] | 5 | def get_bumps_model(model_name): |
---|
| 6 | kernel = core.load_model(model_name) |
---|
| 7 | model = bumps_model.Model(kernel) |
---|
| 8 | return model |
---|
[0ac3db5] | 9 | |
---|
[5bb9c79] | 10 | def sesans_fit(file, model, initial_vals={}, custom_params={}, param_range=[], acceptance_angle=None): |
---|
[02e70ff] | 11 | """ |
---|
| 12 | |
---|
[a98958b] | 13 | @param file: SESANS file location |
---|
[84db7a5] | 14 | @param model: Bumps model object or model name - can be model, model_1 * model_2, and/or model_1 + model_2 |
---|
[a98958b] | 15 | @param initial_vals: dictionary of {param_name : initial_value} |
---|
| 16 | @param custom_params: dictionary of {custom_parameter_name : Parameter() object} |
---|
| 17 | @param param_range: dictionary of {parameter_name : [minimum, maximum]} |
---|
[84db7a5] | 18 | @param constraints: dictionary of {parameter_name : constraint} |
---|
[a98958b] | 19 | @return: FitProblem for Bumps usage |
---|
| 20 | """ |
---|
[4c1bfb3] | 21 | initial_vals['background'] = 0.0 |
---|
[a98958b] | 22 | try: |
---|
| 23 | loader = Loader() |
---|
| 24 | data = loader.load(file) |
---|
| 25 | if data is None: raise IOError("Could not load file %r"%(file)) |
---|
[e806077] | 26 | |
---|
[a98958b] | 27 | except: |
---|
| 28 | # If no loadable data file, generate random data |
---|
| 29 | SElength = np.linspace(0, 2400, 61) # [A] |
---|
| 30 | data = np.ones_like(SElength) |
---|
| 31 | err_data = np.ones_like(SElength)*0.03 |
---|
[0ac3db5] | 32 | |
---|
[a98958b] | 33 | class Sample: |
---|
| 34 | zacceptance = 0.1 # [A^-1] |
---|
| 35 | thickness = 0.2 # [cm] |
---|
[c97724e] | 36 | |
---|
[a98958b] | 37 | class SESANSData1D: |
---|
| 38 | #q_zmax = 0.23 # [A^-1] |
---|
| 39 | lam = 0.2 # [nm] |
---|
| 40 | x = SElength |
---|
| 41 | y = data |
---|
| 42 | dy = err_data |
---|
| 43 | sample = Sample() |
---|
[d459d4e] | 44 | has_no_finite_acceptance = acceptance_angle is not None |
---|
[a98958b] | 45 | data = SESANSData1D() |
---|
[170ea69] | 46 | data.acceptance_angle = acceptance_angle |
---|
[9c117a2] | 47 | |
---|
[d459d4e] | 48 | data.has_no_finite_acceptance = acceptance_angle is not None |
---|
[84db7a5] | 49 | if "radius" in initial_vals: |
---|
| 50 | radius = initial_vals.get("radius") |
---|
| 51 | else: |
---|
| 52 | radius = 1000 |
---|
[4c1bfb3] | 53 | data.Rmax = 30*radius # [A] |
---|
[e806077] | 54 | |
---|
[84db7a5] | 55 | if isinstance(model, basestring): |
---|
| 56 | model = get_bumps_model(model) |
---|
[346bc88] | 57 | |
---|
[84db7a5] | 58 | # Load custom parameters, initial values and parameter ranges |
---|
[a98958b] | 59 | for k, v in custom_params.items(): |
---|
| 60 | setattr(model, k, v) |
---|
| 61 | model._parameter_names.append(k) |
---|
| 62 | for k, v in initial_vals.items(): |
---|
| 63 | param = model.parameters().get(k) |
---|
| 64 | setattr(param, "value", v) |
---|
| 65 | for k, v in param_range.items(): |
---|
| 66 | param = model.parameters().get(k) |
---|
| 67 | if param is not None: |
---|
| 68 | setattr(param.bounds, "limits", v) |
---|
[346bc88] | 69 | |
---|
[84db7a5] | 70 | if False: # for future implementation |
---|
[a98958b] | 71 | M_sesans = bumps_model.Experiment(data=data, model=model) |
---|
| 72 | M_sans = bumps_model.Experiment(data=sans_data, model=model) |
---|
| 73 | problem = FitProblem([M_sesans, M_sans]) |
---|
| 74 | else: |
---|
| 75 | M_sesans = bumps_model.Experiment(data=data, model=model) |
---|
| 76 | problem = FitProblem(M_sesans) |
---|
[4c1bfb3] | 77 | return problem |
---|