Skip to content

Feature Proposal: xarray.interactive module #3709

Closed
@TomNicholas

Description

@TomNicholas

Feature proposal: xarray.interactive module

I've been experimenting with ipython widgets in jupyter notebooks, and I've been working on how we might use them to make xarray more interactive.

Motivation:

For most users who are exploring their data, it will be common to find themselves rerunning the same cells repeatedly but with slightly different values.
In xarray's case that will often be in an .isel() or .sel() call, or selecting variables from a dataset.
IPython widgets allow you to interact with your functions in a very intuitive way, which we could exploit.
There are lots of tutorials on how to interact with pandas data (e.g. this great one), but I haven't seen any for interacting with xarray objects.

Relationship to other libraries:

Some downstream plotting libaries (such as @hvplot) already use widgets when interactively plotting xarray-derived data structures, but they don't seem to go the full N dimensions.
This also isn't something that should be confined to plotting functions - you often choose slices or variables at the start of analysis, not just at the end.
I'll come back to this idea later.

The default ipython widgets are pretty good, but we could write an xarray.interactive module in such a way that downstream developers can easily replace them with their own widgets.

Usage examples:

# imports
import ipywidgets as widgets
import xarray.plot as xplot
import xarray.interactive as interactive

# Load tutorial data
ds = xr.tutorial.open_dataset('air_temperature')['air']

Plotting against multiple dimensions interactively

interactive.isel(da, xplot.plot, lat=10, lon=50)

isel_lat_and_lon

Interactively select a range from a dimension

def plot_mean_over_time(da):
    da.mean(dim=time)
interactive.isel(da, plot_mean_over_time, time=slice(100, 500))

mean_over_time_slice

Animate over one dimension

from ipywidgets import Play
interactive.isel(da, xplot.plot, time=Play())

Play

API ideas:

We can write a function like this

interactive.isel(da, func=xplot.plot, time=10)

which could also be used as a decorator something like this

@interactive.isel(da, time=10)
def plot(da)
    return xplot.plot(da)

It would be nicer to be able to do this

@Interactive(da).isel(time=10)
def plot(da)
    return xplot.plot(da)

but Guido forbade it.

But we can attach these functions to an accessor to get

da.interactive.isel(xplot.plot, time=10)

Other ideas

Select variables from datasets

@interactive.data_vars(da1=ds['n'], da2=ds['T'], ...)
def correlation(da1, da2, ...)
    ...

# Would produce a dropdown list of variables for each dataset

Choose dimensions to apply functions over

@interactive.dims(dim='time')
def mean(da, dim)
    ...
    
# Would produce a dropdown list of dimensions in the dataarray

General interactive.explore() method to see variation over any number of dimensions, the default being all of them.

What do people think about this? Is it something that makes sense to include within xarray itself? (Dependencies aren't a problem because it's fine to have ipywidgets as an optional dependency just for this module.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions