-
Notifications
You must be signed in to change notification settings - Fork 443
/
fileio.py
655 lines (514 loc) · 19.1 KB
/
fileio.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
"""Contains a dictionary that maps file extensions to VTK readers."""
import pathlib
from pathlib import Path
import warnings
import numpy as np
import pyvista
from pyvista.core import _vtk_core as _vtk
from pyvista.core.errors import PyVistaDeprecationWarning
from .observers import Observer
def set_pickle_format(format: str): # noqa: A002
"""Set the format used to serialize :class:`pyvista.DataObject` when pickled.
Parameters
----------
format : str
The format for serialization. Acceptable values are "xml" or "legacy".
Raises
------
ValueError
If the provided format is not supported.
"""
supported = {'xml', 'legacy'}
format_ = format.lower()
if format_ not in supported:
raise ValueError(
f'Unsupported pickle format `{format_}`. Valid options are `{"`, `".join(supported)}`.',
)
pyvista.PICKLE_FORMAT = format_
def _get_ext_force(filename, force_ext=None):
if force_ext:
return str(force_ext).lower()
else:
return get_ext(filename)
def get_ext(filename):
"""Extract the extension of the filename.
For files with the .gz suffix, the previous extension is returned as well.
This is needed e.g. for the compressed NIFTI format (.nii.gz).
Parameters
----------
filename : str
The filename from which to extract the extension.
Returns
-------
str
The extracted extension. For files with the .gz suffix, the previous
extension is returned as well.
"""
path = Path(filename)
base = str(path.parent / path.stem)
ext = path.suffix
ext = ext.lower()
if ext == ".gz":
path = Path(base)
ext_pre = path.suffix.lower()
ext = f"{ext_pre}{ext}"
return ext
def set_vtkwriter_mode(vtk_writer, use_binary=True):
"""Set any vtk writer to write as binary or ascii.
Parameters
----------
vtk_writer : vtkDataWriter, vtkPLYWriter, vtkSTLWriter, or _vtk.vtkXMLWriter
The vtk writer instance to be configured.
use_binary : bool, default: True
If ``True``, the writer is set to write files in binary format. If
``False``, the writer is set to write files in ASCII format.
Returns
-------
vtkDataWriter, vtkPLYWriter, vtkSTLWriter, or _vtk.vtkXMLWriter
The configured vtk writer instance.
"""
from vtkmodules.vtkIOGeometry import vtkSTLWriter
from vtkmodules.vtkIOLegacy import vtkDataWriter
from vtkmodules.vtkIOPLY import vtkPLYWriter
if isinstance(vtk_writer, (vtkDataWriter, vtkPLYWriter, vtkSTLWriter)):
if use_binary:
vtk_writer.SetFileTypeToBinary()
else:
vtk_writer.SetFileTypeToASCII()
elif isinstance(vtk_writer, _vtk.vtkXMLWriter):
if use_binary:
vtk_writer.SetDataModeToBinary()
else:
vtk_writer.SetDataModeToAscii()
return vtk_writer
def read(filename, force_ext=None, file_format=None, progress_bar=False):
"""Read any file type supported by ``vtk`` or ``meshio``.
Automatically determines the correct reader to use then wraps the
corresponding mesh as a pyvista object. Attempts native ``vtk``
readers first then tries to use ``meshio``.
See :func:`pyvista.get_reader` for list of formats supported.
.. note::
See https://github.com/nschloe/meshio for formats supported by
``meshio``. Be sure to install ``meshio`` with ``pip install
meshio`` if you wish to use it.
Parameters
----------
filename : str
The string path to the file to read. If a list of files is
given, a :class:`pyvista.MultiBlock` dataset is returned with
each file being a separate block in the dataset.
force_ext : str, optional
If specified, the reader will be chosen by an extension which
is different to its actual extension. For example, ``'.vts'``,
``'.vtu'``.
file_format : str, optional
Format of file to read with meshio.
progress_bar : bool, default: False
Optionally show a progress bar. Ignored when using ``meshio``.
Returns
-------
pyvista.DataSet
Wrapped PyVista dataset.
Examples
--------
Load an example mesh.
>>> import pyvista as pv
>>> from pyvista import examples
>>> mesh = pv.read(examples.antfile)
>>> mesh.plot(cpos='xz')
Load a vtk file.
>>> mesh = pv.read('my_mesh.vtk') # doctest:+SKIP
Load a meshio file.
>>> mesh = pv.read("mesh.obj") # doctest:+SKIP
"""
if file_format is not None and force_ext is not None:
raise ValueError('Only one of `file_format` and `force_ext` may be specified.')
if isinstance(filename, (list, tuple)):
multi = pyvista.MultiBlock()
for each in filename:
name = Path(str(each)).name if isinstance(each, (str, pathlib.Path)) else None
multi.append(read(each, file_format=file_format), name)
return multi
filename = str(Path(str(filename)).expanduser().resolve())
if not Path(filename).is_file() and not Path(filename).is_dir():
raise FileNotFoundError(f'File ({filename}) not found')
# Read file using meshio.read if file_format is present
if file_format:
return read_meshio(filename, file_format)
ext = _get_ext_force(filename, force_ext)
if ext in ['.e', '.exo']:
return read_exodus(filename)
try:
reader = pyvista.get_reader(filename, force_ext)
except ValueError:
# if using force_ext, we are explicitly only using vtk readers
if force_ext is not None:
raise OSError("This file was not able to be automatically read by pvista.")
from meshio._exceptions import ReadError
try:
return read_meshio(filename)
except ReadError:
raise OSError("This file was not able to be automatically read by pyvista.")
else:
observer = Observer()
observer.observe(reader.reader)
if progress_bar:
reader.show_progress()
mesh = reader.read()
if observer.has_event_occurred():
warnings.warn(
f"The VTK reader `{reader.reader.GetClassName()}` in pyvista reader `{reader}` raised an error"
"while reading the file.\n"
f'\t"{observer.get_message()}"',
)
return mesh
def _apply_attrs_to_reader(reader, attrs):
"""For a given pyvista reader, call methods according to attrs.
Parameters
----------
reader : pyvista.BaseReader
Reader to call methods on.
attrs : dict
Mapping of methods to call on reader.
"""
warnings.warn(
"attrs use is deprecated. Use a Reader class for more flexible control",
PyVistaDeprecationWarning,
)
for name, args in attrs.items():
attr = getattr(reader.reader, name)
if args is not None:
if not isinstance(args, (list, tuple)):
args = [args]
attr(*args)
else:
attr()
def read_texture(filename, progress_bar=False):
"""Load a texture from an image file.
Will attempt to read any file type supported by ``vtk``, however
if it fails, it will attempt to use ``imageio`` to read the file.
Parameters
----------
filename : str
The path of the texture file to read.
progress_bar : bool, default: False
Optionally show a progress bar.
Returns
-------
pyvista.Texture
PyVista texture object.
Examples
--------
Read in an example jpg map file as a texture.
>>> from pathlib import Path
>>> import pyvista as pv
>>> from pyvista import examples
>>> Path(examples.mapfile).name
'2k_earth_daymap.jpg'
>>> texture = pv.read_texture(examples.mapfile)
>>> type(texture)
<class 'pyvista.plotting.texture.Texture'>
"""
filename = str(Path(filename).expanduser().resolve())
try:
# initialize the reader using the extension to find it
image = read(filename, progress_bar=progress_bar)
if image.n_points < 2:
raise ValueError("Problem reading the image with VTK.")
return pyvista.Texture(image)
except (KeyError, ValueError):
# Otherwise, use the imageio reader
pass
return pyvista.Texture(_try_imageio_imread(filename)) # pragma: no cover
def read_exodus(
filename,
animate_mode_shapes=True,
apply_displacements=True,
displacement_magnitude=1.0,
read_point_data=True,
read_cell_data=True,
enabled_sidesets=None,
):
"""Read an ExodusII file (``'.e'`` or ``'.exo'``).
Parameters
----------
filename : str
The path to the exodus file to read.
animate_mode_shapes : bool, default: True
When ``True`` then this reader will report a continuous time
range [0,1] and animate the displacements in a periodic
sinusoid.
apply_displacements : bool, default: True
Geometric locations can include displacements. When ``True``,
the nodal positions are 'displaced' by the standard exodus
displacement vector. If displacements are turned off, the user
can explicitly add them by applying a warp filter.
displacement_magnitude : bool, default: 1.0
This is a number between 0 and 1 that is used to scale the
``DisplacementMagnitude`` in a sinusoidal pattern.
read_point_data : bool, default: True
Read in data associated with points.
read_cell_data : bool, default: True
Read in data associated with cells.
enabled_sidesets : str | int, optional
The name of the array that store the mapping from side set
cells back to the global id of the elements they bound.
Returns
-------
pyvista.DataSet
Wrapped PyVista dataset.
Examples
--------
>>> import pyvista as pv
>>> data = pv.read_exodus('mymesh.exo') # doctest:+SKIP
"""
from .helpers import wrap
# lazy import here to avoid loading module on import pyvista
try:
from vtkmodules.vtkIOExodus import vtkExodusIIReader
except ImportError:
from vtk import vtkExodusIIReader
reader = vtkExodusIIReader()
reader.SetFileName(filename)
reader.UpdateInformation()
reader.SetAnimateModeShapes(animate_mode_shapes)
reader.SetApplyDisplacements(apply_displacements)
reader.SetDisplacementMagnitude(displacement_magnitude)
if read_point_data: # read in all point data variables
reader.SetAllArrayStatus(vtkExodusIIReader.NODAL, 1)
if read_cell_data: # read in all cell data variables
reader.SetAllArrayStatus(vtkExodusIIReader.ELEM_BLOCK, 1)
if enabled_sidesets is None:
enabled_sidesets = list(range(reader.GetNumberOfSideSetArrays()))
for sideset in enabled_sidesets:
if isinstance(sideset, int):
name = reader.GetSideSetArrayName(sideset)
elif isinstance(sideset, str):
name = sideset
else:
raise ValueError(f'Could not parse sideset ID/name: {sideset}')
reader.SetSideSetArrayStatus(name, 1)
reader.Update()
return wrap(reader.GetOutput())
def is_meshio_mesh(obj):
"""Test if passed object is instance of ``meshio.Mesh``.
Parameters
----------
obj : object
Any object.
Returns
-------
bool
``True`` if ``obj`` is a ``meshio.Mesh``.
"""
try:
import meshio
return isinstance(obj, meshio.Mesh)
except ImportError:
return False
def from_meshio(mesh):
"""Convert a ``meshio`` mesh instance to a PyVista mesh.
Parameters
----------
mesh : meshio.Mesh
A mesh instance from the ``meshio`` library.
Returns
-------
pyvista.UnstructuredGrid
A PyVista unstructured grid representation of the input ``meshio`` mesh.
Raises
------
ImportError
If the appropriate version of ``meshio`` library is not found.
"""
try: # meshio<5.0 compatibility
from meshio.vtk._vtk import meshio_to_vtk_type, vtk_type_to_numnodes
except ImportError: # pragma: no cover
from meshio._vtk_common import meshio_to_vtk_type
from meshio.vtk._vtk_42 import vtk_type_to_numnodes
# Extract cells from meshio.Mesh object
cells = []
cell_type = []
for c in mesh.cells:
if c.type.startswith("polyhedron"):
vtk_type = meshio_to_vtk_type["polyhedron"]
for cell in c.data:
connectivity = [len(cell)]
for face in cell:
connectivity += [len(face), *face]
connectivity.insert(0, len(connectivity))
cells.append(connectivity)
else:
vtk_type = meshio_to_vtk_type[c.type]
numnodes = vtk_type_to_numnodes[vtk_type]
if numnodes == -1:
# Count nodes in each cell
fill_values = np.array([[len(data)] for data in c.data], dtype=c.data.dtype)
else:
fill_values = np.full((len(c.data), 1), numnodes, dtype=c.data.dtype)
cells.append(np.hstack((fill_values, c.data)).ravel())
cell_type += [vtk_type] * len(c.data)
# Extract cell data from meshio.Mesh object
cell_data = {k: np.concatenate(v) for k, v in mesh.cell_data.items()}
# Create pyvista.UnstructuredGrid object
points = mesh.points
# Convert to 3D if points are 2D
if points.shape[1] == 2:
zero_points = np.zeros((len(points), 1), dtype=points.dtype)
points = np.hstack((points, zero_points))
grid = pyvista.UnstructuredGrid(
np.concatenate(cells).astype(np.int64, copy=False),
np.array(cell_type),
np.array(points, np.float64),
)
# Set point data
grid.point_data.update({k: np.array(v, np.float64) for k, v in mesh.point_data.items()})
# Set cell data
grid.cell_data.update(cell_data)
# Call datatype-specific post-load processing
grid._post_file_load_processing()
return grid
def read_meshio(filename, file_format=None):
"""Read any mesh file using meshio.
Parameters
----------
filename : str
The name of the file to read. It should include the file extension.
file_format : str, optional
The format of the file to read. If not provided, the file format will
be inferred from the file extension.
Returns
-------
pyvista.Dataset
The mesh read from the file.
Raises
------
ImportError
If the meshio package is not installed.
"""
try:
import meshio
except ImportError: # pragma: no cover
raise ImportError("To use this feature install meshio with:\n\npip install meshio")
# Make sure relative paths will work
filename = str(Path(str(filename)).expanduser().resolve())
# Read mesh file
mesh = meshio.read(filename, file_format)
return from_meshio(mesh)
def save_meshio(filename, mesh, file_format=None, **kwargs):
"""Save mesh to file using meshio.
Parameters
----------
filename : str
Filename to save the mesh to.
mesh : pyvista.DataSet
Any PyVista mesh/spatial data type.
file_format : str, optional
File type for meshio to save. For example ``'.bdf'``. This
is normally inferred from the extension but this can be
overridden.
**kwargs : dict, optional
Additional keyword arguments. See
``meshio.write_points_cells`` for more details.
Examples
--------
Save a pyvista sphere to a Abaqus data file.
>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> pv.save_meshio('mymesh.inp', sphere) # doctest:+SKIP
"""
try:
import meshio
except ImportError: # pragma: no cover
raise ImportError("To use this feature install meshio with:\n\npip install meshio")
try: # for meshio<5.0 compatibility
from meshio.vtk._vtk import vtk_to_meshio_type
except: # pragma: no cover
from meshio._vtk_common import vtk_to_meshio_type
# Make sure relative paths will work
filename = str(Path(str(filename)).expanduser().resolve())
# Cast to pyvista.UnstructuredGrid
if not isinstance(mesh, pyvista.UnstructuredGrid):
mesh = mesh.cast_to_unstructured_grid()
# Copy useful arrays to avoid repeated calls to properties
vtk_offset = mesh.offset
vtk_cells = mesh.cells
vtk_cell_type = mesh.celltypes
# Check that meshio supports all cell types in input mesh
pixel_voxel = {8, 11} # Handle pixels and voxels
for cell_type in np.unique(vtk_cell_type):
if cell_type not in vtk_to_meshio_type.keys() and cell_type not in pixel_voxel:
raise TypeError(f"meshio does not support VTK type {cell_type}.")
# Get cells
cells = []
c = 0
for i, (offset, cell_type) in enumerate(zip(vtk_offset, vtk_cell_type)):
if cell_type == 42:
cell_ = mesh.get_cell(i)
cell = [face.point_ids for face in cell_.faces]
cell_type = f"polyhedron{cell_.n_points}"
else:
numnodes = vtk_cells[offset + c]
cell = vtk_cells[offset + 1 + c : offset + 1 + c + numnodes]
c += 1
cell = (
cell
if cell_type not in pixel_voxel
else cell[[0, 1, 3, 2]] if cell_type == 8 else cell[[0, 1, 3, 2, 4, 5, 7, 6]]
)
cell_type = cell_type if cell_type not in pixel_voxel else cell_type + 1
cell_type = vtk_to_meshio_type[cell_type]
if len(cells) > 0 and cells[-1][0] == cell_type:
cells[-1][1].append(cell)
else:
cells.append((cell_type, [cell]))
# Get point data
point_data = {k.replace(" ", "_"): v for k, v in mesh.point_data.items()}
# Get cell data
vtk_cell_data = mesh.cell_data
indices = np.insert(np.cumsum([len(c[1]) for c in cells]), 0, 0)
cell_data = (
{
k.replace(" ", "_"): [v[i1:i2] for i1, i2 in zip(indices[:-1], indices[1:])]
for k, v in vtk_cell_data.items()
}
if vtk_cell_data
else {}
)
# Save using meshio
meshio.write_points_cells(
filename=filename,
points=np.array(mesh.points),
cells=cells,
point_data=point_data,
cell_data=cell_data,
file_format=file_format,
**kwargs,
)
def _process_filename(filename):
return str(Path(str(filename)).expanduser().resolve())
def _try_imageio_imread(filename):
"""Attempt to read a file using ``imageio.imread``.
Parameters
----------
filename : str
Name of the file to read using ``imageio``.
Returns
-------
imageio.core.util.Array
Image read from ``imageio``.
Raises
------
ModuleNotFoundError
Raised when ``imageio`` is not installed when attempting to read
``filename``.
"""
try:
from imageio import imread
except ModuleNotFoundError: # pragma: no cover
raise ModuleNotFoundError(
'Problem reading the image with VTK. Install imageio to try to read the '
'file using imageio with:\n\n'
' pip install imageio',
) from None
return imread(filename)