Numpy and Xarray

Authors
Dr. Atle E. Rimehaug | Dr. Nicholas Del Grosso

Numpy and Xarray

Most neuroscience data ends up as a multidimensional array at some point: a recording with samples and channels, an image stack with pixels and time, or a tensor of trials, conditions, and time points. This homework session introduces the two array libraries:

  • NumPy provides the n-dimensional array (ndarray) and the vectorised operations that work on it.
  • Xarray builds on top of NumPy by attaching names, coordinates, and metadata to each axis, so that you can refer to dimensions and indices by meaning rather than by position.

Setup

Import Libraries

import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
from pathlib import Path

Section 1: NumPy Arrays

Background

Numpy can be used to store data of the same type together in array. Compared to plain Python lists, arrays are more memory-efficient and faster for numerical work. An array has a fixed shape (the size of each dimension) and a dtype (the type of element stored), and supports slicing and arithmetic along chosen axes of your array. In this section you will learn different ways of creating arrays, how to access specific columns, rows, or elements with slicing, and how to perform mathematical operations on the data in your array.

Exercises

Code Description
np.linspace(0, 1, 10) 10 evenly spaced values between 0 and 1 (inclusive).
np.random.uniform(0, 1, size=10) 10 random numbers drawn uniformly between 0 and 1.
np.zeros(8) Create a 1-D array of length 8 filled with zeros.
np.zeros((2, 3)) Create a 2x3 array filled with zeros.
np.ones(n) Create an array of length n filled with ones.
a.shape Inspect the array shape (the size of each dimension).
a[0], a[:, 1] Select a row, or select a column with : along one axis.
a.mean(axis=1), a.mean(axis=0), a.mean() Average along a chosen axis, or over all elements.

Example: Create an array containing 10 evenly spaced points from 0 to 1.

np.linspace(0, 1, 10)
array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
       0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ])

Exercise: Create an array containing 100 evenly spaced points from 0 to 2.

Solution
np.linspace(0, 2, 100)
array([0.        , 0.02020202, 0.04040404, 0.06060606, 0.08080808,
       0.1010101 , 0.12121212, 0.14141414, 0.16161616, 0.18181818,
       0.2020202 , 0.22222222, 0.24242424, 0.26262626, 0.28282828,
       0.3030303 , 0.32323232, 0.34343434, 0.36363636, 0.38383838,
       0.4040404 , 0.42424242, 0.44444444, 0.46464646, 0.48484848,
       0.50505051, 0.52525253, 0.54545455, 0.56565657, 0.58585859,
       0.60606061, 0.62626263, 0.64646465, 0.66666667, 0.68686869,
       0.70707071, 0.72727273, 0.74747475, 0.76767677, 0.78787879,
       0.80808081, 0.82828283, 0.84848485, 0.86868687, 0.88888889,
       0.90909091, 0.92929293, 0.94949495, 0.96969697, 0.98989899,
       1.01010101, 1.03030303, 1.05050505, 1.07070707, 1.09090909,
       1.11111111, 1.13131313, 1.15151515, 1.17171717, 1.19191919,
       1.21212121, 1.23232323, 1.25252525, 1.27272727, 1.29292929,
       1.31313131, 1.33333333, 1.35353535, 1.37373737, 1.39393939,
       1.41414141, 1.43434343, 1.45454545, 1.47474747, 1.49494949,
       1.51515152, 1.53535354, 1.55555556, 1.57575758, 1.5959596 ,
       1.61616162, 1.63636364, 1.65656566, 1.67676768, 1.6969697 ,
       1.71717172, 1.73737374, 1.75757576, 1.77777778, 1.7979798 ,
       1.81818182, 1.83838384, 1.85858586, 1.87878788, 1.8989899 ,
       1.91919192, 1.93939394, 1.95959596, 1.97979798, 2.        ])

Exercise: Create an array containing 11 evenly spaced points from 0 to 10.

Solution
np.linspace(0, 10, 11)
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

Exercise: Create an array containing 10 random numbers between 0 and 1.

Solution
np.random.uniform(0, 1, size=10)
array([0.23688046, 0.0732585 , 0.38124546, 0.6830139 , 0.62902789,
       0.61705231, 0.83581813, 0.51359123, 0.70993304, 0.62060063])

Exercise: Create an array containing 10 random numbers between -5 and 5.

Solution
np.random.uniform(-5, 5, size=10)
array([ 2.98420124,  2.62496487, -1.59613291, -0.52788463, -0.94545015,
       -4.50761589, -1.92293556,  1.80233451,  3.7823791 ,  3.72298346])

Exercise: Create an array of length 8 containing zeros.

Solution
np.zeros(8)
array([0., 0., 0., 0., 0., 0., 0., 0.])

Exercise: Create a 2D array of dimensions (2, 3) containing zeros.

Solution
np.zeros((2,3))
array([[0., 0., 0.],
       [0., 0., 0.]])

Exercise: Create a 2D array of dimensions (4, 10) containing zeros and assign it to a variable named data.

Solution
data = np.zeros((4,10))
data.shape
(4, 10)

Run the cell below to create a 2D array named data with dimensions (4,10) containing numbers between 0’s on the first row, 1’s on the second row, 2’s on the third row, and 3’s on the third row.

data = np.zeros((4,10))
data[1, :] += np.ones(data.shape[1])
data[2, :] += 2*np.ones(data.shape[1])
data[3, :] += 3*np.ones(data.shape[1])
data
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
       [3., 3., 3., 3., 3., 3., 3., 3., 3., 3.]])

Exercise: Compute the mean of data across rows (axis 1). The result should contain 4 numbers.

Solution
data.mean(axis=1)
array([0., 1., 2., 3.])

Exercise: Compute the mean of data across columns (axis 0). The result should contain 10 numbers.

Solution
data.mean(axis=0)
array([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5])

Exercise: Compute the mean of all elements (no axis specification) in data. The result should be a single number.

Solution
data.mean()
np.float64(1.5)

Exercise: Select the first row in data.

Solution
data[0]
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

Exercise: Select the second row in data.

Solution
data[1]
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

Exercise: Select the second column in data.

Solution
data[:, 1]
array([0., 1., 2., 3.])

Section 2: Intro to Working with Xarray

Background

NumPy arrays are powerful but minimal: they store numerical data without any built-in information about what the axes represent. In scientific datasets, however, each axis usually corresponds to meaningful quantities such as space, time, wavelength, or experimental condition. When working with multidimensional data, it is often useful to label these dimensions explicitly.

Xarray extends NumPy by adding labels and metadata to multidimensional arrays. Each array can have named dimensions, coordinate values, and descriptive metadata attached to it. This makes many operations clearer and safer, since calculations can reference dimensions by name rather than by numeric index.

Exercises

In this section you will create DataArrays from NumPy arrays, attach coordinates and metadata, and select data by name and by label.

Code Description
xr.DataArray(data=a, dims=['channel', 'time']) Wrap a NumPy array a and name its axes.
xr.DataArray(data=a, dims=['channel', 'time'], coords={'time': t, 'channel': ch}, name='eeg') Attach coordinate values and a variable name.
da['time'] Access the time coordinate.
da.sel(channel='Cz') Select the data for a single named channel.
da.sel(time=slice(0.5, 1.0)) Select all samples whose time coordinate is between 0.5 and 1.0.
da.sel(channel=['Cz', 'Fz']) Select two named channels at once.
da.mean(dim='time') Reduce along a named dimension.
da.attrs = {'units': 'uV'} Attach metadata describing the array.
da.plot() Quick plot that uses dimension names and coordinates as axis labels.

The exercises in this section introduce the core concepts of XArray: treating a DataArray like a NumPy array, labeling dimensions, attaching coordinates, and adding metadata to describe the data.

xr.DataArray() as a Numpy-Like Array

At its core, an XArray DataArray wraps a NumPy array. Most numerical operations that work on NumPy arrays also work on DataArray objects, including slicing, aggregation, and arithmetic. This means that existing NumPy-style code often requires very little modification to work with labeled arrays.

Example: Create a three-dimensional DataArray from a Numpy array:

da = xr.DataArray(
    data=np.random.random(size=(10, 20, 30))
)
da
<xarray.DataArray (dim_0: 10, dim_1: 20, dim_2: 30)> Size: 48kB
array([[[0.74417186, 0.74445603, 0.95485254, ..., 0.88820844,
         0.55213267, 0.48404075],
        [0.00275093, 0.3579072 , 0.75258374, ..., 0.27916734,
         0.514455  , 0.88058488],
        [0.57873984, 0.45545297, 0.93010972, ..., 0.32624526,
         0.78828613, 0.42932637],
        ...,
        [0.41887157, 0.80663047, 0.13968038, ..., 0.13054864,
         0.87589024, 0.14550716],
        [0.55759933, 0.40887151, 0.87438825, ..., 0.85080726,
         0.55307975, 0.27550017],
        [0.24989024, 0.31993706, 0.34680131, ..., 0.22875437,
         0.05087074, 0.25131869]],
       [[0.76005113, 0.02177647, 0.91158968, ..., 0.52800382,
         0.52162327, 0.51041078],
        [0.50955224, 0.95705875, 0.70874692, ..., 0.30923278,
         0.09337201, 0.32978501],
        [0.98615452, 0.30643747, 0.03030972, ..., 0.88933094,
         0.85680121, 0.33738652],
...
        [0.12657064, 0.1321854 , 0.68884289, ..., 0.95679977,
         0.8632937 , 0.60814303],
        [0.11753152, 0.42347135, 0.31698314, ..., 0.75090063,
         0.93977372, 0.89589521],
        [0.32558841, 0.61482495, 0.38355799, ..., 0.07163196,
         0.29885202, 0.09875427]],
       [[0.61390494, 0.26959781, 0.85925555, ..., 0.94484535,
         0.8747301 , 0.56055315],
        [0.49863259, 0.6509259 , 0.22192394, ..., 0.82829552,
         0.52779011, 0.6794777 ],
        [0.67750384, 0.71031353, 0.27677078, ..., 0.44135758,
         0.32312176, 0.12458308],
        ...,
        [0.50797523, 0.97377595, 0.64857974, ..., 0.20780358,
         0.76608664, 0.96767893],
        [0.12810999, 0.70222897, 0.75951487, ..., 0.00662053,
         0.37500774, 0.78622253],
        [0.52685255, 0.42335374, 0.76164968, ..., 0.02070425,
         0.39137587, 0.59303456]]], shape=(10, 20, 30))
Dimensions without coordinates: dim_0, dim_1, dim_2
xarray.DataArray
  • dim_0: 10
  • dim_1: 20
  • dim_2: 30
  • 0.7442 0.7445 0.9549 0.1813 0.4209 ... 0.8974 0.0207 0.3914 0.593
    array([[[0.74417186, 0.74445603, 0.95485254, ..., 0.88820844,
             0.55213267, 0.48404075],
            [0.00275093, 0.3579072 , 0.75258374, ..., 0.27916734,
             0.514455  , 0.88058488],
            [0.57873984, 0.45545297, 0.93010972, ..., 0.32624526,
             0.78828613, 0.42932637],
            ...,
            [0.41887157, 0.80663047, 0.13968038, ..., 0.13054864,
             0.87589024, 0.14550716],
            [0.55759933, 0.40887151, 0.87438825, ..., 0.85080726,
             0.55307975, 0.27550017],
            [0.24989024, 0.31993706, 0.34680131, ..., 0.22875437,
             0.05087074, 0.25131869]],
           [[0.76005113, 0.02177647, 0.91158968, ..., 0.52800382,
             0.52162327, 0.51041078],
            [0.50955224, 0.95705875, 0.70874692, ..., 0.30923278,
             0.09337201, 0.32978501],
            [0.98615452, 0.30643747, 0.03030972, ..., 0.88933094,
             0.85680121, 0.33738652],
    ...
            [0.12657064, 0.1321854 , 0.68884289, ..., 0.95679977,
             0.8632937 , 0.60814303],
            [0.11753152, 0.42347135, 0.31698314, ..., 0.75090063,
             0.93977372, 0.89589521],
            [0.32558841, 0.61482495, 0.38355799, ..., 0.07163196,
             0.29885202, 0.09875427]],
           [[0.61390494, 0.26959781, 0.85925555, ..., 0.94484535,
             0.8747301 , 0.56055315],
            [0.49863259, 0.6509259 , 0.22192394, ..., 0.82829552,
             0.52779011, 0.6794777 ],
            [0.67750384, 0.71031353, 0.27677078, ..., 0.44135758,
             0.32312176, 0.12458308],
            ...,
            [0.50797523, 0.97377595, 0.64857974, ..., 0.20780358,
             0.76608664, 0.96767893],
            [0.12810999, 0.70222897, 0.75951487, ..., 0.00662053,
             0.37500774, 0.78622253],
            [0.52685255, 0.42335374, 0.76164968, ..., 0.02070425,
             0.39137587, 0.59303456]]], shape=(10, 20, 30))

Exercise: Select the first 5 rows of da, using the slicing syntax x[:10, :, :]

