[fa79f5c] | 1 | import logging |
---|
| 2 | |
---|
[a98958b] | 3 | from bumps.names import * |
---|
[a06430c] | 4 | from sasmodels import core, bumps_model, sesans |
---|
[170ea69] | 5 | from sas.sascalc.dataloader.loader import Loader |
---|
[0a33675] | 6 | |
---|
[84db7a5] | 7 | def get_bumps_model(model_name): |
---|
| 8 | kernel = core.load_model(model_name) |
---|
| 9 | model = bumps_model.Model(kernel) |
---|
| 10 | return model |
---|
[0ac3db5] | 11 | |
---|
[fa79f5c] | 12 | def sesans_fit(file, model, initial_vals={}, custom_params={}, param_range=[], |
---|
| 13 | acceptance_angle=None): |
---|
[02e70ff] | 14 | """ |
---|
| 15 | |
---|
[a98958b] | 16 | @param file: SESANS file location |
---|
[84db7a5] | 17 | @param model: Bumps model object or model name - can be model, model_1 * model_2, and/or model_1 + model_2 |
---|
[a98958b] | 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]} |
---|
[84db7a5] | 21 | @param constraints: dictionary of {parameter_name : constraint} |
---|
[a98958b] | 22 | @return: FitProblem for Bumps usage |
---|
| 23 | """ |
---|
[fa79f5c] | 24 | logging.basicConfig() |
---|
| 25 | |
---|
[4c1bfb3] | 26 | initial_vals['background'] = 0.0 |
---|
[a98958b] | 27 | try: |
---|
| 28 | loader = Loader() |
---|
[fa79f5c] | 29 | data = loader.load(file)[0] |
---|
| 30 | if data is None: |
---|
| 31 | raise IOError("Could not load file %r"%(file)) |
---|
[e806077] | 32 | |
---|
[fa79f5c] | 33 | except Exception: |
---|
| 34 | raise |
---|
[a98958b] | 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 |
---|
[0ac3db5] | 39 | |
---|
[a98958b] | 40 | class Sample: |
---|
| 41 | zacceptance = 0.1 # [A^-1] |
---|
| 42 | thickness = 0.2 # [cm] |
---|
[c97724e] | 43 | |
---|
[a98958b] | 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() |
---|
[d459d4e] | 51 | has_no_finite_acceptance = acceptance_angle is not None |
---|
[a98958b] | 52 | data = SESANSData1D() |
---|
[170ea69] | 53 | data.acceptance_angle = acceptance_angle |
---|
[9c117a2] | 54 | |
---|
[d459d4e] | 55 | data.has_no_finite_acceptance = acceptance_angle is not None |
---|
[fa902d4] | 56 | radius = initial_vals.get("radius", 1000) |
---|
[4c1bfb3] | 57 | data.Rmax = 30*radius # [A] |
---|
[e806077] | 58 | |
---|
[fa79f5c] | 59 | if isinstance(model, str): |
---|
[84db7a5] | 60 | model = get_bumps_model(model) |
---|
[346bc88] | 61 | |
---|
[84db7a5] | 62 | # Load custom parameters, initial values and parameter ranges |
---|
[9217ef8] | 63 | pars = model.parameters().copy() |
---|
| 64 | pars.update(custom_params) |
---|
[a98958b] | 65 | for k, v in custom_params.items(): |
---|
[9217ef8] | 66 | if k not in pars: |
---|
| 67 | par_error("Can't set parameter %r in model"%k, pars) |
---|
[a98958b] | 68 | setattr(model, k, v) |
---|
| 69 | model._parameter_names.append(k) |
---|
| 70 | for k, v in initial_vals.items(): |
---|
[9217ef8] | 71 | if k not in pars: |
---|
| 72 | par_error("Can't get parameter %r from model"%k, pars) |
---|
| 73 | param = pars[k] |
---|
[fa902d4] | 74 | param.value = v |
---|
[a98958b] | 75 | for k, v in param_range.items(): |
---|
[9217ef8] | 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) |
---|
[346bc88] | 80 | |
---|
[84db7a5] | 81 | if False: # for future implementation |
---|
[a98958b] | 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) |
---|
[4c1bfb3] | 88 | return problem |
---|
[9217ef8] | 89 | |
---|
| 90 | |
---|
| 91 | def par_error(msg, pars): |
---|
| 92 | raise ValueError(msg+"\nAvailable parameters: %s"%", ".join(sorted(pars.keys()))) |
---|