File: test_random.py

package info (click to toggle)
python-geopandas 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,752 kB
  • sloc: python: 26,021; makefile: 147; sh: 25
file content (88 lines) | stat: -rw-r--r-- 2,501 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import numpy

import shapely

import geopandas
from geopandas.tools._random import uniform

import pytest


@pytest.fixture
def multipolygons(nybb_filename):
    return geopandas.read_file(nybb_filename).geometry


@pytest.fixture
def polygons(multipolygons):
    return multipolygons.explode(ignore_index=True).geometry


@pytest.fixture
def multilinestrings(multipolygons):
    return multipolygons.boundary


@pytest.fixture
def linestrings(polygons):
    return polygons.boundary


@pytest.fixture
def points(multipolygons):
    return multipolygons.centroid


@pytest.mark.parametrize("size", [10, 100])
@pytest.mark.parametrize(
    "geom_fixture", ["multipolygons", "polygons", "multilinestrings", "linestrings"]
)
def test_uniform(geom_fixture, size, request):
    geom = request.getfixturevalue(geom_fixture)[0]
    sample = uniform(geom, size=size, rng=1)
    sample_series = (
        geopandas.GeoSeries(sample).explode(index_parts=True).reset_index(drop=True)
    )
    assert len(sample_series) == size
    sample_in_geom = sample_series.buffer(0.00000001).sindex.query(
        geom, predicate="intersects"
    )
    assert len(sample_in_geom) == size


def test_uniform_unsupported(points):
    with pytest.warns(UserWarning, match="Sampling is not supported"):
        sample = uniform(points[0], size=10, rng=1)
    assert sample.is_empty


def test_uniform_generator(polygons):
    sample = uniform(polygons[0], size=10, rng=1)
    sample2 = uniform(polygons[0], size=10, rng=1)
    assert sample.equals(sample2)

    generator = numpy.random.default_rng(seed=1)
    gen_sample = uniform(polygons[0], size=10, rng=generator)
    gen_sample2 = uniform(polygons[0], size=10, rng=generator)

    assert sample.equals(gen_sample)
    assert not sample.equals(gen_sample2)


@pytest.mark.parametrize("size", range(5, 12))
def test_unimodality(size):  # GH 3470
    circle = shapely.Point(0, 0).buffer(1)
    generator = numpy.random.default_rng(seed=1)
    centers_x = []
    centers_y = []
    for _ in range(200):
        pts = shapely.get_coordinates(uniform(circle, size=2**size, rng=generator))
        centers_x.append(numpy.mean(pts[:, 0]))
        centers_y.append(numpy.mean(pts[:, 1]))

    numpy.testing.assert_allclose(numpy.mean(centers_x), 0, atol=1e-2)
    numpy.testing.assert_allclose(numpy.mean(centers_y), 0, atol=1e-2)

    stats = pytest.importorskip("scipy.stats")
    assert stats.shapiro(centers_x).pvalue > 0.05
    assert stats.shapiro(centers_y).pvalue > 0.05