GithubHelp home page GithubHelp logo

pyvista / pyvista-xarray Goto Github PK

View Code? Open in Web Editor NEW
96.0 13.0 6.0 54.44 MB

xarray DataArray accessors for PyVista

License: Apache License 2.0

Python 98.57% Dockerfile 0.75% Shell 0.68%
3d-visualization pyvista xarray-accessor hacktoberfest

pyvista-xarray's Introduction

PyVista xarray

PyPI codecov MyBinder

xarray DataArray accessors for PyVista to visualize datasets in 3D

๐Ÿš€ Usage

You must import pvxarray in order to register the DataArray accessor with xarray. After which, a pyvista namespace of accessors will be available.

Try on MyBinder: https://mybinder.org/v2/gh/pyvista/pyvista-xarray/HEAD

The following is an example to visualize a RectilinearGrid with PyVista:

import pvxarray
import xarray as xr

ds = xr.tutorial.load_dataset("air_temperature")
da = ds.air[dict(time=0)]  # Select DataArray for a timestep

# Plot in 3D
da.pyvista.plot(x="lon", y="lat", show_edges=True, cpos='xy')

# Or grab the mesh object for use with PyVista
mesh = da.pyvista.mesh(x="lon", y="lat")

air_temperature

Or you can read VTK meshes directly to xarray:

import xarray as xr

ds = xr.open_dataset("data.vtk", engine="pyvista")
ds["data array"].pyvista.plot(x="x", y="y", z="z")

โฌ‡๏ธ Installation

๐Ÿ Installing with conda

Conda makes managing pyvista-xarray's dependencies across platforms quite easy and this is the recommended method to install:

conda install -c conda-forge pyvista-xarray

๐ŸŽก Installing with pip

If you prefer pip, then you can install from PyPI: https://pypi.org/project/pyvista-xarray/

pip install pyvista-xarray

Upstream Work

Many of the examples leverage brand new features in PyVista v0.38.1 and GeoVista which may not yet be released when you're reading this. Here is a list of pull requests needed for some of the examples:

Work that was required and merged:

๐Ÿ’ญ Feedback

Please share your thoughts and questions on the Discussions board. If you would like to report any bugs or make feature requests, please open an issue.

If filing a bug report, please share a scooby Report:

import pvxarray
print(pvxarray.Report())

๐Ÿ Further Examples

The following are a few simple examples taken from the xarray and rioxarray documentation. There are also more sophisticated examples in the examples/ directory in this repository.

Simple RectilinearGrid

import numpy as np
import pvxarray
import xarray as xr

lon = np.array([-99.83, -99.32])
lat = np.array([42.25, 42.21])
z = np.array([0, 10])
temp = 15 + 8 * np.random.randn(2, 2, 2)

ds = xr.Dataset(
    {
        "temperature": (["z", "x", "y"], temp),
    },
    coords={
        "lon": (["x"], lon),
        "lat": (["y"], lat),
        "z": (["z"], z),
    },
)

mesh = ds.temperature.pyvista.mesh(x="lon", y="lat", z="z")
mesh.plot()

Raster with rioxarray

import pvxarray
import rioxarray
import xarray as xr

da = rioxarray.open_rasterio("TC_NG_SFBay_US_Geo_COG.tif")
da = da.rio.reproject("EPSG:3857")

# Grab the mesh object for use with PyVista
mesh = da.pyvista.mesh(x="x", y="y", component="band")

mesh.plot(scalars="data", cpos='xy', rgb=True)

raster

import pvxarray
import rioxarray

da = rioxarray.open_rasterio("Elevation.tif")
da = da.rio.reproject("EPSG:3857")

# Grab the mesh object for use with PyVista
mesh = da.pyvista.mesh(x="x", y="y")

# Warp top and plot in 3D
mesh.warp_by_scalar().plot()

topo

StructuredGrid

import pvxarray
import pyvista as pv
import xarray as xr

ds = xr.tutorial.open_dataset("ROMS_example.nc", chunks={"ocean_time": 1})

if ds.Vtransform == 1:
    Zo_rho = ds.hc * (ds.s_rho - ds.Cs_r) + ds.Cs_r * ds.h
    z_rho = Zo_rho + ds.zeta * (1 + Zo_rho / ds.h)
elif ds.Vtransform == 2:
    Zo_rho = (ds.hc * ds.s_rho + ds.Cs_r * ds.h) / (ds.hc + ds.h)
    z_rho = ds.zeta + (ds.zeta + ds.h) * Zo_rho

