forked from tomgoldstein/loss-landscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h52vtp.py
259 lines (228 loc) · 11.3 KB
/
h52vtp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Convert h5 files to vtp files in VTK XML format that can be opened by ParaView.
The data type of the vtp file is "vtkPolyData", each PolyData piece specifies a set
of points and cells independently from the other pieces. The points are described
explicitly by the Points element. The cells are described explicitly by the Verts,
Lines, Strips, and Polys elements.
<VTKFile type="PolyData" ...>
<PolyData>
<Piece NumberOfPoints="#" NumberOfVerts="#" NumberOfLines="#"
NumberOfStrips="#" NumberOfPolys="#">
<PointData>...</PointData>
<CellData>...</CellData>
<Points>...</Points>
<Verts>...</Verts>
<Lines>...</Lines>
<Strips>...</Strips>
<Polys>...</Polys>
</Piece>
</PolyData>
</VTKFile>
"""
import math
import argparse
import h5py
import numpy as np
from scipy import interpolate
def h5_to_vtp(surf_file, surf_name='train_loss', log=False, zmax=-1, interp=-1):
#set this to True to generate points
show_points = False
#set this to True to generate polygons
show_polys = True
f = h5py.File(surf_file,'r')
[xcoordinates, ycoordinates] = np.meshgrid(f['xcoordinates'][:], f['ycoordinates'][:][:])
vals = f[surf_name]
x_array = xcoordinates[:].ravel()
y_array = ycoordinates[:].ravel()
z_array = vals[:].ravel()
# Interpolate the resolution up to the desired amount
if interp > 0:
m = interpolate.interp2d(xcoordinates[0,:], ycoordinates[:,0], vals, kind='cubic')
x_array = np.linspace(min(x_array), max(x_array), interp)
y_array = np.linspace(min(y_array), max(y_array), interp)
z_array = m(x_array, y_array).ravel()
x_array, y_array = np.meshgrid(x_array, y_array)
x_array = x_array.ravel()
y_array = y_array.ravel()
vtp_file = surf_file + "_" + surf_name
if zmax > 0:
z_array[z_array > zmax] = zmax
vtp_file += "_zmax=" + str(zmax)
if log:
z_array = np.log(z_array + 0.1)
vtp_file += "_log"
vtp_file += ".vtp"
print("Here's your output file:{}".format(vtp_file))
number_points = len(z_array)
print("number_points = {} points".format(number_points))
matrix_size = int(math.sqrt(number_points))
print("matrix_size = {} x {}".format(matrix_size, matrix_size))
poly_size = matrix_size - 1
print("poly_size = {} x {}".format(poly_size, poly_size))
number_polys = poly_size * poly_size
print("number_polys = {}".format(number_polys))
min_value_array = [min(x_array), min(y_array), min(z_array)]
max_value_array = [max(x_array), max(y_array), max(z_array)]
min_value = min(min_value_array)
max_value = max(max_value_array)
averaged_z_value_array = []
poly_count = 0
for column_count in range(poly_size):
stride_value = column_count * matrix_size
for row_count in range(poly_size):
temp_index = stride_value + row_count
averaged_z_value = (z_array[temp_index] + z_array[temp_index + 1] +
z_array[temp_index + matrix_size] +
z_array[temp_index + matrix_size + 1]) / 4.0
averaged_z_value_array.append(averaged_z_value)
poly_count += 1
avg_min_value = min(averaged_z_value_array)
avg_max_value = max(averaged_z_value_array)
output_file = open(vtp_file, 'w')
output_file.write('<VTKFile type="PolyData" version="1.0" byte_order="LittleEndian" header_type="UInt64">\n')
output_file.write(' <PolyData>\n')
if (show_points and show_polys):
output_file.write(' <Piece NumberOfPoints="{}" NumberOfVerts="{}" NumberOfLines="0" NumberOfStrips="0" NumberOfPolys="{}">\n'.format(number_points, number_points, number_polys))
elif (show_polys):
output_file.write(' <Piece NumberOfPoints="{}" NumberOfVerts="0" NumberOfLines="0" NumberOfStrips="0" NumberOfPolys="{}">\n'.format(number_points, number_polys))
else:
output_file.write(' <Piece NumberOfPoints="{}" NumberOfVerts="{}" NumberOfLines="0" NumberOfStrips="0" NumberOfPolys="">\n'.format(number_points, number_points))
# <PointData>
output_file.write(' <PointData>\n')
output_file.write(' <DataArray type="Float32" Name="zvalue" NumberOfComponents="1" format="ascii" RangeMin="{}" RangeMax="{}">\n'.format(min_value_array[2], max_value_array[2]))
for vertexcount in range(number_points):
if (vertexcount % 6) is 0:
output_file.write(' ')
output_file.write('{}'.format(z_array[vertexcount]))
if (vertexcount % 6) is 5:
output_file.write('\n')
else:
output_file.write(' ')
if (vertexcount % 6) is not 5:
output_file.write('\n')
output_file.write(' </DataArray>\n')
output_file.write(' </PointData>\n')
# <CellData>
output_file.write(' <CellData>\n')
if (show_polys and not show_points):
output_file.write(' <DataArray type="Float32" Name="averaged zvalue" NumberOfComponents="1" format="ascii" RangeMin="{}" RangeMax="{}">\n'.format(avg_min_value, avg_max_value))
for vertexcount in range(number_polys):
if (vertexcount % 6) is 0:
output_file.write(' ')
output_file.write('{}'.format(averaged_z_value_array[vertexcount]))
if (vertexcount % 6) is 5:
output_file.write('\n')
else:
output_file.write(' ')
if (vertexcount % 6) is not 5:
output_file.write('\n')
output_file.write(' </DataArray>\n')
output_file.write(' </CellData>\n')
# <Points>
output_file.write(' <Points>\n')
output_file.write(' <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="ascii" RangeMin="{}" RangeMax="{}">\n'.format(min_value, max_value))
for vertexcount in range(number_points):
if (vertexcount % 2) is 0:
output_file.write(' ')
output_file.write('{} {} {}'.format(x_array[vertexcount], y_array[vertexcount], z_array[vertexcount]))
if (vertexcount % 2) is 1:
output_file.write('\n')
else:
output_file.write(' ')
if (vertexcount % 2) is not 1:
output_file.write('\n')
output_file.write(' </DataArray>\n')
output_file.write(' </Points>\n')
# <Verts>
output_file.write(' <Verts>\n')
output_file.write(' <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="0" RangeMax="{}">\n'.format(number_points - 1))
if (show_points):
for vertexcount in range(number_points):
if (vertexcount % 6) is 0:
output_file.write(' ')
output_file.write('{}'.format(vertexcount))
if (vertexcount % 6) is 5:
output_file.write('\n')
else:
output_file.write(' ')
if (vertexcount % 6) is not 5:
output_file.write('\n')
output_file.write(' </DataArray>\n')
output_file.write(' <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1" RangeMax="{}">\n'.format(number_points))
if (show_points):
for vertexcount in range(number_points):
if (vertexcount % 6) is 0:
output_file.write(' ')
output_file.write('{}'.format(vertexcount + 1))
if (vertexcount % 6) is 5:
output_file.write('\n')
else:
output_file.write(' ')
if (vertexcount % 6) is not 5:
output_file.write('\n')
output_file.write(' </DataArray>\n')
output_file.write(' </Verts>\n')
# <Lines>
output_file.write(' <Lines>\n')
output_file.write(' <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="0" RangeMax="{}">\n'.format(number_polys - 1))
output_file.write(' </DataArray>\n')
output_file.write(' <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1" RangeMax="{}">\n'.format(number_polys))
output_file.write(' </DataArray>\n')
output_file.write(' </Lines>\n')
# <Strips>
output_file.write(' <Strips>\n')
output_file.write(' <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="0" RangeMax="{}">\n'.format(number_polys - 1))
output_file.write(' </DataArray>\n')
output_file.write(' <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1" RangeMax="{}">\n'.format(number_polys))
output_file.write(' </DataArray>\n')
output_file.write(' </Strips>\n')
# <Polys>
output_file.write(' <Polys>\n')
output_file.write(' <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="0" RangeMax="{}">\n'.format(number_polys - 1))
if (show_polys):
polycount = 0
for column_count in range(poly_size):
stride_value = column_count * matrix_size
for row_count in range(poly_size):
temp_index = stride_value + row_count
if (polycount % 2) is 0:
output_file.write(' ')
output_file.write('{} {} {} {}'.format(temp_index, (temp_index + 1), (temp_index + matrix_size + 1), (temp_index + matrix_size)))
if (polycount % 2) is 1:
output_file.write('\n')
else:
output_file.write(' ')
polycount += 1
if (polycount % 2) is 1:
output_file.write('\n')
output_file.write(' </DataArray>\n')
output_file.write(' <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1" RangeMax="{}">\n'.format(number_polys))
if (show_polys):
for polycount in range(number_polys):
if (polycount % 6) is 0:
output_file.write(' ')
output_file.write('{}'.format((polycount + 1) * 4))
if (polycount % 6) is 5:
output_file.write('\n')
else:
output_file.write(' ')
if (polycount % 6) is not 5:
output_file.write('\n')
output_file.write(' </DataArray>\n')
output_file.write(' </Polys>\n')
output_file.write(' </Piece>\n')
output_file.write(' </PolyData>\n')
output_file.write('</VTKFile>\n')
output_file.write('')
output_file.close()
print("Done with file:{}".format(vtp_file))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert h5 file to XML-based VTK file that can be opened with ParaView')
parser.add_argument('--surf_file', '-f', default='', help='The h5 file that contains surface values')
parser.add_argument('--surf_name', default='train_loss',
help='The type of surface to plot: train_loss | test_loss | train_acc | test_acc ')
parser.add_argument('--zmax', default=-1, type=float, help='Maximum z value to map')
parser.add_argument('--interp', default=-1, type=int, help='Interpolate the surface to this resolution (1000 recommended)')
parser.add_argument('--log', action='store_true', default=False, help='log scale')
args = parser.parse_args()
h5_to_vtp(args.surf_file, args.surf_name, log=args.log, zmax=args.zmax, interp=args.interp)