Comments (6)
Hi @byba1990, thanks for posting your question! This sounds like a sophisticated use case and I'll do my best to help you complete these two tasks.
First, would this dataset be something you are willing to attach here so that I could work directly with the data you have in mind?
Below are some first passes at addressing your questions - please let me know if I misinterpreted any of your questions or if these are/aren't helpful.
Part 1
Is there a way to divide axially a volume, in order to compute total and average variables over this smaller volumes obtained?
Perhaps the .clip
filter would work well for you. This filter cuts/clips a mesh/volume along a user defined plane. Here's an example to clip a mesh at the center normal to the Y-axis:
import pyvista as pv
from pyvista import examples
# Load and clip a mesh
mesh = examples.load_channels()
clipped = mesh.clip(normal='y')
# Compare clipped domain to whole domain
p = pv.Plotter()
p.add_mesh(mesh.outline(), color='white')
p.add_mesh(clipped)
p.show()
# Now compute the total and average variables over the clipped domain
variable = clipped.active_scalar_name
average = np.average(clipped[variable])
total = np.sum(clipped[variable])
print('Average: {}'.format(average))
print('Total: {}'.format(total))
Average: 0.27347072
Total: 1709192
Alternative clipping/sub-meshing
You could also leverage:
- the
.clip_box()
filter to extract specific volumetric regions of your mesh - the
.extract_cells()
filter underUnstructuredGrid
s via thecast_to_unstructured_grid()
filter to extract specific non-voxel cells in your mesh. - the
.threshold()
filter to extract regions of the mesh that have data values in a give range to understand distributions of variable within specific ranges.
Part 2
Perhaps the best alternative would be to create a high resolution line source (maybe we can use @akaszynski's brand-new Spline feature in PyVista?) to sample a dataset much like what you have in the code snippet above then plot the variables in 2D via matplotlib
with X-axis being the distance along the line and the Y-axis being the variable of interest's values. Here's an example with an arbitrarily placed line at a high-ish resolution:
import pyvista as pv
import numpy as np
import matplotlib.pyplot as plt
from pyvista import examples
# Example dataset with some interesting data values
mesh = examples.download_kitchen()
mesh.plot()
# Make two points to construct the line between
a = [mesh.bounds[0], mesh.bounds[2], mesh.bounds[4]]
b = [mesh.bounds[1], mesh.bounds[3], mesh.bounds[5]]
line = pv.Line(a, b, resolution=100)
# And preview line's extent
p = pv.Plotter()
p.add_mesh(mesh, style='wireframe', color='w')
p.add_mesh(sampled, color='b')
p.show()
# Sample the mesh's variables onto the line
sampled = line.sample(mesh)
# Compute distances of every point along line
# NOTE: this only works for linear lines
compute = lambda p0, p1: np.sqrt(np.sum((p1 - p0)**2, axis=1))
distance = compute(a, line.points)
# Grab a variable of interest
variable = mesh.active_scalar_name
values = sampled[variable]
# Plot it in 2D
if values.ndim > 1:
for i in range(values.shape[1]):
plt.plot(distance, values[:, i], label='Component {}'.format(i))
plt.legend()
else:
plt.plot(distance, values)
plt.xlabel('Distance')
plt.ylabel(variable)
plt.title('Plot Variable Along Line')
plt.show()
from pyvista-support.
Please note that this inspired a new .plot_over_line()
filter in pyvista/pyvista#250 - @byba1990, I'd greatly appreciate your feedback on this new filter
from pyvista-support.
Hi @byba1990 - checking in to see if you found these details helpful. Please let us know if you have any further questions or if this issue can be closed.
from pyvista-support.
Dear @banesullivan,
thank you very much for your time and kindness! Your suggestions has been very helpful! Sorry for my late reply.
Howerver, to sum up:
- filter to select volumes with clip. It seems that it works but from your suggestion:
# Now compute the total and average variables over the clipped domain
variable = clipped.active_scalar_name
average = np.average(mesh[variable])
total = np.sum(mesh[variable])
print('Average: {}'.format(average))
print('Total: {}'.format(total))
the average and total should be referred to clipped (only for values over this subregion), right?
During my own tests, I checked the cells for clipped and mesh but, if my clipped is a subregion that corresponds to half of my initial mesh:
total cell number of mesh=7560
total cell number of clipped=11340
Why? I suppose that the number of clipped cells is half of mesh cells, i.e. 3780. Moreover, with
sizeClipped=clipped.compute_cell_sizes()
I have negative values for volume.
However, checking the total volumes of clipped and mesh:
sizeClipped=clipped.compute_cell_sizes()
sizeMesh=mesh.compute_cell_sizes()
TotVolClip=np.abs(np.sum(sizeClipped['Volume'])) # abs to have positive volumes
TotVolMesh=np.sum(sizeMesh['Volume'])
fraction=TotVolClip/TotVolMesh
my fraction results 0.500, correctly. So I think that total and average values are correct for clipped.
- Plot overline works perfectly, thanks. Thanks also for your appreciation, in relation to the inspiration of new plot_overline() filter for pyvista.
Thank you very much for your time Bane!
Regards,
Christian
from pyvista-support.
Hi @byba1990:
Typo
the average and total should be referred to clipped (only for values over this subregion), right?
Ah yes, my mistake - those should indeed be called on clipped
, not mesh
. I just edited my original post so this doesn't confuse newcomers
Number of Cells in Clipped Mesh
total cell number of mesh=7560
total cell number of clipped=11340Why? I suppose that the number of clipped cells is half of mesh cells, i.e. 3780. Moreover, with
This is an artifact of the clip
filter. When clipping the input mesh, it is tessellated into triangles or tetrahedrals. So if the input mesh has voxels or non-triangular cells, then it will have to be regenerated as several triangular cells put together - thus a clipped mesh could have more cells than the input mesh which is what you are seeing. You could pass the show_edges=True
argument when plotting to see the tetrahedrals.
This is an issue that has been greatly bugging me for a while that I do not have an elegant solution for at the moment. See also: https://discourse.vtk.org/t/is-it-possible-to-turn-off-triangles-on-vtkclipdataset-filter/279
On this note, the slice
filter allows us to not generate the triangles so it should be possible to do this with the clip
filter as well.
Negative Volume
sizeClipped=clipped.compute_cell_sizes()
I have negative values for volume.
Hm... that's really strange - would you mind uploading your mesh here in a zip
archive so I could toy with it? (mesh.save('mymesh.vtk')
)
from pyvista-support.
pyvista/pyvista@a68b45b fixes the clip triangulation issue
from pyvista-support.
Related Issues (20)
- create uniform mesh HOT 3
- Change the orientation of a scalar bar's title
- Applying textures on models from .obj files HOT 6
- How to create an Unstructured grid from points and faces HOT 2
- Type of data, plotting with glyphs HOT 3
- Controlling the thickness and orientation of a slice in Pyvista HOT 3
- Controlling the image position in window, without any click
- Mask/Remove Voxels in Air (small intersection with surface) HOT 3
- custom slider parameters
- running pyvista on headless machine with Centos OS HOT 1
- Seismic Horizon with Pyvista HOT 8
- Algorithm in sample function?
- Automatically updates HOT 3
- Slow interaction HOT 1
- read from text HOT 4
- Uniform Grid thresholding question HOT 8
- Take data by an input x,y,x coordinate (csv file) from vtk data file HOT 4
- Program broke when creating a mesh using pv.PolyData() HOT 4
- ATTENTION: Please move questions to PyVista's Discussions HOT 1
- plotter.enable_terrain_style not working for me HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvista-support.