Skip to content

Commit c6c62af

Browse files
tests for pruneOutsideBox
1 parent aee7a4f commit c6c62af

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

test/test_cleanfigure.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import pytest
2+
from tikzplotlib import cleanfigure
3+
import numpy as np
4+
from matplotlib import pyplot as plt
5+
6+
RC_PARAMS = {"figure.figsize": [5, 5], "figure.dpi": 220, "pgf.rcfonts": False}
7+
8+
9+
def test_pruneOutsideBox():
10+
"""test against matlab2tikz implementation
11+
12+
octave code to generate baseline results. Note that octave has indexing 1...N, whereas python has indexing 0...N-1.
13+
```octave
14+
x = linspace(1, 100, 20);
15+
y1 = linspace(1, 100, 20);
16+
17+
figure
18+
plot(x, y1)
19+
xlim([20, 80])
20+
ylim([20, 80])
21+
cleanfigure;
22+
```
23+
"""
24+
x = np.linspace(1, 100, 20)
25+
y = np.linspace(1, 100, 20)
26+
27+
with plt.rc_context(rc=RC_PARAMS):
28+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
29+
(l,) = ax.plot(x, y)
30+
ax.set_ylim([20, 80])
31+
ax.set_xlim([20, 80])
32+
cleanfigure.pruneOutsideBox(fig, ax, l)
33+
plt.show()
34+
35+
36+
def test_replaceDataWithNaN():
37+
"""test against matlab2tikz implementation
38+
39+
octave code to generate baseline results. Note that octave has indexing 1...N, whereas python has indexing 0...N-1.
40+
```octave
41+
x = linspace(1, 100, 20);
42+
y1 = linspace(1, 100, 20);
43+
44+
figure
45+
plot(x, y1)
46+
xlim([20, 80])
47+
ylim([20, 80])
48+
cleanfigure;
49+
```
50+
"""
51+
id_replace = np.array([0, 16])
52+
data = np.stack([np.linspace(1, 100, 20)] * 2, axis=1)
53+
54+
newdata = cleanfigure.replaceDataWithNaN(data, id_replace)
55+
assert newdata.shape == data.shape
56+
assert np.any(np.isnan(newdata))
57+
58+
59+
def test_removeData():
60+
"""test against matlab2tikz implementation
61+
62+
octave code to generate baseline results. Note that octave has indexing 1...N, whereas python has indexing 0...N-1.
63+
```octave
64+
x = linspace(1, 100, 20);
65+
y1 = linspace(1, 100, 20);
66+
67+
figure
68+
plot(x, y1)
69+
xlim([20, 80])
70+
ylim([20, 80])
71+
cleanfigure;
72+
```
73+
"""
74+
id_remove = np.array([1, 2, 3, 17, 18, 19])
75+
data = np.stack([np.linspace(1, 100, 20)] * 2, axis=1)
76+
77+
newdata = cleanfigure.removeData(data, id_remove)
78+
assert newdata.shape == (14, 2)
79+
80+
81+
def test_removeNaNs():
82+
"""test against matlab2tikz implementation
83+
84+
octave code to generate baseline results. Note that octave has indexing 1...N, whereas python has indexing 0...N-1.
85+
```octave
86+
x = linspace(1, 100, 20);
87+
y1 = linspace(1, 100, 20);
88+
89+
figure
90+
plot(x, y1)
91+
xlim([20, 80])
92+
ylim([20, 80])
93+
cleanfigure;
94+
```
95+
"""
96+
id_replace = np.array([0, 16])
97+
id_remove = np.array([1, 2, 3, 17, 18, 19])
98+
data = np.stack([np.linspace(1, 100, 20)] * 2, axis=1)
99+
newdata = cleanfigure.replaceDataWithNaN(data, id_replace)
100+
newdata = cleanfigure.removeData(newdata, id_remove)
101+
newdata = cleanfigure.removeNaNs(newdata)
102+
assert not np.any(np.isnan(newdata))
103+
assert newdata.shape == (12, 2)
104+
105+
106+
def test_isInBox():
107+
"""octave code to generate baseline results
108+
109+
110+
```octave
111+
x = 1:10;
112+
y = 1:10;
113+
data = [x', y'];
114+
xlim = [3, 7];
115+
ylim = [3, 7];
116+
mask = isInBox(data, xlim, ylim)
117+
```
118+
"""
119+
x = np.linspace(1, 100, 20)
120+
y = np.linspace(1, 100, 20)
121+
data = np.stack([x, y], axis=1)
122+
xLim = np.array([20, 80])
123+
yLim = np.array([20, 80])
124+
tol = 1.0e-10
125+
relaxedXLim = xLim + np.array([-tol, tol])
126+
relaxedYLim = yLim + np.array([-tol, tol])
127+
mask = cleanfigure.isInBox(data, relaxedXLim, relaxedYLim)
128+
assert int(np.sum(mask)) == 12
129+
130+
131+
def test_getVisualLimits():
132+
x = np.linspace(1, 100, 20)
133+
y = np.linspace(1, 100, 20)
134+
135+
with plt.rc_context(rc=RC_PARAMS):
136+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
137+
(l,) = ax.plot(x, y)
138+
ax.set_xlim([20, 80])
139+
ax.set_ylim([20, 80])
140+
xLim, yLim = cleanfigure.getVisualLimits(fig, ax)
141+
assert np.allclose(xLim, np.array([20, 80]))
142+
assert np.allclose(yLim, np.array([20, 80]))
143+
144+
145+
def test_interactiveManipulation():
146+
"""Create a plot, then remove some data, and see if the graph is updated
147+
"""
148+
x = np.linspace(1, 100, 20)
149+
y = np.linspace(1, 100, 20)
150+
id_remove = np.arange(0, 10)
151+
152+
with plt.rc_context(rc=RC_PARAMS):
153+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
154+
(l,) = ax.plot(x, y)
155+
xData, yData = cleanfigure.getVisualData(ax, l)
156+
data = np.stack([xData, yData], axis=1)
157+
data = cleanfigure.removeData(data, id_remove)
158+
l.set_xdata(data[:, 0])
159+
l.set_ydata(data[:, 1])
160+
plt.show()
161+

0 commit comments

Comments
 (0)