mailr23604 - /trunk/lib/compat.py


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

Header


Content

Posted by edward on May 29, 2014 - 19:45:
Author: bugman
Date: Thu May 29 19:45:39 2014
New Revision: 23604

URL: http://svn.gna.org/viewcvs/relax?rev=23604&view=rev
Log:
Created the lib.compat.norm() compatibility function for numpy.linalg.norm().

For numpy 1.8 and higher, the numpy.linalg.norm() function has introduced the 
'axis' argument.  This
is an incredibly fast way of determining the norm of an array of vectors.  
This is used by the frame
order analysis.

However for older numpy versions, this causes the frame order analysis, and 
many corresponding
system and GUI tests to fail.  Therefore this new lib.compat.norm() function 
has been designed to
default to numpy.linalg.norm() if the axis argument is supported, or to 
switch to the much slower
numpy.apply_along_axis(numpy.linalg.norm, axis, x) call which is supported by 
older numpy.


Modified:
    trunk/lib/compat.py

Modified: trunk/lib/compat.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/lib/compat.py?rev=23604&r1=23603&r2=23604&view=diff
==============================================================================
--- trunk/lib/compat.py (original)
+++ trunk/lib/compat.py Thu May 29 19:45:39 2014
@@ -110,6 +110,14 @@
 else:
     import pickle
 
+# Numpy.
+import numpy
+try:
+    numpy.linalg.norm(numpy.ones((3,3)), axis=1)
+    numpy_norm_axis = True
+except:
+    numpy_norm_axis = False
+
 
 def bz2_open(file, mode='r'):
     """Abstract the numerous ways BZ2 files are handled in Python.
@@ -217,6 +225,25 @@
 
     # Return the file object.
     return file_obj
+
+
+def norm(x, ord=None, axis=None):
+    """Replacement numpy.linalg.norm() function to handle the axis argument 
for old numpy.
+    @param x:       Input array.  If `axis` is None, `x` must be 1-D or 2-D.
+    @type x:        array_like
+    @keyword ord:   Order of the norm (see table under ``Notes``). inf means 
numpy's `inf` object.
+    @type ord:      {non-zero int, inf, -inf, 'fro'}, optional
+    @keyword axis:  If `axis` is an integer, it specifies the axis of `x` 
along which to compute the vector norms.  If `axis` is a 2-tuple, it 
specifies the axes that hold 2-D matrices, and the matrix norms of these 
matrices are computed.  If `axis` is None then either a vector norm (when `x` 
is 1-D) or a matrix norm (when `x` is 2-D) is returned.
+    @type axis:     {int, 2-tuple of ints, None}, optional
+    """
+
+    # The axis argument exists.
+    if numpy_norm_axis:
+        return numpy.linalg.norm(x, ord=ord, axis=axis)
+
+    # Support for older version (this is much slower).
+    else:
+        return numpy.apply_along_axis(numpy.linalg.norm, axis, x)
 
 
 def sorted(data):




Related Messages


Powered by MHonArc, Updated Thu May 29 20:00:03 2014