Fix binary (un-)pickling of PyVista objects
See original GitHub issueSome time ago support was added for pickling/unplickling of PyVista dataobject. This feature is really useful in case of inter process communication like multi processing. However the feature is implemented use a ascii writer, which is tremendously slow.
I played somewhat with the code of @banesullivan (see: https://github.com/pyvista/pyvista-support/issues/59#issue-494762368) and tried to use the binary writer. I found out it was due to the pickle.dump
the arrays were getting messed up. If you don’t pipe it to an output string but to a file the example works like a charm.
Of course some functionality has be added to removed the files after they are red but I would really like to see the SetFileTypeToBinary
to be the default.
import vtk
import pickle
import pyvista as pv
import numpy as np
def pickle_vtk(mesh, filename):
writer = vtk.vtkDataSetWriter()
writer.SetInputDataObject(mesh)
writer.SetFileName(filename)
writer.SetFileTypeToBinary()
writer.Write()
return filename
def unpickle_vtk(filename):
reader = vtk.vtkDataSetReader()
reader.SetFileName(filename)
reader.Update()
return pv.wrap(reader.GetOutput())
#### Example
from pyvista import examples
mesh = examples.load_random_hills()
fname = 'filename.vtkpickle'
pickle_vtk(mesh, fname)
test = unpickle_vtk(fname)
assert np.allclose(mesh.points, test.points)
assert mesh.n_arrays == test.n_arrays
Issue Analytics
- State:
- Created 2 years ago
- Comments:17 (16 by maintainers)
Top Results From Across the Web
Pickling PyVista datasets · Issue #59 - GitHub
Here's an example of how to pickle a PyVista dataset! ... Fix binary (un-)pickling of PyVista objects pyvista/pyvista#1768.
Read more >Examples — PyVista 0.37.0 documentation
These examples demo how to read various file types into PyVista mesh objects, create meshes from NumPy arrays, and how to create primitive...
Read more >How to Pickle and Unpickle Objects in Python - Stack Abuse
Pickling and unpickling in Python is the process that is used to describe ... Let's try to "pickle" the athletes object to a...
Read more >How do I read binary pickle data first, then unpickle it?
I've read here that pickling can be sped up by first reading the entire file into memory, and then unpickling it (that particular...
Read more >Release 1.1.0 Alex Tait, Thomas Ferreira de Lima
The lightlab package contains an object built onto PyVISA, representing the ... 2: Fixing the PEP8 violations of the previous figure.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think @adeak comment could have separated better between the ask for more information and asking for better consideration of the people being pinged. This is my interpretation of the comment. Although I agree with both points separately.
The pinging seems nonspecific here and misses key contributors to this functionality. Large amounts of pings should be reserved for when there is a critical bug or a large blocker for continued development. This particular issue is localized.
Having two places for this conversation will be distracting, can we move this discussion to #3269 so that it is easier to track?
We don’t even need to be able to cleanup the file if you use a BytesIO object, which I think should be pickle-able.