pyvista / pyvista-support

[moved] Please head over to the Discussions tab of the PyVista repository
https://github.com/pyvista/pyvista/discussions
60 stars 4 forks source link

Resample to structured grid and convert to numpy array #445

Open alexjarosch opened 3 years ago

alexjarosch commented 3 years ago

Hi all,

I struggle with resampling a unstructured dataset to a structured grid and then convert the point dataset back to an numpy array on which I could do some math. Here is a basic example utilizing the embryo dataset:

import pyvista as pv
from pyvista import examples
import matplotlib.pyplot as plt

# get a sample dataset
data_to_probe = examples.download_embryo()

# create a grid to resample
mesh = pv.create_grid(data_to_probe, dimensions=(70, 72, 74))

# resample the data
result = mesh.sample(data_to_probe)

# convert the resampled data to an numpy array
SLCImage = pv.point_array(result, 'SLCImage')

# attempt to reshape
SLCImage_shape = SLCImage.reshape((70, 72, 74))

# this should show a slice in the xy plane at the middel of z
plt.imshow(SLCImage_shape[:, :, 46])
plt.show()

I get the dataset and create a grid (which is structured) on a lower resolution. Then I utilize the point_array feature the extract the values from the point dataset 'SLCImage'. However in the last step when I attempt to reshape the numpy array values into a 3D array with the same dimensions as the resample grid, I fail. To prove my point I plot a slice through the extracted numpy array, which shows that the ordering of the numpy array is wrong.

Does anybody know how to reshape the point_array correctly? Any help would be highly appreciated.

Best wishes, Alex