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

TST: Adding unit tests for whole echoclass module. #1197

Merged
merged 2 commits into from
Jul 7, 2022
Merged
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
179 changes: 179 additions & 0 deletions pyart/retrieve/tests/test_echo_class.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Unit Tests for Py-ART's retrieve/echo_class.py module. """

import numpy as np
from numpy.testing import assert_allclose
import pytest

import pyart
Expand All @@ -20,3 +21,181 @@ def test_steiner_conv_strat_modify_area(area_relation):
area_relation=area_relation)
assert eclass['data'].min() == 0
assert eclass['data'].max() == 2


def test_hydroclass_semisupervised():
radar = pyart.io.read(pyart.testing.NEXRAD_ARCHIVE_MSG31_FILE)
temp_dict = pyart.config.get_metadata('temperature')
temp = np.empty(radar.fields['reflectivity']['data'].shape)
temp[:] = -10.0
temp_dict['data'] = temp
radar.add_field('temperature', temp_dict, replace_existing=True)
radar.fields['specific_differential_phase'] = radar.fields.pop('differential_phase')
radar_small = radar.extract_sweeps([0])

mass_centers = pyart.retrieve.echo_class._get_mass_centers(10e9)

hydro_nofreq = pyart.retrieve.hydroclass_semisupervised(radar_small)
assert 'units' in hydro_nofreq.keys()
assert 'standard_name' in hydro_nofreq.keys()
assert 'long_name' in hydro_nofreq.keys()
assert 'coordinates' in hydro_nofreq.keys()

assert hydro_nofreq['data'].dtype == 'int64'
assert_allclose(
hydro_nofreq['data'][0][0:5],
[6, 6, 6, 6, 6])
assert_allclose(
hydro_nofreq['data'][0][-5:],
[2, 2, 2, 2, 2])

radar_small.instrument_parameters['frequency'] = {'data': np.array([5e9])}
hydro_freq = pyart.retrieve.hydroclass_semisupervised(radar_small)
assert_allclose(
hydro_freq['data'][0][0:5],
[6, 6, 6, 6, 6])
assert_allclose(
hydro_freq['data'][0][-5:],
[2, 2, 2, 2, 2])

hydro = pyart.retrieve.hydroclass_semisupervised(
radar_small, mass_centers=mass_centers)
assert_allclose(
hydro['data'][0][0:5],
[6, 6, 6, 6, 6])
assert_allclose(
hydro['data'][0][-5:],
[2, 2, 2, 2, 2])


def test_data_limits_table():
dlimits_dict = pyart.retrieve.echo_class._data_limits_table()
test_dict = {'Zh': (60.0, -10.0),
'ZDR': (5.0, -5.0),
'KDP': (7.0, -10.0),
'RhoHV': (-5.23, -50.0)}

assert type(dlimits_dict) == dict
assert dlimits_dict == test_dict


def test_get_freq():
freq_band_s = pyart.retrieve.get_freq_band(3e9)
assert freq_band_s == 'S'

freq_band_c = pyart.retrieve.get_freq_band(6e9)
assert freq_band_c == 'C'

freq_band_x = pyart.retrieve.get_freq_band(10e9)
assert freq_band_x == 'X'

freq_band_bad = pyart.retrieve.get_freq_band(10)
assert freq_band_bad == None


def test_mass_centers_dict():
mass_centers_dict = pyart.retrieve.echo_class._mass_centers_table()

assert type(mass_centers_dict)
assert 'C' in mass_centers_dict.keys()
assert 'X' in mass_centers_dict.keys()

assert_allclose(
mass_centers_dict['C'][0],
[1.35829e+01, 4.06300e-01, 4.97000e-02, 9.86800e-01,
1.33030e+03], atol=1e-7)
assert_allclose(
mass_centers_dict['C'][8],
[5.06186e+01, -6.49000e-02, 9.46000e-02, 9.90400e-01,
1.17990e+03], atol=1e-7)

assert_allclose(
mass_centers_dict['X'][0],
[1.9077e+01, 4.1390e-01, 9.9000e-03, 9.8410e-01,
1.0617e+03], atol=1e-7)

