Skip to content

Commit 81d7ba6

Browse files
wouellettewouellette
wouellette
authored and
wouellette
committed
updated unit tests
1 parent c559fcf commit 81d7ba6

16 files changed

+146
-49
lines changed

label_maker/label.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def make_labels(dest_folder, zoom, country, classes, ml_type, bounding_box, spar
122122

123123
# write out labels as numpy arrays
124124
labels_file = op.join(dest_folder, 'labels.npz')
125-
# print('Writing out labels to {}'.format(labels_file))
125+
print('Writing out labels to {}'.format(labels_file))
126126
np.savez(labels_file, **tile_results)
127127

128128
# write out labels as GeoJSON or PNG

label_maker/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ def cli():
8484
raise Exception(v.errors)
8585

8686
# for aoi, overwrite bounding_box config key to correct labelling
87-
if 'aoi' in config.keys:
87+
if 'aoi' in config.keys():
8888
config['bounding_box'] = get_bounds(json.load(open(config.get('aoi'), 'r')))
8989

9090
# custom validation for top level keys
9191
# require either: country & bounding_box or geojson
92-
if 'geojson' not in config.keys()) and not ('country' in config.keys() and 'bounding_box' in config.keys()):
92+
if 'geojson' not in config.keys() and not ('country' in config.keys() and 'bounding_box' in config.keys()):
9393
raise Exception('either "geojson" or "country" and "bounding_box" must be present in the configuration JSON')
9494

9595
# for geojson, overwrite other config keys to correct labeling

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Pillow==4.3.0
1212
protobuf==3.5.0.post1
1313
pyclipper==1.0.6
1414
pycurl==7.43.0.1
15-
pyproj==1.9.5.1
16-
rasterio==1.0a12
15+
pyproj==2.1.3
16+
rasterio==1.0.21
1717
requests>=2.20.0
1818
Shapely>=1.6.3
1919
six==1.10.0
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"country": ["portugal"],
3+
"aoi": "test/fixtures/integration/portugal_aoi.geojson",
4+
"zoom": 17,
5+
"classes": [
6+
{ "name": "Water Tower", "filter": ["==", "man_made", "water_tower"] },
7+
{ "name": "Building", "filter": ["has", "building"] },
8+
{ "name": "Farmland", "filter": ["==", "landuse", "farmland"] },
9+
{ "name": "Ruins", "filter": ["==", "historic", "ruins"] },
10+
{ "name": "Parking", "filter": ["==", "amenity", "parking"] },
11+
{ "name": "Roads", "filter": ["has", "highway"] }
12+
],
13+
"imagery": "https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.jpg?access_token=ACCESS_TOKEN",
14+
"background_ratio": 1,
15+
"ml_type": "classification",
16+
"seed": 19
17+
}

