1

I've two 1000x1 lists composing two boundaries (upper and lower). I've as well 74 curves with varying dimensions of (x,y) (around 80000x1) and I would like to find a way to check if the curve (blue) is entirely inside the boundaries (red area).

I've been searching for curves crossing/intersection algorithms for different x,y resolutions but couldn't find any.

Would you have any idea how to deal with this problem?

enter image description here

Lucas Tonon
  • 117
  • 8
  • I would like to avoid interpolating the signal and comparing the matching x axis. In fact I would like to know if there is a built-in method or something that could most likely compare two curves(red and blue) and when they get close enough it would interpolate and see if it crosses. Obs: I've already found something like it for Matlab when in college – Lucas Tonon Jul 30 '20 at 13:31
  • https://github.com/sukhbinder/intersection/blob/master/intersect/intersect.py I've just found this github repository, it helped me almost to 100% of what I wanted The only thing is that It won't detect if the curve is completely out of its boundaries. – Lucas Tonon Jul 30 '20 at 13:46
  • 1
    if you had some tolerance of missing, you could try to fit a curve in the blue data zone you are testing and check if it intersects with top and bottom lines with substract-find roots sort of a thing. This wouldn't work properly if there are aggressive curves and obviously sudden jumps in the blue part. Other than that if you are working point by point then there is not much to do for sudden spikes except brute force checking everything. I am interested to see what will be suggested tho. Good luck – koksalb Jul 30 '20 at 14:25

2 Answers2

1

Ideally, you'd want to set up a list of y values for each co-ordinate in the red area. Then you'd want to check every data point y co-ordinate of the curve with x co-ordinate between red area Xmin <= x <= red area X max. If all the data point y coordinates are in the list, then you know the curve is inside the red area, if one of the data point y coordinates isn't, then you know that some of the curve is outside of the red area.

 red_area_yvals = [y1, y2, y3, y4, y5, ...]
 red_area_xmin = x1
 red_area_xmax = xmax
 curvecoordinates = [[a1,b1],[a2,b2],....]
 check=True

 for coord in curvecoordinates:
     if  coord[0] >= red_area_xmin and coord[0] <= red_area_xmax:
         if coord[1] in red_area_yvals:
             continue 
         else:
             check=False
             break
    
if check:
     print("fully in")
else:
     print("not fully in")

edit: if the red area was a perfect rectangle this would work, for a more complex red area I would think about checking both combination of x and y rather than just y.

2nd edit: There is probably a way of doing this with integration (i.e areas under the curve, taking away the area from under bottom of the red area to y=0 then doing the same for above the curve and comparing the two) but that is beyond my mathematical ability and in Python for complex curves.

Pokebab
  • 183
  • 8
0

great question. Here's my take on it:

# define upper (UL) and lower limits (LL): simulated based on chart
Y_UL = [40, 41, 42, 43, 42, 45]
Y_LL = [32, 33, 34, 35, 34, 38]
# get the measurements: simulated based on chart
my_Y = [35, 36, 35, 35, 35, 36]
# define lambda to encode 1/0 and limits as parameter for a list
my_lambda_onehot_UL = lambda X, UL_list, LL_list: [

    # encode:
    # `1` if within upper and lower limits (no touching)
    # `0` if touching the limits and outside
    1 if (UL_list[idx] > x and x > LL_list[idx]) else 0

    # iterate the list including indexes
    for idx, x in enumerate(X)
]
# run the lambda
print(my_lambda_onehot_UL(my_Y, Y_UL, Y_LL))
# [1, 1, 1, 0, 1, 0]

Output explanation:

  • value #4, 35 is on the lower limit 35
  • value #6, 36 is outside of (38;45)
# check if any point is outside using `numpy`
from numpy import mean as np__mean
np__mean(my_list_01)
# 0.6666666666666666

Having this allow one to quickly collect arrays of 1's and 0's which can then be reduce()'d using averaging, if average is 1 -> all points are within.

The key question remains regarding true values my_Y, Y_UL, and Y_LL.

I wonder if this improves process in any way

Curious Watcher
  • 580
  • 6
  • 12