Completed
Push — master ( 895c96...326f8b )
by Ryan
22s
created

test_find_intersections_invalid_direction()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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 pytest
8
9
from metpy.calc import find_intersections, nearest_intersection_idx, resample_nn_1d
10
from metpy.testing import assert_array_almost_equal, assert_array_equal
11
12
13
def test_resample_nn():
14
    """Test 1d nearest neighbor functionality."""
15
    a = np.arange(5.)
16
    b = np.array([2, 3.8])
17
    truth = np.array([2, 4])
18
19
    assert_array_equal(truth, resample_nn_1d(a, b))
20
21
22
def test_nearest_intersection_idx():
23
    """Test nearest index to intersection functionality."""
24
    x = np.linspace(5, 30, 17)
25
    y1 = 3 * x**2
26
    y2 = 100 * x - 650
27
    truth = np.array([2, 12])
28
29
    assert_array_equal(truth, nearest_intersection_idx(y1, y2))
30
31
32
@pytest.mark.parametrize('direction, expected', [
33
    ('all', np.array([[8.88, 24.44], [238.84, 1794.53]])),
34
    ('increasing', np.array([[24.44], [1794.53]])),
35
    ('decreasing', np.array([[8.88], [238.84]]))
36
])
37
def test_find_intersections(direction, expected):
38
    """Test finding the intersection of two curves functionality."""
39
    x = np.linspace(5, 30, 17)
40
    y1 = 3 * x**2
41
    y2 = 100 * x - 650
42
    # Note: Truth is what we will get with this sampling, not the mathematical intersection
43
    assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
44
45
46
def test_find_intersections_no_intersections():
47
    """Test finding the intersection of two curves with no intersections."""
48
    x = np.linspace(5, 30, 17)
49
    y1 = 3 * x + 0
50
    y2 = 5 * x + 5
51
    # Note: Truth is what we will get with this sampling, not the mathematical intersection
52
    truth = np.array([[],
53
                      []])
54
    assert_array_equal(truth, find_intersections(x, y1, y2))
55
56
57
def test_find_intersections_invalid_direction():
58
    """Test exception if an invalid direction is given."""
59
    x = np.linspace(5, 30, 17)
60
    y1 = 3 * x ** 2
61
    y2 = 100 * x - 650
62
    with pytest.raises(ValueError):
63
        find_intersections(x, y1, y2, direction='increaing')
64