def test_record_collection_without_taxonomy(self): import geojson r = data.Record([0, 1, 2], ['car', 'road vehicle'], some_property='some property', another_property=45) rc = data.RecordCollection(r) mixin_suite(rc) # Base validity tests assert hasattr(rc, 'id') assert re.match( r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\Z', rc.id) assert len(rc) == 1 assert rc[0] == r r2 = data.Record([[[0, 0], [0, 1], [1, 1], [0, 0]]], ['car', 'truck'], some_property='some property', another_property=45) rc.append(r2) assert len(rc) == 2 assert rc[1] == r2 assert geojson.dumps(rc, sort_keys=True) == '{"features": [' \ '{"geometry": {"coordinates": [0, 1, 2], "type": "Point"}, ' \ '"properties": {"another_property": 45, ' \ '"category": ["car", "road vehicle"], ' \ '"confidence": null, "some_property": "some property"}, ' \ '"type": "Feature"}, ' \ '{"geometry": {"coordinates": [[[0, 0], [0, 1], [1, 1], [0, 0]]], '\ '"type": "Polygon"}, ' \ '"properties": {"another_property": 45, ' \ '"category": ["car", "truck"], ' \ '"confidence": null, ' \ '"some_property": "some property"}, "type": "Feature"}], ' \ '"type": "FeatureCollection"}' assert 'car' in rc.taxonomy assert rc.taxonomy['car'].parent == rc.taxonomy.root assert 'truck' in rc.taxonomy assert rc.taxonomy['truck'].parent == rc.taxonomy.root assert 'road vehicle' in rc.taxonomy assert rc.taxonomy['road vehicle'].parent == rc.taxonomy.root rc[1] = data.Record([[[0, 0], [0, 1], [1, 1], [0, 0]]], ['trucks'], some_property='some property', another_property=45) assert rc[1].labels == (Label('trucks'), ) assert 'trucks' in rc.taxonomy assert rc.taxonomy['trucks'].parent == rc.taxonomy.root rc = data.RecordCollection(r)
def test_data_point(self, tiles): dp = data.DataPoint(tiles, data.Annotation(data.RecordCollection())) mixin_suite(dp) # Base validity tests class DummyGeo(object): @property def __geo_interface__(self): return 0 dp = data.DataPoint(tiles, DummyGeo(), some_property='some_value') assert hasattr(dp, 'id') assert re.match( r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\Z', dp.id) assert dp.some_property == 'some_value' with pytest.raises( TypeError, match= 'Expected "annotation" to expose the __geo_interface__ attribute' ): dp = data.DataPoint(tiles, 0, some_property='some_value') with pytest.raises( TypeError, match='Expected an ordered dictionary like object as tiles'): dp = data.DataPoint({}, DummyGeo(), some_property='some_value')
def test_annotation(self): import geojson class Dummy(object): @property def __geo_interface__(self): return None r = data.Record([0, 1, 2], ['car', 'road vehicle'], some_property='some property', another_property=45) rc = data.RecordCollection( r, data.Record([[[0, 0], [0, 1], [1, 1], [0, 0]]], ['car', 'road vehicle'], some_property='some property', another_property=45)) vm = data.VectorMask([[[0, 0], [0, 1], [1, 1], [0, 0]]], 'vector-data', some_property='some_value', another_property=45) mc = data.MaskCollection(vm) with pytest.raises( TypeError, match= 'Expected "record_collection" to expose the __geo_interface__ attribute' ): a = data.Annotation(0, mc, some_property='some_value') a = data.Annotation(Dummy(), mc, some_property='some_value') assert a.__geo_interface__ is None a = data.Annotation(rc, mc, some_property='some_value') mixin_suite(a) # Base validity tests assert hasattr(a, 'id') assert re.match( r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\Z', a.id) assert a.some_property == 'some_value' assert a[0] == r assert geojson.dumps(a, sort_keys=True) == '{"features": [' \ '{"geometry": {"coordinates": [0, 1, 2], "type": "Point"}, ' \ '"properties": {"another_property": 45, ' \ '"category": ["car", "road vehicle"], ' \ '"confidence": null, "some_property": "some property"}, ' \ '"type": "Feature"}, ' \ '{"geometry": {"coordinates": [[[0, 0], [0, 1], [1, 1], [0, 0]]], ' \ '"type": "Polygon"}, ' \ '"properties": {"another_property": 45, ' \ '"category": ["car", "road vehicle"], ' \ '"confidence": null, ' \ '"some_property": "some property"}, "type": "Feature"}], ' \ '"type": "FeatureCollection"}'
def test_deprecated_data_point(self): # Old deprecated interface with pytest.deprecated_call(): dp = data.DataPoint( data.TileWrapper(np.zeros((5, 5, 3)), filename='somefile.png', some_property='some_value'), data.Annotation(data.RecordCollection())) mixin_suite(dp) # Base validity tests with pytest.deprecated_call(): assert isinstance(dp.tile, data.Tile) class DummyGeo(object): @property def __geo_interface__(self): return 0 class DummyArray(object): @property def __array_interface__(self): return 1 class DummyTile(data.Tile): @property def __array_interface__(self): return 1 dp = data.DataPoint(DummyTile(DummyArray()), DummyGeo(), some_property='some_value') assert hasattr(dp, 'id') assert re.match( r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\Z', dp.id) assert dp.some_property == 'some_value' with pytest.raises( TypeError, match='Expected an ordered dictionary like object as tiles'): dp = data.DataPoint(0, DummyGeo(), some_property='some_value') with pytest.raises( TypeError, match= 'Expected "annotation" to expose the __geo_interface__ attribute' ): dp = data.DataPoint(DummyTile(DummyArray()), 0, some_property='some_value')
def test_record_collection_with_taxonomy_add(self): # noqa: R701 import geojson taxonomy = Taxonomy(Label('road vehicle'), Label('car')) invalid_taxonomy = Taxonomy( Label('road vehicle', children=(Label('car'), )), Label('other')) different_taxonomy = Taxonomy(Label('road vehicle'), Label('other')) incomplete_taxonomy = Taxonomy(Label('road vehicle')) r = data.Record([0, 1, 2], ['car', 'road vehicle'], some_property='some property', another_property=45) rc = data.RecordCollection(r) mixin_suite(rc) # Base validity tests assert hasattr(rc, 'id') assert re.match( r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\Z', rc.id) with pytest.raises(ValueError, match='Expected at most'): rc.taxonomy = incomplete_taxonomy with pytest.raises(ValueError, match='are not part of the taxonomy'): rc.taxonomy = different_taxonomy with pytest.raises( ValueError, match='Some labels are part of the same true-root subtree'): rc.taxonomy = invalid_taxonomy rc.taxonomy = taxonomy assert len(rc) == 1 assert rc[0] == r r2 = data.Record([[[0, 0], [0, 1], [1, 1], [0, 0]]], ['car', 'truck'], some_property='some property', another_property=45) with pytest.raises(ValueError, match='are not part of the taxonomy'): rc.append(r2) rc.taxonomy = Taxonomy( Label('road vehicle', children=(Label('truck'), )), Label('car')) rc.append(r2) assert rc.get()[0].labels == (Label('car'), Label('road vehicle')) assert rc.get()[1].labels == (Label('car'), Label('truck')) assert tuple(label.labels for label in rc.get()[0:]) == (rc.get()[0].labels, rc.get()[1].labels) assert rc.get(max_depth=1)[0].labels == (Label('car'), Label('road vehicle')) assert rc.get(max_depth=1)[1].labels == (Label('car'), Label('road vehicle')) assert tuple(label.labels for label in rc.get(max_depth=1)[0:]) == (rc.get( max_depth=1)[0].labels, rc.get(max_depth=1)[1].labels) assert rc.get( max_depth={'road vehicle': 0})[0].labels == (Label('car'), Label('road vehicle')) assert rc.get( max_depth={'road vehicle': 0})[1].labels == (Label('car'), Label('road vehicle')) assert tuple(label.labels for label in rc.get(max_depth={'road vehicle': 0})[0:]) \ == (rc.get(max_depth={'road vehicle': 0})[0].labels, rc.get(max_depth={'road vehicle': 0})[1].labels) assert rc.get( max_depth={'car': 0})[0].labels == (Label('car'), Label('road vehicle')) assert rc.get(max_depth={'car': 0})[1].labels == (Label('car'), Label('truck')) assert tuple(label.labels for label in rc.get(max_depth={'car': 0})[0:]) \ == (rc.get(max_depth={'car': 0})[0].labels, rc.get(max_depth={'car': 0})[1].labels) assert len(rc) == 2 assert rc[1] == r2 assert geojson.dumps(rc, sort_keys=True) == '{"features": [' \ '{"geometry": {"coordinates": [0, 1, 2], "type": "Point"}, ' \ '"properties": {"another_property": 45, ' \ '"category": ["car", "road vehicle"], ' \ '"confidence": null, "some_property": "some property"}, ' \ '"type": "Feature"}, ' \ '{"geometry": {"coordinates": [[[0, 0], [0, 1], [1, 1], [0, 0]]], '\ '"type": "Polygon"}, ' \ '"properties": {"another_property": 45, ' \ '"category": ["car", "truck"], ' \ '"confidence": null, ' \ '"some_property": "some property"}, "type": "Feature"}], ' \ '"type": "FeatureCollection"}' with pytest.raises(ValueError, match='are not part of'): rc[1] = data.Record([[[0, 0], [0, 1], [1, 1], [0, 0]]], ['car', 'trucks'], some_property='some property', another_property=45) with pytest.raises( ValueError, match='Some labels are part of the same true-root subtree'): rc[1] = data.Record([[[0, 0], [0, 1], [1, 1], [0, 0]]], ['road vehicle', 'truck'], some_property='some property', another_property=45) rc[1] = data.Record([[[0, 0], [0, 1], [1, 1], [0, 0]]], ['car'], some_property='some property', another_property=45) assert rc[1].labels == (Label('car'), )