def create_points_grid(grid_limits, n_grid_points): """Creates a grid of points. Parameters ---------- grid_limits : list of tuple List with a tuple of min/max limits for each axis. If None, [(0, 1), (0, 1)] limits will be used. n_grid_points : int Number of grid points. """ grid_bounds = [(0, 1), (0, 1)] if grid_limits is None else grid_limits x_min, x_max = (grid_bounds[0][0], grid_bounds[0][1]) y_min, y_max = (grid_bounds[1][0], grid_bounds[1][1]) # Padding x and y grid points padding_x, padding_y = (0.05 * (x_max - x_min), 0.05 * (y_max - y_min)) # Create the equi-spaced indices for each axis x_grid_points = CArray.linspace(x_min - padding_x, x_max + padding_x, num=n_grid_points) y_grid_points = CArray.linspace(y_min - padding_y, y_max + padding_y, num=n_grid_points) # Create the grid pad_xgrid, pad_ygrid = CArray.meshgrid((x_grid_points, y_grid_points)) pad_grid_point_features = CArray.concatenate(pad_xgrid.reshape( (pad_xgrid.size, 1)), pad_ygrid.reshape( (pad_ygrid.size, 1)), axis=1) return pad_grid_point_features, pad_xgrid, pad_ygrid
def _concat_allaxis(array1, array2): self.logger.info("a1: {:} ".format(array1)) self.logger.info("a2: {:} ".format(array2)) # check concatenate, axis None (ravelled) concat_res = CArray.concatenate(array1, array2, axis=None) self.logger.info("concat(a1, a2): {:}".format(concat_res)) # If axis is None, result should be ravelled... if array1.isdense: self.assertEqual(1, concat_res.ndim) else: # ... but if array is sparse let's check for shape[0] self.assertEqual(1, concat_res.shape[0]) # Let's check the elements of the resulting array a1_comp = array1.todense().ravel() a2_comp = array2.todense().ravel() if array1.issparse: # result will be sparse, so always 2d a1_comp = a1_comp.atleast_2d() a2_comp = a2_comp.atleast_2d() self.assert_array_equal(concat_res[:array1.size], a1_comp) self.assert_array_equal(concat_res[array1.size:], a2_comp) array1_shape0 = array1.atleast_2d().shape[0] array1_shape1 = array1.atleast_2d().shape[1] array2_shape0 = array2.atleast_2d().shape[0] array2_shape1 = array2.atleast_2d().shape[1] # check append on axis 0 (vertical) concat_res = CArray.concatenate(array1, array2, axis=0) self.logger.info("concat(a1, a2, axis=0): {:}".format(concat_res)) self.assertEqual(array1_shape1, concat_res.shape[1]) self.assertEqual(array1_shape0 + array2_shape0, concat_res.shape[0]) self.assert_array_equal(concat_res[:array1_shape0, :], array1) self.assert_array_equal(concat_res[array1_shape0:, :], array2) # check append on axis 1 (horizontal) concat_res = CArray.concatenate(array1, array2, axis=1) self.logger.info("concat(a1, a2, axis=1): {:}".format(concat_res)) self.assertEqual(array1_shape1 + array2_shape1, concat_res.shape[1]) self.assertEqual(array1_shape0, concat_res.shape[0]) self.assert_array_equal(concat_res[:, :array1_shape1], array1) self.assert_array_equal(concat_res[:, array1_shape1:], array2)
def _grad(self, x): """Rosenbrock function gradient wrt. point x. Gradient available for 2-Dimensional points only. """ x = x.atleast_2d() if x.shape[1] != 2: raise ValueError("Gradient of Rosenbrock function " "only available for 2 dimensions") # Computing gradient of each dimension grad1 = -400 * (x[1] - x[0] ** 2) * x[0] + 2 * (x[0] - 1) grad2 = 200 * (x[1] - x[0] ** 2) return CArray.concatenate(grad1, grad2, axis=1).ravel()
def _grad(self, x): """Three-Hump Camel function gradient wrt. point x.""" x = x.atleast_2d() if x.shape[1] != 2: raise ValueError("Gradient of Three-Hump Camel function " "only available for 2 dimensions") # Computing gradient of each dimension grad1_1 = 4 * x[0] - 4.2 * x[0] ** 3 grad1_2 = x[0] ** 5 + x[1] grad2_1 = 0 grad2_2 = x[0] + 2 * x[1] grad1 = grad1_1 + grad1_2 grad2 = grad2_1 + grad2_2 return CArray.concatenate(grad1, grad2, axis=1).ravel()
def _grad(self, x): """McCormick function gradient wrt. point x.""" x = x.atleast_2d() if x.shape[1] != 2: raise ValueError("Gradient of McCormick function " "only available for 2 dimensions") # Computing gradient of each dimension grad1_1 = (x[0] + x[1]).cos() grad1_2 = 2 * (x[0] - x[1]) grad1_3 = -1.5 grad2_1 = (x[0] + x[1]).cos() grad2_2 = -2 * (x[0] - x[1]) grad2_3 = 2.5 grad1 = grad1_1 + grad1_2 + grad1_3 grad2 = grad2_1 + grad2_2 + grad2_3 return CArray.concatenate(grad1, grad2, axis=1).ravel()
def _grad(self, x): """Beale function gradient wrt. point x.""" x = x.atleast_2d() if x.shape[1] != 2: raise ValueError("Gradient of Beale function " "only available for 2 dimensions") # Computing gradient of each dimension grad1_1 = 2 * (1.5 - x[0] + x[0] * x[1]) * (-1 + x[1]) grad1_2 = 2 * (2.25 - x[0] + x[0] * x[1]**2) * (-1 + x[1]**2) grad1_3 = 2 * (2.625 - x[0] + x[0] * x[1]**3) * (-1 + x[1]**3) grad2_1 = 2 * (1.5 - x[0] + x[0] * x[1]) * x[0] grad2_2 = 2 * (2.25 - x[0] + x[0] * x[1]**2) * (2 * x[0] * x[1]) grad2_3 = 2 * (2.625 - x[0] + x[0] * x[1] ** 3) * \ (3 * x[0] * x[1] ** 2) grad1 = grad1_1 + grad1_2 + grad1_3 grad2 = grad2_1 + grad2_2 + grad2_3 return CArray.concatenate(grad1, grad2, axis=1).ravel()
def test_input_shape(self): """Test CArray.input_shape behavior.""" array = CArray([[[2, 3], [22, 33]], [[4, 5], [44, 55]]]) array_s = \ CArray([[[2, 3], [22, 33]], [[4, 5], [44, 55]]], tosparse=True) ref_shape = (2, 2, 2) # not propagate on getitem (as it returns new objects) out = array[0:2, 0:2] self.assertEqual(out.input_shape, out.shape) out = array_s[0:2, 0:2] self.assertEqual(out.input_shape, out.shape) # not propagate on other generic methods (as they return new objects) out = array.astype(float) self.assertEqual(out.input_shape, out.shape) out = array.unique() self.assertEqual(out.input_shape, out.shape) out = array.all(axis=0) self.assertEqual(out.input_shape, out.shape) # not propagate on classmethods (es. concatenate/append) out = CArray.concatenate(array, array, axis=0) self.assertEqual(out.input_shape, out.shape) out = CArray.concatenate(array, array, axis=None) self.assertEqual(out.input_shape, out.shape) # should propagate on copy/deepcopy from copy import copy, deepcopy array_c = copy(array) self.assertEqual(array_c.input_shape, ref_shape) array_c = copy(array_s) self.assertEqual(array_c.input_shape, ref_shape) array_c = deepcopy(array) self.assertEqual(array_c.input_shape, ref_shape) array_c = deepcopy(array_s) self.assertEqual(array_c.input_shape, ref_shape) array_c = array.deepcopy() self.assertEqual(array_c.input_shape, ref_shape) array_c = array_s.deepcopy() self.assertEqual(array_c.input_shape, ref_shape) # should propagate on setitem array_c = array.deepcopy() array_c[0:2, 0:2] = 200 self.assertEqual(array_c.input_shape, ref_shape) array_c = array.deepcopy() array_c[0:2, 0:2] = CArray([[100, 200], [300, 400]]) self.assertEqual(array_c.input_shape, ref_shape) array_c = array_s.deepcopy() array_c[0:2, 0:2] = CArray([[100, 200], [300, 400]]) self.assertEqual(array_c.input_shape, ref_shape) # should propagate on todense/tosparse self.assertEqual(array.tosparse().input_shape, ref_shape) self.assertEqual(array.todense().input_shape, ref_shape) self.assertEqual(array_s.tosparse().input_shape, ref_shape) self.assertEqual(array_s.todense().input_shape, ref_shape)
def test_concatenate(self): """Test for CArray.concatenate() method.""" self.logger.info("Test for CArray.concatenate() method.") def _concat_allaxis(array1, array2): self.logger.info("a1: {:} ".format(array1)) self.logger.info("a2: {:} ".format(array2)) # check concatenate, axis None (ravelled) concat_res = CArray.concatenate(array1, array2, axis=None) self.logger.info("concat(a1, a2): {:}".format(concat_res)) # If axis is None, result should be ravelled... if array1.isdense: self.assertEqual(1, concat_res.ndim) else: # ... but if array is sparse let's check for shape[0] self.assertEqual(1, concat_res.shape[0]) # Let's check the elements of the resulting array a1_comp = array1.todense().ravel() a2_comp = array2.todense().ravel() if array1.issparse: # result will be sparse, so always 2d a1_comp = a1_comp.atleast_2d() a2_comp = a2_comp.atleast_2d() self.assert_array_equal(concat_res[:array1.size], a1_comp) self.assert_array_equal(concat_res[array1.size:], a2_comp) array1_shape0 = array1.atleast_2d().shape[0] array1_shape1 = array1.atleast_2d().shape[1] array2_shape0 = array2.atleast_2d().shape[0] array2_shape1 = array2.atleast_2d().shape[1] # check append on axis 0 (vertical) concat_res = CArray.concatenate(array1, array2, axis=0) self.logger.info("concat(a1, a2, axis=0): {:}".format(concat_res)) self.assertEqual(array1_shape1, concat_res.shape[1]) self.assertEqual(array1_shape0 + array2_shape0, concat_res.shape[0]) self.assert_array_equal(concat_res[:array1_shape0, :], array1) self.assert_array_equal(concat_res[array1_shape0:, :], array2) # check append on axis 1 (horizontal) concat_res = CArray.concatenate(array1, array2, axis=1) self.logger.info("concat(a1, a2, axis=1): {:}".format(concat_res)) self.assertEqual(array1_shape1 + array2_shape1, concat_res.shape[1]) self.assertEqual(array1_shape0, concat_res.shape[0]) self.assert_array_equal(concat_res[:, :array1_shape1], array1) self.assert_array_equal(concat_res[:, array1_shape1:], array2) _concat_allaxis(self.array_dense, self.array_dense) _concat_allaxis(self.array_sparse, self.array_sparse) _concat_allaxis(self.array_sparse, self.array_dense) _concat_allaxis(self.array_dense, self.array_sparse) # Test for #767 append does not work when one of the arrays is not csr self.array_sparse._data._data = self.array_sparse._data._data.todok() _concat_allaxis(self.array_sparse, self.array_sparse) self.array_sparse._data._data = self.array_sparse._data._data.tocsr() # check concat on empty arrays empty_sparse = CArray([], tosparse=True) empty_dense = CArray([], tosparse=False) self.assertTrue((CArray.concatenate(empty_sparse, empty_dense, axis=None) == empty_dense).all()) self.assertTrue((CArray.concatenate(empty_sparse, empty_dense, axis=0) == empty_dense).all()) self.assertTrue((CArray.concatenate(empty_sparse, empty_dense, axis=1) == empty_dense).all())