From 294bd1de029799ea6549745c9d60f5d3f65bfa57 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Thu, 6 Feb 2020 14:15:33 -0700 Subject: [PATCH 01/23] fixed issue with asserting valid data, and fixed geodataframes being invalid inputs. --- matplotcheck/vector.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index acc44459..cbf66e8d 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -80,7 +80,7 @@ def assert_no_legend_overlap(self, m="Legends overlap eachother"): def _convert_length(self, arr, n): """ helper function for 'get_points_by_attributes' and 'get_lines_by_attributes' - takes an array of either legnth 1 or n. + takes an array of either legnth 1 or n. If array is length 1: array of array's only element repeating n times is returned If array is length n: original array is returned Else: function raises value error @@ -106,7 +106,7 @@ def _convert_length(self, arr, n): ) def get_points_by_attributes(self): - """Returns a sorted list of lists where each list contains tuples of xycoords for points of + """Returns a sorted list of lists where each list contains tuples of xycoords for points of the same attributes: color, marker, and markersize Returns @@ -287,11 +287,11 @@ def _convert_linestyle(self, ls): return (ls[0], onoffseq) def get_lines(self): - """Returns a dataframe with all lines on ax + """Returns a dataframe with all lines on ax Returns ------- - output: DataFrame with column 'lines'. Each row represents one line segment. + output: DataFrame with column 'lines'. Each row represents one line segment. Its value in 'lines' is a list of tuples representing the line segement. """ lines = [ @@ -473,14 +473,16 @@ def assert_polygons( ): """Asserts the polygon data in Axes ax is equal to polygons_expected to decimal place dec with error message m If polygons_expected is am empty list or None, assertion is passed - + Parameters ---------- polygons_expected: list of polygons expected to be founds on Axes ax dec: int stating the desired decimal precision. If None, polygons must be exact m: string error message if assertion is not met """ - if polygons_expected: + if len(polygons_expected) != 0 : + if type(polygons_expected) == gpd.geodataframe.GeoDataFrame: + polygons_expected = self._convert_multipolygons(polygons_expected['geometry']) polygons = self.get_polygons() if dec: assert len(polygons_expected) == len(polygons), m From 94d167668a6ec1090356e4da60f334d7ed100f69 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Thu, 6 Feb 2020 14:25:40 -0700 Subject: [PATCH 02/23] Used better check for type of input. --- matplotcheck/vector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index cbf66e8d..fc5e081e 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -481,7 +481,7 @@ def assert_polygons( m: string error message if assertion is not met """ if len(polygons_expected) != 0 : - if type(polygons_expected) == gpd.geodataframe.GeoDataFrame: + if isinstance(polygons_expected, gpd.geodataframe.GeoDataFrame): polygons_expected = self._convert_multipolygons(polygons_expected['geometry']) polygons = self.get_polygons() if dec: From 73d91b858e9ed4059b7f2224e3d1ef32657199ab Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Thu, 6 Feb 2020 17:06:30 -0700 Subject: [PATCH 03/23] black changes --- matplotcheck/vector.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index fc5e081e..e1841af5 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -480,9 +480,11 @@ def assert_polygons( dec: int stating the desired decimal precision. If None, polygons must be exact m: string error message if assertion is not met """ - if len(polygons_expected) != 0 : + if len(polygons_expected) != 0: if isinstance(polygons_expected, gpd.geodataframe.GeoDataFrame): - polygons_expected = self._convert_multipolygons(polygons_expected['geometry']) + polygons_expected = self._convert_multipolygons( + polygons_expected["geometry"] + ) polygons = self.get_polygons() if dec: assert len(polygons_expected) == len(polygons), m From 7311468cd3df5df24fd6212cf38c44db5c6add73 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 11:27:49 -0700 Subject: [PATCH 04/23] Added new tests for the assert_polygon function in the vector tester module --- matplotcheck/tests/test_vector.py | 100 ++++++++++++++++++++++++++++++ matplotcheck/vector.py | 20 +++++- 2 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 matplotcheck/tests/test_vector.py diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py new file mode 100644 index 00000000..1a9b682c --- /dev/null +++ b/matplotcheck/tests/test_vector.py @@ -0,0 +1,100 @@ +"""Tests for the vector module""" +import pytest +from shapely.geometry import Polygon, LineString +import matplotlib.pyplot as plt +import geopandas as gpd +from matplotcheck.vector import VectorTester + +@pytest.fixture +def basic_polygon(): + """ + A square polygon spanning (2, 2) to (4.25, 4.25) in x and y directions + Borrowed from rasterio/tests/conftest.py + Returns + ------- + dict: GeoJSON-style geometry object. + Coordinates are in grid coordinates (Affine.identity()). + """ + return Polygon([(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]) + +@pytest.fixture +def basic_polygon_gdf(basic_polygon): + """ + A GeoDataFrame containing the basic polygon geometry + Returns + ------- + GeoDataFrame containing the basic_polygon polygon + """ + gdf = gpd.GeoDataFrame( + geometry=[basic_polygon], crs={"init": "epsg:4326"} + ) + return gdf + +@pytest.fixture +def poly_geo_plot(basic_polygon_gdf): + """Create a polygon testing object""" + fig, ax = plt.subplots() + + basic_polygon_gdf.plot(ax=ax) + ax.set_title("My Plot Title", fontsize=30) + ax.set_xlabel("x label") + ax.set_ylabel("y label") + + axis = plt.gca() + + return VectorTester(axis) + +""" Assert Polygon Tests""" + +def test_list_of_polygons_check(poly_geo_plot, basic_polygon): + """Check that the polygon assert works with a list of polygons""" + x, y = basic_polygon.exterior.coords.xy + poly_list = [list(zip(x,y))] + poly_geo_plot.assert_polygons(poly_list) + plt.close() + +def test_polygon_geodataframe_check(poly_geo_plot, basic_polygon_gdf): + """Check that the polygon assert works with a polygon geodataframe""" + poly_geo_plot.assert_polygons(basic_polygon_gdf) + plt.close() + +def test_empty_list_polygon_check(poly_geo_plot): + """Check that the polygon assert fails an empty list""" + with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): + poly_geo_plot.assert_polygons([]) + plt.close() + +def test_empty_list_entry_polygon_check(poly_geo_plot): + """Check that the polygon assert fails a list with an empty entry""" + with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): + poly_geo_plot.assert_polygons([[]]) + plt.close() + +def test_empty_gdf_polygon_check(poly_geo_plot): + """Check that the polygon assert fails an empty GeoDataFrame""" + with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): + poly_geo_plot.assert_polygons(gpd.GeoDataFrame([])) + plt.close() + +def test_polygon_dec_check(poly_geo_plot, basic_polygon): + """Check that the polygon assert works with a polygon geodataframe""" + x, y = basic_polygon.exterior.coords.xy + poly_list = [[(x[0]+.1, x[1]) for x in list(zip(x,y))]] + poly_geo_plot.assert_polygons(poly_list, dec=1) + plt.close() + +def test_polygon_dec_check_fail(poly_geo_plot, basic_polygon): + """Check that the polygon assert works with a polygon geodataframe""" + with pytest.raises(AssertionError, match="Incorrect Polygon"): + x, y = basic_polygon.exterior.coords.xy + poly_list = [(x[0]+.5, x[1]) for x in list(zip(x,y))] + poly_geo_plot.assert_polygons(poly_list, dec=1) + plt.close() + +def test_polygon_custom_fail_message(poly_geo_plot, basic_polygon): + """Check that the polygon assert works with a polygon geodataframe""" + with pytest.raises(AssertionError, match="Test Message"): + x, y = basic_polygon.exterior.coords.xy + poly_list = [(x[0]+.5, x[1]) for x in list(zip(x,y))] + poly_geo_plot.assert_polygons(poly_list, m="Test Message") + plt.close() diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index e1841af5..cfb82b07 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -476,11 +476,21 @@ def assert_polygons( Parameters ---------- - polygons_expected: list of polygons expected to be founds on Axes ax - dec: int stating the desired decimal precision. If None, polygons must be exact - m: string error message if assertion is not met + polygons_expected: List or GeoDataFrame + List of polygons expected to be founds on Axes ax or a GeoDataFrame + containing the expected polygons. + dec: int (Optional) + Int stating the desired decimal precision. If None, polygons must + be exact. + m: string (default = "Incorrect Polygon Data") + String error message if assertion is not met. """ if len(polygons_expected) != 0: + if isinstance(polygons_expected, list): + if len(polygons_expected[0]) == 0: + raise ValueError( + "Empty list or GeoDataFrame passed into assert_polygons." + ) if isinstance(polygons_expected, gpd.geodataframe.GeoDataFrame): polygons_expected = self._convert_multipolygons( polygons_expected["geometry"] @@ -498,3 +508,7 @@ def assert_polygons( ) else: np.testing.assert_equal(polygons, sorted(polygons_expected), m) + else: + raise ValueError( + "Empty list or GeoDataFrame passed into assert_polygons." + ) From b83ce1553c872a683f45dc9e258ca752bc8832c8 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 11:38:08 -0700 Subject: [PATCH 05/23] Updated docstrings to be more accurate to tests --- matplotcheck/tests/test_vector.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 1a9b682c..5f732ad0 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -8,8 +8,8 @@ @pytest.fixture def basic_polygon(): """ - A square polygon spanning (2, 2) to (4.25, 4.25) in x and y directions - Borrowed from rasterio/tests/conftest.py + A square polygon spanning (2, 2) to (4.25, 4.25) in x and y directions. + Borrowed from rasterio/tests/conftest.py. Returns ------- dict: GeoJSON-style geometry object. @@ -20,10 +20,10 @@ def basic_polygon(): @pytest.fixture def basic_polygon_gdf(basic_polygon): """ - A GeoDataFrame containing the basic polygon geometry + A GeoDataFrame containing the basic polygon geometry. Returns ------- - GeoDataFrame containing the basic_polygon polygon + GeoDataFrame containing the basic_polygon polygon. """ gdf = gpd.GeoDataFrame( geometry=[basic_polygon], crs={"init": "epsg:4326"} @@ -32,7 +32,7 @@ def basic_polygon_gdf(basic_polygon): @pytest.fixture def poly_geo_plot(basic_polygon_gdf): - """Create a polygon testing object""" + """Create a polygon vector tester object.""" fig, ax = plt.subplots() basic_polygon_gdf.plot(ax=ax) @@ -47,7 +47,7 @@ def poly_geo_plot(basic_polygon_gdf): """ Assert Polygon Tests""" def test_list_of_polygons_check(poly_geo_plot, basic_polygon): - """Check that the polygon assert works with a list of polygons""" + """Check that the polygon assert works with a list of polygons.""" x, y = basic_polygon.exterior.coords.xy poly_list = [list(zip(x,y))] poly_geo_plot.assert_polygons(poly_list) @@ -59,32 +59,37 @@ def test_polygon_geodataframe_check(poly_geo_plot, basic_polygon_gdf): plt.close() def test_empty_list_polygon_check(poly_geo_plot): - """Check that the polygon assert fails an empty list""" + """Check that the polygon assert fails an empty list.""" with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): poly_geo_plot.assert_polygons([]) plt.close() def test_empty_list_entry_polygon_check(poly_geo_plot): - """Check that the polygon assert fails a list with an empty entry""" + """Check that the polygon assert fails a list with an empty entry.""" with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): poly_geo_plot.assert_polygons([[]]) plt.close() def test_empty_gdf_polygon_check(poly_geo_plot): - """Check that the polygon assert fails an empty GeoDataFrame""" + """Check that the polygon assert fails an empty GeoDataFrame.""" with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): poly_geo_plot.assert_polygons(gpd.GeoDataFrame([])) plt.close() def test_polygon_dec_check(poly_geo_plot, basic_polygon): - """Check that the polygon assert works with a polygon geodataframe""" - x, y = basic_polygon.exterior.coords.xy + """ + Check that the polygon assert passes when the polygon is off by less than + the maximum decimal precision. + """ x, y = basic_polygon.exterior.coords.xy poly_list = [[(x[0]+.1, x[1]) for x in list(zip(x,y))]] poly_geo_plot.assert_polygons(poly_list, dec=1) plt.close() def test_polygon_dec_check_fail(poly_geo_plot, basic_polygon): - """Check that the polygon assert works with a polygon geodataframe""" + """ + Check that the polygon assert fails when the polygon is off by more than + the maximum decimal precision. + """ with pytest.raises(AssertionError, match="Incorrect Polygon"): x, y = basic_polygon.exterior.coords.xy poly_list = [(x[0]+.5, x[1]) for x in list(zip(x,y))] @@ -92,7 +97,7 @@ def test_polygon_dec_check_fail(poly_geo_plot, basic_polygon): plt.close() def test_polygon_custom_fail_message(poly_geo_plot, basic_polygon): - """Check that the polygon assert works with a polygon geodataframe""" + """Check that the corrct error message is raised when polygons fail""" with pytest.raises(AssertionError, match="Test Message"): x, y = basic_polygon.exterior.coords.xy poly_list = [(x[0]+.5, x[1]) for x in list(zip(x,y))] From cc6f9bcdb029c4eb3d18cb8dc5261224ebcb4341 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 11:40:06 -0700 Subject: [PATCH 06/23] Added changes to Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 046dccfa..a3b0c83f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] * Created a vignette covering the testing of histograms (@ryla5068, #149) +* Allowed `assert_polygons()` to accept GeoDataFrames and added tests (@nkorinek, #188) ## [0.1.1] * Added test for bin heights of histograms (@ryla5068, #124) From 35299fe3f88c0bc292f2570fac141a0eb09a28f5 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 12:15:32 -0700 Subject: [PATCH 07/23] Fixed a typo that was causing failure --- matplotcheck/tests/test_vector.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 5f732ad0..ba4da22f 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -80,7 +80,8 @@ def test_polygon_dec_check(poly_geo_plot, basic_polygon): """ Check that the polygon assert passes when the polygon is off by less than the maximum decimal precision. - """ x, y = basic_polygon.exterior.coords.xy + """ + x, y = basic_polygon.exterior.coords.xy poly_list = [[(x[0]+.1, x[1]) for x in list(zip(x,y))]] poly_geo_plot.assert_polygons(poly_list, dec=1) plt.close() From 5056d56bea29346fec7d300601b34bb4745abd0d Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 12:23:23 -0700 Subject: [PATCH 08/23] Fixed typo in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3b0c83f..415c27fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] * Created a vignette covering the testing of histograms (@ryla5068, #149) -* Allowed `assert_polygons()` to accept GeoDataFrames and added tests (@nkorinek, #188) +* Allowed `assert_polygons()` to accept GeoDataFrames and added tests (@nkorinek, #175) ## [0.1.1] * Added test for bin heights of histograms (@ryla5068, #124) From cf37a01cd2ef71a9bc8e5daedc0bc1147b2ab8ec Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 12:36:40 -0700 Subject: [PATCH 09/23] Fixed issues found by codacy --- matplotcheck/tests/test_vector.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index ba4da22f..149d0a25 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -1,6 +1,6 @@ """Tests for the vector module""" import pytest -from shapely.geometry import Polygon, LineString +from shapely.geometry import Polygon import matplotlib.pyplot as plt import geopandas as gpd from matplotcheck.vector import VectorTester @@ -33,7 +33,7 @@ def basic_polygon_gdf(basic_polygon): @pytest.fixture def poly_geo_plot(basic_polygon_gdf): """Create a polygon vector tester object.""" - fig, ax = plt.subplots() + _, ax = plt.subplots() basic_polygon_gdf.plot(ax=ax) ax.set_title("My Plot Title", fontsize=30) @@ -45,7 +45,6 @@ def poly_geo_plot(basic_polygon_gdf): return VectorTester(axis) """ Assert Polygon Tests""" - def test_list_of_polygons_check(poly_geo_plot, basic_polygon): """Check that the polygon assert works with a list of polygons.""" x, y = basic_polygon.exterior.coords.xy From 6dfc679d3104d5b7998b3c0642b447fbd2b5c328 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 12:43:08 -0700 Subject: [PATCH 10/23] Fixed issues with string statements --- matplotcheck/tests/test_vector.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 149d0a25..5e178428 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -5,6 +5,8 @@ import geopandas as gpd from matplotcheck.vector import VectorTester +"""Fixtures""" + @pytest.fixture def basic_polygon(): """ From ef6abf9b74cfca851a0f941570f4287c21666cf0 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 12:49:24 -0700 Subject: [PATCH 11/23] Fixed issues with string statements --- matplotcheck/tests/test_vector.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 5e178428..6dbd9de7 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -5,8 +5,6 @@ import geopandas as gpd from matplotcheck.vector import VectorTester -"""Fixtures""" - @pytest.fixture def basic_polygon(): """ @@ -46,7 +44,6 @@ def poly_geo_plot(basic_polygon_gdf): return VectorTester(axis) -""" Assert Polygon Tests""" def test_list_of_polygons_check(poly_geo_plot, basic_polygon): """Check that the polygon assert works with a list of polygons.""" x, y = basic_polygon.exterior.coords.xy From d2591678dc972842f688b8d3c64c206fd875d5a0 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 13:00:14 -0700 Subject: [PATCH 12/23] Added import to fix CI error --- matplotcheck/tests/test_vector.py | 1 + 1 file changed, 1 insertion(+) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 6dbd9de7..43b945fa 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -1,6 +1,7 @@ """Tests for the vector module""" import pytest from shapely.geometry import Polygon +from descartes import PolygonPatch import matplotlib.pyplot as plt import geopandas as gpd from matplotcheck.vector import VectorTester From 851d090836bf9f6c61805b777ad850ecbcfd63d0 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Mon, 10 Feb 2020 13:09:59 -0700 Subject: [PATCH 13/23] Fix didn't work --- matplotcheck/tests/test_vector.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 43b945fa..6dbd9de7 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -1,7 +1,6 @@ """Tests for the vector module""" import pytest from shapely.geometry import Polygon -from descartes import PolygonPatch import matplotlib.pyplot as plt import geopandas as gpd from matplotcheck.vector import VectorTester From d55a425cb69e1794348f239aac56f580115db5b7 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 12:45:48 -0700 Subject: [PATCH 14/23] Added descartes to the dev-requirements --- dev-requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/dev-requirements.txt b/dev-requirements.txt index a4664dc8..3f9dcd5f 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -10,3 +10,4 @@ codecov==2.0.15 setuptools==42.0.2 pre-commit==1.20.0 pip==19.0.3 +descartes==1.1.0 From 3206f1620cb2ef18b5822db536eeeb494595b20c Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 13:13:27 -0700 Subject: [PATCH 15/23] black changes --- matplotcheck/tests/test_vector.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 6dbd9de7..81b08677 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -5,6 +5,7 @@ import geopandas as gpd from matplotcheck.vector import VectorTester + @pytest.fixture def basic_polygon(): """ @@ -17,6 +18,7 @@ def basic_polygon(): """ return Polygon([(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]) + @pytest.fixture def basic_polygon_gdf(basic_polygon): """ @@ -25,11 +27,10 @@ def basic_polygon_gdf(basic_polygon): ------- GeoDataFrame containing the basic_polygon polygon. """ - gdf = gpd.GeoDataFrame( - geometry=[basic_polygon], crs={"init": "epsg:4326"} - ) + gdf = gpd.GeoDataFrame(geometry=[basic_polygon], crs={"init": "epsg:4326"}) return gdf + @pytest.fixture def poly_geo_plot(basic_polygon_gdf): """Create a polygon vector tester object.""" @@ -44,46 +45,53 @@ def poly_geo_plot(basic_polygon_gdf): return VectorTester(axis) + def test_list_of_polygons_check(poly_geo_plot, basic_polygon): """Check that the polygon assert works with a list of polygons.""" x, y = basic_polygon.exterior.coords.xy - poly_list = [list(zip(x,y))] + poly_list = [list(zip(x, y))] poly_geo_plot.assert_polygons(poly_list) plt.close() + def test_polygon_geodataframe_check(poly_geo_plot, basic_polygon_gdf): """Check that the polygon assert works with a polygon geodataframe""" poly_geo_plot.assert_polygons(basic_polygon_gdf) plt.close() + def test_empty_list_polygon_check(poly_geo_plot): """Check that the polygon assert fails an empty list.""" - with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): + with pytest.raises(ValueError, match="Empty list or GeoDataFrame "): poly_geo_plot.assert_polygons([]) plt.close() + def test_empty_list_entry_polygon_check(poly_geo_plot): """Check that the polygon assert fails a list with an empty entry.""" - with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): + with pytest.raises(ValueError, match="Empty list or GeoDataFrame "): poly_geo_plot.assert_polygons([[]]) plt.close() + def test_empty_gdf_polygon_check(poly_geo_plot): """Check that the polygon assert fails an empty GeoDataFrame.""" - with pytest.raises(ValueError, match = "Empty list or GeoDataFrame "): + with pytest.raises(ValueError, match="Empty list or GeoDataFrame "): poly_geo_plot.assert_polygons(gpd.GeoDataFrame([])) plt.close() + def test_polygon_dec_check(poly_geo_plot, basic_polygon): """ Check that the polygon assert passes when the polygon is off by less than the maximum decimal precision. """ x, y = basic_polygon.exterior.coords.xy - poly_list = [[(x[0]+.1, x[1]) for x in list(zip(x,y))]] + poly_list = [[(x[0] + 0.1, x[1]) for x in list(zip(x, y))]] poly_geo_plot.assert_polygons(poly_list, dec=1) plt.close() + def test_polygon_dec_check_fail(poly_geo_plot, basic_polygon): """ Check that the polygon assert fails when the polygon is off by more than @@ -91,14 +99,15 @@ def test_polygon_dec_check_fail(poly_geo_plot, basic_polygon): """ with pytest.raises(AssertionError, match="Incorrect Polygon"): x, y = basic_polygon.exterior.coords.xy - poly_list = [(x[0]+.5, x[1]) for x in list(zip(x,y))] + poly_list = [(x[0] + 0.5, x[1]) for x in list(zip(x, y))] poly_geo_plot.assert_polygons(poly_list, dec=1) plt.close() + def test_polygon_custom_fail_message(poly_geo_plot, basic_polygon): """Check that the corrct error message is raised when polygons fail""" with pytest.raises(AssertionError, match="Test Message"): x, y = basic_polygon.exterior.coords.xy - poly_list = [(x[0]+.5, x[1]) for x in list(zip(x,y))] + poly_list = [(x[0] + 0.5, x[1]) for x in list(zip(x, y))] poly_geo_plot.assert_polygons(poly_list, m="Test Message") plt.close() From a3aea2f95d894ffb0bf47ef3e9f80737ea1dfb51 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 13:27:41 -0700 Subject: [PATCH 16/23] Taking out title for doc build --- matplotcheck/tests/test_vector.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 81b08677..04511d22 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -1,4 +1,3 @@ -"""Tests for the vector module""" import pytest from shapely.geometry import Polygon import matplotlib.pyplot as plt From 575ed2c597f7a9605172e20a7d9eedf6ae568a5c Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 13:45:41 -0700 Subject: [PATCH 17/23] minor formatting updates --- matplotcheck/tests/test_vector.py | 1 + matplotcheck/vector.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 04511d22..81b08677 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -1,3 +1,4 @@ +"""Tests for the vector module""" import pytest from shapely.geometry import Polygon import matplotlib.pyplot as plt diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index cfb82b07..3ea5e417 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -471,7 +471,8 @@ def _convert_multipolygons(self, series): def assert_polygons( self, polygons_expected, dec=None, m="Incorrect Polygon Data" ): - """Asserts the polygon data in Axes ax is equal to polygons_expected to decimal place dec with error message m + """ + Asserts the polygon data in Axes ax is equal to polygons_expected to decimal place dec with error message m If polygons_expected is am empty list or None, assertion is passed Parameters From 092a1c5736e15948935389f9e6009c5ad931e6f3 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 14:21:53 -0700 Subject: [PATCH 18/23] Trying to take care of title issue --- matplotcheck/vector.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index 3ea5e417..42d81cc3 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -471,10 +471,8 @@ def _convert_multipolygons(self, series): def assert_polygons( self, polygons_expected, dec=None, m="Incorrect Polygon Data" ): - """ - Asserts the polygon data in Axes ax is equal to polygons_expected to decimal place dec with error message m + """Asserts the polygon data in Axes ax is equal to polygons_expected to decimal place dec with error message m If polygons_expected is am empty list or None, assertion is passed - Parameters ---------- polygons_expected: List or GeoDataFrame From 75483d282733e053d3cbcf214adcedd38ce59fae Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 14:41:14 -0700 Subject: [PATCH 19/23] Trying to take care of title issue --- matplotcheck/vector.py | 1 + 1 file changed, 1 insertion(+) diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index 42d81cc3..4a6fe053 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -473,6 +473,7 @@ def assert_polygons( ): """Asserts the polygon data in Axes ax is equal to polygons_expected to decimal place dec with error message m If polygons_expected is am empty list or None, assertion is passed + Parameters ---------- polygons_expected: List or GeoDataFrame From bb8c73ee4707c016dc3a7ca79bf33f5708cb7723 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 17:14:30 -0700 Subject: [PATCH 20/23] Actually fixing the docstring issue this time --- matplotcheck/vector.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/matplotcheck/vector.py b/matplotcheck/vector.py index 4a6fe053..1996b4bc 100644 --- a/matplotcheck/vector.py +++ b/matplotcheck/vector.py @@ -472,18 +472,18 @@ def assert_polygons( self, polygons_expected, dec=None, m="Incorrect Polygon Data" ): """Asserts the polygon data in Axes ax is equal to polygons_expected to decimal place dec with error message m - If polygons_expected is am empty list or None, assertion is passed - - Parameters - ---------- - polygons_expected: List or GeoDataFrame - List of polygons expected to be founds on Axes ax or a GeoDataFrame - containing the expected polygons. - dec: int (Optional) - Int stating the desired decimal precision. If None, polygons must - be exact. - m: string (default = "Incorrect Polygon Data") - String error message if assertion is not met. + If polygons_expected is am empty list or None, assertion is passed. + + Parameters + ---------- + polygons_expected : List or GeoDataFrame + List of polygons expected to be founds on Axes ax or a GeoDataFrame + containing the expected polygons. + dec : int (Optional) + Int stating the desired decimal precision. If None, polygons must + be exact. + m : string (default = "Incorrect Polygon Data") + String error message if assertion is not met. """ if len(polygons_expected) != 0: if isinstance(polygons_expected, list): From b7ec2d5661f5ab659853a8916ffd83db95fff391 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 18:42:11 -0700 Subject: [PATCH 21/23] Updating test data location for future tests --- matplotcheck/tests/conftest.py | 25 +++++++++++++++++++++++++ matplotcheck/tests/test_vector.py | 25 ------------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/matplotcheck/tests/conftest.py b/matplotcheck/tests/conftest.py index 4931fd81..39fcdab1 100644 --- a/matplotcheck/tests/conftest.py +++ b/matplotcheck/tests/conftest.py @@ -42,6 +42,31 @@ def pd_gdf(): return gdf +@pytest.fixture +def basic_polygon(): + """ + A square polygon spanning (2, 2) to (4.25, 4.25) in x and y directions. + Borrowed from rasterio/tests/conftest.py. + Returns + ------- + dict: GeoJSON-style geometry object. + Coordinates are in grid coordinates (Affine.identity()). + """ + return Polygon([(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]) + + +@pytest.fixture +def basic_polygon_gdf(basic_polygon): + """ + A GeoDataFrame containing the basic polygon geometry. + Returns + ------- + GeoDataFrame containing the basic_polygon polygon. + """ + gdf = gpd.GeoDataFrame(geometry=[basic_polygon], crs={"init": "epsg:4326"}) + return gdf + + @pytest.fixture def pd_xlabels(): """Create a DataFrame which uses the column labels as x-data.""" diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 81b08677..8182ec15 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -6,31 +6,6 @@ from matplotcheck.vector import VectorTester -@pytest.fixture -def basic_polygon(): - """ - A square polygon spanning (2, 2) to (4.25, 4.25) in x and y directions. - Borrowed from rasterio/tests/conftest.py. - Returns - ------- - dict: GeoJSON-style geometry object. - Coordinates are in grid coordinates (Affine.identity()). - """ - return Polygon([(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]) - - -@pytest.fixture -def basic_polygon_gdf(basic_polygon): - """ - A GeoDataFrame containing the basic polygon geometry. - Returns - ------- - GeoDataFrame containing the basic_polygon polygon. - """ - gdf = gpd.GeoDataFrame(geometry=[basic_polygon], crs={"init": "epsg:4326"}) - return gdf - - @pytest.fixture def poly_geo_plot(basic_polygon_gdf): """Create a polygon vector tester object.""" From 70f4c2d1edb4598284c4839a542ff8561067c6b7 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 18:45:19 -0700 Subject: [PATCH 22/23] Fixed import issue --- matplotcheck/base.py | 1 + matplotcheck/tests/test_vector.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/matplotcheck/base.py b/matplotcheck/base.py index fe828643..b7c17b37 100644 --- a/matplotcheck/base.py +++ b/matplotcheck/base.py @@ -15,6 +15,7 @@ from scipy import stats import pandas as pd import geopandas as gpd +from shapely.geometry import Polygon import numbers diff --git a/matplotcheck/tests/test_vector.py b/matplotcheck/tests/test_vector.py index 8182ec15..e65a0611 100644 --- a/matplotcheck/tests/test_vector.py +++ b/matplotcheck/tests/test_vector.py @@ -1,6 +1,5 @@ """Tests for the vector module""" import pytest -from shapely.geometry import Polygon import matplotlib.pyplot as plt import geopandas as gpd from matplotcheck.vector import VectorTester From e637c1451beaf34b6098d7810020bd3ee73438a6 Mon Sep 17 00:00:00 2001 From: Nathan Korinek Date: Wed, 12 Feb 2020 18:47:11 -0700 Subject: [PATCH 23/23] Moved import statement again --- matplotcheck/base.py | 1 - matplotcheck/tests/conftest.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/matplotcheck/base.py b/matplotcheck/base.py index b7c17b37..fe828643 100644 --- a/matplotcheck/base.py +++ b/matplotcheck/base.py @@ -15,7 +15,6 @@ from scipy import stats import pandas as pd import geopandas as gpd -from shapely.geometry import Polygon import numbers diff --git a/matplotcheck/tests/conftest.py b/matplotcheck/tests/conftest.py index 39fcdab1..278b3380 100644 --- a/matplotcheck/tests/conftest.py +++ b/matplotcheck/tests/conftest.py @@ -2,6 +2,7 @@ import pytest import pandas as pd import geopandas as gpd +from shapely.geometry import Polygon import numpy as np import matplotlib.pyplot as plt from matplotcheck.base import PlotTester