Solution
da[:5, :, :]
<xarray.DataArray (dim_0: 5, dim_1: 20, dim_2: 30)> Size: 24kB
array([[[0.74417186, 0.74445603, 0.95485254, ..., 0.88820844,
         0.55213267, 0.48404075],
        [0.00275093, 0.3579072 , 0.75258374, ..., 0.27916734,
         0.514455  , 0.88058488],
        [0.57873984, 0.45545297, 0.93010972, ..., 0.32624526,
         0.78828613, 0.42932637],
        ...,
        [0.41887157, 0.80663047, 0.13968038, ..., 0.13054864,
         0.87589024, 0.14550716],
        [0.55759933, 0.40887151, 0.87438825, ..., 0.85080726,
         0.55307975, 0.27550017],
        [0.24989024, 0.31993706, 0.34680131, ..., 0.22875437,
         0.05087074, 0.25131869]],
       [[0.76005113, 0.02177647, 0.91158968, ..., 0.52800382,
         0.52162327, 0.51041078],
        [0.50955224, 0.95705875, 0.70874692, ..., 0.30923278,
         0.09337201, 0.32978501],
        [0.98615452, 0.30643747, 0.03030972, ..., 0.88933094,
         0.85680121, 0.33738652],
...
        [0.12091439, 0.51173377, 0.07838954, ..., 0.4251066 ,
         0.50346027, 0.42225372],
        [0.74678128, 0.43688457, 0.37482319, ..., 0.43030269,
         0.47682434, 0.35709109],
        [0.17590855, 0.4143141 , 0.67488795, ..., 0.24536429,
         0.6553955 , 0.30296064]],
       [[0.51441081, 0.87134214, 0.39202382, ..., 0.83921874,
         0.47816971, 0.0192038 ],
        [0.66965384, 0.66465191, 0.10207379, ..., 0.75671827,
         0.00705633, 0.63045001],
        [0.2510453 , 0.21916377, 0.47945753, ..., 0.04086338,
         0.08151262, 0.46256557],
        ...,
        [0.65344197, 0.54165881, 0.93981879, ..., 0.10147449,
         0.17815015, 0.91378422],
        [0.78493301, 0.77408745, 0.59415959, ..., 0.4499055 ,
         0.02994517, 0.02281548],
        [0.65433303, 0.94499115, 0.72718413, ..., 0.75527957,
         0.11544911, 0.0718399 ]]], shape=(5, 20, 30))
Dimensions without coordinates: dim_0, dim_1, dim_2
xarray.DataArray
  • dim_0: 5
  • dim_1: 20
  • dim_2: 30
  • 0.7442 0.7445 0.9549 0.1813 0.4209 ... 0.3582 0.7553 0.1154 0.07184
    array([[[0.74417186, 0.74445603, 0.95485254, ..., 0.88820844,
             0.55213267, 0.48404075],
            [0.00275093, 0.3579072 , 0.75258374, ..., 0.27916734,
             0.514455  , 0.88058488],
            [0.57873984, 0.45545297, 0.93010972, ..., 0.32624526,
             0.78828613, 0.42932637],
            ...,
            [0.41887157, 0.80663047, 0.13968038, ..., 0.13054864,
             0.87589024, 0.14550716],
            [0.55759933, 0.40887151, 0.87438825, ..., 0.85080726,
             0.55307975, 0.27550017],
            [0.24989024, 0.31993706, 0.34680131, ..., 0.22875437,
             0.05087074, 0.25131869]],
           [[0.76005113, 0.02177647, 0.91158968, ..., 0.52800382,
             0.52162327, 0.51041078],
            [0.50955224, 0.95705875, 0.70874692, ..., 0.30923278,
             0.09337201, 0.32978501],
            [0.98615452, 0.30643747, 0.03030972, ..., 0.88933094,
             0.85680121, 0.33738652],
    ...
            [0.12091439, 0.51173377, 0.07838954, ..., 0.4251066 ,
             0.50346027, 0.42225372],
            [0.74678128, 0.43688457, 0.37482319, ..., 0.43030269,
             0.47682434, 0.35709109],
            [0.17590855, 0.4143141 , 0.67488795, ..., 0.24536429,
             0.6553955 , 0.30296064]],
           [[0.51441081, 0.87134214, 0.39202382, ..., 0.83921874,
             0.47816971, 0.0192038 ],
            [0.66965384, 0.66465191, 0.10207379, ..., 0.75671827,
             0.00705633, 0.63045001],
            [0.2510453 , 0.21916377, 0.47945753, ..., 0.04086338,
             0.08151262, 0.46256557],
            ...,
            [0.65344197, 0.54165881, 0.93981879, ..., 0.10147449,
             0.17815015, 0.91378422],
            [0.78493301, 0.77408745, 0.59415959, ..., 0.4499055 ,
             0.02994517, 0.02281548],
            [0.65433303, 0.94499115, 0.72718413, ..., 0.75527957,
             0.11544911, 0.0718399 ]]], shape=(5, 20, 30))

Exercise: Compute the mean, using either DataArray.mean() or np.mean()

Solution
da.mean()
<xarray.DataArray ()> Size: 8B
array(0.49653279)
xarray.DataArray
  • 0.4965
    array(0.49653279)
np.mean(da)
<xarray.DataArray ()> Size: 8B
array(0.49653279)
xarray.DataArray
  • 0.4965
    array(0.49653279)

Exercise: Compute the mean over the third axis, using da.mean(axis=2)

Solution
da.mean(axis=2)
<xarray.DataArray (dim_0: 10, dim_1: 20)> Size: 2kB
array([[0.53537833, 0.46886817, 0.49907704, 0.49886188, 0.44376631,
        0.50819679, 0.56884198, 0.58407161, 0.59156347, 0.48657386,
        0.57351651, 0.57230974, 0.47015929, 0.41033661, 0.51423499,
        0.5378961 , 0.5980966 , 0.50527531, 0.46246696, 0.54402144],
       [0.58372329, 0.53171824, 0.59910785, 0.49938965, 0.51414678,
        0.48882777, 0.56889124, 0.5249178 , 0.61461307, 0.50170071,
        0.53850623, 0.54387213, 0.5285257 , 0.49035052, 0.44924196,
        0.35816476, 0.39598508, 0.52549319, 0.49691696, 0.53753849],
       [0.51694106, 0.42529152, 0.42183067, 0.50766911, 0.50435507,
        0.50355895, 0.5237247 , 0.49349013, 0.48608234, 0.48866164,
        0.51649334, 0.56179093, 0.51041807, 0.40812765, 0.43619607,
        0.4965137 , 0.42934926, 0.44718106, 0.50669696, 0.5579623 ],
       [0.41661541, 0.53293437, 0.51573365, 0.4533813 , 0.53452537,
        0.59069753, 0.49128288, 0.4334842 , 0.60044273, 0.44978719,
        0.44313214, 0.41043926, 0.59375099, 0.55942316, 0.40008244,
        0.48750782, 0.50716591, 0.52265576, 0.54565971, 0.50816128],
       [0.53836207, 0.53327768, 0.4914158 , 0.5528335 , 0.45912548,
        0.44929408, 0.56616935, 0.5660036 , 0.50033076, 0.52756685,
        0.47805902, 0.51900645, 0.55283957, 0.47765311, 0.50586643,
        0.53041174, 0.41544928, 0.55660398, 0.57621006, 0.48427627],
       [0.45885675, 0.49327936, 0.54333995, 0.51524779, 0.546856  ,
        0.4853281 , 0.44774305, 0.46627292, 0.43120673, 0.554067  ,
        0.45389392, 0.45324451, 0.49497296, 0.42010513, 0.546199  ,
        0.50421622, 0.56731476, 0.50539156, 0.53988636, 0.48749038],
       [0.55427574, 0.41012924, 0.52512854, 0.45310322, 0.47464841,
        0.57418447, 0.39333881, 0.59622624, 0.40457853, 0.4704868 ,
        0.53300059, 0.49485267, 0.42007644, 0.45283672, 0.43175829,
        0.39616826, 0.43445524, 0.55618329, 0.45519623, 0.61561502],
       [0.40911436, 0.50506936, 0.47117697, 0.45533388, 0.5741073 ,
        0.47439114, 0.50267765, 0.48130919, 0.47486234, 0.58411438,
        0.43281125, 0.44261874, 0.43073551, 0.45265657, 0.58087857,
        0.48283553, 0.40971459, 0.36459261, 0.52814119, 0.47887697],
       [0.39028938, 0.54796006, 0.44688512, 0.47577797, 0.58934632,
        0.51457567, 0.48727706, 0.39135097, 0.51003739, 0.5025027 ,
        0.45438619, 0.50826317, 0.51833442, 0.38297362, 0.4897886 ,
        0.51469086, 0.4741349 , 0.4853987 , 0.49128467, 0.41650021],
       [0.50168305, 0.48437945, 0.48032487, 0.54876564, 0.37960303,
        0.53468946, 0.49966252, 0.35446308, 0.56823857, 0.54057086,
        0.43315715, 0.57267819, 0.49802796, 0.4870567 , 0.59715784,
        0.43691622, 0.52405894, 0.56707802, 0.48451246, 0.43950614]])
Dimensions without coordinates: dim_0, dim_1
xarray.DataArray
  • dim_0: 10
  • dim_1: 20
  • 0.5354 0.4689 0.4991 0.4989 0.4438 ... 0.5241 0.5671 0.4845 0.4395
    array([[0.53537833, 0.46886817, 0.49907704, 0.49886188, 0.44376631,
            0.50819679, 0.56884198, 0.58407161, 0.59156347, 0.48657386,
            0.57351651, 0.57230974, 0.47015929, 0.41033661, 0.51423499,
            0.5378961 , 0.5980966 , 0.50527531, 0.46246696, 0.54402144],
           [0.58372329, 0.53171824, 0.59910785, 0.49938965, 0.51414678,
            0.48882777, 0.56889124, 0.5249178 , 0.61461307, 0.50170071,
            0.53850623, 0.54387213, 0.5285257 , 0.49035052, 0.44924196,
            0.35816476, 0.39598508, 0.52549319, 0.49691696, 0.53753849],
           [0.51694106, 0.42529152, 0.42183067, 0.50766911, 0.50435507,
            0.50355895, 0.5237247 , 0.49349013, 0.48608234, 0.48866164,
            0.51649334, 0.56179093, 0.51041807, 0.40812765, 0.43619607,
            0.4965137 , 0.42934926, 0.44718106, 0.50669696, 0.5579623 ],
           [0.41661541, 0.53293437, 0.51573365, 0.4533813 , 0.53452537,
            0.59069753, 0.49128288, 0.4334842 , 0.60044273, 0.44978719,
            0.44313214, 0.41043926, 0.59375099, 0.55942316, 0.40008244,
            0.48750782, 0.50716591, 0.52265576, 0.54565971, 0.50816128],
           [0.53836207, 0.53327768, 0.4914158 , 0.5528335 , 0.45912548,
            0.44929408, 0.56616935, 0.5660036 , 0.50033076, 0.52756685,
            0.47805902, 0.51900645, 0.55283957, 0.47765311, 0.50586643,
            0.53041174, 0.41544928, 0.55660398, 0.57621006, 0.48427627],
           [0.45885675, 0.49327936, 0.54333995, 0.51524779, 0.546856  ,
            0.4853281 , 0.44774305, 0.46627292, 0.43120673, 0.554067  ,
            0.45389392, 0.45324451, 0.49497296, 0.42010513, 0.546199  ,
            0.50421622, 0.56731476, 0.50539156, 0.53988636, 0.48749038],
           [0.55427574, 0.41012924, 0.52512854, 0.45310322, 0.47464841,
            0.57418447, 0.39333881, 0.59622624, 0.40457853, 0.4704868 ,
            0.53300059, 0.49485267, 0.42007644, 0.45283672, 0.43175829,
            0.39616826, 0.43445524, 0.55618329, 0.45519623, 0.61561502],
           [0.40911436, 0.50506936, 0.47117697, 0.45533388, 0.5741073 ,
            0.47439114, 0.50267765, 0.48130919, 0.47486234, 0.58411438,
            0.43281125, 0.44261874, 0.43073551, 0.45265657, 0.58087857,
            0.48283553, 0.40971459, 0.36459261, 0.52814119, 0.47887697],
           [0.39028938, 0.54796006, 0.44688512, 0.47577797, 0.58934632,
            0.51457567, 0.48727706, 0.39135097, 0.51003739, 0.5025027 ,
            0.45438619, 0.50826317, 0.51833442, 0.38297362, 0.4897886 ,
            0.51469086, 0.4741349 , 0.4853987 , 0.49128467, 0.41650021],
           [0.50168305, 0.48437945, 0.48032487, 0.54876564, 0.37960303,
            0.53468946, 0.49966252, 0.35446308, 0.56823857, 0.54057086,
            0.43315715, 0.57267819, 0.49802796, 0.4870567 , 0.59715784,
            0.43691622, 0.52405894, 0.56707802, 0.48451246, 0.43950614]])

Labeling the Data and the Dimensions: name= and dims=

One of XArray’s most useful features is the ability to name dimensions explicitly. Instead of referring to axes by position—such as “axis 0” or “axis 2”—operations can refer to dimensions using meaningful labels like “x”, “y”, or “time”.

Once dimensions are named, many operations become easier to read and harder to misuse. For example, computing the mean across time can be expressed as mean(dim=“time”), which clearly communicates the intent of the calculation.

Exercise: Make a new da 3-dimensional array variable using xr.DataArray(), this time additionally setting name="image" and dims=['x', 'y', 'time']

