00001 import numpy as np, math
00002 import numpy
00003 import math
00004
00005 import sys
00006
00007
00008
00009
00010 def bound(value, lower, upper):
00011 raise RuntimeError('math_util.bound moved to hrl_lib.util')
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 def bound_mat(m, lower, upper):
00022 if lower >= upper:
00023 t = lower
00024 lower = upper
00025 upper = t
00026
00027 m = m.copy()
00028 m[np.where(m > upper)] = upper
00029 m[np.where(m < lower)] = lower
00030 return m
00031
00032 def approx_equal(a, b, epsilon=.001):
00033 return (b < (a+epsilon)) and ((a-epsilon) < b)
00034
00035 def approx_equalv(a, b, epsilon=.001):
00036 return np.all(np.abs(a - b) < epsilon)
00037
00038 def radians(mat):
00039 return mat * (np.pi/180.0)
00040
00041 def degrees(mat):
00042 return mat * (180.0 / np.pi)
00043
00044
00045
00046
00047 def vec_of_ang(a):
00048 return numpy.matrix([numpy.cos(a), numpy.sin(a)]).T
00049
00050 def ang_of_vec(a):
00051 a = a / numpy.linalg.norm(a)
00052 return math.atan2(a[1,0], a[0,0])
00053
00054 def avg_ang(wa, a, wb, b):
00055 """
00056 Calculates the average between two angles
00057 wa weight of first angle
00058 a first angle
00059 wb weight of second angle
00060 b second angle
00061 """
00062 return ang_of_vec(wa * vec_of_ang(a) + wb * vec_of_ang(b))
00063
00064 def blend_ang(alpha, a, b):
00065 return avg_ang(alpha, a, 1-alpha,b)
00066
00067 def standard_rad(t):
00068 if t > 0:
00069 return ((t + numpy.pi) % (numpy.pi * 2)) - numpy.pi
00070 else:
00071 return ((t - numpy.pi) % (numpy.pi * -2)) + numpy.pi
00072
00073 def best_turn_dir(reference, new_angle):
00074 """ positive is left, negative is right! """
00075 return standard_rad(reference - new_angle)
00076
00077 def cart_of_pol(p):
00078 """ Finds cartesian coordinates of polar points [r, t]' """
00079 r = p[0,:]
00080 t = p[1,:]
00081 x = numpy.multiply(numpy.cos(t), r)
00082 y = numpy.multiply(numpy.sin(t), r)
00083 return numpy.vstack((x,y))
00084
00085 def pol_of_cart(p):
00086 """ Find polar coordinates of cartesian points [x, y]' """
00087 norm = numpy.linalg.norm(p)
00088 ang = math.atan2(p[1,0], p[0,0])
00089 return numpy.matrix([norm, ang]).T
00090
00091
00092
00093
00094
00095
00096
00097
00098 def argmax2d(mat):
00099 max_1d = np.argmax(mat)
00100 row_length = float(mat.shape[1])
00101 row_idx = np.floor(max_1d / row_length)
00102 col_idx = max_1d % row_length
00103 return row_idx, col_idx
00104
00105
00106
00107 def renormalize(npimage_gray):
00108 min_img = np.min(npimage_gray)
00109 max_img = np.max(npimage_gray)
00110 ret = np.matrix(np.round(((npimage_gray - min_img) / (max_img - min_img) * 255.0)), 'uint8')
00111 return ret
00112
00113 def list_mat_to_mat(list_mat, axis=0):
00114 return np.concatenate(tuple(list_mat), axis=axis)
00115
00116 def list_of_mat(mat):
00117 for i in range(mat.shape[1]):
00118 yield mat[:,i]
00119
00120 def nearest(mat, target):
00121 '''
00122 Return a sorted list of the nearest (euclidean dist) element
00123 of a matrix to a target value and their indeices.
00124 @param mat mxn
00125 @param target mx1
00126 '''
00127
00128
00129
00130
00131
00132
00133
00134 diff_vec = mat - target
00135 pwr = np.ones_like(mat[0])*2
00136 dist = np.power(np.sum(np.power(diff_vec, pwr),axis=0),0.5)
00137 indices = dist.argsort(axis=1)
00138
00139
00140
00141
00142
00143
00144
00145 return mat[:, indices.A1], indices
00146
00147 if __name__ == '__main__':
00148 import hrl_lib.util as ut
00149 import pdb
00150 mat = ut.load_pickle('mat.pkl')
00151 target = ut.load_pickle('target.pkl')
00152
00153 diff_vec = mat - target
00154 pwr = np.ones_like(mat[0])*2
00155 dist = np.power(np.sum(np.power(diff_vec, pwr),axis=0),0.5)
00156 sort_order = dist.argsort(axis=1)
00157
00158
00159 indices = np.concatenate((np.matrix(range(sort_order.shape[1])), sort_order), 0)[:, sort_order.A1]
00160
00161 print sort_order
00162 print indices
00163 pdb.set_trace()
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 def blend(alpha, a, b):
00183 return (alpha * a) + ((1-alpha) * b)
00184
00185 def approx_equal(a, b, epsilon=.001):
00186 return (b < (a+epsilon)) and ((a-epsilon) < b)
00187
00188 def norm(mat):
00189 """
00190 Calculate L2 norm for column vectors in a matrix
00191 """
00192 return np.power(np.sum(np.power(mat,2), axis=0), 0.5)
00193
00194 def rnd(v):
00195 return int(round(v))
00196
00197 def point_to_homo(p):
00198 """ Convert points into homogeneous coordinates """
00199 o = numpy.matrix(numpy.ones((1, p.shape[1])))
00200 return numpy.vstack((p,o))
00201
00202 """ Convert points back from homogeneous coordinates """
00203 def homo_to_point(p):
00204 return p[0:p.shape[0]-1,:]