def test_find_intersections_invalid_direction(): """Test exception if an invalid direction is given.""" x = np.linspace(5, 30, 17) y1 = 3 * x**2 y2 = 100 * x - 650 with pytest.raises(ValueError): find_intersections(x, y1, y2, direction='increaing')
def test_find_intersections_invalid_direction(): """Test exception if an invalid direction is given.""" x = np.linspace(5, 30, 17) y1 = 3 * x ** 2 y2 = 100 * x - 650 with pytest.raises(ValueError): find_intersections(x, y1, y2, direction='increaing')
def test_find_intersections(direction, expected): """Test finding the intersection of two curves functionality.""" x = np.linspace(5, 30, 17) y1 = 3 * x**2 y2 = 100 * x - 650 # Note: Truth is what we will get with this sampling, not the mathematical intersection assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
def test_find_intersections(direction, expected): """Test finding the intersection of two curves functionality.""" x = np.linspace(5, 30, 17) y1 = 3 * x**2 y2 = 100 * x - 650 # Note: Truth is what we will get with this sampling, not the mathematical intersection assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
def test_find_intersections_no_intersections(): """Test finding the intersection of two curves with no intersections.""" x = np.linspace(5, 30, 17) y1 = 3 * x + 0 y2 = 5 * x + 5 # Note: Truth is what we will get with this sampling, not the mathematical intersection truth = np.array([[], []]) assert_array_equal(truth, find_intersections(x, y1, y2))
def test_find_intersections_units(): """Test handling of units when logarithmic interpolation is called.""" x = np.linspace(5, 30, 17) * units.hPa y1 = 3 * x.m**2 y2 = 100 * x.m - 650 truth = np.array([24.43, 1794.54]) x_test, y_test = find_intersections(x, y1, y2, direction='increasing', log_x=True) assert_array_almost_equal(truth, np.array([x_test.m, y_test.m]).flatten(), 2) assert x_test.units == units.hPa
def test_find_intersections_no_intersections(): """Test finding the intersection of two curves with no intersections.""" x = np.linspace(5, 30, 17) y1 = 3 * x + 0 y2 = 5 * x + 5 # Note: Truth is what we will get with this sampling, not the mathematical intersection truth = np.array([[], []]) assert_array_equal(truth, find_intersections(x, y1, y2))
def test_find_intersections_intersections_in_data_at_ends(direction, expected): """Test finding intersections when intersections are in the data. Test data includes points of intersection, sequential points of intersection, intersection at the ends of the data, and intersections in increasing/decreasing direction. """ x = np.arange(14) y1 = np.array([0, 3, 2, 1, -1, 2, 2, 0, 1, 0, 0, -2, 2, 0]) y2 = np.zeros_like(y1) assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
def test_find_intersections_intersections_in_data_at_ends(direction, expected): """Test finding intersections when intersections are in the data. Test data includes points of intersection, sequential points of intersection, intersection at the ends of the data, and intersections in increasing/decreasing direction. """ x = np.arange(14) y1 = np.array([0, 3, 2, 1, -1, 2, 2, 0, 1, 0, 0, -2, 2, 0]) y2 = np.zeros_like(y1) assert_array_almost_equal(expected, find_intersections(x, y1, y2, direction=direction), 2)
def test_find_intersections(): """Test finding the intersection of two curves functionality.""" x = np.linspace(5, 30, 17) y1 = 3 * x**2 y2 = 100 * x - 650 # Truth is what we will get with this sampling, # not the mathematical intersection truth = np.array([[8.88, 24.44], [238.84, 1794.53]]) assert_array_almost_equal(truth, find_intersections(x, y1, y2), 2)
def test_find_intersections(): """Test finding the intersection of two curves functionality.""" x = np.linspace(5, 30, 17) y1 = 3 * x**2 y2 = 100 * x - 650 # Truth is what we will get with this sampling, # not the mathematical intersection truth = np.array([[8.88, 24.44], [238.84, 1794.53]]) assert_array_almost_equal(truth, find_intersections(x, y1, y2), 2)
def _find_append_zero_crossings(x, y): r""" Find and interpolate zero crossings. Estimate the zero crossings of an x,y series and add estimated crossings to series, returning a sorted array with no duplicate values. Parameters ---------- x : `pint.Quantity` x values of data y : `pint.Quantity` y values of data Returns ------- x : `pint.Quantity` x values of data y : `pint.Quantity` y values of data """ # Find and append crossings to the data crossings = find_intersections(x[1:], y[1:], np.zeros_like(y[1:]) * y.units) print(crossings) x = concatenate((x, crossings[0])) y = concatenate((y, crossings[1])) # Resort so that data are in order sort_idx = np.argsort(x) x = x[sort_idx] y = y[sort_idx] # Remove duplicate data points if there are any keep_idx = np.ediff1d(x, to_end=[1]) > 0 x = x[keep_idx] y = y[keep_idx] return x, y