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_createdataset.py 4.3 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
  1. import gzip
  2. import io
  3. import shutil
  4. import tempfile
  5. from collections import Counter
  6. from pathlib import Path
  7. import pytest
  8. import numpy as np
  9. import rioxarray
  10. from PIL import Image
  11. from scripts.createdataset import _split_tile, Extractor, split_tiles
  12. Path.ls = lambda x: list(x.iterdir())
  13. @pytest.fixture
  14. def sample_image():
  15. test_image = Path("tests") / "testdata" / "ortho_ms_2019_EPSG3044_092_011.tif.gz"
  16. with tempfile.TemporaryDirectory() as tmpdir:
  17. with gzip.open(test_image, "rb") as f_in:
  18. with open(Path(tmpdir) / test_image.stem, "wb") as f_out:
  19. shutil.copyfileobj(f_in, f_out)
  20. with rioxarray.open_rasterio(
  21. Path(tmpdir) / test_image.stem, chunks={"band": 4, "x": 512, "y": 512}
  22. ) as t:
  23. return t.persist()
  24. @pytest.fixture
  25. def sample_mask():
  26. test_mask = (
  27. Path("tests") / "testdata" / "ortho_ms_2019_EPSG3044_092_011_mask.tif.gz"
  28. )
  29. with tempfile.TemporaryDirectory() as tmpdir:
  30. with gzip.open(test_mask, "rb") as f_in:
  31. with open(Path(tmpdir) / test_mask.stem, "wb") as f_out:
  32. shutil.copyfileobj(f_in, f_out)
  33. with rioxarray.open_rasterio(
  34. Path(tmpdir) / test_mask.stem, chunks={"band": 1, "x": 512, "y": 512}
  35. ) as t:
  36. return t.persist()
  37. @pytest.fixture
  38. def extractor():
  39. return Extractor(tile_size=256, source_dim=2048)
  40. def test_extractor_substile_shapes(extractor, sample_image, sample_mask):
  41. assert extractor(sample_image, n_bands=4).shape == (64, 4, 256, 256)
  42. assert extractor(sample_mask, n_bands=1).shape == (64, 1, 256, 256)
  43. def split_tile_subroutine():
  44. test_image = Path("tests") / "testdata" / "ortho_ms_2019_EPSG3044_092_011.tif.gz"
  45. test_mask = (
  46. Path("tests") / "testdata" / "ortho_ms_2019_EPSG3044_092_011_mask.tif.gz"
  47. )
  48. with tempfile.TemporaryDirectory() as tmpdir:
  49. with gzip.open(test_image, "rb") as f_in_i, gzip.open(
  50. test_mask, "rb"
  51. ) as f_in_m:
  52. with open(Path(tmpdir) / test_image.stem, "wb") as fout_image:
  53. shutil.copyfileobj(f_in_i, fout_image)
  54. with open(Path(tmpdir) / test_mask.stem, "wb") as fout_mask:
  55. shutil.copyfileobj(f_in_m, fout_mask)
  56. img_file = Path(tmpdir) / test_image.stem
  57. msk_file = Path(tmpdir) / test_mask.stem
  58. samples = _split_tile(
  59. img_file, msk_file, source_dim=2048, tile_size=256, format="TIFF"
  60. )
  61. return samples
  62. class TestSplitTile:
  63. samples = split_tile_subroutine()
  64. @property
  65. def sample_mask(self):
  66. raw_image = self.samples[0]["mask.tif"]
  67. return Image.open(io.BytesIO(raw_image))
  68. @property
  69. def sample_image(self):
  70. raw_image = self.samples[0]["rgbn.tif"]
  71. return Image.open(io.BytesIO(raw_image))
  72. def test_all_shard_keys_present(self):
  73. c = Counter()
  74. for s in self.samples:
  75. for k in list(set(s)):
  76. c[k] += 1
  77. assert c["__key__"] == c["rgbn.tif"] == c["mask.tif"] == c["txt"] == 64
  78. def test_shard_rgbn_shape(self):
  79. raw_image = self.samples[0]["rgbn.tif"]
  80. image = Image.open(io.BytesIO(raw_image))
  81. assert np.rollaxis(np.asarray(image), 2, 0).shape == (4, 256, 256)
  82. def test_shard_rgbn_filetype(self):
  83. raw_image = self.samples[0]["rgbn.tif"]
  84. image = Image.open(io.BytesIO(raw_image))
  85. assert image.format == "TIFF"
  86. def test_shard_mask_shape(self):
  87. assert np.asarray(self.sample_mask).shape == (256, 256)
  88. def test_shard_mask_filetype(self):
  89. assert self.sample_image.format == "TIFF"
  90. def test_shard_mask_valid_values(self):
  91. """Assert that mask values are in {0,1,2}"""
  92. raw_image = self.samples[0]["mask.tif"]
  93. image = Image.open(io.BytesIO(raw_image))
  94. values = set(np.asarray(image).ravel())
  95. assert values.issubset({0, 1, 2}) is True
  96. def test_shard_txt_matches_mask(self):
  97. """Assert that txt stats match actual mask values"""
  98. raw_image = self.samples[0]["mask.tif"]
  99. mask = np.asarray(Image.open(io.BytesIO(raw_image)))
  100. px_count = np.ones_like(mask)[mask > 0].sum()
  101. mask_frac = (px_count / mask.size) * 100
  102. frac = float(self.samples[0]["txt"])
  103. assert pytest.approx(frac, abs=1e-2) == mask_frac
Tip!

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

Comments

Loading...