Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
values = _prepare(values, out=out, clip=clip)
np.multiply(values, self.exp, out=values)
np.add(values, 1., out=values)
np.log(values, out=values)
np.true_divide(values, np.log(self.exp + 1.), out=values)
return values
@property
def inverse(self):
return InvertedLogStretch(self.exp)
class InvertedLogStretch(BaseStretch):
"""
Inverse transformation for `~astropy.image.scaling.LogStretch`.
"""
def __init__(self, a):
super(InvertedLogStretch, self).__init__()
self.exp = a
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
np.multiply(values, np.log(self.exp + 1.), out=values)
np.exp(values, out=values)
np.subtract(values, 1., out=values)
np.true_divide(values, self.exp, out=values)
values = _prepare(values, out=out, clip=clip)
np.multiply(values, np.log(self.exp + 1.), out=values)
np.exp(values, out=values)
np.subtract(values, 1., out=values)
np.true_divide(values, self.exp, out=values)
return values
@property
def inverse(self):
return LogStretch(self.exp)
class AsinhStretch(BaseStretch):
r"""
An asinh stretch.
The stretch is given by:
.. math::
y = \frac{{\rm asinh}(x / a)}{{\rm asinh}(1 / a)}.
"""
def __init__(self, a=0.1):
super(AsinhStretch, self).__init__()
self.a = a
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
np.true_divide(values, self.a, out=values)
np.sinh(values, out=values)
np.true_divide(values, np.sinh(1. / self.a), out=values)
return values
@property
def inverse(self):
return AsinhStretch(a=1. / np.sinh(1. / self.a))
class HistEqStretch(BaseStretch):
"""
A histogram equalization stretch.
Parameters
----------
data : float
The data defining the equalization
"""
def __init__(self, data, values=None):
# Assume data is not necessarily normalized at this point
self.data = np.sort(data.ravel())
vmin = self.data.min()
vmax = self.data.max()
self.data = (self.data - vmin) / (vmax - vmin)
np.subtract(values, self.bias, out=values)
np.multiply(values, self.contrast, out=values)
np.add(values, 0.5, out=values)
if clip:
np.clip(values, 0, 1, out=values)
return values
@property
def inverse(self):
return InvertedContrastBiasStretch(self.contrast, self.bias)
class InvertedContrastBiasStretch(BaseStretch):
"""
Inverse transformation for ContrastBiasStretch.
"""
def __init__(self, contrast, bias):
super(InvertedContrastBiasStretch, self).__init__()
self.contrast = contrast
self.bias = bias
def __call__(self, values, out=None, clip=True):
# As a special case here, we only clip *after* the transformation since
# it does not map [0:1] to [0:1]
values = _prepare(values, out=out, clip=False)
The stretch is given by:
.. math::
y = x^2
"""
def __init__(self):
super(SquaredStretch, self).__init__(2)
@property
def inverse(self):
return SqrtStretch()
class LogStretch(BaseStretch):
r"""
A log stretch.
The stretch is given by:
.. math::
y = \frac{\log{(a x + 1)}}{\log{(a + 1)}}.
"""
def __init__(self, a=1000.0):
super(LogStretch, self).__init__()
self.exp = a
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
self.values = values
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
values[:] = np.interp(values, self.values, self.data)
return values
@property
def inverse(self):
return HistEqStretch(self.data, values=self.values)
class ContrastBiasStretch(BaseStretch):
"""
A stretch that takes into account contrast and bias.
The stretch is given by:
.. math::
y = (x - {\\rm bias}) * {\\rm contrast} + 0.5
and the output values are clipped to the [0:1] range.
"""
def __init__(self, contrast, bias):
super(ContrastBiasStretch, self).__init__()
self.contrast = contrast
self.bias = bias
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
np.true_divide(values, self.a, out=values)
np.arcsinh(values, out=values)
np.true_divide(values, np.arcsinh(1. / self.a), out=values)
return values
@property
def inverse(self):
return SinhStretch(a=1. / np.arcsinh(1. / self.a))
class SinhStretch(BaseStretch):
r"""
A sinh stretch.
The stretch is given by:
.. math::
y = \frac{{\rm sinh}(x / a)}{{\rm sinh}(1 / a)}
"""
def __init__(self, a=1. / 3.):
super(SinhStretch, self).__init__()
self.a = a
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
np.power(self.exp, values, out=values)
np.subtract(values, 1, out=values)
np.true_divide(values, self.exp - 1.0, out=values)
return values
@property
def inverse(self):
return InvertedPowerDistStretch(a=self.exp)
class InvertedPowerDistStretch(BaseStretch):
"""
Inverse transformation for `~astropy.image.scaling.PowerDistStretch`.
"""
def __init__(self, a=1000.0):
if a == 1: # singularity
raise ValueError("a cannot be set to 1")
super(InvertedPowerDistStretch, self).__init__()
self.exp = a
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
np.multiply(values, self.exp - 1.0, out=values)
np.add(values, 1, out=values)
self.power = a
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
np.power(values, self.power, out=values)
return values
@property
def inverse(self):
return PowerStretch(1. / self.power)
class PowerDistStretch(BaseStretch):
r"""
An alternative power stretch.
The stretch is given by:
.. math::
y = \frac{a^x - 1}{a - 1}
"""
def __init__(self, a=1000.0):
if a == 1: # singularity
raise ValueError("a cannot be set to 1")
super(PowerDistStretch, self).__init__()
self.exp = a
def __call__(self, values, out=None, clip=True):
self.values = values
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
values[:] = np.interp(values, self.data, self.values)
return values
@property
def inverse(self):
return InvertedHistEqStretch(self.data, values=self.values)
class InvertedHistEqStretch(BaseStretch):
"""
Inverse transformation for `~astropy.image.scaling.HistEqStretch`.
"""
def __init__(self, data, values=None):
self.data = data
if values is None:
self.values = np.linspace(0., 1., len(self.data))
else:
self.values = values
def __call__(self, values, out=None, clip=True):
values = _prepare(values, out=out, clip=clip)
values[:] = np.interp(values, self.values, self.data)