Skip to content

Commit e899cac

Browse files
committed
better error reporting
1 parent b831389 commit e899cac

File tree

5 files changed

+121
-109
lines changed

5 files changed

+121
-109
lines changed

test/helpers.py

Lines changed: 100 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -12,112 +12,115 @@
1212
import tempfile
1313

1414

15-
def compute_phash(fig):
16-
# convert to tikz file
17-
_, tmp_base = tempfile.mkstemp()
18-
tikz_file = tmp_base + '_tikz.tex'
19-
matplotlib2tikz.save(
20-
tikz_file,
21-
figurewidth='7.5cm'
22-
)
23-
24-
# test other height specs
25-
matplotlib2tikz.save(
26-
tikz_file + '.height',
27-
figureheight='7.5cm',
28-
show_info=True,
29-
strict=True,
30-
)
31-
32-
# save reference figure
33-
mpl_reference = tmp_base + '_reference.pdf'
34-
plt.savefig(mpl_reference)
35-
36-
# close figure
37-
plt.close(fig)
38-
39-
# create a latex wrapper for the tikz
40-
wrapper = '''\\documentclass{standalone}
41-
\\usepackage[utf8]{inputenc}
42-
\\usepackage{pgfplots}
43-
\\usepgfplotslibrary{groupplots}
44-
\\usetikzlibrary{shapes.arrows}
45-
\\pgfplotsset{compat=newest}
46-
\\begin{document}
47-
\\input{%s}
48-
\\end{document}''' % tikz_file
49-
tex_file = tmp_base + '.tex'
50-
with open(tex_file, 'w') as f:
51-
f.write(wrapper)
52-
53-
# change into the directory of the TeX file
54-
os.chdir(os.path.dirname(tex_file))
55-
56-
# compile the output to pdf
57-
try:
58-
tex_out = subprocess.check_output(
59-
# use pdflatex for now until travis features a more modern lualatex
60-
['pdflatex', '--interaction=nonstopmode', tex_file],
61-
stderr=subprocess.STDOUT
15+
class Phash(object):
16+
def __init__(self, fig):
17+
# convert to tikz file
18+
_, tmp_base = tempfile.mkstemp()
19+
tikz_file = tmp_base + '_tikz.tex'
20+
matplotlib2tikz.save(
21+
tikz_file,
22+
figurewidth='7.5cm'
6223
)
63-
except subprocess.CalledProcessError as e:
64-
print('Command output:')
65-
print('=' * 70)
66-
print(e.output)
67-
print('=' * 70)
68-
if 'DISPLAY' not in os.environ:
69-
cmd = ['curl', '-sT', tikz_file, 'chunk.io']
70-
out = subprocess.check_output(
71-
cmd,
72-
stderr=subprocess.STDOUT
73-
)
74-
print('Uploaded TikZ file to %s' % out.decode('utf-8'))
75-
raise
76-
77-
pdf_file = tmp_base + '.pdf'
78-
79-
# Convert PDF to PNG.
80-
ptp_out = subprocess.check_output(
81-
['pdftoppm', '-rx', '600', '-ry', '600', '-png', pdf_file, tmp_base],
82-
stderr=subprocess.STDOUT
83-
)
84-
png_file = tmp_base + '-1.png'
85-
86-
# compute the phash of the PNG
87-
return (
88-
imagehash.phash(Image.open(png_file)).__str__(), png_file, pdf_file,
89-
tex_out, ptp_out, mpl_reference, tikz_file
90-
)
9124

25+
# test other height specs
26+
matplotlib2tikz.save(
27+
tikz_file + '.height',
28+
figureheight='7.5cm',
29+
show_info=True,
30+
strict=True,
31+
)
9232

93-
def assert_phash(fig, reference_phash):
94-
phash, png_file, pdf_file, tex_out, ptp_out, mpl_reference, tikz_file =\
95-
compute_phash(fig)
96-
97-
if reference_phash != phash:
33+
# save reference figure
34+
mpl_reference = tmp_base + '_reference.pdf'
35+
plt.savefig(mpl_reference)
36+
37+
# close figure
38+
plt.close(fig)
39+
40+
# create a latex wrapper for the tikz
41+
wrapper = '''\\documentclass{standalone}
42+
\\usepackage[utf8]{inputenc}
43+
\\usepackage{pgfplots}
44+
\\usepgfplotslibrary{groupplots}
45+
\\usetikzlibrary{shapes.arrows}
46+
\\pgfplotsset{compat=newest}
47+
\\begin{document}
48+
\\input{%s}
49+
\\end{document}''' % tikz_file
50+
tex_file = tmp_base + '.tex'
51+
with open(tex_file, 'w') as f:
52+
f.write(wrapper)
53+
54+
# change into the directory of the TeX file
55+
os.chdir(os.path.dirname(tex_file))
56+
57+
# compile the output to pdf
58+
try:
59+
tex_out = subprocess.check_output(
60+
# use pdflatex for now until travis features a more modern
61+
# lualatex
62+
['pdflatex', '--interaction=nonstopmode', tex_file],
63+
stderr=subprocess.STDOUT
64+
)
65+
except subprocess.CalledProcessError as e:
66+
print('Command output:')
67+
print('=' * 70)
68+
print(e.output)
69+
print('=' * 70)
70+
if 'DISPLAY' not in os.environ:
71+
cmd = ['curl', '-sT', tikz_file, 'chunk.io']
72+
out = subprocess.check_output(
73+
cmd,
74+
stderr=subprocess.STDOUT
75+
)
76+
print('Uploaded TikZ file to %s' % out.decode('utf-8'))
77+
raise
78+
79+
pdf_file = tmp_base + '.pdf'
80+
81+
# Convert PDF to PNG.
82+
ptp_out = subprocess.check_output(
83+
[
84+
'pdftoppm', '-rx', '600', '-ry', '600', '-png',
85+
pdf_file, tmp_base
86+
],
87+
stderr=subprocess.STDOUT
88+
)
89+
png_file = tmp_base + '-1.png'
90+
91+
self.phash = imagehash.phash(Image.open(png_file)).__str__()
92+
self.png_file = png_file
93+
self.pdf_file = pdf_file
94+
self.tex_out = tex_out
95+
self.ptp_out = ptp_out
96+
self.mpl_reference = mpl_reference
97+
self.tikz_file = tikz_file
98+
return
99+
100+
def get_details(self):
98101
# Copy pdf_file in test directory
99-
shutil.copy(pdf_file, os.path.dirname(os.path.abspath(__file__)))
102+
shutil.copy(self.pdf_file, os.path.dirname(os.path.abspath(__file__)))
100103

104+
print('Output file: %s' % self.png_file)
105+
print('computed pHash: %s' % self.phash)
101106
# Compute the Hamming distance between the two 64-bit numbers
102-
hamming_dist = \
103-
bin(int(phash, 16) ^ int(reference_phash, 16)).count('1')
104-
print('Output file: %s' % png_file)
105-
print('computed pHash: %s' % phash)
106-
print('reference pHash: %s' % reference_phash)
107-
print(
108-
'Hamming distance: %s (out of %s)' %
109-
(hamming_dist, 4 * len(phash))
110-
)
107+
# hamming_dist = \
108+
# bin(int(phash, 16) ^ int(reference_phash, 16)).count('1')
109+
# print('reference pHash: %s' % reference_phash)
110+
# print(
111+
# 'Hamming distance: %s (out of %s)' %
112+
# (hamming_dist, 4 * len(phash))
113+
# )
111114
print('pdflatex output:')
112-
print(tex_out.decode('utf-8'))
115+
print(self.tex_out.decode('utf-8'))
113116

114117
print('pdftoppm output:')
115-
print(ptp_out.decode('utf-8'))
118+
print(self.ptp_out.decode('utf-8'))
116119

117120
if 'DISPLAY' not in os.environ:
118121
# upload to chunk.io if we're on a headless client
119122
out = subprocess.check_output(
120-
['curl', '-sT', mpl_reference, 'chunk.io'],
123+
['curl', '-sT', self.mpl_reference, 'chunk.io'],
121124
stderr=subprocess.STDOUT
122125
)
123126
print(
@@ -126,25 +129,23 @@ def assert_phash(fig, reference_phash):
126129
)
127130

128131
out = subprocess.check_output(
129-
['curl', '-sT', tikz_file, 'chunk.io'],
132+
['curl', '-sT', self.tikz_file, 'chunk.io'],
130133
stderr=subprocess.STDOUT
131134
)
132135
print('Uploaded TikZ file to %s' % out.decode('utf-8'))
133136

134137
out = subprocess.check_output(
135-
['curl', '-sT', pdf_file, 'chunk.io'],
138+
['curl', '-sT', self.pdf_file, 'chunk.io'],
136139
stderr=subprocess.STDOUT
137140
)
138141
print('Uploaded output PDF file to %s' % out.decode('utf-8'))
139142

140143
out = subprocess.check_output(
141-
['curl', '-sT', png_file, 'chunk.io'],
144+
['curl', '-sT', self.png_file, 'chunk.io'],
142145
stderr=subprocess.STDOUT
143146
)
144147
print('Uploaded output PNG file to %s' % out.decode('utf-8'))
145-
146-
assert reference_phash == phash
147-
return
148+
return
148149

149150

150151
def print_tree(obj, indent=''):

test/test_annotate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
from helpers import assert_phash
3+
from helpers import Phash
44

55
import matplotlib.pyplot as plt
66
import numpy as np
@@ -37,4 +37,5 @@ def plot():
3737

3838

3939
def test():
40-
assert_phash(plot(), 'ab8a79a1549654be')
40+
phash = Phash(plot())
41+
assert phash.phash == 'ab8a79a1549654bg', phash.get_details()

test/test_barchart_errorbars.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
at the correct z-order to be sucessful.
66
77
"""
8-
from helpers import assert_phash
8+
from helpers import Phash
99

1010

1111
def plot():
@@ -27,12 +27,19 @@ def plot():
2727

2828
errBarStyle = dict(ecolor='black', lw=5, capsize=8, capthick=5)
2929

30-
ax.bar(x-w, y1, w, color='b', yerr=y1err, align='center', error_kw=errBarStyle)
31-
ax.bar(x, y2, w, color='g', yerr=y2err, align='center', error_kw=errBarStyle)
32-
ax.bar(x+w, y3, w, color='r', yerr=y3err, align='center', error_kw=errBarStyle)
30+
ax.bar(
31+
x-w, y1, w, color='b', yerr=y1err, align='center', error_kw=errBarStyle
32+
)
33+
ax.bar(
34+
x, y2, w, color='g', yerr=y2err, align='center', error_kw=errBarStyle
35+
)
36+
ax.bar(
37+
x+w, y3, w, color='r', yerr=y3err, align='center', error_kw=errBarStyle
38+
)
3339

3440
return fig
3541

3642

3743
def test():
38-
assert_phash(plot(), '5f09a9e6b1728746')
44+
phash = Phash(plot())
45+
assert phash.phash == '5f09a9e6b1728746', phash.get_details()

test/test_barchart_legend.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
manually added.
1111
1212
"""
13-
desc = 'Bar Chart'
14-
phash = '5f19a966937285cc'
13+
from helpers import Phash
1514

1615

1716
def plot():
@@ -35,3 +34,7 @@ def plot():
3534

3635
return fig
3736

37+
38+
def test():
39+
phash = Phash(plot())
40+
assert phash.phash == '5f19a966937285cc', phash.get_details()

test/test_basic_sin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def plot():
2525

2626

2727
def test():
28-
assert_phash(plot(), '1f36e5c621c1e7c1')
28+
assert_phash(plot(), '1f86e1f521c6e5c1')

0 commit comments

Comments
 (0)