test/fixtures/integration/config.integration.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"country": "portugal",
2+
"country": ["portugal"],
33
"bounding_box": [
44
-9.4575,
55
38.8467,

test/fixtures/integration/config.integration.object_detection.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"country": "portugal",
2+
"country": ["portugal"],
33
"bounding_box": [
44
-9.4575,
55
38.8467,

test/fixtures/integration/config.integration.segmentation.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"country": "portugal",
2+
"country": ^["portugal"],
33
"bounding_box": [
44
-9.4575,
55
38.8467,

test/fixtures/integration/config.integration.segmentation_sparse.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"country": "portugal",
2+
"country": ["portugal"],
33
"bounding_box": [
44
-9.4575,
55
38.8467,

test/fixtures/integration/config.integration_sparse.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"country": "portugal",
2+
"country": ["portugal"],
33
"bounding_box": [
44
-9.4575,
55
38.8467,

test/fixtures/integration/config.intergration.geotiff_package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"country": "sao_tome_and_principe",
2+
"country": ["sao_tome_and_principe"],
33
"bounding_box": [6.72746119738703,0.3382909054246151,6.72776258384623,0.33878086940393715],
44
"zoom": 20,
55
"classes": [

test/fixtures/integration/portugal_aoi.geojson

+20
Large diffs are not rendered by default.

test/integration/test_classification_labels.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_cli(self):
2929
Farmland: 0 tiles
3030
Ruins: 1 tiles
3131
Parking: 1 tiles
32-
Roads: 8 tiles
32+
Roads: 0 tiles
3333
Total tiles: 9
3434
Writing out labels to integration-cl/labels.npz
3535
"""
@@ -41,15 +41,15 @@ def test_cli(self):
4141

4242
# our labels should look like this
4343
expected_labels = {
44-
'62092-50162-17': np.array([1, 0, 0, 0, 0, 0, 0]),
45-
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 1]),
46-
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
47-
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
48-
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
49-
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
50-
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
51-
'62094-50163-17': np.array([0, 1, 1, 0, 0, 0, 1]),
52-
'62093-50163-17': np.array([0, 0, 0, 0, 1, 1, 1])
44+
'62092-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
45+
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 0]),
46+
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
47+
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
48+
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
49+
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
50+
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
51+
'62094-50163-17': np.array([0, 319, 0.5, 0, 0, 0, 0]),
52+
'62093-50163-17': np.array([0, 0, 0, 0, 217, 1268, 0])
5353
}
5454

5555
labels = np.load('integration-cl/labels.npz')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Test that the following CLI command returns the expected outputs
2+
label-maker labels --dest integration-cl --config test/fixtures/integration/config.integration.json"""
3+
import unittest
4+
import json
5+
from os import makedirs
6+
from shutil import copyfile, rmtree
7+
import subprocess
8+
9+
import numpy as np
10+
11+
class TestClassificationLabel(unittest.TestCase):
12+
"""Tests for classification label creation"""
13+
@classmethod
14+
def setUpClass(cls):
15+
makedirs('integration-cl')
16+
copyfile('test/fixtures/integration/portugal-z17.mbtiles', 'integration-cl/portugal-z17.mbtiles')
17+
18+
@classmethod
19+
def tearDownClass(cls):
20+
rmtree('integration-cl')
21+
22+
def test_cli(self):
23+
"""Verify stdout, geojson, and labels.npz produced by CLI"""
24+
# our command line output should look like this
25+
expected_output = """Determining labels for each tile
26+
---
27+
Water Tower: 1 tiles
28+
Building: 1 tiles
29+
Farmland: 0 tiles
30+
Ruins: 1 tiles
31+
Parking: 1 tiles
32+
Roads: 0 tiles
33+
Total tiles: 9
34+
Writing out labels to integration-cl/labels.npz
35+
"""
36+
37+
cmd = 'label-maker labels --dest integration-cl --config test/fixtures/integration/config.integration.json'
38+
cmd = cmd.split(' ')
39+
with subprocess.Popen(cmd, universal_newlines=True, stdout=subprocess.PIPE) as p:
40+
self.assertEqual(expected_output, p.stdout.read())
41+
42+
# our labels should look like this
43+
expected_labels = {
44+
'62092-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
45+
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 0]),
46+
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
47+
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
48+
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
49+
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
50+
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
51+
'62094-50163-17': np.array([0, 319, 0.5, 0, 0, 0, 0]),
52+
'62093-50163-17': np.array([0, 0, 0, 0, 217, 1268, 0])
53+
}
54+
55+
labels = np.load('integration-cl/labels.npz')
56+
self.assertEqual(len(labels.files), len(expected_labels.keys())) # First check number of tiles
57+
for tile in labels.files:
58+
self.assertTrue(np.array_equal(expected_labels[tile], labels[tile])) # Now, content
59+
60+
# our GeoJSON looks like the fixture
61+
with open('test/fixtures/integration/classification.geojson') as fixture:
62+
with open('integration-cl/classification.geojson') as geojson_file:
63+
expected_geojson = json.load(fixture)
64+
geojson = json.load(geojson_file)
65+
66+
self.assertCountEqual(expected_geojson, geojson)

test/integration/test_classification_labels_geojson.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_cli(self):
2929
Farmland: 0 tiles
3030
Ruins: 1 tiles
3131
Parking: 1 tiles
32-
Roads: 8 tiles
32+
Roads: 0 tiles
3333
Total tiles: 9
3434
Writing out labels to integration-cl/labels.npz
3535
"""
@@ -41,15 +41,15 @@ def test_cli(self):
4141

4242
# our labels should look like this
4343
expected_labels = {
44-
'62092-50162-17': np.array([1, 0, 0, 0, 0, 0, 0]),
45-
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 1]),
46-
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
47-
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
48-
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
49-
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
50-
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
51-
'62094-50163-17': np.array([0, 1, 1, 0, 0, 0, 1]),
52-
'62093-50163-17': np.array([0, 0, 0, 0, 1, 1, 1])
44+
'62092-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
45+
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 0]),
46+
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
47+
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
48+
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
49+
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
50+
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
51+
'62094-50163-17': np.array([0, 319, 0.5, 0, 0, 0, 0]),
52+
'62093-50163-17': np.array([0, 0, 0, 0, 217, 1268, 0])
5353
}
5454

5555
labels = np.load('integration-cl/labels.npz')

test/integration/test_classification_labels_sparse.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_cli(self):
2929
Farmland: 0 tiles
3030
Ruins: 1 tiles
3131
Parking: 1 tiles
32-
Roads: 8 tiles
32+
Roads: 0 tiles
3333
Total tiles: 9
3434
Using sparse mode; subselected 0 background tiles
3535
Writing out labels to integration-cl/labels.npz
@@ -42,14 +42,8 @@ def test_cli(self):
4242

4343
# our labels should look like this
4444
expected_labels = {
45-
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 1]),
46-
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
47-
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
48-
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
49-
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
50-
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
51-
'62094-50163-17': np.array([0, 1, 1, 0, 0, 0, 1]),
52-
'62093-50163-17': np.array([0, 0, 0, 0, 1, 1, 1])
45+
'62094-50163-17': np.array([0, 319, 0.5, 0, 0, 0, 0]),
46+
'62093-50163-17': np.array([0, 0, 0, 0, 217, 1268, 0])
5347
}
5448

5549
labels = np.load('integration-cl/labels.npz')

test/integration/test_directory_move.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ def test_cli(self):
3333

3434
# our labels should look like this
3535
expected_labels = {
36-
'62092-50162-17': np.array([1, 0, 0, 0, 0, 0, 0]),
37-
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 1]),
38-
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
39-
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
40-
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
41-
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 1]),
42-
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 1]),
43-
'62094-50163-17': np.array([0, 1, 1, 0, 0, 0, 1]),
44-
'62093-50163-17': np.array([0, 0, 0, 0, 1, 1, 1])
36+
'62092-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
37+
'62092-50163-17': np.array([0, 0, 0, 0, 0, 0, 0]),
38+
'62092-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
39+
'62093-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
40+
'62093-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
41+
'62094-50162-17': np.array([0, 0, 0, 0, 0, 0, 0]),
42+
'62094-50164-17': np.array([0, 0, 0, 0, 0, 0, 0]),
43+
'62094-50163-17': np.array([0, 319, 0.5, 0, 0, 0, 0]),
44+
'62093-50163-17': np.array([0, 0, 0, 0, 217, 1268, 0])
4545
}
4646

4747
# move back into the directory

0 commit comments

Comments
 (0)