eofs.xarray raising TypeError (Using a DataArray to construct a variable is ambiguous)

Question:

I’m working on a multidimensional dataset using xarray and had some issues with eofs, the EOF analysis package, and particularly, with its xarray interface.

My xarray DataArray looks like this:

<xarray.DataArray 'timeMonthly_avg_flux' (time: 1800, y: 601, x: 601)>
array([[[0., 0., ..., 0., 0.],
    [0., 0., ..., 0., 0.],
    ...,
    [0., 0., ..., 0., 0.],
    [0., 0., ..., 0., 0.]],
   [[0., 0., ..., 0., 0.],
    [0., 0., ..., 0., 0.],
    ...,
    [0., 0., ..., 0., 0.],
    [0., 0., ..., 0., 0.]]])
Coordinates:
lat      (y, x) float64 ...
lon      (y, x) float64 ...
time     (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2150-12-31
x        (x) float64 -3e+06 -2.99e+06 -2.98e+06 ... 2.98e+06 2.99e+06 3e+06
y        (y) float64 -3e+06 -2.99e+06 -2.98e+06 ... 2.98e+06 2.99e+06 3e+06

The problem arises when I run the following:

from eofs.xarray import Eof
solver = Eof(flux) # flux is the above DataArray
flux_eofs = solver.eofs()

for which I get the following TypeError:

TypeError: Using a DataArray object to construct a variable is ambiguous, please extract the data using the .data property.

Also noting that other methods in this function work as intended: I am able to call the principal components as below:

flux_pcs = solver.pcs()

The dataset does have NaN values, but as far as I can tell, the eofs.xarray module has been designed to handle NaNs. For now, my workaround has been to convert the dataset into a Numpy array and use the eofs.standard interface instead, and convert the outputs back into xarray Datasets/DataArrays as required. All methods work as intended when I do this:

from eofs.standard import Eof
flux_np = flux.to_numpy()
solver = Eof(flux_np)
flux_eofs = solver.eofs()

I could find two other instances of this error being raised: as part of the w2w package, where it seems to have been something to do with the python environment, and here, as part of the PyWake project, but it’s not clear to me what the problem was.

Asked By: Shiva

||

Answers:

I recently ran into the same error message in my project (it is different in nature to yours). I pip uninstalled the latest version of xarray on my PC (0.20.2) and installed an older version (0.16.0), and (at least) that error went away.

Answered By: Duelek

For everyone encountering this issue, this is a bug which has been also raised and discussed on Github (also by the author of this question :)). The xarray compability is a bit broken currently.

For the time being this can be fixed manually by changing the

eofs/lib/eofs/xarray.py – file

#Lines 638 to 640
# Add non-dimension coordinates. 
pcs.coords.update({coord.name: (coord.dims, coord)
                       for coord in time_ndcoords})

to

# Add non-dimension coordinates.
pcs.coords.update({coord.name: (coord.dims, coord.data)
                       for coord in time_ndcoords})

There is a pull request fixing this that has unfortunately not been merged yet.

Answered By: Mathi

Sorry for the shameless self-promotion 😉 you may want to give xeofs a try. It provides EOF analysis (and more) in xarray.

Answered By: nicrie
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.