These example scripts are intended to help a developer get acquainted with the SrFit programming interface. Although one can write scripts like these to drive optimization, these scripts do not represent the SrFit user interface. That interface will be made available in a future release.

A secondary purpose of these tutorials is to generate interest in SrFit. By reading through the examples we hope that you are inspired to think of exciting new ways to get the most out of your scientific data. If you think SrFit can help you with that, please feel free to contact us through the DiffPy website.



Three things are required for optimization: a function that generates a quantity to be minimized, variables that can be used to manipulate that function and an algorithm that can drive the function output to a smaller value by steering the variables. For scientific purposes, the quantity to be minimized is the disagreement between a measured profile and a theoretical profile. The scientific understanding of the system under consideration partly determines the suitability of the theoretical profile generator and the scientific relevance of the variables used to drive it.

The purpose of SrFit is to give users the means to combine known information about a system of interest in order to extract scientifically relevant quantities, and thus understanding from it. Various experimental procedures and theoretical calculations may be needed to gain the desired understanding of the system. SrFit helps users combine these views of the system in a coherent and consistent manner.

To achieve this purpose, SrFit provides:

  1. a function to be optimized (the residual) given one or more measured profiles, one or more profile generators and variables to be steered by an optimizer.
  2. constraints and restraints that encapsulate known information about the system.
  3. a clearly defined programming interface that developers can use to add their own profile generators, thereby enabling the combination of more views of a system.
  4. an equation building interface that allows users to tweak profile generators when scientific understanding of a system is more advanced than the existing software.

The examples described below will go into detail about each of these points.


The following examples are contained in the doc/examples/ directory of the SrFit source distribution. They can be downloaded from http://dev.danse.us/packages/srfit_examples-alpha9.zip

For each example, start by running the example by typing in the command line

python example.py

where example.py represents the example file. The output will show on screen and a plot window will display. Once you’ve studied the output and plot, close the plot window and open the example file. In the file there will be a description of what the script is doing and the purpose of the example. By reading through the example, you will be introduced to the SrFit programming interface.

Here is an overview of the examples. It is recommended that you work through them in the order listed below.


  • gaussianrecipe.py
    Introductory recipe building and configuration. This introduces the fundamental classes in SrFit.
  • debyemodel.py
    Introductory recipe building and configuration. This shows how to use a function created by someone else in a refinement. This example also introduces restraints.
  • debyemodelII.py
    Refine two different values of a variable from two different regions of a profile. This example introduces constraints and working with multiple contributions to a fit.


  • gaussiangenerator.py
    Create a custom ProfileGenerator and use it in a refinement. This is an instructive extension to gaussianrecipe.py.
  • npintensity.py
    Use diffpy.structure to build a nanoparticle intensity generator, and use it to refine a structure to simulated data.
  • npintensityII.py
    Use the calculator built in npintensity.py to simultaneously refine a structure to two data sets.

Use Cases

There are several examples that demonstrate various SrFit use cases. These do not adopt the tutorial format of the previous examples. Regardless, developers should read through these use cases to gain an understanding of PDF and SAS refinement with SrFit.


These demonstrate other SrFit features. These are in flux and may not be in future versions.

  • simplerecipe.py
    This introduces the SimpleRecipe class that is a FitRecipe with an embedded Profile and FitContribution. SimpleRecipe exposes methods from the Profile and FitContribution, and adds other methods so it is easy to set up a simple fit.
  • interface.py
    This example introduces some interface enhancements that allow the SrFit recipes to be written with less code. This is not the same as a full-featured scripting interface, but rather somewhere in between a scripting interface and the API.