1
|
|
|
import os |
2
|
|
|
from contextlib import contextmanager |
3
|
|
|
|
4
|
|
|
import matplotlib as mpl |
5
|
|
|
import matplotlib.pyplot as plt |
6
|
|
|
import numpy as np |
7
|
|
|
from click.testing import CliRunner |
8
|
|
|
from matplotlib.colors import colorConverter |
9
|
|
|
|
10
|
|
|
from replotlib import Axes |
11
|
|
|
from replotlib.cli import main |
12
|
|
|
|
13
|
|
|
PATH = os.path.dirname(os.path.realpath(__file__)) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@contextmanager |
17
|
|
|
def ignored(*exceptions): |
18
|
|
|
try: |
19
|
|
|
yield |
20
|
|
|
except exceptions: |
21
|
|
|
pass |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def diff(file1, file2, ignore=None): |
25
|
|
|
if isinstance(ignore, str): |
26
|
|
|
ignore = [ignore] |
27
|
|
|
with open(file1) as f1, open(file2) as f2: |
28
|
|
|
for lineno, (line1, line2) in enumerate(zip(f1, f2), 1): |
29
|
|
|
ignoreline = False |
30
|
|
|
if ignore: |
31
|
|
|
for ig in ignore: |
32
|
|
|
if ig in line1 and ig in line2: |
33
|
|
|
ignoreline = True |
34
|
|
|
break |
35
|
|
|
if not ignoreline and line1 != line2: |
36
|
|
|
return lineno |
37
|
|
|
return None |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def test_json(): |
41
|
|
|
gen_orig() |
42
|
|
|
runner = CliRunner() |
43
|
|
|
runner.invoke(main, ['-s', PATH + '/test.eps', PATH + '/test.rplt']) |
44
|
|
|
assert diff(PATH + '/test.eps', PATH + '/test_orig.eps', |
45
|
|
|
['CreationDate', 'Title']) is None |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
# def test_hdf5(): |
49
|
|
|
# gen_orig('test_h5.rplt', 'hdf5') |
50
|
|
|
# runner = CliRunner() |
51
|
|
|
# runner.invoke(main, ['test.rplt']) |
52
|
|
|
# runner.invoke(main, ['-s', 'test.eps', 'test.rplt']) |
53
|
|
|
# assert diff('test.eps', 'test_orig.eps', 'CreationDate') is None |
54
|
|
|
# assert diff('test.eps', 'test_orig.eps', |
55
|
|
|
# ['CreationDate', 'Can Ignore more than one item']) is None |
56
|
|
|
|
57
|
|
|
def reset_mpl(): |
58
|
|
|
mpl.rcParams.update(mpl.rcParamsDefault) |
59
|
|
|
colorConverter.cache = {} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def test_main(): |
63
|
|
|
reset_mpl() |
64
|
|
|
runner = CliRunner() |
65
|
|
|
runner.invoke(main, [PATH + '/test.rplt']) |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def test_main_save(): |
69
|
|
|
reset_mpl() |
70
|
|
|
mpl.rcParams.update(mpl.rcParamsDefault) |
71
|
|
|
runner = CliRunner() |
72
|
|
|
runner.invoke(main, ['-s', PATH + '/test.eps', PATH + '/test.rplt']) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_main_style(): |
76
|
|
|
reset_mpl() |
77
|
|
|
mpl.rcParams.update(mpl.rcParamsDefault) |
78
|
|
|
runner = CliRunner() |
79
|
|
|
runner.invoke(main, ['--style_file', PATH + '/style.json', '-s', |
80
|
|
|
PATH + '/test_styl.eps', PATH + '/test.rplt']) |
81
|
|
|
|
82
|
|
|
def test_main_bb(): |
83
|
|
|
reset_mpl() |
84
|
|
|
mpl.rcParams.update(mpl.rcParamsDefault) |
85
|
|
|
runner = CliRunner() |
86
|
|
|
runner.invoke(main, ['--bb', '-s', PATH + '/test_bb.eps', |
87
|
|
|
PATH + '/test.rplt']) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def gen_orig(test_file= PATH + '/test.rplt', file_type='json'): |
91
|
|
|
style = {'nice_color': {'color': 'green'}} |
92
|
|
|
with ignored(OSError): |
93
|
|
|
os.remove(test_file) |
94
|
|
|
print(test_file, file_type) |
95
|
|
|
plt.rcParams['lines.linewidth'] = 3 |
96
|
|
|
plt.rcParams['lines.color'] = 'k' |
97
|
|
|
ax = Axes(test_file, file_type=file_type, style=style) |
98
|
|
|
ax.rcParams = {'lines.linewidth': 3, |
99
|
|
|
'lines.color': 'k'} |
100
|
|
|
style = {'strong_color': {'color': 'blue'}, |
101
|
|
|
'errorbar': {'fmt': 'o'}} |
102
|
|
|
ax.style = style |
103
|
|
|
x = np.linspace(0, 1) |
104
|
|
|
ax.plot(x, x**2, color='c', label=r'$x^2$') |
105
|
|
|
ax.errorbar(x, x**2, x**2*0.1) |
106
|
|
|
ax.plot(x, x**3, style='strong_color') |
107
|
|
|
ax.plot(x, x**4, style='nice_color') |
108
|
|
|
ax.set_xlabel('x') |
109
|
|
|
ax.set_ylabel('y') |
110
|
|
|
ax.legend() |
111
|
|
|
plt.savefig(PATH + '/test_orig.eps') |
112
|
|
|
|