Skip to content

Commit

Permalink
Merge pull request #123 from ocefpaf/ruff_all_streams
Browse files Browse the repository at this point in the history
Ruff-ALL streams.py
  • Loading branch information
ocefpaf authored Aug 7, 2024
2 parents 67d3ee9 + 53833c9 commit e56dec6
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 316 deletions.
6 changes: 1 addition & 5 deletions ioos_qc/axds.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ def valid_range_test(
# This is required because we don't want to restrict a user from using a pd.Series
# directly with this function. If the data was coming from a Store, it would
# always be a numpy array.
elif (
dtype is None
and hasattr(inp, "values")
and hasattr(inp.values, "dtype")
):
elif dtype is None and hasattr(inp, "values") and hasattr(inp.values, "dtype"):
dtype = inp.values.dtype

# Save original shape
Expand Down
19 changes: 4 additions & 15 deletions ioos_qc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,9 @@ def run(self, **passedkwargs):
# Get the arguments that the test functions support
sig = signature(self.func)
valid_keywords = [
p.name
for p in sig.parameters.values()
if p.kind == p.POSITIONAL_OR_KEYWORD
p.name for p in sig.parameters.values() if p.kind == p.POSITIONAL_OR_KEYWORD
]
testkwargs = {
k: v for k, v in testkwargs.items() if k in valid_keywords
}
testkwargs = {k: v for k, v in testkwargs.items() if k in valid_keywords}
try:
results.append(
CallResult(
Expand Down Expand Up @@ -320,11 +316,7 @@ def calls(self):

@property
def aggregate_calls(self):
return [
c
for c in self._calls
if hasattr(c.func, "aggregate") and c.func.aggregate is True
]
return [c for c in self._calls if hasattr(c.func, "aggregate") and c.func.aggregate is True]

def has(self, stream_id: str, method: Union[callable, str]):
if isinstance(method, str):
Expand Down Expand Up @@ -422,10 +414,7 @@ def __init__(self, source: ConfigTypes) -> None:
elif self.config["region"] and "features" in self.config["region"]:
# Feature based GeoJSON
self.region = GeometryCollection(
[
shape(feature["geometry"])
for feature in self.config["region"]["features"]
],
[shape(feature["geometry"]) for feature in self.config["region"]["features"]],
)
elif self.config["region"] and "geometry" in self.config["region"]:
# Geometry based GeoJSON
Expand Down
5 changes: 1 addition & 4 deletions ioos_qc/config_creator/config_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ def create_config(self, variable_config):
def _load_datasets(self):
"""Load datasets."""
L.debug(f"Loading {len(self.config)} datasets...")
return {
name: xr.load_dataset(self.config[name]["file_path"])
for name in self.config
}
return {name: xr.load_dataset(self.config[name]["file_path"]) for name in self.config}

def _determine_dataset_years(self):
"""Determine year used in datasets, return as dict {dataset_name, year}.
Expand Down
4 changes: 1 addition & 3 deletions ioos_qc/config_creator/get_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ def ocean_atlas_variable_enhance(output_dir, month) -> None:

def ocean_atlas_merge_time(output_dir) -> None:
variable_merged_files = output_dir.glob("ocean_atlas_??.nc")
variable_merged_files = [
str(merged_file) for merged_file in list(variable_merged_files)
]
variable_merged_files = [str(merged_file) for merged_file in list(variable_merged_files)]
variable_merged_files.sort()
output_file = output_dir.parent / "ocean_atlas.nc"

Expand Down
35 changes: 9 additions & 26 deletions ioos_qc/qartod.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,9 @@ def location_test(
# Ignore warnings when comparing NaN values even though they are masked
# https://github.com/numpy/numpy/blob/master/doc/release/1.8.0-notes.rst#runtime-warnings-when-comparing-nan-numbers
with np.errstate(invalid="ignore"):
flag_arr[
(lon < bbox.minx)
| (lat < bbox.miny)
| (lon > bbox.maxx)
| (lat > bbox.maxy)
] = QartodFlags.FAIL
flag_arr[(lon < bbox.minx) | (lat < bbox.miny) | (lon > bbox.maxx) | (lat > bbox.maxy)] = (
QartodFlags.FAIL
)

return flag_arr.reshape(original_shape)

Expand Down Expand Up @@ -238,9 +235,7 @@ def gross_range_test(
raise ValueError(msg)
# Flag suspect outside of user span
with np.errstate(invalid="ignore"):
flag_arr[(inp < uspan.minv) | (inp > uspan.maxv)] = (
QartodFlags.SUSPECT
)
flag_arr[(inp < uspan.minv) | (inp > uspan.maxv)] = QartodFlags.SUSPECT

# Flag suspect outside of sensor span
with np.errstate(invalid="ignore"):
Expand Down Expand Up @@ -421,11 +416,7 @@ def check(self, tinp, inp, zinp):
# Only test non-masked values between the min and max.
# Ignore warnings about comparing masked values
with np.errstate(invalid="ignore"):
z_idx = (
(~zinp.mask)
& (zinp >= m.zspan.minv)
& (zinp <= m.zspan.maxv)
)
z_idx = (~zinp.mask) & (zinp >= m.zspan.minv) & (zinp <= m.zspan.maxv)
else:
# If there is no z data in the config, don't try to filter by depth!
# Set z_idx to all True to prevent filtering
Expand All @@ -451,12 +442,8 @@ def check(self, tinp, inp, zinp):

with np.errstate(invalid="ignore"):
flag_arr[(values_idx & fail_idx)] = QartodFlags.FAIL
flag_arr[(values_idx & ~fail_idx & suspect_idx)] = (
QartodFlags.SUSPECT
)
flag_arr[(values_idx & ~fail_idx & ~suspect_idx)] = (
QartodFlags.GOOD
)
flag_arr[(values_idx & ~fail_idx & suspect_idx)] = QartodFlags.SUSPECT
flag_arr[(values_idx & ~fail_idx & ~suspect_idx)] = QartodFlags.GOOD

return flag_arr

Expand Down Expand Up @@ -752,9 +739,7 @@ def flat_line_test(
tinp = mapdates(tinp).flatten()

# The thresholds are in seconds so we round make sure the interval is also in seconds
time_interval = (
np.median(np.diff(tinp)).astype("timedelta64[s]").astype(float)
)
time_interval = np.median(np.diff(tinp)).astype("timedelta64[s]").astype(float)

def rolling_window(a, window):
"""https://rigtorp.se/2011/01/01/rolling-statistics-numpy.html."""
Expand Down Expand Up @@ -880,9 +865,7 @@ def window_func(w):
if min_obs is not None:
min_periods = min_obs
elif min_period is not None:
time_interval = (
np.median(np.diff(tinp)).astype("timedelta64[s]").astype(float)
)
time_interval = np.median(np.diff(tinp)).astype("timedelta64[s]").astype(float)
min_periods = (min_period / time_interval).astype(int)
else:
min_periods = None
Expand Down
8 changes: 4 additions & 4 deletions ioos_qc/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class CollectedResult:
lon: np.ndarray = None

def __repr__(self) -> str:
return f"<CollectedResult stream_id={self.stream_id} package={self.package} test={self.test}>"
return (
f"<CollectedResult stream_id={self.stream_id} package={self.package} test={self.test}>"
)

def function_name(self) -> str:
return self.function.__name__
Expand Down Expand Up @@ -178,8 +180,6 @@ def collect_results_dict(results):
collected[r.stream_id][testpackage][testname] = np.copy(
flag_arr,
)
collected[r.stream_id][testpackage][testname][r.subset_indexes] = (
testresults
)
collected[r.stream_id][testpackage][testname][r.subset_indexes] = testresults

return collected
28 changes: 6 additions & 22 deletions ioos_qc/stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ def save(

# Exclusion list, skip everything defined
if exclude is not None and (
cr.function in exclude
or cr.stream_id in exclude
or cr.test in cr.test in include
cr.function in exclude or cr.stream_id in exclude or cr.test in cr.test in include
):
continue

Expand Down Expand Up @@ -223,9 +221,7 @@ def save(

# Get flags from module attribute called FLAGS
flags = inspect.getmodule(cr.function).FLAGS
varflagnames = [
d for d in flags.__dict__ if not d.startswith("__")
]
varflagnames = [d for d in flags.__dict__ if not d.startswith("__")]
varflagvalues = [getattr(flags, d) for d in varflagnames]

# Set QC variable attributes
Expand All @@ -247,11 +243,7 @@ def save(
if len(config.contexts) == 1:
calls = config.calls_by_stream_id(cr.stream_id)

calls = [
c
for c in calls
if c.module == cr.package and c.method == cr.test
]
calls = [c for c in calls if c.module == cr.package and c.method == cr.test]
if not calls:
# No stream_id found!
continue
Expand Down Expand Up @@ -372,12 +364,8 @@ def save(self, path_or_ncd, config, results):

# Get flags from module attribute called FLAGS
flags = testpackage.FLAGS
varflagnames = [
d for d in flags.__dict__ if not d.startswith("__")
]
varflagvalues = [
getattr(flags, d) for d in varflagnames
]
varflagnames = [d for d in flags.__dict__ if not d.startswith("__")]
varflagvalues = [getattr(flags, d) for d in varflagnames]

if qcvarname not in ncd.variables:
v = ncd.createVariable(
Expand Down Expand Up @@ -412,11 +400,7 @@ def save(self, path_or_ncd, config, results):
v.setncattr("ioos_qc_target", vname)
# If there is only one context we can write variable specific configs
if len(config.contexts) == 1:
varconfig = (
config.contexts[0]
.streams[vname]
.config[modu][testname]
)
varconfig = config.contexts[0].streams[vname].config[modu][testname]
varconfig = json.dumps(
varconfig,
cls=GeoNumpyDateEncoder,
Expand Down
Loading

0 comments on commit e56dec6

Please sign in to comment.