Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xarray/Iris incompatibilities around integer arrays #418

Closed
freemansw1 opened this issue Mar 13, 2024 · 4 comments
Closed

Xarray/Iris incompatibilities around integer arrays #418

freemansw1 opened this issue Mar 13, 2024 · 4 comments
Labels
bug Code that is failing or producing the wrong result

Comments

@freemansw1
Copy link
Member

When converting between xarray.DataArray and iris.cube.Cube for integer arrays, a bug in xarray (according to @w-k-jones prior to 2023.06) causes an exception to be raised. @w-k-jones has provided a pretty detailed description of the issue here: #378 (comment) .

I'm moving this to a new issue so that we can figure out what to do, which we will for some examples that @w-k-jones has added with #378 and with the xarray transition for segmentation that I have worked on with #417.

As far as I see it, we have the following options:

  1. Pin our xarray version requirements to the non-offending versions (it would be good to go back and see if we can allow any older versions)
  2. Find a way to work around the issue for conversions
  3. Skip ahead to v2.0 and ditch iris entirely without a deprecation period (okay, okay, I can dream)
@freemansw1 freemansw1 added the bug Code that is failing or producing the wrong result label Mar 13, 2024
@w-k-jones
Copy link
Member

w-k-jones commented Mar 13, 2024

I think I've handled this in the past by replacing the masked integer array with a filled array in the iris cube before converting to xarray, but I can't find the notebook at the moment. I will try to recreate it as a proof of concept

@w-k-jones
Copy link
Member

w-k-jones commented Mar 13, 2024

def convert_cube_to_dataarray(cube):
    """
    Convert an iris cube to an xarray dataarray, averting error for integer dtypes in xarray<v2023.06

    Parameters
    ----------
    cube : iris.cube.Cube
        Iris data cube

    Returns
    -------
    dataarray : xr.DataArray
        dataarray converted from cube. If the cube's core data is a masked array and has integer dtype,
        the returned datarray will have a numpy array with masked values filled with the minimum value for
        that integer dtype. Otherwise the data will be identical to that produced using xr.DataArray.from_iris
    """
    if isinstance(cube.core_data(), ma.core.MaskedArray) and np.issubdtype(cube.core_data().dtype, np.integer):
        return xr.DataArray.from_iris(cube.copy(cube.core_data().filled(np.iinfo(cube.core_data().dtype).min)))
    return xr.DataArray.from_iris(cube)

This should produce results identical to xarray>=v2023.06 for older versions, and without the warning message

@w-k-jones
Copy link
Member

I have now added a fix to this as part of #378

@w-k-jones
Copy link
Member

Fixed with #378

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Code that is failing or producing the wrong result
Projects
None yet
Development

No branches or pull requests

2 participants