/
old_filter.py
executable file
·194 lines (132 loc) · 5.13 KB
/
old_filter.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from scipy.stats.stats import pearsonr
import multiprocessing as mp
file_to_filter = "/media/user/DataFB/AutoHeadFix_Data/0731/EL_LRL/Videos/M1312000377_1438367429.638635.raw"
file_to_save = "/media/user/DataFB/AutoHeadFix_Data/0731/EL_LRL/test.rawf"
corrfile_to_save = "/media/user/DataFB/AutoHeadFix_Data/0731/EL_LRL/corr.rawf"
width = 256
height = 256
frame_rate = 30.0
frame_size = width * height * 3
starting_frame = 100
def get_frames(rgb_file):
global total_number_of_frames
with open(rgb_file, "rb") as file:
frames = np.fromfile(file, dtype=np.uint8)
total_number_of_frames = int(np.size(frames)/frame_size)
frames = np.reshape(frames, (total_number_of_frames, width, height, 3))
frames = frames[starting_frame:, :, :, 1]
frames = np.asarray(frames, dtype=np.float32)
total_number_of_frames = frames.shape[0]
return frames
nyq = frame_rate/2.0
low_limit = 0.1/nyq
high_limit = 1.0/nyq
order = 4
rp = 0.1
Wn = [low_limit, high_limit]
def cheby_filter(frames):
b, a = signal.cheby1(order, rp, Wn, 'bandpass', analog=False)
print "Filtering..."
frames = signal.filtfilt(b, a, frames, axis=0)
#for i in range(frames.shape[-1]):
# frames[:, i] = signal.filtfilt(b, a, frames[:, i])
print "Done!"
return frames
def calculate_avg(frames):
return np.mean(frames, axis=0)
def calculate_df_f0(frames):
print frames.shape
baseline = np.mean(frames, axis=0)
frames = np.divide(np.subtract(frames, baseline), baseline)
return frames
def save_to_file(filename, frames, dtype):
with open(filename, "wb") as file:
frames.astype(dtype).tofile(file)
# Left Barrel
#seed_x = 139
#seed_y = 57
# Left Visual
#seed_x = 200
#seed_y = 80
# Left Hind Limb
#seed_x = 126
#seed_y = 97
# MC
seed_x = 62
seed_y = 113
##
##def correlation_map(seed_x, seed_y, frames):
## seed_pixel = np.asarray(frames[:, seed_x, seed_y], dtype=np.float32)
##
## print np.shape(seed_pixel)
## # Reshape into time and space
## frames = np.reshape(frames, (total_number_of_frames, width*height))
## print np.shape(frames)
## correlation_map = []
## for i in range(frames.shape[-1]):
## correlation_map.append(pearsonr(frames[:, i], seed_pixel)[0])
##
## correlation_map = np.asarray(correlation_map, dtype=np.float32)
## correlation_map = np.reshape(correlation_map, (width, height))
## print np.shape(correlation_map)
##
## return correlation_map
class CorrelationMapDisplayer:
def __init__(self, frames):
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
image = self.get_correlation_map(128, 128, frames)
self.imgplot = self.ax.imshow(image)
self.canvas = self.fig.canvas
self.cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)
self.frames = frames
def display(self, c_map, low, high):
self.imgplot.set_cmap(c_map)
self.imgplot.set_clim(low, high)
self.fig.colorbar(self.imgplot)
plt.show()
def onclick(self, event):
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
# X and Y must be flipped to have correct dimmensions!
image = self.get_correlation_map(int(event.ydata), int(event.xdata), self.frames)
self.imgplot = self.ax.imshow(image)
self.canvas.draw()
def get_correlation_map(self, seed_x, seed_y, frames):
seed_pixel = np.asarray(frames[:, seed_x, seed_y], dtype=np.float32)
print np.shape(seed_pixel)
# Reshape into time and space
frames = np.reshape(frames, (total_number_of_frames, width*height))
print np.shape(frames)
correlation_map = []
for i in range(frames.shape[-1]):
correlation_map.append(pearsonr(frames[:, i], seed_pixel)[0])
correlation_map = np.asarray(correlation_map, dtype=np.float32)
correlation_map = np.reshape(correlation_map, (width, height))
print np.shape(correlation_map)
return correlation_map
def display_image(image, c_map, low_end_limit, high_end_limit, frames):
fig = plt.figure()
ax = fig.add_subplot(111)
imgplot = ax.imshow(image)
imgplot.set_cmap(c_map)
imgplot.set_clim(low_end_limit, high_end_limit)
fig.colorbar(imgplot)
displayer = CorrelationMapDisplayer(fig, image, frames)
plt.show()
frames = get_frames(file_to_filter)
avg_pre_filt = calculate_avg(frames)
#frames = np.reshape(frames, (total_number_of_frames, width*height))
frames = cheby_filter(frames)
#frames = np.reshape(frames, (total_number_of_frames, width, height))
frames += avg_pre_filt
frames = calculate_df_f0(frames)
#corr_map = correlation_map(seed_x, seed_y, frames)
mapper = CorrelationMapDisplayer(frames)
mapper.display('spectral', 0.5, 1.0)
#display_image(corr_map, 'spectral', 0.5, 1.0, frames)
#save_to_file(corrfile_to_save, corr_map, np.float32)
#save_to_file(file_to_save, frames, np.float32)