VTKio 0.1.0.dev2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,393 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ Module for writing data files in VTK's XML and VTKHDF based formats.
4
+
5
+ Supports ASCII, Base64 and Appended Raw encoding of data.
6
+
7
+ This module contains the following functions:
8
+
9
+ Functions
10
+ ---------
11
+ vtk_writer()
12
+ Generic XML writer class for VTK files.
13
+ write_vti()
14
+ Function for writing VTK `ImageData` datasets to VTK files.
15
+ write_vtr()
16
+ Function for writing VTK `RectilinearData` datasets to VTK files.
17
+ write_vts()
18
+ Function for writing VTK `StructuredData` datasets to VTK files.
19
+ write_vtp()
20
+ Function for writing VTK `PolyData` datasets to VTK files.
21
+ write_vtu()
22
+ Function for writing VTK `UnstructuredData` datasets to VTK files.
23
+ vtk_multiblock_writer()
24
+ Function for writing VTK multi-block files.
25
+
26
+ """
27
+
28
+
29
+ __author__ = 'J.P. Morrissey'
30
+ __copyright__ = 'Copyright 2022-2025'
31
+ __maintainer__ = 'J.P. Morrissey'
32
+ __email__ = 'morrissey.jp@gmail.com'
33
+ __status__ = 'Development'
34
+
35
+
36
+ # Standard Library
37
+
38
+
39
+ # Imports
40
+ import numpy as np
41
+
42
+ # Local Sources
43
+ from .vtkhdf import VTKHDFImageDataWriter, VTKHDFStructuredGridWriter, VTKHDFRectilinearGridWriter, \
44
+ VTKHDFUnstructuredGridWriter, VTKHDFPolyDataWriter
45
+ from .xml_writer import XMLImageDataWriter, XMLPolyDataWriter, XMLRectilinearGridWriter, XMLStructuredGridWriter, XMLUnstructuredGridWriter
46
+
47
+ __all__ = ['write_vti', 'write_vtr', 'write_vts', 'write_vtu', 'write_vtp']
48
+
49
+
50
+ def write_vti(filepath, whole_extent: object, piece_extent: object=None, spacing: object=(1, 1, 1), origin: object=(0, 0, 0),
51
+ direction: object = (1, 0, 0, 0, 1, 0, 0, 0, 1), point_data: object = None, cell_data: object = None,
52
+ field_data: object = None, additional_metadata=None, encoding='ascii', ascii_precision=16,
53
+ ascii_ncolumns=6, add_declaration=True, appended_encoding='base64', version='1.0', file_format='xml'):
54
+ """
55
+ Write data to a .vti (VTK Image Data - uniformly spaced 3D grid) file.
56
+
57
+ This function supports customization of dataset attributes such as origin, spacing,
58
+ and direction, as well as the inclusion of point, cell, and field data.
59
+ It handles various encodings and enables fine control over ASCII format precision and structure.
60
+
61
+ Parameters
62
+ ----------
63
+ filepath : str
64
+ Path of the .vti file where the data will be written.
65
+ whole_extent : object
66
+ The full extent of the dataset described in terms of its bounds.
67
+ piece_extent : object
68
+ The extent of the individual piece of the dataset being written.
69
+ spacing : object, optional
70
+ Spacing between points in the dataset. Default is (1, 1, 1).
71
+ origin : object, optional
72
+ Coordinates of the origin of the dataset. Default is (0, 0, 0).
73
+ direction : object, optional
74
+ Direction cosine matrix specifying the orientation of the dataset.
75
+ Default is (1, 0, 0, 0, 1, 0, 0, 0, 1).
76
+ point_data : object, optional
77
+ Data assigned to the points of the dataset.
78
+ Default is None.
79
+ cell_data : object, optional
80
+ Data assigned to the cells of the dataset.
81
+ Default is None.
82
+ field_data : object, optional
83
+ General field data that is not associated with points or cells.
84
+ Default is None.
85
+ encoding : str, optional
86
+ File encoding type. Options include 'ascii', 'binary', and 'appended'.
87
+ Default is 'ascii'.
88
+ ascii_precision : int, optional
89
+ Precision for floating-point numbers in ASCII format. Default is 16.
90
+ ascii_ncolumns : int, optional
91
+ Number of columns for writing in ASCII format. Default is 6.
92
+ add_declaration : bool, optional
93
+ Whether to add an XML declaration in the .vti file. Default is True.
94
+ appended_encoding : str, optional
95
+ Encoding type for appended data in the file. Default is 'base64'.
96
+ version : str, optional
97
+ Version of the VTK XML format to use. Default is '1.0'.
98
+
99
+ """
100
+ if file_format.lower() == 'xml':
101
+
102
+ file = XMLImageDataWriter(filepath, encoding=encoding, origin=origin, spacing=spacing, whole_extent=whole_extent,
103
+ piece_extent=piece_extent, direction=direction, cell_data=cell_data, field_data=field_data,
104
+ point_data=point_data, appended_encoding=appended_encoding, ascii_precision=ascii_precision,
105
+ ascii_ncolumns=ascii_ncolumns, declaration=add_declaration, version=version)
106
+
107
+ file.write_xml_file()
108
+
109
+ elif file_format.lower() in ['vtkhdf', 'hdf5', 'hdf', 'vtk hdf']:
110
+ file = VTKHDFImageDataWriter(filepath, whole_extent, origin, spacing, direction=direction, cell_data=cell_data,
111
+ point_data=point_data, field_data=field_data,
112
+ additional_metadata=additional_metadata)
113
+ file.write_vtkhdf_file()
114
+
115
+ else:
116
+ raise ValueError(f"Unsupported file format: {file_format}. Supported formats are 'xml' and 'vtkhdf'.")
117
+
118
+
119
+ def write_vtr(filepath, x, y, z, whole_extent=None, piece_extent=None, point_data=None, cell_data=None, field_data=None,
120
+ additional_metadata=None, encoding='ascii', ascii_precision=16,
121
+ ascii_ncolumns=6, add_declaration=True, appended_encoding='base64', version='1.0',file_format='xml'):
122
+ """
123
+ Write a .vtr file (VTK RectilinearGrid format).
124
+
125
+ The function manages the entire file writing process and it handles various encodings and enables fine control
126
+ over ASCII format precision and structure.
127
+
128
+
129
+ Parameters
130
+ ----------
131
+ filepath : str
132
+ The file path where the .vtr file will be saved.
133
+ x : array_like
134
+ A 1D array representing the x-coordinates of the rectilinear grid.
135
+ y : array_like
136
+ A 1D array representing the y-coordinates of the rectilinear grid.
137
+ z : array_like
138
+ A 1D array representing the z-coordinates of the rectilinear grid.
139
+ whole_extent : tuple of int, optional
140
+ A tuple that specifies the whole extent of the grid as (x_min, x_max, y_min, y_max, z_min, z_max).
141
+ piece_extent : tuple of int, optional
142
+ A tuple that specifies the extent of this piece of the grid as (x_min, x_max, y_min, y_max, z_min, z_max).
143
+ point_data : dict, optional
144
+ A dictionary containing point data arrays to be written as part of the PointData section.
145
+ cell_data : dict, optional
146
+ A dictionary containing cell data arrays to be written as part of the CellData section.
147
+ field_data : dict, optional
148
+ A dictionary containing field data arrays to be added to the FieldData section.
149
+ encoding : {'ascii', 'binary', 'appended'}, optional
150
+ Specifies how data is encoded in the file. Default is 'ascii'.
151
+ ascii_precision : int, optional
152
+ The precision for floating point numbers when using 'ascii' encoding. Default is 16.
153
+ ascii_ncolumns : int, optional
154
+ The number of columns per row for data arrays in 'ascii' encoding. Default is 6.
155
+ add_declaration : bool, optional
156
+ If True, writes an XML declaration header at the beginning of the file. Default is True.
157
+ appended_encoding : {'base64', 'raw'}, optional
158
+ Specifies the secondary encoding scheme for the appended data sections when using 'appended' encoding.
159
+ Default is 'base64'.
160
+ version : str, optional
161
+ Version of the VTK XML format to use. Default is '1.0'.
162
+
163
+ """
164
+ if file_format.lower() == 'xml':
165
+
166
+ file = XMLRectilinearGridWriter(filepath, x, y, z, cell_data=cell_data, field_data=field_data, point_data=point_data,
167
+ whole_extent=whole_extent, piece_extent=piece_extent, encoding=encoding,
168
+ appended_encoding=appended_encoding, version=version, ascii_precision=ascii_precision,
169
+ ascii_ncolumns=ascii_ncolumns, declaration=add_declaration)
170
+
171
+ file.write_xml_file()
172
+
173
+ elif file_format.lower() in ['vtkhdf', 'hdf5', 'hdf', 'vtk hdf']:
174
+ file = VTKHDFRectilinearGridWriter(filepath, x, y, z, point_data=point_data, cell_data=cell_data, field_data=field_data,
175
+ additional_metadata=additional_metadata)
176
+ file.write_vtkhdf_file()
177
+
178
+ else:
179
+ raise ValueError(f"Unsupported file format: {file_format}. Supported formats are 'xml' and 'vtkhdf'.")
180
+
181
+
182
+ def write_vts(filepath, points, whole_extent=None, piece_extent=None, num_cells=None, point_data=None,
183
+ cell_data=None, field_data=None, additional_metadata=None, encoding='ascii', ascii_precision=16,
184
+ ascii_ncolumns=6, add_declaration=True, appended_encoding='base64', version='1.0', file_format='xml'):
185
+ """
186
+ Write the given data to a .vts (VTK Structured Grid XML) file format.
187
+
188
+ This function generates a VTS file given the points that define the grid, along
189
+ with optional data arrays for points, cells, and fields.
190
+
191
+ The whole extent defines the global extents of the entire data, while the piece extent specifies the sub-region
192
+ of data applicable for the generated VTS file.
193
+
194
+ Parameters
195
+ ----------
196
+ filepath : str
197
+ Path to save the output VTS file.
198
+ points : array_like
199
+ Array of points to define the spatial grid.
200
+ whole_extent : array_like, optional
201
+ Defines the extent of the entire dataset. Defaults to None.
202
+ piece_extent : array_like, optional
203
+ Defines the piece extent corresponding to the data being saved. Defaults to None.
204
+ num_cells : array_like, optional
205
+ Number of cells in each dimension. If provided, it overrides `whole_extent`
206
+ and `piece_extent`. Defaults to None.
207
+ point_data : dict, optional
208
+ Dictionary containing data arrays to associate with each grid point. Defaults to None.
209
+ cell_data : dict, optional
210
+ Dictionary containing data arrays to associate with each cell in the grid. Defaults to None.
211
+ field_data : dict, optional
212
+ Dataset-wide field data to include in the file. Defaults to None.
213
+ encoding : {'ascii', 'binary', 'appended'}, optional
214
+ Specifies the data encoding format. Defaults to 'ascii'.
215
+ ascii_precision : int, optional
216
+ Number of decimal places for floating-point numbers in ASCII encoding.
217
+ Defaults to 16.
218
+ ascii_ncolumns : int, optional
219
+ Maximum number of columns when writing ASCII data. Defaults to 6.
220
+ add_declaration : bool, optional
221
+ Whether to include an XML declaration at the start of the file. Defaults to True.
222
+ appended_encoding : {'base64', 'raw'}, optional
223
+ Encoding type for appended data. Used only when the `encoding` parameter
224
+ is set to 'appended'. Defaults to 'base64'.
225
+ version : str, optional
226
+ Version of the VTK file format. Defaults to '1.0'.
227
+
228
+ """
229
+ if num_cells is not None:
230
+ num_cells = np.asarray(num_cells, dtype=np.int32)
231
+ whole_extent = np.array([[0, 0, 0], num_cells]).T.flatten()
232
+ piece_extent = whole_extent
233
+
234
+ if file_format.lower() == 'xml':
235
+
236
+ file = XMLStructuredGridWriter(filepath, points, cell_data=cell_data, field_data=field_data, point_data=point_data,
237
+ whole_extent=whole_extent, piece_extent=piece_extent, encoding=encoding,
238
+ appended_encoding=appended_encoding, version=version, ascii_precision=ascii_precision,
239
+ ascii_ncolumns=ascii_ncolumns, declaration=add_declaration)
240
+
241
+ file.write_xml_file()
242
+
243
+ elif file_format.lower() in ['vtkhdf', 'hdf5', 'hdf', 'vtk hdf']:
244
+ file = VTKHDFStructuredGridWriter(filepath, points, num_cells=num_cells,
245
+ point_data=point_data, cell_data=cell_data, field_data=field_data,
246
+ additional_metadata=additional_metadata)
247
+ file.write_vtkhdf_file()
248
+
249
+ else:
250
+ raise ValueError(f"Unsupported file format: {file_format}. Supported formats are 'xml' and 'vtkhdf'.")
251
+
252
+
253
+ def write_vtu(filepath, nodes, cell_type=None, connectivity=None, offsets=None, point_data=None, cell_data=None,
254
+ field_data=None, additional_metadata=None, encoding='ascii', ascii_precision=16, ascii_ncolumns=6,
255
+ add_declaration=True, appended_encoding='base64', version='1.0', file_format='xml'):
256
+ """
257
+ Write VTK Unstructured Grid (VTU) files containing required and optional data attributes.
258
+
259
+ Supports multiple encoding formats including ASCII, binary and appended data modes for efficient storage and
260
+ transfer. Advanced data organization methods and field data are also supported for flexibility and proper
261
+ representation of dataset metadata.
262
+
263
+ Parameters
264
+ ----------
265
+ filepath : str
266
+ Path to the generated VTU file.
267
+ nodes : numpy.ndarray
268
+ Array of node coordinates, typically shape (N, 3) for 3D points.
269
+ cell_type : numpy.ndarray, optional
270
+ Array specifying VTK cell types for each cell.
271
+ connectivity : numpy.ndarray, optional
272
+ Array defining node connectivity for each cell.
273
+ offsets : numpy.ndarray, optional
274
+ Array indicating start indices of each cell in connectivity array.
275
+ point_data : dict, optional
276
+ Dictionary of arrays containing data associated with points.
277
+ cell_data : dict, optional
278
+ Dictionary of arrays containing data associated with cells.
279
+ field_data : dict, optional
280
+ Dictionary of arrays containing data associated with the entire dataset.
281
+ encoding : {'ascii', 'binary', 'appended'}, optional
282
+ Output encoding format. Defaults to 'ascii'.
283
+ ascii_precision : int, optional
284
+ Decimal precision for ASCII output. Defaults to 16.
285
+ ascii_ncolumns : int, optional
286
+ Number of columns for ASCII output. Defaults to 6.
287
+ add_declaration : bool, optional
288
+ Include XML declaration if True. Defaults to True.
289
+ appended_encoding : {'base64', 'raw'}, optional
290
+ Encoding for appended data mode. Defaults to 'base64'.
291
+ version : str, optional
292
+ VTK XML format version. Defaults to '1.0'.
293
+
294
+ Returns
295
+ -------
296
+ None
297
+ Writes VTU file to specified filepath.
298
+ """
299
+ if file_format.lower() == 'xml':
300
+
301
+ file = XMLUnstructuredGridWriter(filepath, nodes, cell_type=cell_type, connectivity=connectivity, offsets=offsets,
302
+ cell_data=cell_data, field_data=field_data, point_data=point_data, encoding=encoding,
303
+ appended_encoding=appended_encoding, version=version, ascii_precision=ascii_precision,
304
+ ascii_ncolumns=ascii_ncolumns, declaration=add_declaration)
305
+
306
+ file.write_xml_file()
307
+
308
+ elif file_format.lower() in ['vtkhdf', 'hdf5', 'hdf', 'vtk hdf']:
309
+ file = VTKHDFUnstructuredGridWriter(filepath, nodes=nodes, cell_types=cell_type, connectivity=connectivity, offsets=offsets,
310
+ point_data=point_data, cell_data=cell_data, field_data=field_data,
311
+ additional_metadata=additional_metadata,)
312
+ file.write_vtkhdf_file()
313
+
314
+ else:
315
+ raise ValueError(f"Unsupported file format: {file_format}. Supported formats are 'xml' and 'vtkhdf'.")
316
+
317
+
318
+ def write_vtp(filepath, point_data=None, cell_data=None, field_data=None, points=None, verts=None, lines=None,
319
+ strips=None, polys=None, additional_metadata=None, encoding='ascii', ascii_precision=16,
320
+ file_format='xml', ascii_ncolumns=6, add_declaration=True, appended_encoding='base64', version='1.0'):
321
+ """
322
+ Write VTK PolyData (VTP) files, in either ASCII or binary formats.
323
+
324
+ Supports configuration of precision, encoding, and data organization in the output file and
325
+ includes geometric data, topology, and attributes.
326
+
327
+ Parameters
328
+ ----------
329
+ filepath : str
330
+ Path to the generated VTP file.
331
+
332
+ point_data : dict, optional
333
+ Dictionary of data arrays associated with points. Keys represent array names, and
334
+ values are the corresponding data.
335
+
336
+ cell_data : dict, optional
337
+ Dictionary of data arrays associated with cells. Keys represent array names, and
338
+ values are the corresponding data.
339
+
340
+ field_data : dict or None, optional
341
+ General-purpose data arrays applicable to the entire dataset, where keys represent
342
+ array names and values are the corresponding data.
343
+
344
+ points : numpy.ndarray or None, optional
345
+ Array of point coordinates, typically of shape (N, 3), representing the geometry.
346
+
347
+ verts : numpy.ndarray or None, optional
348
+ Vertex connectivity data, typically representing singular points.
349
+
350
+ lines : numpy.ndarray or None, optional
351
+ Line connectivity data, representing polyline geometry.
352
+
353
+ strips : numpy.ndarray or None, optional
354
+ Triangle strip connectivity data.
355
+
356
+ polys : numpy.ndarray or None, optional
357
+ Polygonal face connectivity data.
358
+
359
+ encoding : {'ascii', 'binary', 'appended'}, optional
360
+ Encoding format of the output file. Defaults to 'ascii'.
361
+
362
+ ascii_precision : int, optional
363
+ Number of decimal places for floating-point values in ASCII mode. Defaults to 16.
364
+
365
+ ascii_ncolumns : int, optional
366
+ Number of data columns for arrays written in ASCII mode. Defaults to 6.
367
+
368
+ add_declaration : bool, optional
369
+ Includes XML declaration at the start of the file if True. Defaults to True.
370
+
371
+ appended_encoding : {'base64', 'raw'}, optional
372
+ Encoding format for appended data when using appended mode. Can be either 'base64'
373
+ or 'raw'. Defaults to 'base64'.
374
+
375
+ version : str, optional
376
+ Version of the VTK XML file format. Defaults to '1.0'.
377
+ """
378
+ if file_format.lower() == 'xml':
379
+ file = XMLPolyDataWriter(filepath, points=points, verts=verts, lines=lines, polys=polys, strips=strips,
380
+ point_data=point_data, cell_data=cell_data, field_data=field_data, encoding=encoding,
381
+ appended_encoding=appended_encoding, version=version, ascii_precision=ascii_precision,
382
+ ascii_ncolumns=ascii_ncolumns, declaration=add_declaration)
383
+
384
+ file.write_xml_file()
385
+
386
+ elif file_format.lower() in ['vtkhdf', 'hdf5', 'hdf', 'vtk hdf']:
387
+ file = VTKHDFPolyDataWriter(filepath, points, lines=lines, strips=strips,
388
+ polys=polys, verts=verts, point_data=point_data,
389
+ cell_data=cell_data, field_data=field_data, additional_metadata=additional_metadata)
390
+ file.write_vtkhdf_file()
391
+
392
+ else:
393
+ raise ValueError(f"Unsupported file format: {file_format}. Supported formats are 'xml' and 'vtkhdf'.")