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

format & lint – cg/tests/*.py #614

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 8 additions & 13 deletions libpysal/cg/tests/test_ashapes.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
from unittest import TestCase, skipIf
from ...examples import get_path
from ..alpha_shapes import alpha_shape, alpha_shape_auto
import numpy as np
import os

import geopandas
import numpy as np
from packaging.version import Version
from shapely import geometry

try:
import geopandas
from shapely import geometry
from ...examples import get_path
from ..alpha_shapes import alpha_shape, alpha_shape_auto

GEOPANDAS_EXTINCT = False
GPD_013 = Version(geopandas.__version__) >= Version("0.13")
except ImportError:
GEOPANDAS_EXTINCT = True
GPD_013 = Version(geopandas.__version__) >= Version("0.13")

this_directory = os.path.dirname(__file__)


@skipIf(GEOPANDAS_EXTINCT, "Geopandas is missing, so test will not run.")
class Test_Alpha_Shapes:
class TestAlphaShapes:
def setup_method(self):
eberly = geopandas.read_file(get_path("eberly_net.shp"))
eberly_vertices = eberly.geometry.apply(
Expand Down
10 changes: 4 additions & 6 deletions libpysal/cg/tests/test_geoJSON.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from ..shapes import Point, Chain, asShape
from ...io.fileio import FileIO as psopen
from ... import examples as pysal_examples
from ...io.fileio import FileIO as psopen # noqa N813
from ..shapes import Chain, Point, asShape


class Testtest_MultiPloygon:
class TesttestMultiPloygon:
def test___init__1(self):
"""Tests conversion of polygons with multiple shells to
geoJSON multipolygons and back.

"""

ncovr = pysal_examples.load_example("NCOVR")
shp = psopen(pysal_examples.get_path("NAT.shp"), "r")
multipolygons = [p for p in shp if len(p.parts) > 1]
geoJSON = [p.__geo_interface__ for p in multipolygons]
for poly in multipolygons:
martinfleis marked this conversation as resolved.
Show resolved Hide resolved
json = poly.__geo_interface__
shape = asShape(json)
Expand All @@ -22,7 +20,7 @@ def test___init__1(self):
assert str(shape.parts) == str(poly.parts)


class Testtest_MultiLineString:
class TesttestMultiLineString:
def test_multipart_chain(self):
vertices = [
[Point((0, 0)), Point((1, 0)), Point((1, 5))],
Expand Down
13 changes: 7 additions & 6 deletions libpysal/cg/tests/test_locators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""locators Unittest."""
from ..locators import *

# ruff: noqa: F403, F405
from ..shapes import *


Expand All @@ -15,14 +17,14 @@ def setup_method(self):
pg = Polygon
polys = []
for i in range(5):
l = i * 10
r = l + 10
l_ = i * 10
r = l_ + 10
b = 10
t = 20
sw = pt((l, b))
sw = pt((l_, b))
se = pt((r, b))
ne = pt((r, t))
nw = pt((l, t))
nw = pt((l_, t))
polys.append(pg([sw, se, ne, nw]))
self.pl2 = PolygonLocator(polys)

Expand All @@ -40,7 +42,6 @@ def test_inside(self):
assert len(res) == 1

def test_overlapping(self):

qr = Rectangle(3, 3, 5, 5)
res = self.pl.overlapping(qr)
assert len(res) == 2
Expand All @@ -50,4 +51,4 @@ def test_overlapping(self):

qr = Rectangle(2, 12, 35, 15)
res = self.pl2.overlapping(qr)
assert len(res) == 4
assert len(res) == 4
12 changes: 6 additions & 6 deletions libpysal/cg/tests/test_rtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ def setup_method(self):
k = 10
w = 20
objects = {}
id = 0
id_ = 0
for i in range(k):
mn_y = i * w
mx_y = mn_y + w
for j in range(k):
mn_x = j * w
mx_x = mn_x + w
objects[id] = Rect(mn_x, mn_y, mx_x, mx_y)
id += 1
objects[id_] = Rect(mn_x, mn_y, mx_x, mx_y)
id_ += 1
self.objects = objects

def test_rtree(self):
t = RTree()
for object in self.objects:
t.insert(object, self.objects[object])
for object_ in self.objects:
t.insert(object_, self.objects[object_])
assert len(self.objects) == 100

qr = Rect(5, 5, 25, 25)
Expand Down Expand Up @@ -49,5 +49,5 @@ def test_rtree(self):

qr = Rect(5, 6, 65, 7)

res = [r.leaf_obj() for r in t.query_rect((qr)) if r.is_leaf()]
res = [r.leaf_obj() for r in t.query_rect(qr) if r.is_leaf()]
assert len(res) == 4
7 changes: 5 additions & 2 deletions libpysal/cg/tests/test_segmentLocator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Segment Locator Unittest."""
from ..shapes import *

# ruff: noqa: F403, F405

from ..segmentLocator import *
from ..shapes import *


class TestSegmentGrid:
Expand Down Expand Up @@ -32,4 +35,4 @@ def test_nearest_2(self):
# Bottom Edge
assert [0, 2, 3] == self.grid.nearest(Point((5.0, -100000.0)))
# Top Edge
assert [0, 1, 2] == self.grid.nearest(Point((5.0, 100000.0)))
assert [0, 1, 2] == self.grid.nearest(Point((5.0, 100000.0)))
103 changes: 52 additions & 51 deletions libpysal/cg/tests/test_shapes.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import pytest
from ..shapes import Point, LineSegment, Line, Ray, Chain, Rectangle, Polygon

from ..shapes import Chain, Line, LineSegment, Point, Polygon, Ray, Rectangle

class Testtest_Point:

class TesttestPoint:
def test___init__1(self):
"""Tests whether points are created without issue."""

for l in [(-5.0, 10.0), (0.0, -6.0), (float(1e300), float(-1e300))]:
p = Point(l)
for l_ in [(-5.0, 10.0), (0.0, -6.0), (1e300, float(-1e300))]:
Point(l_)

def test___str__1(self):
"""Tests whether the string produced is valid for corner cases."""

for l in [(-5, 10), (0, -6.0), (float(1e300), -1e300)]:
p = Point(l)
for l_ in [(-5, 10), (0, -6.0), (1e300, -1e300)]:
p = Point(l_)
# Recast to floats like point does
assert str(p) == str((float(l[0]), float(l[1])))
assert str(p) == str((float(l_[0]), float(l_[1])))


class Testtest_LineSegment:
class TesttestLineSegment:
def test_is_ccw1(self):
"""Test corner cases for horizontal segment starting at origin."""

Expand Down Expand Up @@ -186,7 +187,7 @@ def test_line1(self):
assert math.isnan(ls.line.b)

ls = LineSegment(Point((0, 0)), Point((0, 0)))
assert ls.line == None
assert ls.line is None

ls = LineSegment(Point((5, 0)), Point((10, 0)))
ls1 = LineSegment(Point((5, 0)), Point((10, 1)))
Expand All @@ -197,69 +198,69 @@ def test_line1(self):
assert ls.intersect(ls2)


class Testtest_Line:
class TesttestLine:
def test___init__1(self):
"""Tests a variety of generic cases."""

for m, b in [(4, 0.0), (-140, 5), (0, 0)]:
l = Line(m, b)
_ = Line(m, b)

def test_y1(self):
"""Tests a variety of generic and special cases (+-infinity)."""

l = Line(0, 0)
assert l.y(0) == 0
assert l.y(-1e600) == 0
assert l.y(1e600) == 0
l_ = Line(0, 0)
assert l_.y(0) == 0
assert l_.y(-1e600) == 0
assert l_.y(1e600) == 0

l = Line(1, 1)
assert l.y(2) == 3
assert l.y(-1e600) == -1e600
assert l.y(1e600) == 1e600
l_ = Line(1, 1)
assert l_.y(2) == 3
assert l_.y(-1e600) == -1e600
assert l_.y(1e600) == 1e600

l = Line(-1, 1)
assert l.y(2) == -1
assert l.y(-1e600) == 1e600
assert l.y(1e600) == -1e600
l_ = Line(-1, 1)
assert l_.y(2) == -1
assert l_.y(-1e600) == 1e600
assert l_.y(1e600) == -1e600

def test_x1(self):
"""Tests a variety of generic and special cases (+-infinity)."""

l = Line(0, 0)
l_ = Line(0, 0)

# self.assertEquals(l.x(0), 0)
with pytest.raises(ArithmeticError):
l.x(0)
l_.x(0)
with pytest.raises(ArithmeticError):
l.x(-1e600)
l_.x(-1e600)
with pytest.raises(ArithmeticError):
l.x(1e600)
l_.x(1e600)

l = Line(1, 1)
assert l.x(3) == 2
assert l.x(-1e600) == -1e600
assert l.x(1e600) == 1e600
l_ = Line(1, 1)
assert l_.x(3) == 2
assert l_.x(-1e600) == -1e600
assert l_.x(1e600) == 1e600

l = Line(-1, 1)
assert l.x(2) == -1
assert l.x(-1e600) == 1e600
assert l.x(1e600) == -1e600
l_ = Line(-1, 1)
assert l_.x(2) == -1
assert l_.x(-1e600) == 1e600
assert l_.x(1e600) == -1e600


class Testtest_Ray:
class TesttestRay:
def test___init__1(self):
"""Tests generic cases."""

r = Ray(Point((0, 0)), Point((1, 1)))
r = Ray(Point((8, -3)), Point((-5, 9)))
_ = Ray(Point((0, 0)), Point((1, 1)))
_ = Ray(Point((8, -3)), Point((-5, 9)))


class Testtest_Chain:
class TesttestChain:
def test___init__1(self):
"""Generic testing that no exception is thrown."""

c = Chain([Point((0, 0))])
c = Chain([[Point((0, 0)), Point((1, 1))], [Point((2, 5))]])
_ = Chain([Point((0, 0))])
_ = Chain([[Point((0, 0)), Point((1, 1))], [Point((2, 5))]])

def test_vertices1(self):
"""Testing for repeated vertices and multiple parts."""
Expand Down Expand Up @@ -325,7 +326,7 @@ def test_len1(self):
assert Chain(vertices).len == 6 + 10


class Testtest_Polygon:
class TesttestPolygon:
def test___init__1(self):
"""Test various input configurations (list vs. lists of lists, holes)."""

Expand All @@ -337,32 +338,32 @@ def test___init__1(self):
# one part, multi holes
# multi part, multi holes

p = Polygon([Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))])
p = Polygon(
_ = Polygon([Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))])
_ = Polygon(
[
[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
[Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))],
]
)
p = Polygon(
_ = Polygon(
[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
holes=[Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
)
p = Polygon(
_ = Polygon(
[
[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
[Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))],
],
holes=[Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
)
p = Polygon(
_ = Polygon(
[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
holes=[
[Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
[Point((6, 6)), Point((6, 8)), Point((8, 8)), Point((8, 6))],
],
)
p = Polygon(
_ = Polygon(
[
[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
[Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))],
Expand Down Expand Up @@ -586,21 +587,21 @@ def test_contains_point(self):
assert p.contains_point((10, 10)) == 0


class Testtest_Rectangle:
class TesttestRectangle:
def test___init__1(self):
"""Test exceptions are thrown correctly."""

try:
# right < left
r = Rectangle(1, 1, -1, 5)
_ = Rectangle(1, 1, -1, 5)
except ArithmeticError:
pass
else:
pytest.fail()

try:
# upper < lower
r = Rectangle(1, 1, 5, -1)
_ = Rectangle(1, 1, 5, -1)
except ArithmeticError:
pass
else:
Expand Down
10 changes: 6 additions & 4 deletions libpysal/cg/tests/test_sphere.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import pytest
from .. import sphere
from ...io.fileio import FileIO as psopen
from ... import examples as pysal_examples
import math

import numpy as np
import pytest

from ... import examples as pysal_examples
from ...io.fileio import FileIO as psopen # noqa N813
from .. import sphere


class TestSphere:
Expand Down
Loading
Loading