source: sasmodels/example/multiscatfit.py @ c03e0cc

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since c03e0cc was 49d1f8b8, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

move multiscat into sasmodels namespace to make it easier to run the example

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4Fit model using multiple scattering.
5
6As of this writing, multiscattering isn't integrated into sasmodels, and a
7number of hacks are required to get it running as a fit.
8
9The appropriate items need to be on the python path.  These include
10sasview (for reading the data), sasmodels and bumps.  The multiscat module
11(currently in the sasmodels/explore directory) is also needed, either beside
12this example fit file, or by putting sasmdoels/explore on the python path.
13
14On Unix/Mac running as developer I do::
15
16    # Show the model without fitting
17    PYTHONPATH=..:../explore:../../bumps:../../sasview/src python multiscatfit.py
18
19    # Run the fit
20    PYTHONPATH=..:../explore:../../bumps:../../sasview/src ../../bumps/run.py \
21    multiscatfit.py --store=/tmp/t1
22
23You may be able to run multiscatfit.py against the distributed sasview
24application (if it is new enough, and if you have multiscat.py in the
25same directory).  You probably need a command such as::
26
27    sasview.exe bumps.cli multiscatfit.py --store=t1
28"""
29
30import sys
31from bumps.names import *
32from sasmodels.core import load_model
33from sasmodels.bumps_model import Model, Experiment
34from sasmodels.data import load_data, set_beam_stop, set_top
35
36from sasmodels.multiscat import MultipleScattering
37
38## Load the data
39#data = load_data('DEC07267.DAT')
40#set_beam_stop(data, 0.003, outer=0.025)
41data = load_data('latex_smeared.xml', index=0)
42
43## Define the model
44kernel = load_model("ellipsoid")
45
46model = Model(
47    kernel,
48    scale=0.005, background=0.05,
49    radius_polar=2200, radius_equatorial=2200,
50    sld=.291, sld_solvent=7.105,
51    #theta=90, theta_pd=0, theta_pd_n=0, theta_pd_nsigma=3,
52    #phi=90, phi_pd=0, phi_pd_n=20, phi_pd_nsigma=3,
53    radius_polar_pd=0.222296, radius_polar_pd_n=1, radius_polar_pd_nsigma=0,
54    radius_equatorial_pd=.000128, radius_equatorial_pd_n=1, radius_equatorial_pd_nsigma=0,
55    )
56
57# SET THE FITTING PARAMETERS
58model.radius_polar.range(15, 3000)
59model.radius_equatorial.range(15, 3000)
60#model.theta.range(0, 90)
61#model.theta_pd.range(0,10)
62#model.phi_pd.range(0,20)
63#model.phi.range(0, 180)
64model.background.range(0,1000)
65model.scale.range(0, 0.1)
66
67# Mulitple scattering probability parameter
68# HACK: the probability is stuffed in as an extra parameter to the experiment.
69probability = Parameter(name="probability", value=0.0)
70probability.range(0.0, 0.9)
71
72M = Experiment(data=data, model=model, extra_pars={'probability': probability})
73
74# Stack mulitple scattering on top of the existing resolution function.
75# Because resolution functions in sasview don't have fitting parameters,
76# we instead allow the multiple scattering calculator to take a function
77# instead of a probability.  This function returns the current value of
78# the parameter. ** THIS IS TEMPORARY ** when multiple scattering is
79# properly integrated into sasmodels and sasview, its fittable parameter
80# will be treated like the model parameters.
81M.resolution = MultipleScattering(resolution=M.resolution,
82                                  probability=lambda: probability.value,
83                                  )
84M._kernel_inputs = M.resolution.q_calc
85problem = FitProblem(M)
86
87if __name__ == "__main__":
88    #M.theory()
89    M.plot()
90    import pylab; pylab.show()
Note: See TracBrowser for help on using the repository browser.