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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divisions by zero in cube arithmetic give inconsistent results #5523

Closed
schlunma opened this issue Sep 28, 2023 · 4 comments
Closed

Divisions by zero in cube arithmetic give inconsistent results #5523

schlunma opened this issue Sep 28, 2023 · 4 comments

Comments

@schlunma
Copy link
Contributor

schlunma commented Sep 28, 2023

馃悰 Bug Report

The results of zero divisions when performing cube arithmetic depend on cube shapes and whether if they have proper coordinates or not. Sometimes a proper zero-division warning is raised (with an output of nan), sometimes no warning is raised (with an output of -- = masked value). See following code example for more details.

I guess this is somehow related iris' advanced broadcasting using coordinates.

How To Reproduce

Steps to reproduce the behaviour:

import iris.analysis
from iris.cube import Cube
from iris.coords import DimCoord


# Cubes with proper coordinates
x_coord = DimCoord([0], var_name='x')
y_coord = DimCoord([1, 2], var_name='y')
cube = Cube([[0.0, 0.0]], dim_coords_and_dims=[(x_coord, 0), (y_coord, 1)])

# The following raises a zero-division warning
res = cube / cube
print(res.data)  # [[nan, nan]]

# The following raises a zero-division warning
res = cube / cube.collapsed(['x', 'y'], iris.analysis.MEAN)
print(res.data)  # [[nan, nan]]

# The following does NOT raise a zero-division warning
res = cube / cube.collapsed('x', iris.analysis.MEAN)
print(res.data)  # [[--, --]]

# The following does NOT raise a zero-division warning
res = cube / cube.collapsed('y', iris.analysis.MEAN)
print(res.data)  # [[--, --]]


# Cubes without proper coordinates
# These cases ALWAYS raise a zero-division warning

res = Cube([[0.0, 0.0]]) / Cube([[0.0, 0.0]])
print(res.data)  # [[nan, nan]]

res = Cube([[0.0, 0.0]]) / Cube([0.0, 0.0])
print(res.data)  # [[nan, nan]]

res = Cube([[0.0, 0.0]]) / Cube([0.0])
print(res.data)  # [[nan, nan]]

Expected behaviour

All of these cases above should return the same output. I personally have a slight preference towards returning nan values, as this allows us to properly distinguish between a prior mask and the zero divisions.

Environment

  • OS & Version: Red Hat Enterprise Linux 8.6 (Ootpa)
  • Iris Version: 3.7.0
@valeriupredoi
Copy link

valeriupredoi commented Sep 28, 2023

this could be sitting a lot deeper in the stack, in netCDF4python layer, there are all manners of surprising side effects when _FillValue is not defined eg Unidata/netcdf4-python#748

@rcomer
Copy link
Member

rcomer commented Sep 28, 2023

Is this because collapsed returns a cube with a masked array? #5314

@schlunma
Copy link
Contributor Author

Is this because collapsed returns a cube with a masked array? #5314

Yes, that's the reason! Changing the data to a regular array after collapsed fixes this.

Thanks, I think this can be closed then 馃憤

@valeriupredoi
Copy link

cheers guys! Happy it's not in deeper stacks 馃榿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants