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

Geocat-comp ruff cleanup #584

Merged
merged 3 commits into from
Mar 28, 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
1 change: 1 addition & 0 deletions docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ v2024.03.0 (Unreleased)
Internal Changes
^^^^^^^^^^^^^^^^
* Additional pre-commit hook for codespell by `Cora Schneck`_ in (:pr:`579`)
* Remove unused imports, unused variables, code cleanup `Cora Schneck`_ in (:pr:`584`)
* Add M1 runners to CI by `Katelyn FitzGerald`_ in (:pr:`581`)
* Reorganize dask compatibility tests by `Anissa Zacharias`_ in (:pr:`568`)

Expand Down
5 changes: 3 additions & 2 deletions geocat/comp/gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,13 @@ def gradient(data: SupportedTypes,
"""

if (lat is None or lon is None):
if type(data) in [xr.core.dataarray.DataArray, xr.core.dataset.Dataset]:
if isinstance(data,
(xr.core.dataarray.DataArray, xr.core.dataset.Dataset)):
if data.coords is not None:
if 'lat' in data.coords.keys() and 'lon' in data.coords.keys():
lon = data.coords['lon']
lat = data.coords['lat']
elif type(data) is type(np.ndarray):
elif isinstance(data, np.ndarray):
raise Exception('lat or lon is None. \
If the input data are in a numpy.ndarray, \
lat and lon as either 1d or 2d ndarrays must be provided.')
Expand Down
4 changes: 2 additions & 2 deletions geocat/comp/meteorology.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ def relhum_ice(temperature: typing.Union[np.ndarray, list, float],
# ensure all inputs same size
if np.shape(temperature) != np.shape(mixing_ratio) or np.shape(
temperature) != np.shape(pressure):
raise ValueError(f"relhum_ice: dimensions of inputs are not the same")
raise ValueError("relhum_ice: dimensions of inputs are not the same")

relative_humidity = _relhum_ice(temperature, mixing_ratio, pressure)

Expand Down Expand Up @@ -979,7 +979,7 @@ def relhum_water(temperature: typing.Union[np.ndarray, list, float],
# ensure all inputs same size
if np.shape(temperature) != np.shape(mixing_ratio) or np.shape(
temperature) != np.shape(pressure):
raise ValueError(f"relhum_water: dimensions of inputs are not the same")
raise ValueError("relhum_water: dimensions of inputs are not the same")

relative_humidity = _relhum_water(temperature, mixing_ratio, pressure)

Expand Down
9 changes: 4 additions & 5 deletions test/test_climatologies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import cftime
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -213,7 +212,7 @@ def test_yearly_anomaly(self) -> None:
('daily, "year", False', daily, 'year', False)])
def test_keep_attrs(self, name, dset, freq, keep_attrs) -> None:
result = climate_anomaly(dset, freq, keep_attrs=keep_attrs)
if keep_attrs or keep_attrs == None:
if keep_attrs or keep_attrs is None:
assert result.attrs == dset.attrs
elif not keep_attrs:
assert result.attrs == {}
Expand Down Expand Up @@ -304,7 +303,7 @@ class Test_Month_to_Season:
('False', False)])
def test_month_to_season_keep_attrs(self, name, keep_attrs) -> None:
season_ds = month_to_season(self.ds1, 'JFM', keep_attrs=keep_attrs)
if keep_attrs or keep_attrs == None:
if keep_attrs or keep_attrs is None:
assert season_ds.attrs == self.ds1.attrs
elif not keep_attrs:
assert season_ds.attrs == {}
Expand Down Expand Up @@ -550,7 +549,7 @@ class Test_Calendar_Average():
def test_calendar_average_keep_attrs(self, name, dset, freq,
keep_attrs) -> None:
result = calendar_average(dset, freq, keep_attrs=keep_attrs)
if keep_attrs or keep_attrs == None:
if keep_attrs or keep_attrs is None:
assert result.attrs == dset.attrs
elif not keep_attrs:
assert result.attrs == {}
Expand Down Expand Up @@ -863,7 +862,7 @@ def test_climatology_average_keep_attrs(self, name, dset, freq,
freq=freq,
custom_seasons=custom_seasons,
keep_attrs=keep_attrs)
if keep_attrs or keep_attrs == None:
if keep_attrs or keep_attrs is None:
assert result.attrs == dset.attrs
elif not keep_attrs:
assert result.attrs == {}
Expand Down
2 changes: 1 addition & 1 deletion test/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import geocat.datafiles as gdf

# import everything for dask compatibility and performance tests
from geocat.comp import *
from geocat.comp import dewtemp, heat_index, relhum, actual_saturation_vapor_pressure, saturation_vapor_pressure, saturation_vapor_pressure_slope, max_daylight, psychrometric_constant, gradient, interp_hybrid_to_pressure, interp_sigma_to_hybrid


@pytest.fixture(scope="module")
Expand Down
1 change: 0 additions & 1 deletion test/test_fourier_filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import math as m
import sys
import numpy as np
import xarray as xr

Expand Down
1 change: 0 additions & 1 deletion test/test_gradient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import pytest

import numpy as np
Expand Down
44 changes: 21 additions & 23 deletions test/test_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

import geocat.datafiles as gdf
import numpy as np
import numpy.testing as nt
Expand All @@ -14,7 +12,7 @@
try:
ds_atmos = xr.open_dataset(gdf.get("netcdf_files/atmos.nc"),
decode_times=False)
except:
except Exception:
ds_atmos = xr.open_dataset("test/atmos.nc", decode_times=False)

_hyam = ds_atmos.hyam
Expand All @@ -32,7 +30,7 @@ def ds_out(self):
"vinth2p_output.nc"
) # Generated by running ncl_tests/vinth2p_test_conwomap_5.ncl on
# atmos.nc
except:
except Exception:
return xr.open_dataset("test/vinth2p_output.nc")

# Sample input data
Expand Down Expand Up @@ -72,13 +70,13 @@ def test_interp_hybrid_to_pressure_atmos_4d(self, ds_out) -> None:

def test_interp_hybrid_to_pressure_atmos_wrong_method(self) -> None:
with pytest.raises(ValueError):
u_int = interp_hybrid_to_pressure(self.data,
self.ps[0, :, :],
_hyam,
_hybm,
p0=_p0,
new_levels=self.pres3d,
method="wrong_method")
interp_hybrid_to_pressure(self.data,
self.ps[0, :, :],
_hyam,
_hybm,
p0=_p0,
new_levels=self.pres3d,
method="wrong_method")


class Test_interp_hybrid_to_pressure_extrapolate:
Expand All @@ -90,7 +88,7 @@ def ds_ccsm(self):
return xr.open_dataset(
gdf.get("netcdf_files/ccsm35.h0.0021-01.demo.nc"),
decode_times=False)
except:
except Exception:
return xr.open_dataset("test/ccsm35.h0.0021-01.demo.nc",
decode_times=False)

Expand All @@ -100,7 +98,7 @@ def ds_out(self):
try:
return xr.open_dataset("test/vinth2p_ecmwf_output.nc",
decode_times=False)
except:
except Exception:
return xr.open_dataset("vinth2p_ecmwf_output.nc",
decode_times=False)

Expand Down Expand Up @@ -250,7 +248,7 @@ def ds_u(self):
return xr.open_dataset(
gdf.get("netcdf_files/u.89335.1_subset_time361.nc"),
decode_times=False)
except:
except Exception:
return xr.open_dataset("test/u.89335.1_subset_time361.nc",
decode_times=False)

Expand All @@ -261,7 +259,7 @@ def ds_ps(self):
try:
return xr.open_dataset(gdf.get("netcdf_files/ps.89335.1.nc"),
decode_times=False)
except:
except Exception:
return xr.open_dataset("test/ps.89335.1.nc", decode_times=False)

@pytest.fixture(scope="class")
Expand All @@ -271,7 +269,7 @@ def ds_out(self):
return xr.open_dataset(
"sigma2hybrid_output.nc"
) # Generated by running ncl_tests/test_sigma2hybrid.ncl
except:
except Exception:
return xr.open_dataset("test/sigma2hybrid_output.nc")

hyam = xr.DataArray([0.0108093, 0.0130731, 0.03255911, 0.0639471])
Expand Down Expand Up @@ -327,13 +325,13 @@ def test_interp_sigma_to_hybrid_3d_transposed(self, u, sigma, ps,

def test_interp_sigma_to_hybrid_wrong_method(self, u, sigma, ps) -> None:
with pytest.raises(ValueError):
xh = interp_sigma_to_hybrid(u,
sigma,
ps,
self.hyam,
self.hybm,
p0=_p0,
method="wrong_method")
interp_sigma_to_hybrid(u,
sigma,
ps,
self.hyam,
self.hybm,
p0=_p0,
method="wrong_method")


class Test_interp_manually_calc:
Expand Down
20 changes: 7 additions & 13 deletions test/test_meteorology.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import sys
import pytest

import geocat.datafiles as gdf
import metpy.calc as mpcalc
from metpy.units import units
import numpy as np
import pandas as pd
import xarray as xr

from geocat.comp.meteorology import (
Expand Down Expand Up @@ -249,7 +244,7 @@ def ncl_gt(self):
return xr.open_dataarray(
"satvpr_tdew_fao56_output.nc"
).values # Generated by running ncl_tests/test_satvpr_tdew_fao56.ncl
except:
except Exception:
return xr.open_dataarray("test/satvpr_tdew_fao56_output.nc").values

def test_numpy_input(self, ncl_gt) -> None:
Expand Down Expand Up @@ -300,7 +295,7 @@ def ncl_gt(self):
return xr.open_dataarray(
"max_daylight_test.nc"
).values # Generated by running ncl_tests/test_max_daylight.ncl
except:
except Exception:
return xr.open_dataarray("test/max_daylight_test.nc").values

def test_numpy_input(self, ncl_gt) -> None:
Expand Down Expand Up @@ -348,7 +343,7 @@ def ncl_gt(self):
return xr.open_dataarray(
"psychro_fao56_output.nc"
).values # Generated by running ncl_tests/test_psychro_fao56.ncl
except:
except Exception:
return xr.open_dataarray("test/psychro_fao56_output.nc").values

def test_numpy_input(self, ncl_gt) -> None:
Expand Down Expand Up @@ -395,7 +390,7 @@ def ncl_gt(self):
return xr.open_dataarray(
"satvpr_temp_fao56_output.nc"
).values # Generated by running ncl_tests/test_satvpr_temp_fao56.ncl
except:
except Exception:
return xr.open_dataarray("test/satvpr_temp_fao56_output.nc").values

def test_numpy_input(self, ncl_gt) -> None:
Expand Down Expand Up @@ -446,7 +441,7 @@ def ncl_gt(self):
return xr.open_dataarray(
"satvpr_slope_fao56_output.nc"
).values # Generated by running ncl_tests/test_satvpr_slope_fao56.ncl
except:
except Exception:
return xr.open_dataarray("test/satvpr_slope_fao56_output.nc").values

def test_numpy_input(self, ncl_gt) -> None:
Expand Down Expand Up @@ -524,13 +519,12 @@ def test_negative_pressure_warning(self) -> None:
pressure_lev_negative = self.pressure_lev.copy()
pressure_lev_negative[0] = -5
with pytest.warns(UserWarning):
delta_p = delta_pressure(pressure_lev_negative,
self.surface_pressure_scalar)
delta_pressure(pressure_lev_negative, self.surface_pressure_scalar)

def test_relative_pressure_warning(self) -> None:
surface_pressure_low = 0.5
with pytest.warns(UserWarning):
delta_p = delta_pressure(self.pressure_lev, surface_pressure_low)
delta_pressure(self.pressure_lev, surface_pressure_low)

def test_output_type(self) -> None:
delta_pressure_da = delta_pressure(self.pressure_lev_da,
Expand Down
2 changes: 0 additions & 2 deletions test/test_spherical.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import math as ma
import sys
import pytest

import numpy as np
import scipy.special as ss
Expand Down
5 changes: 1 addition & 4 deletions test/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from abc import ABCMeta
import numpy as np
import xarray as xr
Expand Down Expand Up @@ -321,12 +320,11 @@ class Test_eof_ts(BaseEOFTestClass):
def _nc_ds(self):
try:
return xr.open_dataset("eofunc_dataset.nc")
except:
except Exception:
return xr.open_dataset("test/eofunc_dataset.nc")

def test_01(self, _nc_ds) -> None:
sst = _nc_ds.sst
evec = _nc_ds.evec
expected_tsout = _nc_ds.tsout

actual_tsout = eofunc_pcs(sst, npcs=5)
Expand All @@ -350,7 +348,6 @@ def test_01_deprecated(self, _nc_ds) -> None:

def test_02(self, _nc_ds) -> None:
sst = _nc_ds.sst
evec = _nc_ds.evec
expected_tsout = _nc_ds.tsout

actual_tsout = eofunc_pcs(sst, npcs=5, meta=True)
Expand Down
Loading