1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
|
"""Built-in examples that ship with PyVista and do not need to be downloaded.
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.load_ant()
>>> mesh.plot()
"""
from __future__ import annotations
import math
import os
from pathlib import Path
import numpy as np
import pyvista
from pyvista.examples._dataset_loader import _DatasetLoader
from pyvista.examples._dataset_loader import _SingleFileDownloadableDatasetLoader
# get location of this folder and the example files
dir_path = str(Path(os.path.realpath(__file__)).parent)
antfile = str(Path(dir_path) / 'ant.ply')
planefile = str(Path(dir_path) / 'airplane.ply')
hexbeamfile = str(Path(dir_path) / 'hexbeam.vtk')
spherefile = str(Path(dir_path) / 'sphere.ply')
uniformfile = str(Path(dir_path) / 'uniform.vtk')
rectfile = str(Path(dir_path) / 'rectilinear.vtk')
globefile = str(Path(dir_path) / 'globe.vtk')
mapfile = str(Path(dir_path) / '2k_earth_daymap.jpg')
channelsfile = str(Path(dir_path) / 'channels.vti')
logofile = str(Path(dir_path) / 'pyvista_logo.png')
nutfile = str(Path(dir_path) / 'nut.ply')
frogtissuesfile = str(Path(dir_path) / 'frog_tissues.vti')
def load_ant():
"""Load ply ant mesh.
Returns
-------
pyvista.PolyData
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_ant()
>>> dataset.plot()
.. seealso::
:ref:`Ant Dataset <ant_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_ant.load()
_dataset_ant = _SingleFileDownloadableDatasetLoader(antfile, read_func=pyvista.PolyData) # type: ignore[arg-type]
def load_airplane():
"""Load ply airplane mesh.
Returns
-------
pyvista.PolyData
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_airplane()
>>> dataset.plot()
.. seealso::
:ref:`Airplane Dataset <airplane_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_airplane.load()
_dataset_airplane = _SingleFileDownloadableDatasetLoader(planefile, read_func=pyvista.PolyData) # type: ignore[arg-type]
def load_sphere():
"""Load sphere ply mesh.
Returns
-------
pyvista.PolyData
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_sphere()
>>> dataset.plot()
.. seealso::
:ref:`Sphere Dataset <sphere_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_sphere.load()
_dataset_sphere = _SingleFileDownloadableDatasetLoader(spherefile, read_func=pyvista.PolyData) # type: ignore[arg-type]
def load_uniform():
"""Load a sample uniform grid.
Returns
-------
pyvista.ImageData
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_uniform()
>>> dataset.plot()
.. seealso::
:ref:`Uniform Dataset <uniform_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_uniform.load()
_dataset_uniform = _SingleFileDownloadableDatasetLoader(uniformfile, read_func=pyvista.ImageData) # type: ignore[arg-type]
def load_rectilinear():
"""Load a sample uniform grid.
Returns
-------
pyvista.RectilinearGrid
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_rectilinear()
>>> dataset.plot()
.. seealso::
:ref:`Rectilinear Dataset <rectilinear_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_rectilinear.load()
_dataset_rectilinear = _SingleFileDownloadableDatasetLoader(
rectfile,
read_func=pyvista.RectilinearGrid, # type: ignore[arg-type]
)
def load_hexbeam():
"""Load a sample UnstructuredGrid.
Returns
-------
pyvista.UnstructuredGrid
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_hexbeam()
>>> dataset.plot()
.. seealso::
:ref:`Hexbeam Dataset <hexbeam_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_hexbeam.load()
_dataset_hexbeam = _SingleFileDownloadableDatasetLoader(
hexbeamfile,
read_func=pyvista.UnstructuredGrid, # type: ignore[arg-type]
)
def load_tetbeam():
"""Load a sample UnstructuredGrid containing only tetrahedral cells.
Returns
-------
pyvista.UnstructuredGrid
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_tetbeam()
>>> dataset.plot()
.. seealso::
:ref:`Tetbeam Dataset <tetbeam_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_tetbeam.load()
def _tetbeam_load_func():
# make the geometry identical to the hexbeam
xrng = np.linspace(0, 1, 3)
yrng = np.linspace(0, 1, 3)
zrng = np.linspace(0, 5, 11)
grid = pyvista.RectilinearGrid(xrng, yrng, zrng)
return grid.to_tetrahedra()
_dataset_tetbeam = _DatasetLoader(_tetbeam_load_func)
def load_structured():
"""Load a simple StructuredGrid.
Returns
-------
pyvista.StructuredGrid
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_structured()
>>> dataset.plot()
.. seealso::
:ref:`Structured Dataset <structured_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_structured.load()
def _structured_load_func():
x = np.arange(-10, 10, 0.25)
y = np.arange(-10, 10, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)
return pyvista.StructuredGrid(x, y, z)
_dataset_structured = _DatasetLoader(_structured_load_func)
def load_globe():
"""Load a globe source.
Returns
-------
pyvista.PolyData
Globe dataset with earth texture.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_globe()
>>> texture = examples.load_globe_texture()
>>> dataset.plot(texture=texture)
.. seealso::
:ref:`Globe Dataset <globe_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_globe.load()
_dataset_globe = _SingleFileDownloadableDatasetLoader(globefile, read_func=pyvista.PolyData) # type: ignore[arg-type]
def load_globe_texture():
"""Load a pyvista.Texture that can be applied to the globe source.
Returns
-------
pyvista.Texture
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_globe_texture()
>>> dataset.plot()
.. seealso::
:ref:`Globe Texture Dataset <globe_texture_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_globe_texture.load()
_dataset_globe_texture = _SingleFileDownloadableDatasetLoader(
mapfile,
read_func=pyvista.read_texture, # type: ignore[arg-type]
)
def load_channels():
"""Load a uniform grid of fluvial channels in the subsurface.
Returns
-------
pyvista.ImageData
Dataset.
Examples
--------
>>> from pyvista import examples
>>> dataset = examples.load_channels()
>>> dataset.plot()
.. seealso::
:ref:`Channels Dataset <channels_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_channels.load()
_dataset_channels = _SingleFileDownloadableDatasetLoader(channelsfile)
def load_spline():
"""Load an example spline mesh.
This example data was created with:
.. code-block:: python
>>> import numpy as np
>>> import pyvista as pv
>>> theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
>>> z = np.linspace(-2, 2, 100)
>>> r = z**2 + 1
>>> x = r * np.sin(theta)
>>> y = r * np.cos(theta)
>>> points = np.column_stack((x, y, z))
>>> mesh = pv.Spline(points, 1000)
Returns
-------
pyvista.PolyData
Spline mesh.
Examples
--------
>>> from pyvista import examples
>>> spline = examples.load_spline()
>>> spline.plot()
.. seealso::
:ref:`Spline Dataset <spline_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_spline.load()
def _spline_load_func():
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
points = np.column_stack((x, y, z))
return pyvista.Spline(points, 1000)
_dataset_spline = _DatasetLoader(_spline_load_func)
def load_random_hills():
"""Create random hills toy example.
Uses the parametric random hill function to create hills oriented
like topography and adds an elevation array.
This example dataset was created with:
.. code-block:: python
>>> mesh = pv.ParametricRandomHills() # doctest:+SKIP
>>> mesh = mesh.elevation() # doctest:+SKIP
Returns
-------
pyvista.PolyData
Random hills mesh.
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.load_random_hills()
>>> mesh.plot()
.. seealso::
:ref:`Random Hills Dataset <random_hills_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_random_hills.load()
def _random_hills_load_func():
mesh = pyvista.ParametricRandomHills()
return mesh.elevation()
_dataset_random_hills = _DatasetLoader(_random_hills_load_func)
def load_sphere_vectors():
"""Create example sphere with a swirly vector field defined on nodes.
Returns
-------
pyvista.PolyData
Mesh containing vectors.
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.load_sphere_vectors()
>>> mesh.point_data
pyvista DataSetAttributes
Association : POINT
Active Scalars : vectors
Active Vectors : vectors
Active Texture : None
Active Normals : Normals
Contains arrays :
Normals float32 (842, 3) NORMALS
vectors float32 (842, 3) VECTORS
.. seealso::
:ref:`Sphere Vectors Dataset <sphere_vectors_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_sphere_vectors.load()
def _sphere_vectors_load_func() -> pyvista.PolyData:
sphere = pyvista.Sphere(radius=math.pi)
# make cool swirly pattern
vectors = np.vstack(
(
np.sin(sphere.points[:, 0]),
np.cos(sphere.points[:, 1]),
np.cos(sphere.points[:, 2]),
),
).T
# add and scale
sphere['vectors'] = vectors * 0.3
sphere.set_active_vectors('vectors')
return sphere
_dataset_sphere_vectors = _DatasetLoader(_sphere_vectors_load_func)
def load_explicit_structured(dimensions=(5, 6, 7), spacing=(20, 10, 1)):
"""Load a simple explicit structured grid.
Parameters
----------
dimensions : tuple(int), optional
Grid dimensions. Default is (5, 6, 7).
spacing : tuple(int), optional
Grid spacing. Default is (20, 10, 1).
Returns
-------
pyvista.ExplicitStructuredGrid
An explicit structured grid.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid.plot(show_edges=True)
.. seealso::
:ref:`Explicit Structured Dataset <explicit_structured_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_explicit_structured.load(dimensions=dimensions, spacing=spacing)
def _explicit_structured_load_func(dimensions=(5, 6, 7), spacing=(20, 10, 1)):
ni, nj, nk = np.asarray(dimensions) - 1
si, sj, sk = spacing
xi = np.arange(0.0, (ni + 1) * si, si)
yi = np.arange(0.0, (nj + 1) * sj, sj)
zi = np.arange(0.0, (nk + 1) * sk, sk)
return pyvista.StructuredGrid(
*np.meshgrid(xi, yi, zi, indexing='ij')
).cast_to_explicit_structured_grid()
_dataset_explicit_structured = _DatasetLoader(_explicit_structured_load_func)
def load_nut():
"""Load an example nut mesh.
Returns
-------
pyvista.PolyData
A sample nut surface dataset.
Examples
--------
Load an example nut and plot with smooth shading.
>>> from pyvista import examples
>>> mesh = examples.load_nut()
>>> mesh.plot(smooth_shading=True, split_sharp_edges=True)
.. seealso::
:ref:`Nut Dataset <nut_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_nut.load()
_dataset_nut = _SingleFileDownloadableDatasetLoader(nutfile)
def load_hydrogen_orbital(n=1, l=0, m=0, zoom_fac=1.0): # noqa: PLR0917
"""Load the hydrogen wave function for a :class:`pyvista.ImageData`.
This is the solution to the Schrödinger equation for hydrogen
evaluated in three-dimensional Cartesian space.
Inspired by `Hydrogen Wave Function
<http://staff.ustc.edu.cn/~zqj/posts/Hydrogen-Wavefunction/>`_.
Parameters
----------
n : int, default: 1
Principal quantum number. Must be a positive integer. This is often
referred to as the "energy level" or "shell".
l : int, default: 0
Azimuthal quantum number. Must be a non-negative integer strictly
smaller than ``n``. By convention this value is represented by the
letters s, p, d, f, etc.
m : int, default: 0
Magnetic quantum number. Must be an integer ranging from ``-l`` to
``l`` (inclusive). This is the orientation of the angular momentum in
space.
zoom_fac : float, default: 1.0
Zoom factor for the electron cloud. Increase this value to focus on the
center of the electron cloud.
Returns
-------
pyvista.ImageData
ImageData containing two ``point_data`` arrays:
* ``'real_wf'`` - Real part of the wave function.
* ``'wf'`` - Complex wave function.
Notes
-----
This example requires `sympy <https://www.sympy.org/>`_.
Examples
--------
Plot the 3dxy orbital of a hydrogen atom. This corresponds to the quantum
numbers ``n=3``, ``l=2``, and ``m=-2``.
>>> from pyvista import examples
>>> grid = examples.load_hydrogen_orbital(3, 2, -2)
>>> grid.plot(volume=True, opacity=[1, 0, 1], cmap='magma')
See :ref:`atomic_orbitals_example` for additional examples using
this function.
.. seealso::
:ref:`Hydrogen Orbital Dataset <hydrogen_orbital_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_hydrogen_orbital.load(n=n, l=l, m=m, zoom_fac=zoom_fac)
def _hydrogen_orbital_load_func(n=1, l=0, m=0, zoom_fac=1.0): # noqa: PLR0917
try:
from sympy import lambdify
from sympy.abc import phi
from sympy.abc import r
from sympy.abc import theta
from sympy.physics.hydrogen import Psi_nlm
except ImportError: # pragma: no cover
msg = '\n\nInstall sympy to run this example. Run:\n\n pip install sympy\n'
raise ImportError(msg) from None
if n < 1:
msg = '`n` must be at least 1'
raise ValueError(msg)
if l not in range(n):
msg = f'`l` must be one of: {list(range(n))}'
raise ValueError(msg)
if m not in range(-l, l + 1):
msg = f'`m` must be one of: {list(range(-l, l + 1))}'
raise ValueError(msg)
psi = lambdify((r, phi, theta), Psi_nlm(n, l, m, r, phi, theta, 1), 'numpy')
org = 1.5 * n**2 + 1.0 if n == 1 else 1.5 * n**2 + 10.0
org /= zoom_fac
dim = 100
sp = (org * 2) / (dim - 1)
grid = pyvista.ImageData(
dimensions=(dim, dim, dim),
spacing=(sp, sp, sp),
origin=(-org, -org, -org),
)
r, theta, phi = pyvista.cartesian_to_spherical(grid.x, grid.y, grid.z)
wfc = psi(r, phi, theta).reshape(grid.dimensions)
grid['real_wf'] = np.real(wfc.ravel())
grid['wf'] = wfc.ravel()
return grid
_dataset_hydrogen_orbital = _DatasetLoader(_hydrogen_orbital_load_func)
def load_logo():
"""Load the PyVista logo as a :class:`pyvista.ImageData`.
.. note::
Alternative versions of the logo file are also available from the ``logo``
directory at https://github.com/pyvista/pyvista/. This includes
higher-resolution ``.png`` files and a vectorized ``.svg`` version.
.. versionchanged:: 0.45
The dimensions of the image is now ``1389 x 592``.
Previously, it was ``1920 x 718``.
Returns
-------
pyvista.ImageData
ImageData of the PyVista logo.
Examples
--------
>>> from pyvista import examples
>>> image = examples.load_logo()
>>> image.dimensions
(1389, 592, 1)
>>> image.plot(cpos='xy', zoom='tight', rgb=True, show_axes=False)
.. seealso::
:ref:`Logo Dataset <logo_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _dataset_logo.load()
_dataset_logo = _SingleFileDownloadableDatasetLoader(logofile)
def load_frog_tissues():
"""Load frog tissues dataset.
This dataset contains tissue segmentation labels for the frog dataset.
.. versionadded:: 0.44.0
Returns
-------
pyvista.ImageData
Dataset.
Examples
--------
Load data
>>> import numpy as np
>>> import pyvista as pv
>>> from pyvista import examples
>>> data = examples.load_frog_tissues()
Plot tissue labels as a volume
First, define plotting parameters
>>> # Configure colors / color bar
>>> clim = data.get_data_range() # Set color bar limits to match data
>>> cmap = 'glasbey' # Use a categorical colormap
>>> categories = True # Ensure n_colors matches number of labels
>>> opacity = 'foreground' # Make foreground opaque, background transparent
>>> opacity_unit_distance = 1
Set plotting resolution to half the image's spacing
>>> res = np.array(data.spacing) / 2
Define rendering parameters
>>> mapper = 'gpu'
>>> shade = True
>>> ambient = 0.3
>>> diffuse = 0.6
>>> specular = 0.5
>>> specular_power = 40
Make and show plot
>>> p = pv.Plotter()
>>> _ = p.add_volume(
... data,
... clim=clim,
... ambient=ambient,
... shade=shade,
... diffuse=diffuse,
... specular=specular,
... specular_power=specular_power,
... mapper=mapper,
... opacity=opacity,
... opacity_unit_distance=opacity_unit_distance,
... categories=categories,
... cmap=cmap,
... resolution=res,
... )
>>> p.camera_position = 'yx' # Set camera to provide a dorsal view
>>> p.show()
.. seealso::
:ref:`Frog Tissues Dataset <frog_tissues_dataset>`
See this dataset in the Dataset Gallery for more info.
:ref:`Frog Dataset <frog_dataset>`
:ref:`medical_dataset_gallery`
Browse other medical datasets.
"""
return _dataset_frog_tissues.load()
_dataset_frog_tissues = _SingleFileDownloadableDatasetLoader(frogtissuesfile)
|