|
4 | 4 | import io
|
5 | 5 |
|
6 | 6 | import numpy as np
|
| 7 | +from numpy.testing import assert_array_almost_equal, assert_array_equal |
| 8 | + |
7 | 9 | import pytest
|
8 | 10 |
|
9 | 11 | from matplotlib.testing.decorators import image_comparison
|
@@ -49,33 +51,158 @@ def test_diamond():
|
49 | 51 |
|
50 | 52 | def test_noise():
|
51 | 53 | np.random.seed(0)
|
52 |
| - x = np.random.uniform(size=(5000,)) * 50 |
| 54 | + x = np.random.uniform(size=(50000,)) * 50 |
53 | 55 |
|
54 | 56 | fig, ax = plt.subplots()
|
55 | 57 | p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0)
|
56 | 58 |
|
57 | 59 | path = p1[0].get_path()
|
58 | 60 | transform = p1[0].get_transform()
|
59 | 61 | path = transform.transform_path(path)
|
60 |
| - simplified = list(path.iter_segments(simplify=(800, 600))) |
| 62 | + simplified = path.cleaned(simplify=True) |
| 63 | + |
| 64 | + assert simplified.vertices.size == 25512 |
| 65 | + |
| 66 | + |
| 67 | +def test_antiparallel_simplification(): |
| 68 | + def _get_simplified(x, y): |
| 69 | + fig, ax = plt.subplots() |
| 70 | + p1 = ax.plot(x, y) |
| 71 | + |
| 72 | + path = p1[0].get_path() |
| 73 | + transform = p1[0].get_transform() |
| 74 | + path = transform.transform_path(path) |
| 75 | + simplified = path.cleaned(simplify=True) |
| 76 | + simplified = transform.inverted().transform_path(simplified) |
| 77 | + |
| 78 | + return simplified |
| 79 | + |
| 80 | + # test ending on a maximum |
| 81 | + x = [0, 0, 0, 0, 0, 1] |
| 82 | + y = [.5, 1, -1, 1, 2, .5] |
| 83 | + |
| 84 | + simplified = _get_simplified(x, y) |
| 85 | + |
| 86 | + assert_array_almost_equal([[0., 0.5], |
| 87 | + [0., -1.], |
| 88 | + [0., 2.], |
| 89 | + [1., 0.5]], |
| 90 | + simplified.vertices[:-2, :]) |
| 91 | + |
| 92 | + # test ending on a minimum |
| 93 | + x = [0, 0, 0, 0, 0, 1] |
| 94 | + y = [.5, 1, -1, 1, -2, .5] |
| 95 | + |
| 96 | + simplified = _get_simplified(x, y) |
| 97 | + |
| 98 | + assert_array_almost_equal([[0., 0.5], |
| 99 | + [0., 1.], |
| 100 | + [0., -2.], |
| 101 | + [1., 0.5]], |
| 102 | + simplified.vertices[:-2, :]) |
| 103 | + |
| 104 | + # test ending in between |
| 105 | + x = [0, 0, 0, 0, 0, 1] |
| 106 | + y = [.5, 1, -1, 1, 0, .5] |
| 107 | + |
| 108 | + simplified = _get_simplified(x, y) |
| 109 | + |
| 110 | + assert_array_almost_equal([[0., 0.5], |
| 111 | + [0., 1.], |
| 112 | + [0., -1.], |
| 113 | + [0., 0.], |
| 114 | + [1., 0.5]], |
| 115 | + simplified.vertices[:-2, :]) |
| 116 | + |
| 117 | + # test no anti-parallel ending at max |
| 118 | + x = [0, 0, 0, 0, 0, 1] |
| 119 | + y = [.5, 1, 2, 1, 3, .5] |
| 120 | + |
| 121 | + simplified = _get_simplified(x, y) |
| 122 | + |
| 123 | + assert_array_almost_equal([[0., 0.5], |
| 124 | + [0., 3.], |
| 125 | + [1., 0.5]], |
| 126 | + simplified.vertices[:-2, :]) |
| 127 | + |
| 128 | + # test no anti-parallel ending in middle |
| 129 | + x = [0, 0, 0, 0, 0, 1] |
| 130 | + y = [.5, 1, 2, 1, 1, .5] |
| 131 | + |
| 132 | + simplified = _get_simplified(x, y) |
| 133 | + |
| 134 | + assert_array_almost_equal([[0., 0.5], |
| 135 | + [0., 2.], |
| 136 | + [0., 1.], |
| 137 | + [1., 0.5]], |
| 138 | + simplified.vertices[:-2, :]) |
| 139 | + |
| 140 | + |
| 141 | +# Only consider angles in 0 <= angle <= pi/2, otherwise |
| 142 | +# using min/max will get the expected results out of order: |
| 143 | +# min/max for simplification code depends on original vector, |
| 144 | +# and if angle is outside above range then simplification |
| 145 | +# min/max will be opposite from actual min/max. |
| 146 | +@pytest.mark.parametrize('angle', [0, np.pi/4, np.pi/3, np.pi/2]) |
| 147 | +@pytest.mark.parametrize('offset', [0, .5]) |
| 148 | +def test_angled_antiparallel(angle, offset): |
| 149 | + scale = 5 |
| 150 | + np.random.seed(19680801) |
| 151 | + # get 15 random offsets |
| 152 | + # TODO: guarantee offset > 0 results in some offsets < 0 |
| 153 | + vert_offsets = (np.random.rand(15) - offset) * scale |
| 154 | + # always start at 0 so rotation makes sense |
| 155 | + vert_offsets[0] = 0 |
| 156 | + # always take the first step the same direction |
| 157 | + vert_offsets[1] = 1 |
| 158 | + # compute points along a diagonal line |
| 159 | + x = np.sin(angle) * vert_offsets |
| 160 | + y = np.cos(angle) * vert_offsets |
| 161 | + |
| 162 | + # will check these later |
| 163 | + x_max = x[1:].max() |
| 164 | + x_min = x[1:].min() |
| 165 | + |
| 166 | + y_max = y[1:].max() |
| 167 | + y_min = y[1:].min() |
| 168 | + |
| 169 | + if offset > 0: |
| 170 | + p_expected = Path([[0, 0], |
| 171 | + [x_max, y_max], |
| 172 | + [x_min, y_min], |
| 173 | + [x[-1], y[-1]], |
| 174 | + [0, 0]], |
| 175 | + codes=[1, 2, 2, 2, 0]) |
| 176 | + |
| 177 | + else: |
| 178 | + p_expected = Path([[0, 0], |
| 179 | + [x_max, y_max], |
| 180 | + [x[-1], y[-1]], |
| 181 | + [0, 0]], |
| 182 | + codes=[1, 2, 2, 0]) |
| 183 | + |
| 184 | + p = Path(np.vstack([x, y]).T) |
| 185 | + p2 = p.cleaned(simplify=True) |
61 | 186 |
|
62 |
| - assert len(simplified) == 3884 |
| 187 | + assert_array_almost_equal(p_expected.vertices, |
| 188 | + p2.vertices) |
| 189 | + assert_array_equal(p_expected.codes, p2.codes) |
63 | 190 |
|
64 | 191 |
|
65 | 192 | def test_sine_plus_noise():
|
66 | 193 | np.random.seed(0)
|
67 |
| - x = (np.sin(np.linspace(0, np.pi * 2.0, 1000)) + |
68 |
| - np.random.uniform(size=(1000,)) * 0.01) |
| 194 | + x = (np.sin(np.linspace(0, np.pi * 2.0, 50000)) + |
| 195 | + np.random.uniform(size=(50000,)) * 0.01) |
69 | 196 |
|
70 | 197 | fig, ax = plt.subplots()
|
71 | 198 | p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0)
|
72 | 199 |
|
73 | 200 | path = p1[0].get_path()
|
74 | 201 | transform = p1[0].get_transform()
|
75 | 202 | path = transform.transform_path(path)
|
76 |
| - simplified = list(path.iter_segments(simplify=(800, 600))) |
| 203 | + simplified = path.cleaned(simplify=True) |
77 | 204 |
|
78 |
| - assert len(simplified) == 876 |
| 205 | + assert simplified.vertices.size == 25240 |
79 | 206 |
|
80 | 207 |
|
81 | 208 | @image_comparison(baseline_images=['simplify_curve'], remove_text=True)
|
@@ -110,9 +237,9 @@ def test_fft_peaks():
|
110 | 237 | path = p1[0].get_path()
|
111 | 238 | transform = p1[0].get_transform()
|
112 | 239 | path = transform.transform_path(path)
|
113 |
| - simplified = list(path.iter_segments(simplify=(800, 600))) |
| 240 | + simplified = path.cleaned(simplify=True) |
114 | 241 |
|
115 |
| - assert len(simplified) == 20 |
| 242 | + assert simplified.vertices.size == 36 |
116 | 243 |
|
117 | 244 |
|
118 | 245 | def test_start_with_moveto():
|
|
0 commit comments