{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interpolate scalar data on mesh"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from vedo import dataurl, Mesh, Points, show, settings\n",
"from vedo.pyplot import plot\n",
"\n",
"settings.default_backend = \"vtk\" # or k3d, ipyvtk, or 2d"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
" \n",
" | \n",
" \n",
"vedo.mesh.Mesh
(....embl.es/examples/data/290.vtk) \n",
"\n",
" bounds (x/y/z) | 10.45 ... 1395 -702.5 ... 682.8 -834.5 ... 457.9 | \n",
" center of mass | (641, 14.0, -281) | \n",
" average size | 631.596 | \n",
" nr. points / faces | 1576 / 3031 | \n",
"\n",
"\n",
" \n",
" |
"
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Load a mesh of a mouse limb at 12 days of development\n",
"msh = Mesh(dataurl + \"290.vtk\")\n",
"msh"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Pick 100 points where we measure the value of a gene expression\n",
"ids = np.random.randint(0, msh.npoints, 100)\n",
"pts = msh.points()[ids] # slice the numpy array\n",
"x = pts[:, 0] # x coordinates of the points\n",
"gene = np.sin((x+150)/500)**2 # we are making this up!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
" \n",
"vedo.mesh.Mesh
(....embl.es/examples/data/290.vtk) \n",
"\n",
" bounds (x/y/z) | 10.45 ... 1395 -702.5 ... 682.8 -834.5 ... 457.9 | \n",
" center of mass | (641, 14.0, -281) | \n",
" average size | 631.596 | \n",
" nr. points / faces | 1576 / 3031 | \n",
" point data array | Scalars | \n",
"\n",
" \n",
" |
"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a set of points with those values\n",
"points = Points(pts, r=10).cmap(\"Greens\", gene)\n",
"\n",
"# Interpolate the gene data onto the mesh, by averaging the 5 closest points\n",
"msh.interpolate_data_from(points, n=5).cmap(\"Greens\").add_scalarbar()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Create a graph of the gene expression as function of x-position\n",
"gene_plot = plot(x, gene, lw=0, title=\"Gene expression\").as2d(scale=0.5)\n",
"\n",
"# Show the mesh, the points and the graph\n",
"show(msh, points, gene_plot).close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}