def mex_hat(x, sigma): r"""Mexican hat This method implements a Mexican hat (or Ricker) wavelet. Parameters ---------- x : float Input data point sigma : float Standard deviation (filter scale) Returns ------- float Mexican hat filtered data point Examples -------- >>> from modopt.signal.filter import mex_hat >>> mex_hat(2, 1) -0.35213905225713371 """ x = check_float(x) sigma = check_float(sigma) xs = (x / sigma)**2 val = 2 * (3 * sigma)**-0.5 * np.pi**-0.25 return val * (1 - xs) * np.exp(-0.5 * xs)
def mex_hat_dir(x, y, sigma): r"""Directional Mexican hat This method implements a directional Mexican hat (or Ricker) wavelet. Parameters ---------- x : float Input data point for Gaussian y : float Input data point for Mexican hat sigma : float Standard deviation (filter scale) Returns ------- float directional Mexican hat filtered data point Examples -------- >>> from modopt.signal.filter import mex_hat_dir >>> mex_hat_dir(1, 2, 1) 0.17606952612856686 """ x = check_float(x) sigma = check_float(sigma) return -0.5 * (x / sigma)**2 * mex_hat(y, sigma)
def __init__(self, weights, thresh_factor=1.0, verbose=False): self.weights = check_float(weights) self.original_weights = np.copy(self.weights) self.thresh_factor = check_float(thresh_factor) self._rw_num = 1 self.verbose = verbose
def test_check_float(self): """Test check_float.""" npt.assert_array_equal( types.check_float(1.0), 1.0, err_msg='Float check failed', ) npt.assert_array_equal( types.check_float(1), 1.0, err_msg='Float check failed', ) npt.assert_array_equal( types.check_float(self.data1), self.data3, err_msg='Float check failed', ) npt.assert_array_equal( types.check_float(self.data2), self.data3, err_msg='Float check failed', ) npt.assert_raises(TypeError, types.check_float, '1')
def mex_hat(data_point, sigma): """Mexican hat. This method implements a Mexican hat (or Ricker) wavelet. Parameters ---------- data_point : float Input data point sigma : float Standard deviation (filter scale) Returns ------- float Mexican hat filtered data point Examples -------- >>> from modopt.signal.filter import mex_hat >>> mex_hat(2, 1) -0.3521390522571337 """ data_point = check_float(data_point) sigma = check_float(sigma) xs = (data_point / sigma) ** 2 factor = 2 * (3 * sigma) ** -0.5 * np.pi ** -0.25 return factor * (1 - xs) * np.exp(-0.5 * xs)
def __init__(self, data, psf, psf_type='fixed', convolve_method='astropy'): self.obs_data = data self.op = self._H_op_method self.trans_op = self._Ht_op_method check_float(psf) check_npndarray(psf, writeable=False) self._psf = psf self._psf_type = psf_type self._convolve_method = convolve_method PowerMethod.__init__(self, self.trans_op_op, self.obs_data.shape)
def obs_data(self, data): if self._grad_data_type in (float, np.floating): data = check_float(data) check_npndarray(data, dtype=self._grad_data_type, writeable=False) self._obs_data = data
def reweight(self, data): r"""Reweight This method implements the reweighting from section 4 in [CWB2007]_ Notes ----- Reweighting implemented as: .. math:: w = w \left( \frac{1}{1 + \frac{|x^w|}{n \sigma}} \right) """ print(' - Reweighting: {}'.format(self._rw_num)) self._rw_num += 1 data = check_float(data) if data.shape != self.weights.shape: raise ValueError('Input data must have the same shape as the ' 'initial weights.') self.weights *= (1.0 / (1.0 + np.abs(data) / (self.thresh_factor * self.original_weights)))
def __init__(self, filters, method='scipy'): self._filters = check_float(filters) self.op = lambda x: filter_convolve_stack( x, self._filters, method=method) self.adj_op = lambda x: filter_convolve_stack( x, self._filters, filter_rot=True, method=method)
def obs_data(self, input_data): if self._grad_data_type in {float, np.floating}: input_data = check_float(input_data) check_npndarray( input_data, dtype=self._grad_data_type, writeable=False, verbose=self.verbose, ) self._obs_data = input_data
def Gaussian_filter(x, sigma, norm=True): r"""Gaussian filter This method implements a Gaussian filter. Parameters ---------- x : float Input data point sigma : float Standard deviation (filter scale) norm : bool Option to return normalised data (default is ``True``) Returns ------- float Gaussian filtered data point Examples -------- >>> from modopt.signal.filter import Gaussian_filter >>> Gaussian_filter(1, 1) 0.24197072451914337 >>> Gaussian_filter(1, 1, False) 0.60653065971263342 """ x = check_float(x) sigma = check_float(sigma) val = np.exp(-0.5 * (x / sigma)**2) if norm: return val / (np.sqrt(2 * np.pi) * sigma) else: return val
def gaussian_filter(data_point, sigma, norm=True): """Gaussian filter. This method implements a Gaussian filter. Parameters ---------- data_point : float Input data point sigma : float Standard deviation (filter scale) norm : bool Option to return normalised data (default is ``True``) Returns ------- float Gaussian filtered data point Examples -------- >>> from modopt.signal.filter import gaussian_filter >>> gaussian_filter(1, 1) 0.24197072451914337 >>> gaussian_filter(1, 1, False) 0.6065306597126334 """ data_point = check_float(data_point) sigma = check_float(sigma) numerator = np.exp(-0.5 * (data_point / sigma) ** 2) if norm: return numerator / (np.sqrt(2 * np.pi) * sigma) return numerator
def reweight(self, input_data): r"""Reweight. This method implements the reweighting from section 4 in :cite:`candes2007`. Parameters ---------- input_data : numpy.ndarray Input data Raises ------ ValueError For invalid input shape Notes ----- Reweighting implemented as: .. math:: w = w \left( \frac{1}{1 + \frac{|x^w|}{n \sigma}} \right) where :math:`w` are the weights, :math:`x` is the ``input_data`` and :math:`n` is the ``thresh_factor``. """ if self.verbose: print(' - Reweighting: {0}'.format(self._rw_num)) self._rw_num += 1 input_data = check_float(input_data) if input_data.shape != self.weights.shape: raise ValueError( 'Input data must have the same shape as the initial weights.', ) thresh_weights = self.thresh_factor * self.original_weights self.weights *= np.array( 1.0 / (1.0 + np.abs(input_data) / (thresh_weights)), )
def grad(self, value): if self._grad_data_type in (float, np.floating): value = check_float(value) self._grad = value
def grad(self, input_value): if self._grad_data_type in {float, np.floating}: input_value = check_float(input_value) self._grad = input_value