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

test_find_intersections_invalid()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
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 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
def test_find_intersections_default():
33
    """Test finding the intersection of two curves functionality."""
34
    x = np.linspace(5, 30, 17)
35
    y1 = 3 * x**2
36
    y2 = 100 * x - 650
37
    # Truth is what we will get with this sampling,
38
    # not the mathematical intersection
39
    truth = np.array([[8.88, 24.44],
40
                      [238.84, 1794.53]])
41
42
    assert_array_almost_equal(truth, find_intersections(x, y1, y2), 2)
43
44
45
def test_find_intersections_all():
46
    """Test finding the intersection of two curves functionality."""
47
    x = np.linspace(5, 30, 17)
48
    y1 = 3 * x**2
49
    y2 = 100 * x - 650
50
    # Truth is what we will get with this sampling,
51
    # not the mathematical intersection
52
    truth = np.array([[8.88, 24.44],
53
                      [238.84, 1794.53]])
54
55
    assert_array_almost_equal(truth, find_intersections(x, y1, y2, direction='all'), 2)
56
57
58
def test_find_intersections_increasing():
59
    """Test finding the intersection of two curves functionality with increasing filter."""
60
    x = np.linspace(5, 30, 17)
61
    y1 = 3 * x**2
62
    y2 = 100 * x - 650
63
    # Truth is what we will get with this sampling,
64
    # not the mathematical intersection
65
    truth = np.array([[24.44],
66
                      [1794.53]])
67
68
    assert_array_almost_equal(truth, find_intersections(x, y1, y2, direction='increasing'), 2)
69
70
71
def test_find_intersections_decreasing():
72
    """Test finding the intersection of two curves functionality with decreasing filter."""
73
    x = np.linspace(5, 30, 17)
74
    y1 = 3 * x**2
75
    y2 = 100 * x - 650
76
    # Truth is what we will get with this sampling,
77
    # not the mathematical intersection
78
    truth = np.array([[8.88],
79
                      [238.84]])
80
81
    assert_array_almost_equal(truth, find_intersections(x, y1, y2, direction='decreasing'), 2)
82
83
84
def test_find_intersections_invalid():
85
    """Test finding the intersection of two curves functionality with decreasing filter."""
86
    x = np.linspace(5, 30, 17)
87
    y1 = 3 * x**2
88
    y2 = 100 * x - 650
89
90
    with pytest.raises(ValueError):
91
        find_intersections(x, y1, y2, direction='decreasingd')
92
93
94
def test_find_intersections_no_intersections():
95
    """Test finding the intersection of two curves with no intersections."""
96
    x = np.linspace(5, 30, 17)
97
    y1 = 3 * x + 0
98
    y2 = 5 * x + 5
99
    # Truth is what we will get with this sampling,
100
    # not the mathematical intersection
101
    truth = np.array([[],
102
                      []])
103
104
    assert_array_equal(truth, find_intersections(x, y1, y2))
105