1

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.

1 Answers1

2

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

farshad
  • 764
  • 10
  • 25
  • is there a way I can count consecutive `True`s in a row? I think I must opt for a loop. – bugrahaskan Jan 25 '23 at 17:58
  • This seems like the definition of longest chain. Can you please explain more? – farshad Jan 25 '23 at 18:10
  • I want to know intervals where, say, at least 3 `True`s are in a row. it is for dividing the graph into parts for given condition (slope less than epsilon). – bugrahaskan Jan 25 '23 at 18:15
  • There might be, depending on form of output that will be useful to your case. You can ask a new question with an example of input and desired output. – farshad Jan 25 '23 at 21:39