Difference between revisions of "Tutorial for adding relaxation dispersion models to relax"

From relax wiki
Jump to navigation Jump to search
(→‎Reference commits: Dispersion model link fixes.)
(→‎Reference commits: Dispersion model link fixes.)
Line 219: Line 219:
 
=== Reference commits ===
 
=== Reference commits ===
  
* The [[NS 2-site expanded]] model at [http://article.gmane.org/gmane.science.nmr.relax.scm/18243 r20488]
+
* The [[NS CPMG 2-site expanded]] model at [http://article.gmane.org/gmane.science.nmr.relax.scm/18243 r20488]
 
 
  
 
== See also ==
 
== See also ==
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
[[Category:Relaxation dispersion analysis]]
 
[[Category:Relaxation dispersion analysis]]

Revision as of 21:22, 13 September 2014

See the mail archive for the original post.

The following is a tutorial for adding new relaxation dispersion models for either CPMG-type or R1rho-type experiments to the software relax. This includes both the models based on the analytic, closed-form expressions as well as the models involving numeric solutions of the Bloch-McConnell equations.

The tutorial will follow the example of the addition of the models already present within relax, pointing to the relevant commits for reference. To see the commit message and the code changes in colour, click on the links found within these commit messages. The models in the reference commits sections are in reverse chronological order and therefore the top links will be the most recent and relevant.


The test suite

This step is normally performed first. This is the most important part that makes sure that the code not only works now, but will continue working for the entire lifetime of the relax project.

The idea is that real or synthetic data, for example as Sparky peak lists, is obtained or created for the model and added to the test suite directory test_suite/shared_data/dispersion/. This is then used in a system test to check that the code in relax can consistently reproduce the results.

Synthetic data

It is very important that the code added to the relax library is not used to create the synthetic data! This type of data is useful for checking that the known solution can be found by relax. The only issue is that the same mistake can be made in both relax and the script used to generated the synthetic data, in which case the buggy relax code will never be detected. To mitigate against this, testing against other software is recommended.

Measured data

An alternative is to use real measured relaxation dispersion data. This data should be added as peak lists containing peak intensities to test_suite/shared_data/dispersion/. As the real solution cannot be known a priori, the results from relax must be compared to results obtained from another software program (possibly directly from a publication). The steps required for using such data are:

  • Create a new directory name for the test data.
  • Add the original full peak lists to the directory.
  • Make truncated versions of these files (ending in _trunc.*) and add these as well. These will be used for the system test instead of the full data to allow the test to finish in a reasonable amount of time.
  • Add a script which performs the full analysis in relax for the model. Also a script which performs the analysis using only the R2eff model. See the test_suite/shared_data/dispersion/Hansen/*.py scripts for reference - these scripts should be copied to your data directory and modified (using the svn cp command). Once the scripts are functional, they can be copied and modified for the truncated data (again using the svn cp command).
  • Copy the full analysis script to test_suite/system_tests/scripts/relax_disp/ with an appropriate name (always using the svn cp command). This can then be used in a new system test. Better still, the final save file from the r2eff_calc.py script for the truncated data can be used to start the script. This is again to save a lot of computation time in the test. See the test_tp02_data_to_ns_r1rho_2site() system test in the test_suite/system_tests/relax_disp.py file for a template.

If you are not a relax developer, a support request can be submitted. You can attach files and add comments to that request for a relax developer to make the changes for you.

Reference commits


Creating a new experiment type

If the model being added is for a completely new data type, then support for this must be added. In almost all cases, the experiment type will already be supported.

Reference commits


Adding the model to the list

Firstly the model should be added to the lists of the specific_analyses.relax_disp.variables module. The model name is stored in a special variable which will be used throughout relax.

Reference commits


The relax_disp.select_model user function front end

The next step is to add the model, its description, the equations for the analytic models, and all references to the relax_disp.select_model user function front end.


Reference commits

The relax_disp.select_model user function back end

Now the back end of the relax_disp.select_model user function for the model can be added. This involved identifying the model and constructing the parameter list.

Reference commits


Adding support for the parameters

This is needed to enable the model. It involves modifying many of the modules in the specific_analyses.relax_disp package.

Reference commits


The target function

The target function is used in optimisation and is a class method which takes as a single argument the parameter vector. This list is changed by the minimisation algorithm during optimisation. The target function should then return a single floating point number - the chi-squared value.

Again in this example, the code for the M61 is copied from the LM63 model and then modified.

Reference commits


The relax library

Now the dispersion function needs to be added to the relax library (in the lib.relax_disp package). This should be designed as a simple Python function which takes the dispersion parameters and experimental variables, and calculates the R2eff/R1rho values. The module can contain auxiliary functions for the calculation. Some auxiliary functions, if not specific to relaxation dispersion, may be better placed in other locations within the relax library. Remember to add all new modules to the lib.relax_disp.__init__ package __all__ list.

The relaxation dispersion functions in the library currently take as an argument a data structure for the back-calculated R2eff/R1rho values and populate this structure. This design is not essential if the target function, described in the next point, handles the library function appropriately. Just look at the files in lib/dispersion to get an idea of the design used.

The dispersion code in the relax library must be robust. This involves identifying parameter values or combinations which would cause failures in the mathematical operations (numerical issues not present in the mathematics must be considered). Note that parameter values of 0 are common within a grid search. It should be decided if the R2eff/R1rho value should be set to zero, to another value, or to something large (e.g. 1e100). For example:

Divisions - always catch zeros in the denominator with if statements, even if you believe that this will never be encountered. Square roots - make sure that the value inside is always > 0. Trigonometric functions - these should be tested for where they are not defined or where the software implementation can no longer handle certain values. For example try cosh(1000) in Python.

Reference commits


Comparing to other software

It can happen that a bug present in the lib.dispersion package code is also replicated in the synthetic data. This is not uncommon. Therefore it is very useful to use other software with the test data from the test-suite step to see if the original parameters can be found. A good example can be seen in the test_suite/shared_data/dispersion/Hansen which contains Dr. Flemming Hansen's CPMG data (see the README file) and the results from different programs including NESSY, relax, CPMGFit, and ShereKhan. The comparison is in the file 'software_comparison'.

Once the relax code is able to find identical or better results than the dispersion softwares, then the values found in the test suite optimisation can be locked in. The assertEqual() and assertAlmostEqual() methods can be used to only allow the test to pass when the correct values are found.


Debugging

This step should not require an explanation. It goes hand-in-hand with the test suite and the comparison to other software.


The auto-analysis

The model variable in specific_analyses.relax_disp.variables needs to be imported into the auto_analyses.relax_disp module. This is then used in the write_results() method to output text files and Grace plots of the parameters. Be sure that the model variable is added to each part of this method corresponding to the parameters of the model.

Reference commits


The GUI

The model needs to also be added to the graphical user interface (GUI). This is in the gui.analyses.auto_relax_disp module. The model variable should first be imported. In the __init__() method, it should be decided if the model should be selected by default or if the user should manually select the model during the analysis. If the former, then it should be added to the ds.relax_gui.analyses[data_index].disp_models list.

For the model to be accessible via the GUI, it must be added to the Disp_model_list_cpmg or Disp_model_list_r1rho model list classes (at the bottom of the module). The model variable should be added to the models list, and the list of parameters added to the params list.

Reference commits


The relax manual

The next step is to add the model, its description, the equations for the analytic models, and all references to the relaxation dispersion chapter of the relax manual (the source is the docs/latex/dispersion.tex file). The model could also be included in the script section of the chapter.

Reference commits

The sample scripts

If the added model is to be presented to the user, it should also be added to the sample scripts. This includes all scripts in the sample_scripts/relax_disp/ directory. For example it can be included in the MODELS list in the cpmg_analysis.py script.

Reference commits

See also