def test_invalid_definition(self): # incorrect definition d = {"stop": 50} with pytest.raises(ValueError, match='UniformCoordinates1d definition requires "start"'): UniformCoordinates1d.from_definition(d) d = {"start": 0} with pytest.raises(ValueError, match='UniformCoordinates1d definition requires "stop"'): UniformCoordinates1d.from_definition(d)
def test_from_definition_size(self): # numerical d = {"start": 0, "stop": 50, "size": 6} c = UniformCoordinates1d.from_definition(d) assert_equal(c.coordinates, [0, 10, 20, 30, 40, 50]) # datetime, size d = {"start": "2018-01-01", "stop": "2018-01-03", "size": 3} c = UniformCoordinates1d.from_definition(d) assert_equal(c.coordinates, np.array(["2018-01-01", "2018-01-02", "2018-01-03"]).astype(np.datetime64))
def test_from_definition_size(self): # numerical d = {'start': 0, 'stop': 50, 'size': 6} c = UniformCoordinates1d.from_definition(d) assert_equal(c.coordinates, [0, 10, 20, 30, 40, 50]) # datetime, size d = {'start': '2018-01-01', 'stop': '2018-01-03', 'size': 3} c = UniformCoordinates1d.from_definition(d) assert_equal( c.coordinates, np.array(['2018-01-01', '2018-01-02', '2018-01-03']).astype(np.datetime64))
def from_definition(cls, d): if "center" not in d: raise ValueError( 'PolarCoordinates definition requires "center" property') if "radius" not in d: raise ValueError( 'PolarCoordinates definition requires "radius" property') if "theta" not in d and "theta_size" not in d: raise ValueError( 'PolarCoordinates definition requires "theta" or "theta_size" property' ) if "dims" not in d: raise ValueError( 'PolarCoordinates definition requires "dims" property') # center center = d["center"] # radius if isinstance(d["radius"], list): radius = ArrayCoordinates1d(d["radius"]) elif "values" in d["radius"]: radius = ArrayCoordinates1d.from_definition(d["radius"]) elif "start" in d["radius"] and "stop" in d["radius"] and ( "step" in d["radius"] or "size" in d["radius"]): radius = UniformCoordinates1d.from_definition(d["radius"]) else: raise ValueError( "Could not parse radius coordinates definition with keys %s" % d.keys()) # theta if "theta" not in d: theta = None elif isinstance(d["theta"], list): theta = ArrayCoordinates1d(d["theta"]) elif "values" in d["theta"]: theta = ArrayCoordinates1d.from_definition(d["theta"]) elif "start" in d["theta"] and "stop" in d["theta"] and ( "step" in d["theta"] or "size" in d["theta"]): theta = UniformCoordinates1d.from_definition(d["theta"]) else: raise ValueError( "Could not parse theta coordinates definition with keys %s" % d.keys()) kwargs = { k: v for k, v in d.items() if k not in ["center", "radius", "theta"] } return PolarCoordinates(center, radius, theta, **kwargs)
def from_definition(cls, d): """ Create StackedCoordinates from a stacked coordinates definition. Arguments --------- d : list stacked coordinates definition Returns ------- :class:`StackedCoordinates` stacked coordinates object See Also -------- definition """ coords = [] for elem in d: if "start" in elem and "stop" in elem and ("step" in elem or "size" in elem): c = UniformCoordinates1d.from_definition(elem) elif "values" in elem: c = ArrayCoordinates1d.from_definition(elem) else: raise ValueError( "Could not parse coordinates definition with keys %s" % elem.keys()) coords.append(c) return cls(coords)
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_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 from_definition(cls, d): """ Create podpac Coordinates from a coordinates definition. Arguments --------- d : list coordinates definition Returns ------- :class:`Coordinates` podpac Coordinates See Also -------- from_json, definition """ if not isinstance(d, list): raise TypeError( "Could not parse coordinates definition of type '%s'" % type(d)) coords = [] for elem in d: if isinstance(elem, list): c = StackedCoordinates.from_definition(elem) elif 'start' in elem and 'stop' in elem and ('step' in elem or 'size' in elem): c = UniformCoordinates1d.from_definition(elem) elif 'values' in elem: c = ArrayCoordinates1d.from_definition(elem) else: raise ValueError( "Could not parse coordinates definition item with keys %s" % elem.keys()) coords.append(c) return cls(coords)