Solution
da = xr.DataArray(
    data=np.random.random(size=(10, 20, 30)),
    name='image',
    dims=['x', 'y', 'time']
)
da
<xarray.DataArray 'image' (x: 10, y: 20, time: 30)> Size: 48kB
array([[[0.0039415 , 0.78943741, 0.54325302, ..., 0.95936073,
         0.79082559, 0.61625175],
        [0.96889197, 0.17519487, 0.72437646, ..., 0.33941244,
         0.40408226, 0.79841416],
        [0.54112423, 0.7706653 , 0.65830526, ..., 0.15686106,
         0.9824433 , 0.43613284],
        ...,
        [0.76221535, 0.47759567, 0.14542407, ..., 0.4208138 ,
         0.55232978, 0.76773044],
        [0.59812252, 0.39322958, 0.66669151, ..., 0.34625546,
         0.95246261, 0.51787839],
        [0.46948938, 0.08545758, 0.68082324, ..., 0.82627506,
         0.77587873, 0.57557807]],
       [[0.30350489, 0.97628479, 0.94716344, ..., 0.54790831,
         0.37801491, 0.73351821],
        [0.73097905, 0.36998393, 0.39280519, ..., 0.02998589,
         0.59142054, 0.50099998],
        [0.2495287 , 0.11382447, 0.74685947, ..., 0.92944393,
         0.146996  , 0.50578664],
...
        [0.80592317, 0.33796297, 0.75097242, ..., 0.70512213,
         0.89242147, 0.84002832],
        [0.42108829, 0.38238295, 0.90508302, ..., 0.55602547,
         0.70083685, 0.14360367],
        [0.61424044, 0.49482312, 0.28689914, ..., 0.42776683,
         0.3088656 , 0.71104696]],
       [[0.89006316, 0.84110542, 0.36207882, ..., 0.42675834,
         0.58977536, 0.23959072],
        [0.74065725, 0.57315567, 0.14812481, ..., 0.33372056,
         0.8140718 , 0.94888713],
        [0.90544292, 0.44261141, 0.99270592, ..., 0.11925748,
         0.24569573, 0.57039713],
        ...,
        [0.26782605, 0.70975953, 0.96394786, ..., 0.06054418,
         0.48596833, 0.3115355 ],
        [0.50646405, 0.82913632, 0.97594465, ..., 0.98941264,
         0.21712661, 0.54722449],
        [0.50358535, 0.67379158, 0.55387312, ..., 0.53059179,
         0.85953671, 0.69058448]]], shape=(10, 20, 30))
Dimensions without coordinates: x, y, time
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • time: 30
  • 0.003941 0.7894 0.5433 0.9674 0.1188 ... 0.8072 0.5306 0.8595 0.6906
    array([[[0.0039415 , 0.78943741, 0.54325302, ..., 0.95936073,
             0.79082559, 0.61625175],
            [0.96889197, 0.17519487, 0.72437646, ..., 0.33941244,
             0.40408226, 0.79841416],
            [0.54112423, 0.7706653 , 0.65830526, ..., 0.15686106,
             0.9824433 , 0.43613284],
            ...,
            [0.76221535, 0.47759567, 0.14542407, ..., 0.4208138 ,
             0.55232978, 0.76773044],
            [0.59812252, 0.39322958, 0.66669151, ..., 0.34625546,
             0.95246261, 0.51787839],
            [0.46948938, 0.08545758, 0.68082324, ..., 0.82627506,
             0.77587873, 0.57557807]],
           [[0.30350489, 0.97628479, 0.94716344, ..., 0.54790831,
             0.37801491, 0.73351821],
            [0.73097905, 0.36998393, 0.39280519, ..., 0.02998589,
             0.59142054, 0.50099998],
            [0.2495287 , 0.11382447, 0.74685947, ..., 0.92944393,
             0.146996  , 0.50578664],
    ...
            [0.80592317, 0.33796297, 0.75097242, ..., 0.70512213,
             0.89242147, 0.84002832],
            [0.42108829, 0.38238295, 0.90508302, ..., 0.55602547,
             0.70083685, 0.14360367],
            [0.61424044, 0.49482312, 0.28689914, ..., 0.42776683,
             0.3088656 , 0.71104696]],
           [[0.89006316, 0.84110542, 0.36207882, ..., 0.42675834,
             0.58977536, 0.23959072],
            [0.74065725, 0.57315567, 0.14812481, ..., 0.33372056,
             0.8140718 , 0.94888713],
            [0.90544292, 0.44261141, 0.99270592, ..., 0.11925748,
             0.24569573, 0.57039713],
            ...,
            [0.26782605, 0.70975953, 0.96394786, ..., 0.06054418,
             0.48596833, 0.3115355 ],
            [0.50646405, 0.82913632, 0.97594465, ..., 0.98941264,
             0.21712661, 0.54722449],
            [0.50358535, 0.67379158, 0.55387312, ..., 0.53059179,
             0.85953671, 0.69058448]]], shape=(10, 20, 30))

Exercise: Select the fourth time sample using da.sel(time=4)

Solution
da.sel(time=4)
<xarray.DataArray 'image' (x: 10, y: 20)> Size: 2kB
array([[0.11882777, 0.0064471 , 0.31942185, 0.30657858, 0.5429336 ,
        0.5560278 , 0.17213332, 0.39721463, 0.30440203, 0.78302354,
        0.59342502, 0.05194957, 0.18825771, 0.53040442, 0.69137112,
        0.59502772, 0.63226676, 0.90867948, 0.990193  , 0.95958723],
       [0.93572164, 0.1384223 , 0.59740557, 0.58539249, 0.08018034,
        0.97127743, 0.13486546, 0.27722555, 0.52398664, 0.37059438,
        0.83854011, 0.74302013, 0.51303593, 0.62656426, 0.05598297,
        0.81081006, 0.26740709, 0.29081507, 0.13260901, 0.12057768],
       [0.95930328, 0.65308736, 0.8595202 , 0.21798345, 0.57560739,
        0.03073045, 0.63908678, 0.81347569, 0.80644115, 0.34353143,
        0.36322591, 0.1258525 , 0.02918215, 0.54015009, 0.15116414,
        0.10229714, 0.60451892, 0.09615116, 0.32393941, 0.68837933],
       [0.52346758, 0.70659528, 0.80141959, 0.45788281, 0.38923827,
        0.55549605, 0.27550308, 0.18736632, 0.1073278 , 0.96816081,
        0.99186747, 0.86433019, 0.88113725, 0.40091696, 0.87811977,
        0.73539728, 0.02566882, 0.42991059, 0.74836712, 0.20475986],
       [0.30880308, 0.25557733, 0.16050504, 0.43442382, 0.96081307,
        0.48505522, 0.18695398, 0.39588531, 0.78988792, 0.51472199,
        0.93668684, 0.76828387, 0.24380932, 0.24672899, 0.78764085,
        0.60651638, 0.41285236, 0.94219982, 0.4063638 , 0.13704357],
       [0.94098374, 0.24693438, 0.54906829, 0.02992752, 0.19871608,
        0.83570473, 0.64260989, 0.02872966, 0.70227149, 0.06650087,
        0.63710499, 0.39424256, 0.91043752, 0.71900698, 0.02173563,
        0.01149888, 0.69195822, 0.43919835, 0.88077602, 0.04841189],
       [0.77480229, 0.04435872, 0.62387575, 0.51662449, 0.16848953,
        0.00816849, 0.76179641, 0.70802111, 0.49274269, 0.25883653,
        0.29084103, 0.21643527, 0.10885414, 0.01021476, 0.13053143,
        0.89241041, 0.74919145, 0.59326318, 0.34920397, 0.1520944 ],
       [0.97946072, 0.43177934, 0.31413392, 0.12741698, 0.59997689,
        0.36282171, 0.11889245, 0.24901184, 0.36657414, 0.77566968,
        0.86718643, 0.90545262, 0.39804694, 0.15755338, 0.26430029,
        0.45621072, 0.1783266 , 0.42725007, 0.585054  , 0.67238779],
       [0.43446377, 0.62063861, 0.33598525, 0.73531296, 0.09659241,
        0.64835791, 0.97019964, 0.7140658 , 0.46257141, 0.51541174,
        0.20934494, 0.56924159, 0.89535195, 0.17296989, 0.46379739,
        0.30231347, 0.4182294 , 0.54460657, 0.71552108, 0.48006797],
       [0.9842779 , 0.65364941, 0.6838362 , 0.20700734, 0.77382736,
        0.24803536, 0.17901725, 0.29743559, 0.66340212, 0.21315208,
        0.74314827, 0.05013461, 0.68989971, 0.96392929, 0.33485558,
        0.10981863, 0.86184196, 0.77522117, 0.32576152, 0.97492833]])
Dimensions without coordinates: x, y
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • 0.1188 0.006447 0.3194 0.3066 0.5429 ... 0.8618 0.7752 0.3258 0.9749
    array([[0.11882777, 0.0064471 , 0.31942185, 0.30657858, 0.5429336 ,
            0.5560278 , 0.17213332, 0.39721463, 0.30440203, 0.78302354,
            0.59342502, 0.05194957, 0.18825771, 0.53040442, 0.69137112,
            0.59502772, 0.63226676, 0.90867948, 0.990193  , 0.95958723],
           [0.93572164, 0.1384223 , 0.59740557, 0.58539249, 0.08018034,
            0.97127743, 0.13486546, 0.27722555, 0.52398664, 0.37059438,
            0.83854011, 0.74302013, 0.51303593, 0.62656426, 0.05598297,
            0.81081006, 0.26740709, 0.29081507, 0.13260901, 0.12057768],
           [0.95930328, 0.65308736, 0.8595202 , 0.21798345, 0.57560739,
            0.03073045, 0.63908678, 0.81347569, 0.80644115, 0.34353143,
            0.36322591, 0.1258525 , 0.02918215, 0.54015009, 0.15116414,
            0.10229714, 0.60451892, 0.09615116, 0.32393941, 0.68837933],
           [0.52346758, 0.70659528, 0.80141959, 0.45788281, 0.38923827,
            0.55549605, 0.27550308, 0.18736632, 0.1073278 , 0.96816081,
            0.99186747, 0.86433019, 0.88113725, 0.40091696, 0.87811977,
            0.73539728, 0.02566882, 0.42991059, 0.74836712, 0.20475986],
           [0.30880308, 0.25557733, 0.16050504, 0.43442382, 0.96081307,
            0.48505522, 0.18695398, 0.39588531, 0.78988792, 0.51472199,
            0.93668684, 0.76828387, 0.24380932, 0.24672899, 0.78764085,
            0.60651638, 0.41285236, 0.94219982, 0.4063638 , 0.13704357],
           [0.94098374, 0.24693438, 0.54906829, 0.02992752, 0.19871608,
            0.83570473, 0.64260989, 0.02872966, 0.70227149, 0.06650087,
            0.63710499, 0.39424256, 0.91043752, 0.71900698, 0.02173563,
            0.01149888, 0.69195822, 0.43919835, 0.88077602, 0.04841189],
           [0.77480229, 0.04435872, 0.62387575, 0.51662449, 0.16848953,
            0.00816849, 0.76179641, 0.70802111, 0.49274269, 0.25883653,
            0.29084103, 0.21643527, 0.10885414, 0.01021476, 0.13053143,
            0.89241041, 0.74919145, 0.59326318, 0.34920397, 0.1520944 ],
           [0.97946072, 0.43177934, 0.31413392, 0.12741698, 0.59997689,
            0.36282171, 0.11889245, 0.24901184, 0.36657414, 0.77566968,
            0.86718643, 0.90545262, 0.39804694, 0.15755338, 0.26430029,
            0.45621072, 0.1783266 , 0.42725007, 0.585054  , 0.67238779],
           [0.43446377, 0.62063861, 0.33598525, 0.73531296, 0.09659241,
            0.64835791, 0.97019964, 0.7140658 , 0.46257141, 0.51541174,
            0.20934494, 0.56924159, 0.89535195, 0.17296989, 0.46379739,
            0.30231347, 0.4182294 , 0.54460657, 0.71552108, 0.48006797],
           [0.9842779 , 0.65364941, 0.6838362 , 0.20700734, 0.77382736,
            0.24803536, 0.17901725, 0.29743559, 0.66340212, 0.21315208,
            0.74314827, 0.05013461, 0.68989971, 0.96392929, 0.33485558,
            0.10981863, 0.86184196, 0.77522117, 0.32576152, 0.97492833]])

Exercise: Select the first-through fifth rows by name using da.sel(x=slice(0, 5))

Solution
da.sel(x=slice(0, 5))
<xarray.DataArray 'image' (x: 5, y: 20, time: 30)> Size: 24kB
array([[[0.0039415 , 0.78943741, 0.54325302, ..., 0.95936073,
         0.79082559, 0.61625175],
        [0.96889197, 0.17519487, 0.72437646, ..., 0.33941244,
         0.40408226, 0.79841416],
        [0.54112423, 0.7706653 , 0.65830526, ..., 0.15686106,
         0.9824433 , 0.43613284],
        ...,
        [0.76221535, 0.47759567, 0.14542407, ..., 0.4208138 ,
         0.55232978, 0.76773044],
        [0.59812252, 0.39322958, 0.66669151, ..., 0.34625546,
         0.95246261, 0.51787839],
        [0.46948938, 0.08545758, 0.68082324, ..., 0.82627506,
         0.77587873, 0.57557807]],
       [[0.30350489, 0.97628479, 0.94716344, ..., 0.54790831,
         0.37801491, 0.73351821],
        [0.73097905, 0.36998393, 0.39280519, ..., 0.02998589,
         0.59142054, 0.50099998],
        [0.2495287 , 0.11382447, 0.74685947, ..., 0.92944393,
         0.146996  , 0.50578664],
...
        [0.08475462, 0.65920112, 0.09582319, ..., 0.78578663,
         0.97011753, 0.3306514 ],
        [0.43106394, 0.59960424, 0.7835912 , ..., 0.40637456,
         0.7166951 , 0.06775339],
        [0.04005816, 0.06695144, 0.97286564, ..., 0.98201629,
         0.18760001, 0.74538662]],
       [[0.2748003 , 0.73946035, 0.1171143 , ..., 0.66676379,
         0.40213707, 0.75550514],
        [0.96819433, 0.71962817, 0.10249323, ..., 0.88995039,
         0.28369164, 0.77572307],
        [0.92039652, 0.81808681, 0.76063804, ..., 0.11975378,
         0.74099945, 0.27487136],
        ...,
        [0.12420268, 0.71143541, 0.05588293, ..., 0.57812928,
         0.61598677, 0.31831476],
        [0.16537859, 0.5341819 , 0.36216241, ..., 0.82449977,
         0.68714985, 0.84212864],
        [0.96927536, 0.50890743, 0.71925353, ..., 0.29615807,
         0.84791532, 0.21192984]]], shape=(5, 20, 30))
