I have to show this phrase in my data analysis: Whenever gazeAngleVelocity goes from more than zero to zero, that's the start of a fixation. Whenever it goes from zero to greater than zero, that's the end of a fixation. Create a flag whenever this happens. I have no idea which command would be needed; does anyone have any idea?
CodePudding user response:
My solution simply compares a number to the previous number. It might be a bit off depending on if the first number's sign is different from the last. Let me know if this works:
a = [1,1,-1,-2,-3,4,5,-3,1]
fixations = []
for index in range(len(a)):
if a[index-1] > 0 and a[index] < 0:
fixations.append("fixation_start")
elif a[index-1] < 0 and a[index] > 0:
fixations.append("fixation_end")
else:
fixations.append("continuation")
CodePudding user response:
Using numpy array you can compare a[i]
with a[i 1]
as follows:
import numpy as np
a = np.arange(4)
print('complete array:')
print(a) # [0, 1, 2, 3]
print('all array but first element')
print(a[1:]) # [1, 2, 3]
print('all array but last element')
print(a[:-1]) # [0, 1, 2]
# now we can compare them:
print('positions where next is greater than previous')
print(a[1:] > a[:-1]) # [True, True, True]
However, consider that, when removing the first (or last) element, there is one "missing" element, so, the resulting array contains one element less than the original.
In your case, you need to impose two constaints, so you also need an &
(and). It should be something like:
import numpy as np
a = np.array([3, 2, 1, 0, 11, 2, 3, 2, 1111, 0, 11111])
print('start of a fixation')
print(a[:-1][(a[1:] == 0 ) & (a[:-1] > 0)]) # [1, 1111]
print('end of a fixation')
print(a[1:][(a[1:] > 0 ) & (a[:-1] == 0)]) # [11, 11111]
CodePudding user response:
You can shift the column to compare the values to the previous ones.
(For this example, I'll use some dummy data.)
curr = df["gazeAngleVelocity"]
prev = curr.shift()
df["fixation_start"] = (prev > 0) & (curr == 0)
df["fixation_end"] = (prev == 0) & (curr > 0)
df
Which gives:
gazeAngleVelocity fixation_start fixation_end
0 0.5 False False
1 1.1 False False
2 0.8 False False
3 0.0 True False
4 1.1 False True
5 2.3 False False
6 0.0 True False
7 0.0 False False
If you want to put the flags on the rows before the change, just shift in the other direction, which gets you the next values:
next_ = curr.shift(-1)
df["fixation_start"] = (curr > 0) & (next_ == 0)
df["fixation_end"] = (curr == 0) & (next_ > 0)
df
Which gives:
gazeAngleVelocity fixation_start fixation_end
0 0.5 False False
1 1.1 False False
2 0.8 True False
3 0.0 False True
4 1.1 False False
5 2.3 True False
6 0.0 False False
7 0.0 False False