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