Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional vector tests #212

Merged
merged 60 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
8344d56
Merge pull request #1 from earthlab/master
nkorinek Feb 6, 2020
b87394d
Merge pull request #2 from earthlab/master
nkorinek Feb 12, 2020
7cf2703
Merge pull request #3 from earthlab/master
nkorinek Feb 12, 2020
c4d904e
Merge pull request #4 from earthlab/master
nkorinek Feb 13, 2020
c6deb54
Merge pull request #5 from earthlab/master
nkorinek Feb 13, 2020
30b4aa1
Merge pull request #7 from earthlab/master
nkorinek Feb 17, 2020
c1bb42b
Added a get_points() and assert_points() function to the vector tester.
nkorinek Feb 26, 2020
6cd48e3
Added in get_points() and assert_points() functions with tests
nkorinek Feb 26, 2020
5e6a175
Added proper documentation
nkorinek Feb 26, 2020
408ab38
Small fix to please codacy
nkorinek Feb 26, 2020
8325ed9
black
nkorinek Feb 27, 2020
7ac5d41
Updated changelog
nkorinek Feb 27, 2020
6ab2486
Merge pull request #8 from earthlab/master
nkorinek Mar 3, 2020
8a2ce1b
Fixing merge conflicts
nkorinek Mar 3, 2020
678fdf4
First round of tests for vector
nkorinek Mar 7, 2020
5077e25
Merge pull request #9 from earthlab/master
nkorinek Mar 7, 2020
5f4f719
Fixed conflicts
nkorinek Mar 7, 2020
50a9360
black
nkorinek Mar 7, 2020
4ff1b26
Merge pull request #10 from earthlab/master
nkorinek Mar 11, 2020
c75e421
Rough draft for bug fix
nkorinek Mar 16, 2020
075052c
Fixed bug with multiple geometries plotted alongside bug with identic…
nkorinek Mar 16, 2020
34838ee
fixing merge conflict
nkorinek Mar 16, 2020
a792725
typo
nkorinek Mar 16, 2020
6876a50
Fixed small bug with markersize
nkorinek Mar 17, 2020
b73e4ef
Added comments explaining code
nkorinek Mar 17, 2020
8785353
merging
nkorinek Mar 17, 2020
c53603f
rough drafts of more tests
nkorinek Mar 17, 2020
8c6a87d
Added more tests for legends
nkorinek Mar 17, 2020
89a2adc
Added more tests, and took out broken tests
nkorinek Mar 17, 2020
e70c887
small codacy fix
nkorinek Mar 17, 2020
7cf2ec7
Fixed test!
nkorinek Mar 18, 2020
d2a1972
Taking out redundant tests
nkorinek Mar 18, 2020
46804db
Took out unneccesary tests
nkorinek Mar 18, 2020
dcd8e30
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 18, 2020
ccd3ba3
Added plt.close
nkorinek Mar 18, 2020
4c30c50
Added more tests
nkorinek Mar 18, 2020
7e59d47
Typo
nkorinek Mar 18, 2020
41aedbb
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 18, 2020
601a96d
more tests for uncovered parts of the vector file
nkorinek Mar 18, 2020
bc81a25
black
nkorinek Mar 18, 2020
80f7e77
Fixed issues with vector checking truth value of dataframe, and added…
nkorinek Mar 18, 2020
3b8a20b
Fixing how vector checks for truth value of a dataframe
nkorinek Mar 18, 2020
fcdafa6
Merge branch 'assert_points' of https://github.com/nkorinek/matplotch…
nkorinek Mar 18, 2020
dca38af
Added more coverage!
nkorinek Mar 18, 2020
de25545
Merge pull request #11 from earthlab/master
nkorinek Mar 19, 2020
ecbd29f
Fixing merge conflicts
nkorinek Mar 19, 2020
f489613
Broke tests up into individual files
nkorinek Mar 19, 2020
b9d9537
black
nkorinek Mar 19, 2020
084ffea
Added tests for outlier cases
nkorinek Mar 19, 2020
2f2f01d
Merge pull request #12 from earthlab/master
nkorinek Mar 19, 2020
f7b4e59
Merge branch 'master' of https://github.com/nkorinek/matplotcheck int…
nkorinek Mar 19, 2020
6ce798c
Update matplotcheck/tests/test_points.py
nkorinek Mar 19, 2020
71e8040
took plt.gca() out of tests
nkorinek Mar 19, 2020
f3bb6fe
Merge branch 'additional-vector-tests' of https://github.com/nkorinek…
nkorinek Mar 19, 2020
7de3c09
Added changes suggested on GitHub
nkorinek Mar 20, 2020
fd63fe4
Update CHANGELOG.md
Mar 20, 2020
dd50a4a
fix import order
Mar 20, 2020
e7a8ea6
import order fix
Mar 20, 2020
3fc791e
import order
Mar 20, 2020
44f4334
import order
Mar 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
* Created tests for the vector module (@nkorinek, #209)
* Created functions to test point geometries in VectorTester (@nkorinek, #176)
* made `assert_string_contains()` accept correct strings with spaces in them (@nkorinek, #182)
* added contributors file and updated README to remove that information (@nkorinek, #121)
Expand Down
58 changes: 30 additions & 28 deletions matplotcheck/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Pytest fixtures for matplotcheck tests"""
import pytest
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon, LineString
from matplotcheck.base import PlotTester


Expand Down Expand Up @@ -32,14 +32,14 @@ def pd_gdf():
"""Create a geopandas GeoDataFrame for testing"""
df = pd.DataFrame(
{
"lat": np.random.randint(-85, 85, size=100),
"lon": np.random.randint(-180, 180, size=100),
"lat": np.random.randint(-85, 85, size=5),
"lon": np.random.randint(-180, 180, size=5),
}
)
gdf = gpd.GeoDataFrame(
{"A": np.arange(100)}, geometry=gpd.points_from_xy(df.lon, df.lat)
{"A": np.arange(5)}, geometry=gpd.points_from_xy(df.lon, df.lat)
)

gdf["attr"] = ["Tree", "Tree", "Bush", "Bush", "Bush"]
return gdf


Expand Down Expand Up @@ -68,6 +68,15 @@ def basic_polygon_gdf(basic_polygon):
return gdf


@pytest.fixture
def two_line_gdf():
""" Create Line Objects For Testing """
linea = LineString([(1, 1), (2, 2), (3, 2), (5, 3)])
lineb = LineString([(3, 4), (5, 7), (12, 2), (10, 5), (9, 7.5)])
gdf = gpd.GeoDataFrame([1, 2], geometry=[linea, lineb], crs="epsg:4326")
return gdf


@pytest.fixture
def pd_xlabels():
"""Create a DataFrame which uses the column labels as x-data."""
Expand All @@ -85,9 +94,7 @@ def pt_scatter_plt(pd_df):
ax.set_xlabel("x label")
ax.set_ylabel("y label")

axis = plt.gca()

return PlotTester(axis)
return PlotTester(ax)


@pytest.fixture
Expand All @@ -110,9 +117,7 @@ def pt_line_plt(pd_df):
ax_position.ymax - 0.25, ax_position.ymin - 0.075, "Figure Caption"
)

axis = plt.gca()

return PlotTester(axis)
return PlotTester(ax)


@pytest.fixture
Expand All @@ -123,9 +128,7 @@ def pt_multi_line_plt(pd_df):
ax.set_ylim((0, 140))
ax.legend(loc="center left", title="Legend", bbox_to_anchor=(1, 0.5))

axis = plt.gca()

return PlotTester(axis)
return PlotTester(ax)


@pytest.fixture
Expand All @@ -138,9 +141,7 @@ def pt_bar_plt(pd_df):
ax.set_xlabel("x label")
ax.set_ylabel("y label")

axis = plt.gca()

return PlotTester(axis)
return PlotTester(ax)


@pytest.fixture
Expand All @@ -150,21 +151,22 @@ def pt_time_line_plt(pd_df_timeseries):

pd_df_timeseries.plot("time", "A", kind="line", ax=ax)

axis = plt.gca()

return PlotTester(axis)
return PlotTester(ax)


@pytest.fixture
def pt_geo_plot(pd_gdf):
"""Create a geo plot for testing"""
fig, ax = plt.subplots()
size = 0
point_symb = {"Tree": "green", "Bush": "brown"}

pd_gdf.plot(ax=ax)
ax.set_title("My Plot Title", fontsize=30)
ax.set_xlabel("x label")
ax.set_ylabel("y label")
for ctype, points in pd_gdf.groupby("attr"):
color = point_symb[ctype]
label = ctype
size += 100
points.plot(color=color, ax=ax, label=label, markersize=size)

axis = plt.gca()
ax.legend(title="Legend", loc=(1.1, 0.1))

return PlotTester(axis)
return PlotTester(ax)
171 changes: 171 additions & 0 deletions matplotcheck/tests/test_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
"""Tests for the vector module"""
import matplotlib
import matplotlib.pyplot as plt
import pytest
import geopandas as gpd
from shapely.geometry import LineString

from matplotcheck.vector import VectorTester

matplotlib.use("Agg")


@pytest.fixture
def multi_line_gdf(two_line_gdf):
""" Create a multi-line GeoDataFrame.
This has one multi line and another regular line.
"""
# Create a single and multi line object
multiline_feat = two_line_gdf.unary_union
linec = LineString([(2, 1), (3, 1), (4, 1), (5, 2)])
out_df = gpd.GeoDataFrame(
geometry=gpd.GeoSeries([multiline_feat, linec]), crs="epsg:4326",
)
out_df["attr"] = ["road", "stream"]
return out_df


@pytest.fixture
def mixed_type_geo_plot(pd_gdf, multi_line_gdf):
"""Create a point plot for testing"""
_, ax = plt.subplots()

pd_gdf.plot(ax=ax)
multi_line_gdf.plot(ax=ax)

return VectorTester(ax)


@pytest.fixture
def line_geo_plot(two_line_gdf):
"""Create a line vector tester object."""
_, ax = plt.subplots()

two_line_gdf.plot(ax=ax)

return VectorTester(ax)


@pytest.fixture
def multiline_geo_plot(multi_line_gdf):
"""Create a multiline vector tester object."""
_, ax = plt.subplots()

multi_line_gdf.plot(ax=ax, column="attr")

return VectorTester(ax)


@pytest.fixture
def multiline_geo_plot_bad(multi_line_gdf):
"""Create a multiline vector tester object."""
_, ax = plt.subplots()

multi_line_gdf.plot(ax=ax)

return VectorTester(ax)


def test_assert_line_geo(line_geo_plot, two_line_gdf):
"""Test that lines are asserted correctly"""
line_geo_plot.assert_lines(two_line_gdf)
plt.close("all")


def test_assert_multiline_geo(multiline_geo_plot, multi_line_gdf):
"""Test that multi lines are asserted correctly"""
multiline_geo_plot.assert_lines(multi_line_gdf)
plt.close("all")


def test_assert_line_geo_fail(line_geo_plot, multi_line_gdf):
"""Test that lines fail correctly"""
with pytest.raises(AssertionError, match="Incorrect Line Data"):
line_geo_plot.assert_lines(multi_line_gdf)
plt.close("all")


def test_assert_multiline_geo_fail(multiline_geo_plot, two_line_gdf):
"""Test that multi lines fail correctly"""
with pytest.raises(AssertionError, match="Incorrect Line Data"):
multiline_geo_plot.assert_lines(two_line_gdf)
plt.close("all")


def test_assert_line_fails_list(line_geo_plot):
"""Test that assert_lines fails when passed a list"""
linelist = [
[(1, 1), (2, 2), (3, 2), (5, 3)],
[(3, 4), (5, 7), (12, 2), (10, 5), (9, 7.5)],
]
with pytest.raises(ValueError, match="lines_expected is not expected ty"):
line_geo_plot.assert_lines(linelist)
plt.close("all")


def test_assert_line_geo_passed_nothing(line_geo_plot):
"""Test that assertion passes when passed None"""
line_geo_plot.assert_lines(None)
plt.close("all")


def test_get_lines_geometry(line_geo_plot):
"""Test that get_lines returns the proper values"""
lines = [(LineString(i[0])) for i in line_geo_plot.get_lines().values]
geometries = gpd.GeoDataFrame(geometry=lines)
line_geo_plot.assert_lines(geometries)
plt.close("all")


def test_assert_lines_grouped_by_type(multiline_geo_plot, multi_line_gdf):
"""Test that assert works for grouped line plots"""
multiline_geo_plot.assert_lines_grouped_by_type(multi_line_gdf, "attr")
plt.close("all")


def test_assert_lines_grouped_by_type_fail(
multiline_geo_plot_bad, multi_line_gdf
):
"""Test that assert fails for incorrectly grouped line plots"""
with pytest.raises(AssertionError, match="Line attributes not accurate "):
multiline_geo_plot_bad.assert_lines_grouped_by_type(
multi_line_gdf, "attr"
)
plt.close("all")


def test_assert_lines_grouped_by_type_passes_with_none(multiline_geo_plot):
"""Test that assert passes if nothing is passed into it"""
multiline_geo_plot.assert_lines_grouped_by_type(None, None)
plt.close("all")


def test_assert_lines_grouped_by_type_fails_non_gdf(
multiline_geo_plot, multi_line_gdf
):
"""Test that assert fails if a list is passed into it"""
with pytest.raises(ValueError, match="lines_expected is not of expected "):
multiline_geo_plot.assert_lines_grouped_by_type(
multi_line_gdf.to_numpy(), "attr"
)
plt.close("all")


def test_mixed_type_passes(mixed_type_geo_plot, pd_gdf):
"""Tests that points passes with a mixed type plot"""
mixed_type_geo_plot.assert_points(pd_gdf)
plt.close("all")


def test_get_lines_by_collection(multiline_geo_plot):
"""Test that get_lines_by_collection returns the correct values"""
lines_list = [
[
[(1, 1), (2, 2), (3, 2), (5, 3)],
[(3, 4), (5, 7), (12, 2), (10, 5), (9, 7.5)],
[(2, 1), (3, 1), (4, 1), (5, 2)],
]
]
sorted_lines_list = sorted([sorted(l) for l in lines_list])
assert sorted_lines_list == multiline_geo_plot.get_lines_by_collection()
plt.close("all")
Loading