Skip to content

Commit

Permalink
Stop using deprecated aliases of builtin types (#3997)
Browse files Browse the repository at this point in the history
* Stopped using deprecated aliases of builtin types.
This is required to avoid warnings starting with NumPy 1.20.0.

* Update lib/iris/tests/test_cell.py

Co-authored-by: Bill Little <[email protected]>

* Update lib/iris/tests/test_cell.py

Co-authored-by: Bill Little <[email protected]>

* Updated whatsnew.

Co-authored-by: Bill Little <[email protected]>
  • Loading branch information
akuhnregnier and bjlittle authored Feb 16, 2021
1 parent ce5975a commit 343d329
Show file tree
Hide file tree
Showing 27 changed files with 105 additions and 94 deletions.
5 changes: 5 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ This document explains the changes made to Iris for this release
#. `@bjlittle`_ enabled `cirrus-ci`_ compute credits for non-draft pull-requests
from collaborators targeting the Iris ``master`` branch. (:pull:`4007`)

#. `@akuhnregnier`_ replaced `deprecated numpy 1.20 aliases for builtin types`_.
(:pull:`3997`)


.. comment
Whatsnew author names (@github name) in alphabetical order. Note that,
core dev names are automatically included by the common_links.inc:
.. _@akuhnregnier: https://github.com/akuhnregnier
.. _@gcaria: https://github.com/gcaria
.. _@MHBalsmeier: https://github.com/MHBalsmeier

Expand All @@ -125,6 +129,7 @@ This document explains the changes made to Iris for this release
Whatsnew resources in alphabetical order:
.. _abstract base class: https://docs.python.org/3/library/abc.html
.. _deprecated numpy 1.20 aliases for builtin types: https://numpy.org/doc/1.20/release/1.20.0-notes.html#using-the-aliases-of-builtin-types-like-np-int-is-deprecated
.. _GitHub: https://github.com/SciTools/iris/issues/new/choose
.. _Met Office: https://www.metoffice.gov.uk/
.. _numpy: https://numpy.org/doc/stable/release/1.20.0-notes.html
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def call_func(c):
except TypeError:
try_quick = False
if try_quick:
r = np.zeros(coord.shape, dtype=np.bool)
r = np.zeros(coord.shape, dtype=np.bool_)
if coord.cell(i) == self._coord_thing:
r[i] = True
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ def interp_order(length):
slices[-1] = endslice
slices = tuple(slices) # Numpy>=1.16 : index with tuple, *not* list.

if isinstance(array.dtype, np.float):
if isinstance(array.dtype, np.float64):
data = array[slices]
else:
# Cast non-float data type.
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/analysis/_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def _regrid(

if ma.isMaskedArray(src_data):
data = ma.empty(shape, dtype=dtype)
data.mask = np.zeros(data.shape, dtype=np.bool)
data.mask = np.zeros(data.shape, dtype=np.bool_)
else:
data = np.empty(shape, dtype=dtype)

Expand Down
4 changes: 2 additions & 2 deletions lib/iris/experimental/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim, weights_info, mdtol=0):
# Flag to indicate whether the original data was a masked array.
src_masked = src_data.mask.any() if ma.isMaskedArray(src_data) else False
if src_masked:
src_area_masks = np.full(src_areas_shape, True, dtype=np.bool)
src_area_masks = np.full(src_areas_shape, True, dtype=np.bool_)
else:
new_data_mask = np.full(new_shape, False, dtype=np.bool)
new_data_mask = np.full(new_shape, False, dtype=np.bool_)

# Axes of data over which the weighted mean is calculated.
axis = (y_dim, x_dim)
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/fileformats/_ff.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ def __iter__(self):
return pp._interpret_fields(self._extract_field())


def _parse_binary_stream(file_like, dtype=np.float, count=-1):
def _parse_binary_stream(file_like, dtype=np.float64, count=-1):
"""
Replacement :func:`numpy.fromfile` due to python3 performance issues.
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/fileformats/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ def _data_bytes_to_shaped_array(
# However, we still mask any MDI values in the array (below).
pass
else:
land_mask = mask.data.astype(np.bool)
land_mask = mask.data.astype(np.bool_)
sea_mask = ~land_mask
new_data = np.ma.masked_all(land_mask.shape)
new_data.fill_value = mdi
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/fileformats/pp_load_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def _epoch_date_hours(epoch_hours_unit, datetime):
# numpy.float64. The behaviour of round is to recast this to an
# int, which is not the desired behaviour for PP files.
# So, cast the answer to numpy.float_ to be safe.
epoch_hours = np.float_(epoch_hours_unit.date2num(datetime))
epoch_hours = np.float64(epoch_hours_unit.date2num(datetime))

if days_offset is not None:
# Correct for any modifications to achieve a valid date.
Expand Down
12 changes: 6 additions & 6 deletions lib/iris/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,26 +483,26 @@ def assertDataAlmostEqual(self, data, reference_filename, **kwargs):
stats.get("max", 0.0),
stats.get("min", 0.0),
),
dtype=np.float_,
dtype=np.float64,
)
if math.isnan(stats.get("mean", 0.0)):
self.assertTrue(math.isnan(data.mean()))
else:
data_stats = np.array(
(data.mean(), data.std(), data.max(), data.min()),
dtype=np.float_,
dtype=np.float64,
)
self.assertArrayAllClose(nstats, data_stats, **kwargs)
else:
self._ensure_folder(reference_path)
stats = collections.OrderedDict(
[
("std", np.float_(data.std())),
("min", np.float_(data.min())),
("max", np.float_(data.max())),
("std", np.float64(data.std())),
("min", np.float64(data.min())),
("max", np.float64(data.max())),
("shape", data.shape),
("masked", ma.is_masked(data)),
("mean", np.float_(data.mean())),
("mean", np.float64(data.mean())),
]
)
with open(reference_path, "w") as reference_file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def test_rotated(self):
[100, 100, 100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100, 100],
],
dtype=np.float,
dtype=np.float64,
)

c1_areasum = _cube_area_sum(c1)
Expand Down Expand Up @@ -715,7 +715,7 @@ def test_rotated(self):
[100, 100, 199, 199, 100],
[100, 100, 199, 199, 199],
],
dtype=np.float,
dtype=np.float64,
)
c2_areasum = _cube_area_sum(c2)

Expand Down Expand Up @@ -770,7 +770,7 @@ def test_missing_data_rotated(self):
[100, 100, 100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100, 100],
],
dtype=np.float,
dtype=np.float64,
)

if do_add_missing:
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/tests/test_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ class Terry:
self.assertEqual(self.d.__ne__(Terry()), NotImplemented)

def test_numpy_int_equality(self):
dtypes = (np.int, np.int16, np.int32, np.int64)
dtypes = (np.int_, np.int16, np.int32, np.int64)
for dtype in dtypes:
val = dtype(3)
cell = iris.coords.Cell(val, None)
self.assertEqual(cell, val)

def test_numpy_float_equality(self):
dtypes = (
np.float,
np.float_,
np.float16,
np.float32,
np.float64,
Expand Down
8 changes: 4 additions & 4 deletions lib/iris/tests/test_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def test_concat_masked_2x2d(self):
self.assertEqual(result[0].shape, (2, 4))
mask = np.array(
[[True, False, False, True], [False, True, True, False]],
dtype=np.bool,
dtype=np.bool_,
)
self.assertArrayEqual(result[0].data.mask, mask)

Expand All @@ -436,7 +436,7 @@ def test_concat_masked_2y2d(self):
self.assertEqual(result[0].shape, (4, 2))
mask = np.array(
[[True, False], [False, True], [False, True], [True, False]],
dtype=np.bool,
dtype=np.bool_,
)
self.assertArrayEqual(result[0].data.mask, mask)

Expand All @@ -458,7 +458,7 @@ def test_concat_masked_2y2d_with_concrete_and_lazy(self):
self.assertEqual(result[0].shape, (4, 2))
mask = np.array(
[[True, False], [False, True], [False, True], [True, False]],
dtype=np.bool,
dtype=np.bool_,
)
self.assertArrayEqual(result[0].data.mask, mask)

Expand All @@ -480,7 +480,7 @@ def test_concat_masked_2y2d_with_lazy_and_concrete(self):
self.assertEqual(result[0].shape, (4, 2))
mask = np.array(
[[True, False], [False, True], [False, True], [True, False]],
dtype=np.bool,
dtype=np.bool_,
)
self.assertArrayEqual(result[0].data.mask, mask)

Expand Down
4 changes: 2 additions & 2 deletions lib/iris/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def _make_cube(self, a, b, c, d, data=0):
)

for name, value in zip(["a", "b", "c", "d"], [a, b, c, d]):
dtype = np.str if isinstance(value, str) else np.float32
dtype = np.str_ if isinstance(value, str) else np.float32
cube.add_aux_coord(
AuxCoord(
np.array([value], dtype=dtype), long_name=name, units="1"
Expand Down Expand Up @@ -613,7 +613,7 @@ def _make_cube(self, a, b, data=0, a_dim=False, b_dim=False):
)

for name, value, dim in zip(["a", "b"], [a, b], [a_dim, b_dim]):
dtype = np.str if isinstance(value, str) else np.float32
dtype = np.str_ if isinstance(value, str) else np.float32
ctype = DimCoord if dim else AuxCoord
coord = ctype(
np.array([value], dtype=dtype), long_name=name, units="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def test_rotated_to_osgb(self):
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1],
],
np.bool,
np.bool_,
)
self.assertArrayEqual(expected_mask, ut.data.mask)
self.assertArrayEqual(expected_mask, vt.data.mask)
Expand Down
30 changes: 15 additions & 15 deletions lib/iris/tests/unit/analysis/regrid/test_RectilinearRegridder.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _regrid(self, data, method, extrapolation_mode=None):
def test_default_ndarray(self):
# NaN -> NaN
# Extrapolated -> NaN
data = np.arange(12, dtype=np.float).reshape(3, 4)
data = np.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
for method in self.methods:
result = self._regrid(data, method)
Expand All @@ -278,7 +278,7 @@ def test_default_maskedarray(self):
# NaN -> NaN
# Extrapolated -> Masked
# Masked -> Masked
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
data[2, 3] = ma.masked
for method in self.methods:
Expand All @@ -293,7 +293,7 @@ def test_default_maskedarray_none_masked(self):
# NaN -> NaN
# Extrapolated -> Masked
# Masked -> N/A
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
for method in self.methods:
result = self._regrid(data, method)
Expand All @@ -307,7 +307,7 @@ def test_default_maskedarray_none_masked_expanded(self):
# NaN -> NaN
# Extrapolated -> Masked
# Masked -> N/A
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
# Make sure the mask has been expanded
data.mask = False
data[0, 0] = np.nan
Expand All @@ -322,7 +322,7 @@ def test_default_maskedarray_none_masked_expanded(self):
def test_method_ndarray(self):
# NaN -> NaN
# Extrapolated -> linear
data = np.arange(12, dtype=np.float).reshape(3, 4)
data = np.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
for method in self.methods:
result = self._regrid(data, method, "extrapolate")
Expand All @@ -334,7 +334,7 @@ def test_method_maskedarray(self):
# NaN -> NaN
# Extrapolated -> linear
# Masked -> Masked
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
data[2, 3] = ma.masked
for method in self.methods:
Expand All @@ -348,7 +348,7 @@ def test_method_maskedarray(self):
def test_nan_ndarray(self):
# NaN -> NaN
# Extrapolated -> NaN
data = np.arange(12, dtype=np.float).reshape(3, 4)
data = np.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
for method in self.methods:
result = self._regrid(data, method, "nan")
Expand All @@ -360,7 +360,7 @@ def test_nan_maskedarray(self):
# NaN -> NaN
# Extrapolated -> NaN
# Masked -> Masked
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
data[2, 3] = ma.masked
for method in self.methods:
Expand All @@ -373,15 +373,15 @@ def test_nan_maskedarray(self):

def test_error_ndarray(self):
# Values irrelevant - the function raises an error.
data = np.arange(12, dtype=np.float).reshape(3, 4)
data = np.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
for method in self.methods:
with self.assertRaisesRegex(ValueError, "out of bounds"):
self._regrid(data, method, "error")

def test_error_maskedarray(self):
# Values irrelevant - the function raises an error.
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
data[2, 3] = ma.masked
for method in self.methods:
Expand All @@ -392,7 +392,7 @@ def test_mask_ndarray(self):
# NaN -> NaN
# Extrapolated -> Masked (this is different from all the other
# modes)
data = np.arange(12, dtype=np.float).reshape(3, 4)
data = np.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
for method in self.methods:
result = self._regrid(data, method, "mask")
Expand All @@ -406,7 +406,7 @@ def test_mask_maskedarray(self):
# NaN -> NaN
# Extrapolated -> Masked
# Masked -> Masked
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
data[2, 3] = ma.masked
for method in self.methods:
Expand All @@ -420,7 +420,7 @@ def test_mask_maskedarray(self):
def test_nanmask_ndarray(self):
# NaN -> NaN
# Extrapolated -> NaN
data = np.arange(12, dtype=np.float).reshape(3, 4)
data = np.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
for method in self.methods:
result = self._regrid(data, method, "nanmask")
Expand All @@ -432,7 +432,7 @@ def test_nanmask_maskedarray(self):
# NaN -> NaN
# Extrapolated -> Masked
# Masked -> Masked
data = ma.arange(12, dtype=np.float).reshape(3, 4)
data = ma.arange(12, dtype=np.float64).reshape(3, 4)
data[0, 0] = np.nan
data[2, 3] = ma.masked
for method in self.methods:
Expand All @@ -444,7 +444,7 @@ def test_nanmask_maskedarray(self):
self.assertMaskedArrayEqual(result, expected)

def test_invalid(self):
data = np.arange(12, dtype=np.float).reshape(3, 4)
data = np.arange(12, dtype=np.float64).reshape(3, 4)
emsg = "Invalid extrapolation mode"
for method in self.methods:
with self.assertRaisesRegex(ValueError, emsg):
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/tests/unit/aux_factory/test_OceanSFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ def setUp(self):
np.arange(-0.975, 0, 0.05, dtype=float), units="1", long_name="s"
)
self.eta = AuxCoord(
np.arange(-1, 3, dtype=np.float).reshape(2, 2),
np.arange(-1, 3, dtype=np.float64).reshape(2, 2),
long_name="eta",
units="m",
)
self.depth = AuxCoord(
np.arange(4, dtype=np.float).reshape(2, 2) * 1e3,
np.arange(4, dtype=np.float64).reshape(2, 2) * 1e3,
long_name="depth",
units="m",
)
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/tests/unit/aux_factory/test_OceanSg1Factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ def setUp(self):
np.linspace(-0.959, -0.001, 36), units="1", long_name="c"
)
self.eta = AuxCoord(
np.arange(-1, 3, dtype=np.float).reshape(2, 2),
np.arange(-1, 3, dtype=np.float64).reshape(2, 2),
long_name="eta",
units="m",
)
self.depth = AuxCoord(
np.array([[5, 200], [1000, 4000]], dtype=np.float),
np.array([[5, 200], [1000, 4000]], dtype=np.float64),
long_name="depth",
units="m",
)
Expand Down
Loading

0 comments on commit 343d329

Please sign in to comment.