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

Get rid of warnings in resampler tests #2681

Merged
merged 5 commits into from
Dec 14, 2023
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
53 changes: 7 additions & 46 deletions satpy/resample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python

Check notice on line 1 in satpy/resample.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Lines of Code in a Single File

The lines of code decreases from 631 to 604, improve code health by reducing it to 600. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in satpy/resample.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 4.29 to 4.20, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2018 Satpy developers
#
Expand Down Expand Up @@ -42,7 +42,7 @@
"bucket_sum", "Sum Bucket Resampling", :class:`~satpy.resample.BucketSum`
"bucket_count", "Count Bucket Resampling", :class:`~satpy.resample.BucketCount`
"bucket_fraction", "Fraction Bucket Resampling", :class:`~satpy.resample.BucketFraction`
"gradient_search", "Gradient Search Resampling", :class:`~pyresample.gradient.GradientSearchResampler`
"gradient_search", "Gradient Search Resampling", :meth:`~pyresample.gradient.create_gradient_search_resampler`

The resampling algorithm used can be specified with the ``resampler`` keyword
argument and defaults to ``nearest``:
Expand Down Expand Up @@ -148,13 +148,11 @@

import dask.array as da
import numpy as np
import pyresample
import xarray as xr
import zarr
from packaging import version
from pyresample.ewa import DaskEWAResampler, LegacyDaskEWAResampler
from pyresample.geometry import SwathDefinition
from pyresample.gradient import GradientSearchResampler
from pyresample.gradient import create_gradient_search_resampler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module docstring still uses the class. If it shouldn't, could you update it to point to this function too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this! Fixed in 1bb5655

from pyresample.resampler import BaseResampler as PRBaseResampler

from satpy._config import config_search_paths, get_config_path
Expand All @@ -177,8 +175,6 @@

resamplers_cache: "WeakValueDictionary[tuple, object]" = WeakValueDictionary()

PR_USE_SKIPNA = version.parse(pyresample.__version__) > version.parse("1.17.0")


def hash_dict(the_dict, the_hash=None):
"""Calculate a hash for a dictionary."""
Expand Down Expand Up @@ -772,34 +768,7 @@
repeated_chunks.append(tuple(x * int(factor) for x in axis_chunks))
return tuple(repeated_chunks)


Check notice on line 771 in satpy/resample.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Bumpy Road Ahead

_get_arg_to_pass_for_skipna_handling is no longer above the threshold for logical blocks with deeply nested code. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.
def _get_arg_to_pass_for_skipna_handling(**kwargs):
"""Determine if skipna can be passed to the compute functions for the average and sum bucket resampler."""
# FIXME this can be removed once Pyresample 1.18.0 is a Satpy requirement

if PR_USE_SKIPNA:
if "mask_all_nan" in kwargs:
warnings.warn(
"Argument mask_all_nan is deprecated. Please use skipna for missing values handling. "
"Continuing with default skipna=True, if not provided differently.",
DeprecationWarning,
stacklevel=3
)
kwargs.pop("mask_all_nan")
else:
if "mask_all_nan" in kwargs:
warnings.warn(
"Argument mask_all_nan is deprecated."
"Please update Pyresample and use skipna for missing values handling.",
DeprecationWarning,
stacklevel=3
)
kwargs.setdefault("mask_all_nan", False)
kwargs.pop("skipna")

return kwargs


class BucketResamplerBase(PRBaseResampler):
"""Base class for bucket resampling which implements averaging."""

Expand Down Expand Up @@ -831,12 +800,7 @@

Returns (xarray.DataArray): Data resampled to the target area

"""

Check notice on line 803 in satpy/resample.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Complex Method

BucketResamplerBase.resample decreases in cyclomatic complexity from 14 to 11, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
if not PR_USE_SKIPNA and "skipna" in kwargs:
raise ValueError("You are trying to set the skipna argument but you are using an old version of"
" Pyresample that does not support it."
"Please update Pyresample to 1.18.0 or higher to be able to use this argument.")

self.precompute(**kwargs)
attrs = data.attrs.copy()
data_arr = data.data
Expand Down Expand Up @@ -910,17 +874,16 @@
Returns:
dask.Array
"""
kwargs = _get_arg_to_pass_for_skipna_handling(skipna=skipna, **kwargs)

results = []
if data.ndim == 3:
for i in range(data.shape[0]):
res = self.resampler.get_average(data[i, :, :],
fill_value=fill_value,
skipna=skipna,
**kwargs)
results.append(res)
else:
res = self.resampler.get_average(data, fill_value=fill_value,
res = self.resampler.get_average(data, fill_value=fill_value, skipna=skipna,
**kwargs)
results.append(res)

Expand Down Expand Up @@ -948,16 +911,14 @@

def compute(self, data, skipna=True, **kwargs):
"""Call the resampling."""
kwargs = _get_arg_to_pass_for_skipna_handling(skipna=skipna, **kwargs)

results = []
if data.ndim == 3:
for i in range(data.shape[0]):
res = self.resampler.get_sum(data[i, :, :],
res = self.resampler.get_sum(data[i, :, :], skipna=skipna,
**kwargs)
results.append(res)
else:
res = self.resampler.get_sum(data, **kwargs)
res = self.resampler.get_sum(data, skipna=skipna, **kwargs)
results.append(res)

return da.stack(results)
Expand Down Expand Up @@ -1009,7 +970,7 @@
"nearest": KDTreeResampler,
"bilinear": BilinearResampler,
"native": NativeResampler,
"gradient_search": GradientSearchResampler,
"gradient_search": create_gradient_search_resampler,
"bucket_avg": BucketAvg,
"bucket_sum": BucketSum,
"bucket_count": BucketCount,
Expand Down
81 changes: 8 additions & 73 deletions satpy/tests/test_resample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python

Check notice on line 1 in satpy/tests/test_resample.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Code Duplication

reduced similar code in: TestBucketAvg.test_compute_and_not_use_skipna_handling,TestBucketSum.test_compute_and_not_use_skipna_handling. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.

Check notice on line 1 in satpy/tests/test_resample.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Lines of Code in a Single File

The lines of code in this module is no longer above the threshold
# Copyright (c) 2016 Satpy developers
#
# This file is part of satpy.
Expand Down Expand Up @@ -48,7 +48,6 @@
"""
import dask.array as da
from pyresample.geometry import AreaDefinition, SwathDefinition
from pyresample.utils import proj4_str_to_dict
from xarray import DataArray
ds1 = DataArray(da.zeros(input_shape, chunks=85),
dims=input_dims,
Expand All @@ -62,16 +61,16 @@

input_proj_str = ("+proj=geos +lon_0=-95.0 +h=35786023.0 +a=6378137.0 "
"+b=6356752.31414 +sweep=x +units=m +no_defs")
crs = CRS(input_proj_str)
source = AreaDefinition(
"test_target",
"test_target",
"test_target",
proj4_str_to_dict(input_proj_str),
crs,
input_shape[1], # width
input_shape[0], # height
(-1000., -1500., 1000., 1500.))
ds1.attrs["area"] = source
crs = CRS.from_string(input_proj_str)
ds1 = ds1.assign_coords(crs=crs)

ds2 = ds1.copy()
Expand All @@ -95,7 +94,7 @@
"test_target",
"test_target",
"test_target",
proj4_str_to_dict(output_proj_str),
CRS(output_proj_str),
output_shape[1], # width
output_shape[0], # height
(-1000., -1500., 1000., 1500.),
Expand Down Expand Up @@ -248,8 +247,12 @@
into that chunk size.

"""
from satpy.utils import PerformanceWarning

d_arr = da.zeros((6, 20), chunks=3)
new_data = NativeResampler._expand_reduce(d_arr, {0: 0.5, 1: 0.5})
text = "Array chunk size is not divisible by aggregation factor. Re-chunking to continue native resampling."
with pytest.warns(PerformanceWarning, match=text):
new_data = NativeResampler._expand_reduce(d_arr, {0: 0.5, 1: 0.5})
assert new_data.shape == (3, 10)

def test_expand_reduce_numpy(self):
Expand Down Expand Up @@ -582,17 +585,10 @@
res = self._compute_mocked_bucket_avg(data, return_data=data[0, :, :], fill_value=2)
assert res.shape == (3, 5, 5)

@mock.patch("satpy.resample.PR_USE_SKIPNA", True)
def test_compute_and_use_skipna_handling(self):
"""Test bucket resampler computation and use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_avg(data, fill_value=2, mask_all_nan=True)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
skipna=True)

self._compute_mocked_bucket_avg(data, fill_value=2, skipna=False)
self.bucket.resampler.get_average.assert_called_once_with(
data,
Expand All @@ -605,35 +601,6 @@
fill_value=2,
skipna=True)

@mock.patch("satpy.resample.PR_USE_SKIPNA", False)
def test_compute_and_not_use_skipna_handling(self):
"""Test bucket resampler computation and not use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_avg(data, fill_value=2, mask_all_nan=True)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=True)

self._compute_mocked_bucket_avg(data, fill_value=2, mask_all_nan=False)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)

self._compute_mocked_bucket_avg(data, fill_value=2)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)

self._compute_mocked_bucket_avg(data, fill_value=2, skipna=True)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)

@mock.patch("pyresample.bucket.BucketResampler")
def test_resample(self, pyresample_bucket):
"""Test bucket resamplers resample method."""
Expand Down Expand Up @@ -713,16 +680,10 @@
res = self._compute_mocked_bucket_sum(data, return_data=data[0, :, :])
assert res.shape == (3, 5, 5)

@mock.patch("satpy.resample.PR_USE_SKIPNA", True)
def test_compute_and_use_skipna_handling(self):
"""Test bucket resampler computation and use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_sum(data, mask_all_nan=True)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
skipna=True)

self._compute_mocked_bucket_sum(data, skipna=False)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
Expand All @@ -733,32 +694,6 @@
data,
skipna=True)

@mock.patch("satpy.resample.PR_USE_SKIPNA", False)
def test_compute_and_not_use_skipna_handling(self):
"""Test bucket resampler computation and not use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_sum(data, mask_all_nan=True)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
mask_all_nan=True)

self._compute_mocked_bucket_sum(data, mask_all_nan=False)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
mask_all_nan=False)

self._compute_mocked_bucket_sum(data)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
mask_all_nan=False)

self._compute_mocked_bucket_sum(data, fill_value=2, skipna=True)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)


class TestBucketCount(unittest.TestCase):
"""Test the count bucket resampler."""
Expand Down
Loading