Skip to content

Commit ec4e0ef

Browse files
author
Rodrigo Almeida
authored
Format with black and isort (#448)
1 parent 5400545 commit ec4e0ef

File tree

20 files changed

+565
-407
lines changed

20 files changed

+565
-407
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import sys
1515

1616
sys.path.insert(0, os.path.abspath(".."))
17-
import sphinx_bootstrap_theme
1817
import numpy as np
18+
import sphinx_bootstrap_theme
1919

2020
# -- Project information -----------------------------------------------------
2121

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import os
2-
import sys
3-
import subprocess
41
import logging
5-
from setuptools import setup, find_packages
2+
import os
63
import re
4+
import subprocess
5+
import sys
6+
7+
from setuptools import find_packages, setup
78

89

910
def get_version():

solaris/tile/raster_tile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# from rio_cogeo.cogeo import cog_validate, cog_translate
1212
from ..utils.core import _check_crs, _check_rasterio_im_load
13+
1314
# removing the following until COG functionality is implemented
1415
# from ..utils.tile import read_cog_tile
1516
from ..utils.geo import raster_get_projection_unit, reproject, split_geom

tests/test_cli/compare.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
import os
2-
import numpy as np
3-
import skimage
42
import pickle
5-
import networkx as nx
63
import sys
74

8-
im_fnames = ['sample_fp_mask.tif',
9-
'sample_fbc_mask.tif',
10-
'sample_c_mask.tif',
11-
'sample_b_inner_mask.tif',
12-
'sample_b_outer10_mask.tif']
5+
import networkx as nx
6+
import numpy as np
7+
import skimage
8+
9+
im_fnames = [
10+
"sample_fp_mask.tif",
11+
"sample_fbc_mask.tif",
12+
"sample_c_mask.tif",
13+
"sample_b_inner_mask.tif",
14+
"sample_b_outer10_mask.tif",
15+
]
1316

1417

1518
def main(path):
1619
os.chdir(path) # set to the directory containing this script
1720

1821
# compare expected mask results and truth images
1922
for im_fname in im_fnames:
20-
truth_im = skimage.io.imread(os.path.join('expected', im_fname))
21-
result_im = skimage.io.imread(os.path.join('results', im_fname))
23+
truth_im = skimage.io.imread(os.path.join("expected", im_fname))
24+
result_im = skimage.io.imread(os.path.join("results", im_fname))
2225
assert np.array_equal(truth_im, result_im)
2326

2427
# compare graphs
25-
with open(os.path.join('expected', 'sample_graph.pkl'), 'rb') as f:
28+
with open(os.path.join("expected", "sample_graph.pkl"), "rb") as f:
2629
truth_graph = pickle.load(f)
2730
f.close()
28-
with open(os.path.join('results', 'sample_graph.pkl'), 'rb') as f:
31+
with open(os.path.join("results", "sample_graph.pkl"), "rb") as f:
2932
result_graph = pickle.load(f)
3033
f.close()
3134

3235
assert nx.is_isomorphic(truth_graph, result_graph)
3336

3437

35-
if __name__ == '__main__':
38+
if __name__ == "__main__":
3639
main(sys.argv[1])

tests/test_cli/test_cli.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
2-
import numpy as np
32
import pickle
43
import subprocess
5-
import skimage.io
4+
65
import networkx as nx
6+
import numpy as np
7+
import skimage.io
78

89
data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/"))
910

@@ -65,7 +66,9 @@ def test_make_masks(self):
6566
if os.path.exists(os.path.join(dest_dir, im_fname)):
6667
os.remove(os.path.join(dest_dir, im_fname))
6768
# run the CLI command
68-
subprocess.run(cmd_start + os.path.join(dest_dir, im_fname) + arg, shell=True)
69+
subprocess.run(
70+
cmd_start + os.path.join(dest_dir, im_fname) + arg, shell=True
71+
)
6972
truth_im = skimage.io.imread(os.path.join(expected_dir, im_fname))
7073
result_im = skimage.io.imread(os.path.join(dest_dir, im_fname))
7174
# compare the expected to the result

tests/test_data/test_coco.py

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,81 @@
1-
from solaris.data.coco import geojson2coco
21
import json
32
import os
43

4+
from solaris.data.coco import geojson2coco
5+
56
data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/"))
67

8+
79
class TestGeoJSON2COCO(object):
810
"""Tests for the ``geojson2coco`` function."""
911

1012
def test_multiclass_single_geojson(self):
11-
sample_geojson = os.path.join(data_dir, 'geotiff_labels.geojson')
12-
sample_image = os.path.join(data_dir, 'sample_geotiff.tif')
13-
14-
coco_dict = geojson2coco(sample_image, sample_geojson,
15-
category_attribute='truncated',
16-
output_path=os.path.join(data_dir,
17-
'tmp_coco.json'))
18-
with open(os.path.join(data_dir, 'coco_sample_2.json'), 'r') as f:
13+
sample_geojson = os.path.join(data_dir, "geotiff_labels.geojson")
14+
sample_image = os.path.join(data_dir, "sample_geotiff.tif")
15+
16+
coco_dict = geojson2coco(
17+
sample_image,
18+
sample_geojson,
19+
category_attribute="truncated",
20+
output_path=os.path.join(data_dir, "tmp_coco.json"),
21+
)
22+
with open(os.path.join(data_dir, "coco_sample_2.json"), "r") as f:
1923
expected_dict = json.load(f)
20-
with open(os.path.join(data_dir, 'tmp_coco.json'), 'r') as f:
24+
with open(os.path.join(data_dir, "tmp_coco.json"), "r") as f:
2125
saved_result = json.load(f)
22-
## Simplified test due to rounding errors- JSS
23-
assert coco_dict['annotations'][0]['bbox'] == expected_dict['annotations'][0]['bbox']
24-
assert saved_result['annotations'][0]['bbox'] == expected_dict['annotations'][0]['bbox']
25-
os.remove(os.path.join(data_dir, 'tmp_coco.json'))
26+
## Simplified test due to rounding errors- JSS
27+
assert (
28+
coco_dict["annotations"][0]["bbox"]
29+
== expected_dict["annotations"][0]["bbox"]
30+
)
31+
assert (
32+
saved_result["annotations"][0]["bbox"]
33+
== expected_dict["annotations"][0]["bbox"]
34+
)
35+
os.remove(os.path.join(data_dir, "tmp_coco.json"))
2636

2737
def test_singleclass_multi_geojson(self):
28-
sample_geojsons = [os.path.join(data_dir, 'vectortile_test_expected/geoms_733601_3724734.geojson'),
29-
os.path.join(data_dir, 'vectortile_test_expected/geoms_733601_3724869.geojson')]
30-
sample_images = [os.path.join(data_dir, 'rastertile_test_expected/sample_geotiff_733601_3724734.tif'),
31-
os.path.join(data_dir, 'rastertile_test_expected/sample_geotiff_733601_3724869.tif')]
32-
33-
coco_dict = geojson2coco(sample_images,
34-
sample_geojsons,
35-
matching_re=r'(\d+_\d+)',
36-
license_dict={'CC-BY 4.0': 'https://creativecommons.org/licenses/by/4.0/'},
37-
verbose=0)
38-
39-
with open(os.path.join(data_dir, 'coco_sample_1.json'), 'r') as f:
38+
sample_geojsons = [
39+
os.path.join(
40+
data_dir, "vectortile_test_expected/geoms_733601_3724734.geojson"
41+
),
42+
os.path.join(
43+
data_dir, "vectortile_test_expected/geoms_733601_3724869.geojson"
44+
),
45+
]
46+
sample_images = [
47+
os.path.join(
48+
data_dir, "rastertile_test_expected/sample_geotiff_733601_3724734.tif"
49+
),
50+
os.path.join(
51+
data_dir, "rastertile_test_expected/sample_geotiff_733601_3724869.tif"
52+
),
53+
]
54+
55+
coco_dict = geojson2coco(
56+
sample_images,
57+
sample_geojsons,
58+
matching_re=r"(\d+_\d+)",
59+
license_dict={"CC-BY 4.0": "https://creativecommons.org/licenses/by/4.0/"},
60+
verbose=0,
61+
)
62+
63+
with open(os.path.join(data_dir, "coco_sample_1.json"), "r") as f:
4064
expected_dict = json.load(f)
4165
## Simplified test due to rounding errors- JSS
42-
assert expected_dict['annotations'][0]['bbox'] == coco_dict['annotations'][0]['bbox']
66+
assert (
67+
expected_dict["annotations"][0]["bbox"]
68+
== coco_dict["annotations"][0]["bbox"]
69+
)
4370

4471
def test_from_directories(self):
45-
sample_geojsons = os.path.join(data_dir, 'vectortile_test_expected')
46-
sample_images = os.path.join(data_dir, 'rastertile_test_expected')
47-
coco_dict = geojson2coco(sample_images,
48-
sample_geojsons,
49-
matching_re=r'(\d+_\d+)',
50-
verbose=0)
51-
with open(os.path.join(data_dir, 'coco_sample_3.json'), 'r') as f:
72+
sample_geojsons = os.path.join(data_dir, "vectortile_test_expected")
73+
sample_images = os.path.join(data_dir, "rastertile_test_expected")
74+
coco_dict = geojson2coco(
75+
sample_images, sample_geojsons, matching_re=r"(\d+_\d+)", verbose=0
76+
)
77+
with open(os.path.join(data_dir, "coco_sample_3.json"), "r") as f:
5278
expected_dict = json.load(f)
5379
# this test had issues due to rounding errors, I therefore lowered the
5480
# barrier to passing - NW
55-
assert len(expected_dict['annotations']) == len(coco_dict['annotations'])
81+
assert len(expected_dict["annotations"]) == len(coco_dict["annotations"])

tests/test_eval/evaluator_test.py

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import os
2-
from solaris.eval.base import Evaluator
3-
import solaris
2+
43
import geopandas as gpd
54
import pandas as pd
65

6+
import solaris
7+
from solaris.eval.base import Evaluator
8+
79
data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/"))
810

11+
912
class TestEvaluator(object):
1013
def test_init_from_file(self):
1114
"""Test instantiation of an Evaluator instance from a file."""
12-
base_instance = Evaluator(os.path.join(data_dir,
13-
'gt.geojson'))
15+
base_instance = Evaluator(os.path.join(data_dir, "gt.geojson"))
1416
gdf = solaris.data.gt_gdf()
1517
assert base_instance.ground_truth_sindex.bounds == gdf.sindex.bounds
1618
assert base_instance.proposal_GDF.equals(gpd.GeoDataFrame([]))
1719
assert base_instance.ground_truth_GDF.equals(
18-
base_instance.ground_truth_GDF_Edit)
20+
base_instance.ground_truth_GDF_Edit
21+
)
1922

