Skip to content

Commit

Permalink
Rename selector to picker
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinkens committed Jun 14, 2024
1 parent a2ed4bd commit 8cee9ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
22 changes: 11 additions & 11 deletions satpy/readers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def get_calib_mode(self, calib_wishlist, channel):
return flat[channel]


class CalibrationCoefficientSelector:
class CalibrationCoefficientPicker:
"""Helper for choosing coefficients out of multiple options.
Example: Three sets of coefficients are available (nominal, meirink, gsics).
Expand All @@ -566,7 +566,7 @@ class CalibrationCoefficientSelector:
.. code-block:: python
from satpy.readers.utils import CalibrationCoefficientSelector
from satpy.readers.utils import CalibrationCoefficientPicker
coefs = {
"nominal": {
Expand Down Expand Up @@ -594,29 +594,29 @@ class CalibrationCoefficientSelector:
.. code-block:: python
>>> s = CalibrationCoefficientSelector(coefs, calib_wishlist)
>>> s.get_coefs("ch1")
>>> picker = CalibrationCoefficientPicker(coefs, calib_wishlist)
>>> picker.get_coefs("ch1")
"meirink_ch1"
>>> s.get_coefs("ch2")
>>> picker.get_coefs("ch2")
"gsics_ch2"
>>> s.get_coefs("ch3")
>>> picker.get_coefs("ch3")
KeyError: 'No gsics calibration coefficients for ch3'
>>> s.get_coefs("ch4")
>>> picker.get_coefs("ch4")
{"mygain": 123}
>>> s.get_coefs("ch5")
>>> picker.get_coefs("ch5")
"nominal_ch5
3. Fallback to nominal for ch3:
.. code-block:: python
>>> s = CalibrationCoefficientSelector(coefs, calib_wishlist, fallback="nominal")
>>> s.get_coefs("ch3")
>>> picker = CalibrationCoefficientPicker(coefs, calib_wishlist, fallback="nominal")
>>> picker.get_coefs("ch3")
"nominal_ch3"
"""

def __init__(self, coefs, calib_wishlist, default="nominal", fallback=None):
"""Initialize the coefficient selector.
"""Initialize the coefficient picker.
Args:
coefs (dict): One set of calibration coefficients for each
Expand Down
23 changes: 12 additions & 11 deletions satpy/tests/reader_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def test_generic_open_binary(tmp_path, data, filename, mode):
assert read_binary_data == dummy_data


class TestCalibrationCoefficientSelector:
class TestCalibrationCoefficientPicker:
"""Unit tests for calibration coefficient selection."""

@pytest.fixture(name="coefs")
Expand Down Expand Up @@ -558,9 +558,9 @@ def fixture_coefs(self):
)
def test_get_coefs(self, coefs, wishlist, expected):
"""Test getting calibration coefficients."""
s = hf.CalibrationCoefficientSelector(coefs, wishlist)
picker = hf.CalibrationCoefficientPicker(coefs, wishlist)
coefs = {
channel: s.get_coefs(channel)
channel: picker.get_coefs(channel)
for channel in ["ch1", "ch2"]
}
assert coefs == expected
Expand All @@ -571,37 +571,38 @@ def test_get_coefs(self, coefs, wishlist, expected):
def test_unknown_mode(self, coefs, wishlist):
"""Test handling of unknown calibration mode."""
with pytest.raises(KeyError, match="Unknown calibration mode"):
hf.CalibrationCoefficientSelector(coefs, wishlist)
hf.CalibrationCoefficientPicker(coefs, wishlist)

@pytest.mark.parametrize(
"wishlist", ["mode1", {"ch2": "mode1"}, {("ch1", "ch2"): "mode1"}]
)
def test_missing_coefs(self, coefs, wishlist):
"""Test that an exception is raised when coefficients are missing."""
s = hf.CalibrationCoefficientSelector(coefs, wishlist)
picker = hf.CalibrationCoefficientPicker(coefs, wishlist)
with pytest.raises(KeyError, match="No mode1 calibration"):
s.get_coefs("ch2")
picker.get_coefs("ch2")

@pytest.mark.parametrize(
"wishlist", ["mode1", {"ch2": "mode1"}, {("ch1", "ch2"): "mode1"}]
)
def test_fallback_to_nominal(self, coefs, wishlist, caplog):
"""Test falling back to nominal coefficients."""
s = hf.CalibrationCoefficientSelector(coefs, wishlist, fallback="nominal")
assert s.get_coefs("ch2") == "nominal_ch2"
picker = hf.CalibrationCoefficientPicker(coefs, wishlist,
fallback="nominal")
assert picker.get_coefs("ch2") == "nominal_ch2"
assert "Falling back" in caplog.text

def test_no_default_coefs(self):
"""Test initialization without default coefficients."""
with pytest.raises(KeyError, match="Need at least"):
hf.CalibrationCoefficientSelector({}, {})
hf.CalibrationCoefficientPicker({}, {})

def test_no_fallback(self):
"""Test initialization without fallback coefficients."""
with pytest.raises(KeyError, match="No fallback calibration"):
hf.CalibrationCoefficientSelector({"nominal": 123}, {}, fallback="foo")
hf.CalibrationCoefficientPicker({"nominal": 123}, {}, fallback="foo")

def test_invalid_wishlist_type(self):
"""Test handling of invalid wishlist type."""
with pytest.raises(TypeError, match="Unsupported wishlist type"):
hf.CalibrationCoefficientSelector({"nominal": 123}, 123)
hf.CalibrationCoefficientPicker({"nominal": 123}, 123)

0 comments on commit 8cee9ef

Please sign in to comment.