|
| 1 | +"""Test that the following CLI command returns the expected outputs |
| 2 | +label-maker labels --dest integration-cl --config test/fixtures/integration/config.geojson.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 TestClassificationLabelGeoJSON(unittest.TestCase): |
| 12 | + """Tests for classification label creation""" |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + makedirs('integration-cl') |
| 16 | + copyfile('test/fixtures/integration/labels.geojson', 'integration-cl/labels.geojson') |
| 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: 8 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.geojson.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([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]) |
| 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) |
0 commit comments