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