def test_copy(self): c = UniformCoordinates1d(0, 10, 50, name="lat") c2 = c.copy() assert c is not c2 assert c == c2
def test_datetime_size_invalid(self): with pytest.raises(ValueError, match="Cannot divide timedelta"): c = UniformCoordinates1d("2018-01-01", "2018-01-10", size=20)
def test_numerical_size_floating_point_error(self): c = UniformCoordinates1d(50.619, 50.62795, size=30) assert c.size == 30
def test_datetime_year_step(self): # ascending, exact c = UniformCoordinates1d("2018-01-01", "2021-01-01", "1,Y") a = np.array(["2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01"]).astype(np.datetime64) assert c.start == np.datetime64("2018-01-01") assert c.stop == np.datetime64("2021-01-01") assert c.step == np.timedelta64(1, "Y") assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[0, -1]]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == False assert c.is_uniform == True # descending, exact c = UniformCoordinates1d("2021-01-01", "2018-01-01", "-1,Y") a = np.array(["2021-01-01", "2020-01-01", "2019-01-01", "2018-01-01"]).astype(np.datetime64) assert c.start == np.datetime64("2021-01-01") assert c.stop == np.datetime64("2018-01-01") assert c.step == np.timedelta64(-1, "Y") assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[-1, 0]]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == True assert c.is_uniform == True # ascending, inexact (two cases) c = UniformCoordinates1d("2018-01-01", "2021-04-01", "1,Y") a = np.array(["2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01"]).astype(np.datetime64) assert c.start == np.datetime64("2018-01-01") assert c.stop == np.datetime64("2021-01-01") assert c.step == np.timedelta64(1, "Y") assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[0, -1]]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == False assert c.is_uniform == True c = UniformCoordinates1d("2018-04-01", "2021-01-01", "1,Y") a = np.array(["2018-04-01", "2019-04-01", "2020-04-01"]).astype(np.datetime64) assert c.start == np.datetime64("2018-04-01") assert c.stop == np.datetime64("2020-04-01") assert c.step == np.timedelta64(1, "Y") assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[0, -1]]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == False assert c.is_uniform == True # descending, inexact (two cases) c = UniformCoordinates1d("2021-01-01", "2018-04-01", "-1,Y") a = np.array(["2021-01-01", "2020-01-01", "2019-01-01", "2018-01-01"]).astype(np.datetime64) assert c.start == np.datetime64("2021-01-01") assert c.stop == np.datetime64("2018-01-01") assert c.step == np.timedelta64(-1, "Y") assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[-1, 0]]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == True assert c.is_uniform == True c = UniformCoordinates1d("2021-04-01", "2018-01-01", "-1,Y") a = np.array(["2021-04-01", "2020-04-01", "2019-04-01", "2018-04-01"]).astype(np.datetime64) assert c.start == np.datetime64("2021-04-01") assert c.stop == np.datetime64("2018-04-01") assert c.step == np.timedelta64(-1, "Y") assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[-1, 0]]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == True assert c.is_uniform == True
def test_issubset_dtype(self): c1 = UniformCoordinates1d(0, 10, step=1) c2 = UniformCoordinates1d("2018", "2020", step="1,Y") assert not c1.issubset(c2) assert not c2.issubset(c1)
def test_reshape(self): c = UniformCoordinates1d(1, 6, step=1, name="lat") c2 = c.reshape((2, 3)) assert c2 == ArrayCoordinates1d(c.coordinates.reshape((2, 3)), name="lat")
def test_flatten(self): c = UniformCoordinates1d(1, 5, step=1) c2 = c.flatten() assert c2 == c and c2 is not c
def test_select_outer(self): c = UniformCoordinates1d(20.0, 70.0, 10.0) # inner s = c.select([35.0, 55.0], outer=True) assert s.start == 30.0 assert s.stop == 60.0 assert s.step == 10.0 s, I = c.select([35.0, 55.0], outer=True, return_index=True) assert s.start == 30.0 assert s.stop == 60.0 assert s.step == 10.0 assert c[I] == s # inner with aligned bounds s = c.select([30.0, 60.0], outer=True) assert s.start == 30.0 assert s.stop == 60.0 assert s.step == 10.0 s, I = c.select([30.0, 60.0], outer=True, return_index=True) assert s.start == 30.0 assert s.stop == 60.0 assert s.step == 10.0 assert c[I] == s # above s = c.select([45, 100], outer=True) assert s.start == 40.0 assert s.stop == 70.0 assert s.step == 10.0 s, I = c.select([45, 100], outer=True, return_index=True) assert s.start == 40.0 assert s.stop == 70.0 assert s.step == 10.0 assert c[I] == s # below s = c.select([5, 55], outer=True) assert s.start == 20.0 assert s.stop == 60.0 assert s.step == 10.0 s, I = c.select([5, 55], outer=True, return_index=True) assert s.start == 20.0 assert s.stop == 60.0 assert s.step == 10.0 assert c[I] == s # between coordinates s = c.select([52, 55], outer=True) assert s.start == 50.0 assert s.stop == 60.0 assert s.step == 10.0 s, I = c.select([52, 55], outer=True, return_index=True) assert s.start == 50.0 assert s.stop == 60.0 assert s.step == 10.0 assert c[I] == s # backwards bounds s = c.select([70, 30], outer=True) assert isinstance(s, ArrayCoordinates1d) assert_equal(s.coordinates, []) s, I = c.select([70, 30], outer=True, return_index=True) assert isinstance(s, ArrayCoordinates1d) assert_equal(s.coordinates, []) assert_equal(c.coordinates[I], [])