Changes in / [31d5187:cff2939] in sasmodels
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
README.rst
r2a64722 re30d645 10 10 is available. 11 11 12 Install 12 Example 13 13 ------- 14 15 The easiest way to use sasmodels is from `SasView <http://www.sasview.org/>`_.16 17 You can also install sasmodels as a standalone package in python. Use18 `miniconda <https://docs.conda.io/en/latest/miniconda.html>`_19 or `anaconda <https://www.anaconda.com/>`_20 to create a python environment with the sasmodels dependencies::21 22 $ conda create -n sasmodels -c conda-forge numpy scipy matplotlib pyopencl23 24 The option ``-n sasmodels`` names the environment sasmodels, and the option25 ``-c conda-forge`` selects the conda-forge package channel because pyopencl26 is not part of the base anaconda distribution.27 28 Activate the environment and install sasmodels::29 30 $ conda activate sasmodels31 (sasmodels) $ pip install sasmodels32 33 Install `bumps <https://github.com/bumps/bumps>`_ if you want to use it to fit34 your data::35 36 (sasmodels) $ pip install bumps37 38 Usage39 -----40 41 Check that the works::42 43 (sasmodels) $ python -m sasmodels.compare cylinder44 45 To show the orientation explorer::46 47 (sasmodels) $ python -m sasmodels.jitter48 49 Documentation is available online as part of the SasView50 `fitting perspective <http://www.sasview.org/docs/index.html>`_51 as well as separate pages for52 `individual models <http://www.sasview.org/docs/user/sasgui/perspectives/fitting/models/index.html>`_.53 Programming details for sasmodels are available in the54 `developer documentation <http://www.sasview.org/docs/dev/dev.html>`_.55 56 57 Fitting Example58 ---------------59 14 60 15 The example directory contains a radial+tangential data set for an oriented 61 16 rod-like shape. 62 17 63 To load the example data, you will need the SAS data loader from the sasview 64 package. This is not yet available on PyPI, so you will need a copy of the 65 SasView source code to run it. Create a directory somewhere to hold the 66 sasview and sasmodels source code, which we will refer to as $SOURCE. 18 The data is loaded by sas.dataloader from the sasview package, so sasview 19 is needed to run the example. 67 20 68 Use the following to install sasview, and the sasmodels examples:: 21 To run the example, you need sasview, sasmodels and bumps. Assuming these 22 repositories are installed side by side, change to the sasmodels/example 23 directory and enter:: 69 24 70 (sasmodels) $ cd $SOURCE 71 (sasmodels) $ conda install git 72 (sasmodels) $ git clone https://github.com/sasview/sasview.git 73 (sasmodels) $ git clone https://github.com/sasview/sasmodels.git 25 PYTHONPATH=..:../../sasview/src ../../bumps/run.py fit.py \ 26 cylinder --preview 74 27 75 Set the path to the sasview source on your python path within the sasmodels 76 environment. On Windows, this will be:: 28 See bumps documentation for instructions on running the fit. With the 29 python packages installed, e.g., into a virtual environment, then the 30 python path need not be set, and the command would be:: 77 31 78 (sasmodels)> set PYTHONPATH="$SOURCE\sasview\src" 79 (sasmodels)> cd $SOURCE/sasmodels/example 80 (sasmodels)> python -m bumps.cli fit.py cylinder --preview 81 82 On Mac/Linux with the standard shell this will be:: 83 84 (sasmodels) $ export PYTHONPATH="$SOURCE/sasview/src" 85 (sasmodels) $ cd $SOURCE/sasmodels/example 86 (sasmodels) $ bumps fit.py cylinder --preview 32 bumps fit.py cylinder --preview 87 33 88 34 The fit.py model accepts up to two arguments. The first argument is the … … 92 38 both radial and tangential simultaneously, use the word "both". 93 39 94 See `bumps documentation <https://bumps.readthedocs.io/>`_ for detailed 95 instructions on running the fit. 40 Notes 41 ----- 42 43 cylinder.c + cylinder.py is the cylinder model with renamed variables and 44 sld scaled by 1e6 so the numbers are nicer. The model name is "cylinder" 45 46 lamellar.py is an example of a single file model with embedded C code. 96 47 97 48 |TravisStatus|_ -
explore/precision.py
rcd28947 rfba9ca0 207 207 return model_info 208 208 209 # Hack to allow second parameter A in the gammainc and gammaincc functions. 210 # Create a 2-D variant of the precision test if we need to handle other two 211 # parameter functions. 209 # Hack to allow second parameter A in two parameter functions 212 210 A = 1 213 211 def parse_extra_pars(): 214 """215 Parse the command line looking for the second parameter "A=..." for the216 gammainc/gammaincc functions.217 """218 212 global A 219 213 … … 339 333 ) 340 334 add_function( 341 # Note: "a" is given as A=... on the command line via parse_extra_pars342 335 name="gammainc(x)", 343 336 mp_function=lambda x, a=A: mp.gammainc(a, a=0, b=x)/mp.gamma(a), … … 346 339 ) 347 340 add_function( 348 # Note: "a" is given as A=... on the command line via parse_extra_pars349 341 name="gammaincc(x)", 350 342 mp_function=lambda x, a=A: mp.gammainc(a, a=x, b=mp.inf)/mp.gamma(a), -
sasmodels/compare.py
r4de14584 r4de14584 1152 1152 'rel_err' : True, 1153 1153 'explore' : False, 1154 'use_demo' : False,1154 'use_demo' : True, 1155 1155 'zero' : False, 1156 1156 'html' : False, -
sasmodels/direct_model.py
r5024a56 r5024a56 332 332 333 333 # Need to pull background out of resolution for multiple scattering 334 default_background = self._model.info.parameters.common_parameters[1].default 335 background = pars.get('background', default_background) 334 background = pars.get('background', DEFAULT_BACKGROUND) 336 335 pars = pars.copy() 337 336 pars['background'] = 0. -
sasmodels/generate.py
rcd28947 r39a06c9 703 703 """ 704 704 for code in source: 705 if _FQ_PATTERN.search(code) is not None: 705 m = _FQ_PATTERN.search(code) 706 if m is not None: 706 707 return True 707 708 return False … … 711 712 # type: (List[str]) -> bool 712 713 """ 713 Return True if C source defines " double shell_volume(".714 Return True if C source defines "void Fq(". 714 715 """ 715 716 for code in source: 716 if _SHELL_VOLUME_PATTERN.search(code) is not None: 717 m = _SHELL_VOLUME_PATTERN.search(code) 718 if m is not None: 717 719 return True 718 720 return False … … 1006 1008 pars = model_info.parameters.kernel_parameters 1007 1009 else: 1008 pars = (model_info.parameters.common_parameters 1009 + model_info.parameters.kernel_parameters) 1010 pars = model_info.parameters.COMMON + model_info.parameters.kernel_parameters 1010 1011 partable = make_partable(pars) 1011 1012 subst = dict(id=model_info.id.replace('_', '-'), -
sasmodels/kernel.py
rcd28947 re44432d 133 133 nout = 2 if self.info.have_Fq and self.dim == '1d' else 1 134 134 total_weight = self.result[nout*self.q_input.nq + 0] 135 # Note: total_weight = sum(weight > cutoff), with cutoff >= 0, so it136 # is okay to test directly against zero. If weight is zero then I(q),137 # etc. must also be zero.138 135 if total_weight == 0.: 139 136 total_weight = 1. -
sasmodels/modelinfo.py
r39a06c9 r39a06c9 404 404 parameters counted as n individual parameters p1, p2, ... 405 405 406 * *common_parameters* is the list of common parameters, with a unique407 copy for each model so that structure factors can have a default408 background of 0.0.409 410 406 * *call_parameters* is the complete list of parameters to the kernel, 411 407 including scale and background, with vector parameters recorded as … … 420 416 parameters don't use vector notation, and instead use p1, p2, ... 421 417 """ 418 # scale and background are implicit parameters 419 COMMON = [Parameter(*p) for p in COMMON_PARAMETERS] 420 422 421 def __init__(self, parameters): 423 422 # type: (List[Parameter]) -> None 424 425 # scale and background are implicit parameters426 # Need them to be unique to each model in case they have different427 # properties, such as default=0.0 for structure factor backgrounds.428 self.common_parameters = [Parameter(*p) for p in COMMON_PARAMETERS]429 430 423 self.kernel_parameters = parameters 431 424 self._set_vector_lengths() … … 475 468 if p.polydisperse and p.type not in ('orientation', 'magnetic')) 476 469 self.pd_2d = set(p.name for p in self.call_parameters if p.polydisperse) 477 478 def set_zero_background(self):479 """480 Set the default background to zero for this model. This is done for481 structure factor models.482 """483 # type: () -> None484 # Make sure background is the second common parameter.485 assert self.common_parameters[1].id == "background"486 self.common_parameters[1].default = 0.0487 self.defaults = self._get_defaults()488 470 489 471 def check_angles(self): … … 585 567 def _get_call_parameters(self): 586 568 # type: () -> List[Parameter] 587 full_list = self. common_parameters[:]569 full_list = self.COMMON[:] 588 570 for p in self.kernel_parameters: 589 571 if p.length == 1: … … 688 670 689 671 # Gather the user parameters in order 690 result = control + self. common_parameters672 result = control + self.COMMON 691 673 for p in self.kernel_parameters: 692 674 if not is2d and p.type in ('orientation', 'magnetic'): … … 788 770 789 771 info = ModelInfo() 790 791 # Build the parameter table792 772 #print("make parameter table", kernel_module.parameters) 793 773 parameters = make_parameter_table(getattr(kernel_module, 'parameters', [])) 794 795 # background defaults to zero for structure factor models796 structure_factor = getattr(kernel_module, 'structure_factor', False)797 if structure_factor:798 parameters.set_zero_background()799 800 # TODO: remove demo parameters801 # The plots in the docs are generated from the model default values.802 # Sascomp set parameters from the command line, and so doesn't need803 # demo values for testing.804 774 demo = expand_pars(parameters, getattr(kernel_module, 'demo', None)) 805 806 775 filename = abspath(kernel_module.__file__).replace('.pyc', '.py') 807 776 kernel_id = splitext(basename(filename))[0] -
sasmodels/models/hardsphere.py
r304c775 r304c775 162 162 return pars 163 163 164 demo = dict(radius_effective=200, volfraction=0.2, 165 radius_effective_pd=0.1, radius_effective_pd_n=40) 164 166 # Q=0.001 is in the Taylor series, low Q part, so add Q=0.1, 165 167 # assuming double precision sasview is correct -
sasmodels/sasview_model.py
r3a1afed r5024a56 382 382 hidden.add('scale') 383 383 hidden.add('background') 384 self._model_info.parameters.defaults['background'] = 0. 384 385 385 386 # Update the parameter lists to exclude any hidden parameters … … 694 695 return self._calculate_Iq(qx, qy) 695 696 696 def _calculate_Iq(self, qx, qy=None ):697 def _calculate_Iq(self, qx, qy=None, Fq=False, effective_radius_type=1): 697 698 if self._model is None: 698 699 self._model = core.build_model(self._model_info) … … 714 715 #print("values", values) 715 716 #print("is_mag", is_magnetic) 717 if Fq: 718 result = calculator.Fq(call_details, values, cutoff=self.cutoff, 719 magnetic=is_magnetic, 720 effective_radius_type=effective_radius_type) 716 721 result = calculator(call_details, values, cutoff=self.cutoff, 717 722 magnetic=is_magnetic) … … 731 736 Calculate the effective radius for P(q)*S(q) 732 737 733 *mode* is the R_eff type, which defaults to 1 to match the ER734 calculation for sasview models from version 3.x.735 736 738 :return: the value of the effective radius 737 739 """ 738 # ER and VR are only needed for old multiplication models, based on 739 # sas.sascalc.fit.MultiplicationModel. Fail for now. If we want to 740 # continue supporting them then add some test cases so that the code 741 # is exercised. We can access ER/VR using the kernel Fq function by 742 # extending _calculate_Iq so that it calls: 743 # if er_mode > 0: 744 # res = calculator.Fq(call_details, values, cutoff=self.cutoff, 745 # magnetic=False, effective_radius_type=mode) 746 # R_eff, form_shell_ratio = res[2], res[4] 747 # return R_eff, form_shell_ratio 748 # Then use the following in calculate_ER: 749 # ER, VR = self._calculate_Iq(q=[0.1], er_mode=mode) 750 # return ER 751 # Similarly, for calculate_VR: 752 # ER, VR = self._calculate_Iq(q=[0.1], er_mode=1) 753 # return VR 754 # Obviously a combined calculate_ER_VR method would be better, but 755 # we only need them to support very old models, so ignore the 2x 756 # performance hit. 757 raise NotImplementedError("ER function is no longer available.") 740 Fq = self._calculate_Iq([0.1], True, mode) 741 return Fq[2] 758 742 759 743 def calculate_VR(self): … … 764 748 :return: the value of the form:shell volume ratio 765 749 """ 766 # See comments in calculate_ER.767 r aise NotImplementedError("VR function is no longer available.")750 Fq = self._calculate_Iq([0.1], True, mode) 751 return Fq[4] 768 752 769 753 def set_dispersion(self, parameter, dispersion): … … 930 914 CylinderModel().evalDistribution([0.1, 0.1]) 931 915 932 def test_structure_factor_background():933 # type: () -> None934 """935 Check that sasview model and direct model match, with background=0.936 """937 from .data import empty_data1D938 from .core import load_model_info, build_model939 from .direct_model import DirectModel940 941 model_name = "hardsphere"942 q = [0.0]943 944 sasview_model = _make_standard_model(model_name)()945 sasview_value = sasview_model.evalDistribution(np.array(q))[0]946 947 data = empty_data1D(q)948 model_info = load_model_info(model_name)949 model = build_model(model_info)950 direct_model = DirectModel(data, model)951 direct_value_zero_background = direct_model(background=0.0)952 953 assert sasview_value == direct_value_zero_background954 955 # Additionally check that direct value background defaults to zero956 direct_value_default = direct_model()957 assert sasview_value == direct_value_default958 959 960 916 def magnetic_demo(): 961 917 Model = _make_standard_model('sphere') … … 978 934 #print("rpa:", test_rpa()) 979 935 #test_empty_distribution() 980 #test_structure_factor_background()
Note: See TracChangeset
for help on using the changeset viewer.