From 2d6db04c4e3c8391729c052ef299dd6babedff0c Mon Sep 17 00:00:00 2001 From: Miikka Kallio Date: Mon, 3 Jul 2023 11:06:33 +0300 Subject: [PATCH] Fix pre-commit errors --- .../vector_processing/idw_interpolation.py | 21 ++--- .../test_interpolate_vector.py | 87 +++++++++---------- 2 files changed, 47 insertions(+), 61 deletions(-) diff --git a/eis_toolkit/vector_processing/idw_interpolation.py b/eis_toolkit/vector_processing/idw_interpolation.py index 8f2038f9..b8e0ac64 100644 --- a/eis_toolkit/vector_processing/idw_interpolation.py +++ b/eis_toolkit/vector_processing/idw_interpolation.py @@ -1,9 +1,9 @@ from numbers import Number + +import geopandas as gpd +import numpy as np from beartype import beartype from beartype.typing import Optional, Tuple -import numpy as np -from shapely.geometry import Point -import geopandas as gpd from eis_toolkit import exceptions @@ -14,7 +14,7 @@ def _idw_interpolation( target_column: str, resolution: Tuple[Number, Number], extent: Optional[Tuple[float, float, float, float]] = None, - power: Optional[Number] = 2 + power: Optional[Number] = 2, ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: if geodataframe.shape[0] == 0: @@ -87,10 +87,9 @@ def idw_interpolation( target_column: str, resolution: Tuple[Number, Number], extent: Optional[Tuple[float, float, float, float]] = None, - power: Optional[int] = 2 + power: Optional[int] = 2, ) -> Tuple[float, float, dict]: - - """Simple inverse distance weighted (IDW) interpolation. + """Calculate simple inverse distance weighted (IDW) interpolation. Args: geodataframe: The vector dataframe to be interpolated. @@ -105,11 +104,5 @@ def idw_interpolation( Returns: Rasterized vector data and metadata. """ - x, y, interpolated_values = _idw_interpolation( - geodataframe, - target_column, - resolution, - extent, - power - ) + x, y, interpolated_values = _idw_interpolation(geodataframe, target_column, resolution, extent, power) return x, y, interpolated_values diff --git a/tests/vector_processing/test_interpolate_vector.py b/tests/vector_processing/test_interpolate_vector.py index 18f47fdf..09c42fff 100644 --- a/tests/vector_processing/test_interpolate_vector.py +++ b/tests/vector_processing/test_interpolate_vector.py @@ -1,13 +1,12 @@ -import sys from pathlib import Path -import pytest -import numpy as np -from shapely.geometry import Point import geopandas as gpd +import numpy as np +import pytest import rasterio -from eis_toolkit import exceptions +from shapely.geometry import Point +from eis_toolkit import exceptions from eis_toolkit.vector_processing.idw_interpolation import idw_interpolation test_dir = Path(__file__).parent.parent @@ -17,26 +16,33 @@ @pytest.fixture def test_points(): + """Simple test data.""" data = { - 'value1': [1, 2, 3, 4, 5], - 'value2': [5, 4, 3, 2, 1], - 'geometry': [Point(0, 0), Point(1, 1), Point(2, 2), Point(3, 3), Point(4, 4)] + "value1": [1, 2, 3, 4, 5], + "value2": [5, 4, 3, 2, 1], + "geometry": [Point(0, 0), Point(1, 1), Point(2, 2), Point(3, 3), Point(4, 4)], } return gpd.GeoDataFrame(data) @pytest.fixture def validated_points(): + """Test data.""" data = { - 'random_number': [124, 248, 496, 992], - 'geometry': [Point(24.945831, 60.192059), Point(24.6559, 60.2055), - Point(25.0378, 60.2934), Point(24.7284, 60.2124)] + "random_number": [124, 248, 496, 992], + "geometry": [ + Point(24.945831, 60.192059), + Point(24.6559, 60.2055), + Point(25.0378, 60.2934), + Point(24.7284, 60.2124), + ], } return gpd.GeoDataFrame(data) @pytest.fixture def test_empty_gdf(): + """Test empty GeoDataFrame.""" data = { "geometry": [], "values": [], @@ -45,17 +51,14 @@ def test_empty_gdf(): def test_validated_points(validated_points): - target_column = 'random_number' + """Test IDW without extent set.""" + target_column = "random_number" resolution = (0.005, 0.005) extent = None power = 2 interpolated_values = idw_interpolation( - geodataframe=validated_points, - target_column=target_column, - resolution=resolution, - extent=extent, - power=power + geodataframe=validated_points, target_column=target_column, resolution=resolution, extent=extent, power=power ) assert target_column in validated_points.columns @@ -69,17 +72,14 @@ def test_validated_points(validated_points): def test_validated_points_with_extent(validated_points): - target_column = 'random_number' + """Test IDW with extent set.""" + target_column = "random_number" resolution = (0.005, 0.005) extent = (24.655899, 60.192059, 25.037803604, 60.293407876) power = 2 interpolated_values = idw_interpolation( - geodataframe=validated_points, - target_column=target_column, - resolution=resolution, - extent=extent, - power=power + geodataframe=validated_points, target_column=target_column, resolution=resolution, extent=extent, power=power ) assert target_column in validated_points.columns @@ -93,58 +93,51 @@ def test_validated_points_with_extent(validated_points): def test_invalid_column(test_points): - target_column = 'not-in-data-column' + """Test invalid column GeoDataFrame.""" + target_column = "not-in-data-column" resolution = (1, 1) extent = None power = 2 with pytest.raises(exceptions.InvalidParameterValueException): idw_interpolation( - geodataframe=test_points, - target_column=target_column, - resolution=resolution, - extent=extent, - power=power + geodataframe=test_points, target_column=target_column, resolution=resolution, extent=extent, power=power ) def test_empty_geodataframe(test_empty_gdf): - target_column = 'values' + """Test empty GeoDataFrame.""" + target_column = "values" resolution = (5, 5) extent = None power = 5 with pytest.raises(exceptions.EmptyDataFrameException): idw_interpolation( - geodataframe=test_empty_gdf, - target_column=target_column, - resolution=resolution, - extent=extent, - power=power + geodataframe=test_empty_gdf, target_column=target_column, resolution=resolution, extent=extent, power=power ) def test_interpolate_vector(test_points): - target_column = 'value1' + """Test IDW with simple data.""" + target_column = "value1" resolution = (1, 1) extent = None power = 2 interpolated_values = idw_interpolation( - geodataframe=test_points, - target_column=target_column, - resolution=resolution, - extent=extent, - power=power + geodataframe=test_points, target_column=target_column, resolution=resolution, extent=extent, power=power ) assert target_column in test_points.columns interpolated_value = interpolated_values[2] - expected_values = np.array([ - [3, 3.40648594, 4.02086331, 5], - [2.59351406, 3, 3.77021471, 4.02086331], - [1.97913669, 2.22978529, 3, 3.40648594], - [1, 1.97913669, 2.59351406, 3] - ]) + expected_values = np.array( + [ + [3, 3.40648594, 4.02086331, 5], + [2.59351406, 3, 3.77021471, 4.02086331], + [1.97913669, 2.22978529, 3, 3.40648594], + [1, 1.97913669, 2.59351406, 3], + ] + ) np.testing.assert_allclose(interpolated_value, expected_values, rtol=1e-5, atol=1e-5)