Completed
Push — master ( 42fce5...725411 )
by Ryan
01:25
created

test_reduce_point_density_priority()   A

Complexity

Conditions 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 14
loc 14
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
                        reduce_point_density, 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
109
110
@pytest.fixture
111
def thin_point_data():
112
    r"""Provide scattered points for testing."""
113
    xy = np.array([[0.8793620, 0.9005706], [0.5382446, 0.8766988], [0.6361267, 0.1198620],
114
                   [0.4127191, 0.0270573], [0.1486231, 0.3121822], [0.2607670, 0.4886657],
115
                   [0.7132257, 0.2827587], [0.4371954, 0.5660840], [0.1318544, 0.6468250],
116
                   [0.6230519, 0.0682618], [0.5069460, 0.2326285], [0.1324301, 0.5609478],
117
                   [0.7975495, 0.2109974], [0.7513574, 0.9870045], [0.9305814, 0.0685815],
118
                   [0.5271641, 0.7276889], [0.8116574, 0.4795037], [0.7017868, 0.5875983],
119
                   [0.5591604, 0.5579290], [0.1284860, 0.0968003], [0.2857064, 0.3862123]])
120
    return xy
121
122
123 View Code Duplication
@pytest.mark.parametrize('radius, truth',
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
124
                         [(2.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125
                                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool)),
126
                          (1.0, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127
                                          0, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=np.bool)),
128
                          (0.3, np.array([1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0,
129
                                          0, 0, 0, 0, 0, 1, 0, 0, 0, 0], dtype=np.bool)),
130
                          (0.1, np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
131
                                          0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool))
132
                          ])
133
def test_reduce_point_density(thin_point_data, radius, truth):
134
    r"""Test that reduce_point_density works."""
135
    assert_array_equal(reduce_point_density(thin_point_data, radius=radius), truth)
136
137
138 View Code Duplication
@pytest.mark.parametrize('radius, truth',
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
139
                         [(2.0, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140
                                          0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=np.bool)),
141
                          (0.7, np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142
                                          0, 0, 0, 1, 0, 0, 0, 0, 0, 1], dtype=np.bool)),
143
                          (0.3, np.array([1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0,
144
                                          0, 0, 0, 1, 0, 0, 0, 1, 0, 1], dtype=np.bool)),
145
                          (0.1, np.array([1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
146
                                          0, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool))
147
                          ])
148
def test_reduce_point_density_priority(thin_point_data, radius, truth):
149
    r"""Test that reduce_point_density works properly with priority."""
150
    key = np.array([8, 6, 2, 8, 6, 4, 4, 8, 8, 6, 3, 4, 3, 0, 7, 4, 3, 2, 3, 3, 9])
151
    assert_array_equal(reduce_point_density(thin_point_data, radius, key), truth)
152
153
154
def test_reduce_point_density_1d():
155
    r"""Test that reduce_point_density works with 1D points."""
156
    x = np.array([1, 3, 4, 8, 9, 10])
157
    assert_array_equal(reduce_point_density(x, 2.5),
158
                       np.array([1, 0, 1, 1, 0, 0], dtype=np.bool))
159