Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

bonsai_original_smoothing.py 3.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  1. ##
  2. import os
  3. import pickle
  4. import numpy as np
  5. """Reproduce the smoothed centroids that were calculated online in Bonsai and
  6. used to find the center of the cropped image.
  7. """
  8. smoothed_centroid_dir = ('/media/hdd_data1/michael/deeplabcut/smoothed_centroid_coords')
  9. # TODO: integrate this code with the mx.data.BonsaiData class
  10. crop_length = 300
  11. framerate = 30
  12. interframe_interval = 1000./framerate # ms
  13. fix_centroid_coord = lambda x: max(min(x, 2048 - crop_length/2 - 1), crop_length/2 + 1)
  14. fix_centroid_coord_vfunc = np.vectorize(fix_centroid_coord)
  15. tracking_data_dir = '/home/michael/data/122014/cv'
  16. all_days = ['15122014',
  17. '16122014',
  18. '17122014',
  19. '18122014',
  20. '19122014',
  21. '20122014',
  22. '21122014',
  23. '22122014',
  24. '23122014']
  25. all_mice = ['4I',
  26. '4II',
  27. '4III',
  28. '4IV',
  29. '5I',
  30. '5II',
  31. '5III',
  32. '5IV']
  33. days = all_days
  34. # file containing start and end times for each mice in the session video in ms
  35. time_shifts_file_path = '/home/michael/code/mousemaze/cache/time_shifts.pickle'
  36. with open(time_shifts_file_path, 'rb') as f:
  37. times = pickle.load(f)
  38. # TODO: this should be substituted by a for loop for all days
  39. for day in days:
  40. # First we load the coordinates and smooth it like it was done online in
  41. # Bonsai. That way we know the position of the cropped frame within the
  42. # whole frame as it were these smoothed coordinates that were used to
  43. # decide where to save the crop video.
  44. tracking_file_path = os.path.join(tracking_data_dir, day, 'tracking.csv')
  45. tracking_data = np.loadtxt(tracking_file_path, skiprows=1)
  46. xy_coord = tracking_data[:, 0:2]
  47. smooth_xy = np.empty_like(xy_coord)
  48. smooth_xy[0, :] = xy_coord[0, :]
  49. alpha = 0.3
  50. for i, xy in enumerate(xy_coord[1:, :]):
  51. # x and y are smoothed separately
  52. smooth_xy[i+1] = fix_centroid_coord_vfunc(
  53. smooth_xy[i] + alpha * (xy - smooth_xy[i]))
  54. # smooth_xy is now the smoothed coordinates for the whole day. We need
  55. # to now chop it into a chunk per mouse.
  56. # We obtain the frame times by multiplying the frame number by the inter
  57. # frame interval.
  58. metadata_file_path = os.path.join(tracking_data_dir, day, 'metadata.csv')
  59. metadata_data = np.loadtxt(metadata_file_path, skiprows=1)
  60. frame_numbers = metadata_data[:, 1]
  61. # these don't start at zero so we have to subtract the value of the first
  62. frame_numbers -= frame_numbers[0]
  63. frame_times = frame_numbers * interframe_interval
  64. for mouse in all_mice:
  65. t_start, t_stop = times[day][mouse] # in ms
  66. # we now calculate which index of the data table corresponds to these
  67. # times by finding the closest
  68. i_frame_start = np.argmin(np.abs(frame_times - t_start))
  69. i_frame_stop = np.argmin(np.abs(frame_times - t_stop))
  70. mouse_frame_times = frame_times[i_frame_start: i_frame_stop + 1]
  71. # we need to reshape it to add an empty dimension just so we can hstack
  72. # it later
  73. mouse_frame_times = np.reshape(
  74. mouse_frame_times, (mouse_frame_times.size, 1))
  75. mouse_smoothed_centroid = smooth_xy[i_frame_start: i_frame_stop + 1, :]
  76. mouse_frame_times_smoothed_centroid = np.hstack(
  77. (mouse_frame_times, mouse_smoothed_centroid))
  78. mouse_smoothed_centroid_fname = mouse + '_bonsai_smoothed_coords.pickle'
  79. mouse_smoothed_centroid_path = os.path.join(
  80. smoothed_centroid_dir, day, mouse_smoothed_centroid_fname)
  81. with open(mouse_smoothed_centroid_path, 'wb') as f:
  82. pickle.dump(mouse_frame_times_smoothed_centroid, f)
  83. ##
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...