Completed
Pull Request — master (#352)
by
unknown
01:32
created

test_find_intersections_no_intersections()   A

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 11
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
intersection_directions = ['all', 'increasing', 'decreasing', 'decdreasing']
33
34
35
@pytest.mark.parametrize('direction', intersection_directions)
36
def test_find_intersections(direction):
37
    """Test finding the intersection of two curves functionality."""
38
    x = np.linspace(5, 30, 17)
39
    y1 = 3 * x**2
40
    y2 = 100 * x - 650
41
42
    # Note: Truth is what we will get with this sampling, not the mathematical intersection
43
44
    if direction == 'all':
45
        truth = np.array([[8.88, 24.44],
46
                         [238.84, 1794.53]])
47
        assert_array_almost_equal(truth, find_intersections(x, y1, y2, direction=direction), 2)
48
49
    elif direction == 'increasing':
50
        truth = np.array([[24.44],
51
                         [1794.53]])
52
        assert_array_almost_equal(truth, find_intersections(x, y1, y2, direction=direction), 2)
53
54
    elif direction == 'decreasing':
55
        truth = np.array([[8.88],
56
                          [238.84]])
57
        assert_array_almost_equal(truth, find_intersections(x, y1, y2, direction=direction), 2)
58
59
    elif direction == 'decdreasing':
60
        with pytest.raises(ValueError):
61
            find_intersections(x, y1, y2, direction=direction)
62
63
64
def test_find_intersections_no_intersections():
65
    """Test finding the intersection of two curves with no intersections."""
66
    x = np.linspace(5, 30, 17)
67
    y1 = 3 * x + 0
68
    y2 = 5 * x + 5
69
    # Truth is what we will get with this sampling,
70
    # not the mathematical intersection
71
    truth = np.array([[],
72
                      []])
73
74
    assert_array_equal(truth, find_intersections(x, y1, y2))
75