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

Add numpy rules to ruff #2800

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ line-length = 120
[tool.ruff.lint]
# See https://docs.astral.sh/ruff/rules/
# In the future, add "B", "S", "N"
select = ["A", "D", "E", "W", "F", "I", "PT", "TID", "C90", "Q", "T10", "T20"]
select = ["A", "D", "E", "W", "F", "I", "PT", "TID", "C90", "Q", "T10", "T20", "NPY"]

[tool.ruff.lint.per-file-ignores]
"satpy/tests/*" = ["S101"] # assert allowed in tests
Expand Down
29 changes: 15 additions & 14 deletions satpy/tests/reader_tests/_li_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import xarray as xr

from satpy.tests.reader_tests.test_netcdf_utils import FakeNetCDF4FileHandler
from satpy.tests.utils import RANDOM_GEN

# mapping of netcdf type code to numpy data type:
TYPE_MAP = {
Expand All @@ -44,7 +45,7 @@ def l2_le_schema(settings=None):
nfilters = settings.get("num_filters", 2)

def rand_u16(num):
return np.random.randint(low=0, high=np.iinfo(np.uint16).max - 1, size=num, dtype=np.uint16)
return RANDOM_GEN.integers(low=0, high=np.iinfo(np.uint16).max - 1, size=num, dtype=np.uint16)

return {
"providers": settings.get("providers", {}),
Expand Down Expand Up @@ -100,7 +101,7 @@ def rand_u16(num):
"scale_factor": 0.004,
"add_offset": 0.0,
"long_name": "L2 filter results",
"default_data": lambda: np.random.randint(low=0, high=255, size=(nobs, nfilters), dtype=np.uint8)
"default_data": lambda: RANDOM_GEN.integers(low=0, high=255, size=(nobs, nfilters), dtype=np.uint8)
},
"epoch_time": {
"format": "f8",
Expand Down Expand Up @@ -212,13 +213,13 @@ def l2_lef_schema(settings=None):
"long_name": "Radiance of Flash",
"standard_name": "radiance",
"units": "mW.m-2.sr-1",
"default_data": lambda: np.clip(np.round(np.random.normal(500, 100, nobs)), 1, 2 ** 16 - 1)
"default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(500, 100, nobs)), 1, 2 ** 16 - 1)
},
"event_filter_qa": {
"format": "u1",
"shape": ("events",),
"long_name": "L2 event pre-filtering quality assurance value",
"default_data": lambda: np.random.randint(1, 2 ** 8 - 1, nobs)
"default_data": lambda: RANDOM_GEN.integers(1, 2 ** 8 - 1, nobs)
},
"epoch_time": {
"format": "f8",
Expand All @@ -232,21 +233,21 @@ def l2_lef_schema(settings=None):
"shape": ("events",),
"long_name": "Time offset from epoch time",
"units": "seconds",
"default_data": lambda: np.random.uniform(1, 2 ** 31 - 1, nobs)
"default_data": lambda: RANDOM_GEN.uniform(1, 2 ** 31 - 1, nobs)
},
"detector_row": {
"format": "u2",
"shape": ("events",),
"long_name": "Detector row position of event pixel",
"units": "1",
"default_data": lambda: np.random.randint(1, 1000, nobs)
"default_data": lambda: RANDOM_GEN.integers(1, 1000, nobs)
},
"detector_column": {
"format": "u2",
"shape": ("events",),
"long_name": "Detector column position of event pixel",
"units": "1",
"default_data": lambda: np.random.randint(1, 1000, nobs)
"default_data": lambda: RANDOM_GEN.integers(1, 1000, nobs)
},
}
}
Expand Down Expand Up @@ -328,7 +329,7 @@ def l2_lfl_schema(settings=None):
"long_name": "Radiance of Flash",
"standard_name": "radiance",
"units": "mW.m-2.sr-1",
"default_data": lambda: np.round(np.random.normal(500, 100, nobs))
"default_data": lambda: np.round(RANDOM_GEN.normal(500, 100, nobs))
},
"flash_duration": {
"format": "u2",
Expand All @@ -343,15 +344,15 @@ def l2_lfl_schema(settings=None):
"shape": ("flashes",),
"long_name": "L2 filtered flash confidence",
"standard_name": "flash_filter_confidence",
"default_data": lambda: np.clip(np.round(np.random.normal(20, 10, nobs)), 1, 2 ** 7 - 1)
"default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(20, 10, nobs)), 1, 2 ** 7 - 1)
},
"flash_footprint": {
"format": "u2",
"shape": ("flashes",),
"long_name": "Flash footprint size",
"standard_name": "flash_footprint",
"units": "L1 grid pixels",
"default_data": lambda: np.maximum(1, np.round(np.random.normal(5, 3, nobs)))
"default_data": lambda: np.maximum(1, np.round(RANDOM_GEN.normal(5, 3, nobs)))
},
"flash_id": {
"format": "u4",
Expand All @@ -367,7 +368,7 @@ def l2_lfl_schema(settings=None):
"units": "seconds since 2000-01-01 00:00:00.0",
"standard_name": "time",
"precision": "1 millisecond",
"default_data": lambda: np.random.uniform(stime, etime, nobs)
"default_data": lambda: RANDOM_GEN.uniform(stime, etime, nobs)
},
"l1b_geolocation_warning": {
"format": "i1",
Expand Down Expand Up @@ -437,7 +438,7 @@ def l2_af_schema(settings=None):
"flash_accumulation": {
"format": "u2",
"shape": ("pixels",),
"default_data": lambda: np.clip(np.round(np.random.normal(1, 2, nobs)), 1, 2 ** 16 - 1)
"default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(1, 2, nobs)), 1, 2 ** 16 - 1)
},
"mtg_geos_projection": mtg_geos_projection(),
"x": fci_grid_definition("X", nobs),
Expand Down Expand Up @@ -495,7 +496,7 @@ def l2_afr_schema(settings=None):
"long_name": "Area averaged flash radiance accumulation",
"grid_mapping": "mtg_geos_projection",
"coordinate": "sparse: x y",
"default_data": lambda: np.random.randint(low=1, high=6548, size=(120), dtype=np.int16)
"default_data": lambda: RANDOM_GEN.integers(low=1, high=6548, size=(120), dtype=np.int16)
},
"accumulation_start_times": {
"format": "f4",
Expand Down Expand Up @@ -538,7 +539,7 @@ def fci_grid_definition(axis, nobs):
"standard_name": standard_name,
"units": "radian",
"valid_range": np.asarray([1, 5568]),
"default_data": lambda: np.clip(np.round(np.random.normal(2000, 500, nobs)), 1, 2 ** 16 - 1)
"default_data": lambda: np.clip(np.round(RANDOM_GEN.normal(2000, 500, nobs)), 1, 2 ** 16 - 1)
}


