def _fill_holes(of_instance, threshold=0): # calculate velocity scalar vlcty = np.sqrt(of_instance[::, ::, 0]**2 + of_instance[::, ::, 1]**2) # zero mask zero_holes = vlcty <= threshold # targets coord_target_i, coord_target_j = np.meshgrid(range(of_instance.shape[1]), range(of_instance.shape[0])) # source coord_source_i, coord_source_j = coord_target_i[~zero_holes], coord_target_j[~zero_holes] delta_x_source = of_instance[::, ::, 0][~zero_holes] delta_y_source = of_instance[::, ::, 1][~zero_holes] # reshape src = np.vstack((coord_source_i.ravel(), coord_source_j.ravel())).T trg = np.vstack((coord_target_i.ravel(), coord_target_j.ravel())).T # create an object interpolator = ipol.Idw(src, trg) # delta_x_target = interpolator(delta_x_source.ravel()) delta_y_target = interpolator(delta_y_source.ravel()) # reshape output delta_x_target = delta_x_target.reshape(of_instance.shape[0], of_instance.shape[1]) delta_y_target = delta_y_target.reshape(of_instance.shape[0], of_instance.shape[1]) return np.stack([delta_x_target, delta_y_target], axis=-1)
def test_nnearest_warning(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") ipol.Idw(self.src, self.trg, nnearest=len(self.src) + 1) ipol.OrdinaryKriging(self.src, self.trg, nnearest=len(self.src) + 1) ipol.ExternalDriftKriging(self.src, self.trg, nnearest=len(self.src) + 1) for item in w: assert issubclass(item.category, UserWarning) assert "nnearest" in str(item.message)
def test_MissingErrors(self): with pytest.raises(ipol.MissingSourcesError): ipol.Nearest(np.array([]), self.trg) with pytest.raises(ipol.MissingTargetsError): ipol.Nearest(self.src, np.array([])) with pytest.raises(ipol.MissingSourcesError): ipol.Idw(np.array([]), self.trg) with pytest.raises(ipol.MissingTargetsError): ipol.Idw(self.src, np.array([])) with pytest.raises(ipol.MissingSourcesError): ipol.Linear(np.array([]), self.trg) with pytest.raises(ipol.MissingTargetsError): ipol.Linear(self.src, np.array([])) with pytest.raises(ipol.MissingSourcesError): ipol.OrdinaryKriging(np.array([]), self.trg) with pytest.raises(ipol.MissingTargetsError): ipol.OrdinaryKriging(self.src, np.array([])) with pytest.raises(ipol.MissingSourcesError): ipol.ExternalDriftKriging(np.array([]), self.trg) with pytest.raises(ipol.MissingTargetsError): ipol.ExternalDriftKriging(self.src, np.array([]))
def test_Idw_1(self): """testing the basic behaviour of the Idw class""" ip = ipol.Idw(self.src, self.trg) # input more than one dataset res = ip(self.vals) self.assertTrue( np.allclose( res, np.array([[1., 2., 3.], [2., 2., 2.], [1.2, 2., 2.8], [3., 2., 1.]]))) # input only one flat array res = ip(self.vals[:, 2]) self.assertTrue(np.allclose(res, np.array([3., 2., 2.8, 1.])))
def _interpolator(points, coord_source, coord_target, method="idw"): coord_source_i, coord_source_j = coord_source coord_target_i, coord_target_j = coord_target # reshape trg = np.vstack((coord_source_i.ravel(), coord_source_j.ravel())).T src = np.vstack((coord_target_i.ravel(), coord_target_j.ravel())).T if method == "nearest": interpolator = NearestNDInterpolator( src, points.ravel(), tree_options={"balanced_tree": False}) points_interpolated = interpolator(trg) elif method == "linear": interpolator = LinearNDInterpolator(src, points.ravel(), fill_value=0) points_interpolated = interpolator(trg) elif method == "idw": interpolator = ipol.Idw(src, trg) points_interpolated = interpolator(points.ravel()) # reshape output points_interpolated = points_interpolated.reshape(points.shape) return points_interpolated.astype(points.dtype)
trg = np.vstack((trg[0].ravel(), trg[1].ravel())).T # Plot Definition def gridplot(interpolated, title=""): plt.pcolormesh(xtrg, ytrg, interpolated.reshape((len(xtrg), len(ytrg)))) plt.axis("tight") plt.scatter(src[:, 0], src[:, 1], facecolor="None", s=50, marker='s') plt.title(title) plt.xlabel("X") plt.ylabel("Y") # Interpolation IDW idw = ipol.Idw(src, trg) gridplot(idw(vals.ravel()), "IDW") # Other approach for IDW ip_near = ipol.Nearest(src, trg) maxdist = trg[1, 0] - trg[0, 0] result_near = ip_near(vals.ravel(), maxdist=maxdist) # 5.2.3 Kriging =============================== df_meg_nodes_np = np.array(df_meg_nodes) gridx = np.arange(0.0, 5.5, 0.5) gridy = np.arange(0.0, 5.5, 0.5) # Ordninary Kriging ok = ipol.OrdinaryKriging(src, trg) gridplot(ok(vals.ravel()), "Ordinary Kriging")