def test_asArray_N_x_Dim(self): true = np.array([[1,2,3]]) listArray = asArray_N_x_Dim([1,2,3],3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) listArray = asArray_N_x_Dim(np.r_[1,2,3],3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) listArray = asArray_N_x_Dim(np.array([[1,2,3.]]),3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) true = np.array([[1,2],[4,5]]) listArray = asArray_N_x_Dim([[1,2],[4,5]],2) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape)
def test_asArray_N_x_Dim(self): true = np.array([[1, 2, 3]]) listArray = asArray_N_x_Dim([1, 2, 3], 3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) listArray = asArray_N_x_Dim(np.r_[1, 2, 3], 3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) listArray = asArray_N_x_Dim(np.array([[1, 2, 3.]]), 3) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape) true = np.array([[1, 2], [4, 5]]) listArray = asArray_N_x_Dim([[1, 2], [4, 5]], 2) self.assertTrue(np.all(true == listArray)) self.assertTrue(true.shape == listArray.shape)
def closestPointsGrid(grid, pts, dim=2): """Move a list of points to the closest points on a grid. :param numpy.ndarray pts: Points to move :rtype: numpy.ndarray :return: nodeInds """ pts = asArray_N_x_Dim(pts, dim) nodeInds = np.empty(pts.shape[0], dtype=int) for i, pt in enumerate(pts): if dim == 1: nodeInds[i] = ((pt - grid)**2.).argmin() else: nodeInds[i] = ((np.tile(pt, (grid.shape[0], 1)) - grid)**2.).sum(axis=1).argmin() return nodeInds
def closestPointsGrid(grid, pts, dim=2): """Move a list of points to the closest points on a grid. :param numpy.ndarray pts: Points to move :rtype: numpy.ndarray :return: nodeInds """ pts = asArray_N_x_Dim(pts, dim) nodeInds = np.empty(pts.shape[0], dtype=int) for i, pt in enumerate(pts): if dim == 1: nodeInds[i] = ((pt - grid)**2.).argmin() else: nodeInds[i] = ( (np.tile( pt, (grid.shape[0], 1)) - grid)**2.).sum(axis=1).argmin() return nodeInds