supp_figure5_analysis.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import numpy as np
  3. import pandas as pd
  4. from IPython import embed
  5. def main():
  6. df = pd.read_csv(os.path.join("derived_data", "figure2_baseline_properties.csv"), sep=";", index_col=0)
  7. # calculate velocities for the cells in the front as a function of
  8. # their position-difference to cells behind them
  9. receptor_pos = df.receptor_pos_absolute.values
  10. phase_time = (df.phase_shifted/(2*np.pi) * df.eod_period).values * 1000 # ms
  11. receptor_pos_sort, phase_time_sort = list(zip(*sorted(list(zip(receptor_pos, phase_time)))))
  12. receptor_pos_sort = np.asarray(receptor_pos_sort)
  13. phase_time_sort = np.asarray(phase_time_sort)
  14. start_cell_num = 10
  15. min_cell_dist = 5 # mm
  16. shape = (start_cell_num, receptor_pos.shape[0])
  17. pos_diff = np.ones(shape) * np.nan
  18. time_diff = np.ones(shape) * np.nan
  19. velocity = np.ones(shape) * np.nan
  20. for i, (start_pos, start_time) in enumerate(zip(receptor_pos_sort[:start_cell_num], phase_time_sort[:start_cell_num])):
  21. for j, (end_pos, end_time) in enumerate(zip(
  22. receptor_pos_sort[i+min_cell_dist:],
  23. phase_time_sort[i+min_cell_dist:])):
  24. if (end_time-start_time) < 0: # disregard negative velocities (since its within-group variance)
  25. continue
  26. pos_diff[i,j] = end_pos-start_pos
  27. time_diff[i,j] = end_time-start_time
  28. velocity[i,j] = pos_diff[i,j]/time_diff[i,j]
  29. # binning for average
  30. binWidth = 4
  31. flat_pos_diff = pos_diff[np.where(np.isfinite(pos_diff))]
  32. flat_time_diff = time_diff[np.where(np.isfinite(time_diff))]
  33. flat_velocity = velocity[np.where(np.isfinite(velocity))]
  34. pos_diff_bins = np.arange(np.floor(flat_pos_diff.min().min()), np.ceil(flat_pos_diff.max().max())+binWidth, binWidth)
  35. pos_diff_centers = pos_diff_bins[:-1]+binWidth/2
  36. avg_velocity = np.zeros(pos_diff_centers.shape[0])
  37. for i, (left, right) in enumerate(zip(pos_diff_bins[:-1], pos_diff_bins[1:])):
  38. avg_velocity[i] = np.median(flat_velocity[(flat_pos_diff >= left) & (flat_pos_diff < right)])
  39. neighbor_num = 5
  40. neighbor_velocities = np.ones((receptor_pos_sort.shape[0]-neighbor_num, neighbor_num)) * np.nan
  41. for i, (start_pos, start_time) in enumerate(zip(
  42. receptor_pos_sort[:-neighbor_num],
  43. phase_time_sort[:-neighbor_num])):
  44. j = 0
  45. for end_pos, end_time in zip(receptor_pos_sort[i:], phase_time_sort[i:]):
  46. if (end_time-start_time) <= 0:
  47. continue
  48. neighbor_velocities[i,j] = (end_pos-start_pos)/(end_time-start_time)
  49. j += 1
  50. if j >= neighbor_num:
  51. break
  52. np.savez_compressed(os.path.join("derived_data", "suppfig5_velocities.npz"), position_differences=pos_diff, velocities=velocity,
  53. average_velocities=avg_velocity, position_difference_centers=pos_diff_centers)