コード例 #1
0
    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)
コード例 #2
0
    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.])
コード例 #3
0
    def test_segment_lengths_delta(self):
        # numeric
        c = ArrayCoordinates1d([1, 2, 3],
                               ctype='midpoint',
                               segment_lengths=1.0)
        assert c.segment_lengths == 1.0

        # datetime
        c = ArrayCoordinates1d(['2018-01-01', '2018-01-02'],
                               ctype='midpoint',
                               segment_lengths='1,D')
        assert c.segment_lengths == np.timedelta64(1, 'D')

        # mismatch
        with pytest.raises(
                TypeError,
                match="coordinates and segment_lengths type mismatch"):
            ArrayCoordinates1d([1, 2, 3],
                               ctype='midpoint',
                               segment_lengths='1,D')

        with pytest.raises(
                TypeError,
                match="coordinates and segment_lengths type mismatch"):
            ArrayCoordinates1d(['2018-01-01', '2018-01-02'],
                               ctype='midpoint',
                               segment_lengths=1.0)
コード例 #4
0
    def test_get_index(self):
        lat = ArrayCoordinates1d([0, 1, 2, 3], name='lat')
        lon = ArrayCoordinates1d([10, 20, 30, 40], name='lon')
        time = ArrayCoordinates1d(
            ['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04'],
            name='time')
        c = StackedCoordinates([lat, lon, time])

        # integer index
        assert isinstance(c[0], StackedCoordinates)
        assert c[0].size == 1
        assert c[0].dims == c.dims
        assert_equal(c[0]['lat'].coordinates, c['lat'].coordinates[0])

        # index array
        assert isinstance(c[[1, 2]], StackedCoordinates)
        assert c[[1, 2]].size == 2
        assert c[[1, 2]].dims == c.dims
        assert_equal(c[[1, 2]]['lat'].coordinates, c['lat'].coordinates[[1,
                                                                         2]])

        # boolean array
        assert isinstance(c[[False, True, True, False]], StackedCoordinates)
        assert c[[False, True, True, False]].size == 2
        assert c[[False, True, True, False]].dims == c.dims
        assert_equal(c[[False, True, True, False]]['lat'].coordinates,
                     c['lat'].coordinates[[False, True, True, False]])

        # slice
        assert isinstance(c[1:3], StackedCoordinates)
        assert c[1:3].size == 2
        assert c[1:3].dims == c.dims
        assert_equal(c[1:3]['lat'].coordinates, c['lat'].coordinates[1:3])
コード例 #5
0
 def test_invalid_definition(self):
     d = {'coords': [0, 1, 2]}
     with pytest.raises(
             ValueError,
             match='ArrayCoordinates1d definition requires "values" property'
     ):
         ArrayCoordinates1d.from_definition(d)
コード例 #6
0
    def test_intersect_dtype_mismatch(self):
        a = ArrayCoordinates1d([1., 2., 3., 4.], name='time')
        b = ArrayCoordinates1d(['2018-01-01', '2018-01-02'], name='time')

        with pytest.raises(ValueError,
                           match="Cannot intersect mismatched dtypes"):
            a.intersect(b)
コード例 #7
0
    def test_len(self):
        lat = ArrayCoordinates1d([0, 1, 2, 3])
        lon = ArrayCoordinates1d([10, 20, 30, 40])
        time = ArrayCoordinates1d(["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"])
        c = StackedCoordinates([lat, lon, time])

        assert len(c) == 3
コード例 #8
0
    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
コード例 #9
0
    def test_invalid_coords_shaped(self):
        # same size, different shape
        lat = ArrayCoordinates1d(np.arange(12).reshape((3, 4)), name="lat")
        lon = ArrayCoordinates1d(np.arange(12).reshape((4, 3)), name="lon")

        with pytest.raises(ValueError, match="Shape mismatch in stacked coords"):
            StackedCoordinates([lat, lon])
