Home > OS >  Longest chain of points satisfying given condition
Longest chain of points satisfying given condition

Time:01-26

I have a graph of discrete set of points.

    y           x
0   1.000000    1000.000000
1   0.999415    1000.000287
2   0.999420    1000.000358
3   0.999376    1000.000609
4   0.999239    1000.000788
5   0.999011    1000.000967
6   1.000389    1000.001433
7   0.999871    1000.001756
8   0.995070    1000.002723
9   0.996683    1000.003404

I want to determine the longest chain of consecutive points where the slope of the line connecting i-1 to i remains within a given range epsilon = 0.4.

def tangent(df, pt1, pt2):
    y = df.iloc[pt2]['y'] - df.iloc[pt1]['y']
    x = df.iloc[pt2]['x'] - df.iloc[pt1]['x']

    return x/y

The data has been normalized to scale the tangent results.

index = 1
while index < df.shape[0]:
    if abs(math.tan(tangent(df,index-1,index) * math.pi)) < epsilon:
        print("result:",index)
        
    index  = 1

The snippet is the draft to detect all such points.

CodePudding user response:

You can simplify the code using pandas methods which apply to whole column (Series):

import numpy as np
...

# equivalent of your `tangent` function
# df['x'].diff() will return a column where every row is 
# actual rows difference with previous one
df['tang'] = df['x'].diff()/df['y'].diff()

# np.tan will calculate the tan of whole column values at once
matches = np.tan(df['tang']) < epsilon

# Get longest chain
(~matches).cumsum()[matches].value_counts().max()

More info:

Pandas diff function

Getting longest True chain

  • Related