ds.coords["z_rho"] = z_rho.transpose()  # needing transpose seems to be an xarray bug

da = ds.salt[dict(ocean_time=0)]

# Make array ordering consistent
da = da.transpose("s_rho", "xi_rho", "eta_rho", transpose_coords=False)

# Grab StructuredGrid mesh
mesh = da.pyvista.mesh(x="lon_rho", y="lat_rho", z="z_rho")

# Plot in 3D
p = pv.Plotter()
p.add_mesh(mesh, lighting=False, cmap='plasma', clim=[0, 35])
p.view_vector([1, -1, 1])
p.set_scale(zscale=0.001)
p.show()

raster

pyvista-xarray's People

Contributors

annehaley avatar banesullivan avatar pre-commit-ci[bot] avatar richardscottoz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyvista-xarray's Issues

Support 1D

2D and 3D rectilinear grids are supported, 1D should be as well

Potential bug in setting axes for data array (XYZ=ZYX)

Was running into an issue with a particular DataArray where:

mesh = ds.data.pyvista.mesh(x='x', y='y', z='z')

and

mesh = ds.data.pyvista.mesh(x='z', y='y', z='x')

were yielding the same result.

This is unexpected and I think the DataArray wasn't/isn't being reorder properly

Does pyvista/trame require ipywidgets? Also, should we use trame-jupyter-extension by default?

When I install from the pyvista-xarray source and run the examples, Jupyter displays a static image and a warning that it "Failed to use notebook backend" and instructs me to install ipywidgets. Screenshot is below.

  • Installing ipywidgets fixes the problem. Is there something in our code that requires ipython?
  • Separate from that: should we include trame-jupyter-extension in the examples/requirements.txt?, My understanding is that the extension is only needed when using a remote Jupyter server on a host with port constraints, but perhaps we should standardize on including the extension by default?
  • Oh, and because I just noticed, I think we also need to update the examples/requirements.txt file to change pyvista to pyvista[trame].

image

import pvxarray fails

After a pip install, I tried the very simple example in the readme. When trying to import pvxarray I get the following error:

DLL load failed while importing vtkIONetCDF: The specified procedure could not be found.

Here's the code up to the point where it fails:
import pyvista as pv
import numpy as np
import xarray as xr
import pvxarray

Audit DataSet engine

Make sure data are being generated efficiently and not copy data unnecessarily

Entrypoint?

Can/should we add an entrypoint so that the accessors in this package are always registered with xarray making it so import pvxarray is not required

vtk data array not included in xarray

image

image

image

image

