/
test_visualization.py
210 lines (161 loc) · 6.84 KB
/
test_visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from pathlib import Path
import tarfile
from shutil import copyfile
import pytest
import geopandas as gpd
from mock import patch
import pandas as pd
import folium
from shapely import wkt
from folium import Map
from .context import folium_base_map, VizTools
# pylint: disable=unused-import,wrong-import-order
from .fixtures import auth_mock, job_mock
# pylint: disable=unused-argument
def test_folium_base_map():
m = folium_base_map()
assert isinstance(m, folium.Map)
assert m.crs == "EPSG3857"
@patch("matplotlib.pyplot.show")
def test_plot_result_job(capture_canvas, job_mock):
fp_tif = Path(__file__).resolve().parent / "mock_data/s2_128.jpg"
job_mock.results = [fp_tif]
job_mock.plot_results(plot_file_format=[".jpg"])
@patch("matplotlib.pyplot.show")
def test_plot_result_alternative_filepaths_and_titles(capture_canvas):
fp_tif = Path(__file__).resolve().parent / "mock_data/s2_128.jpg"
VizTools().plot_results(
filepaths=[fp_tif, fp_tif, fp_tif],
titles=["a", "b", "c"],
plot_file_format=[".jpg"],
)
def test_plot_result_not_accepted_file_format_raises():
filepaths = [Path("abc/123.hdf", "abc/123.json")]
with pytest.raises(ValueError):
VizTools().plot_results(filepaths=filepaths)
@patch("matplotlib.pyplot.show")
def test_plot_quicklooks(capture_canvas, job_mock):
fp_quicklooks = Path(__file__).resolve().parent / "mock_data/a_quicklook.png"
job_mock.quicklooks = [fp_quicklooks]
job_mock.plot_quicklooks()
@patch("matplotlib.pyplot.show")
def test_plot_quicklooks_alternative_filepaths(capture_canvas):
fp_quicklook = Path(__file__).resolve().parent / "mock_data/a_quicklook.png"
VizTools().plot_quicklooks(filepaths=[fp_quicklook, fp_quicklook, fp_quicklook])
@patch("matplotlib.pyplot.show")
def test_plot_coverage(capture_canvas):
df = gpd.read_file(
Path(__file__).resolve().parent
/ "mock_data/search_results_limited_columns.geojson",
as_dataframe=True,
)
VizTools().plot_coverage(df)
@patch("matplotlib.pyplot.show")
def test_plot_coverage_wrong_legend_column_ignores(capture_canvas):
df = gpd.read_file(
Path(__file__).resolve().parent
/ "mock_data/search_results_limited_columns.geojson",
as_dataframe=True,
)
VizTools().plot_coverage(df, legend_column="abcdefgh")
def test_map_images_2_scenes():
plot_file_format = [".jpg"]
result_csv = Path(__file__).resolve().parent / "mock_data/df_2scenes.csv"
result_df = pd.read_csv(result_csv)
result_df["geometry"] = result_df["geometry"].apply(wkt.loads)
gdf = gpd.GeoDataFrame(result_df, geometry="geometry")
quicklook_1 = (
Path(__file__).resolve().parent
/ "mock_data/quicklooks/quicklook_16e18e15-c941-4aae-97cd-d67b18dc9f6e.jpg"
)
quicklook_2 = (
Path(__file__).resolve().parent
/ "mock_data/quicklooks/quicklook_f8c03432-cec1-41b7-a203-4d871a03290f.jpg"
)
filepaths = [quicklook_1, quicklook_2]
m = VizTools()._map_images(plot_file_format, gdf, filepaths)
m._repr_html_()
# Render does not return an object but an html document (as expected)
out = m._parent.render() # pylint: disable=assignment-from-no-return
assert "Image 1 - f8c03432-cec1-41b7-a203-4d871a03290f" in out
assert "Image 2 - 16e18e15-c941-4aae-97cd-d67b18dc9f6e" in out
def test_map_images_2_scenes_no_column_name():
plot_file_format = [".jpg"]
result_csv = Path(__file__).resolve().parent / "mock_data/df_2scenes.csv"
result_df = pd.read_csv(result_csv)
result_df["geometry"] = result_df["geometry"].apply(wkt.loads)
gdf = gpd.GeoDataFrame(result_df, geometry="geometry")
quicklook_1 = (
Path(__file__).resolve().parent
/ "mock_data/quicklooks/quicklook_16e18e15-c941-4aae-97cd-d67b18dc9f6e.jpg"
)
quicklook_2 = (
Path(__file__).resolve().parent
/ "mock_data/quicklooks/quicklook_f8c03432-cec1-41b7-a203-4d871a03290f.jpg"
)
filepaths = [quicklook_1, quicklook_2]
m = VizTools()._map_images(plot_file_format, gdf, filepaths, name_column="nikoo")
m._repr_html_()
out = m._parent.render() # pylint: disable=assignment-from-no-return
assert "Image 1 - " in out
assert "Image 2 - " in out
def test_map_images_1_scene():
plot_file_format = [".jpg"]
result_csv = Path(__file__).resolve().parent / "mock_data/df_1scene.csv"
result_df = pd.read_csv(result_csv)
result_df["geometry"] = result_df["geometry"].apply(wkt.loads)
gdf = gpd.GeoDataFrame(result_df, geometry="geometry")
quicklook = (
Path(__file__).resolve().parent
/ "mock_data/quicklooks/quicklook_16e18e15-c941-4aae-97cd-d67b18dc9f6e.jpg"
)
filepaths = [quicklook]
m = VizTools()._map_images(plot_file_format, gdf, filepaths)
m._repr_html_()
out = m._parent.render() # pylint: disable=assignment-from-no-return
assert "Image 1 - 2a581680-17e4-4a61-8aa9-9e47e1bf36bb" in out
def test_map_results(job_mock):
fp_tgz = Path(__file__).resolve().parent / "mock_data/result_tif.tgz"
with tarfile.open(fp_tgz) as tar:
tar.extractall(fp_tgz.parent)
fp_tif = (
fp_tgz.parent
/ "output/7e17f023-a8e3-43bd-aaac-5bbef749c7f4/7e17f023-a8e3-43bd-aaac-5bbef749c7f4_0-0.tif"
)
fp_data_json = fp_tgz.parent / "output/data.json"
job_mock.results = [str(fp_tif), str(fp_data_json)]
map_object = job_mock.map_results()
assert isinstance(map_object, Map)
def test_map_results_jobcollection(job_mock):
fp_tgz = Path(__file__).resolve().parent / "mock_data/result_tif.tgz"
fp_merged = (
Path(__file__).resolve().parent / "mock_data/job_collection_merged_result.json"
)
with tarfile.open(fp_tgz) as tar:
tar.extractall(fp_tgz.parent)
fp_tif = (
fp_tgz.parent
/ "output/7e17f023-a8e3-43bd-aaac-5bbef749c7f4/7e17f023-a8e3-43bd-aaac-5bbef749c7f4_0-0.tif"
)
my_dict = {
"job_id_123": [str(fp_tif), f"{str(fp_tgz.parent)}/output/data.json"],
"jobid_456": [str(fp_tif), f"{str(fp_tgz.parent)}/output/data.json"],
"merged_result": [fp_merged],
}
job_mock.results = my_dict
map_object = job_mock.map_results()
assert isinstance(map_object, Map)
def test_map_results_additional_geojson(job_mock):
fp_tgz = Path(__file__).resolve().parent / "mock_data/result_tif.tgz"
with tarfile.open(fp_tgz) as tar:
tar.extractall(fp_tgz.parent)
fp_tif = (
fp_tgz.parent
/ "output/7e17f023-a8e3-43bd-aaac-5bbef749c7f4/7e17f023-a8e3-43bd-aaac-5bbef749c7f4_0-0.tif"
)
fp_data_json = fp_tgz.parent / "output/data.json"
fp_data_geojson = fp_tgz.parent / "output/additional_vector_file.geojson"
copyfile(fp_data_json, fp_data_geojson)
job_mock.results = [str(fp_tif), str(fp_data_json), str(fp_data_geojson)]
map_object = job_mock.map_results()
assert isinstance(map_object, Map)