def test_numarray_cvx_conversions(): _1_10_ = numarray.array( range(1,11) ) test_cases = [ _1_10_, numarray.array([ _1_10_ ]), numarray.transpose( numarray.array([ _1_10_ ]) ), numarray.array([ _1_10_, range(11, 21) ]) ] for t, a in enumerate(test_cases): print "-"*80, '\nTest Case %d\n'%(t+1), "-"*80 print "NumArray matrix shaped", a.shape print a a_cvx = numarray_to_cvx(a) print "CVX matrix shaped", a_cvx.size print a_cvx a = numarray_asmatrix(a) a_numarray = cvx_to_numarray(a_cvx) m, n = a.shape assert (m,n)==a_cvx.size, \ "(m,n) = (%d,%d) != %s = a_cvx.size"%(m, n, a_cvx.size) assert (m,n)==a_numarray.shape, \ "(m,n) = (%d,%d) != %s = a_numarray.size"%(m, n, a_numarray.shape) for i in range(m): for j in range(n): assert a[i,j] == a_cvx[i,j], "a[%d,%d] = %f != %f = a_cvx[%d,%d]"%( i, j, a[i,j], a_cvx[i,j], i, j ) assert a[i,j] == a_numarray[i,j], "a[%d,%d] = %f != %f = a_numarray[%d,%d]"%( i, j, a[i,j], a_numarray[i,j], i, j ) print
def test_numarray_cvx_conversions(): _1_10_ = numarray.array(range(1, 11)) test_cases = [ _1_10_, numarray.array([_1_10_]), numarray.transpose(numarray.array([_1_10_])), numarray.array([_1_10_, range(11, 21)]) ] for t, a in enumerate(test_cases): print "-" * 80, '\nTest Case %d\n' % (t + 1), "-" * 80 print "NumArray matrix shaped", a.shape print a a_cvx = numarray_to_cvx(a) print "CVX matrix shaped", a_cvx.size print a_cvx a = numarray_asmatrix(a) a_numarray = cvx_to_numarray(a_cvx) m, n = a.shape assert (m,n)==a_cvx.size, \ "(m,n) = (%d,%d) != %s = a_cvx.size"%(m, n, a_cvx.size) assert (m,n)==a_numarray.shape, \ "(m,n) = (%d,%d) != %s = a_numarray.size"%(m, n, a_numarray.shape) for i in range(m): for j in range(n): assert a[i, j] == a_cvx[ i, j], "a[%d,%d] = %f != %f = a_cvx[%d,%d]" % ( i, j, a[i, j], a_cvx[i, j], i, j) assert a[i, j] == a_numarray[ i, j], "a[%d,%d] = %f != %f = a_numarray[%d,%d]" % ( i, j, a[i, j], a_numarray[i, j], i, j) print
def regular_xyval_to_2d_grid_values(xyval): """Returns (grid_values, x0, y0, deltax, deltay)""" xyval = numarray.array(xyval) n = len(xyval) x = xyval[:, 0] y = xyval[:, 1] values = xyval[:, 2:].copy() # print "type(values)",type(values) valsize = numarray.size(values, 1) x0 = x[0] y0 = y[0] k = 1 if x[1] == x0: deltay = y[1] - y[0] while x[k] == x0: k = k + 1 deltax = x[k] - x0 ny = k nx = n // ny # print 'A) nx,ny:',nx,ny values.shape = (nx, ny, valsize) # print "A type(values)",type(values) values = numarray.transpose(values, (1, 0, 2)) # print "B type(values)",type(values) elif y[1] == y0: deltax = x[1] - x[0] while y[k] == y0: k = k + 1 deltay = y[k] - y0 nx = k ny = n // nx # print 'B) nx,ny:',nx,ny values.shape = (ny, nx, valsize) # print "C type(values)",type(values) values = numarray.transpose(values, (1, 0, 2)) # print "D type(values)",type(values) else: raise ValueError( "Strange: x[1]!=x0 and y[1]!=y0 this doesn't look like a regular grid..." ) print 'In regular_xyval_to_2d_grid_values: ', type(xyval), type(values) return values, x0, y0, deltax, deltay
def regular_xyval_to_2d_grid_values(xyval): """Returns (grid_values, x0, y0, deltax, deltay)""" xyval = numarray.array(xyval) n = len(xyval) x = xyval[:,0] y = xyval[:,1] values = xyval[:,2:].copy() # print "type(values)",type(values) valsize = numarray.size(values,1) x0 = x[0] y0 = y[0] k = 1 if x[1]==x0: deltay = y[1]-y[0] while x[k]==x0: k = k+1 deltax = x[k]-x0 ny = k nx = n // ny # print 'A) nx,ny:',nx,ny values.shape = (nx,ny,valsize) # print "A type(values)",type(values) values = numarray.transpose(values,(1,0,2)) # print "B type(values)",type(values) elif y[1]==y0: deltax = x[1]-x[0] while y[k]==y0: k = k+1 deltay = y[k]-y0 nx = k ny = n // nx # print 'B) nx,ny:',nx,ny values.shape = (ny,nx,valsize) # print "C type(values)",type(values) values = numarray.transpose(values,(1,0,2)) # print "D type(values)",type(values) else: raise ValueError("Strange: x[1]!=x0 and y[1]!=y0 this doesn't look like a regular grid...") print 'In regular_xyval_to_2d_grid_values: ', type(xyval), type(values) return values, x0, y0, deltax, deltay
def update(self, arr): """Update the accumulators of the StatsCollector given a complete matrix; assume that all observations have the same weight. Properly handle missing values. """ assert self.width() == arr.shape[1] #shape(arr)[1] i = 0 ## Update number of elements counters (length, width) = arr.shape #shape(arr) initial_n = self.n.copy( ) #self.n[:] # Keep old n for argmin/argmax n = zeros(width) + length missings = isnan(arr) nnan = sum(missings, 0) self.n += n self.nnan += nnan self.nnonnan += n - nnan ## Create masked version of arr and update accumulators ma = masked_array(arr, mask=missings) # Here, mask missings only arr_nomissings = arr[~normal_sometrue(missings, 1)] # Here, strip missing rows self.sum = self.sum + sum(ma, 0) # += does not work... self.sum_ssq = self.sum_ssq + sum(ma * ma, 0) # += does not work... self.sum_xxt = self.sum_xxt + matrixmultiply(transpose(arr_nomissings), arr_nomissings) self.sum_nomi = self.sum_nomi + sum(arr_nomissings, 0) self.nxxt += arr_nomissings.shape[0] #shape(arr_nomissings)[0] ## Update (arg)min / make sure old argmin is kept if not updated ma_argmin = argmin(ma, 0) ma_min = ma[ma_argmin, range(width)] min_newpos = argmin(array([self.min, ma_min]), 0).astype('Bool') self.min[min_newpos] = ma_min[min_newpos] # XXX Argmin computation needs to be revised! Does not work, at least # when passing array of shape (1,1). self.argmin[min_newpos] = ma_argmin[min_newpos] + initial_n[min_newpos] ## Update (arg)max / make sure old argmax is kept if not updated ma_argmax = argmax(ma, 0) ma_max = ma[ma_argmax, range(width)] max_newpos = argmax(array([self.max, ma_max]), 0).astype('Bool') self.max[max_newpos] = ma_max[max_newpos] # XXX Argmax computation needs to be revised! Does not work, at least # when passing array of shape (1,1). Also, is the use of min_newpos # correct? self.argmax[max_newpos] = ma_argmax[max_newpos] + initial_n[min_newpos]
def numarray_to_cvx(*args): """Returns CVX matrices from NumArray matrices Note that NumArray one-dimensional arrays are transformed as row vectors in CVX. See/Run test_numarray_cvx_conversions() for details. """ matrices = [] for a in args: a = numarray_asmatrix(a) m, n = a.shape a = numarray.ravel( numarray.transpose(a) ) matrices.append( cvx.matrix(a, (m,n), 'd') ) if len(matrices)==1: return matrices[0] return matrices
def numarray_to_cvx(*args): """Returns CVX matrices from NumArray matrices Note that NumArray one-dimensional arrays are transformed as row vectors in CVX. See/Run test_numarray_cvx_conversions() for details. """ matrices = [] for a in args: a = numarray_asmatrix(a) m, n = a.shape a = numarray.ravel(numarray.transpose(a)) matrices.append(cvx.matrix(a, (m, n), 'd')) if len(matrices) == 1: return matrices[0] return matrices
def update(self, arr): """Update the accumulators of the StatsCollector given a complete matrix; assume that all observations have the same weight. Properly handle missing values. """ assert self.width() == arr.shape[1] #shape(arr)[1] i = 0 ## Update number of elements counters (length,width)= arr.shape #shape(arr) initial_n = self.n.copy()#self.n[:] # Keep old n for argmin/argmax n = zeros(width) + length missings = isnan(arr) nnan = sum(missings,0) self.n += n self.nnan += nnan self.nnonnan += n - nnan ## Create masked version of arr and update accumulators ma = masked_array(arr, mask=missings) # Here, mask missings only arr_nomissings = arr[~normal_sometrue(missings,1)] # Here, strip missing rows self.sum = self.sum + sum(ma,0) # += does not work... self.sum_ssq = self.sum_ssq + sum(ma*ma,0) # += does not work... self.sum_xxt = self.sum_xxt + matrixmultiply(transpose(arr_nomissings), arr_nomissings) self.sum_nomi= self.sum_nomi + sum(arr_nomissings,0) self.nxxt += arr_nomissings.shape[0] #shape(arr_nomissings)[0] ## Update (arg)min / make sure old argmin is kept if not updated ma_argmin = argmin(ma,0) ma_min = ma[ma_argmin, range(width)] min_newpos = argmin(array([self.min, ma_min]), 0).astype('Bool') self.min[min_newpos] = ma_min[min_newpos] # XXX Argmin computation needs to be revised! Does not work, at least # when passing array of shape (1,1). self.argmin[min_newpos] = ma_argmin[min_newpos] + initial_n[min_newpos] ## Update (arg)max / make sure old argmax is kept if not updated ma_argmax = argmax(ma,0) ma_max = ma[ma_argmax, range(width)] max_newpos = argmax(array([self.max, ma_max]), 0).astype('Bool') self.max[max_newpos] = ma_max[max_newpos] # XXX Argmax computation needs to be revised! Does not work, at least # when passing array of shape (1,1). Also, is the use of min_newpos # correct? self.argmax[max_newpos] = ma_argmax[max_newpos] + initial_n[min_newpos]