2023
def test_init_from_gdf(self):
2124
"""Test instantiation of an Evaluator from a pre-loaded GeoDataFrame."""
@@ -24,72 +27,76 @@ def test_init_from_gdf(self):
2427
assert base_instance.ground_truth_sindex.bounds == gdf.sindex.bounds
2528
assert base_instance.proposal_GDF.equals(gpd.GeoDataFrame([]))
2629
assert base_instance.ground_truth_GDF.equals(
27-
base_instance.ground_truth_GDF_Edit)
30+
base_instance.ground_truth_GDF_Edit
31+
)
2832

2933
def test_init_empty_geojson(self):
3034
"""Test instantiation of Evaluator with an empty geojson file."""
31-
base_instance = Evaluator(os.path.join(data_dir,
32-
'empty.geojson'))
33-
expected_gdf = gpd.GeoDataFrame({'sindex': [],
34-
'condition': [],
35-
'geometry': []})
35+
base_instance = Evaluator(os.path.join(data_dir, "empty.geojson"))
36+
expected_gdf = gpd.GeoDataFrame({"sindex": [], "condition": [], "geometry": []})
3637
assert base_instance.ground_truth_GDF.equals(expected_gdf)
3738

3839
def test_score_proposals(self):
3940
"""Test reading in a proposal GDF from a geojson and scoring it."""
40-
eb = Evaluator(os.path.join(data_dir, 'gt.geojson'))
41-
eb.load_proposal(os.path.join(data_dir, 'pred.geojson'))
41+
eb = Evaluator(os.path.join(data_dir, "gt.geojson"))
42+
eb.load_proposal(os.path.join(data_dir, "pred.geojson"))
4243
pred_gdf = solaris.data.pred_gdf()
4344
assert eb.proposal_GDF.iloc[:, 0:3].sort_index().equals(pred_gdf)
44-
expected_score = [{'class_id': 'all',
45-
'iou_field': 'iou_score_all',
46-
'TruePos': 8,
47-
'FalsePos': 20,
48-
'FalseNeg': 20,
49-
'Precision': 0.2857142857142857,
50-
'Recall': 0.2857142857142857,
51-
'F1Score': 0.2857142857142857}]
45+
expected_score = [
46+
{
47+
"class_id": "all",
48+
"iou_field": "iou_score_all",
49+
"TruePos": 8,
50+
"FalsePos": 20,
51+
"FalseNeg": 20,
52+
"Precision": 0.2857142857142857,
53+
"Recall": 0.2857142857142857,
54+
"F1Score": 0.2857142857142857,
55+
}
56+
]
5257
scores = eb.eval_iou(calculate_class_scores=False)
5358
assert scores == expected_score
5459

