Skip to content

Commit c3b2bff

Browse files
committed
Remove shorthand import numpy as np
1 parent d53903b commit c3b2bff

31 files changed

+600
-600
lines changed

docs/developing/grass.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from functools import cached_property
44
from typing import Optional, Tuple, Dict
55

6-
import numpy as np
6+
import numpy
77
import xarray
88
from shapely.geometry import Polygon
99
from shapely.geometry.base import BaseGeometry
@@ -95,12 +95,12 @@ def selector_for_index(self, index: GrassIndex) -> Dict[str, int]:
9595
return {'warp': warp, 'weft': weft}
9696

9797
@cached_property
98-
def polygons(self) -> np.ndarray:
98+
def polygons(self) -> numpy.ndarray:
9999
def make_polygon_for_cell(warp: int, weft: int) -> Polygon:
100100
# Implementation left as an exercise for the reader
101101
return Polygon(...)
102102

103-
return np.array([
103+
return numpy.array([
104104
make_polygon_for_cell(warp, weft)
105105
for warp in range(self.dataset.dimensions['warp'])
106106
for weft in range(self.dataset.dimensions['weft'])

src/emsarray/compat/shapely.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import warnings
22
from typing import Generic, Iterable, Tuple, TypeVar, Union, cast
33

4-
import numpy as np
4+
import numpy
55
import shapely
66
from packaging.version import parse
77
from shapely.errors import ShapelyDeprecationWarning
@@ -23,16 +23,16 @@ class SpatialIndex(Generic[T]):
2323
This also handles the version differences in STRtree between
2424
shapely ~= 1.8.x and shapely >= 2.0.0
2525
"""
26-
items: np.ndarray
26+
items: numpy.ndarray
2727
index: STRtree
2828

29-
dtype: np.dtype = np.dtype([('geom', np.object_), ('data', np.object_)])
29+
dtype: numpy.dtype = numpy.dtype([('geom', numpy.object_), ('data', numpy.object_)])
3030

3131
def __init__(
3232
self,
33-
items: Union[np.ndarray, Iterable[Tuple[BaseGeometry, T]]],
33+
items: Union[numpy.ndarray, Iterable[Tuple[BaseGeometry, T]]],
3434
):
35-
self.items = np.array(items, dtype=self.dtype)
35+
self.items = numpy.array(items, dtype=self.dtype)
3636

3737
if shapely_version >= v2:
3838
self.index = STRtree(self.items['geom'])
@@ -46,19 +46,19 @@ def __init__(
4646
def query(
4747
self,
4848
geom: BaseGeometry,
49-
) -> np.ndarray:
49+
) -> numpy.ndarray:
5050
if shapely_version >= v2:
5151
indices = self.index.query(geom)
5252
else:
5353
indices = self.index._query(geom)
54-
return cast(np.ndarray, self.items.take(indices))
54+
return cast(numpy.ndarray, self.items.take(indices))
5555

5656
def nearest(
5757
self,
5858
geom: BaseGeometry,
59-
) -> np.ndarray:
59+
) -> numpy.ndarray:
6060
if shapely_version >= v2:
6161
indices = self.index.nearest(geom)
6262
else:
6363
indices = self.index._nearest(geom)
64-
return cast(np.ndarray, self.items.take(indices))
64+
return cast(numpy.ndarray, self.items.take(indices))

src/emsarray/conventions/_base.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
List, Optional, Tuple, TypeVar, Union, cast
1212
)
1313

14-
import numpy as np
14+
import numpy
1515
import xarray
1616
from shapely import unary_union
1717
from shapely.geometry import MultiPolygon, Point, Polygon
@@ -355,7 +355,7 @@ def get_time_name(self) -> Hashable:
355355
# A time variable must have units of the form '<units> since <epoc>'
356356
if 'since' in units:
357357
# The variable must now be a numpy datetime
358-
if variable.dtype.type == np.datetime64:
358+
if variable.dtype.type == numpy.datetime64:
359359
return name
360360
raise NoSuchCoordinateError("Could not find time coordinate in dataset")
361361

@@ -441,7 +441,7 @@ def get_all_depth_names(self) -> List[Hashable]:
441441
),
442442
DeprecationWarning,
443443
)
444-
def get_depths(self) -> np.ndarray:
444+
def get_depths(self) -> numpy.ndarray:
445445
"""Get the depth of each vertical layer in this dataset.
446446
447447
.. deprecated:: 0.5.0
@@ -453,7 +453,7 @@ def get_depths(self) -> np.ndarray:
453453
:class:`numpy.ndarray`
454454
An array of depths, one per vertical layer in the dataset.
455455
"""
456-
return cast(np.ndarray, self.depth_coordinate.values)
456+
return cast(numpy.ndarray, self.depth_coordinate.values)
457457

458458
@utils.deprecated(
459459
(
@@ -462,7 +462,7 @@ def get_depths(self) -> np.ndarray:
462462
),
463463
DeprecationWarning,
464464
)
465-
def get_times(self) -> np.ndarray:
465+
def get_times(self) -> numpy.ndarray:
466466
"""Get all timesteps in this dataset.
467467
468468
.. deprecated:: 0.5.0
@@ -476,7 +476,7 @@ def get_times(self) -> np.ndarray:
476476
The datetimes will be whatever native format the dataset uses,
477477
likely :class:`numpy.datetime64`.
478478
"""
479-
return cast(np.ndarray, self.time_coordinate.values)
479+
return cast(numpy.ndarray, self.time_coordinate.values)
480480

481481
@abc.abstractmethod
482482
def ravel_index(self, index: Index) -> int:
@@ -940,7 +940,7 @@ def make_poly_collection(
940940
values = data_array.values[self.mask]
941941
kwargs['array'] = values
942942
if 'clim' not in kwargs:
943-
kwargs['clim'] = (np.nanmin(values), np.nanmax(values))
943+
kwargs['clim'] = (numpy.nanmin(values), numpy.nanmax(values))
944944

945945
if 'transform' not in kwargs:
946946
kwargs['transform'] = self.data_crs
@@ -988,15 +988,15 @@ def make_quiver(
988988
"""
989989
from matplotlib.quiver import Quiver
990990

991-
x, y = np.transpose(self.face_centres)
991+
x, y = numpy.transpose(self.face_centres)
992992

993993
# A Quiver needs some values when being initialized.
994994
# We don't always want to provide values to the quiver,
995995
# sometimes preferring to fill them in later,
996996
# so `u` and `v` are optional.
997-
# If they are not provided, we set default quiver values of `np.nan`.
998-
values: Union[Tuple[np.ndarray, np.ndarray], Tuple[float, float]]
999-
values = np.nan, np.nan
997+
# If they are not provided, we set default quiver values of `numpy.nan`.
998+
values: Union[Tuple[numpy.ndarray, numpy.ndarray], Tuple[float, float]]
999+
values = numpy.nan, numpy.nan
10001000

10011001
if u is not None and v is not None:
10021002
u, v = self._get_data_array(u), self._get_data_array(v)
@@ -1024,7 +1024,7 @@ def make_quiver(
10241024

10251025
@property
10261026
@abc.abstractmethod
1027-
def polygons(self) -> np.ndarray:
1027+
def polygons(self) -> numpy.ndarray:
10281028
"""A :class:`numpy.ndarray` of :class:`shapely.Polygon` instances
10291029
representing the cells in this dataset.
10301030
@@ -1047,7 +1047,7 @@ def polygons(self) -> np.ndarray:
10471047
pass
10481048

10491049
@cached_property
1050-
def face_centres(self) -> np.ndarray:
1050+
def face_centres(self) -> numpy.ndarray:
10511051
"""
10521052
A numpy :class:`~numpy.ndarray` of face centres, which are (x, y) pairs.
10531053
The first dimension will be the same length and in the same order
@@ -1057,14 +1057,14 @@ def face_centres(self) -> np.ndarray:
10571057
# This default implementation simply finds the centroid of each polygon.
10581058
# Subclasses are free to override this if the particular convention and dataset
10591059
# provides the cell centres as a data array.
1060-
centres = np.array([
1061-
polygon.centroid.coords[0] if polygon is not None else [np.nan, np.nan]
1060+
centres = numpy.array([
1061+
polygon.centroid.coords[0] if polygon is not None else [numpy.nan, numpy.nan]
10621062
for polygon in self.polygons
10631063
])
1064-
return cast(np.ndarray, centres)
1064+
return cast(numpy.ndarray, centres)
10651065

10661066
@cached_property
1067-
def mask(self) -> np.ndarray:
1067+
def mask(self) -> numpy.ndarray:
10681068
"""
10691069
A boolean :class:`numpy.ndarray` indicating which cells have valid polygons.
10701070
This can be used to select only items from linear arrays
@@ -1081,10 +1081,10 @@ def mask(self) -> np.ndarray:
10811081
--------
10821082
:meth:`Convention.make_linear`
10831083
"""
1084-
mask = np.fromiter(
1084+
mask = numpy.fromiter(
10851085
(p is not None for p in self.polygons),
10861086
dtype=bool, count=self.polygons.size)
1087-
return cast(np.ndarray, mask)
1087+
return cast(numpy.ndarray, mask)
10881088

10891089
@cached_property
10901090
def geometry(self) -> Union[Polygon, MultiPolygon]:

src/emsarray/conventions/arakawa_c.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from functools import cached_property
1414
from typing import Dict, Hashable, List, Optional, Tuple, cast
1515

16-
import numpy as np
16+
import numpy
1717
import xarray
1818
from shapely.geometry.base import BaseGeometry
1919
from xarray.core.dataset import DatasetCoordinates
@@ -74,7 +74,7 @@ def shape(self) -> Tuple[int, int]:
7474
@cached_property
7575
def size(self) -> int:
7676
"""The size of this grid, ``j * i``."""
77-
return cast(int, np.prod(self.shape))
77+
return cast(int, numpy.prod(self.shape))
7878

7979
def __repr__(self) -> str:
8080
bits = (f"{key}: {value!r}" for key, value in [
@@ -260,13 +260,13 @@ def unravel_index(
260260
if grid_kind is None:
261261
grid_kind = ArakawaCGridKind.face
262262
topology = self._topology_for_grid_kind[grid_kind]
263-
j, i = map(int, np.unravel_index(index, topology.shape))
263+
j, i = map(int, numpy.unravel_index(index, topology.shape))
264264
return (grid_kind, j, i)
265265

266266
def ravel_index(self, indices: ArakawaCIndex) -> int:
267267
grid_kind, j, i = indices
268268
topology = self._topology_for_grid_kind[grid_kind]
269-
return int(np.ravel_multi_index((j, i), topology.shape))
269+
return int(numpy.ravel_multi_index((j, i), topology.shape))
270270

271271
def get_grid_kind_and_size(self, data_array: xarray.DataArray) -> Tuple[ArakawaCGridKind, int]:
272272
dims = set(data_array.dims)
@@ -280,12 +280,12 @@ def get_grid_kind_and_size(self, data_array: xarray.DataArray) -> Tuple[ArakawaC
280280

281281
@cached_property
282282
@utils.timed_func
283-
def polygons(self) -> np.ndarray:
283+
def polygons(self) -> numpy.ndarray:
284284
# Make an array of shape (j, i, 2) of all the nodes
285-
grid = np.stack([self.node.longitude.values, self.node.latitude.values], axis=-1)
285+
grid = numpy.stack([self.node.longitude.values, self.node.latitude.values], axis=-1)
286286

287287
# Transform this in to an array of shape (topology.size, 4, 2)
288-
points = np.stack([
288+
points = numpy.stack([
289289
grid[:-1, :-1],
290290
grid[:-1, +1:],
291291
grid[+1:, +1:],
@@ -295,12 +295,12 @@ def polygons(self) -> np.ndarray:
295295
return utils.make_polygons_with_holes(points)
296296

297297
@cached_property
298-
def face_centres(self) -> np.ndarray:
299-
centres = np.column_stack((
298+
def face_centres(self) -> numpy.ndarray:
299+
centres = numpy.column_stack((
300300
self.make_linear(self.face.longitude).values,
301301
self.make_linear(self.face.latitude).values,
302302
))
303-
return cast(np.ndarray, centres)
303+
return cast(numpy.ndarray, centres)
304304

305305
def selector_for_index(self, index: ArakawaCIndex) -> Dict[Hashable, int]:
306306
kind, j, i = index
@@ -342,7 +342,7 @@ def make_clip_mask(
342342
item.linear_index
343343
for polygon, item in self.spatial_index.query(clip_geometry)
344344
if polygon.intersects(clip_geometry)]
345-
face_mask = np.full(self.face.shape, fill_value=False)
345+
face_mask = numpy.full(self.face.shape, fill_value=False)
346346
face_mask.ravel()[intersecting_indices] = True
347347

348348
# Expand the mask by one cell around the clipped region, as a buffer
@@ -357,7 +357,7 @@ def apply_clip_mask(self, clip_mask: xarray.Dataset, work_dir: Pathish) -> xarra
357357

358358

359359
def c_mask_from_centres(
360-
face_mask: np.ndarray,
360+
face_mask: numpy.ndarray,
361361
dimensions: ArakawaCDimensions,
362362
coords: Optional[DatasetCoordinates] = None,
363363
) -> xarray.Dataset:

0 commit comments

Comments
 (0)