prs513rosewood / uvw Goto Github PK
View Code? Open in Web Editor NEWPython package to write numpy arrays to VTK XML files
License: MIT License
Python package to write numpy arrays to VTK XML files
License: MIT License
Hi,
I'm using uvw successfully to write structured scalar data to vtr-file, and I'm able to open and visualize the data with e.g. Paraview. Now I have vector valued data, e.g. the velocity, which I have stored in three diffrerent numpy arrays for the x,y,z components of the velocity, here U, V, W respectively. I can successfully write the three different ndarrays of shape (nx, ny, nz) as scalar data, and I build vector data from that inside Paraview. I have tried different reshape/ravel/dstack variants (e.g. "velocity=numpy.dstack((U.ravel(), V.ravel(), W.ravel()))"), but I didn't succeed and I have no idea of "how to write the velocity vector, which can easily be build from the components, to the VTK file, such that I can access the vector data in Paraview!?
Please, any help is highly appreciated!
Kind Regards
Jörg
Hi, I'm creating an ImageData
file, and when writing the file, the NumberOfTuples
property produces the size of the flattened data rather than the number of n-component elements in the data array. For instance
grid = uvw.ImageData('test_vtk.vti', [(-10., 10.), (-5, 5), (-5, 5)], [3, 3, 2], compression=True)
grid.addCellData(uvw.DataArray(np.arange(1., 9.).reshape(-1, 2), spatial_axes=[0], name='x0d'))
Produces a vti
file with NumberOfTuples=8
rather than 4
which is is the number of pair data values I set.
Is the NumberOfTuples
property necessary? I think the VTK file works as well without this property but it might be needed to allocate memory properly. There is very little documentation of this value in the VTK docs.
I found that the array is flattened here:
Line 102 in 6210d69
Hello,
I think I found a small error in the "unstructured.py"-part. The Hexahedron type should only have 8 nodes instead of 9 in the NODES_PER_CELL list.
CellType.HEXAHEDRON: 8, instead of CellType.HEXAHEDRON: 9, in line 65. This corrects a few errors I had using the .vtu files with paraview such as clipping elements.
Best greetings and thanks - very helpful stuff
Description of problem:
Hi,
Thank you for writing this module, it's far superior to alternatives I have tried with respect to the compression of output.
However, please consult the following example, adapted from the example given in the uvw README:
import numpy as np
from uvw import ImageData, DataArray
x = np.linspace(-0.5, 0.5, 128)
y = np.linspace(-0.5, 0.5, 128)
z = np.linspace(-0.5, 0.5, 128)
cell_data = np.zeros([128, 128, 128])
cell_data[54:74, 54:74, 10:118] = 1.0
cell_data[10:118, 54:74, 54:74] = 1.0
cell_data[54:74, 10:118, 54:74] = 1.0
grid = ImageData(
"test-good.vti",
ranges=[(-0.5, 0.5), (-0.5, 0.5), (-0.5, 0.5)],
points=[128, 128, 128],
compression=False,
)
grid.addPointData(DataArray(cell_data, range(3), "value"))
grid.write()
grid = ImageData(
"test-mangled.vti",
ranges=[(-0.5, 0.5), (-0.5, 0.5), (-0.5, 0.5)],
points=[128, 128, 128],
compression=False,
)
grid.addCellData(DataArray(cell_data, range(3), "value"))
grid.write()
This example outputs two VTI files representing two identical ndarrays, distinguished by whether the data are added as a point data array or a cell data array. ParaView renderings of these data are shown:
test-good.vti:
test-mangled.vti:
Note that the data in test-mangled.vti
is skewed.
Expected behaviour:
Broadly comparable data representations, distinguished by whether the data is of a cell or point type.
Attempted mitigations:
Commentary
Platform:
uvw 0.5.1
Python 3.10
ParaView 5.11.1
Linux 6.1
If I need to write a file multiple times, is there a way to update the cell or point data appended in a Data
object? SO I can write the same VTK but with updated values (and possibly change the filename as well)
Hello,
In the PVTKFile class you set the comm attribute to be self.comm = MPI.COMM_WORLD
. Can the user use a communicator other than MPI_COMM_WORLD to write files in parallel ?
When trying to create a rectilinear grid from numpy arrays, I got an error if my arrays were formatted with the data type '>f8': TypeError: Array dtype >f8 is not supported by VTK
. This seems similar to an issue recently reported for pyvista (#540).
The following code raised the error:
from uvw import RectilinearGrid, DataArray
import numpy as np
data = np.zeros((5,5,5))
#xdim = np.array([0,1,2,3,4]).astype('>f8')
#ydim = np.array([0,1,2,3,4]).astype('>f8')
#zdim = np.array([0,1,2,3,4]).astype('>f8')
grid = RectilinearGrid('data.vtr', (ydim, xdim, zdim), compression = True)
grid.addPointData(DataArray(data, range(3), 'data'))
grid.write()
Changing xdim
, ydim
, and zdim
as follows resolved the error:
xdim = np.array([0,1,2,3,4], dtype=np.float64)
ydim = np.array([0,1,2,3,4], dtype=np.float64)
zdim = np.array([0,1,2,3,4], dtype=np.float64)
While this may not matter much for cases where the arrays are defined within the code, it can cause problems when data is imported from other files that may have strange formatting.
(Sorry if my terminology is bad, or if this isn't a proper issue. I'm very new to github.)
System info:
OS: Windows 10 Home
Architecture: 64bit
Python 3.7.6
uvw 0.3.1
numpy 1.18.1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.