mailRe: r26812 - /branches/nmrglue/lib/software/nmrglue.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by Edward d'Auvergne on November 28, 2014 - 22:42:
Interesting!  I would recommend that you consider a new module in the
target_functions package for this new Gaussian fitting target function
(using 'svn cp lib/software/nmrglue.py target_functions/gaussian.py').
It will simplify your optimisation if you can initialise a special
class with the required input data, convert these func_*() functions
into class methods, and then pass the class methods into minfx.
You'll benefit from the superior design and greater abstraction if you
need to expand on this.  You'll also quickly find out why this current
design will not work well with the design of optimisation algorithms
;)  The class and __init__() method for temporary data storage will be
the solution you need.

Regards,

Edward



On 28 November 2014 at 22:33,  <tlinnet@xxxxxxxxxxxxx> wrote:
Author: tlinnet
Date: Fri Nov 28 22:33:10 2014
New Revision: 26812

URL: http://svn.gna.org/viewcvs/relax?rev=26812&view=rev
Log:
Initial implementation of a gauss function and its chi2 function, to 
prepare for minfx minimisation.

Task #7873 (https://gna.org/task/index.php?7873): Write wrapper function to 
nmrglue, to read .ft2 files and process them.
Homepage: http://www.nmrglue.com/
Link to nmrglue discussion: 
https://groups.google.com/forum/#!forum/nmrglue-discuss
The code is develop at Github: https://github.com/jjhelmus/nmrglue/
Google code: https://code.google.com/p/nmrglue/
Documentation: http://nmrglue.readthedocs.org/en/latest/index.html

Modified:
    branches/nmrglue/lib/software/nmrglue.py

Modified: branches/nmrglue/lib/software/nmrglue.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/nmrglue/lib/software/nmrglue.py?rev=26812&r1=26811&r2=26812&view=diff
==============================================================================
--- branches/nmrglue/lib/software/nmrglue.py    (original)
+++ branches/nmrglue/lib/software/nmrglue.py    Fri Nov 28 22:33:10 2014
@@ -23,7 +23,7 @@
 """Module for the wrapper functions around the nmrglue module."""

 # Python module imports.
-from numpy import arange, argmax, exp, log, pi, sqrt
+from numpy import arange, argmax, exp, log, ones, pi, sqrt
 import matplotlib.pyplot as plt
 import matplotlib.cm

@@ -32,6 +32,7 @@
 from lib.errors import RelaxError
 from lib.io import get_file_path
 from lib.spectrum.objects import Nmrglue_data
+from target_functions.chi2 import chi2_rankN


 def contour_plot(spectrum_id=None, contour_start=30000., contour_num=20, 
contour_factor=1.20, ppm=True, show=False):
@@ -109,6 +110,51 @@
     return ax


+def func_gauss(params=None, x=None):
+    """Calculate the Gaussian distribution for a given x value.
+
+    @param params:  The vector of parameter values.
+    @type params:   numpy rank-1 float array
+    @keyword x:     The x value to calculate the probability for.
+    @type x:        numpy array
+    @return:        The probability corresponding to x.
+    @rtype:         float
+    """
+
+    # Unpack,
+    # a: The amplitude of the distribution.
+    # x0: The center of the distribution.
+    # sigma: The standard deviation of the distribution.
+    a, x0, sigma = params
+
+    # Calculate and return the probability.
+    return a*exp(-(x-x0)**2/(2*sigma**2))
+
+
+def func_gauss_chi2(params=None, x=None, values=None):
+    """Target function for minimising chi2 in minfx, for Gaussian function 
fit.
+
+    @param params:  The vector of parameter values.
+    @type params:   numpy rank-1 float array
+    @keyword x:     The x points.
+    @type x:        numpy array
+    @param values:  The measured y values.
+    @type values:   numpy array
+    @return:        The chi2 value.
+    @rtype:         float
+    """
+
+    # Calculate.
+    back_calc = func_gauss(params=params, x=x)
+
+    # Return the total chi-squared value.
+    errors = ones(values.shape)
+    chi2 = chi2_rankN(data=values, back_calc_vals=back_calc, errors=errors)
+
+    # Calculate and return the chi-squared value.
+    return chi2
+
+
 def hist_plot(ndarray=None, hist_kwargs=None, show=False):
     """Flatten the 2D numpy array, and plot as histogram.

@@ -178,13 +224,10 @@
     #ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data", va="center", 
ha="center", bbox=dict(boxstyle="round", fc="w"))
     ax.annotate("FWHM=%3.2f std=%3.2f"%(fwhm, fwhm_std), xy=(x0, hm), 
xycoords="data", size=8, va="center", horizontalalignment="center", 
bbox=dict(boxstyle="round", facecolor="w"))

-    # Plot the gaussian function.
-    a = amp
+    # Calculate the gauss values.
+    params = amp, x0, fwhm_std
+    gauss = func_gauss(params=params, x=bincenters)
     sigma = fwhm_std
-    x = bincenters
-
-    # Calculate the gauss values.
-    gauss = a*exp(-(x-x0)**2/(2*sigma**2))

     # Plot the values.
     ax.plot(bincenters, gauss, 'r-', label='gauss')


_______________________________________________
relax (http://www.nmr-relax.com)

This is the relax-commits mailing list
relax-commits@xxxxxxx

To unsubscribe from this list, get a password
reminder, or change your subscription options,
visit the list information page at
https://mail.gna.org/listinfo/relax-commits



Related Messages


Powered by MHonArc, Updated Sat Nov 29 09:40:16 2014