Dimensions without coordinates: x, y, time
xarray.DataArray
'image'
  • x: 5
  • y: 20
  • time: 30
  • 0.003941 0.7894 0.5433 0.9674 0.1188 ... 0.7378 0.2962 0.8479 0.2119
    array([[[0.0039415 , 0.78943741, 0.54325302, ..., 0.95936073,
             0.79082559, 0.61625175],
            [0.96889197, 0.17519487, 0.72437646, ..., 0.33941244,
             0.40408226, 0.79841416],
            [0.54112423, 0.7706653 , 0.65830526, ..., 0.15686106,
             0.9824433 , 0.43613284],
            ...,
            [0.76221535, 0.47759567, 0.14542407, ..., 0.4208138 ,
             0.55232978, 0.76773044],
            [0.59812252, 0.39322958, 0.66669151, ..., 0.34625546,
             0.95246261, 0.51787839],
            [0.46948938, 0.08545758, 0.68082324, ..., 0.82627506,
             0.77587873, 0.57557807]],
           [[0.30350489, 0.97628479, 0.94716344, ..., 0.54790831,
             0.37801491, 0.73351821],
            [0.73097905, 0.36998393, 0.39280519, ..., 0.02998589,
             0.59142054, 0.50099998],
            [0.2495287 , 0.11382447, 0.74685947, ..., 0.92944393,
             0.146996  , 0.50578664],
    ...
            [0.08475462, 0.65920112, 0.09582319, ..., 0.78578663,
             0.97011753, 0.3306514 ],
            [0.43106394, 0.59960424, 0.7835912 , ..., 0.40637456,
             0.7166951 , 0.06775339],
            [0.04005816, 0.06695144, 0.97286564, ..., 0.98201629,
             0.18760001, 0.74538662]],
           [[0.2748003 , 0.73946035, 0.1171143 , ..., 0.66676379,
             0.40213707, 0.75550514],
            [0.96819433, 0.71962817, 0.10249323, ..., 0.88995039,
             0.28369164, 0.77572307],
            [0.92039652, 0.81808681, 0.76063804, ..., 0.11975378,
             0.74099945, 0.27487136],
            ...,
            [0.12420268, 0.71143541, 0.05588293, ..., 0.57812928,
             0.61598677, 0.31831476],
            [0.16537859, 0.5341819 , 0.36216241, ..., 0.82449977,
             0.68714985, 0.84212864],
            [0.96927536, 0.50890743, 0.71925353, ..., 0.29615807,
             0.84791532, 0.21192984]]], shape=(5, 20, 30))

Exercise: Compute the Mean image over time by name, using da.mean(dim='time'):

Solution
da.mean(dim='time')
<xarray.DataArray 'image' (x: 10, y: 20)> Size: 2kB
array([[0.49288   , 0.52536311, 0.53162294, 0.49938499, 0.45049025,
        0.45462914, 0.51877694, 0.451556  , 0.58314608, 0.43769262,
        0.43759725, 0.45128502, 0.46735404, 0.51183461, 0.45485568,
        0.47747395, 0.55607196, 0.49671747, 0.51115143, 0.53042226],
       [0.64614862, 0.49081109, 0.51652219, 0.53160637, 0.58278671,
        0.58219204, 0.5559344 , 0.52751658, 0.47554518, 0.48787244,
        0.40897282, 0.45165729, 0.4986628 , 0.45568042, 0.47177205,
        0.42475376, 0.51228982, 0.47496702, 0.51810984, 0.55575172],
       [0.44672215, 0.55179277, 0.53664366, 0.45180616, 0.53894875,
        0.34906605, 0.51584597, 0.43214807, 0.56067428, 0.5201479 ,
        0.41624839, 0.41534364, 0.53802002, 0.51185199, 0.51860052,
        0.37737511, 0.49406851, 0.42989753, 0.46073355, 0.51518632],
       [0.49145803, 0.43114171, 0.48010862, 0.53870138, 0.51851854,
        0.46592164, 0.52322175, 0.53633628, 0.53857991, 0.38091769,
        0.48250717, 0.47410379, 0.46511836, 0.43738601, 0.54567555,
        0.45268941, 0.49083205, 0.49732334, 0.52215497, 0.44391537],
       [0.55181315, 0.47335764, 0.47307752, 0.55462193, 0.47268303,
        0.50391643, 0.40237155, 0.52638194, 0.54282961, 0.53983861,
        0.54031275, 0.54090207, 0.5641079 , 0.55524692, 0.5498643 ,
        0.55868992, 0.49561466, 0.43698921, 0.48736908, 0.48964031],
       [0.57080762, 0.54150965, 0.45306951, 0.49469778, 0.39822388,
        0.51189018, 0.44625118, 0.48833954, 0.50482681, 0.51356756,
        0.52358719, 0.57480701, 0.59904274, 0.45298714, 0.49537757,
        0.57317365, 0.44963189, 0.48780013, 0.55218229, 0.48800744],
       [0.41085827, 0.4721218 , 0.55777946, 0.48747705, 0.51827412,
        0.55440009, 0.53449502, 0.48557451, 0.44186694, 0.54249618,
        0.47178047, 0.51373122, 0.41162603, 0.59911708, 0.44308093,
        0.57523582, 0.49899387, 0.51612199, 0.44433574, 0.5094633 ],
       [0.46423999, 0.52110696, 0.54751085, 0.5236267 , 0.52262574,
        0.47574738, 0.47635594, 0.52850375, 0.5180659 , 0.51800327,
        0.56835749, 0.52569787, 0.53232593, 0.51915777, 0.53711474,
        0.50998807, 0.58637746, 0.54203452, 0.41912904, 0.5268633 ],
       [0.38180878, 0.44458991, 0.40858482, 0.43837323, 0.46420371,
        0.50070491, 0.53347177, 0.45829398, 0.5005501 , 0.54819781,
        0.53319082, 0.4935839 , 0.56152169, 0.52772437, 0.4853941 ,
        0.5423685 , 0.53570063, 0.6167742 , 0.56435511, 0.53486022],
       [0.46269484, 0.51369752, 0.48961097, 0.56440869, 0.49927548,
        0.51591196, 0.48664964, 0.58453801, 0.51981355, 0.46520979,
        0.47776371, 0.49952828, 0.39779903, 0.59997996, 0.4037436 ,
        0.40797392, 0.42670726, 0.56891686, 0.58856331, 0.48287125]])
Dimensions without coordinates: x, y
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • 0.4929 0.5254 0.5316 0.4994 0.4505 ... 0.4267 0.5689 0.5886 0.4829
    array([[0.49288   , 0.52536311, 0.53162294, 0.49938499, 0.45049025,
            0.45462914, 0.51877694, 0.451556  , 0.58314608, 0.43769262,
            0.43759725, 0.45128502, 0.46735404, 0.51183461, 0.45485568,
            0.47747395, 0.55607196, 0.49671747, 0.51115143, 0.53042226],
           [0.64614862, 0.49081109, 0.51652219, 0.53160637, 0.58278671,
            0.58219204, 0.5559344 , 0.52751658, 0.47554518, 0.48787244,
            0.40897282, 0.45165729, 0.4986628 , 0.45568042, 0.47177205,
            0.42475376, 0.51228982, 0.47496702, 0.51810984, 0.55575172],
           [0.44672215, 0.55179277, 0.53664366, 0.45180616, 0.53894875,
            0.34906605, 0.51584597, 0.43214807, 0.56067428, 0.5201479 ,
            0.41624839, 0.41534364, 0.53802002, 0.51185199, 0.51860052,
            0.37737511, 0.49406851, 0.42989753, 0.46073355, 0.51518632],
           [0.49145803, 0.43114171, 0.48010862, 0.53870138, 0.51851854,
            0.46592164, 0.52322175, 0.53633628, 0.53857991, 0.38091769,
            0.48250717, 0.47410379, 0.46511836, 0.43738601, 0.54567555,
            0.45268941, 0.49083205, 0.49732334, 0.52215497, 0.44391537],
           [0.55181315, 0.47335764, 0.47307752, 0.55462193, 0.47268303,
            0.50391643, 0.40237155, 0.52638194, 0.54282961, 0.53983861,
            0.54031275, 0.54090207, 0.5641079 , 0.55524692, 0.5498643 ,
            0.55868992, 0.49561466, 0.43698921, 0.48736908, 0.48964031],
           [0.57080762, 0.54150965, 0.45306951, 0.49469778, 0.39822388,
            0.51189018, 0.44625118, 0.48833954, 0.50482681, 0.51356756,
            0.52358719, 0.57480701, 0.59904274, 0.45298714, 0.49537757,
            0.57317365, 0.44963189, 0.48780013, 0.55218229, 0.48800744],
           [0.41085827, 0.4721218 , 0.55777946, 0.48747705, 0.51827412,
            0.55440009, 0.53449502, 0.48557451, 0.44186694, 0.54249618,
            0.47178047, 0.51373122, 0.41162603, 0.59911708, 0.44308093,
            0.57523582, 0.49899387, 0.51612199, 0.44433574, 0.5094633 ],
           [0.46423999, 0.52110696, 0.54751085, 0.5236267 , 0.52262574,
            0.47574738, 0.47635594, 0.52850375, 0.5180659 , 0.51800327,
            0.56835749, 0.52569787, 0.53232593, 0.51915777, 0.53711474,
            0.50998807, 0.58637746, 0.54203452, 0.41912904, 0.5268633 ],
           [0.38180878, 0.44458991, 0.40858482, 0.43837323, 0.46420371,
            0.50070491, 0.53347177, 0.45829398, 0.5005501 , 0.54819781,
            0.53319082, 0.4935839 , 0.56152169, 0.52772437, 0.4853941 ,
            0.5423685 , 0.53570063, 0.6167742 , 0.56435511, 0.53486022],
           [0.46269484, 0.51369752, 0.48961097, 0.56440869, 0.49927548,
            0.51591196, 0.48664964, 0.58453801, 0.51981355, 0.46520979,
            0.47776371, 0.49952828, 0.39779903, 0.59997996, 0.4037436 ,
            0.40797392, 0.42670726, 0.56891686, 0.58856331, 0.48287125]])

Exercise: The time points are stored in the numpy array t below. Use mask = t > 40; da.sel(time=mask) to select only the data corresponding to time points greater than 40:

t = np.linspace(0, 100, 30)
Solution
mask = t > 40
da.sel(time=mask)
<xarray.DataArray 'image' (x: 10, y: 20, time: 18)> Size: 29kB
array([[[0.13757261, 0.96057452, 0.5748425 , ..., 0.95936073,
         0.79082559, 0.61625175],
        [0.44183571, 0.07861047, 0.73567072, ..., 0.33941244,
         0.40408226, 0.79841416],
        [0.24794045, 0.9764575 , 0.12447803, ..., 0.15686106,
         0.9824433 , 0.43613284],
        ...,
        [0.35794147, 0.66590307, 0.41273549, ..., 0.4208138 ,
         0.55232978, 0.76773044],
        [0.78574505, 0.82838479, 0.77122155, ..., 0.34625546,
         0.95246261, 0.51787839],
        [0.01540937, 0.50071279, 0.95622748, ..., 0.82627506,
         0.77587873, 0.57557807]],
       [[0.91235141, 0.51493098, 0.99368308, ..., 0.54790831,
         0.37801491, 0.73351821],
        [0.66986171, 0.72273336, 0.69928924, ..., 0.02998589,
         0.59142054, 0.50099998],
        [0.7673196 , 0.87744692, 0.46100502, ..., 0.92944393,
         0.146996  , 0.50578664],
...
        [0.7424357 , 0.75116297, 0.87187246, ..., 0.70512213,
         0.89242147, 0.84002832],
        [0.9222603 , 0.61141845, 0.88010536, ..., 0.55602547,
         0.70083685, 0.14360367],
        [0.33411112, 0.7388485 , 0.33820511, ..., 0.42776683,
         0.3088656 , 0.71104696]],
       [[0.86386572, 0.17767093, 0.18565878, ..., 0.42675834,
         0.58977536, 0.23959072],
        [0.51830988, 0.57053531, 0.37161894, ..., 0.33372056,
         0.8140718 , 0.94888713],
        [0.68368334, 0.2472178 , 0.4632849 , ..., 0.11925748,
         0.24569573, 0.57039713],
        ...,
        [0.37178534, 0.04759758, 0.82647154, ..., 0.06054418,
         0.48596833, 0.3115355 ],
        [0.10883332, 0.66146399, 0.07776063, ..., 0.98941264,
         0.21712661, 0.54722449],
        [0.34176214, 0.38624795, 0.40935824, ..., 0.53059179,
         0.85953671, 0.69058448]]], shape=(10, 20, 18))