コード例 #10
0
    def __getitem__(self, index):
        # fallback for non-slices
        if not isinstance(index, slice):
            return ArrayCoordinates1d(self.coordinates, **self.properties)[index]

        # start, stop, step
        if index.start is None:
            start = self.start
        elif index.start >= 0:
            start = add_coord(self.start, self.step * min(index.start, self.size - 1))
        else:
            start = add_coord(self.start, self.step * max(0, self.size + index.start))

        if index.stop is None:
            stop = self.stop
        elif index.stop >= 0:
            stop = add_coord(self.start, self.step * (min(index.stop, self.size) - 1))
        else:
            stop = add_coord(self.start, self.step * max(0, self.size + index.stop - 1))

        if index.step is None:
            step = self.step
        else:
            step = index.step * self.step
            if index.step < 0:
                start, stop = stop, start

        # empty slice
        if start > stop and step > 0:
            return ArrayCoordinates1d([], **self.properties)
        return UniformCoordinates1d(start, stop, step, **self.properties)
コード例 #11
0
    def test_intersect_name_mismatch(self):
        a = ArrayCoordinates1d([20., 50., 60., 10.], name='lat')
        b = ArrayCoordinates1d([55., 65., 95., 45.], name='lon')

        with pytest.raises(ValueError,
                           match="Cannot intersect mismatched dimensions"):
            a.intersect(b)
コード例 #12
0
    def test_segment_lenths_point(self):
        with pytest.raises(TypeError, match="segment_lengths must be None"):
            ArrayCoordinates1d([1, 2], ctype='point', segment_lengths=1.0)

        with pytest.raises(TypeError, match="segment_lengths must be None"):
            ArrayCoordinates1d([1, 2],
                               ctype='point',
                               segment_lengths=[1.0, 1.0])
コード例 #13
0
    def test_transpose_invalid(self):
        lat = ArrayCoordinates1d([0, 1, 2], name="lat")
        lon = ArrayCoordinates1d([10, 20, 30], name="lon")
        time = ArrayCoordinates1d(["2018-01-01", "2018-01-02", "2018-01-03"], name="time")
        c = StackedCoordinates([lat, lon, time])

        with pytest.raises(ValueError, match="Invalid transpose dimensions"):
            c.transpose("lon", "lat")
コード例 #14
0
    def test_iter(self):
        lat = ArrayCoordinates1d([0, 1, 2, 3])
        lon = ArrayCoordinates1d([10, 20, 30, 40])
        time = ArrayCoordinates1d(["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"])
        c = StackedCoordinates([lat, lon, time])

        for item in c:
            assert isinstance(item, Coordinates1d)
コード例 #15
0
 def test_init_explicit_shaped(self):
     lat = ArrayCoordinates1d([[0, 1, 2], [10, 11, 12]], name="lat")
     lon = ArrayCoordinates1d([[10, 20, 30], [11, 21, 31]], name="lon")
     c = StackedCoordinates([lat, lon])
     assert c.dims == ("lat", "lon")
     assert c.udims == ("lat", "lon")
     assert c.name == "lat_lon"
     repr(c)
コード例 #16
0
    def test_eq_ctype(self):
        c1 = ArrayCoordinates1d([0, 1, 3])
        c2 = ArrayCoordinates1d([0, 1, 3], ctype='midpoint')
        c3 = ArrayCoordinates1d([0, 1, 3], ctype='left')

        assert c1 == c2
        assert c1 != c3
        assert c2 != c3
コード例 #17
0
 def test_definition_segment_lengths(self):
     c = ArrayCoordinates1d([0, 1, 2], segment_lengths=0.5)
     d = c.definition
     assert isinstance(d, dict)
     assert set(d.keys()) == set(['values', 'segment_lengths'])
     json.dumps(d, cls=podpac.core.utils.JSONEncoder)  # test serializable
     c2 = ArrayCoordinates1d.from_definition(d)  # test from_definition
     assert c2 == c
コード例 #18
0
    def test_properties(self):
        c = ArrayCoordinates1d([])
        assert isinstance(c.properties, dict)
        assert set(c.properties) == set()

        c = ArrayCoordinates1d([], name="lat")
        assert isinstance(c.properties, dict)
        assert set(c.properties) == {"name"}
コード例 #19
0
 def test_xdims(self):
     lat = ArrayCoordinates1d([0, 1, 2, 3], name="lat")
     lon = ArrayCoordinates1d([10, 20, 30, 40], name="lon")
     time = ArrayCoordinates1d(
         ["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"],
         name="time")
     c = StackedCoordinates([lat, lon, time])
     assert c.xdims == ("lat_lon_time", )
コード例 #20
0
    def test_xdims(self):
        c = ArrayCoordinates1d([], name="lat")
        assert isinstance(c.xdims, tuple)
        assert c.xdims == ("lat", )

        c = ArrayCoordinates1d([0, 1, 2], name="lat")
        assert isinstance(c.xdims, tuple)
        assert c.xdims == ("lat", )
