Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. Trajectory Direction Resulting_Direction
  2. STRAIGHT NORTH NORTH
  3. STRAIGHT NaN NORTH
  4. LEFT NaN WEST
  5. LEFT NaN WEST
  6. LEFT NaN WEST
  7. STRAIGHT NaN WEST
  8. STRAIGHT NaN WEST
  9. RIGHT NaN NORTH
  10. RIGHT NaN NORTH
  11. RIGHT NaN NORTH
  12.  
  13. df.loc[:,'direction'] = np.NaN
  14. df.loc[df.index == 0, "direction"] = "WEST"
  15. # mask is for finding when a signal hasnt changed in three seconds, but now has
  16. mask = (df.trajectory != df.trajectory.shift(1)) & (df.trajectory == df.trajectory.shift(-1)) & (df.trajectory == df.trajectory.shift(-2))
  17. df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "WEST"),'direction'] = 'SOUTH'
  18. df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "SOUTH"),'direction'] = 'EAST'
  19. df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "EAST"),'direction'] = 'NORTH'
  20. df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "NORTH"),'direction'] = 'WEST'
  21. df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "WEST"),'direction'] = 'NORTH'
  22. df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "SOUTH"),'direction'] = 'WEST'
  23. df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "EAST"),'direction'] = 'SOUTH'
  24. df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "NORTH"),'direction'] = 'EAST'
  25. df.loc[:,'direction'] = df.direction.fillna(method="ffill")
  26. print(df[['trajectory','direction']])
  27.  
  28. thresh = 3
  29. # mark the consecutive direction commands
  30. blocks = df.Trajectory.ne(df.Trajectory.shift()).cumsum()
  31.  
  32.  
  33. # group by blocks
  34. groups = df.groupby(blocks)
  35.  
  36. # enumerate each block
  37. df['mask'] = groups.cumcount()
  38.  
  39. # shift up to mark the beginning
  40. # mod thresh to divide each block into small block of thresh
  41. df['mask'] = groups['mask'].shift(1-thresh) % thresh
  42.  
  43. # for conversion of direction to letters:
  44. changes = {'LEFT': -1,'RIGHT':1}
  45. directions = {0:'NORTH', 1:'EAST', 2:'SOUTH', 3:'WEST'}
  46.  
  47. # update direction changes
  48. direction_changes = (df.Trajectory
  49. .where(df['mask'].eq(2)) # where the changes happends
  50. .map(changes) # replace the changes with number
  51. .fillna(0) # where no direction change is 0
  52. )
  53. # mod 4 for the 4 direction
  54. # and map
  55. df['Resulting_Direction'] = (direction_changes.cumsum() % 4).map(directions)
  56.  
  57. Trajectory Direction Resulting_Direction mask
  58. 0 STRAIGHT NORTH NORTH NaN
  59. 1 STRAIGHT NaN NORTH NaN
  60. 2 LEFT NaN WEST 2.0
  61. 3 LEFT NaN WEST NaN
  62. 4 LEFT NaN WEST NaN
  63. 5 STRAIGHT NaN WEST NaN
  64. 6 STRAIGHT NaN WEST NaN
  65. 7 RIGHT NaN NORTH 2.0
  66. 8 RIGHT NaN NORTH NaN
  67. 9 RIGHT NaN NORTH NaN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement