Completed
Push — master ( 5f4738...de82a3 )
by Ryan
01:49
created

test_log_interp_units()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
1
# Copyright (c) 2008-2015 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
"""Tests for `calc.tools` module."""
5
6
import numpy as np
7
import numpy.ma as ma
8
import pytest
9
10
from metpy.calc import (find_intersections, interpolate_nans, log_interp,
11
                        nearest_intersection_idx, reduce_point_density, resample_nn_1d)
12
from metpy.calc.tools import _next_non_masked_element, delete_masked_points
13
from metpy.testing import assert_array_almost_equal, assert_array_equal
14
from metpy.units import units
15
16
17
def test_resample_nn():
18
    """Test 1d nearest neighbor functionality."""
19
    a = np.arange(5.)
20
    b = np.array([2, 3.8])
21
    truth = np.array([2, 4])
22
23
    assert_array_equal(truth, resample_nn_1d(a, b))
24
25
26
def test_nearest_intersection_idx():
27
    """Test nearest index to intersection functionality."""
28
    x = np.linspace(5, 30, 17)
29
    y1 = 3 * x**2
30
    y2 = 100 * x - 650
31
    truth = np.array([2, 12])
32
33
    assert_array_equal(truth, nearest_intersection_idx(y1, y2))
34
35
36
@pytest.mark.parametrize('direction, expected', [
37
    ('all', np.array([[8.88, 24.44], [238.84, 1794.53]])),
38
    ('increasing', np.array([[24.44], [1794.53]])),
39
    ('decreasing', np.array([[8.88], [238.84]]))
40
])
41
def test_find_intersections(direction, expected):
42
    """Test finding the intersection of two curves functionality."""
43
    x = np.linspace(5, 30, 17)
44
    y1 = 3 * x**2
45
    y2 = 100 * x - 650
46
    # Note: Truth is what we will get with this sampling, not the mathematical intersection
47
    assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
48
49
50
def test_find_intersections_no_intersections():
51
    """Test finding the intersection of two curves with no intersections."""
52
    x = np.linspace(5, 30, 17)
53
    y1 = 3 * x + 0
54
    y2 = 5 * x + 5
55
    # Note: Truth is what we will get with this sampling, not the mathematical intersection
56
    truth = np.array([[],
57
                      []])
58
    assert_array_equal(truth, find_intersections(x, y1, y2))
59
60
61
def test_find_intersections_invalid_direction():
62
    """Test exception if an invalid direction is given."""
63
    x = np.linspace(5, 30, 17)
64
    y1 = 3 * x ** 2
65
    y2 = 100 * x - 650
66
    with pytest.raises(ValueError):
67
        find_intersections(x, y1, y2, direction='increaing')
68
69
70
def test_interpolate_nan_linear():
71
    """Test linear interpolation of arrays with NaNs in the y-coordinate."""
72
    x = np.linspace(0, 20, 15)
73
    y = 5 * x + 3
74
    nan_indexes = [1, 5, 11, 12]
75
    y_with_nan = y.copy()
76
    y_with_nan[nan_indexes] = np.nan
77
    assert_array_almost_equal(y, interpolate_nans(x, y_with_nan), 2)
78
79
80
def test_interpolate_nan_log():
81
    """Test log interpolation of arrays with NaNs in the y-coordinate."""
82
    x = np.logspace(1, 5, 15)
83
    y = 5 * np.log(x) + 3
84
    nan_indexes = [1, 5, 11, 12]
85
    y_with_nan = y.copy()
86
    y_with_nan[nan_indexes] = np.nan
87
    assert_array_almost_equal(y, interpolate_nans(x, y_with_nan, kind='log'), 2)
88
89
90
def test_interpolate_nan_invalid():
91
    """Test log interpolation with invalid parameter."""
92
    x = np.logspace(1, 5, 15)
93
    y = 5 * np.log(x) + 3
94
    with pytest.raises(ValueError):
95
        interpolate_nans(x, y, kind='loog')
96
97
98
@pytest.mark.parametrize('mask, expected_idx, expected_element', [
99
    ([False, False, False, False, False], 1, 1),
100
    ([False, True, True, False, False], 3, 3),
101
    ([False, True, True, True, True], None, None)
102
])
103
def test_non_masked_elements(mask, expected_idx, expected_element):
104
    """Test with a valid element."""