コード例 #21
0
    def test_size(self):
        lat = ArrayCoordinates1d([0, 1, 2, 3])
        lon = ArrayCoordinates1d([10, 20, 30, 40])
        time = ArrayCoordinates1d(
            ['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04'])
        c = StackedCoordinates([lat, lon, time])

        assert c.size == 4
コード例 #22
0
    def test_coord_ref_sys(self):
        c = ArrayCoordinates1d([])
        assert c.coord_ref_sys == 'WGS84'

        c = ArrayCoordinates1d([], coord_ref_sys='SPHER_MERC')
        assert c.coord_ref_sys == 'SPHER_MERC'

        with pytest.raises(tl.TraitError):
            ArrayCoordinates1d([], coord_ref_sys='ABCD')
コード例 #23
0
 def test_select_time_variable_precision(self):
     c = ArrayCoordinates1d(["2012-05-19"], name="time")
     c2 = ArrayCoordinates1d(["2012-05-19T12:00:00"], 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
コード例 #24
0
    def test_copy(self):
        lat = ArrayCoordinates1d([0, 1, 2], name="lat")
        lon = ArrayCoordinates1d([10, 20, 30], name="lon")
        time = ArrayCoordinates1d(["2018-01-01", "2018-01-02", "2018-01-03"], name="time")
        c = StackedCoordinates([lat, lon, time])

        c2 = c.copy()
        assert c2 is not c
        assert c2 == c
コード例 #25
0
    def test_ctype(self):
        lat = ArrayCoordinates1d([0, 1, 2], name='lat', ctype='left')
        lon = ArrayCoordinates1d([10, 20, 30], name='lon')
        c = StackedCoordinates([lat, lon], ctype='right')

        # lon ctype set by StackedCoordinates
        assert c['lon'].ctype == 'right'

        # but lat is left by StackedCoordinates because it was already explicitly set
        assert c['lat'].ctype == 'left'
コード例 #26
0
    def test_copy(self):
        c = ArrayCoordinates1d([1, 2, 3], name='lat')
        c2 = c.copy()
        assert c is not c2
        assert c == c2

        c = ArrayCoordinates1d([1, 2, 3], segment_lengths=0.5)
        c2 = c.copy()
        assert c is not c2
        assert c == c2
コード例 #27
0
    def test_copy(self):
        lat = ArrayCoordinates1d([0, 1, 2], name='lat')
        lon = ArrayCoordinates1d([10, 20, 30], name='lon')
        time = ArrayCoordinates1d(['2018-01-01', '2018-01-02', '2018-01-03'],
                                  name='time')
        c = StackedCoordinates([lat, lon, time])

        c2 = c.copy()
        assert c2 is not c
        assert c2 == c
コード例 #28
0
    def test_select_dtype(self):
        c = ArrayCoordinates1d([20.0, 40.0, 60.0, 10.0, 90.0, 50.0],
                               name="lat")
        with pytest.raises(TypeError):
            c.select({"lat": [np.datetime64("2018-01-01"), "2018-02-01"]})

        c = ArrayCoordinates1d(
            ["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"],
            name="time")
        with pytest.raises(TypeError):
            c.select({"time": [1, 10]})
コード例 #29
0
    def test_distance_units(self):
        lat = ArrayCoordinates1d([0, 1], name='lat')
        lon = ArrayCoordinates1d([0, 1], name='lon')
        time = ArrayCoordinates1d(['2018-01-01', '2018-01-02'], name='time')

        units = podpac.core.units.Units()
        c = StackedCoordinates([lat, lon, time], distance_units=units)

        assert c['lat'].units is units
        assert c['lon'].units is units
        assert c['time'].units is not units
コード例 #30
0
    def test_eq_coordinates(self):
        lat = ArrayCoordinates1d([0, 1, 2], name='lat')
        lon = ArrayCoordinates1d([10, 20, 30], name='lon')
        c1 = StackedCoordinates([lat, lon])
        c2 = StackedCoordinates([lat, lon])
        c3 = StackedCoordinates([lat[::-1], lon])
        c4 = StackedCoordinates([lat, lon[::-1]])

        assert c1 == c2
        assert c1 != c3
        assert c1 != c4