util.py
Go to the documentation of this file.
00001 
00002 import os, sys, tty
00003 import numpy as np
00004 import cPickle as pk
00005 import time
00006 import threading
00007 import math
00008 
00009 from hrl_lib.msg import NumpyArray
00010 
00011 ## Returns a string that can be used as a timestamp (hours and minutes) in logfiles
00012 # @return timestamp-string
00013 def getTime():
00014     return '['+time.strftime("%H:%M:%S", time.localtime())+']'
00015 
00016 #copied from manipulation stack
00017 #angle between two quaternions (as lists)
00018 def quat_angle(quat1, quat2):
00019     dot = sum([x*y for (x,y) in zip(quat1, quat2)])
00020     if dot > 1.:
00021         dot = 1.
00022     if dot < -1.:
00023         dot = -1.
00024     angle = 2*math.acos(math.fabs(dot))
00025     return angle     
00026 
00027 def standard_rad(t):
00028     if t > 0:
00029         return ((t + np.pi) % (np.pi * 2))  - np.pi
00030     else:
00031         return ((t - np.pi) % (np.pi * -2)) + np.pi
00032 
00033 ##
00034 # Converts a list of numpy matrices to one large matrix
00035 # @param list_mat the list of little matrices
00036 # @param axis axis to concatenate little matrices
00037 # @return one large numpy matrix
00038 def list_mat_to_mat(list_mat, axis=0):
00039     return np.concatenate(tuple(list_mat), axis=axis)
00040 
00041 
00042 ## returns current time as a string: year|month|date_hours|min|sec.
00043 ## @return current time as a string: year|month|date_hours|min|sec.
00044 def formatted_time():
00045     date_name = time.strftime('%Y%h%d_%H%M%S', time.localtime())
00046 #    curtime = time.localtime()
00047 #    date_name = time.strftime('%Y%m%d_%I%M%S', curtime)
00048     return date_name
00049 
00050 ## read a pickle and return the object.
00051 # @param filename - name of the pkl
00052 # @return - object that had been pickled.
00053 def load_pickle(filename):
00054     try:
00055         p = open(filename, 'r')
00056     except IOError:
00057         print "hrl_lib.util: Pickle file cannot be opened."
00058         return None
00059     try:
00060         picklelicious = pk.load(p)
00061     except ValueError:
00062         print 'util.load_pickle failed once, trying again'
00063         p.close()
00064         p = open(filename, 'r')
00065         picklelicious = pk.load(p)
00066 
00067     p.close()
00068     return picklelicious
00069 
00070 ## Pickle an object.
00071 # @param object - object to be pickled
00072 # @param filename - name of the pkl file
00073 def save_pickle(object, filename):
00074     pickle_file = open(filename, 'w')
00075     pk.dump(object, pickle_file)
00076     pickle_file.close()
00077 
00078 ## Calculate L2 norm for column vectors in a matrix 
00079 # @param mat - numpy matrix
00080 def norm(mat):
00081     return np.power(np.sum(np.power(mat,2), axis=0), 0.5)
00082 
00083 def approx_equal(a, b, epsilon=.001):
00084     return (b < (a+epsilon)) and ((a-epsilon) < b)
00085 
00086 def unipolar_limit( x, upper ):
00087     """ limit the value of x such that
00088         0 <= x <= upper
00089     """
00090 
00091     if x > upper:
00092         x=upper
00093     if x < 0:
00094         x=0
00095 
00096     return x
00097 
00098 def cart_of_pol(p):
00099     """ Finds cartesian coordinates of polar points [r, t]' """
00100     r = p[0,:]
00101     t = p[1,:]
00102     x = numpy.multiply(numpy.cos(t), r)
00103     y = numpy.multiply(numpy.sin(t), r)
00104     return numpy.vstack((x,y))
00105 
00106 def pol_of_cart(p):
00107     """ Find polar coordinates of cartesian points [x, y]' """
00108     norm = numpy.linalg.norm(p)
00109     ang = math.atan2(p[1,0], p[0,0])
00110     return numpy.matrix([norm, ang]).T
00111 
00112 ##
00113 # Bound the value of a number to be above lower, and lower than upper
00114 # @return a number
00115 def bound(value, lower, upper):
00116     import rospy
00117     rospy.loginfo('hrl_lib.util.bound is DEPRECATED. Please use numpy.clip instead')
00118     if lower >= upper:
00119         t = lower
00120         lower = upper
00121         upper = t
00122 #    print 'bound', value, 'lower', lower, 'upper', upper
00123     #return min(max(value, lower), upper)
00124     ret_val = min(max(value, lower), upper)
00125 #    if ret_val != value:
00126 #        print 'ut.boud bounded something.'
00127     return ret_val
00128 
00129 
00130 
00131 ## wraps a numpy array into hrl's datatype for sending np arrays
00132 # over ros.
00133 # @param np array
00134 # @return NumpyArray object (hrl_lib/msg/NumpyArray.msg)
00135 def wrap_np_array(nparr):
00136     shp = nparr.shape
00137     npstr = nparr.tostring()
00138     npdtype = str(nparr.dtype)
00139     nparr_ros = NumpyArray(None,npstr,shp,npdtype)
00140     return nparr_ros
00141 
00142 ## convert hrl's ros wrapped numpy array to a numpy array
00143 # @param NumpyArray object (hrl_lib/msg/NumpyArray.msg)
00144 # @return np array
00145 def unwrap_np_array(nparr_ros):
00146     npstr,shp,npdtype = nparr_ros.data,nparr_ros.shape,nparr_ros.dtype
00147     nparr = np.fromstring(npstr,dtype=npdtype)
00148     nparr = nparr.reshape(shp)
00149     return nparr
00150 
00151 
00152 ## cartesian product of list of lists.
00153 # code copied from: http://automatthias.wordpress.com/2007/04/28/cartesian-product-of-multiple-sets/
00154 # @return generator. can loop over it, or list(generator) will give
00155 # the entire list.
00156 # NOTE - itertools in python 2.6 provides this functionality. We
00157 # should switch over to it soon.
00158 def cartesian_product(lists, previous_elements = []):
00159     if len(lists) == 1:
00160         for elem in lists[0]:
00161             yield previous_elements + [elem, ]
00162     else:
00163         for elem in lists[0]:
00164             for x in cartesian_product(lists[1:], previous_elements + [elem, ]):
00165                 yield x
00166 
00167 ## choose n elements from list without replacement.
00168 # adapted code from cartesian_product
00169 # @return generator.
00170 def choose_without_replacement(list, n):
00171     lists = [list for i in range(n)]
00172     return _choose_without_replacement(lists)
00173 
00174 def _choose_without_replacement(lists,previous_elements=[],ignore_count=0):
00175     if len(lists) == 1:
00176         for elem in lists[0][ignore_count:]:
00177             yield previous_elements + [elem, ]
00178     else:
00179         for i,elem in enumerate(lists[0][ignore_count:]):
00180             for x in _choose_without_replacement(lists[1:],previous_elements + [elem, ],ignore_count+i+1):
00181                 yield x
00182 
00183 ##
00184 # use festival text to speech to make a soud.
00185 # @param text - string to be said.
00186 def say(text):
00187     os.system( 'echo "' + text + '" | festival --tts' )
00188 
00189 
00190 ## compute rank of a matrix.
00191 # code copied from:
00192 # http://mail.scipy.org/pipermail/numpy-discussion/2008-February/031218.html
00193 def matrixrank(A,tol=1e-8):
00194     s = np.linalg.svd(A,compute_uv=0)
00195     return sum( np.where( s>tol, 1, 0 ) )
00196 
00197 
00198 ## raw_input + matplotlib + ROS == strangeness.
00199 # use this function instead.
00200 def get_keystroke(msg):
00201     print msg
00202     # clear out anythin in the buffer.
00203     tty.setraw(sys.stdin, tty.TCSAFLUSH)
00204     # next 4 lines copied from m3.toolbox from Meka Robotics.
00205     os.system('stty raw')
00206     r = sys.stdin.read(1)
00207     os.system('stty sane')
00208     return r
00209 
00210 ## execute a bash command and get its output as a list of strings.
00211 # e.g get_bash_command_output('ls -t')
00212 def get_bash_command_output(cmd):
00213     p = os.popen(cmd)
00214     output_l = [s.rstrip('\n') for s in p.readlines()]
00215     p.close()
00216     return output_l
00217 
00218 
00219 ## Returns the weighted average and standard deviation.
00220 # http://stackoverflow.com/questions/2413522/weighted-standard-deviation-in-numpy
00221 # values - n x m np MATRIX
00222 # weights - 1D np arrays of length n
00223 # returns - two 1D np arrays
00224 def weighted_avg_and_std(values, weights, unbiased):
00225     average = np.average(values, axis=0, weights=weights)
00226     variance = np.dot(weights, (values-average).A**2)/weights.sum()
00227     if unbiased:
00228         n = len(weights) * 1.
00229         variance = n / (n-1) * variance
00230     return average.A1, np.sqrt(variance)
00231 
00232 
00233 
00234 
00235 
00236 
00237 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06