Dimensions without coordinates: x, y, time
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • time: 18
  • 0.1376 0.9606 0.5748 0.4607 0.3879 ... 0.8072 0.5306 0.8595 0.6906
    array([[[0.13757261, 0.96057452, 0.5748425 , ..., 0.95936073,
             0.79082559, 0.61625175],
            [0.44183571, 0.07861047, 0.73567072, ..., 0.33941244,
             0.40408226, 0.79841416],
            [0.24794045, 0.9764575 , 0.12447803, ..., 0.15686106,
             0.9824433 , 0.43613284],
            ...,
            [0.35794147, 0.66590307, 0.41273549, ..., 0.4208138 ,
             0.55232978, 0.76773044],
            [0.78574505, 0.82838479, 0.77122155, ..., 0.34625546,
             0.95246261, 0.51787839],
            [0.01540937, 0.50071279, 0.95622748, ..., 0.82627506,
             0.77587873, 0.57557807]],
           [[0.91235141, 0.51493098, 0.99368308, ..., 0.54790831,
             0.37801491, 0.73351821],
            [0.66986171, 0.72273336, 0.69928924, ..., 0.02998589,
             0.59142054, 0.50099998],
            [0.7673196 , 0.87744692, 0.46100502, ..., 0.92944393,
             0.146996  , 0.50578664],
    ...
            [0.7424357 , 0.75116297, 0.87187246, ..., 0.70512213,
             0.89242147, 0.84002832],
            [0.9222603 , 0.61141845, 0.88010536, ..., 0.55602547,
             0.70083685, 0.14360367],
            [0.33411112, 0.7388485 , 0.33820511, ..., 0.42776683,
             0.3088656 , 0.71104696]],
           [[0.86386572, 0.17767093, 0.18565878, ..., 0.42675834,
             0.58977536, 0.23959072],
            [0.51830988, 0.57053531, 0.37161894, ..., 0.33372056,
             0.8140718 , 0.94888713],
            [0.68368334, 0.2472178 , 0.4632849 , ..., 0.11925748,
             0.24569573, 0.57039713],
            ...,
            [0.37178534, 0.04759758, 0.82647154, ..., 0.06054418,
             0.48596833, 0.3115355 ],
            [0.10883332, 0.66146399, 0.07776063, ..., 0.98941264,
             0.21712661, 0.54722449],
            [0.34176214, 0.38624795, 0.40935824, ..., 0.53059179,
             0.85953671, 0.69058448]]], shape=(10, 20, 18))

Labeling each Axis using Coordinates and Attributes

Beyond naming dimensions, XArray allows each axis to have coordinate values that describe the physical meaning of each index. For example, a time axis might correspond to timestamps, or a spatial axis might correspond to pixel positions.

Coordinates make it possible to select data based on meaningful values rather than raw indices. For example, selecting frames after a particular time point can be done using coordinate values instead of computing index positions manually.

Example: Run the code below to make a new da using xr.DataArray(), this time additionally mapping the time axis to the time points themselves using coords=:

da = xr.DataArray(
    data=np.random.random(size=(10, 20, 30)),
    name='image',
    dims=['x', 'y', 'time'],
    coords = {
        'time': np.linspace(0, 100, 30),
    }
)
da
<xarray.DataArray 'image' (x: 10, y: 20, time: 30)> Size: 48kB
array([[[0.66411211, 0.61321089, 0.77799478, ..., 0.60487832,
         0.81780103, 0.51288201],
        [0.26831084, 0.63951385, 0.38034877, ..., 0.47071095,
         0.87925474, 0.00362314],
        [0.34497752, 0.30109007, 0.19870258, ..., 0.81097008,
         0.32149049, 0.75546293],
        ...,
        [0.58388595, 0.88828119, 0.93670931, ..., 0.42529124,
         0.49929151, 0.95321901],
        [0.85108608, 0.95602829, 0.2411505 , ..., 0.70897086,
         0.62037308, 0.69338775],
        [0.7415936 , 0.87480223, 0.88257276, ..., 0.5234893 ,
         0.11571473, 0.57900377]],
       [[0.49932819, 0.84881593, 0.93799346, ..., 0.54651641,
         0.72238812, 0.83493695],
        [0.11300725, 0.79783991, 0.32710949, ..., 0.32200166,
         0.11574021, 0.20065044],
        [0.46674217, 0.34070847, 0.54560118, ..., 0.60935145,
         0.43398135, 0.6867824 ],
...
        [0.87345945, 0.97699416, 0.96727549, ..., 0.18244762,
         0.9027874 , 0.89820532],
        [0.56761519, 0.43132623, 0.33496028, ..., 0.0516507 ,
         0.13803519, 0.86534971],
        [0.16781826, 0.44200279, 0.565072  , ..., 0.34858964,
         0.54975653, 0.5794999 ]],
       [[0.9488494 , 0.58982013, 0.02506706, ..., 0.35110223,
         0.65866245, 0.76722738],
        [0.91342788, 0.53646346, 0.24423397, ..., 0.20540218,
         0.75934743, 0.36068679],
        [0.34346403, 0.22931287, 0.421505  , ..., 0.18898619,
         0.44425942, 0.09040306],
        ...,
        [0.81647878, 0.39724468, 0.19077025, ..., 0.78849692,
         0.09476013, 0.22739184],
        [0.28895769, 0.23754446, 0.19223474, ..., 0.72763705,
         0.23681852, 0.90540172],
        [0.00227439, 0.9606731 , 0.07978697, ..., 0.56400248,
         0.47624589, 0.87455739]]], shape=(10, 20, 30))
Coordinates:
  * time     (time) float64 240B 0.0 3.448 6.897 10.34 ... 93.1 96.55 100.0
Dimensions without coordinates: x, y
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • time: 30
  • 0.6641 0.6132 0.778 0.7963 0.2015 ... 0.9717 0.564 0.4762 0.8746
    array([[[0.66411211, 0.61321089, 0.77799478, ..., 0.60487832,
             0.81780103, 0.51288201],
            [0.26831084, 0.63951385, 0.38034877, ..., 0.47071095,
             0.87925474, 0.00362314],
            [0.34497752, 0.30109007, 0.19870258, ..., 0.81097008,
             0.32149049, 0.75546293],
            ...,
            [0.58388595, 0.88828119, 0.93670931, ..., 0.42529124,
             0.49929151, 0.95321901],
            [0.85108608, 0.95602829, 0.2411505 , ..., 0.70897086,
             0.62037308, 0.69338775],
            [0.7415936 , 0.87480223, 0.88257276, ..., 0.5234893 ,
             0.11571473, 0.57900377]],
           [[0.49932819, 0.84881593, 0.93799346, ..., 0.54651641,
             0.72238812, 0.83493695],
            [0.11300725, 0.79783991, 0.32710949, ..., 0.32200166,
             0.11574021, 0.20065044],
            [0.46674217, 0.34070847, 0.54560118, ..., 0.60935145,
             0.43398135, 0.6867824 ],
    ...
            [0.87345945, 0.97699416, 0.96727549, ..., 0.18244762,
             0.9027874 , 0.89820532],
            [0.56761519, 0.43132623, 0.33496028, ..., 0.0516507 ,
             0.13803519, 0.86534971],
            [0.16781826, 0.44200279, 0.565072  , ..., 0.34858964,
             0.54975653, 0.5794999 ]],
           [[0.9488494 , 0.58982013, 0.02506706, ..., 0.35110223,
             0.65866245, 0.76722738],
            [0.91342788, 0.53646346, 0.24423397, ..., 0.20540218,
             0.75934743, 0.36068679],
            [0.34346403, 0.22931287, 0.421505  , ..., 0.18898619,
             0.44425942, 0.09040306],
            ...,
            [0.81647878, 0.39724468, 0.19077025, ..., 0.78849692,
             0.09476013, 0.22739184],
            [0.28895769, 0.23754446, 0.19223474, ..., 0.72763705,
             0.23681852, 0.90540172],
            [0.00227439, 0.9606731 , 0.07978697, ..., 0.56400248,
             0.47624589, 0.87455739]]], shape=(10, 20, 30))
    • time
      (time)
      float64
      0.0 3.448 6.897 ... 96.55 100.0
      array([  0.      ,   3.448276,   6.896552,  10.344828,  13.793103,  17.241379,
              20.689655,  24.137931,  27.586207,  31.034483,  34.482759,  37.931034,
              41.37931 ,  44.827586,  48.275862,  51.724138,  55.172414,  58.62069 ,
              62.068966,  65.517241,  68.965517,  72.413793,  75.862069,  79.310345,
              82.758621,  86.206897,  89.655172,  93.103448,  96.551724, 100.      ])

Exercise: Use da.sel(time = slice(40, None)) to select only the data corresponding to time points greater than 40, without first creating a mask:

Solution
da.sel(time = slice(40, None))
<xarray.DataArray 'image' (x: 10, y: 20, time: 18)> Size: 29kB
array([[[0.74955319, 0.93666601, 0.21106009, ..., 0.60487832,
         0.81780103, 0.51288201],
        [0.74350475, 0.76859113, 0.6631906 , ..., 0.47071095,
         0.87925474, 0.00362314],
        [0.66446173, 0.47080019, 0.57421052, ..., 0.81097008,
         0.32149049, 0.75546293],
        ...,
        [0.22623357, 0.79544261, 0.76282197, ..., 0.42529124,
         0.49929151, 0.95321901],
        [0.28092757, 0.31001275, 0.3237857 , ..., 0.70897086,
         0.62037308, 0.69338775],
        [0.74485266, 0.35880614, 0.76201665, ..., 0.5234893 ,
         0.11571473, 0.57900377]],
       [[0.08429232, 0.28335178, 0.08041266, ..., 0.54651641,
         0.72238812, 0.83493695],
        [0.90843037, 0.24412804, 0.364648  , ..., 0.32200166,
         0.11574021, 0.20065044],
        [0.16273679, 0.06395167, 0.89485638, ..., 0.60935145,
         0.43398135, 0.6867824 ],
...
        [0.40562632, 0.32612597, 0.00543518, ..., 0.18244762,
         0.9027874 , 0.89820532],
        [0.5031834 , 0.10657601, 0.57742191, ..., 0.0516507 ,
         0.13803519, 0.86534971],
        [0.90455835, 0.63873043, 0.59379362, ..., 0.34858964,
         0.54975653, 0.5794999 ]],
       [[0.66665116, 0.13452167, 0.54294749, ..., 0.35110223,
         0.65866245, 0.76722738],
        [0.37659058, 0.64074041, 0.03360847, ..., 0.20540218,
         0.75934743, 0.36068679],
        [0.50707105, 0.99618072, 0.73632629, ..., 0.18898619,
         0.44425942, 0.09040306],
        ...,
        [0.25492463, 0.21089898, 0.11479766, ..., 0.78849692,
         0.09476013, 0.22739184],
        [0.06341571, 0.54083715, 0.31701064, ..., 0.72763705,
         0.23681852, 0.90540172],
        [0.69922958, 0.6827144 , 0.52977601, ..., 0.56400248,
         0.47624589, 0.87455739]]], shape=(10, 20, 18))
Coordinates:
  * time     (time) float64 144B 41.38 44.83 48.28 51.72 ... 93.1 96.55 100.0
Dimensions without coordinates: x, y
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • time: 18
  • 0.7496 0.9367 0.2111 0.9105 0.7847 ... 0.9717 0.564 0.4762 0.8746
    array([[[0.74955319, 0.93666601, 0.21106009, ..., 0.60487832,
             0.81780103, 0.51288201],
            [0.74350475, 0.76859113, 0.6631906 , ..., 0.47071095,
             0.87925474, 0.00362314],
            [0.66446173, 0.47080019, 0.57421052, ..., 0.81097008,
             0.32149049, 0.75546293],
            ...,
            [0.22623357, 0.79544261, 0.76282197, ..., 0.42529124,
             0.49929151, 0.95321901],
            [0.28092757, 0.31001275, 0.3237857 , ..., 0.70897086,
             0.62037308, 0.69338775],
            [0.74485266, 0.35880614, 0.76201665, ..., 0.5234893 ,
             0.11571473, 0.57900377]],
           [[0.08429232, 0.28335178, 0.08041266, ..., 0.54651641,
             0.72238812, 0.83493695],
            [0.90843037, 0.24412804, 0.364648  , ..., 0.32200166,
             0.11574021, 0.20065044],
            [0.16273679, 0.06395167, 0.89485638, ..., 0.60935145,
             0.43398135, 0.6867824 ],
    ...
            [0.40562632, 0.32612597, 0.00543518, ..., 0.18244762,
             0.9027874 , 0.89820532],
            [0.5031834 , 0.10657601, 0.57742191, ..., 0.0516507 ,
             0.13803519, 0.86534971],
            [0.90455835, 0.63873043, 0.59379362, ..., 0.34858964,
             0.54975653, 0.5794999 ]],
           [[0.66665116, 0.13452167, 0.54294749, ..., 0.35110223,
             0.65866245, 0.76722738],
            [0.37659058, 0.64074041, 0.03360847, ..., 0.20540218,
             0.75934743, 0.36068679],
            [0.50707105, 0.99618072, 0.73632629, ..., 0.18898619,
             0.44425942, 0.09040306],
            ...,
            [0.25492463, 0.21089898, 0.11479766, ..., 0.78849692,
             0.09476013, 0.22739184],
            [0.06341571, 0.54083715, 0.31701064, ..., 0.72763705,
             0.23681852, 0.90540172],
            [0.69922958, 0.6827144 , 0.52977601, ..., 0.56400248,
             0.47624589, 0.87455739]]], shape=(10, 20, 18))
    • time
      (time)
      float64
      41.38 44.83 48.28 ... 96.55 100.0
      array([ 41.37931 ,  44.827586,  48.275862,  51.724138,  55.172414,  58.62069 ,
              62.068966,  65.517241,  68.965517,  72.413793,  75.862069,  79.310345,
              82.758621,  86.206897,  89.655172,  93.103448,  96.551724, 100.      ])

