Skip to main content

vtk-hdf

Python interface to read and write the VTK HDF format.

Installation & Paraview

Dependencies are given in the requirements file.

VTK HDF format is relatively new, and will require a recent vtk library to support it. For visualization, this means you'll need Paraview 5.10 or later for basic VTK HDF support. However, for using cell data with the ImageData HDF format (as an example), I needed a nightly build of Paraview in order to get the cell data visualization to work (the latest 5.11.2 release had not worked). Paraview 5.12 should contain the necessary changes to support the entire standard.

Quick Start

Example 1: Reading and writing entire ImageData objects at once

In the most trivial case, assume you have an ImageData object (vtk.ImageData or pyvista.ImageData) that you'd like to write to HDF5. Such an object could be initialized as:

...
import vtkhdf.image as v5i

dimensions = (91, 51, 121)
spacing = (.01, .013, .03)
origin = v5i.origin_of_centered_image(dimensions, spacing, 2)
box = pyvista.ImageData(
    dimensions=dimensions,
    spacing=spacing,
    origin=origin
)

We now have an ImageData object, but it's empty. Let's assign a dataset to it:

# dataset small enough that we can get away with meshgrid
X,Y,_ = v5i.mesh_axes(*v5i.get_point_axes(box.dimensions, box.spacing, box.origin))
data = X*X+Y*Y # positionally-dependent array data
v5i.set_point_array(box, data, "data")

This instance could easily be saved using pyvista.DataObject.save, but we can also write the data in HDF5 format.

with h5py.File("myimage.vtkhdf", "w") as f:
    v5i.write_vtkhdf(f, box)

We can verify that our data was saved correctly by reading it back for comparison or viewing it in Paraview.

mesh = v5i.read_vtkhdf("myimage.vtkhdf")

Example 2: Writing large datasets by slice

Let's assume we'll be working with a much larger ImageData set.

...
import vtkhdf.image as v5i

dimensions = (1200, 1501, 653) # 9.4 GB per 64-bit dataset!
spacing = (1e-3, 2e-3, 5e-4)
origin = v5i.origin_of_centered_image(dimensions, spacing, 2)
x,y,z = v5i.get_point_axes(dimensions, spacing, origin)

While many modern machines could hold this contiguous dataset in memory, often we don't need to and it will make our program more memory-efficient if we don't. Instead, we will initialize and save this ImageData slice-by-slice.

We'll open an HDF file for writing and set a cache size equal to a single slice that we'll be working with. Then we'll initialize the file to hold the 3D ImageData, though we haven't created it yet:

cache_slice_nbytes = dimensions[0] * dimensions[1] * 8
with h5py.File("mybigimage.vtkhdf", "w", rdcc_nbytes=cache_slice_nbytes) as f:
    v5i.initialize(f, v5i.dimensions2extent(dimensions),
                   origin=origin, spacing=spacing)
    dset = v5i.create_point_dataset(f, "data")

As VTK uses column-major ordering (often called Fortran ordering), the data will be sliced by axis 2, the last index of the dataset. Assuming default ImageData direction, this is the "z" axis (though this could easily be changed for "x" or "y").

    slice = np.empty(dimensions[:-1], order="F")
    for k, valz in enumerate(z):
        # avoid meshgrid with newaxis
        slice = np.sqrt(x[:, np.newaxis]**2 + y**2, order="F")
        v5i.write_slice(dset, slice, k)

Note: If the user was working with C-order numpy arrays, the dimensions, origin, and spacing input to v5i.initialize must be reversed from that of the C-order arrays. No modification needs to be made to the C-arrays themselves: v5i.write_slice handles transposing of the data when needed. Slices read via v5i.read_slice can be output in F or C-order according to the user's needs.

Now that we've written to this large file, we can access it later by slice as needed (or all at once if possible).

with h5py.File("mybigimage.vtkhdf", "r", rdcc_nbytes=cache_slice_nbytes) as f:
    dset = v5i.get_point_dataset(f, "data")
    slice = v5i.read_slice(dset, 42)
    assert np.allclose(slice, np.sqrt(x[:, np.newaxis]**2 + y**2, order="F"))

Full examples can be found here.

Further Reading

Metadata

Release files for vtk-hdf 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vtk-hdf 0.2.0
File Size Uploaded
vtk_hdf-0.2.0.tar.gz 17.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for vtk-hdf 0.2.0
File Interpreter ABI Platform
vtk_hdf-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.6 kB

Release files / vtk_hdf-0.2.0.tar.gz

Download URL vtk_hdf-0.2.0.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d85ac06bd83da5f75dcab22f91d84bbaa0b05657456a0b20c5f30af996eb6042
BLAKE2b-256 checksum
How to use checksums
f8524d45d3dae15b6b8bb0c5f1c42965fca9e13280e8f4ee4a59f28ff80c2740
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / vtk_hdf-0.2.0-py3-none-any.whl

Download URL vtk_hdf-0.2.0-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f0e030636d82c2064ac53ce3955895ea96f7af22e0af57bdd207a6538f0195e
BLAKE2b-256 checksum
How to use checksums
9b5f94506eb4b1112f1fd64748ad4a00eeb25edcbb6dc3d39ed9aafb92d000ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page