Completed
Pull Request — master (#372)
by
unknown
01:41
created

test_non_masked_elements()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 11
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, nearest_intersection_idx,
11
                        resample_nn_1d)
12
from metpy.calc.tools import _next_non_masked_element
13
from metpy.testing import assert_array_almost_equal, assert_array_equal
14
15
16
def test_resample_nn():
17
    """Test 1d nearest neighbor functionality."""
18
    a = np.arange(5.)
19
    b = np.array([2, 3.8])
20
    truth = np.array([2, 4])
21
22
    assert_array_equal(truth, resample_nn_1d(a, b))
23
24
25
def test_nearest_intersection_idx():
26
    """Test nearest index to intersection functionality."""
27
    x = np.linspace(5, 30, 17)
28
    y1 = 3 * x**2
29
    y2 = 100 * x - 650
30
    truth = np.array([2, 12])
31
32
    assert_array_equal(truth, nearest_intersection_idx(y1, y2))
33
34
35
@pytest.mark.parametrize('direction, expected', [
36
    ('all', np.array([[8.88, 24.44], [238.84, 1794.53]])),
37
    ('increasing', np.array([[24.44], [1794.53]])),
38
    ('decreasing', np.array([[8.88], [238.84]]))
39
])
40
def test_find_intersections(direction, expected):
41
    """Test finding the intersection of two curves functionality."""
42
    x = np.linspace(5, 30, 17)
43
    y1 = 3 * x**2
44
    y2 = 100 * x - 650
45
    # Note: Truth is what we will get with this sampling, not the mathematical intersection
46
    assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
47
48
49
def test_find_intersections_no_intersections():
50
    """Test finding the intersection of two curves with no intersections."""
51
    x = np.linspace(5, 30, 17)
52
    y1 = 3 * x + 0
53
    y2 = 5 * x + 5
54
    # Note: Truth is what we will get with this sampling, not the mathematical intersection
55
    truth = np.array([[],
56
                      []])
57
    assert_array_equal(truth, find_intersections(x, y1, y2))
58
59
60
def test_find_intersections_invalid_direction():
61
    """Test exception if an invalid direction is given."""
62
    x = np.linspace(5, 30, 17)
63
    y1 = 3 * x ** 2
64
    y2 = 100 * x - 650
65
    with pytest.raises(ValueError):
66
        find_intersections(x, y1, y2, direction='increaing')
67
68
69
def test_interpolate_nan_linear():
70
    """Test linear interpolation of arrays with NaNs in the y-coordinate."""
71
    x = np.linspace(0, 20, 15)
72
    y = 5 * x + 3
73
    nan_indexes = [1, 5, 11, 12]
74
    y_with_nan = y.copy()
75
    y_with_nan[nan_indexes] = np.nan
76
    assert_array_almost_equal(y, interpolate_nans(x, y_with_nan), 2)
77
78
79
def test_interpolate_nan_log():
80
    """Test log interpolation of arrays with NaNs in the y-coordinate."""
81
    x = np.logspace(1, 5, 15)
82
    y = 5 * np.log(x) + 3
83
    nan_indexes = [1, 5, 11, 12]
84
    y_with_nan = y.copy()
85
    y_with_nan[nan_indexes] = np.nan
86
    assert_array_almost_equal(y, interpolate_nans(x, y_with_nan, kind='log'), 2)
87
88
89
def test_interpolate_nan_invalid():
90
    """Test log interpolation with invalid parameter."""
91
    x = np.logspace(1, 5, 15)
92
    y = 5 * np.log(x) + 3
93
    with pytest.raises(ValueError):
94
        interpolate_nans(x, y, kind='loog')
95
96
97
@pytest.mark.parametrize('mask, expected_idx, expected_element', [
98
    ([False, False, False, False, False], 1, 1),
99
    ([False, True, True, False, False], 3, 3),
100
    ([False, True, True, True, True], None, None)
101
])
102
def test_non_masked_elements(mask, expected_idx, expected_element):
103
    """Test with a valid element."""
104
    a = ma.masked_array(np.arange(5), mask=mask)
105
    idx, element = _next_non_masked_element(a, 1)
106
    assert idx == expected_idx
107
    assert element == expected_element
108