question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fix binary (un-)pickling of PyVista objects

See original GitHub issue

Some 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:closed
  • Created 2 years ago
  • Comments:17 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
MatthewFlammcommented, Sep 5, 2022

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?

2reactions
MatthewFlammcommented, Nov 2, 2021

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.

import vtk
import pickle
import pyvista as pv
import numpy as np
import io

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 = io.BytesIO()
pickle_vtk(mesh, fname)
test = unpickle_vtk(fname)

assert np.allclose(mesh.points, test.points)
assert mesh.n_arrays == test.n_arrays
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found