Version 15 (modified by smk78, 9 years ago) (diff)

Introduction

In the new sasmodels package https://github.com/SasView/sasmodels there are three ways of writing models:

The documentation for the sasmodels package can be found at http://www.sasview.org/sasmodels/

Before following the conversion instructions below, it is worth reading the documentation at http://www.sasview.org/sasmodels/api/generate.html#module-sasmodels.generate which describes the structure of a model.

CREATE NEW MODEL FILES.

If you have not already done so, download the sasmodels package from https://github.com/sasview/sasmodels.

In the sasmodels/models directory, copy the appropriate files using the examples above to mymodel.py and mymodel.c as required where “mymodel’ is the NEW name for the model to be moved. Please follow new naming rules:

  • No capitalization and thus no CamelCase. If necessary use underscore to separate (i.e. barbell not BarBell or broad_peak not BroadPeak)
  • Remove “model” from the name (i.e. barbell not BarBellModel)

EDIT NEW MODEL FILES

The model interface definition is in the .py file. This file contains:

  • model documentation. The .py starts with an r (for raw) and three sets of quotes to start the doc string and ends with a second set of 3 quotes. This is where the FULL documentation for the model goes (to be picked up by sphinx). Paste the model documentation from the appropriate section of model_functions.rst (found at src/sas/models/media/model_functions.rst) here. For example, for the BarBellModel, search for BarBellModel in the rst file then copy everything after the *2.x.x. ModelNameModel**. Edit the result as follows:
    • the *2.x.x.x Model** section with its brief description of the model becomes the model title; it feels odd that the documentation string does not start with name and brief description but it is automatically added by the model loader
    • replace *2.x.x.x. Definition** with Definition followed by a line of 10 dashes.
    • wherever possible try to replace images of math functions with Latex. you can use the live demo Mathjax page (http://www.mathjax.org/) to make sure the equation looks as expected. also a lot of the Latex code can be taken from (or edited) from the PDF document created by Paul Kienzle: http://sasview.org/attachment/wiki/SasModels%20Work%20Package/Equations.docx.pdf.
    • remove the table of parameters which will get autogenerated
    • copy the figures from the sasview/src/sas/models/media/img subdirectory to the sasmodels/sasmodels/models/img subdirectory
    • figure captions should be indented relative to the ..image:: tag. This allows us to for example change the font of the caption through CSS.
    • remove the figure number since this will be generated automatically for the caption.
    • make sure all references to model parameters are appropriately renamed, and that the relationship between equation variables and model parameters are obvious.
  • name = "mymodel". This is the name of the model that is shown to the user. Use the name of the file as described above in 1. There will be less confusion if the model name matches the file name, but they could be different.
  • title = "short description". This is a new value to be used for tooltip for example. It can probably be pulled directly from the model_functions.rst file.
  • description = """doc string""". Cut and paste the self.description string from the SasView model header in src/sas/models/include. This gives a brief description of the equation and the parameters without the need to read the entire model documentation. Make sure the parameter names match the current model definition.
  • parameters = [["name", "units", default, [min,max], "type", "tooltip"],…]. This is where the parameters get defined. The syntax should be obvious from the default. Copy the parameters from the model header file src/sas/models/include
    • PARAMETER NAMES: VERY IMPORTANT. We are trying to make the model parameter more consistent between models. So solvent sld for example should have exactly the same name in every model. Current list of new parameters is:
      • radius = radius
      • sld = scattering length density of particle
      • solvent_sld = scattering length density of matrix
      • cor_length = correlation length
      • exp = exponent (example: porod_exp)
      • peak_pos = q_peak or q0 etc
      • theta = axis_theta, phi = axis_phi
      • add more as you see them
      • NOTE: There is no need to specify 'scale' or 'background', these are implicit to all models.
    • lower and upper limits can be any number or -inf or inf
      • add limits where required; the sasview models don't usually define them
      • the limits will show up as the default limits for the fit, making it easy for example to force the radius to always be greater than zero.
    • type can take 3 values at this time: “”, “volume”, or “orientation”
      • "volume" parameters are passed to Iq(), Iqxy(), and form_volume(), and have polydispersity loops generated automatically
      • "orientation" parameters are only passed to Iqxy().
    • the order of the parameters in the definition will be the order of the parameters in the user interface and the order of the parameters in Iq(), Iqxy() and form_volume().
  • COMPUTATION
    • Pure Python
      • def Iq (this section does not exist for c models. It is replaced by the list of c files to call)
        • Iq.vectorized = True or False. This is used to tag the definition as accepting a Q vector, or having to compute each Q individually.
          • use functions from numpy rather than math
          • don't use if statements. Instead do tricks like "a = x*(q>0) + y*(q⇐0)" which sets a to x if q is positive or y if q is zero
      • def Iqxy. If this model does not have an oriented form just set it equal to the square root of the sum of Iq(x) squared and Iq(y) squared.
      • src/sas/models contains the pure python models, mixed together with the python + C models.
    • Python + C
      • model Iq, Iqxy are defined in src/sas/models/c_extension/c_models
      • supporting scattering calculations are defined in src/sas/models/c_extension/libigor
      • special functions are defined in src/sas/models/c_extension/cephes. With model definitions you are limited to the simple math functions defined in opencl
      • common functions should be moved into their own file, such as lib/J1.c for the bessel function J1.
      • in place of the above methods point to the C source files needed. This will be a list of any lib files needed, followed by the model.c file, where model is the same name as given to the python model.py. For example:
            source = ["lib/J1.c", "lib/gauss76.c", "my_model.c"]
        
      • the c file must contain the form_volume, Iq, Iqxy methods. These and any other functions defined (e.g. the _cyl() helper function) must be defined as doubles in the first lines of the file.
  • For certain models, such as those that can be multiplied by a structure factor, the ER attribute should be set to the Equivalent Radius (of a sphere).
  • For certain models, namely core shell type models, the VR attribute returns the volume ratio for the core-shell.
  • demo is a dictionary containing the value of each parameter as given in the rst documentation. Make sure to enter the appropriate values from that documentation. This will then be used to generate the example curve in said documentation.
    • include polydispersity parameters (e.g., radius_pd=0.2, radius_pd_n=9) so that you can compare the polydispersity calculations against those from sasview; use -mono on the compare.sh command line to see the direct calculation without polydispersity.
  • oldname is the name for this model used in SasView. Make sure to put the correct original name. This is required for the transition to allow compatibility and to test that the models are equivalent.
  • oldpars is the name of the parameters as used in SasView. For each parameter give the name of the parameter as used in the original SasView code version of this model. Again this is required for the transition.
  • tests section (for unit test purposes) VERY IMPORTANT. Syntax: test is a list of lists. each list is one test and contains, in order,
    • a dictionary of parameter values. This can be {} using the default parameters, or filled with some parameters that will be different from the default ({‘radius’:10.0, ‘sld’:4}) unlisted parameters will be given the default values.
    • the input q value or tuple of qx, qy values
    • the output Iq or Iqxy expected of the model for the parameters and q value given
    • inputs and outputs can also be lists if you have several values to test for the same model parameters
    • for ER and VR, give the inputs as 'ER' and 'VR' respectively
    • include at least one test for each model. Please make sure that the answer value is correct (i.e. not a random number please)
    • to summarize:
          tests = [
              [ {parameters}, q, I(q)],
              [ {parameters}, [q], [I(q)] ],
              [ {parameters}, [q1, q2, ...], [I(q1), I(q2), ...]],
              [ {parameters}, (qx, qy), I(qx, Iqy)],
              [ {parameters}, [(qx1, qy1), (qx2, qy2), ...],
                              [I(qx1,qy1), I(qx2,qy2), ...]],
              [ {parameters}, 'ER', ER(pars) ],
              [ {parameters}, 'VR', VR(pars) ],
          ]
      

where

    parameters = {'k1': v1, 'k2': v2, ...}

TEST YOUR NEWLY CONVERTED MODEL

Test your new model by runing compare.py to verify that the converted model is giving the same results as it did in SasView? prior to conversion. In order to do this, you need either a version of sasview installed in your python path, or a built sasview in a checked out directory at the same level as the checked out sasmodels repository.

If the latter, you need to add the sasview build directory to your PYTHONPATH or use compare.sh which will do so for you.

The first thing to test is that you are getting the same answer as SasView for the 1D version of the model. This is done with:

./compare.sh -1d modelname 

This will result in some comparison metrics between the openCL implementation (if installed - should revert to using ctypes if no OpenCL is installed) and a plot of the two calculations and a comparison plot.

If the model has 2D orientational calculation, then you should additionally test with:

./compare.sh -2d modelname

Brief help for the comparison script can be obtained by just running ./compare.sh

Run the unit tests that you have added:

python -m sasmodels.model_test modelname

FINISHING

Once compare passes properly and everything is done edit the models table to indicate that the conversion is complete