Adding Descriptions to the data

Support for basic data descriptions is quite extensive. Things like units, long names for plotting, processing history, and even descriptions for explaining each part of the data are supported by adding the data to a dictionary attached to xarray objects called attrs. Some keys are recognized by other tooling (e.g. units, description, long_name), but for the most part, any kind of key-value combination is supported for metadata.

Example: Run the code below to create a new da DataArray using DataArray, this time with extra attributes describing the main variables.

time = xr.DataArray(
    data = np.linspace(0, 100, 30),
    name = 'time',
    dims=['time'],
    attrs = {
        'units': 's',
        'description': 'time samples for each image frame'
    }
)

da = xr.DataArray(
    data=np.random.random(size=(10, 20, 30)),
    name='image',
    dims=['x', 'y', 'time'],
    coords = {
        'time': time,
    },
    attrs = {
        'units': 'brightness',
        'description': 'a generated random image stack',
        'long_name': 'calcium image pixel brightness',
    }
)
da
<xarray.DataArray 'image' (x: 10, y: 20, time: 30)> Size: 48kB
array([[[0.82354816, 0.52292853, 0.10666037, ..., 0.42289179,
         0.99782003, 0.49607635],
        [0.12985498, 0.12763057, 0.07046139, ..., 0.64814128,
         0.58234373, 0.12883534],
        [0.55022341, 0.34076009, 0.81538328, ..., 0.57887583,
         0.8056537 , 0.13388089],
        ...,
        [0.26854525, 0.53283331, 0.63230879, ..., 0.13280682,
         0.72024067, 0.71239432],
        [0.57303676, 0.19654853, 0.95910557, ..., 0.86204161,
         0.82693005, 0.73520056],
        [0.31431091, 0.33030612, 0.30249442, ..., 0.40123431,
         0.30194928, 0.351028  ]],
       [[0.38628946, 0.7558288 , 0.78802452, ..., 0.79105905,
         0.89664269, 0.65264137],
        [0.36112009, 0.78401218, 0.9242207 , ..., 0.49747566,
         0.98220942, 0.54447116],
        [0.78281561, 0.62245213, 0.3897531 , ..., 0.58020462,
         0.73119609, 0.10952419],
...
        [0.80265334, 0.53560826, 0.89920452, ..., 0.66503514,
         0.76782656, 0.31708577],
        [0.66716965, 0.6248026 , 0.78283432, ..., 0.94025448,
         0.15667577, 0.02134525],
        [0.38662875, 0.13445524, 0.60421954, ..., 0.69339363,
         0.79644892, 0.03368359]],
       [[0.74141697, 0.90176798, 0.77728334, ..., 0.13535366,
         0.13664637, 0.68282886],
        [0.1595881 , 0.70037268, 0.27823299, ..., 0.44081631,
         0.52533783, 0.58288148],
        [0.58639489, 0.9931304 , 0.19861053, ..., 0.4506233 ,
         0.2062197 , 0.79268328],
        ...,
        [0.53752456, 0.96575008, 0.46299236, ..., 0.45842997,
         0.05353041, 0.00610113],
        [0.99512609, 0.41493162, 0.43907269, ..., 0.23812992,
         0.56804535, 0.24241836],
        [0.75024963, 0.90986467, 0.980608  , ..., 0.91442848,
         0.91002944, 0.61638018]]], shape=(10, 20, 30))
Coordinates:
  * time     (time) float64 240B 0.0 3.448 6.897 10.34 ... 93.1 96.55 100.0
Dimensions without coordinates: x, y
Attributes:
    units:        brightness
    description:  a generated random image stack
    long_name:    calcium image pixel brightness
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • time: 30
  • 0.8235 0.5229 0.1067 0.4261 0.6436 ... 0.1459 0.9144 0.91 0.6164
    array([[[0.82354816, 0.52292853, 0.10666037, ..., 0.42289179,
             0.99782003, 0.49607635],
            [0.12985498, 0.12763057, 0.07046139, ..., 0.64814128,
             0.58234373, 0.12883534],
            [0.55022341, 0.34076009, 0.81538328, ..., 0.57887583,
             0.8056537 , 0.13388089],
            ...,
            [0.26854525, 0.53283331, 0.63230879, ..., 0.13280682,
             0.72024067, 0.71239432],
            [0.57303676, 0.19654853, 0.95910557, ..., 0.86204161,
             0.82693005, 0.73520056],
            [0.31431091, 0.33030612, 0.30249442, ..., 0.40123431,
             0.30194928, 0.351028  ]],
           [[0.38628946, 0.7558288 , 0.78802452, ..., 0.79105905,
             0.89664269, 0.65264137],
            [0.36112009, 0.78401218, 0.9242207 , ..., 0.49747566,
             0.98220942, 0.54447116],
            [0.78281561, 0.62245213, 0.3897531 , ..., 0.58020462,
             0.73119609, 0.10952419],
    ...
            [0.80265334, 0.53560826, 0.89920452, ..., 0.66503514,
             0.76782656, 0.31708577],
            [0.66716965, 0.6248026 , 0.78283432, ..., 0.94025448,
             0.15667577, 0.02134525],
            [0.38662875, 0.13445524, 0.60421954, ..., 0.69339363,
             0.79644892, 0.03368359]],
           [[0.74141697, 0.90176798, 0.77728334, ..., 0.13535366,
             0.13664637, 0.68282886],
            [0.1595881 , 0.70037268, 0.27823299, ..., 0.44081631,
             0.52533783, 0.58288148],
            [0.58639489, 0.9931304 , 0.19861053, ..., 0.4506233 ,
             0.2062197 , 0.79268328],
            ...,
            [0.53752456, 0.96575008, 0.46299236, ..., 0.45842997,
             0.05353041, 0.00610113],
            [0.99512609, 0.41493162, 0.43907269, ..., 0.23812992,
             0.56804535, 0.24241836],
            [0.75024963, 0.90986467, 0.980608  , ..., 0.91442848,
             0.91002944, 0.61638018]]], shape=(10, 20, 30))
    • time
      (time)
      float64
      0.0 3.448 6.897 ... 96.55 100.0
      units :
      s
      description :
      time samples for each image frame
      array([  0.      ,   3.448276,   6.896552,  10.344828,  13.793103,  17.241379,
              20.689655,  24.137931,  27.586207,  31.034483,  34.482759,  37.931034,
              41.37931 ,  44.827586,  48.275862,  51.724138,  55.172414,  58.62069 ,
              62.068966,  65.517241,  68.965517,  72.413793,  75.862069,  79.310345,
              82.758621,  86.206897,  89.655172,  93.103448,  96.551724, 100.      ])
  • units :
    brightness
    description :
    a generated random image stack
    long_name :
    calcium image pixel brightness

Exercise: View the attributes of the da DataArray with da.attrs

Solution
da.attrs
{'units': 'brightness',
 'description': 'a generated random image stack',
 'long_name': 'calcium image pixel brightness'}

Exercise: View the attributes of the time coordinate on the da DataArray with da.time.attrs:

Solution
da.time.attrs
{'units': 's', 'description': 'time samples for each image frame'}

Exercise: Plot the mean pixel brightness over time and check that some attributes are used automatically in the plot, with da.mean(dim=['x', 'y']).plot():

Solution
da.mean(dim=['x', 'y']).plot();

There are many, many more features that XArray provides to add convenience to an analysis, but this should be enough to get us started.

Example: Wrap a 2-D NumPy array of EEG-like data in a DataArray with named dimensions, coordinates, and a name.

Solution
rng = np.random.default_rng(0)
time = np.linspace(0, 2, 1000)
channels = ['Fz', 'Cz', 'Pz', 'Oz']
values = rng.normal(size=(len(channels), len(time)))

eeg = xr.DataArray(
    data=values,
    dims=['channel', 'time'],
    coords={'channel': channels, 'time': time},
    name='eeg',
)
eeg
<xarray.DataArray 'eeg' (channel: 4, time: 1000)> Size: 32kB
array([[ 0.12573022, -0.13210486,  0.64042265, ...,  1.38997173,
        -0.66434209, -0.22997116],
       [ 1.18390191,  0.30382685,  0.19205435, ..., -2.01881162,
        -0.90942756,  0.36922933],
       [ 0.41925483, -0.50224455, -0.85769996, ..., -1.65759515,
        -1.99552682, -0.61334415],
       [-0.38847831, -0.27536744, -1.24011117, ...,  0.55029559,
         0.55104841, -0.8705243 ]], shape=(4, 1000))
Coordinates:
  * channel  (channel) <U2 32B 'Fz' 'Cz' 'Pz' 'Oz'
  * time     (time) float64 8kB 0.0 0.002002 0.004004 ... 1.996 1.998 2.0
xarray.DataArray
'eeg'
  • channel: 4
  • time: 1000
  • 0.1257 -0.1321 0.6404 0.1049 -0.5357 ... -0.4626 0.5503 0.551 -0.8705
    array([[ 0.12573022, -0.13210486,  0.64042265, ...,  1.38997173,
            -0.66434209, -0.22997116],
           [ 1.18390191,  0.30382685,  0.19205435, ..., -2.01881162,
            -0.90942756,  0.36922933],
           [ 0.41925483, -0.50224455, -0.85769996, ..., -1.65759515,
            -1.99552682, -0.61334415],
           [-0.38847831, -0.27536744, -1.24011117, ...,  0.55029559,
             0.55104841, -0.8705243 ]], shape=(4, 1000))
    • channel
      (channel)
      <U2
      'Fz' 'Cz' 'Pz' 'Oz'
      array(['Fz', 'Cz', 'Pz', 'Oz'], dtype='<U2')
    • time
      (time)
      float64
      0.0 0.002002 0.004004 ... 1.998 2.0
      array([0.      , 0.002002, 0.004004, ..., 1.995996, 1.997998, 2.      ],
            shape=(1000,))

Exercise: Get the time coordinate of the eeg DataArray.

Solution
eeg['time']
<xarray.DataArray 'time' (time: 1000)> Size: 8kB
array([0.      , 0.002002, 0.004004, ..., 1.995996, 1.997998, 2.      ],
      shape=(1000,))
Coordinates:
  * time     (time) float64 8kB 0.0 0.002002 0.004004 ... 1.996 1.998 2.0
xarray.DataArray
'time'
  • time: 1000
  • 0.0 0.002002 0.004004 0.006006 0.008008 ... 1.994 1.996 1.998 2.0
    array([0.      , 0.002002, 0.004004, ..., 1.995996, 1.997998, 2.      ],
          shape=(1000,))
    • time
      (time)
      float64
      0.0 0.002002 0.004004 ... 1.998 2.0
      array([0.      , 0.002002, 0.004004, ..., 1.995996, 1.997998, 2.      ],
            shape=(1000,))

Exercise: Select the channel labelled 'Cz' using .sel.

Solution
eeg.sel(channel='Cz')
<xarray.DataArray 'eeg' (time: 1000)> Size: 8kB
array([ 1.18390191e+00,  3.03826845e-01,  1.92054350e-01,  2.65943448e-01,
       -1.36615788e+00, -3.89553474e-01, -9.56445654e-01,  1.97413652e-01,
       -5.43998130e-01, -4.40591215e-02, -7.72925633e-02, -3.63703849e-02,
       -3.47471219e-02, -6.52378106e-01, -1.05400280e+00, -6.64356309e-01,
        1.07153834e+00,  3.74161806e-01,  5.86955361e-01,  1.37996792e+00,
       -1.17943093e+00,  5.09952421e-01, -1.07507411e+00, -3.34332599e-01,
        4.84239843e-01,  1.61434527e+00, -7.82164942e-01, -9.47962536e-02,
        1.15623680e+00, -1.48980812e+00,  3.62112915e-01, -3.08278150e-01,
       -8.81694971e-01,  1.46630823e-01,  5.94925862e-01, -9.12099762e-01,
        3.79989782e-01,  1.73369990e-01, -1.24181331e+00,  1.55341287e+00,
        1.08990267e+00, -8.59926748e-01, -5.86630883e-01,  7.70780234e-01,
       -4.95449047e-01, -1.83971861e+00,  1.04888211e+00,  8.78142297e-03,
        1.90702619e+00,  3.57467123e-01,  1.90992489e-01,  2.87450048e+00,
       -1.71867346e-01, -9.51815765e-01,  2.29197980e-01,  1.13575490e+00,
       -1.16512014e+00, -9.08265109e-01,  4.49777098e-01, -3.19734539e+00,
       -1.09267866e+00,  7.95484082e-01, -5.86739916e-01, -1.62648313e+00,
        1.92556666e+00, -1.41053516e+00, -5.23365547e-01, -3.72731499e-01,
        8.31443940e-02, -3.69505202e-01, -8.09650786e-02,  5.74945791e-02,
       -8.66764288e-02,  9.32738407e-02, -2.37892569e+00,  4.41064698e-01,
       -1.40445670e+00, -2.16664551e+00,  1.38134791e+00, -1.28551528e+00,
...
       -2.04496213e-02, -3.17708018e-01, -1.40671992e+00,  1.43973441e+00,
        1.56344292e+00, -5.64182503e-01,  5.21928192e-01, -1.48806679e-01,
        2.43440708e-01,  1.53213096e+00, -2.68640614e-01,  1.79216748e+00,
       -2.88194853e-01, -9.35539343e-02,  1.88931744e+00,  2.75217197e-01,
       -2.03194163e+00,  2.24802875e-01, -1.28185323e+00, -1.66260289e+00,
       -9.42642864e-01,  3.26080381e-01, -1.11336765e+00, -1.40125851e+00,
       -3.79761532e-01,  4.04610648e-01, -6.96378284e-01, -1.30643639e+00,
       -2.17269261e+00, -7.76202488e-01,  8.44308442e-01,  1.00209022e+00,
        7.89811452e-01, -1.75291242e-01,  1.29351384e-01,  1.26770345e+00,
       -8.68677427e-01,  3.64075576e-01, -1.87089640e+00,  6.82297662e-01,
       -4.51043039e-01, -1.15563144e+00,  7.72833948e-01, -1.11123495e+00,
       -8.67281678e-01,  1.53681842e+00, -1.04602508e+00, -6.56005027e-01,
       -6.83254494e-01,  7.84800952e-01, -1.83591170e+00,  1.25039568e+00,
       -2.43353864e-01, -3.16866355e-01, -8.96681417e-01, -1.45451328e-01,
        1.47039352e+00,  2.12852386e-01, -9.32540192e-01, -3.47355163e-01,
        5.34453067e-01,  8.53250722e-02, -2.57310592e-01,  6.04572913e-01,
        3.61086685e-01, -1.07771601e+00, -1.91491635e+00,  5.00488203e-01,
        1.85744412e+00, -8.20731394e-01, -1.41011717e-01,  1.35408138e+00,
        1.73963397e+00,  6.52358877e-02,  1.24972527e+00,  7.50633971e-01,
       -5.55815730e-01, -2.01881162e+00, -9.09427562e-01,  3.69229331e-01])
