Skip to content

Commit

Permalink
ADD: pytests to missing funcs (openradar#228)
Browse files Browse the repository at this point in the history
* ADD: pytests to missing funcs
* ADD: Update history
  • Loading branch information
syedhamidali authored and rcjackson committed Nov 26, 2024
1 parent c70b616 commit 7c0c6b9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 0.8.0 (2024-10-28)

This is the first version which uses datatree directly from xarray. Thus, xarray is pinned to version >= 2024.10.0.
* ENH: Added pytests to the missing functions in the `test_xradar` and `test_iris` in order to increase codecov in ({pull}`228`) by [@syedhamidali](https://github.com/syedhamidali).
* ENH: Updated Readme ({pull}`226`) by [@syedhamidali](https://github.com/syedhamidali).
* ADD: Added new module `transform` for transforming CF1 data to CF2 and vice versa ({pull}`224`) by [@syedhamidali](https://github.com/syedhamidali).
* Use DataTree from xarray and add xarray nightly run ({pull}`213`, {pull}`214`, {pull}`215`, {pull}`218`) by [@kmuehlbauer](https://github.com/kmuehlbauer).
Expand Down
92 changes: 92 additions & 0 deletions tests/io/test_iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,95 @@ def test_decode_string():
def test__get_fmt_string():
fmt = "<12sHHi12s12s12s6s12s12sHiiiiiiiiii2sH12sHB1shhiihh80s16s12s48s"
assert iris._get_fmt_string(iris.PRODUCT_CONFIGURATION) == fmt


def test_read_from_record(iris0_file, file_or_filelike):
"""Test reading a specified number of words from a record."""
with _get_data_file(iris0_file, file_or_filelike) as sigmetfile:
data = iris.IrisRecordFile(sigmetfile, loaddata=True)
data.init_record(0) # Start from the first record
record_data = data.read_from_record(10, dtype="int16")
assert len(record_data) == 10
assert isinstance(record_data, np.ndarray)


def test_decode_data(iris0_file, file_or_filelike):
"""Test decoding of data with provided product function."""

# Sample data to decode
data = np.array([0, 2, 3, 128, 255], dtype="int16")
# Sample product dict with decoding function and parameters
prod = {
"func": iris.decode_vel,
"dtype": "int16",
"fkw": {"scale": 0.5, "offset": -1},
}

# Open the file as per the testing framework
with _get_data_file(iris0_file, file_or_filelike) as sigmetfile:
iris_file = iris.IrisRawFile(sigmetfile, loaddata=False)

# Decode data using the provided product function
decoded_data = iris_file.decode_data(data, prod)

# Check that the decoded data is as expected
assert isinstance(decoded_data, np.ndarray), "Decoded data should be a numpy array"
assert decoded_data.dtype in [
np.float32,
np.float64,
], "Decoded data should have float32 or float64 type"

# Expected decoded values
expected_data = [-13.325, 13.325, 26.65, 1692.275, 3384.55]
np.testing.assert_array_almost_equal(decoded_data, expected_data, decimal=2)


def test_get_sweep(iris0_file, file_or_filelike):
"""Test retrieval of sweep data for specified moments."""

# Select the sweep number and moments to retrieve
sweep_number = 1
moments = ["DB_DBZ", "DB_VEL"]

# Open the file and load data
with _get_data_file(iris0_file, file_or_filelike) as sigmetfile:
iris_file = iris.IrisRawFile(sigmetfile, loaddata=True)

# Use get_sweep to retrieve data for the selected sweep and moments
iris_file.get_sweep(sweep_number, moments)
sweep_data = iris_file.data[sweep_number]["sweep_data"]

# Verify that sweep_data structure is populated with the selected moments
for moment in moments:
assert moment in sweep_data, f"{moment} should be in sweep_data"
moment_data = sweep_data[moment]
assert moment_data.shape == (360, 664), f"{moment} data shape mismatch"

# Check data types for moments, including masked arrays for velocity
if moment == "DB_VEL":
assert isinstance(
moment_data, np.ma.MaskedArray
), "DB_VEL should be a masked array"
else:
assert isinstance(
moment_data, np.ndarray
), f"{moment} should be a numpy array"

# Optional: check for expected placeholder/masked values
if moment == "DB_DBZ":
assert (
moment_data == -32
).sum() > 0, "DB_DBZ should contain placeholder values (-32)"
if moment == "DB_VEL":
assert moment_data.mask.sum() > 0, "DB_VEL should have masked values"


def test_array_from_file(iris0_file, file_or_filelike):
"""Test retrieving an array from a file."""
with _get_data_file(iris0_file, file_or_filelike) as sigmetfile:
data = iris.IrisRawFile(sigmetfile, loaddata=True)
array_data = data.read_from_file(5) # Adjusted to read_from_file

# Assertions for the read array
assert len(array_data) == 5
assert isinstance(array_data, np.ndarray)
11 changes: 11 additions & 0 deletions tests/test_xradar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
# Distributed under the MIT License. See LICENSE for more info.

"""Tests for `xradar` package."""
import importlib
from unittest import mock

import pytest

import xradar


@pytest.fixture
def response():
Expand All @@ -21,3 +25,10 @@ def test_content(response):
"""Sample pytest test function with the pytest fixture as an argument."""
# from bs4 import BeautifulSoup
# assert 'GitHub' in BeautifulSoup(response.content).title.string


def test_version_import_fallback():
# Temporarily remove `version` to simulate ImportError for this specific import.
with mock.patch.dict("sys.modules", {"xradar.version": None}):
importlib.reload(xradar)
assert xradar.__version__ == "999"

0 comments on commit 7c0c6b9

Please sign in to comment.