def test_nn_selector(self): selector = Selector("nearest") for request in self.coords: for source in self.coords: dims = [ d for d in self.coords[source].udims if d in self.coords[request].udims ] if len(dims) == 0: continue # Invalid combination if "fine" in request and "fine" in source: continue if "coarse" in request and "coarse" in source: continue if "coarse" in request and "fine" in source: truth = self.nn_request_coarse_from_fine if "fine" in request and "coarse" in source: truth = self.nn_request_fine_from_coarse c, ci = selector.select(self.coords[source], self.coords[request]) np.testing.assert_array_equal( ci, (np.array(truth), ), err_msg= "Selection using source {} and request {} failed with {} != {} (truth)" .format(source, request, ci, truth), )
def select_coordinates(self, udims, source_coordinates, eval_coordinates, index_type="numpy"): """ {interpolator_select} """ selector = Selector(method=self.method) return selector.select(source_coordinates, eval_coordinates, index_type=index_type)
def test_point2uniform_non_square_xarray_type(self): u_fine = Coordinates([self.lat_fine, self.lon_fine[:-1]], ["lat", "lon"]) u_coarse = Coordinates([self.lat_coarse[:-1], self.lon_coarse], ["lat", "lon"]) p_fine = Coordinates([[self.lat_fine, self.lon_fine]], [["lat", "lon"]]) p_coarse = Coordinates([[self.lat_coarse, self.lon_coarse]], [["lat", "lon"]]) selector = Selector("nearest") # Test xarray indices instead cx, cix = selector.select(u_fine, p_coarse, index_type="xarray") cn, cin = selector.select(u_fine, p_coarse, index_type="numpy") xarr = Node().create_output_array(u_fine) xarr[...] = np.random.rand(*xarr.shape) np.testing.assert_equal(xarr[cix], xarr.data[cin])
def test_uniform2uniform(self): fine = Coordinates([self.lat_fine, self.lon_fine], ["lat", "lon"]) coarse = Coordinates([self.lat_coarse, self.lon_coarse], ["lat", "lon"]) selector = Selector("nearest") c, ci = selector.select(fine, coarse) for cci, trth in zip( ci, np.ix_(self.nn_request_coarse_from_fine, self.nn_request_coarse_from_fine)): np.testing.assert_array_equal(cci, trth) c, ci = selector.select(coarse, fine) for cci, trth in zip( ci, np.ix_(self.nn_request_fine_from_coarse, self.nn_request_fine_from_coarse)): np.testing.assert_array_equal(cci, trth)
def test_slice_index(self): selector = Selector("nearest") src = Coordinates([[0, 1, 2, 3, 4, 5]], dims=["lat"]) # uniform req = Coordinates([[2, 4]], dims=["lat"]) c, ci = selector.select(src, req, index_type="slice") assert isinstance(ci[0], slice) assert c == src[ci] # non uniform req = Coordinates([[1, 2, 4]], dims=["lat"]) c, ci = selector.select(src, req, index_type="slice") assert isinstance(ci[0], slice) assert c == src[ci] # empty req = Coordinates([[10]], dims=["lat"]) c, ci = selector.select(src, req, index_type="slice") assert isinstance(ci[0], slice) assert c == src[ci] # singleton req = Coordinates([[2]], dims=["lat"]) c, ci = selector.select(src, req, index_type="slice") assert isinstance(ci[0], slice) assert c == src[ci]
def test_nearest_selector_negative_time_step(self): selector = Selector("nearest") request1 = Coordinates([clinspace("2020-01-01", "2020-01-11", 11)], ["time"]) request2 = Coordinates([clinspace("2020-01-11", "2020-01-01", 11)], ["time"]) source1 = Coordinates( [clinspace("2020-01-22T00", "2020-01-01T00", 126)], ["time"]) source2 = Coordinates( [clinspace("2020-01-01T00", "2020-01-22T00", 126)], ["time"]) c11, ci11 = selector.select(source1, request1) assert len(c11["time"]) == 11 assert len(ci11[0]) == 11 c12, ci12 = selector.select(source1, request2) assert len(c12["time"]) == 11 assert len(ci12[0]) == 11 c21, ci21 = selector.select(source2, request1) assert len(c21["time"]) == 11 assert len(ci21[0]) == 11 c22, ci22 = selector.select(source2, request2) assert len(c22["time"]) == 11 assert len(ci22[0]) == 11 np.testing.assert_equal(ci11[0], ci12[0]) np.testing.assert_equal(ci21[0], ci22[0])
def test_nn_nonmonotonic_selector(self): selector = Selector("nearest") for request in ["lat_coarse", "lat_fine"]: for source in ["lat_random_fine", "lat_random_coarse"]: if "fine" in request and "fine" in source: truth = set(self.nn_request_fine_from_random_fine) if "coarse" in request and "coarse" in source: truth = set(self.nn_request_coarse_from_random_coarse) if "coarse" in request and "fine" in source: truth = set(self.nn_request_coarse_from_random_fine) if "fine" in request and "coarse" in source: truth = set(self.nn_request_fine_from_random_coarse) src_coords = Coordinates([getattr(self, source)], ["lat"]) req_coords = Coordinates([getattr(self, request)], ["lat"]) c, ci = selector.select(src_coords, req_coords) np.testing.assert_array_equal( ci, (np.array(list(truth)), ), err_msg= "Selection using source {} and request {} failed with {} != {} (truth)" .format(source, request, ci, list(truth)), )
def test_point2uniform_non_square(self): u_fine = Coordinates([self.lat_fine, self.lon_fine[:-1]], ["lat", "lon"]) u_coarse = Coordinates([self.lat_coarse[:-1], self.lon_coarse], ["lat", "lon"]) p_fine = Coordinates([[self.lat_fine, self.lon_fine]], [["lat", "lon"]]) p_coarse = Coordinates([[self.lat_coarse, self.lon_coarse]], [["lat", "lon"]]) selector = Selector("nearest") c, ci = selector.select(u_fine, p_coarse) for cci, trth in zip( ci, np.ix_(self.nn_request_coarse_from_fine, self.nn_request_coarse_from_fine)): np.testing.assert_array_equal(cci, trth) c, ci = selector.select(u_coarse, p_fine) for cci, trth in zip( ci, np.ix_(self.nn_request_fine_from_coarse[:-1], self.nn_request_fine_from_coarse)): np.testing.assert_array_equal(cci, trth) c, ci = selector.select(p_fine, u_coarse) np.testing.assert_array_equal( ci, (self.nn_request_coarse_from_fine_grid[:-1], )) c, ci = selector.select(p_coarse, u_fine) np.testing.assert_array_equal(ci, (self.nn_request_fine_from_coarse, )) # Respect bounds selector.respect_bounds = True c, ci = selector.select(u_fine, p_coarse) for cci, trth in zip( ci, np.ix_(self.nn_request_coarse_from_fine, self.nn_request_coarse_from_fine)): np.testing.assert_array_equal(cci, trth)
def test_nearest_selector_negative_step(self): selector = Selector("nearest") request1 = Coordinates([clinspace(-0.5, -1, 11)], ["lat"]) request2 = Coordinates([clinspace(-1, -0.5, 11)], ["lat"]) source1 = Coordinates([clinspace(-2, 0, 100)], ["lat"]) source2 = Coordinates([clinspace(0, -2, 100)], ["lat"]) c11, ci11 = selector.select(source1, request1) assert len(c11["lat"]) == 11 assert len(ci11[0]) == 11 c12, ci12 = selector.select(source1, request2) assert len(c12["lat"]) == 11 assert len(ci12[0]) == 11 c21, ci21 = selector.select(source2, request1) assert len(c21["lat"]) == 11 assert len(ci21[0]) == 11 c22, ci22 = selector.select(source2, request2) assert len(c22["lat"]) == 11 assert len(ci22[0]) == 11 np.testing.assert_equal(ci11[0], ci12[0]) np.testing.assert_equal(ci21[0], ci22[0])