[a98958b] | 1 | #TODO: Convert units properly (nm -> A) |
---|
| 2 | #TODO: Implement constraints |
---|
[c97724e] | 3 | |
---|
[a98958b] | 4 | from bumps.names import * |
---|
[0a33675] | 5 | from sasmodels import core, bumps_model |
---|
| 6 | |
---|
[a98958b] | 7 | HAS_CONVERTER = True |
---|
| 8 | try: |
---|
| 9 | from sas.sascalc.data_util.nxsunit import Converter |
---|
| 10 | except ImportError: |
---|
| 11 | HAS_CONVERTER = False |
---|
| 12 | |
---|
| 13 | def sesans_fit(file, model_name, initial_vals={}, custom_params={}, param_range=[]): |
---|
| 14 | """ |
---|
[0ac3db5] | 15 | |
---|
[a98958b] | 16 | @param file: SESANS file location |
---|
| 17 | @param model_name: model name string - 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 | @return: FitProblem for Bumps usage |
---|
| 22 | """ |
---|
| 23 | try: |
---|
| 24 | from sas.sascalc.dataloader.loader import Loader |
---|
| 25 | loader = Loader() |
---|
| 26 | data = loader.load(file) |
---|
| 27 | if data is None: raise IOError("Could not load file %r"%(file)) |
---|
| 28 | if HAS_CONVERTER == True: |
---|
| 29 | default_unit = "A" |
---|
| 30 | data_conv_q = Converter(data._xunit) |
---|
| 31 | data.x = data_conv_q(data.x, units=default_unit) |
---|
| 32 | data._xunit = default_unit |
---|
[e806077] | 33 | |
---|
[a98958b] | 34 | except: |
---|
| 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() |
---|
| 51 | data = SESANSData1D() |
---|
[9c117a2] | 52 | |
---|
[a98958b] | 53 | radius = 1000 |
---|
| 54 | data.Rmax = 3*radius # [A] |
---|
[e806077] | 55 | |
---|
[a98958b] | 56 | kernel = core.load_model(model_name) |
---|
| 57 | model = bumps_model.Model(kernel) |
---|
[346bc88] | 58 | |
---|
[a98958b] | 59 | # Load custom parameters, initial values and parameter constraints |
---|
| 60 | for k, v in custom_params.items(): |
---|
| 61 | setattr(model, k, v) |
---|
| 62 | model._parameter_names.append(k) |
---|
| 63 | for k, v in initial_vals.items(): |
---|
| 64 | param = model.parameters().get(k) |
---|
| 65 | setattr(param, "value", v) |
---|
| 66 | for k, v in param_range.items(): |
---|
| 67 | param = model.parameters().get(k) |
---|
| 68 | if param is not None: |
---|
| 69 | setattr(param.bounds, "limits", v) |
---|
[346bc88] | 70 | |
---|
[a98958b] | 71 | if False: # have sans data |
---|
| 72 | M_sesans = bumps_model.Experiment(data=data, model=model) |
---|
| 73 | M_sans = bumps_model.Experiment(data=sans_data, model=model) |
---|
| 74 | problem = FitProblem([M_sesans, M_sans]) |
---|
| 75 | else: |
---|
| 76 | M_sesans = bumps_model.Experiment(data=data, model=model) |
---|
| 77 | problem = FitProblem(M_sesans) |
---|
| 78 | return problem |
---|