updating interpolation task to accept custom list of dates
import numpy as np
from dateutil import parser
-from datetime import timedelta
+from datetime import timedelta, datetime
from scipy import interpolate
from eolearn.core import EOTask, EOPatch, FeatureType
@@ -29,8 +29,9 @@ class InterpolationTask(EOTask):
replaced with interpolated values (if possible) in the existing EOPatch. Otherwise ``resample_range`` can be
set to tuple in a form of (start_date, end_date, step_days), e.g. ('2018-01-01', '2018-06-01', 16). This will
create a new EOPatch with resampled values for times start_date, start_date + step_days,
- start_date + 2 * step_days, ... . End date is excluded from timestamps.
- :type resample_range: (str, str, int) or None
+ start_date + 2 * step_days, ... . End date is excluded from timestamps. Additionally, ``resample_range`` can
+ be a list of dates or date-strings where the interpolation will be evaluated.
+ :type resample_range: (str, str, int) or (str, ...) or (date, ...) or None
:param result_interval: Maximum and minimum of returned data
:type result_interval: (float, float)
:param mask_feature: Feature that contains binary masks of interpolated feature
@@ -134,6 +135,7 @@ class InterpolationTask(EOTask):
input_y = data.T[~np.isnan(data).T]
# build interpolation function
+ if len(input_x) > 1:
interp_func = self.get_interpolation_function(input_x, input_y)
# interpolate non-NaN values in resampled time values
@@ -163,15 +165,30 @@ class InterpolationTask(EOTask):
"""
if self.resample_range is None:
return timestamp
+
if not isinstance(self.resample_range, (tuple, list)):
raise ValueError('Invalid resample_range {}, expected tuple'.format(self.resample_range))
+ if (len(self.resample_range) == 3 and
+ isinstance(self.resample_range[0], str) and
+ isinstance(self.resample_range[1], str) and
+ isinstance(self.resample_range[2], int)):
+
start_date = parser.parse(self.resample_range[0])
end_date = parser.parse(self.resample_range[1])
step = timedelta(days=self.resample_range[2])
days = [start_date]
while days[-1] + step < end_date:
days.append(days[-1] + step)
+ elif (self.resample_range and
+ np.all([isinstance(date, str) for date in self.resample_range])):
+ days = [parser.parse(date) for date in self.resample_range]
+ elif (self.resample_range and
+ np.all([isinstance(date, datetime) for date in self.resample_range])):
+ days = [date for date in self.resample_range]
+ else:
+ raise ValueError('Invalid format in {}, expected strings or datetimes'.format(self.resample_range))
+
return days
def execute(self, eopatch):
