Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_tiler.py 4.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. import tempfile
  2. from math import prod
  3. from pathlib import Path
  4. from typing import Tuple, Union
  5. import pytest
  6. from attr import dataclass
  7. import numpy as np
  8. import rioxarray
  9. from deadtrees.deployment.tiler import divisible_without_remainder, inspect_tile, Tiler
  10. from deadtrees.utils.data_handling import (
  11. make_blocks_vectorized,
  12. unmake_blocks_vectorized,
  13. )
  14. @pytest.fixture
  15. def tiler():
  16. return Tiler()
  17. @dataclass
  18. class TileData:
  19. filename: Union[str, Path]
  20. size: Tuple[int, int]
  21. subtiles: Tuple[int, int]
  22. example1 = TileData(
  23. Path("tests/testdata/tiles/ortho_2019_ESPG3044_49_11.tif"),
  24. (8192, 8192),
  25. (16, 16),
  26. )
  27. example2 = TileData(
  28. Path("tests/testdata/tiles/ortho_2019_ESPG3044_27_37.tif"),
  29. (8192, 7433),
  30. (16, 15),
  31. )
  32. example3 = TileData(
  33. Path("tests/testdata/tiles/ortho_2019_ESPG3044_52_26.tif"),
  34. (2649, 8192),
  35. (6, 16),
  36. )
  37. examples = [example1, example2, example3]
  38. @pytest.mark.parametrize("a,b,result", [(10, 2, True), (5, 4, False), (2, 0, False)])
  39. def test_divisible_without_remainder(a, b, result):
  40. assert divisible_without_remainder(a, b) == result
  41. class TestBlocksVectorized:
  42. source = np.array([np.arange(16).reshape(4, 4)] * 3)
  43. target = np.array(
  44. [
  45. [[[0, 1], [4, 5]], [[0, 1], [4, 5]], [[0, 1], [4, 5]]],
  46. [[[2, 3], [6, 7]], [[2, 3], [6, 7]], [[2, 3], [6, 7]]],
  47. [[[8, 9], [12, 13]], [[8, 9], [12, 13]], [[8, 9], [12, 13]]],
  48. [[[10, 11], [14, 15]], [[10, 11], [14, 15]], [[10, 11], [14, 15]]],
  49. ]
  50. )
  51. def test_make_blocks_vectorized(self):
  52. """break tile into batch of subtiles"""
  53. np.testing.assert_array_equal(
  54. make_blocks_vectorized(self.source, 2), self.target
  55. )
  56. def test_unmake_blocks_vectorized(self):
  57. """place batches back into tile (2d)"""
  58. np.testing.assert_array_equal(
  59. unmake_blocks_vectorized(self.target[:, 0, :, :], 2, 4, 4), self.source[0]
  60. )
  61. @pytest.mark.parametrize("tile", examples)
  62. def test_tiler_inspect_tile_size(tile):
  63. assert inspect_tile(tile.filename).size == tile.size
  64. @pytest.mark.parametrize("tile", examples)
  65. def test_tiler_inspect_tile_subtiles(tile):
  66. assert inspect_tile(tile.filename).subtiles == tile.subtiles
  67. @pytest.mark.parametrize("tile", examples[0:1])
  68. def test_tiler_inspect_tile_subtile_not_divisible(tile):
  69. with pytest.raises(ValueError):
  70. inspect_tile(tile.filename, subtile_shape=(512, 211))
  71. @pytest.mark.parametrize(
  72. "tile",
  73. [
  74. str(example1.filename),
  75. example1.filename,
  76. rioxarray.open_rasterio(example1.filename).sel(band=1, drop=True),
  77. ],
  78. )
  79. def test_tiler_infile_types(tile):
  80. assert inspect_tile(tile).size == example1.size
  81. def test_tiler_catch_bad_subtile_dims():
  82. with pytest.raises(ValueError):
  83. Tiler(example1.filename, tile_shape=(8192, 8192), subtile_shape=(256, 250))
  84. @pytest.mark.parametrize("tile", examples)
  85. def test_tiler_load_file_subtiles_to_use(tiler, tile):
  86. tiler.load_file(tile.filename)
  87. assert sum(tiler._subtiles_to_use) == prod(tile.subtiles)
  88. @pytest.mark.parametrize("tile", examples)
  89. def test_tiler_get_batches(tiler, tile):
  90. tiler.load_file(tile.filename)
  91. assert tiler.get_batches().shape == (prod(tile.subtiles), 3, 512, 512)
  92. @pytest.mark.parametrize("tile", examples)
  93. def test_tiler_put_batches(tiler, tile):
  94. tiler.load_file(tile.filename)
  95. batches = tiler.get_batches()
  96. pred_batches = np.random.choice(
  97. a=[1, 0], size=(len(batches), 512, 512), p=[0.1, 0.9]
  98. ) # single layer
  99. tiler.put_batches(pred_batches)
  100. assert tiler._outdata.shape == (8192, 8192)
  101. @pytest.mark.parametrize("tile", examples)
  102. def test_tiler_write_file(tiler, tile):
  103. tiler.load_file(tile.filename)
  104. batches = tiler.get_batches()
  105. pred_batches = np.random.choice(
  106. a=[1, 0], size=(len(batches), 512, 512), p=[0.1, 0.9]
  107. ) # single layer
  108. tiler.put_batches(pred_batches)
  109. with tempfile.NamedTemporaryFile(suffix=".tif") as tmp:
  110. tiler.write_file(tmp.name)
  111. assert rioxarray.open_rasterio(tmp.name).values.shape == (1, *tile.size)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...