# Trying to write a code that detects a maximum value before a drop off in slope

I'm trying to write code for a force test that will output the maximum force before structural failure occurs. I'm a bit of a novice to python, so the issue here might be something simple that I'm missing. Essentially, when I run the code, it will run indefinitely without outputting any value (so far, the longest I've let it run is about 10 minutes). I've been using a fake data frame with a vector length of about 80, so I think it must be an issue with the code rather than an issue of my vector being too long/requiring a long processing time.

Here's the code:

def find_new_max(data):

n=5
a=1

groups=np.array_split(data['Force'], len(data['Force'])//n)

slope=[group.max() - group.min() for group in groups[0:a]]

while np.all(np.array(slope)) > 0:
a=a+1

return(max(data['Force'][0:a*n]))

find_new_max(df)


Essentially, the goal is to have the code analyze slope by looking at groups consisting of five data points each, starting with just the first group, and then moving on to the consecutive group if a negative slope is not identified. Once it finds a negative slope, the loop should stop, and it should use the new "a" value to search the vector up to the group with the negative slope for a maximum value.

I'm not quite sure what's wrong here, as no errors are popping up. I've also tried to have it search only one group at a time like this:

def find_new_max(data):

n=5
b=0
a=1

groups=np.array_split(data['Force'], len(data['Force'])//n)

slope=[group.max() - group.min() for group in groups[b:a]]

while np.all(np.array(slope)) > 0:
b=b+1
a=a+1

return(max(data['Force'][0:a*n]))

find_new_max(df)


But the same problem occurs.

Any help is appreciated, thank you!

• Within the while loop you are updating the value of a, but since you are not recalculating the value of slope the value for the while statement does not change, causing it to always be true and therefore run indefinitely. Commented Sep 18, 2023 at 19:09
• @Oxbowerce I changed the code to include slope=[group.max() - group.min() for group in groups[:a]] under the while statement to ensure that the value of slope is also changing as a updates, but the problem persists. Commented Sep 18, 2023 at 19:27
• Fixed it! I was using group.max() and group.min(), which would always yeild a positive number. Just switched to group[-1] and group[0] respectively and it works. Commented Sep 18, 2023 at 19:58