def test_matrix2cube(self): """Test matrix2cube.""" npt.assert_array_equal( transform.matrix2cube(self.matrix, self.cube[0].shape), self.cube, err_msg='Incorrect transformation: matrix2cube', )
def _op_method(self, input_data, extra_factor=1.0, rank=None): """Operator. This method returns the input data after the singular values have been thresholded. Parameters ---------- input_data : numpy.ndarray Input data array extra_factor : float Additional multiplication factor (default is ``1.0``) rank: int, optional Estimation of the rank to save computation time in standard mode, if not set an internal estimation is used. Returns ------- numpy.ndarray SVD thresholded data Raises ------ ValueError if lowr_type is not in ``{'standard', 'ngole'}`` """ # Update threshold with extra factor. threshold = self.thresh * extra_factor if self.lowr_type == 'standard' and self.rank is None and rank is None: data_matrix = svd_thresh( cube2matrix(input_data), threshold, thresh_type=self.thresh_type, ) elif self.lowr_type == 'standard': data_matrix, update_rank = svd_thresh_coef_fast( cube2matrix(input_data), threshold, n_vals=rank or self.rank, extra_vals=5, thresh_type=self.thresh_type, ) self.rank = update_rank # save for future use elif self.lowr_type == 'ngole': data_matrix = svd_thresh_coef( cube2matrix(input_data), self.operator, threshold, thresh_type=self.thresh_type, ) else: raise ValueError('lowr_type should be standard or ngole') # Return updated data. return matrix2cube(data_matrix, input_data.shape[1:])
def svd_thresh_coef(input_data, operator, threshold, thresh_type='hard'): """Threshold the singular values coefficients. This method thresholds the input data using singular value decomposition. Parameters ---------- input_data : numpy.ndarray Input data array, 2D matrix operator : class Operator class instance threshold : float or numpy.ndarray Threshold value(s) thresh_type : {'hard', 'soft'} Type of noise to be added (default is ``'hard'``) Returns ------- numpy.ndarray Thresholded data Raises ------ TypeError If operator not callable """ if not callable(operator): raise TypeError('Operator must be a callable function.') # Get SVD of data matrix u_vec, s_values, v_vec = calculate_svd(input_data) # Diagnalise s s_values = np.diag(s_values) # Compute coefficients a_matrix = np.dot(s_values, v_vec) # Get the shape of the array array_shape = np.repeat(np.int(np.sqrt(u_vec.shape[0])), 2) # Compute threshold matrix. ti = np.array([ np.linalg.norm(elem) for elem in operator(matrix2cube(u_vec, array_shape)) ]) threshold *= np.repeat(ti, a_matrix.shape[1]).reshape(a_matrix.shape) # Threshold coefficients. a_new = thresh(a_matrix, threshold, thresh_type) # Return the thresholded image. return np.dot(u_vec, a_new)
def svd_thresh_coef(data, operator, threshold, thresh_type='hard'): """Threshold the singular values coefficients This method thresholds the input data using singular value decomposition Parameters ---------- data : numpy.ndarray Input data array, 2D matrix operator : class Operator class instance threshold : float or numpy.ndarray Threshold value(s) threshold_type : {'hard', 'soft'} Type of noise to be added (default is 'hard') Returns ------- numpy.ndarray Thresholded data Raises ------ ValueError For invalid string entry for n_pc """ if not callable(operator): raise TypeError('Operator must be a callable function.') # Get SVD of data matrix u, s, v = calculate_svd(data) # Diagnalise s s = np.diag(s) # Compute coefficients a = np.dot(s, v) # Get the shape of the array array_shape = np.repeat(np.int(np.sqrt(u.shape[0])), 2) # Compute threshold matrix. ti = np.array( [np.linalg.norm(x) for x in operator(matrix2cube(u, array_shape))]) threshold *= np.repeat(ti, a.shape[1]).reshape(a.shape) # Threshold coefficients. a_new = thresh(a, threshold, thresh_type) # Return the thresholded image. return np.dot(u, a_new)
def _op_method(self, input_data, extra_factor=1.0): """Operator. This method returns the input data after the singular values have been thresholded. Parameters ---------- input_data : numpy.ndarray Input data array extra_factor : float Additional multiplication factor (default is ``1.0``) Returns ------- numpy.ndarray SVD thresholded data """ # Update threshold with extra factor. threshold = self.thresh * extra_factor if self.lowr_type == 'standard': data_matrix = svd_thresh( cube2matrix(input_data), threshold, thresh_type=self.thresh_type, ) elif self.lowr_type == 'ngole': data_matrix = svd_thresh_coef( cube2matrix(input_data), self.operator, threshold, thresh_type=self.thresh_type, ) # Return updated data. return matrix2cube(data_matrix, input_data.shape[1:])