Reshaping data to have less loops, by Devis.
@@ -442,7 +442,7 @@ class LinearInterpolationNumba(InterpolationTask):
new_eopatch.bbox = eopatch.bbox
# Interpolate
- feature_data = self.interpolate4d(feature_data, times, resampled_times)
+ feature_data = self.interpolate2d(feature_data, times, resampled_times)
# Normalize
if self.result_interval:
@@ -499,6 +499,42 @@ class LinearInterpolationNumba(InterpolationTask):
return new_bands
+ @staticmethod
+ @numba.jit(nopython=True)
+ def interpolate2d(data, times, resampled_times):
+ """ Interpolates data feature
+
+ :param data: Array in a shape of t x h x w x n
+ :type data: numpy.ndarray
+ :param times: Array of reference times relative to the first timestamp
+ :type times:
+ :param resampled_times: Array of reference times relative to the first timestamp in initial timestamp array.
+ :type resampled_times: numpy.array
+ :return: Array of interpolated values
+ :rtype: numpy.ndarray
+ """
+
+ timestamps, height, width, depth = data.shape
+ new_bands = np.empty((len(resampled_times), height * width * depth))
+ data = data.reshape(timestamps, height * width * depth)
+ for n_feat in range(height * width * depth):
+ mask1d = ~np.isnan(data[:, n_feat])
+ new_data = np.interp(resampled_times, times[mask1d], data[:, n_feat][mask1d])
+
+ true_index = np.where(mask1d)
+ index_first, index_last = true_index[0][0], true_index[0][-1]
+ min_time, max_time = times[index_first], times[index_last]
+ first = np.where(resampled_times < min_time)[0]
+ if first.size:
+ new_data[:first[-1] + 1] = np.nan
+ last = np.where(max_time < resampled_times)[0]
+ if last.size:
+ new_data[last[0]:] = np.nan
+
+ new_bands[:, n_feat] = new_data
+
+ return new_bands.reshape(len(resampled_times), height, width, depth)
+
@staticmethod
def datetime_to_utc(datetimes, start=None):
""" Converts list of utc datetimes to list of unix timestamp