pyvista                   0.38.5             pyhd8ed1ab_0    conda-forge
pyvista-xarray            0.1.3              pyhd8ed1ab_0    conda-forge
pywin32                   304              py39h99910a6_2    conda-forge
pywinpty                  2.0.10           py39h99910a6_0    conda-forge
pyyaml                    6.0              py39ha55989b_5    conda-forge
pyzmq                     25.0.2           py39hea35a22_0    conda-forge
qt-main                   5.15.8              h9e85ed6_18    conda-forge
qtconsole                 5.4.1              pyhd8ed1ab_0    conda-forge
qtconsole-base            5.4.1              pyha770c72_0    conda-forge
qtpy                      2.3.0              pyhd8ed1ab_0    conda-forge
random-user-agent         1.0.1                    pypi_0    pypi
rasterio                  1.3.9            py39hdc4e632_0    conda-forge
re2                       2023.02.02           h63175ca_0    conda-forge
reproc                    14.2.4               hcfcfb64_0    conda-forge
reproc-cpp                14.2.4               h63175ca_0    conda-forge
requests                  2.28.2             pyhd8ed1ab_0    conda-forge
rfc3339-validator         0.1.4              pyhd8ed1ab_0    conda-forge
rfc3986-validator         0.1.1              pyh9f0ad1d_0    conda-forge
richardutils              0.1.dev129+gea92c2c          pypi_0    pypi
rioxarray                 0.14.0             pyhd8ed1ab_1    conda-forge
rtfde                     0.0.2              pyhd8ed1ab_1    conda-forge
rtree                     1.0.1            py39h09fdee3_1    conda-forge
ruamel.yaml               0.17.32          py39ha55989b_0    conda-forge
ruamel.yaml.clib          0.2.7            py39ha55989b_1    conda-forge
s3fs                      2023.3.0           pyhd8ed1ab_0    conda-forge
s3transfer                0.6.0              pyhd8ed1ab_0    conda-forge
scikit-learn              1.2.2            py39hb6325b5_1    conda-forge
scipy                     1.10.1           py39hfbf2dce_0    conda-forge
scooby                    0.7.1              pyhd8ed1ab_0    conda-forge
selenium                  3.141.0         py39hb82d6ee_1003    conda-forge
send2trash                1.8.0              pyhd8ed1ab_0    conda-forge
setuptools                65.6.3           py39haa95532_0  
shapely                   2.0.2            py39h61a8cf5_1    conda-forge
sip                       6.7.7            py39h99910a6_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
snappy                    1.1.10               hfb803bf_0    conda-forge
sniffio                   1.3.0              pyhd8ed1ab_0    conda-forge
snowballstemmer           2.2.0              pyhd8ed1ab_0    conda-forge
snuggs                    1.4.7                      py_0    conda-forge
sortedcontainers          2.4.0              pyhd8ed1ab_0    conda-forge
soupsieve                 2.3.2.post1        pyhd8ed1ab_0    conda-forge
speechrecognition         3.10.0             pyhd8ed1ab_0    conda-forge
sphinx                    5.3.0              pyhd8ed1ab_0    conda-forge
sphinx-basic-ng           1.0.0b1            pyhd8ed1ab_1    conda-forge
sphinx-inline-tabs        2023.4.21          pyhd8ed1ab_0    conda-forge
sphinxcontrib-apidoc      0.3.0                      py_1    conda-forge
sphinxcontrib-applehelp   1.0.4              pyhd8ed1ab_0    conda-forge
sphinxcontrib-devhelp     1.0.2                      py_0    conda-forge
sphinxcontrib-htmlhelp    2.0.1              pyhd8ed1ab_0    conda-forge
sphinxcontrib-jsmath      1.0.1                      py_0    conda-forge
sphinxcontrib-qthelp      1.0.3                      py_0    conda-forge
sphinxcontrib-serializinghtml 1.1.5              pyhd8ed1ab_2    conda-forge
sqlalchemy                2.0.17           py39ha55989b_0    conda-forge
sqlite                    3.41.1               h2bbff1b_0  
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
svt-av1                   1.7.0                h63175ca_0    conda-forge
tabulate                  0.9.0              pyhd8ed1ab_1    conda-forge
tbb                       2021.11.0            h91493d7_1    conda-forge
tbb-devel                 2021.11.0            h3ec46f0_1    conda-forge
tblib                     1.7.0              pyhd8ed1ab_0    conda-forge
terminado                 0.15.0           py39hcbf5309_0    conda-forge
textract                  1.6.5              pyhd8ed1ab_0    conda-forge
threadpoolctl             3.1.0              pyh8a188c0_0    conda-forge
tiledb                    2.18.2               hc200977_1    conda-forge
tinycss2                  1.2.1              pyhd8ed1ab_0    conda-forge
tk                        8.6.13               h5226925_1    conda-forge
toml                      0.10.2             pyhd8ed1ab_0    conda-forge
toolz                     0.12.0             pyhd8ed1ab_0    conda-forge
tornado                   6.2              py39ha55989b_1    conda-forge
tqdm                      4.65.0             pyhd8ed1ab_1    conda-forge
traitlets                 5.9.0              pyhd8ed1ab_0    conda-forge
traittypes                0.2.1              pyh9f0ad1d_2    conda-forge
trame                     2.5.0              pyhd8ed1ab_1    conda-forge
trame-client              2.9.4              pyhd8ed1ab_0    conda-forge
trame-components          2.1.1              pyhd8ed1ab_0    conda-forge
trame-deckgl              2.0.2              pyhd8ed1ab_0    conda-forge
trame-markdown            2.0.2              pyhd8ed1ab_0    conda-forge
trame-matplotlib          2.0.2              pyhd8ed1ab_0    conda-forge
trame-plotly              2.1.1              pyhd8ed1ab_0    conda-forge
trame-rca                 0.3.1              pyhd8ed1ab_0    conda-forge
trame-router              2.0.2              pyhd8ed1ab_0    conda-forge
trame-server              2.11.5             pyhd8ed1ab_0    conda-forge
trame-simput              2.3.2              pyhd8ed1ab_0    conda-forge
trame-vega                2.0.3              pyhd8ed1ab_0    conda-forge
trame-vtk                 2.5.4              pyhd8ed1ab_0    conda-forge
trame-vuetify             2.3.1              pyhd8ed1ab_0    conda-forge
typing-extensions         4.5.0                hd8ed1ab_0    conda-forge
typing_extensions         4.5.0              pyha770c72_0    conda-forge
tzdata                    2022g                h04d1e81_0  
tzlocal                   4.3              py39hcbf5309_0    conda-forge
ucrt                      10.0.22621.0         h57928b3_0    conda-forge
unicodedata2              15.0.0           py39ha55989b_0    conda-forge
urllib3                   1.26.15            pyhd8ed1ab_0    conda-forge
utfcpp                    3.2.3                h57928b3_0    conda-forge
vc                        14.2                 h21ff451_1  
vc14_runtime              14.34.31931         h5081d32_13    conda-forge
vectormath                0.2.2                    pypi_0    pypi
vs2015_runtime            14.34.31931         h4c5c07a_10    conda-forge
vtk                       9.2.6           qt_py39h1234567_218    conda-forge
vtk-base                  9.2.6           qt_py39h1234567_218    conda-forge
wcwidth                   0.2.6              pyhd8ed1ab_0    conda-forge
webencodings              0.5.1                      py_1    conda-forge
websocket-client          1.5.1              pyhd8ed1ab_0    conda-forge
wheel                     0.38.4           py39haa95532_0  
widgetsnbextension        4.0.7              pyhd8ed1ab_0    conda-forge
win32_setctime            1.1.0              pyhd8ed1ab_0    conda-forge
win_inet_pton             1.1.0              pyhd8ed1ab_6    conda-forge
win_unicode_console       0.5             py39hcbf5309_1003    conda-forge
wincertstore              0.2              py39haa95532_2  
winpty                    0.4.3                         4    conda-forge
wrapt                     1.15.0           py39ha55989b_0    conda-forge
wslink                    1.10.1             pyhd8ed1ab_0    conda-forge
x264                      1!164.3095           h8ffe710_2    conda-forge
x265                      3.5                  h2d74725_3    conda-forge
xarray                    2023.3.0           pyhd8ed1ab_0    conda-forge