Expand Down
17 changes: 9 additions & 8 deletions satpy/tests/reader_tests/test_epic_l1b_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
import pytest

from satpy.readers.epic_l1b_h5 import CALIB_COEFS

b317_data = np.random.uniform(low=0, high=5200, size=(100, 100))
b688_data = np.random.uniform(low=0, high=5200, size=(100, 100))
sza_data = np.random.uniform(low=0, high=100, size=(100, 100))
vaa_data = np.random.uniform(low=-180, high=180, size=(100, 100))
lon_data = np.random.uniform(low=-90, high=90, size=(100, 100))
lat_data = np.random.uniform(low=-180, high=180, size=(100, 100))
mas_data = np.random.choice([0, 1], size=(100, 100))
from satpy.tests.utils import RANDOM_GEN

b317_data = RANDOM_GEN.uniform(low=0, high=5200, size=(100, 100))
b688_data = RANDOM_GEN.uniform(low=0, high=5200, size=(100, 100))
sza_data = RANDOM_GEN.uniform(low=0, high=100, size=(100, 100))
vaa_data = RANDOM_GEN.uniform(low=-180, high=180, size=(100, 100))
lon_data = RANDOM_GEN.uniform(low=-90, high=90, size=(100, 100))
lat_data = RANDOM_GEN.uniform(low=-180, high=180, size=(100, 100))
mas_data = RANDOM_GEN.choice([0, 1], size=(100, 100))


@pytest.fixture()
Expand Down
4 changes: 2 additions & 2 deletions satpy/tests/reader_tests/test_generic_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pytest
import xarray as xr

