-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathtest_utils.py
More file actions
118 lines (91 loc) · 3.77 KB
/
test_utils.py
File metadata and controls
118 lines (91 loc) · 3.77 KB
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
import sys
import os
import pytest
import numpy as np
from affine import Affine
from shapely.geometry import LineString, Polygon
from rasterstats.utils import \
stats_to_csv, get_percentile, remap_categories, boxify_points, \
rebin_sum, rasterize_pctcover_geom
from rasterstats import zonal_stats
from rasterstats.utils import VALID_STATS
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
DATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
raster = os.path.join(DATA, 'slope.tif')
def test_csv():
polygons = os.path.join(DATA, 'polygons.shp')
stats = zonal_stats(polygons, raster, stats="*")
csv = stats_to_csv(stats)
assert csv.split()[0] == ','.join(sorted(VALID_STATS))
def test_categorical_csv():
polygons = os.path.join(DATA, 'polygons.shp')
categorical_raster = os.path.join(DATA, 'slope_classes.tif')
stats = zonal_stats(polygons, categorical_raster, categorical=True)
csv = stats_to_csv(stats)
assert csv.split()[0] == "1.0,2.0,5.0"
def test_get_percentile():
assert get_percentile('percentile_0') == 0.0
assert get_percentile('percentile_100') == 100.0
assert get_percentile('percentile_13.2') == 13.2
def test_get_bad_percentile():
with pytest.raises(ValueError):
get_percentile('foo')
with pytest.raises(ValueError):
get_percentile('percentile_101')
with pytest.raises(ValueError):
get_percentile('percentile_101')
with pytest.raises(ValueError):
get_percentile('percentile_-1')
with pytest.raises(ValueError):
get_percentile('percentile_foobar')
def test_remap_categories():
feature_stats = {1: 22.343, 2: 54.34, 3: 987.5}
category_map = {1: 'grassland', 2: 'forest'}
new_stats = remap_categories(category_map, feature_stats)
assert 1 not in new_stats.keys()
assert 'grassland' in new_stats.keys()
assert 3 in new_stats.keys()
def test_boxify_non_point():
line = LineString([(0, 0), (1, 1)])
with pytest.raises(ValueError):
boxify_points(line, None)
def test_rebin_sum():
test_input = np.array(
[
[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4]
])
test_output = rebin_sum(test_input, (2,2), np.int32)
correct_output = np.array([[4, 8],[12, 16]])
assert np.array_equal(test_output, correct_output)
def test_rasterize_pctcover_geom():
# https://goodcode.io/articles/python-dict-object/
class objectview(object):
def __init__(self, d):
self.__dict__ = d
polygon_a = Polygon([[0, 0], [2, 0], [2, 2], [0, 2]])
shape_a = (2, 2)
affine_a = Affine(1, 0, 0,
0, -1, 2)
like_a = objectview({'shape': shape_a, 'affine': affine_a})
pct_cover_a = rasterize_pctcover_geom(polygon_a, like_a, scale=10, all_touched=False)
correct_output_a = np.array([[1, 1], [1, 1]])
assert np.array_equal(pct_cover_a, correct_output_a)
polygon_b = Polygon([[0.5, 0.5], [1.5, 0.5], [1.5, 1.5], [0.5, 1.5]])
shape_b = (2, 2)
affine_b = Affine(1, 0, 0,
0, -1, 2)
like_b = objectview({'shape': shape_b, 'affine': affine_b})
pct_cover_b = rasterize_pctcover_geom(polygon_b, like_b, scale=10, all_touched=False)
correct_output_b = np.array([[0.25, 0.25], [0.25, 0.25]])
assert np.array_equal(pct_cover_b, correct_output_b)
polygon_c = Polygon([[0.5, 0.5], [1.5, 0.5], [1.5, 1.5], [0.5, 1.5]])
shape_c = (2, 2)
affine_c = Affine(1, 0, 0,
0, -1, 2)
like_c = objectview({'shape': shape_c, 'affine': affine_c})
pct_cover_c = rasterize_pctcover_geom(polygon_c, like_c, scale=100, all_touched=False)
correct_output_c = np.array([[0.25, 0.25], [0.25, 0.25]])
assert np.array_equal(pct_cover_c, correct_output_c)