Component array ordering flaky behavior

One of the Binder examples is not working as expected.

import rioxarray

da = rioxarray.open_rasterio("bahamas_rgb.tif")
da = da.rio.reproject("EPSG:3857")

# Grab the mesh object for use with PyVista
mesh = da.pyvista.mesh(x="x", y="y", component="band")

# plot in 3D
mesh.plot(rgb=True, cpos="xy")

image

add more file extension handlers

the dataset backend here uses pyvista.read() to load files supported by PyVista/VTK. Currently these are limited to vti, vts, and vtr files but any formats the generate a vtkRectilinearGrid, vtkImageData, or vtkStructuredGrid are supported.

This dataset engine should list any file extensions supported by PyVista that will generate those data types. I'm wondering if we could have PyVista list these extensions upstream?

See https://github.com/pyvista/pyvista/blob/75d51c6c1167137051b34c7acb8286c3df8eb926/pyvista/utilities/reader.py#L2081

Animated plots exported as gifs?

A gif is worth 1000 plots - can we make them via pyvista-xarray?

I saw this pyvista example and immediately thought about plotting xarray data that has a 'time' dimension as an animated 3D plot over time. Can we just wrap similar code to this example inside the pyvista accessor?

cc @jbusecke

module 'numpy' has no attribute 'bool'

I have this error though I use the latest np version 1.24.2:

np.bool was a deprecated alias for the builtin bool. To avoid this error in existing code, use bool by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.bool_ here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

Memory sharing issues after PyVista 0.37 for RectilinearGrid

See 0e64e7d

pyvista/pyvista#3179 broke the test_convert_vtr test when it validates that the X and Y coordinates share memory but not Z. This is super odd and I cannot figure out what is going wrong as the changes in pyvista/pyvista#3179 do not seemingly affect this. So for some reason X and Y are DEEP copied now while Z is still SHALLOW copied.

@MatthewFlamm, I hate to throw you in the deep end but I'm wondering if you might have any insights come to mind as to why that pull request would have broken this test.

proposed update to make pyvista-xarray + trame-jupyter-extension working on headless server

Hi,

First of all thank you very much for this PyVista plugin, it gives so much opportunities to render xarray datasets !

As described in this thread I am experiencing issues to make pyvista-xarray working on an headless server with trame-jupyter-extension: Kitware/trame-jupyter-extension#4

  • It works inside a Trame application using await ui
  • It does not work using the default da.pyvista.plot()