105
    a = ma.masked_array(np.arange(5), mask=mask)
106
    idx, element = _next_non_masked_element(a, 1)
107
    assert idx == expected_idx
108
    assert element == expected_element
109
110
111
@pytest.fixture
112
def thin_point_data():
113
    r"""Provide scattered points for testing."""
114
    xy = np.array([[0.8793620, 0.9005706], [0.5382446, 0.8766988], [0.6361267, 0.1198620],
115
                   [0.4127191, 0.0270573], [0.1486231, 0.3121822], [0.2607670, 0.4886657],
116
                   [0.7132257, 0.2827587], [0.4371954, 0.5660840], [0.1318544, 0.6468250],
117
                   [0.6230519, 0.0682618], [0.5069460, 0.2326285], [0.1324301, 0.5609478],
118
                   [0.7975495, 0.2109974], [0.7513574, 0.9870045], [0.9305814, 0.0685815],
119
                   [0.5271641, 0.7276889], [0.8116574, 0.4795037], [0.7017868, 0.5875983],
120
                   [0.5591604, 0.5579290], [0.1284860, 0.0968003], [0.2857064, 0.3862123]])
121
    return xy
122
123 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
124
@pytest.mark.parametrize('radius, truth',
125
                         [(2.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126
                                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool)),
127
                          (1.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128
                                          0, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=np.bool)),
129
                          (0.3, np.array([1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0,
130
                                          0, 0, 0, 0, 0, 1, 0, 0, 0, 0], dtype=np.bool)),
131
                          (0.1, np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
132
                                          0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool))
133
                          ])
134
def test_reduce_point_density(thin_point_data, radius, truth):
135
    r"""Test that reduce_point_density works."""
136
    assert_array_equal(reduce_point_density(thin_point_data, radius=radius), truth)
137
138 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
139
@pytest.mark.parametrize('radius, truth',
140
                         [(2.0, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141
                                          0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=np.bool)),
142
                          (0.7, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143
                                          0, 0, 0, 1, 0, 0, 0, 0, 0, 1], dtype=np.bool)),
144
                          (0.3, np.array([1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0,
145
                                          0, 0, 0, 1, 0, 0, 0, 1, 0, 1], dtype=np.bool)),
146
                          (0.1, np.array([1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
147
                                          0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool))
148
                          ])
149
def test_reduce_point_density_priority(thin_point_data, radius, truth):
150
    r"""Test that reduce_point_density works properly with priority."""
151
    key = np.array([8, 6, 2, 8, 6, 4, 4, 8, 8, 6, 3, 4, 3, 0, 7, 4, 3, 2, 3, 3, 9])
152
    assert_array_equal(reduce_point_density(thin_point_data, radius, key), truth)
153
154
155
def test_reduce_point_density_1d():
156
    r"""Test that reduce_point_density works with 1D points."""
157
    x = np.array([1, 3, 4, 8, 9, 10])
158
    assert_array_equal(reduce_point_density(x, 2.5),
159
                       np.array([1, 0, 1, 1, 0, 0], dtype=np.bool))
160
161
162
def test_delete_masked_points():
163
    """Test deleting masked points."""
164
    a = ma.masked_array(np.arange(5), mask=[False, True, False, False, False])
165
    b = ma.masked_array(np.arange(5), mask=[False, False, False, True, False])
166
    expected = np.array([0, 2, 4])
167
    a, b = delete_masked_points(a, b)
168
    assert_array_equal(a, expected)
169
    assert_array_equal(b, expected)
170
171
172
def test_log_interp():
173
    """Test interpolating with log x-scale."""
174
    x_log = np.array([1e3, 1e4, 1e5, 1e6])
175
    y_log = np.log(x_log) * 2 + 3
176
    x_interp = np.array([5e3, 5e4, 5e5])
177
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548])
178
    y_interp = log_interp(x_interp, x_log, y_log)
179
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
180
181
182
def test_log_interp_units():
183
    """Test interpolating with log x-scale with units."""
184
    x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa
185
    y_log = (np.log(x_log.m) * 2 + 3) * units.degC
186
    x_interp = np.array([5e3, 5e4, 5e5]) * units.hPa
187
    y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) * units.degC
188
    y_interp = log_interp(x_interp, x_log, y_log)
189
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
190