OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
TanhTransformer.py
Go to the documentation of this file.
1 from ._CustomTransformer import _CustomTransformer
2 
3 import numpy as np
4 
5 
7  ''' tanh-estimator (Hampel et al. 1986; Latha & Thangasamy, 2011) '''
8  scale = 0.01
9 
10  def _fit(self, X, *args, **kwargs):
11  m = np.median(X, 0)
12  d = np.abs(X - m)
13 
14  a = np.percentile(d, 70, 0)
15  b = np.percentile(d, 85, 0)
16  c = np.percentile(d, 95, 0)
17 
18  Xab = np.abs(X)
19  Xsi = np.sign(X)
20  phi = np.zeros(X.shape)
21  idx = np.logical_and(0 <= Xab, Xab < a)
22  phi[idx] = X[idx]
23  idx = np.logical_and(a <= Xab, Xab < b)
24  phi[idx] = (a * Xsi)[idx]
25  idx = np.logical_and(b <= Xab, Xab < c)
26  phi[idx] = (a * Xsi * ((c - Xab) / (c - b)))[idx]
27 
28  self.mu_gh = np.mean(phi, 0)
29  self.sig_gh = np.std(phi, 0)
30 
31  def _transform(self, X, *args, **kwargs):
32  return 0.5 * (np.tanh(self.scale * ((X - self.mu_gh)/self.sig_gh)) + 1)
33 
34  def _inverse_transform(self, X, *args, **kwargs):
35  return ((np.tan(X * 2 - 1) / self.scale) * self.sig_gh) + self.mu_gh
36