Skip to content

Instantly share code, notes, and snippets.

@mgaitan
Created October 21, 2013 12:11
Show Gist options
  • Select an option

  • Save mgaitan/7082865 to your computer and use it in GitHub Desktop.

Select an option

Save mgaitan/7082865 to your computer and use it in GitHub Desktop.
import numpy as np
from numpy.testing import assert_array_equal
def segments(x, y, bp_x, bp_y):
"""
recibe dos arrays de 1D (x, y) y otros dos vectores (bp_x, bp_y) que dan las coordenadas de algunos puntos en x,y..
Devuelve una tupla (X_segs, Y_segs) cada uno de 2D, donde cada fila es el
segmento del array original hasta el valor más a cercano a un punto en (bp_x, bp_y)
"""
# to do
pass
def test_simple():
x = np.array([1, 2, 3, 4, 5])
y = np.array([-5, 3, 4.1, 10, 12])
x_segs, y_segs = segments(x, y, [3], [4.1])
assert_array_equal(x_segs, [[1, 2, 3],
[4, 5]])
assert_array_equal(y_segs, [[-5, 3, 4.1],
[10, 12]])
def test_multiples_points():
x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([-5, 3, 4.1, 10, 10, 10, 10])
x_segs, y_segs = segments(x, y, [2, 5], [3, 10])
assert_array_equal(x_segs, [[1, 2],
[3, 4, 5],
[6, 7]])
assert_array_equal(y_segs, [[-5, 3],
[4.1, 10, 10],
[10, 10]])
def test_break_points_are_not_explicitly_in_x_y():
x = np.linspace(-2, 2, 10)
y = x ** 2
x_segs, y_segs = segments(x, y, [0], [0])
expected_x_segs = np.array([
[-2. , -1.55555556, -1.11111111, -0.66666667, -0.22222222],
[0.22222222, 0.66666667, 1.11111111, 1.55555556, 2. ]
])
expected_y_segs = np.array([
[4. , 2.41975309, 1.2345679 , 0.44444444, 0.04938272],
[0.04938272, 0.44444444, 1.2345679 , 2.41975309, 4. ]
])
assert_array_equal(x_segs, expected_x_segs)
assert_array_equal(y_segs, expected_y_segs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment