Skip to content

Commit

Permalink
Merge pull request #283 from davidhassell/numpy-to-np
Browse files Browse the repository at this point in the history
Change `numpy` to `np`
  • Loading branch information
davidhassell authored Jan 17, 2024
2 parents 1ca764e + 82634e3 commit 92f8fdd
Show file tree
Hide file tree
Showing 25 changed files with 205 additions and 216 deletions.
4 changes: 2 additions & 2 deletions cfdm/cellmethod.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from copy import deepcopy

import numpy
import numpy as np

from . import core, mixin
from .data import Data
Expand Down Expand Up @@ -529,7 +529,7 @@ def sorted(self, indices=None):
return new

if indices is None:
indices = numpy.argsort(axes)
indices = np.argsort(axes)
elif len(indices) != len(axes):
raise ValueError(
f"Can't sort cell method axes. The given indices ({indices}) "
Expand Down
4 changes: 2 additions & 2 deletions cfdm/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from enum import Enum

import numpy
import numpy as np

"""A dictionary of useful constants.
Expand Down Expand Up @@ -57,4 +57,4 @@ class ValidLogLevels(Enum):
>>> f[...] = cfdm.masked
"""
masked = numpy.ma.masked
masked = np.ma.masked
6 changes: 3 additions & 3 deletions cfdm/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
raise ImportError(_error0 + str(error1))

try:
import numpy
import numpy as np
except ImportError as error1:
raise ImportError(_error0 + str(error1))

Expand Down Expand Up @@ -61,10 +61,10 @@

# Check the version of numpy
_minimum_vn = "1.15"
if Version(numpy.__version__) < Version(_minimum_vn):
if Version(np.__version__) < Version(_minimum_vn):
raise ValueError(
f"Bad numpy version: cfdm.core requires numpy>={_minimum_vn}. "
f"Got {numpy.__version__} at {numpy.__file__}"
f"Got {np.__version__} at {np.__file__}"
)

from .constructs import Constructs
Expand Down
8 changes: 4 additions & 4 deletions cfdm/core/data/data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import numpy
import numpy as np

from .. import abstract
from .abstract import Array
Expand Down Expand Up @@ -167,7 +167,7 @@ def array(self):
array = self._get_Array().array

# Set the numpy array fill value
if numpy.ma.isMA(array):
if np.ma.isMA(array):
array.set_fill_value(self.get_fill_value(None))

return array
Expand Down Expand Up @@ -788,8 +788,8 @@ def _set_Array(self, array, copy=True):
"""
if not isinstance(array, Array):
if not isinstance(array, numpy.ndarray):
array = numpy.asanyarray(array)
if not isinstance(array, np.ndarray):
array = np.asanyarray(array)

array = NumpyArray(array)

Expand Down
8 changes: 4 additions & 4 deletions cfdm/core/data/numpyarray.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import numpy
import numpy as np

from . import abstract

Expand Down Expand Up @@ -101,10 +101,10 @@ def array(self):
"""
array = self._get_component("array")

if not array.ndim and numpy.ma.isMA(array):
# This is because numpy.ma.copy doesn't work for
if not array.ndim and np.ma.isMA(array):
# This is because np.ma.copy doesn't work for
# scalar arrays (at the moment, at least)
ma_array = numpy.ma.empty((), dtype=array.dtype)
ma_array = np.ma.empty((), dtype=array.dtype)
ma_array[...] = array
array = ma_array
else:
Expand Down
4 changes: 2 additions & 2 deletions cfdm/core/dimensioncoordinate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import numpy
import numpy as np

from . import abstract

Expand Down Expand Up @@ -104,7 +104,7 @@ def set_data(self, data, copy=True, inplace=True):
None
"""
if numpy.ndim(data) != 1:
if np.ndim(data) != 1:
raise ValueError(
"Dimension coordinate construct must have 1-dimensional data. "
f"Got {data!r}"
Expand Down
6 changes: 3 additions & 3 deletions cfdm/core/field.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import numpy
import numpy as np

from . import Constructs, Domain, abstract, mixin

Expand Down Expand Up @@ -531,9 +531,9 @@ def set_data(self, data, axes=None, copy=True, inplace=True):
if axes is None:
existing_axes = f.get_data_axes(default=None)
if existing_axes is not None:
f.set_data_axes(axes=existing_axes, _shape=numpy.shape(data))
f.set_data_axes(axes=existing_axes, _shape=np.shape(data))
else:
f.set_data_axes(axes=axes, _shape=numpy.shape(data))
f.set_data_axes(axes=axes, _shape=np.shape(data))

super(Field, f).set_data(data, copy=copy, inplace=True)

Expand Down
4 changes: 2 additions & 2 deletions cfdm/core/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pickle import dumps, loads

import netCDF4
import numpy
import numpy as np

from . import __cf_version__, __file__, __version__

Expand Down Expand Up @@ -60,7 +60,7 @@ def environment(display=True, paths=True):
"netcdf library": (netCDF4.__netcdf4libversion__, ""),
"Python": (platform.python_version(), sys.executable),
"netCDF4": (netCDF4.__version__, os.path.abspath(netCDF4.__file__)),
"numpy": (numpy.__version__, os.path.abspath(numpy.__file__)),
"numpy": (np.__version__, os.path.abspath(np.__file__)),
"cfdm.core": (__version__, os.path.abspath(__file__)),
}
string = "{0}: {1!s}"
Expand Down
12 changes: 6 additions & 6 deletions cfdm/data/netcdfarray.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import netCDF4
import numpy
import numpy as np

from . import abstract
from .mixin import FileArrayMixin
Expand Down Expand Up @@ -239,7 +239,7 @@ def __getitem__(self, indices):
# A netCDF string type scalar variable comes out as Python
# str object, so convert it to a numpy array.
# --------------------------------------------------------
array = numpy.array(array, dtype=f"U{len(array)}")
array = np.array(array, dtype=f"U{len(array)}")

if not self.ndim:
# Hmm netCDF4 has a thing for making scalar size 1, 1d
Expand All @@ -260,9 +260,9 @@ def __getitem__(self, indices):

array = netCDF4.chartostring(array)
shape = array.shape
array = numpy.array([x.rstrip() for x in array.flat], dtype="U")
array = numpy.reshape(array, shape)
array = numpy.ma.masked_where(array == "", array)
array = np.array([x.rstrip() for x in array.flat], dtype="U")
array = np.reshape(array, shape)
array = np.ma.masked_where(array == "", array)
elif not string_type and kind == "O":
# --------------------------------------------------------
# A netCDF string type N-d (N>=1) variable comes out as a
Expand All @@ -273,7 +273,7 @@ def __getitem__(self, indices):
# --------------------------------------------------------
# netCDF4 does not auto-mask VLEN variable, so do it here.
# --------------------------------------------------------
array = numpy.ma.where(array == "", numpy.ma.masked, array)
array = np.ma.where(array == "", np.ma.masked, array)

return array

Expand Down
28 changes: 14 additions & 14 deletions cfdm/mixin/container.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

import numpy
import numpy as np

from ..decorators import _manage_log_level_via_verbosity
from ..docstring import _docstring_substitution_definitions
Expand Down Expand Up @@ -143,19 +143,19 @@ def _equals(
# --------------------------------------------------------
return eq(x, **kwargs)

x = numpy.asanyarray(x)
y = numpy.asanyarray(y)
x = np.asanyarray(x)
y = np.asanyarray(y)
if x.shape != y.shape:
return False

# # ------------------------------------------------------------
# # Cast x and y as numpy arrays
# # ------------------------------------------------------------
# if not isinstance(x, numpy.ndarray):
# x = numpy.asanyarray(x)
# if not isinstance(x, np.ndarray):
# x = np.asanyarray(x)
#
# if not isinstance(y, numpy.ndarray):
# y = numpy.asanyarray(y)
# if not isinstance(y, np.ndarray):
# y = np.asanyarray(y)

# THIS IS WHERE SOME NUMPY FUTURE WARNINGS ARE COMING FROM

Expand All @@ -166,14 +166,14 @@ def _equals(
):
return False

x_is_masked = numpy.ma.isMA(x)
y_is_masked = numpy.ma.isMA(y)
x_is_masked = np.ma.isMA(x)
y_is_masked = np.ma.isMA(y)

if not (x_is_masked or y_is_masked):
try:
return bool(numpy.allclose(x, y, rtol=rtol, atol=atol))
return bool(np.allclose(x, y, rtol=rtol, atol=atol))
except (IndexError, NotImplementedError, TypeError):
return bool(numpy.all(x == y))
return bool(np.all(x == y))
else:
if x_is_masked and y_is_masked:
if (x.mask != y.mask).any():
Expand All @@ -184,10 +184,10 @@ def _equals(
return False

try:
return bool(numpy.ma.allclose(x, y, rtol=rtol, atol=atol))
return bool(np.ma.allclose(x, y, rtol=rtol, atol=atol))
except (IndexError, NotImplementedError, TypeError):
out = numpy.ma.all(x == y)
if out is numpy.ma.masked:
out = np.ma.all(x == y)
if out is np.ma.masked:
return True
else:
return bool(out)
Expand Down
1 change: 0 additions & 1 deletion cfdm/read_write/netcdf/netcdfread.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import netCDF4
import netcdf_flattener
import numpy
import numpy as np
from packaging.version import Version

Expand Down
Loading

0 comments on commit 92f8fdd

Please sign in to comment.