Skip to content

Commit

Permalink
fix: missed some pylints with wrong .rc file (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
benkozi authored Feb 6, 2025
1 parent 6fd4506 commit 02f815f
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ clear-cache-post-run=no
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-allow-list=
extension-pkg-allow-list=mpi4py,netCDF4

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
Expand Down
18 changes: 9 additions & 9 deletions tests/test_python/test_smoke_dust/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from pathlib import Path
from typing import Union

import netCDF4 as nc
import numpy as np
import pytest
from netCDF4 import Dataset

from smoke_dust.core.context import SmokeDustContext

Expand Down Expand Up @@ -47,11 +47,11 @@ def create_fake_grid_out(root_dir: Path, shape: FakeGridOutShape) -> None:
root_dir: Directory to write grid to.
shape: Grid output shape.
"""
with nc.Dataset(root_dir / "ds_out_base.nc", "w") as ds:
ds.createDimension("grid_yt", shape.y_size)
ds.createDimension("grid_xt", shape.x_size)
with Dataset(root_dir / "ds_out_base.nc", "w") as nc_ds:
nc_ds.createDimension("grid_yt", shape.y_size)
nc_ds.createDimension("grid_xt", shape.x_size)
for varname in ["area", "grid_latt", "grid_lont"]:
var = ds.createVariable(varname, "f4", ("grid_yt", "grid_xt"))
var = nc_ds.createVariable(varname, "f4", ("grid_yt", "grid_xt"))
var[:] = np.ones((shape.y_size, shape.x_size))


Expand Down Expand Up @@ -88,8 +88,8 @@ def create_fake_context(root_dir: Path, overrides: Union[dict, None] = None) ->
try:
context = SmokeDustContext.model_validate(kwds)
except:
for ii in ["CDATE", "DATA"]:
os.unsetenv(ii)
for env_var in ["CDATE", "DATA"]:
os.unsetenv(env_var)
raise
return context

Expand All @@ -103,8 +103,8 @@ def create_file_hash(path: Path) -> str:
Returns:
The file's hex digest.
"""
with open(path, "rb") as f:
with open(path, "rb") as target_file:
file_hash = hashlib.md5()
while chunk := f.read(8192):
while chunk := target_file.read(8192):
file_hash.update(chunk)
return file_hash.hexdigest()
34 changes: 18 additions & 16 deletions tests/test_python/test_smoke_dust/test_core/test_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from pathlib import Path
from typing import Type

import netCDF4 as nc
import numpy as np
import pandas as pd
import pytest
from _pytest.fixtures import SubRequest
from netCDF4 import Dataset
from pydantic import BaseModel
from pytest_mock import MockerFixture

Expand Down Expand Up @@ -41,13 +41,15 @@ def create_fake_restart_files(
restart_dir.mkdir()
for date in forecast_dates:
restart_file = restart_dir / f"{date[:8]}.{date[8:10]}0000.phy_data.nc"
with nc.Dataset(restart_file, "w") as ds:
ds.createDimension("Time")
ds.createDimension("yaxis_1", shape.y_size)
ds.createDimension("xaxis_1", shape.x_size)
totprcp_ave = ds.createVariable("totprcp_ave", "f4", ("Time", "yaxis_1", "xaxis_1"))
with Dataset(restart_file, "w") as nc_ds:
nc_ds.createDimension("Time")
nc_ds.createDimension("yaxis_1", shape.y_size)
nc_ds.createDimension("xaxis_1", shape.x_size)
totprcp_ave = nc_ds.createVariable("totprcp_ave", "f4", ("Time", "yaxis_1", "xaxis_1"))
totprcp_ave[0, ...] = np.ones(shape.as_tuple)
rrfs_hwp_ave = ds.createVariable("rrfs_hwp_ave", "f4", ("Time", "yaxis_1", "xaxis_1"))
rrfs_hwp_ave = nc_ds.createVariable(
"rrfs_hwp_ave", "f4", ("Time", "yaxis_1", "xaxis_1")
)
rrfs_hwp_ave[0, ...] = totprcp_ave[:] + 2


Expand All @@ -69,12 +71,12 @@ def create_fake_rave_interpolated(
for date in forecast_dates:
intp_file = root_dir / f"{rave_to_intp}{date}00_{date}59.nc"
dims = ("t", "lat", "lon")
with nc.Dataset(intp_file, "w") as ds:
ds.createDimension("t")
ds.createDimension("lat", shape.y_size)
ds.createDimension("lon", shape.x_size)
with Dataset(intp_file, "w") as nc_ds:
nc_ds.createDimension("t")
nc_ds.createDimension("lat", shape.y_size)
nc_ds.createDimension("lon", shape.x_size)
for varname in ["frp_avg_hr", "FRE"]:
var = ds.createVariable(varname, "f4", dims)
var = nc_ds.createVariable(varname, "f4", dims)
var[0, ...] = np.ones(shape.as_tuple)


Expand All @@ -86,10 +88,10 @@ def create_fake_veg_map(root_dir: Path, shape: FakeGridOutShape) -> None:
root_dir: The directory to create the file in.
shape: Shape of the output grid.
"""
with nc.Dataset(root_dir / "veg_map.nc", "w") as ds:
ds.createDimension("grid_yt", shape.y_size)
ds.createDimension("grid_xt", shape.x_size)
emiss_factor = ds.createVariable("emiss_factor", "f4", ("grid_yt", "grid_xt"))
with Dataset(root_dir / "veg_map.nc", "w") as nc_ds:
nc_ds.createDimension("grid_yt", shape.y_size)
nc_ds.createDimension("grid_xt", shape.x_size)
emiss_factor = nc_ds.createVariable("emiss_factor", "f4", ("grid_yt", "grid_xt"))
emiss_factor[:] = np.ones((shape.y_size, shape.x_size))


Expand Down
20 changes: 10 additions & 10 deletions tests/test_python/test_smoke_dust/test_core/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,27 @@ def create_fake_rave_and_rrfs_like_data(params: FakeGridParams) -> xr.Dataset:
lon = np.arange(params.shape.x_size, dtype=float) + params.min_lon
lat = np.arange(params.shape.y_size, dtype=float) + params.min_lat
lon_mesh, lat_mesh = np.meshgrid(lon, lat)
ds = xr.Dataset()
nc_ds = xr.Dataset()
dims = ["grid_yt", "grid_xt"]
ds["grid_lont"] = xr.DataArray(lon_mesh, dims=dims)
ds["grid_latt"] = xr.DataArray(lat_mesh, dims=dims)
nc_ds["grid_lont"] = xr.DataArray(lon_mesh, dims=dims)
nc_ds["grid_latt"] = xr.DataArray(lat_mesh, dims=dims)
if params.with_corners:
lonc = np.hstack((lon - 0.5, [lon[-1] + 0.5]))
latc = np.hstack((lat - 0.5, [lat[-1] + 0.5]))
lonc_mesh, latc_mesh = np.meshgrid(lonc, latc)
ds["grid_lon"] = xr.DataArray(lonc_mesh, dims=["grid_y", "grid_x"])
ds["grid_lat"] = xr.DataArray(latc_mesh, dims=["grid_y", "grid_x"])
nc_ds["grid_lon"] = xr.DataArray(lonc_mesh, dims=["grid_y", "grid_x"])
nc_ds["grid_lat"] = xr.DataArray(latc_mesh, dims=["grid_y", "grid_x"])
if params.fields is not None:
if params.ntime is not None:
field_dims = ["time"] + dims
else:
field_dims = dims
for field in params.fields:
ds[field] = create_analytic_data_array(
nc_ds[field] = create_analytic_data_array(
field_dims, lon_mesh, lat_mesh, ntime=params.ntime
)
ds.to_netcdf(params.path)
return ds
nc_ds.to_netcdf(params.path)
return nc_ds


class TestSmokeDustRegridProcessor: # pylint: disable=too-few-public-methods
Expand All @@ -184,6 +184,6 @@ def test_run(
f"*{data_for_test.context.rave_to_intp}*nc", root_dir=tmp_path
)
assert len(interpolated_files) == 24
for f in interpolated_files:
fpath = tmp_path / f
for intp_file in interpolated_files:
fpath = tmp_path / intp_file
assert create_file_hash(fpath) == "8e90b769137aad054a2e49559d209c4d"
4 changes: 2 additions & 2 deletions tests/test_python/test_smoke_dust/test_generate_emissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def test(tmp_path: Path, fake_grid_out_shape: FakeGridOutShape, mocker: MockerFi
]
result = runner.invoke(app, args, catch_exceptions=False)
except:
for ii in ["CDATE", "DATA"]:
os.unsetenv(ii)
for env_var in ["CDATE", "DATA"]:
os.unsetenv(env_var)
raise
print(result.output)

Expand Down
48 changes: 24 additions & 24 deletions ush/smoke_dust/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from typing import Tuple, Literal, Dict

import netCDF4 as nc
from netCDF4 import Dataset
import numpy as np
import pandas as pd
from mpi4py import MPI
Expand All @@ -18,7 +18,7 @@ def open_nc(
mode: Literal["r", "w", "a"] = "r",
clobber: bool = False,
parallel: bool = True,
) -> nc.Dataset:
) -> Dataset:
"""
Open a netCDF file for various operations.
Expand All @@ -31,7 +31,7 @@ def open_nc(
Returns:
A netCDF dataset object.
"""
ds = nc.Dataset(
nc_ds = Dataset(
path,
mode=mode,
clobber=clobber,
Expand All @@ -40,23 +40,23 @@ def open_nc(
info=MPI.Info(),
)
try:
yield ds
yield nc_ds
finally:
ds.close()
nc_ds.close()


def create_sd_coordinate_variable(
ds: nc.Dataset,
nc_ds: Dataset,
sd_variable: SmokeDustVariable,
) -> None:
"""
Create a smoke/dust netCDF spatial coordinate variable.
Args:
ds: Dataset to update.
nc_ds: Dataset to update.
sd_variable: Contains variable metadata.
"""
var_out = ds.createVariable(
var_out = nc_ds.createVariable(
sd_variable.name, "f4", ("lat", "lon"), fill_value=sd_variable.fill_value_float
)
var_out.units = sd_variable.units
Expand All @@ -67,19 +67,19 @@ def create_sd_coordinate_variable(


def create_sd_variable(
ds: nc.Dataset,
nc_ds: Dataset,
sd_variable: SmokeDustVariable,
fill_first_time_index: bool = True,
) -> None:
"""
Create a smoke/dust netCDF variable.
Args:
ds: Dataset to update
nc_ds: Dataset to update
sd_variable: Contains variable metadata
fill_first_time_index: If True, fill the first time index with provided `fill_value_float`
"""
var_out = ds.createVariable(
var_out = nc_ds.createVariable(
sd_variable.name,
"f4",
("t", "lat", "lon"),
Expand All @@ -106,26 +106,26 @@ def create_sd_variable(


def create_template_emissions_file(
ds: nc.Dataset, grid_shape: Tuple[int, int], is_dummy: bool = False
nc_ds: Dataset, grid_shape: Tuple[int, int], is_dummy: bool = False
):
"""
Create a smoke/dust template netCDF emissions file.
Args:
ds: The target netCDF dataset object.
nc_ds: The target netCDF dataset object.
grid_shape: The grid shape to create.
is_dummy: Converted to a netCDF attribute to indicate if the created file is dummy
emissions or will contain actual values.
"""
ds.createDimension("t", None)
ds.createDimension("lat", grid_shape[0])
ds.createDimension("lon", grid_shape[1])
setattr(ds, "PRODUCT_ALGORITHM_VERSION", "Beta")
setattr(ds, "TIME_RANGE", "1 hour")
setattr(ds, "is_dummy", str(is_dummy))
nc_ds.createDimension("t", None)
nc_ds.createDimension("lat", grid_shape[0])
nc_ds.createDimension("lon", grid_shape[1])
setattr(nc_ds, "PRODUCT_ALGORITHM_VERSION", "Beta")
setattr(nc_ds, "TIME_RANGE", "1 hour")
setattr(nc_ds, "is_dummy", str(is_dummy))

for varname in ["geolat", "geolon"]:
create_sd_coordinate_variable(ds, SD_VARS.get(varname))
create_sd_coordinate_variable(nc_ds, SD_VARS.get(varname))


def create_descriptive_statistics(
Expand All @@ -145,13 +145,13 @@ def create_descriptive_statistics(
Returns:
A dataframe containing descriptive statistics fields.
"""
df = pd.DataFrame.from_dict({k: v.filled(np.nan).ravel() for k, v in container.items()})
desc = df.describe()
data_frame = pd.DataFrame.from_dict({k: v.filled(np.nan).ravel() for k, v in container.items()})
desc = data_frame.describe()
adds = {}
for field_name in container.keys():
adds[field_name] = [
df[field_name].sum(),
df[field_name].isnull().sum(),
data_frame[field_name].sum(),
data_frame[field_name].isnull().sum(),
origin,
path,
]
Expand Down
12 changes: 7 additions & 5 deletions ush/smoke_dust/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ def _initialize_values_(cls, values: dict) -> dict:
def _finalize_model_(self) -> "SmokeDustContext":
self._logger = self._init_logging_()

with open_nc(self.grid_out, parallel=False) as ds:
with open_nc(self.grid_out, parallel=False) as nc_ds:
self.grid_out_shape = (
ds.dimensions["grid_yt"].size,
ds.dimensions["grid_xt"].size,
nc_ds.dimensions["grid_yt"].size,
nc_ds.dimensions["grid_xt"].size,
)
self.log(f"{self.grid_out_shape=}")
return self
Expand All @@ -205,7 +205,7 @@ def veg_map(self) -> Path:
@property
def rave_to_intp(self) -> str:
"""File prefix for interpolated RAVE files."""
return self.predef_grid.value + "_intp_"
return self.predef_grid.value + "_intp_" # pylint: disable=no-member

@property
def grid_in(self) -> Path:
Expand Down Expand Up @@ -287,7 +287,9 @@ def _init_logging_(self) -> logging.Logger:
"loggers": {
project_name: {
"handlers": ["default"],
"level": getattr(logging, self.log_level.value.upper()),
"level": getattr(
logging, self.log_level.value.upper() # pylint: disable=no-member
),
},
},
}
Expand Down
Loading

0 comments on commit 02f815f

Please sign in to comment.