from satpy.tests.utils import make_dataid
from satpy.tests.utils import RANDOM_GEN, make_dataid


class TestGenericImage(unittest.TestCase):
Expand Down Expand Up @@ -62,7 +62,7 @@ def setUp(self):
a__[:10, :10] = 0
a__ = da.from_array(a__, chunks=(50, 50))

r_nan__ = np.random.uniform(0., 1., size=(self.y_size, self.x_size))
r_nan__ = RANDOM_GEN.uniform(0., 1., size=(self.y_size, self.x_size))
r_nan__[:10, :10] = np.nan
r_nan__ = da.from_array(r_nan__, chunks=(50, 50))

Expand Down
9 changes: 5 additions & 4 deletions satpy/tests/reader_tests/test_goci2_l2_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pytest_lazyfixture import lazy_fixture

from satpy import Scene
from satpy.tests.utils import RANDOM_GEN

# NOTE:
# The following fixtures are not defined in this file, but are used and injected by Pytest:
Expand Down Expand Up @@ -78,7 +79,7 @@ def _create_bad_lon_lat():
@pytest.fixture(scope="session")
def ac_file(tmp_path_factory):
"""Create a fake atmospheric correction product."""
data = np.random.random((10, 10))
data = RANDOM_GEN.random((10, 10))
RhoC = xr.Dataset(
{"RhoC_555": (["number_of_lines", "pixels_per_line"], data)},
coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)},
Expand All @@ -102,7 +103,7 @@ def ac_file(tmp_path_factory):
@pytest.fixture(scope="module")
def iop_file(tmp_path_factory):
"""Create a fake IOP product."""
data = np.random.random((10, 10))
data = RANDOM_GEN.random((10, 10))
a = xr.Dataset(
{"a_total_555": (["number_of_lines", "pixels_per_line"], data)},
coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)},
Expand All @@ -124,7 +125,7 @@ def iop_file(tmp_path_factory):
@pytest.fixture(scope="module")
def generic_file(tmp_path_factory):
"""Create a fake ouput product like Chl, Zsd etc."""
data = np.random.random((10, 10))
data = RANDOM_GEN.random((10, 10))
geophysical_data = xr.Dataset(
{"Chl": (["number_of_lines", "pixels_per_line"], data)},
coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)},
Expand All @@ -141,7 +142,7 @@ def generic_file(tmp_path_factory):
@pytest.fixture(scope="module")
def generic_bad_file(tmp_path_factory):
"""Create a PP product with lon/lat base name missing."""
data = np.random.random((10, 10))
data = RANDOM_GEN.random((10, 10))
geophysical_data = xr.Dataset(
{"PP": (["number_of_lines", "pixels_per_line"], data)},
coords={"number_of_lines": np.arange(10), "pixels_per_line": np.arange(10)},
Expand Down
7 changes: 4 additions & 3 deletions satpy/tests/reader_tests/test_hrit_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

from satpy.readers import FSFile
from satpy.readers.hrit_base import HRITFileHandler, decompress, get_xritdecompress_cmd, get_xritdecompress_outfile
from satpy.tests.utils import RANDOM_GEN

# NOTE:
# The following fixtures are not defined in this file, but are used and injected by Pytest:
Expand Down Expand Up @@ -145,9 +146,9 @@ def create_stub_hrit(filename, open_fun=open, meta=mda):
lines = meta["number_of_lines"]
cols = meta["number_of_columns"]
total_bits = lines * cols * nbits
arr = np.random.randint(0, 256,
size=int(total_bits / 8),
dtype=np.uint8)
arr = RANDOM_GEN.integers(0, 256,
size=int(total_bits / 8),
dtype=np.uint8)
with open_fun(filename, mode="wb") as fd:
fd.write(b" " * meta["total_header_length"])
bytes_data = arr.tobytes()
Expand Down
8 changes: 4 additions & 4 deletions satpy/tests/reader_tests/test_insat3d_img_l1b_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
open_dataset,
open_datatree,
)
from satpy.tests.utils import make_dataid
from satpy.tests.utils import RANDOM_GEN, make_dataid

