mailRe: r23246 - in /branches/disp_speed: lib/dispersion/ns_cpmg_2site_expanded.py target_functions/relax_disp.py


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

Header


Content

Posted by Edward d'Auvergne on May 20, 2014 - 10:39:
Hi Troels,

As I mentioned previously (in the thread at
http://thread.gmane.org/gmane.science.nmr.relax.scm/20984), it is good
practice to separate the two changes here.  I.e. have one commit for
the API change, and one commit for the other changes.

Cheers,

Edward



On 20 May 2014 01:47,  <tlinnet@xxxxxxxxxxxxx> wrote:
Author: tlinnet
Date: Tue May 20 01:47:30 2014
New Revision: 23246

URL: http://svn.gna.org/viewcvs/relax?rev=23246&view=rev
Log:
Math-domain catching for model: 'NS CPMG 2-site expanded'.

task #7793: (https://gna.org/task/?7793) Speed-up of dispersion models.

This is to implement catching of math domain errors, before they occur.
These can be found via the --numpy-raise function to the systemtests.

To make the code look clean, the class object "back_calc" is no longer
being updated per time point, but is updated in the relax_disp target 
function in
one go.

Modified:
    branches/disp_speed/lib/dispersion/ns_cpmg_2site_expanded.py
    branches/disp_speed/target_functions/relax_disp.py

Modified: branches/disp_speed/lib/dispersion/ns_cpmg_2site_expanded.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/disp_speed/lib/dispersion/ns_cpmg_2site_expanded.py?rev=23246&r1=23245&r2=23246&view=diff
==============================================================================
--- branches/disp_speed/lib/dispersion/ns_cpmg_2site_expanded.py        
(original)
+++ branches/disp_speed/lib/dispersion/ns_cpmg_2site_expanded.py        Tue 
May 20 01:47:30 2014
@@ -235,14 +235,13 @@
 """

 # Python module imports.
-from math import log
-from numpy import exp, power, sqrt
+from numpy import array, exp, isfinite, power, log, min, sqrt, sum

 # relax module imports.
 from lib.float import isNaN


-def r2eff_ns_cpmg_2site_expanded(r20=None, pA=None, dw=None, k_AB=None, 
k_BA=None, relax_time=None, inv_relax_time=None, tcp=None, back_calc=None, 
num_points=None, num_cpmg=None):
+def r2eff_ns_cpmg_2site_expanded(r20=None, pA=None, dw=None, k_AB=None, 
k_BA=None, relax_time=None, inv_relax_time=None, tcp=None, num_points=None, 
num_cpmg=None):
     """The 2-site numerical solution to the Bloch-McConnell equation using 
complex conjugate matrices.

     This function calculates and stores the R2eff values.
@@ -264,9 +263,7 @@
     @type inv_relax_time:       float
     @keyword tcp:               The tau_CPMG times (1 / 4.nu1).
     @type tcp:                  numpy rank-1 float array
-    @keyword back_calc:         The array for holding the back calculated 
R2eff values.  Each element corresponds to one of the CPMG nu1 frequencies.
-    @type back_calc:            numpy rank-1 float array
-    @keyword num_points:        The number of points on the dispersion 
curve, equal to the length of the tcp and back_calc arguments.
+    @keyword num_points:        The number of points on the dispersion 
curve, equal to the length of the tcp .
     @type num_points:           int
     @keyword num_cpmg:          The array of numbers of CPMG blocks.
     @type num_cpmg:             numpy int16, rank-1 array
@@ -342,7 +339,15 @@
     t116 = power(0.5*(t97_t99 + t112), t115)
     t118 = 1.0/t112
     t120 = t97_nt99 + t112
-    t122 = power(0.5*(t97_t99 - t112), t115)
+
+    half_t97_t99_m_t112 = 0.5*(t97_t99 - t112)
+    # Catch math domain error of power(val < 1.e-7, 40).
+    # This is when abs(half_t97_t99_m_t112) < 1.e-7.
+    if min(abs(half_t97_t99_m_t112.real)) < 1.e-7:
+        R2eff = array([1e100]*num_points)
+        return R2eff
+
+    t122 = power(half_t97_t99_m_t112, t115)
     t127 = 0.5/t108
     t120_t122 = t120*t122
     t139 = 0.5/(k_AB + k_BA) * ((t120_t122 - t113*t116)*t118*k_BA + 
(t120_t122 - t116*t120)*t127*t113*t118*k_AB)
@@ -355,8 +360,11 @@
     Mx = intensity / intensity0

     # Calculate the R2eff using a two-point approximation, i.e. assuming 
that the decay is mono-exponential, and store it for each dispersion point.
-    for i in range(num_points):
-        if Mx[i] <= 0.0 or isNaN(Mx[i]):
-            back_calc[i] = 1e99
-        else:
-            back_calc[i]= -inv_relax_time * log(Mx[i])
+    R2eff = -inv_relax_time * log(Mx)
+
+    # Catch errors, taking a sum over array is the fastest way to check for
+    # +/- inf (infinity) and nan (not a number).
+    if not isfinite(sum(R2eff)) or min(Mx) <= 0.0 or not isfinite(sum(Mx)):
+        R2eff = array([1e100]*num_points)
+
+    return R2eff

Modified: branches/disp_speed/target_functions/relax_disp.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/disp_speed/target_functions/relax_disp.py?rev=23246&r1=23245&r2=23246&view=diff
==============================================================================
--- branches/disp_speed/target_functions/relax_disp.py  (original)
+++ branches/disp_speed/target_functions/relax_disp.py  Tue May 20 01:47:30 
2014
@@ -1480,7 +1480,7 @@
                 dw_frq = dw[si] * self.frqs[0][si][mi]

                 # Back calculate the R2eff values.
-                r2eff_ns_cpmg_2site_expanded(r20=R20[r20_index], pA=pA, 
dw=dw_frq, k_AB=k_AB, k_BA=k_BA, relax_time=self.relax_times[0][mi], 
inv_relax_time=self.inv_relax_times[0][mi], tcp=self.tau_cpmg[0][mi], 
back_calc=self.back_calc[0][si][mi][0], 
num_points=self.num_disp_points[0][si][mi][0], num_cpmg=self.power[0][mi])
+                self.back_calc[0][si][mi][0] = 
r2eff_ns_cpmg_2site_expanded(r20=R20[r20_index], pA=pA, dw=dw_frq, 
k_AB=k_AB, k_BA=k_BA, relax_time=self.relax_times[0][mi], 
inv_relax_time=self.inv_relax_times[0][mi], tcp=self.tau_cpmg[0][mi], 
num_points=self.num_disp_points[0][si][mi][0], num_cpmg=self.power[0][mi])

                 # For all missing data points, set the back-calculated 
value to the measured values so that it has no effect on the chi-squared 
value.
                 for di in range(self.num_disp_points[0][si][mi][0]):


_______________________________________________
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 Tue May 20 11:00:14 2014