def _validate_boundary(self, d): val = d["value"] for dim, boundary in val.items(): if dim not in VALID_DIMENSION_NAMES: raise ValueError("Invalid dimension '%s' in boundary" % dim) if np.array(boundary).ndim == 0: try: delta = make_coord_delta(boundary) except ValueError: raise ValueError( "Invalid boundary for dimension '%s' ('%s' is not a valid coordinate delta)" % (dim, boundary)) if np.array(delta).astype(float) < 0: raise ValueError( "Invalid boundary for dimension '%s' (%s < 0)" % (dim, delta)) if np.array(boundary).ndim == 1: make_coord_delta_array(boundary) raise NotImplementedError( "Non-centered boundary not yet supported for dimension '%s'" % dim) if np.array(boundary).ndim == 2: for elem in boundary: make_coord_delta_array(elem) raise NotImplementedError( "Non-uniform boundary not yet supported for dimension '%s'" % dim) return val
def get_area_bounds(self, boundary): """ Get low and high coordinate area bounds. Arguments --------- boundary : float, timedelta, array, None Boundary offsets in this dimension. * For a centered uniform boundary (same for every coordinate), use a single positive float or timedelta offset. This represents the "total segment length" / 2. * For a uniform boundary (segment or polygon same for every coordinate), use an array of float or timedelta offsets * For a fully specified boundary, use an array of boundary arrays (2-D array, N_coords x boundary spec), one per coordinate. The boundary_spec can be a single number, two numbers, or an array of numbers. * For point coordinates, use None. Returns ------- low: float, np.datetime64 low area bound high: float, np.datetime64 high area bound """ # point coordinates if boundary is None: return self.bounds # empty coordinates if self.size == 0: return self.bounds if np.array(boundary).ndim == 0: # shortcut for uniform centered boundary boundary = make_coord_delta(boundary) lo_offset = -boundary hi_offset = boundary elif np.array(boundary).ndim == 1: # uniform boundary polygon boundary = make_coord_delta_array(boundary) lo_offset = min(boundary) hi_offset = max(boundary) else: L, H = self.argbounds lo_offset = min(make_coord_delta_array(boundary[L])) hi_offset = max(make_coord_delta_array(boundary[H])) lo, hi = self.bounds lo = add_coord(lo, lo_offset) hi = add_coord(hi, hi_offset) return lo, hi
def __init__(self, name=None, ctype=None, units=None, segment_lengths=None, coord_ref_sys=None): """*Do not use.*""" if name is not None: self.name = name if ctype is not None: self.set_trait('ctype', ctype) if units is not None: self.set_trait('units', units) if coord_ref_sys is not None: self.set_trait('coord_ref_sys', coord_ref_sys) if segment_lengths is not None: if np.array(segment_lengths).ndim == 0: segment_lengths = make_coord_delta(segment_lengths) else: segment_lengths = make_coord_delta_array(segment_lengths) segment_lengths.setflags(write=False) self.set_trait('segment_lengths', segment_lengths) super(Coordinates1d, self).__init__()
def test_mixed_type(self): with pytest.raises(ValueError): make_coord_delta_array([5.0, '1,D']) with pytest.raises(ValueError): make_coord_delta_array(['1,D', 5.0]) with pytest.raises(ValueError): make_coord_delta_array([5.0, np.timedelta64(1, 'D')]) with pytest.raises(ValueError): make_coord_delta_array([np.timedelta64(1, 'D'), 5.0])
def test_mixed_type(self): with pytest.raises(ValueError): make_coord_delta_array([5.0, "1,D"]) with pytest.raises(ValueError): make_coord_delta_array(["1,D", 5.0]) with pytest.raises(ValueError): make_coord_delta_array([5.0, np.timedelta64(1, "D")]) with pytest.raises(ValueError): make_coord_delta_array([np.timedelta64(1, "D"), 5.0])
def test_numerical_singleton(self): a = np.array([5.0]) f = 5.0 i = 5 # float np.testing.assert_array_equal(make_coord_delta_array(f), a) np.testing.assert_array_equal(make_coord_delta_array([f]), a) # float array np.testing.assert_array_equal(make_coord_delta_array(np.array(f)), a) np.testing.assert_array_equal(make_coord_delta_array(np.array([f])), a) # int np.testing.assert_array_equal(make_coord_delta_array(i), a) np.testing.assert_array_equal(make_coord_delta_array([i]), a) # int array np.testing.assert_array_equal(make_coord_delta_array(np.array(i)), a) np.testing.assert_array_equal(make_coord_delta_array(np.array([i])), a)
def test_timedelta_singleton(self): a = np.array([np.timedelta64(1, 'D')]) s = '1,D' u = u'1,D' td64 = np.timedelta64(1, 'D') td = np.timedelta64(1, 'D').item() # str np.testing.assert_array_equal(make_coord_delta_array(s), a) np.testing.assert_array_equal(make_coord_delta_array([s]), a) # unicode np.testing.assert_array_equal(make_coord_delta_array(u), a) np.testing.assert_array_equal(make_coord_delta_array([u]), a) # timedelta64 np.testing.assert_array_equal(make_coord_delta_array(td64), a) np.testing.assert_array_equal(make_coord_delta_array([td64]), a) # python timedelta np.testing.assert_array_equal(make_coord_delta_array(td), a) np.testing.assert_array_equal(make_coord_delta_array([td]), a)
def test_date_array(self): a = np.array([np.timedelta64(1, 'D'), np.timedelta64(2, 'D')]) s = ['1,D', '2,D'] u = [u'1,D', u'2,D'] td64 = [np.timedelta64(1, 'D'), np.timedelta64(2, 'D')] td = [np.timedelta64(1, 'D').item(), np.timedelta64(2, 'D').item()] # str np.testing.assert_array_equal(make_coord_delta_array(s), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(s)), a) # unicode np.testing.assert_array_equal(make_coord_delta_array(u), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(u)), a) # timedelta64 np.testing.assert_array_equal(make_coord_delta_array(td64), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(td64)), a) # python timedelta np.testing.assert_array_equal(make_coord_delta_array(td), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(td)), a)
def test_date_array(self): a = np.array([np.timedelta64(1, "D"), np.timedelta64(2, "D")]) s = ["1,D", "2,D"] u = ["1,D", "2,D"] td64 = [np.timedelta64(1, "D"), np.timedelta64(2, "D")] td = [np.timedelta64(1, "D").item(), np.timedelta64(2, "D").item()] # str np.testing.assert_array_equal(make_coord_delta_array(s), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(s)), a) # unicode np.testing.assert_array_equal(make_coord_delta_array(u), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(u)), a) # timedelta64 np.testing.assert_array_equal(make_coord_delta_array(td64), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(td64)), a) # python timedelta np.testing.assert_array_equal(make_coord_delta_array(td), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(td)), a)
def test_invalid_shape(self): with pytest.raises(ValueError): make_coord_delta_array([[0, 1], [5, 6]]) with pytest.raises(ValueError): make_coord_delta_array(np.array([[0, 1], [5, 6]]))
def test_invalid_time_string(self): with pytest.raises(ValueError): make_coord_delta_array(['invalid'])
def test_invalid_type(self): with pytest.raises(TypeError): make_coord_delta_array([{}])
def test_numerical_array(self): a = np.array([5.0, 5.5]) l = [5, 5.5] np.testing.assert_array_equal(make_coord_delta_array(l), a) np.testing.assert_array_equal(make_coord_delta_array(np.array(l)), a)