I have installed a TLJH server (following OVH howto from tljh website).
The server has no X installed.

So in order to get pyvista working on IT, I have installed pyvista + vtk-osmesa (off screen mesa) as described below:

sudo /opt/tljh/user/bin/mamba install -c conda-forge xarray dask datashader jupyterlab
sudo /opt/tljh/user/bin/pip install "pyvista[jupyter]" pyvista-xarray  # do not forget [jupyter]
sudo /opt/tljh/user/bin/pip uninstall vtk                                                # remove VTK that leverage X for its rendering
sudo /opt/tljh/user/bin/pip install --extra-index-url https://wheels.vtk.org vtk-osmesa

With this installation, on my remote server I can run the cone application successfuly :

from trame.app.demo import Cone

app = Cone()
await app.ui.ready
app.ui

But this code still produce a 404 error:

import pvxarray
import xarray as xr

ds = xr.tutorial.load_dataset("air_temperature")
da = ds.air[dict(time=0)]  # Select DataArray for a timestep
da.pyvista.plot(x="lon", y="lat", show_edges=True, cpos='xy')

with the missing url having this face:

 404 GET /user/manager/proxy/40789/index.html?ui=P_0x7fa2d4083760_4&reconnect=auto (manager@<ip>)

So is it possible to make the da.pyvista.plot working out of the box in this configuration ?

--------------------------------------------------------------------------------
  Date: Mon Jan 01 09:12:02 2024 UTC

                OS : Linux
            CPU(s) : 48
           Machine : x86_64
      Architecture : 64bit
               RAM : 251.7 GiB
       Environment : Jupyter
       File system : ext4

  Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06)
  [GCC 11.3.0]

           pyvista : 0.43.1
            xarray : 2023.12.0
            scooby : 0.9.2
               vtk : 9.3.0
             numpy : 1.26.2
              dask : 2023.12.1
--------------------------------------------------------------------------------

conda-forge

Nice package! I would like to integrate it with leafmap. Have you considered getting the package on conda-forge? I would be happy to get it on conda-forge and then transfer ownership to you. Let me know.

Nodata issue

I am trying to display an elevation dataset, but it seems pyvista-xarray somehow uses the nodata value as the minimum value to render the data, making it look like a long vertical column. Is it possible to specify a minimum value to prevent pyxarray from using the nodata value for rendering?

import rioxarray

da = rioxarray.open_rasterio("madison.tif")
da = da.rio.reproject("EPSG:3857")

# Grab the mesh object for use with PyVista
mesh = da[dict(band=0)].pyvista.mesh(x="x", y="y")

# plot in 3D
mesh.warp_by_scalar().plot()

image

Rendering the original lidar point cloud

import leafmap
leafmap.view_lidar('madison.las', cmap='terrain')

image

Support cell data

Write a filter that will switch cell/point data and coordinates if user wants the data arrays to live on cells and coords to represent cell centers

3d viz of rectilinear gridded data - only edges of plot showing values

I was trying to follow one of your examples with my own data, but for some reason only data on the edge of the domain is showing in the plot. I plotted cross sections with xarray plotting functions as a sanity check. The data is there in the original xarray dataset and subsequent dataarray. Any pointers would be appreciated!

Code to reproduce error:

import pyvista as pv
import pvxarray
import xarray as xr

file_path = 'path/to/file/cm1out_000030.nc' # edit path here
ds = xr.open_dataset(file_path)
ds = ds.squeeze('time')
da = ds.qi4
mesh = da.pyvista.mesh(x="xh", y="yh", z="zh")
mesh.plot(opacity='linear', cmap='bone')
image

The file is too big to upload here.
So here's a link to it in google drive: https://drive.google.com/file/d/144WYF4Q9WSGC1JW7Qw5OTjlRPyjMatqq/view?usp=sharing


Date: Wed Oct 25 16:42:08 2023 EDT

            OS : Darwin
        CPU(s) : 10
       Machine : arm64
  Architecture : 64bit
           RAM : 16.0 GiB
   Environment : Jupyter
   File system : apfs

Python 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:26:40)
[Clang 14.0.6 ]

       pyvista : 0.42.3
        xarray : 2023.10.1
        scooby : 0.7.1
           vtk : 9.2.6
         numpy : 1.26.0
       netcdf4 : 1.6.4

Add a binder link?

I have told a bunch of people about pyvista-xarray today. Everyone is excited! ๐ŸŽ‰

If you could create a binder, folks could try it out more easily. This would probably help motivate more users and contributors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.