Coordinates:
  * time     (time) float64 8kB 0.0 0.002002 0.004004 ... 1.996 1.998 2.0
    channel  <U2 8B 'Cz'
xarray.DataArray
'eeg'
  • time: 1000
  • 1.184 0.3038 0.1921 0.2659 -1.366 ... -0.5558 -2.019 -0.9094 0.3692
    array([ 1.18390191e+00,  3.03826845e-01,  1.92054350e-01,  2.65943448e-01,
           -1.36615788e+00, -3.89553474e-01, -9.56445654e-01,  1.97413652e-01,
           -5.43998130e-01, -4.40591215e-02, -7.72925633e-02, -3.63703849e-02,
           -3.47471219e-02, -6.52378106e-01, -1.05400280e+00, -6.64356309e-01,
            1.07153834e+00,  3.74161806e-01,  5.86955361e-01,  1.37996792e+00,
           -1.17943093e+00,  5.09952421e-01, -1.07507411e+00, -3.34332599e-01,
            4.84239843e-01,  1.61434527e+00, -7.82164942e-01, -9.47962536e-02,
            1.15623680e+00, -1.48980812e+00,  3.62112915e-01, -3.08278150e-01,
           -8.81694971e-01,  1.46630823e-01,  5.94925862e-01, -9.12099762e-01,
            3.79989782e-01,  1.73369990e-01, -1.24181331e+00,  1.55341287e+00,
            1.08990267e+00, -8.59926748e-01, -5.86630883e-01,  7.70780234e-01,
           -4.95449047e-01, -1.83971861e+00,  1.04888211e+00,  8.78142297e-03,
            1.90702619e+00,  3.57467123e-01,  1.90992489e-01,  2.87450048e+00,
           -1.71867346e-01, -9.51815765e-01,  2.29197980e-01,  1.13575490e+00,
           -1.16512014e+00, -9.08265109e-01,  4.49777098e-01, -3.19734539e+00,
           -1.09267866e+00,  7.95484082e-01, -5.86739916e-01, -1.62648313e+00,
            1.92556666e+00, -1.41053516e+00, -5.23365547e-01, -3.72731499e-01,
            8.31443940e-02, -3.69505202e-01, -8.09650786e-02,  5.74945791e-02,
           -8.66764288e-02,  9.32738407e-02, -2.37892569e+00,  4.41064698e-01,
           -1.40445670e+00, -2.16664551e+00,  1.38134791e+00, -1.28551528e+00,
    ...
           -2.04496213e-02, -3.17708018e-01, -1.40671992e+00,  1.43973441e+00,
            1.56344292e+00, -5.64182503e-01,  5.21928192e-01, -1.48806679e-01,
            2.43440708e-01,  1.53213096e+00, -2.68640614e-01,  1.79216748e+00,
           -2.88194853e-01, -9.35539343e-02,  1.88931744e+00,  2.75217197e-01,
           -2.03194163e+00,  2.24802875e-01, -1.28185323e+00, -1.66260289e+00,
           -9.42642864e-01,  3.26080381e-01, -1.11336765e+00, -1.40125851e+00,
           -3.79761532e-01,  4.04610648e-01, -6.96378284e-01, -1.30643639e+00,
           -2.17269261e+00, -7.76202488e-01,  8.44308442e-01,  1.00209022e+00,
            7.89811452e-01, -1.75291242e-01,  1.29351384e-01,  1.26770345e+00,
           -8.68677427e-01,  3.64075576e-01, -1.87089640e+00,  6.82297662e-01,
           -4.51043039e-01, -1.15563144e+00,  7.72833948e-01, -1.11123495e+00,
           -8.67281678e-01,  1.53681842e+00, -1.04602508e+00, -6.56005027e-01,
           -6.83254494e-01,  7.84800952e-01, -1.83591170e+00,  1.25039568e+00,
           -2.43353864e-01, -3.16866355e-01, -8.96681417e-01, -1.45451328e-01,
            1.47039352e+00,  2.12852386e-01, -9.32540192e-01, -3.47355163e-01,
            5.34453067e-01,  8.53250722e-02, -2.57310592e-01,  6.04572913e-01,
            3.61086685e-01, -1.07771601e+00, -1.91491635e+00,  5.00488203e-01,
            1.85744412e+00, -8.20731394e-01, -1.41011717e-01,  1.35408138e+00,
            1.73963397e+00,  6.52358877e-02,  1.24972527e+00,  7.50633971e-01,
           -5.55815730e-01, -2.01881162e+00, -9.09427562e-01,  3.69229331e-01])
    • time
      (time)
      float64
      0.0 0.002002 0.004004 ... 1.998 2.0
      array([0.      , 0.002002, 0.004004, ..., 1.995996, 1.997998, 2.      ],
            shape=(1000,))
    • channel
      ()
      <U2
      'Cz'
      array('Cz', dtype='<U2')

Exercise: Select the data between 0.5 and 1.0 seconds for channels 'Fz' and 'Cz'.

Solution
eeg.sel(time=slice(0.5, 1.0), channel=['Fz', 'Cz'])
<xarray.DataArray 'eeg' (channel: 2, time: 250)> Size: 4kB
array([[ 2.01910002e-01, -3.88350386e-02,  1.06632455e+00,
        -9.21633924e-01,  8.04716931e-01,  8.52748471e-01,
        -6.67687292e-01,  1.63244006e-01, -8.30751957e-01,
         2.34580807e+00, -7.04139562e-01, -4.53074444e-01,
        -1.06583802e+00, -3.46121275e-01, -5.87602670e-03,
         7.67789135e-01, -6.10486646e-01, -1.85773959e-01,
        -1.41648937e+00, -8.27402227e-01,  2.75580756e+00,
         1.04124319e+00, -7.81424042e-01, -1.33739724e+00,
        -9.75582831e-01, -2.16908595e-02,  3.47277884e-02,
        -7.44360601e-01, -1.28657447e+00,  1.42237851e+00,
         4.51685451e-01, -3.74568020e-01, -2.20661208e-01,
        -5.29530459e-01, -2.93604545e+00,  1.15661827e-01,
        -1.07054441e+00, -1.00268430e+00, -6.40262405e-01,
         7.32301715e-01, -1.17053081e+00, -1.43428146e+00,
         6.39852075e-01,  7.54368905e-01, -9.58933708e-01,
         5.62397677e-01, -2.91632430e-01,  3.01292177e-01,
        -1.26096028e+00,  8.32894449e-01,  1.20325895e+00,
         6.37073236e-01,  5.58339962e-01, -3.77227516e+00,
         2.60629749e-01, -2.54453167e-02, -1.47045507e-01,
        -6.30577990e-01,  5.53649749e-02,  4.12117405e-01,
...
        -9.28744286e-01, -1.12398084e+00, -6.40181104e-02,
         3.23613327e-01,  5.03171892e-01,  7.81969353e-01,
         9.79585129e-01, -2.40307670e+00,  9.03440333e-01,
        -1.04744778e+00,  1.11406857e+00,  3.36644328e-01,
         6.30559818e-01, -5.40925748e-01,  2.82651321e-01,
        -1.86721258e+00,  1.09524662e+00, -3.30838739e-01,
        -2.58908029e+00,  1.06148355e+00,  1.67677373e+00,
        -8.76129516e-01,  1.21270989e+00, -7.49755451e-01,
        -2.26652063e+00, -3.57892474e-01,  3.26136452e-01,
         1.33551129e-01,  1.03597949e-01,  2.96422858e-01,
         2.16555664e-01, -2.16038303e-01, -6.01381373e-01,
        -2.65800848e-01, -3.51672566e-01, -3.02922623e-01,
        -1.10295343e+00, -5.42594385e-01,  8.22261888e-02,
         3.09418910e-01, -6.40289475e-02, -1.83659914e-01,
         5.34449874e-01,  8.14468661e-01,  6.80079879e-01,
        -1.74990909e+00, -4.55405091e-01,  7.05654587e-01,
         5.15397324e-01,  6.79810601e-01,  3.14351508e-01,
        -8.53536802e-01,  1.22256453e+00, -1.35958887e+00,
         9.85757792e-01, -1.63751312e-02, -1.47265422e+00,
        -3.96158801e-01]])
Coordinates:
  * channel  (channel) <U2 16B 'Fz' 'Cz'
  * time     (time) float64 2kB 0.5005 0.5025 0.5045 ... 0.995 0.997 0.999
xarray.DataArray
'eeg'
  • channel: 2
  • time: 250
  • 0.2019 -0.03884 1.066 -0.9216 ... 0.9858 -0.01638 -1.473 -0.3962
    array([[ 2.01910002e-01, -3.88350386e-02,  1.06632455e+00,
            -9.21633924e-01,  8.04716931e-01,  8.52748471e-01,
            -6.67687292e-01,  1.63244006e-01, -8.30751957e-01,
             2.34580807e+00, -7.04139562e-01, -4.53074444e-01,
            -1.06583802e+00, -3.46121275e-01, -5.87602670e-03,
             7.67789135e-01, -6.10486646e-01, -1.85773959e-01,
            -1.41648937e+00, -8.27402227e-01,  2.75580756e+00,
             1.04124319e+00, -7.81424042e-01, -1.33739724e+00,
            -9.75582831e-01, -2.16908595e-02,  3.47277884e-02,
            -7.44360601e-01, -1.28657447e+00,  1.42237851e+00,
             4.51685451e-01, -3.74568020e-01, -2.20661208e-01,
            -5.29530459e-01, -2.93604545e+00,  1.15661827e-01,
            -1.07054441e+00, -1.00268430e+00, -6.40262405e-01,
             7.32301715e-01, -1.17053081e+00, -1.43428146e+00,
             6.39852075e-01,  7.54368905e-01, -9.58933708e-01,
             5.62397677e-01, -2.91632430e-01,  3.01292177e-01,
            -1.26096028e+00,  8.32894449e-01,  1.20325895e+00,
             6.37073236e-01,  5.58339962e-01, -3.77227516e+00,
             2.60629749e-01, -2.54453167e-02, -1.47045507e-01,
            -6.30577990e-01,  5.53649749e-02,  4.12117405e-01,
    ...
            -9.28744286e-01, -1.12398084e+00, -6.40181104e-02,
             3.23613327e-01,  5.03171892e-01,  7.81969353e-01,
             9.79585129e-01, -2.40307670e+00,  9.03440333e-01,
            -1.04744778e+00,  1.11406857e+00,  3.36644328e-01,
             6.30559818e-01, -5.40925748e-01,  2.82651321e-01,
            -1.86721258e+00,  1.09524662e+00, -3.30838739e-01,
            -2.58908029e+00,  1.06148355e+00,  1.67677373e+00,
            -8.76129516e-01,  1.21270989e+00, -7.49755451e-01,
            -2.26652063e+00, -3.57892474e-01,  3.26136452e-01,
             1.33551129e-01,  1.03597949e-01,  2.96422858e-01,
             2.16555664e-01, -2.16038303e-01, -6.01381373e-01,
            -2.65800848e-01, -3.51672566e-01, -3.02922623e-01,
            -1.10295343e+00, -5.42594385e-01,  8.22261888e-02,
             3.09418910e-01, -6.40289475e-02, -1.83659914e-01,
             5.34449874e-01,  8.14468661e-01,  6.80079879e-01,
            -1.74990909e+00, -4.55405091e-01,  7.05654587e-01,
             5.15397324e-01,  6.79810601e-01,  3.14351508e-01,
            -8.53536802e-01,  1.22256453e+00, -1.35958887e+00,
             9.85757792e-01, -1.63751312e-02, -1.47265422e+00,
            -3.96158801e-01]])
    • channel
      (channel)
      <U2
      'Fz' 'Cz'
      array(['Fz', 'Cz'], dtype='<U2')
    • time
      (time)
      float64
      0.5005 0.5025 ... 0.997 0.999
      array([0.500501, 0.502503, 0.504505, ..., 0.994995, 0.996997, 0.998999],
            shape=(250,))

Exercise: Compute the mean across time for every channel using the dimension name (not the axis number).