5560
def test_score_proposals_return_gdfs(self):
56-
eb = Evaluator(os.path.join(data_dir, 'gt.geojson'))
57-
eb.load_proposal(os.path.join(data_dir, 'pred.geojson'))
58-
expected_score = [{'class_id': 'all',
59-
'iou_field': 'iou_score_all',
60-
'TruePos': 8,
61-
'FalsePos': 20,
62-
'FalseNeg': 20,
63-
'Precision': 0.2857142857142857,
64-
'Recall': 0.2857142857142857,
65-
'F1Score': 0.2857142857142857}]
61+
eb = Evaluator(os.path.join(data_dir, "gt.geojson"))
62+
eb.load_proposal(os.path.join(data_dir, "pred.geojson"))
63+
expected_score = [
64+
{
65+
"class_id": "all",
66+
"iou_field": "iou_score_all",
67+
"TruePos": 8,
68+
"FalsePos": 20,
69+
"FalseNeg": 20,
70+
"Precision": 0.2857142857142857,
71+
"Recall": 0.2857142857142857,
72+
"F1Score": 0.2857142857142857,
73+
}
74+
]
6675
scores, tp_gdf, fn_gdf, fp_gdf = eb.eval_iou_return_GDFs(
67-
calculate_class_scores=False)
76+
calculate_class_scores=False
77+
)
6878
assert scores == expected_score
69-
assert len(tp_gdf) == expected_score[0]['TruePos']
70-
assert len(fp_gdf) == expected_score[0]['FalsePos']
71-
assert len(fn_gdf) == expected_score[0]['FalseNeg']
79+
assert len(tp_gdf) == expected_score[0]["TruePos"]
80+
assert len(fp_gdf) == expected_score[0]["FalsePos"]
81+
assert len(fn_gdf) == expected_score[0]["FalseNeg"]
7282