# NOTE:
# The following fixtures are not defined in this file, but are used and injected by Pytest:
Expand All @@ -30,10 +30,10 @@
alb_units = "%"
temp_units = "K"
chunks_1km = (1, 46, 1126)
values_1km = np.random.randint(0, 1000, shape_1km, dtype=np.uint16)
values_1km = RANDOM_GEN.integers(0, 1000, shape_1km, dtype=np.uint16)
values_1km[0, 0, 0] = 0
values_4km = np.random.randint(0, 1000, shape_4km, dtype=np.uint16)
values_8km = np.random.randint(0, 1000, shape_8km, dtype=np.uint16)
values_4km = RANDOM_GEN.integers(0, 1000, shape_4km, dtype=np.uint16)
values_8km = RANDOM_GEN.integers(0, 1000, shape_8km, dtype=np.uint16)

values_by_resolution = {1000: values_1km,
4000: values_4km,
Expand Down
13 changes: 7 additions & 6 deletions satpy/tests/reader_tests/test_mirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from satpy.dataset import DataID
from satpy.readers import load_reader
from satpy.readers.yaml_reader import FileYAMLReader
from satpy.tests.utils import RANDOM_GEN

METOP_FILE = "IMG_SX.M2.D17037.S1601.E1607.B0000001.WE.HR.ORB.nc"
NPP_MIRS_L2_SWATH = "NPR-MIRS-IMG_v11r6_npp_s201702061601000_e201702061607000_c202012201658410.nc"
Expand Down Expand Up @@ -77,7 +78,7 @@

def fake_coeff_from_fn(fn):
"""Create Fake Coefficients."""
ameans = np.random.uniform(261, 267, N_CHANNEL)
ameans = RANDOM_GEN.uniform(261, 267, N_CHANNEL)
locations = [
[1, 2],
[1, 2],
Expand Down Expand Up @@ -117,7 +118,7 @@ def fake_coeff_from_fn(fn):
str_coeff = " ".join([str(x) for x in random_coeff])
random_means = np.zeros(all_nchx[nx])
str_means = " ".join([str(x) for x in random_means])
error_val = np.random.uniform(0, 4)
error_val = RANDOM_GEN.uniform(0, 4)
coeffs_line = " {:>2} {:>2} {} {} {}\n".format(idx, fov,
str_coeff,
str_means,
Expand All @@ -138,15 +139,15 @@ def _get_datasets_with_attributes(**kwargs):
"_FillValue": -999,
"valid_range": [0, 50000]},
dims=("Scanline", "Field_of_view", "Channel"))
rr = xr.DataArray(np.random.randint(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16),
rr = xr.DataArray(RANDOM_GEN.integers(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16),
attrs={"long_name": "Rain Rate (mm/hr)",
"units": "mm/hr",
"coordinates": "Longitude Latitude",
"scale_factor": 0.1,
"_FillValue": -999,
"valid_range": [0, 1000]},
dims=("Scanline", "Field_of_view"))
sfc_type = xr.DataArray(np.random.randint(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16),
sfc_type = xr.DataArray(RANDOM_GEN.integers(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16),
attrs={"description": "type of surface:0-ocean," +
"1-sea ice,2-land,3-snow",
"units": "1",
Expand Down Expand Up @@ -187,12 +188,12 @@ def _get_datasets_with_less_attributes():
attrs={"long_name": "Channel Temperature (K)",
"scale_factor": 0.01},
dims=("Scanline", "Field_of_view", "Channel"))
rr = xr.DataArray(np.random.randint(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16),
rr = xr.DataArray(RANDOM_GEN.integers(100, 500, size=(N_SCANLINE, N_FOV), dtype=np.int16),
attrs={"long_name": "Rain Rate (mm/hr)",
"scale_factor": 0.1},
dims=("Scanline", "Field_of_view"))

sfc_type = xr.DataArray(np.random.randint(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16),
sfc_type = xr.DataArray(RANDOM_GEN.integers(0, 4, size=(N_SCANLINE, N_FOV), dtype=np.int16),
attrs={"description": "type of surface:0-ocean," +
"1-sea ice,2-land,3-snow"},
dims=("Scanline", "Field_of_view"))
Expand Down
Loading
Loading