Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_compute_metadata_invalid_options(big_raster_file_nodata):
from terracotta.drivers.raster_base import RasterDriver
with pytest.raises(ValueError):
RasterDriver.compute_metadata(
str(big_raster_file_nodata), max_shape=(256, 256), use_chunks=True
)
with pytest.raises(ValueError):
RasterDriver.compute_metadata(str(big_raster_file_nodata), max_shape=(256, 256, 1))
def test_compute_metadata_invalid_raster(invalid_raster_file, use_chunks):
from terracotta.drivers.raster_base import RasterDriver
if use_chunks:
pytest.importorskip('crick')
with pytest.raises(ValueError):
RasterDriver.compute_metadata(str(invalid_raster_file), use_chunks=use_chunks)
def test_compute_metadata_invalid_options(big_raster_file):
from terracotta.drivers.raster_base import RasterDriver
with pytest.raises(ValueError):
RasterDriver.compute_metadata(str(big_raster_file), max_shape=(256, 256), use_chunks=True)
with pytest.raises(ValueError):
RasterDriver.compute_metadata(str(big_raster_file), max_shape=(256, 256, 1))
def test_compute_metadata_invalid_options(big_raster_file_nodata):
from terracotta.drivers.raster_base import RasterDriver
with pytest.raises(ValueError):
RasterDriver.compute_metadata(
str(big_raster_file_nodata), max_shape=(256, 256), use_chunks=True
)
with pytest.raises(ValueError):
RasterDriver.compute_metadata(str(big_raster_file_nodata), max_shape=(256, 256, 1))
def test_compute_metadata_unoptimized(unoptimized_raster_file):
from terracotta import exceptions
from terracotta.drivers.raster_base import RasterDriver
with rasterio.open(str(unoptimized_raster_file)) as src:
data = src.read(1)
valid_data = data[np.isfinite(data) & (data != src.nodata)]
dataset_shape = list(rasterio.features.dataset_features(
src, bidx=1, as_mask=True, geographic=True
))
convex_hull = MultiPolygon([shape(s['geometry']) for s in dataset_shape]).convex_hull
# compare
with pytest.warns(exceptions.PerformanceWarning):
mtd = RasterDriver.compute_metadata(str(unoptimized_raster_file), use_chunks=False)
np.testing.assert_allclose(mtd['valid_percentage'], 100 * valid_data.size / data.size)
np.testing.assert_allclose(mtd['range'], (valid_data.min(), valid_data.max()))
np.testing.assert_allclose(mtd['mean'], valid_data.mean())
np.testing.assert_allclose(mtd['stdev'], valid_data.std())
# allow some error margin since we only compute approximate quantiles
np.testing.assert_allclose(
mtd['percentiles'],
np.percentile(valid_data, np.arange(1, 100)),
rtol=2e-2
)
assert geometry_mismatch(shape(mtd['convex_hull']), convex_hull) < 1e-8
def test_compute_metadata_approximate(big_raster_file):
from terracotta.drivers.raster_base import RasterDriver
with rasterio.open(str(big_raster_file)) as src:
data = src.read(1)
valid_data = data[np.isfinite(data) & (data != src.nodata)]
dataset_shape = list(rasterio.features.dataset_features(
src, bidx=1, as_mask=True, geographic=True
))
convex_hull = MultiPolygon([shape(s['geometry']) for s in dataset_shape]).convex_hull
# compare
mtd = RasterDriver.compute_metadata(str(big_raster_file), max_shape=(512, 512))
np.testing.assert_allclose(mtd['valid_percentage'], 100 * valid_data.size / data.size, atol=1)
np.testing.assert_allclose(
mtd['range'], (valid_data.min(), valid_data.max()), atol=valid_data.max() / 100
)
np.testing.assert_allclose(mtd['mean'], valid_data.mean(), rtol=0.02)
np.testing.assert_allclose(mtd['stdev'], valid_data.std(), rtol=0.02)
np.testing.assert_allclose(
mtd['percentiles'],
np.percentile(valid_data, np.arange(1, 100)),
atol=valid_data.max() / 100, rtol=0.05
)
assert geometry_mismatch(shape(mtd['convex_hull']), convex_hull) < 0.1
elif nodata_type == 'masked':
raster_file = big_raster_file_mask
elif nodata_type == 'none':
raster_file = big_raster_file_nomask
elif nodata_type == 'nan':
raster_file = raster_file_float
with rasterio.open(str(raster_file)) as src:
data = src.read(1, masked=True)
valid_data = np.ma.masked_invalid(data).compressed()
convex_hull = convex_hull_exact(src)
# compare
if nodata_type == 'none':
with pytest.warns(UserWarning) as record:
mtd = RasterDriver.compute_metadata(str(raster_file), max_shape=(512, 512))
assert 'does not have a valid nodata value' in str(record[0].message)
else:
mtd = RasterDriver.compute_metadata(str(raster_file), max_shape=(512, 512))
np.testing.assert_allclose(mtd['valid_percentage'], 100 * valid_data.size / data.size, atol=1)
np.testing.assert_allclose(
mtd['range'], (valid_data.min(), valid_data.max()), atol=valid_data.max() / 100
)
np.testing.assert_allclose(mtd['mean'], valid_data.mean(), rtol=0.02)
np.testing.assert_allclose(mtd['stdev'], valid_data.std(), rtol=0.02)
np.testing.assert_allclose(
mtd['percentiles'],
np.percentile(valid_data, np.arange(1, 100)),
atol=valid_data.max() / 100, rtol=0.02
)
from terracotta.drivers.raster_base import RasterDriver
if use_chunks:
pytest.importorskip('crick')
with rasterio.open(str(big_raster_file)) as src:
data = src.read(1)
valid_data = data[np.isfinite(data) & (data != src.nodata)]
dataset_shape = list(rasterio.features.dataset_features(
src, bidx=1, as_mask=True, geographic=True
))
convex_hull = MultiPolygon([shape(s['geometry']) for s in dataset_shape]).convex_hull
# compare
mtd = RasterDriver.compute_metadata(str(big_raster_file), use_chunks=use_chunks)
np.testing.assert_allclose(mtd['valid_percentage'], 100 * valid_data.size / data.size)
np.testing.assert_allclose(mtd['range'], (valid_data.min(), valid_data.max()))
np.testing.assert_allclose(mtd['mean'], valid_data.mean())
np.testing.assert_allclose(mtd['stdev'], valid_data.std())
# allow some error margin since we only compute approximate quantiles
np.testing.assert_allclose(
mtd['percentiles'],
np.percentile(valid_data, np.arange(1, 100)),
rtol=2e-2
)
assert geometry_mismatch(shape(mtd['convex_hull']), convex_hull) < 1e-8