|
4 | 4 | import io
|
5 | 5 |
|
6 | 6 | import numpy as np
|
| 7 | +from numpy.testing import assert_array_almost_equal |
| 8 | + |
7 | 9 | import pytest
|
8 | 10 |
|
9 | 11 | from matplotlib.testing.decorators import image_comparison
|
@@ -62,6 +64,102 @@ def test_noise():
|
62 | 64 | assert simplified.vertices.size == 25340
|
63 | 65 |
|
64 | 66 |
|
| 67 | +def test_antiparallel_simplification(): |
| 68 | + # test ending on a maximum |
| 69 | + x = [ 0, 0, 0, 0, 0, 1] |
| 70 | + y = [.5, 1, -1, 1, 2, .5] |
| 71 | + |
| 72 | + fig, ax = plt.subplots() |
| 73 | + p1 = ax.plot(x, y) |
| 74 | + |
| 75 | + path = p1[0].get_path() |
| 76 | + transform = p1[0].get_transform() |
| 77 | + path = transform.transform_path(path) |
| 78 | + simplified = path.cleaned(simplify=True) |
| 79 | + simplified = transform.inverted().transform_path(simplified) |
| 80 | + |
| 81 | + assert_array_almost_equal([[ 0. , 0.5], |
| 82 | + [ 0. , -1. ], |
| 83 | + [ 0. , 2. ], |
| 84 | + [ 1. , 0.5]], |
| 85 | + simplified.vertices[:-2, :]) |
| 86 | + |
| 87 | + # test ending on a minimum |
| 88 | + x = [ 0, 0, 0, 0, 0, 1] |
| 89 | + y = [.5, 1, -1, 1, -2, .5] |
| 90 | + |
| 91 | + fig, ax = plt.subplots() |
| 92 | + p1 = ax.plot(x, y) |
| 93 | + |
| 94 | + path = p1[0].get_path() |
| 95 | + transform = p1[0].get_transform() |
| 96 | + path = transform.transform_path(path) |
| 97 | + simplified = path.cleaned(simplify=True) |
| 98 | + simplified = transform.inverted().transform_path(simplified) |
| 99 | + |
| 100 | + assert_array_almost_equal([[ 0. , 0.5], |
| 101 | + [ 0. , 1. ], |
| 102 | + [ 0. , -2. ], |
| 103 | + [ 1. , 0.5]], |
| 104 | + simplified.vertices[:-2, :]) |
| 105 | + |
| 106 | + # test ending in between |
| 107 | + x = [ 0, 0, 0, 0, 0, 1] |
| 108 | + y = [.5, 1, -1, 1, 0, .5] |
| 109 | + |
| 110 | + fig, ax = plt.subplots() |
| 111 | + p1 = ax.plot(x, y) |
| 112 | + |
| 113 | + path = p1[0].get_path() |
| 114 | + transform = p1[0].get_transform() |
| 115 | + path = transform.transform_path(path) |
| 116 | + simplified = path.cleaned(simplify=True) |
| 117 | + simplified = transform.inverted().transform_path(simplified) |
| 118 | + |
| 119 | + assert_array_almost_equal([[ 0. , 0.5], |
| 120 | + [ 0. , 1. ], |
| 121 | + [ 0. , -1. ], |
| 122 | + [ 0. , 0. ], |
| 123 | + [ 1. , 0.5]], |
| 124 | + simplified.vertices[:-2, :]) |
| 125 | + |
| 126 | + # test no anti-parallel ending at max |
| 127 | + x = [ 0, 0, 0, 0, 0, 1] |
| 128 | + y = [.5, 1, 2, 1, 3, .5] |
| 129 | + |
| 130 | + fig, ax = plt.subplots() |
| 131 | + p1 = ax.plot(x, y) |
| 132 | + |
| 133 | + path = p1[0].get_path() |
| 134 | + transform = p1[0].get_transform() |
| 135 | + path = transform.transform_path(path) |
| 136 | + simplified = path.cleaned(simplify=True) |
| 137 | + simplified = transform.inverted().transform_path(simplified) |
| 138 | + |
| 139 | + assert_array_almost_equal([[ 0. , 0.5], |
| 140 | + [ 0. , 3. ], |
| 141 | + [ 1. , 0.5]], |
| 142 | + simplified.vertices[:-2, :]) |
| 143 | + |
| 144 | + # test no anti-parallel ending in middle |
| 145 | + x = [ 0, 0, 0, 0, 0, 1] |
| 146 | + y = [.5, 1, 2, 1, 1, .5] |
| 147 | + |
| 148 | + fig, ax = plt.subplots() |
| 149 | + p1 = ax.plot(x, y) |
| 150 | + |
| 151 | + path = p1[0].get_path() |
| 152 | + transform = p1[0].get_transform() |
| 153 | + path = transform.transform_path(path) |
|
57AE
154 | + simplified = path.cleaned(simplify=True) |
| 155 | + simplified = transform.inverted().transform_path(simplified) |
| 156 | + |
| 157 | + assert_array_almost_equal([[ 0. , 0.5], |
| 158 | + [ 0. , 2. ], |
| 159 | + [ 0. , 1. ], |
| 160 | + [ 1. , 0.5]], |
| 161 | + simplified.vertices[:-2, :]) |
| 162 | + |
65 | 163 | def test_sine_plus_noise():
|
66 | 164 | np.random.seed(0)
|
67 | 165 | x = (np.sin(np.linspace(0, np.pi * 2.0, 50000)) +
|
|
0 commit comments