def test_numerical_singleton(self): # positive step c = UniformCoordinates1d(1, 1, 10) a = np.array([1], dtype=float) assert c.start == 1 assert c.stop == 1 assert c.step == 10 assert_equal(c.coordinates, a) assert_equal(c.bounds, [1, 1]) assert c.size == 1 assert c.dtype == float assert c.is_monotonic == True assert c.is_descending == None assert c.is_uniform == True # negative step c = UniformCoordinates1d(1, 1, -10) a = np.array([1], dtype=float) assert c.start == 1 assert c.stop == 1 assert c.step == -10 assert_equal(c.coordinates, a) assert_equal(c.bounds, [1, 1]) assert c.size == 1 assert c.dtype == float assert c.is_monotonic == True assert c.is_descending == None assert c.is_uniform == True
def simplify(self): # NOTE: podpac prefers unstacked UniformCoordinates to AffineCoordinates # if that changes, just return self.copy() if self.affine.is_rectilinear: tol = 1e-15 # tolerance for deciding when a number is zero a = self.affine shape = self.shape if np.abs(a.e) <= tol and np.abs(a.a) <= tol: order = -1 step = np.array([a.d, a.b]) else: order = 1 step = np.array([a.e, a.a]) origin = a.f + step[0] / 2, a.c + step[1] / 2 end = origin[0] + step[0] * (shape[::order][0] - 1), origin[1] + step[1] * (shape[::order][1] - 1) # when the shape == 1, UniformCoordinates1d cannot infer the step from the size # we have have to create the UniformCoordinates1d manually if shape[::order][0] == 1: lat = UniformCoordinates1d(origin[0], end[0], step=step[0], name="lat") else: lat = clinspace(origin[0], end[0], shape[::order][0], "lat") if shape[::order][1] == 1: lon = UniformCoordinates1d(origin[1], end[1], step=step[1], name="lon") else: lon = clinspace(origin[1], end[1], shape[::order][1], "lon") return [lat, lon][::order] return self.copy()
def test_datetime(self): # ascending c = UniformCoordinates1d("2018-01-01", "2018-01-04", "1,D") a = np.array(["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"]).astype(np.datetime64) assert c.start == np.datetime64("2018-01-01") assert c.stop == np.datetime64("2018-01-04") assert c.step == np.timedelta64(1, "D") 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 c = UniformCoordinates1d("2018-01-04", "2018-01-01", "-1,D") a = np.array(["2018-01-04", "2018-01-03", "2018-01-02", "2018-01-01"]).astype(np.datetime64) assert c.start == np.datetime64("2018-01-04") assert c.stop == np.datetime64("2018-01-01") assert c.step == np.timedelta64(-1, "D") 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_numerical_inexact(self): # ascending c = UniformCoordinates1d(0, 49, 10) a = np.array([0, 10, 20, 30, 40], dtype=float) assert c.start == 0 assert c.stop == 49 assert c.step == 10 assert_equal(c.coordinates, a) assert_equal(c.bounds, [0, 40]) assert c.size == 5 assert c.dtype == float assert c.is_monotonic == True assert c.is_descending == False assert c.is_uniform == True # descending c = UniformCoordinates1d(50, 1, -10) a = np.array([50, 40, 30, 20, 10], dtype=float) assert c.start == 50 assert c.stop == 1 assert c.step == -10 assert_equal(c.coordinates, a) assert_equal(c.bounds, [10, 50]) assert c.dtype == float assert c.size == a.size assert c.is_monotonic == True assert c.is_descending == True assert c.is_uniform == True
def test_in(self): c = UniformCoordinates1d(0, 50, 10, name="lat") assert 0 in c assert 10 in c assert 50 in c assert -10 not in c assert 60 not in c assert 5 not in c assert np.datetime64("2018") not in c assert "a" not in c c = UniformCoordinates1d(50, 0, -10, name="lat") assert 0 in c assert 10 in c assert 50 in c assert -10 not in c assert 60 not in c assert 5 not in c assert np.datetime64("2018") not in c assert "a" not in c c = UniformCoordinates1d("2020-01-01", "2020-01-09", "2,D", name="time") assert np.datetime64("2020-01-01") in c assert np.datetime64("2020-01-03") in c assert np.datetime64("2020-01-09") in c assert np.datetime64("2020-01-11") not in c assert np.datetime64("2020-01-02") not in c assert 10 not in c assert "a" not in c
def test_datetime_singleton(self): # positive step c = UniformCoordinates1d('2018-01-01', '2018-01-01', '1,D') a = np.array(['2018-01-01']).astype(np.datetime64) assert c.start == np.datetime64('2018-01-01') assert c.stop == np.datetime64('2018-01-01') assert c.step == np.timedelta64(1, 'D') assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[0, -1]]) assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == None assert c.is_uniform == True # negative step c = UniformCoordinates1d('2018-01-01', '2018-01-01', '-1,D') a = np.array(['2018-01-01']).astype(np.datetime64) assert c.start == np.datetime64('2018-01-01') assert c.stop == np.datetime64('2018-01-01') assert c.step == np.timedelta64(-1, 'D') assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[-1, 0]]) assert c.size == a.size assert c.dtype == np.datetime64 assert c.is_monotonic == True assert c.is_descending == None assert c.is_uniform == True
def test_numerical_size(self): # ascending c = UniformCoordinates1d(0, 10, size=20) assert c.start == 0 assert c.stop == 10 assert c.step == 10 / 19. assert_equal(c.coordinates, np.linspace(0, 10, 20)) assert_equal(c.bounds, [0, 10]) assert c.size == 20 assert c.dtype == float assert c.is_monotonic == True assert c.is_descending == False assert c.is_uniform == True # descending c = UniformCoordinates1d(10, 0, size=20) assert c.start == 10 assert c.stop == 0 assert c.step == -10 / 19. assert_equal(c.coordinates, np.linspace(10, 0, 20)) assert_equal(c.bounds, [0, 10]) assert c.size == 20 assert c.dtype == float assert c.is_monotonic == True assert c.is_descending == True assert c.is_uniform == True
def test_datetime(self): # ascending c = UniformCoordinates1d('2018-01-01', '2018-01-04', '1,D') a = np.array(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04']).astype(np.datetime64) assert c.start == np.datetime64('2018-01-01') assert c.stop == np.datetime64('2018-01-04') assert c.step == np.timedelta64(1, 'D') assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[0, -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 c = UniformCoordinates1d('2018-01-04', '2018-01-01', '-1,D') a = np.array(['2018-01-04', '2018-01-03', '2018-01-02', '2018-01-01']).astype(np.datetime64) assert c.start == np.datetime64('2018-01-04') assert c.stop == np.datetime64('2018-01-01') assert c.step == np.timedelta64(-1, 'D') assert_equal(c.coordinates, a) assert_equal(c.bounds, a[[-1, 0]]) 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_equal_segment_lengths(object): c1 = UniformCoordinates1d(0, 50, 10) c2 = UniformCoordinates1d(0, 50, 10, segment_lengths=10) c3 = UniformCoordinates1d(0, 50, 10, segment_lengths=5) assert c1 == c2 assert c1 != c3
def test_datetime_size(self): # ascending c = UniformCoordinates1d("2018-01-01", "2018-01-10", size=10) assert c.start == np.datetime64("2018-01-01") assert c.stop == np.datetime64("2018-01-10") assert_equal(c.bounds, [np.datetime64("2018-01-01"), np.datetime64("2018-01-10")]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == 10 assert c.dtype == np.datetime64 assert c.is_descending == False # descending c = UniformCoordinates1d("2018-01-10", "2018-01-01", size=10) assert c.start == np.datetime64("2018-01-10") assert c.stop == np.datetime64("2018-01-01") assert_equal(c.bounds, [np.datetime64("2018-01-01"), np.datetime64("2018-01-10")]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == 10 assert c.dtype == np.datetime64 assert c.is_descending == True # increase resolution c = UniformCoordinates1d("2018-01-01", "2018-01-10", size=21) assert c.start == np.datetime64("2018-01-01") assert c.stop == np.datetime64("2018-01-10") assert_equal(c.bounds, [np.datetime64("2018-01-01"), np.datetime64("2018-01-10")]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == 21 assert c.dtype == np.datetime64 assert c.is_descending == False
def test_numerical(self): # ascending c = UniformCoordinates1d(0, 50, 10) a = np.array([0, 10, 20, 30, 40, 50], dtype=float) assert c.start == 0 assert c.stop == 50 assert c.step == 10 assert_equal(c.coordinates, a) assert_equal(c.bounds, [0, 50]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == 6 assert c.dtype == float assert c.is_monotonic == True assert c.is_descending == False assert c.is_uniform == True # descending c = UniformCoordinates1d(50, 0, -10) a = np.array([50, 40, 30, 20, 10, 0], dtype=float) assert c.start == 50 assert c.stop == 0 assert c.step == -10 assert_equal(c.coordinates, a) assert_equal(c.bounds, [0, 50]) assert c.coordinates[c.argbounds[0]] == c.bounds[0] assert c.coordinates[c.argbounds[1]] == c.bounds[1] assert c.size == 6 assert c.dtype == float assert c.is_monotonic == True assert c.is_descending == True assert c.is_uniform == True
def test_select_time_variable_precision(self): c = UniformCoordinates1d("2012-05-19", "2012-05-20", "1,D", name="time") c2 = UniformCoordinates1d("2012-05-20T12:00:00", "2012-05-21T12:00:00", "1,D", name="time") s = c.select(c2.bounds, outer=True) s1 = c.select(c2.bounds, outer=False) s2 = c2.select(c.bounds) assert s.size == 1 assert s1.size == 0 assert s2.size == 1
def test_issubset_uniform_coordinates(self): a = ArrayCoordinates1d([2, 1]) u1 = UniformCoordinates1d(start=1, stop=3, step=1) u2 = UniformCoordinates1d(start=1, stop=3, step=0.5) u3 = UniformCoordinates1d(start=1, stop=4, step=2) # self assert a.issubset(u1) assert a.issubset(u2) assert not a.issubset(u3)
def test_copy(self): c = UniformCoordinates1d(0, 10, 50, name='lat') c2 = c.copy() assert c is not c2 assert c == c2 c = UniformCoordinates1d(0, 10, 50, segment_lengths=0.5) c2 = c.copy() assert c is not c2 assert c == c2
def test_intersect(self): a = ArrayCoordinates1d([ 40., 70., 50., ]) u1 = UniformCoordinates1d(10., 60., 10.) u2 = UniformCoordinates1d(35., 85., 5.) assert isinstance(u1.intersect(a), UniformCoordinates1d) assert isinstance(u1.intersect(u2), UniformCoordinates1d)
def test_segment_lengths_inferred(self): # numerical c = UniformCoordinates1d(0, 50, 10) assert c.segment_lengths == 10 # datetime c = UniformCoordinates1d('2018-01-01', '2018-01-04', '1,D') assert c.segment_lengths == np.timedelta64(1, 'D') # point coordinates c = UniformCoordinates1d(0, 50, 10, ctype='point') assert c.segment_lengths is None
def test_intersect(self): a = ArrayCoordinates1d([20., 50., 60., 10.], ctype='point') b = ArrayCoordinates1d([55., 65., 95., 45.], ctype='point') c = ArrayCoordinates1d([80., 70., 90.], ctype='point') e = ArrayCoordinates1d([], ctype='point') u = UniformCoordinates1d(45, 95, 10) # overlap, in both directions ab = a.intersect(b) assert_equal(ab.coordinates, [50., 60.]) ab, I = a.intersect(b, return_indices=True) assert_equal(ab.coordinates, [50., 60.]) assert_equal(a.coordinates[I], [50., 60.]) ba = b.intersect(a) assert_equal(ba.coordinates, [55., 45.]) ba, I = b.intersect(a, return_indices=True) assert_equal(ba.coordinates, [55., 45.]) assert_equal(b.coordinates[I], [55., 45.]) # no overlap ac = a.intersect(c) assert_equal(ac.coordinates, []) ac, I = a.intersect(c, return_indices=True) assert_equal(ac.coordinates, []) assert_equal(a.coordinates[I], []) ca = a.intersect(c) assert_equal(ca.coordinates, []) ca, I = a.intersect(c, return_indices=True) assert_equal(ca.coordinates, []) assert_equal(c.coordinates[I], []) # empty self ea = e.intersect(a) assert_equal(ea.coordinates, []) ea, I = e.intersect(a, return_indices=True) assert_equal(ea.coordinates, []) assert_equal(e.coordinates[I], []) # empty other ae = a.intersect(e) assert_equal(ae.coordinates, []) ae, I = a.intersect(e, return_indices=True) assert_equal(ae.coordinates, []) assert_equal(a.coordinates[I], []) # UniformCoordinates1d other au = a.intersect(u) assert_equal(au.coordinates, [50., 60.]) au, I = a.intersect(u, return_indices=True) assert_equal(au.coordinates, [50., 60.]) assert_equal(a.coordinates[I], [50., 60.])
def crange(start, stop, step, name=None): """ Create uniformly-spaced 1d coordinates with a start, stop, and step. For numerical coordinates, the start, stop, and step are converted to ``float``. For time coordinates, the start and stop are converted to numpy ``datetime64``, and the step is converted to numpy ``timedelta64``. For convenience, podpac automatically converts datetime strings such as ``'2018-01-01'`` to ``datetime64`` and timedelta strings such as ``'1,D'`` to ``timedelta64``. Arguments --------- start : float, datetime64, datetime, str Start coordinate. stop : float, datetime64, datetime, str Stop coordinate. step : float, timedelta64, timedelta, str Signed, non-zero step between coordinates. name : str, optional Dimension name. Returns ------- :class:`UniformCoordinates1d` Uniformly-spaced 1d coordinates. """ return UniformCoordinates1d(start, stop, step=step, name=name)
def test_get_area_bounds_datetime(self): c = UniformCoordinates1d("2018-01-01", "2018-01-04", "1,D") # point area_bounds = c.get_area_bounds(None) assert_equal(area_bounds, make_coord_array(["2018-01-01", "2018-01-04"])) # uniform area_bounds = c.get_area_bounds("1,D") assert_equal(area_bounds, make_coord_array(["2017-12-31", "2018-01-05"])) area_bounds = c.get_area_bounds("1,M") assert_equal(area_bounds, make_coord_array(["2017-12-01", "2018-02-04"])) area_bounds = c.get_area_bounds("1,Y") assert_equal(area_bounds, make_coord_array(["2017-01-01", "2019-01-04"])) # segment area_bounds = c.get_area_bounds(["0,h", "12,h"]) assert_equal( area_bounds, make_coord_array(["2018-01-01 00:00", "2018-01-04 12:00"]))
def __init__(self, center, radius, theta=None, theta_size=None, dims=None): # radius if not isinstance(radius, Coordinates1d): radius = ArrayCoordinates1d(radius) # theta if theta is not None and theta_size is not None: raise TypeError( "PolarCoordinates expected theta or theta_size, not both.") if theta is None and theta_size is None: raise TypeError("PolarCoordinates requires theta or theta_size.") if theta_size is not None: theta = UniformCoordinates1d(start=0, stop=2 * np.pi, size=theta_size + 1)[:-1] elif not isinstance(theta, Coordinates1d): theta = ArrayCoordinates1d(theta) self.set_trait("center", center) self.set_trait("radius", radius) self.set_trait("theta", theta) if dims is not None: self.set_trait("dims", dims)
def test_equal_array_coordinates(self): c1 = UniformCoordinates1d(0, 50, 10) c2 = ArrayCoordinates1d([0, 10, 20, 30, 40, 50]) c3 = ArrayCoordinates1d([10, 20, 30, 40, 50, 60]) assert c1 == c2 assert c1 != c3
def test_unique(self): c = UniformCoordinates1d(1, 5, step=1) c2 = c.unique() assert c2 == c and c2 is not c c2, I = c.unique(return_index=True) assert c2 == c and c2 is not c assert c2 == c[I]
def test_issubset(self): c1 = UniformCoordinates1d(2, 1, step=-1) c2 = UniformCoordinates1d(1, 3, step=1) c3 = UniformCoordinates1d(0, 2, step=1) c4 = UniformCoordinates1d(1, 4, step=0.5) c5 = UniformCoordinates1d(1.5, 2.5, step=0.5) c6 = UniformCoordinates1d(1.4, 2.4, step=0.5) c7 = UniformCoordinates1d(1.4, 2.4, step=10) # self assert c1.issubset(c1) # subsets assert c1.issubset(c2) assert c1.issubset(c3) assert c1.issubset(c4) assert c5.issubset(c4) assert c7.issubset(c6) # not subsets assert not c2.issubset(c1) assert not c2.issubset(c3) assert not c3.issubset(c1) assert not c3.issubset(c2) assert not c4.issubset(c1) assert not c6.issubset(c4)
def clinspace(start, stop, size, name=None): """ Create uniformly-spaced 1d or stacked coordinates with a start, stop, and size. For numerical coordinates, the start and stop are converted to ``float``. For time coordinates, the start and stop are converted to numpy ``datetime64``. For convenience, podpac automatically converts datetime strings such as ``'2018-01-01'`` to ``datetime64``. Arguments --------- start : float, datetime64, datetime, str, tuple Start coordinate for 1d coordinates, or tuple of start coordinates for stacked coordinates. stop : float, datetime64, datetime, str, tuple Stop coordinate for 1d coordinates, or tuple of stop coordinates for stacked coordinates. size : int Number of coordinates. name : str, optional Dimension name. Returns ------- :class:`UniformCoordinates1d` Uniformly-spaced 1d coordinates. Raises ------ ValueError If the start and stop are not the same size. """ if np.array(start).size != np.array(stop).size: raise ValueError( "Size mismatch, 'start' and 'stop' must have the same size (%s != %s)" % (np.array(start).size, np.array(stop).size) ) # As of numpy 0.16, np.array([0, (0, 10)]) no longer raises a ValueError # so we have to explicitly check for sizes of start and stop (see above) a = np.array([start, stop]) if a.ndim == 2: cs = [UniformCoordinates1d(start[i], stop[i], size=size) for i in range(a[0].size)] c = StackedCoordinates(cs, name=name) else: c = UniformCoordinates1d(start, stop, size=size, name=name) return c
def test_definition_segment_lengths(self): c = UniformCoordinates1d(0, 50, 10, segment_lengths=0.5) d = c.definition assert isinstance(d, dict) assert set(d.keys()) == set( ['start', 'stop', 'step', 'segment_lengths']) json.dumps(d, cls=podpac.core.utils.JSONEncoder) # test serializable c2 = UniformCoordinates1d.from_definition(d) # test from_definition assert c2 == c
def test_definition(self): # numerical c = UniformCoordinates1d(0, 50, 10, name="lat", ctype="point") d = c.definition assert isinstance(d, dict) assert set(d.keys()) == set(['start', 'stop', 'step', 'name', 'ctype']) json.dumps(d, cls=podpac.core.utils.JSONEncoder) # test serializable c2 = UniformCoordinates1d.from_definition(d) # test from_definition assert c2 == c # datetimes c = UniformCoordinates1d('2018-01-01', '2018-01-03', '1,D') d = c.definition assert isinstance(d, dict) assert set(d.keys()) == set(['start', 'stop', 'step']) json.dumps(d, cls=podpac.core.utils.JSONEncoder) # test serializable c2 = UniformCoordinates1d.from_definition(d) # test from_definition assert c2 == c
def test_issubset_coordinates(self): u = UniformCoordinates1d(1, 3, 1, name="lat") c1 = Coordinates([[1, 2, 3], [10, 20, 30]], dims=["lat", "lon"]) c2 = Coordinates([[1, 2, 4], [10, 20, 30]], dims=["lat", "lon"]) c3 = Coordinates([[10, 20, 30]], dims=["alt"]) assert u.issubset(c1) assert not u.issubset(c2) assert not u.issubset(c3)
def test_definition(self): # numerical c = UniformCoordinates1d(0, 50, 10, name="lat") d = c.definition assert isinstance(d, dict) assert set(d.keys()) == set(["start", "stop", "step", "name"]) json.dumps(d, cls=podpac.core.utils.JSONEncoder) # test serializable c2 = UniformCoordinates1d.from_definition(d) # test from_definition assert c2 == c # datetimes c = UniformCoordinates1d("2018-01-01", "2018-01-03", "1,D") d = c.definition assert isinstance(d, dict) assert set(d.keys()) == set(["start", "stop", "step"]) json.dumps(d, cls=podpac.core.utils.JSONEncoder) # test serializable c2 = UniformCoordinates1d.from_definition(d) # test from_definition assert c2 == c
def test_index_segment_lengths(self): # array of segment_lengths c = UniformCoordinates1d( 0, 50, 10, segment_lengths=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6]) c2 = c[1] assert c2.segment_lengths == 0.2 or np.array_equal( c2.segment_lengths, [0.2]) c2 = c[1:3] assert_equal(c2.segment_lengths, [0.2, 0.3]) c2 = c[[1, 3]] assert_equal(c2.segment_lengths, [0.2, 0.4]) c2 = c[[4, 1, 2]] assert_equal(c2.segment_lengths, [0.5, 0.2, 0.3]) c2 = c[[True, True, True, False, True, False]] assert_equal(c2.segment_lengths, [0.1, 0.2, 0.3, 0.5]) # uniform segment_lengths c = UniformCoordinates1d(0, 50, 10, segment_lengths=0.5) c2 = c[1] assert c2.segment_lengths == 0.5 c2 = c[1:3] assert c2.segment_lengths == 0.5 c2 = c[[1, 3]] assert c2.segment_lengths == 0.5 c2 = c[[4, 1, 2]] assert c2.segment_lengths == 0.5 c2 = c[[True, True, True, False, True, False]] assert c2.segment_lengths == 0.5 # inferred segment_lengths c = UniformCoordinates1d(0, 50, 10) c2 = c[1] assert c2.segment_lengths == 10 or np.array_equal( c2.segment_lengths, [10])
def test_simplify(self): c = UniformCoordinates1d(1, 5, step=1) c2 = c.simplify() assert c2 == c and c2 is not c # reversed, step -2 c = UniformCoordinates1d(4, 0, step=-2) c2 = c.simplify() assert c2 == c and c2 is not c # time, convert to UniformCoordinates c = UniformCoordinates1d("2020-01-01", "2020-01-05", step="1,D") c2 = c.simplify() assert c2 == c and c2 is not c # time, reverse -2,h c = UniformCoordinates1d("2020-01-01T12:00", "2020-01-01T08:00", step="-3,h") c2 = c.simplify() assert c2 == c and c2 is not c