12
12
import tempfile
13
13
14
14
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'
62
23
)
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
- )
91
24
25
+ # test other height specs
26
+ matplotlib2tikz .save (
27
+ tikz_file + '.height' ,
28
+ figureheight = '7.5cm' ,
29
+ show_info = True ,
30
+ strict = True ,
31
+ )
92
32
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 ):
98
101
# 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__ )))
100
103
104
+ print ('Output file: %s' % self .png_file )
105
+ print ('computed pHash: %s' % self .phash )
101
106
# 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
+ # )
111
114
print ('pdflatex output:' )
112
- print (tex_out .decode ('utf-8' ))
115
+ print (self . tex_out .decode ('utf-8' ))
113
116
114
117
print ('pdftoppm output:' )
115
- print (ptp_out .decode ('utf-8' ))
118
+ print (self . ptp_out .decode ('utf-8' ))
116
119
117
120
if 'DISPLAY' not in os .environ :
118
121
# upload to chunk.io if we're on a headless client
119
122
out = subprocess .check_output (
120
- ['curl' , '-sT' , mpl_reference , 'chunk.io' ],
123
+ ['curl' , '-sT' , self . mpl_reference , 'chunk.io' ],
121
124
stderr = subprocess .STDOUT
122
125
)
123
126
print (
@@ -126,25 +129,23 @@ def assert_phash(fig, reference_phash):
126
129
)
127
130
128
131
out = subprocess .check_output (
129
- ['curl' , '-sT' , tikz_file , 'chunk.io' ],
132
+ ['curl' , '-sT' , self . tikz_file , 'chunk.io' ],
130
133
stderr = subprocess .STDOUT
131
134
)
132
135
print ('Uploaded TikZ file to %s' % out .decode ('utf-8' ))
133
136
134
137
out = subprocess .check_output (
135
- ['curl' , '-sT' , pdf_file , 'chunk.io' ],
138
+ ['curl' , '-sT' , self . pdf_file , 'chunk.io' ],
136
139
stderr = subprocess .STDOUT
137
140
)
138
141
print ('Uploaded output PDF file to %s' % out .decode ('utf-8' ))
139
142
140
143
out = subprocess .check_output (
141
- ['curl' , '-sT' , png_file , 'chunk.io' ],
144
+ ['curl' , '-sT' , self . png_file , 'chunk.io' ],
142
145
stderr = subprocess .STDOUT
143
146
)
144
147
print ('Uploaded output PNG file to %s' % out .decode ('utf-8' ))
145
-
146
- assert reference_phash == phash
147
- return
148
+ return
148
149
149
150
150
151
def print_tree (obj , indent = '' ):
0 commit comments