7383
def test_iou_by_building(self):
7484
"""Test output of ground truth table with per-building IoU scores"""
7585
data_folder = data_dir
76-
path_truth = os.path.join(data_folder, 'SN2_sample_truth.csv')
77-
path_pred = os.path.join(data_folder, 'SN2_sample_preds.csv')
78-
path_ious = os.path.join(data_folder, 'SN2_sample_iou_by_building.csv')
79-
path_temp = './temp.pd'
86+
path_truth = os.path.join(data_folder, "SN2_sample_truth.csv")
87+
path_pred = os.path.join(data_folder, "SN2_sample_preds.csv")
88+
path_ious = os.path.join(data_folder, "SN2_sample_iou_by_building.csv")
89+
path_temp = "./temp.pd"
8090
eb = Evaluator(path_truth)
81-
eb.load_proposal(path_pred, conf_field_list=['Confidence'],
82-
proposalCSV=True)
83-
eb.eval_iou_spacenet_csv(miniou=0.5, imageIDField='ImageId',
84-
min_area=20)
91+
eb.load_proposal(path_pred, conf_field_list=["Confidence"], proposalCSV=True)
92+
eb.eval_iou_spacenet_csv(miniou=0.5, imageIDField="ImageId", min_area=20)
8593
output = eb.get_iou_by_building()
8694
result_actual = pd.DataFrame(output)
87-
result_actual.sort_values(by=['ImageId', 'BuildingId'], inplace=True)
88-
ious_actual = list(result_actual['iou_score'])
95+
result_actual.sort_values(by=["ImageId", "BuildingId"], inplace=True)
96+
ious_actual = list(result_actual["iou_score"])
8997
result_expected = pd.read_csv(path_ious, index_col=0)
90-
result_expected.sort_values(by=['ImageId', 'BuildingId'], inplace=True)
91-
ious_expected = list(result_expected['iou_score'])
92-
maxdifference = max([abs(x-y) for x, y in zip(ious_actual,
93-
ious_expected)])
94-
epsilon = 1E-9
98+
result_expected.sort_values(by=["ImageId", "BuildingId"], inplace=True)
99+
ious_expected = list(result_expected["iou_score"])
100+
maxdifference = max([abs(x - y) for x, y in zip(ious_actual, ious_expected)])
101+
epsilon = 1e-9
95102
assert maxdifference < epsilon

tests/test_eval/iou_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from solaris.eval.iou import calculate_iou, process_iou
2-
from solaris import data
3-
from shapely.geometry import Polygon
41
import numpy as np
2+
from shapely.geometry import Polygon
3+
4+
from solaris import data
5+
from solaris.eval.iou import calculate_iou, process_iou
56

67

78
class TestEvalFuncs(object):

0 commit comments

Comments
 (0)