added constant to the NDI calculation
@@ -47,10 +47,12 @@ class EuclideanNormTask(MapFeatureTask):
class NormalizedDifferenceIndexTask(MapFeatureTask):
""" The task calculates a Normalized Difference Index (NDI) between two bands A and B as:
- :math:`NDI = \\dfrac{A-B}{A+B}`
+ :math:`NDI = \\dfrac{A-B+c}{A+B+c}`,
+
+ where c is provided as the *constant* argument.
"""
- def __init__(self, input_feature, output_feature, bands, undefined_value=np.nan):
+ def __init__(self, input_feature, output_feature, bands, constant=0, undefined_value=np.nan):
"""
:param input_feature: A source feature from which to take the bands.
:type input_feature: an object supported by the :class:`FeatureParser<eolearn.core.utilities.FeatureParser>`
@@ -58,6 +60,8 @@ class NormalizedDifferenceIndexTask(MapFeatureTask):
:type output_feature: an object supported by the :class:`FeatureParser<eolearn.core.utilities.FeatureParser>`
:param bands: A list of bands from which to calculate the NDI.
:type bands: list
+ :param constant: A constant to be used in the NDI calculation. It is set to 0 by default.
+ :type constant: float
:param undefined_value: A value to override any calculation result that is not a finite value (e.g.: inf, nan).
"""
super().__init__(input_feature, output_feature)
@@ -67,6 +71,7 @@ class NormalizedDifferenceIndexTask(MapFeatureTask):
self.band_a, self.band_b = bands
self.undefined_value = undefined_value
+ self.constant = constant
def map_method(self, feature):
"""
@@ -76,7 +81,7 @@ class NormalizedDifferenceIndexTask(MapFeatureTask):
band_a, band_b = feature[..., self.band_a], feature[..., self.band_b]
with np.errstate(divide='ignore'):
- ndi = (band_a - band_b) / (band_a + band_b)
+ ndi = (band_a - band_b + self.constant) / (band_a + band_b + self.constant)
ndi[~np.isfinite(ndi)] = self.undefined_value