assert_allclose(
mass_centers_dict['X'][8],
[4.42216e+01, -3.41900e-01, 6.87000e-02, 9.68300e-01,
1.27270e+03], atol=1e-7)


def test_mass_centers():
mass_centers_good_freq = pyart.retrieve.echo_class._get_mass_centers(10e9)
assert_allclose(
mass_centers_good_freq[0],
[1.90770e+01, 4.13900e-01, 9.90000e-03, 9.84100e-01,
1.06170e+03], atol=1e-7)

low_bad_freq = pyart.retrieve.echo_class._get_mass_centers(3e9)
assert_allclose(
low_bad_freq[0],
[1.35829e+01, 4.06300e-01, 4.97000e-02, 9.86800e-01,
1.33030e+03], atol=1e-7)

high_bad_freq = pyart.retrieve.echo_class._get_mass_centers(13e9)
assert_allclose(
high_bad_freq[0],
[1.90770e+01, 4.13900e-01, 9.90000e-03, 9.84100e-01,
1.06170e+03], atol=1e-7)


def test_assign_to_class():
rhohv = np.array(([0.2, 0.2, 0.2, 0.2, 0.2],
[0.2, 0.2, 0.2, 0.2, 0.2]))
zdr = np.array(([-7.8, -7.8, -7.8, -7.8, -7.8],
[-7.8, -7.8, -7.8, -7.8, -7.8]))
kdp = np.array(([180.5, 180.5, 180.5, 180.5, 180.5],
[180.5, 180.5, 180.5, 180.5, 180.5]))
relh = np.array(([10.0, 10.0, 10.0, 10.0, 10.0],
[10.0, 10.0, 10.0, 10.0, 10.0]))
zh = np.array(([-32.0, -32.0, -32.0, -32.0, -32.0],
[-32.0, -32.0, -32.0, -32.0, -32.0]))

mass_centers = pyart.retrieve.echo_class._get_mass_centers(10e9)
hydroclass, min_dist = pyart.retrieve.echo_class._assign_to_class(
zh, zdr, kdp, rhohv, relh, mass_centers)

assert_allclose(hydroclass[0], [7, 7, 7, 7, 7], atol=1e-7)
assert_allclose(
min_dist[0],
[227.0343910, 227.0343910, 227.0343910,
227.0343910, 227.0343910], atol=1e-7)


def test_standardize():
kdp = np.array(([180.5, 180.5, 180.5, 180.5, 180.5],
[180.5, 180.5, 180.5, 180.5, 180.5]))
field_std_kdp = pyart.retrieve.echo_class._standardize(
kdp, 'KDP', mx=180.0, mn=181.0)
assert_allclose(field_std_kdp[0],
[-1.0, -1.0, -1.0, -1.0, -1.0],
atol=1e-6)

relh = np.array(([10.0, 10.0, 10.0, 10.0, 10.0],
[10.0, 10.0, 10.0, 10.0, 10.0]))
field_std_relh = pyart.retrieve.echo_class._standardize(
relh, 'relH', mx=9.0, mn=5.0)
assert_allclose(
field_std_relh[0],
[0.024994, 0.024994, 0.024994, 0.024994, 0.024994],
atol=1e-6)

rhohv = np.array(([0.2, 0.2, 0.2, 0.2, 0.2],
[0.2, 0.2, 0.2, 0.2, 0.2]))
field_std_rhohv = pyart.retrieve.echo_class._standardize(
rhohv, 'RhoHV', mx=0.1, mn=1.0)
assert_allclose(
field_std_rhohv[0],
[-1.0, -1.0, -1.0, -1.0, -1.0],
atol=1e-6)

field_std_no_limits = pyart.retrieve.echo_class._standardize(
rhohv, 'RhoHV', mx=None, mn=None)
assert_allclose(
field_std_no_limits[0],
[1.0, 1.0, 1.0, 1.0, 1.0],
atol=1e-6)

pytest.raises(
ValueError, pyart.retrieve.echo_class._standardize, rhohv, 'foo')