Solution
eeg.mean(dim='time')
<xarray.DataArray 'eeg' (channel: 4)> Size: 32B
array([-0.04802828, -0.00802289, -0.04586215,  0.04150495])
Coordinates:
  * channel  (channel) <U2 32B 'Fz' 'Cz' 'Pz' 'Oz'
xarray.DataArray
'eeg'
  • channel: 4
  • -0.04803 -0.008023 -0.04586 0.0415
    array([-0.04802828, -0.00802289, -0.04586215,  0.04150495])
    • channel
      (channel)
      <U2
      'Fz' 'Cz' 'Pz' 'Oz'
      array(['Fz', 'Cz', 'Pz', 'Oz'], dtype='<U2')

Exercise: Attach metadata so that eeg has units = 'uV' and description = 'simulated EEG data', and the time coordinate has units = 's'. Then plot eeg.sel(channel='Cz') and check that the axis labels in the plot use the attached metadata.

Solution
eeg.attrs = {'units': 'uV', 'description': 'simulated EEG data'}
eeg['time'].attrs = {'units': 's'}
eeg.sel(channel='Cz').plot();

Section 3: Saving and Loading DataArrays

Background

Once data is organized in an Xarray structure, it can easily be saved to disk using scientific file formats such as NetCDF4, which is built on top of the HDF5 storage system. These formats are widely used in scientific computing because they support structured metadata, multidimensional datasets, and efficient storage of large arrays.

Saving data in these formats allows large datasets to be stored and accessed efficiently without requiring them to be fully loaded into memory. It also makes the data portable and accessible to tools outside of Python.

Exercises

In this section you will save a DataArray to a NetCDF file and load it back into memory.

Code Description
Path('data').mkdir(exist_ok=True) Create a directory if it does not already exist.
da.to_netcdf('data/file.nc') Write a DataArray to a NetCDF file.
xr.load_dataarray('data/file.nc') Read a DataArray back into memory from a file.

Exercise: Run the cell below to create a DataArray assigned to a variable da. Then, use da.to_netcdf() with the engine='netcdf4' option to create an HDF5-compatible file called example.nc.

time = xr.DataArray(
    data = np.linspace(0, 100, 30),
    name = 'time',
    dims=['time'],
    attrs = {
        'units': 's',
        'description': 'time samples for each image frame'
    }
)

da = xr.DataArray(
    data=np.random.random(size=(10, 20, 30)),
    name='image',
    dims=['x', 'y', 'time'],
    coords = {
        'time': time,
    },
    attrs = {
        'units': 'brightness',
        'description': 'A generated random image stack',
        'long_name': 'calcium image pixel brightness',
    }
)
da
<xarray.DataArray 'image' (x: 10, y: 20, time: 30)> Size: 48kB
array([[[0.93455784, 0.87070321, 0.25765789, ..., 0.36804857,
         0.20330511, 0.22250059],
        [0.26910653, 0.32319007, 0.61266363, ..., 0.72065349,
         0.69022405, 0.9841665 ],
        [0.30866584, 0.34258123, 0.37106183, ..., 0.87508457,
         0.54426929, 0.53391249],
        ...,
        [0.47750194, 0.09396009, 0.16947604, ..., 0.82964438,
         0.9052506 , 0.70531234],
        [0.28216774, 0.06112643, 0.01806789, ..., 0.38480884,
         0.37651963, 0.15904927],
        [0.54590487, 0.22034884, 0.44358047, ..., 0.56451792,
         0.15344121, 0.47713979]],
       [[0.46278164, 0.37850388, 0.52076647, ..., 0.20957928,
         0.14561883, 0.13893709],
        [0.17786602, 0.05443162, 0.08487059, ..., 0.57080271,
         0.03242944, 0.6644074 ],
        [0.29483855, 0.23221452, 0.77716782, ..., 0.50057916,
         0.49205944, 0.21075994],
...
        [0.73339498, 0.07334617, 0.36866892, ..., 0.23494169,
         0.72033225, 0.56247379],
        [0.885213  , 0.97839821, 0.53160162, ..., 0.91053768,
         0.57536331, 0.37315945],
        [0.58191917, 0.68534983, 0.17020612, ..., 0.87829471,
         0.73874034, 0.41930747]],
       [[0.4606276 , 0.0973907 , 0.70948389, ..., 0.56368729,
         0.74048363, 0.5122833 ],
        [0.45101316, 0.08566925, 0.70246856, ..., 0.47707637,
         0.6644182 , 0.53748297],
        [0.55387455, 0.17310703, 0.72945653, ..., 0.67118948,
         0.71301375, 0.87009826],
        ...,
        [0.32045915, 0.95910132, 0.01923958, ..., 0.52898408,
         0.78974531, 0.96774799],
        [0.86897641, 0.84767354, 0.18733604, ..., 0.14231991,
         0.99106918, 0.37830103],
        [0.40767868, 0.37804333, 0.43709734, ..., 0.4875185 ,
         0.01975035, 0.5726516 ]]], shape=(10, 20, 30))
Coordinates:
  * time     (time) float64 240B 0.0 3.448 6.897 10.34 ... 93.1 96.55 100.0
Dimensions without coordinates: x, y
Attributes:
    units:        brightness
    description:  A generated random image stack
    long_name:    calcium image pixel brightness
xarray.DataArray
'image'
  • x: 10
  • y: 20
  • time: 30
  • 0.9346 0.8707 0.2577 0.8091 0.4525 ... 0.08951 0.4875 0.01975 0.5727
    array([[[0.93455784, 0.87070321, 0.25765789, ..., 0.36804857,
             0.20330511, 0.22250059],
            [0.26910653, 0.32319007, 0.61266363, ..., 0.72065349,
             0.69022405, 0.9841665 ],
            [0.30866584, 0.34258123, 0.37106183, ..., 0.87508457,
             0.54426929, 0.53391249],
            ...,
            [0.47750194, 0.09396009, 0.16947604, ..., 0.82964438,
             0.9052506 , 0.70531234],
            [0.28216774, 0.06112643, 0.01806789, ..., 0.38480884,
             0.37651963, 0.15904927],
            [0.54590487, 0.22034884, 0.44358047, ..., 0.56451792,
             0.15344121, 0.47713979]],
           [[0.46278164, 0.37850388, 0.52076647, ..., 0.20957928,
             0.14561883, 0.13893709],
            [0.17786602, 0.05443162, 0.08487059, ..., 0.57080271,
             0.03242944, 0.6644074 ],
            [0.29483855, 0.23221452, 0.77716782, ..., 0.50057916,
             0.49205944, 0.21075994],
    ...
            [0.73339498, 0.07334617, 0.36866892, ..., 0.23494169,
             0.72033225, 0.56247379],
            [0.885213  , 0.97839821, 0.53160162, ..., 0.91053768,
             0.57536331, 0.37315945],
            [0.58191917, 0.68534983, 0.17020612, ..., 0.87829471,
             0.73874034, 0.41930747]],
           [[0.4606276 , 0.0973907 , 0.70948389, ..., 0.56368729,
             0.74048363, 0.5122833 ],
            [0.45101316, 0.08566925, 0.70246856, ..., 0.47707637,
             0.6644182 , 0.53748297],
            [0.55387455, 0.17310703, 0.72945653, ..., 0.67118948,
             0.71301375, 0.87009826],
            ...,
            [0.32045915, 0.95910132, 0.01923958, ..., 0.52898408,
             0.78974531, 0.96774799],
            [0.86897641, 0.84767354, 0.18733604, ..., 0.14231991,
             0.99106918, 0.37830103],
            [0.40767868, 0.37804333, 0.43709734, ..., 0.4875185 ,
             0.01975035, 0.5726516 ]]], shape=(10, 20, 30))
    • time
      (time)
      float64
      0.0 3.448 6.897 ... 96.55 100.0
      units :
      s
      description :
      time samples for each image frame
      array([  0.      ,   3.448276,   6.896552,  10.344828,  13.793103,  17.241379,
              20.689655,  24.137931,  27.586207,  31.034483,  34.482759,  37.931034,
              41.37931 ,  44.827586,  48.275862,  51.724138,  55.172414,  58.62069 ,
              62.068966,  65.517241,  68.965517,  72.413793,  75.862069,  79.310345,
              82.758621,  86.206897,  89.655172,  93.103448,  96.551724, 100.      ])
  • units :
    brightness
    description :
    A generated random image stack
    long_name :
    calcium image pixel brightness
da.to_netcdf('example.nc', engine='netcdf4')

Exercise: Open the example.nc file in the HDF5 Viewer at https://myhdf5.hdfgroup.org/ to verify it is a valid HDF5 file, and use it to do the following tasks:

  1. View the time variable as a line plot.
  2. View the time values themselves in a matrix.
  3. View the Image data as a heatmap.
  4. Find the “description” attribute for the image variable (hint: check the inspect tab)

Exercise: Create a data directory next to this notebook.

Solution
Path('data').mkdir(exist_ok=True, parents=True)

Exercise: Run the cell below to create the eeg DataArray. Then, save the eeg DataArray to data/eeg.nc.

rng = np.random.default_rng(0)
time = np.linspace(0, 2, 1000)
channels = ['Fz', 'Cz', 'Pz', 'Oz']
values = rng.normal(size=(len(channels), len(time)))

eeg = xr.DataArray(
    data=values,
    dims=['channel', 'time'],
    coords={'channel': channels, 'time': time},
    name='eeg',
)
eeg
<xarray.DataArray 'eeg' (channel: 4, time: 1000)> Size: 32kB
array([[ 0.12573022, -0.13210486,  0.64042265, ...,  1.38997173,
        -0.66434209, -0.22997116],
       [ 1.18390191,  0.30382685,  0.19205435, ..., -2.01881162,
        -0.90942756,  0.36922933],
       [ 0.41925483, -0.50224455, -0.85769996, ..., -1.65759515,
        -1.99552682, -0.61334415],
       [-0.38847831, -0.27536744, -1.24011117, ...,  0.55029559,
         0.55104841, -0.8705243 ]], shape=(4, 1000))
Coordinates:
  * channel  (channel) <U2 32B 'Fz' 'Cz' 'Pz' 'Oz'
  * time     (time) float64 8kB 0.0 0.002002 0.004004 ... 1.996 1.998 2.0
xarray.DataArray
'eeg'
  • channel: 4
  • time: 1000
  • 0.1257 -0.1321 0.6404 0.1049 -0.5357 ... -0.4626 0.5503 0.551 -0.8705
    array([[ 0.12573022, -0.13210486,  0.64042265, ...,  1.38997173,
            -0.66434209, -0.22997116],
           [ 1.18390191,  0.30382685,  0.19205435, ..., -2.01881162,
            -0.90942756,  0.36922933],
           [ 0.41925483, -0.50224455, -0.85769996, ..., -1.65759515,
            -1.99552682, -0.61334415],
           [-0.38847831, -0.27536744, -1.24011117, ...,  0.55029559,
             0.55104841, -0.8705243 ]], shape=(4, 1000))
    • channel
      (channel)
      <U2
      'Fz' 'Cz' 'Pz' 'Oz'
      array(['Fz', 'Cz', 'Pz', 'Oz'], dtype='<U2')
    • time
      (time)
      float64
      0.0 0.002002 0.004004 ... 1.998 2.0
      array([0.      , 0.002002, 0.004004, ..., 1.995996, 1.997998, 2.      ],
            shape=(1000,))
Solution
eeg.to_netcdf('data/eeg.nc')

Exercise: Load data/eeg.nc into a new variable and display it to verify that its dimensions, coordinates, and attributes match the original.

Solution
eeg_loaded = xr.load_dataarray('data/eeg.nc')
eeg_loaded
<xarray.DataArray 'eeg' (channel: 4, time: 1000)> Size: 32kB
array([[ 0.12573022, -0.13210486,  0.64042265, ...,  1.38997173,
        -0.66434209, -0.22997116],
       [ 1.18390191,  0.30382685,  0.19205435, ..., -2.01881162,
        -0.90942756,  0.36922933],
       [ 0.41925483, -0.50224455, -0.85769996, ..., -1.65759515,
        -1.99552682, -0.61334415],
       [-0.38847831, -0.27536744, -1.24011117, ...,  0.55029559,
         0.55104841, -0.8705243 ]], shape=(4, 1000))
Coordinates:
  * channel  (channel) <U2 32B 'Fz' 'Cz' 'Pz' 'Oz'
  * time     (time) float64 8kB 0.0 0.002002 0.004004 ... 1.996 1.998 2.0
xarray.DataArray
'eeg'
  • channel: 4
  • time: 1000
  • 0.1257 -0.1321 0.6404 0.1049 -0.5357 ... -0.4626 0.5503 0.551 -0.8705
    array([[ 0.12573022, -0.13210486,  0.64042265, ...,  1.38997173,
            -0.66434209, -0.22997116],
           [ 1.18390191,  0.30382685,  0.19205435, ..., -2.01881162,
            -0.90942756,  0.36922933],
           [ 0.41925483, -0.50224455, -0.85769996, ..., -1.65759515,
            -1.99552682, -0.61334415],
           [-0.38847831, -0.27536744, -1.24011117, ...,  0.55029559,
             0.55104841, -0.8705243 ]], shape=(4, 1000))
    • channel
      (channel)
      <U2
      'Fz' 'Cz' 'Pz' 'Oz'
      array(['Fz', 'Cz', 'Pz', 'Oz'], dtype='<U2')
    • time
      (time)
      float64
      0.0 0.002002 0.004004 ... 1.998 2.0
      array([0.      , 0.002002, 0.004004, ..., 1.995996, 1.997998, 2.      ],
            shape=(1000,))