Skip to content

Commit

Permalink
ENH: Adding ci.assert_raster_max_mismatch allowing a mismatch betwe…
Browse files Browse the repository at this point in the history
…en two rasters' pixels
  • Loading branch information
remi-braun committed Jul 29, 2021
1 parent 261b2e6 commit 6e73963
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.4.Z (2021-MM-DD)

## 1.4.8 (2021-07-29)
- ENH: Adding `ci.assert_raster_max_mismatch` allowing a mismatch between two rasters' pixels

## 1.4.7 (2021-07-28)
- FIX: Fixing the management of shapefiles on the cloud (caching the .shp and all other files)
- FIX: `ci.assert_geom_equal` manages correctly GeoSeries
Expand Down
5 changes: 5 additions & 0 deletions CI/SCRIPTS/test_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def test_assert_raster():
rasters_rio.write(arr, meta, raster_too_much_path)
ci.assert_raster_almost_equal(raster_float_path, raster_too_much_path)

# Max mismatch
ci.assert_raster_max_mismatch(raster_path, raster_path, max_mismatch_pct=0.001)
with pytest.raises(AssertionError):
ci.assert_raster_max_mismatch(raster_float_path, raster_almost_path)


@s3_env
def test_assert_xml():
Expand Down
2 changes: 1 addition & 1 deletion sertit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
.. include:: ../README.md
"""

__version__ = "1.4.7"
__version__ = "1.4.8"
44 changes: 44 additions & 0 deletions sertit/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,50 @@ def assert_raster_almost_equal(
np.testing.assert_almost_equal(dst_1.read(), dst_2.read(), decimal=decimal)


def assert_raster_max_mismatch(
path_1: Union[str, CloudPath, Path],
path_2: Union[str, CloudPath, Path],
max_mismatch_pct=0.5,
) -> None:
"""
Assert that two rasters are almost equal.
(everything is equal except the transform and the arrays that are almost equal)
Accepts an offset of `1E{decimal}` on the array and a precision of 10^-9 on the transform
-> Useful for pytests.
```python
>>> path = r"CI\DATA\rasters\raster.tif"
>>> path2 = r"CI\DATA\rasters\raster_almost.tif"
>>> assert_raster_equal(path, path2)
>>> # Raises AssertionError if sth goes wrong
```
Args:
path_1 (Union[str, CloudPath, Path]): Raster 1
path_2 (Union[str, CloudPath, Path]): Raster 2
max_mismatch_pct (float): Maximum of element mismatch in %
"""

with rasterio.open(str(path_1)) as dst_1:
with rasterio.open(str(path_2)) as dst_2:
assert dst_1.meta["driver"] == dst_2.meta["driver"]
assert dst_1.meta["dtype"] == dst_2.meta["dtype"]
assert dst_1.meta["nodata"] == dst_2.meta["nodata"]
assert dst_1.meta["width"] == dst_2.meta["width"]
assert dst_1.meta["height"] == dst_2.meta["height"]
assert dst_1.meta["count"] == dst_2.meta["count"]
assert dst_1.meta["crs"] == dst_2.meta["crs"]
assert dst_1.meta["transform"].almost_equals(
dst_2.meta["transform"], precision=1e-9
)
nof_mismatch = np.count_nonzero(dst_1.read() != dst_2.read())
assert (
nof_mismatch / (dst_1.count * dst_1.width * dst_1.height)
) * 100.0 < max_mismatch_pct


def assert_dir_equal(
path_1: Union[str, CloudPath, Path], path_2: Union[str, CloudPath, Path]
) -> None:
Expand Down

0 comments on commit 6e73963

Please sign in to comment.