-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSP_behavior_looming_state_space.py
432 lines (386 loc) · 16.1 KB
/
SP_behavior_looming_state_space.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# %%
# 徐阳
# 开发时间:2021/9/11 20:01
import numpy as np
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
import matplotlib
matplotlib.use('Qt5Agg')
# color_list = sns.color_palette("Spectral", 40)
"""
Arousal Behavior Class Combine-SP behavior 2022.09.23
2、Running:[23, 24, 38] 3、Trotting:[6, 7, 36]
4、Walking:[19, 30, 31] 5、Stepping:[10, 18]
6、Left turning:[26] 7、Right turning:[16]
8、Rising:[17] 9、Standing:[29]
10、Climbing:[8, 9]
11、Sniffing:[2, 3, 4, 11, 21, 22, 25, 33, 37]
12、Grooming:[20, 34, 40] 13、Immobility:[1, 12, 13]
14、LORR:[27, 28, 39] 15、Paralysis:[5]
16、Twitching:[14, 15, 32, 35]
"""
behavior_dict = {
'flight': '#f25832',
'Running': '#cd5c5c',
'Trotting': '#fc7c59',
'Walking': '#ff9e80',
'Stepping': '#ffbfa9',
'Left turning': '#d3afa4',
'Right turning': '#e3c9c2',
'Rising': '#f4a460',
'Standing': '#ffcc00',
'Climbing': '#ffe735',
'Sniffing': '#ff6e00',
'Grooming': '#48a36d',
'Immobility': '#c896c8',
'LORR': '#4798b3',
'Paralysis': '#8bb9cc',
'Twitching': '#c5dce5'
}
color_list = list(behavior_dict.values())
def search_csv(path=".", name=""): # 抓取csv文件
result = []
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
search_csv(item_path, name)
elif os.path.isfile(item_path):
if name + ".csv" == item:
# global csv_result
# csv_result.append(name)
result.append(item_path)
# print(csv_result)
# print(item_path + ";", end="")
# result = item
return result
def read_csv(path='.', name="", column="", element="", state_name=""):
"""
column[0]: file_name column[1]:第一次looming时间点
sheet1:Fwake状态 sheet2:Frorr状态
"""
item_path = os.path.join(path, name)
with open(item_path, 'rb') as f:
csv_data = pd.read_excel(f, sheet_name=state_name)
# df1 = csv_data.set_index([column]) # 选取某一列数据
# sel_data = df1.loc[element] # 根据元素提取特定数据
return csv_data
def pre_data(file_path, movement_label_num, special_time_start, special_time_end):
with open(file_path, 'rb') as f:
csv_data = pd.read_csv(f)
label = csv_data.loc[:, 'new_label']
segBoundary = csv_data.loc[:, 'segBoundary']
seg_space_list = []
seg_before_list = []
# special_time_end = 27000
# special_time_start = 0
# movement_label_num = 15
for j in range(1, len(segBoundary), 1):
if segBoundary[j] >= special_time_end > segBoundary[j - 1]:
stop_num = j
for k in range(1, len(segBoundary), 1):
if special_time_start == 0:
start_num = 0
elif segBoundary[k] >= special_time_start > segBoundary[k - 1]:
start_num = k - 1
for i in range(start_num, stop_num + 1, 1):
# for i in range(641, 671, 1):
if label[i] == movement_label_num:
if i == start_num:
if i == 0:
seg_space = segBoundary[i]
seg_space_list.append(seg_space)
seg_before = 0
seg_before_list.append(seg_before)
else:
# print(i)
seg_space = segBoundary[i] - special_time_start
seg_space_list.append(seg_space)
seg_before = special_time_start
seg_before_list.append(seg_before)
# print(seg_before_list, seg_space_list)
elif i == stop_num:
seg_space = special_time_end - segBoundary[i - 1]
seg_space_list.append(seg_space)
seg_before = segBoundary[i - 1]
seg_before_list.append(seg_before)
else:
seg_space = segBoundary[i] - segBoundary[i - 1]
seg_space_list.append(seg_space)
seg_before = segBoundary[i - 1]
seg_before_list.append(seg_before)
# seg_space_list.remove(seg_space_list[1])
# seg_before_list.remove(seg_before_list[1])
# seg_before_list.insert(0, seg_before_list[0])
x_range_list = []
for i in range(0, len(seg_before_list), 1):
x_left = seg_before_list[i] - special_time_start
x_broken = seg_space_list[i]
if x_left < 0:
x_broken = seg_space_list[i] + x_left
x_left = 0
elif x_broken < 0:
x_broken = 0
else:
x_left = seg_before_list[i] - special_time_start
x_broken = seg_space_list[i]
x_range_list.append((x_left, x_broken))
# seg_before_list.insert(0, seg_before_list[0])
return x_range_list
def data_combine(file_path, special_time_start, special_time_end):
# special_time1 = special_time_start * 60 * 30
# special_time2 = special_time_end * 60 * 30
special_time1 = special_time_start
special_time2 = special_time_end
data = []
for i in range(1, 17):
behavior = pre_data(file_path, i, special_time1, special_time2)
data.append(behavior)
return data
if __name__ == '__main__':
"""
Arousal looming
"""
# mouse_state = 'RoRR'
# a = read_csv(path=r'D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new',
# name="video_info.xlsx", column="looming_time4",
# state_name="Male_{}".format(mouse_state)) # Male_Wakefulness
#
# file_list_1 = []
# # for item in a['Video_name'][0:len(a['Video_name'])]:
# for item in a['Video_name'][0:5]:
# item = item.replace("-camera-0", "")
# file_list1 = search_csv(
# path=r"D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new\BeAOutputs\csv_file_output",
# name="{}_Feature_Space".format(item))
# file_list_1.append(file_list1)
# file_list_1 = list(np.ravel(file_list_1))
#
# b = read_csv(path=r'D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new',
# name="video_info.xlsx", column="looming_time4",
# state_name="Female_{}".format(mouse_state)) # Female_Wakefulness
#
# file_list_2 = []
# # for item in b['Video_name'][0:len(a['Video_name'])]:
# for item in b['Video_name'][0:6]:
# item = item.replace("-camera-0", "")
# file_list1 = search_csv(
# path=r"D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new\BeAOutputs\csv_file_output",
# name="{}_Feature_Space".format(item))
# file_list_2.append(file_list1)
# file_list_2 = list(np.ravel(file_list_2))
# # time = 4
# for time in range(1, 5):
#
# Male_data = []
# Male_time = []
# for i in range(0, len(file_list_1)):
# # single_data = data_combine(file_list_1[i], 10, 15)
# # print(a['looming_time{}'.format(time)][i] + 0 * 30, a['looming_time{}'.format(time)][i] + 120 * 30)
#
# single_data = data_combine(file_list_1[i], a['looming_time{}'.format(time)][i],
# a['looming_time{}'.format(time)][i] + 120 * 30)
# # single_data = data_combine(file_list_1[i], 0, 108000)
#
# Male_data.append(single_data)
#
# Female_data = []
# for i in range(0, len(file_list_2)):
# # single_data = data_combine(file_list_2[i], 10, 15)
# # single_data = data_combine(file_list_2[i], 0, 108000)
# single_data = data_combine(file_list_2[i], b['looming_time{}'.format(time)][i],
# b['looming_time{}'.format(time)][i] + 120 * 30)
# # single_data = data_combine(file_list_2[i], 6618, 6618 + 2 * 1800)
# Female_data.append(single_data)
#
# # plt.figure(figsize=(10, 3), dpi=300)
# fig = plt.figure(figsize=(4, 3), dpi=300)
# ax = fig.add_subplot(111)
# for j in range(len(Male_data)):
# for i in range(len(Female_data[0])):
# plt.broken_barh(Male_data[j][i], (j, 0.8), facecolors=color_list[i])
# plt.broken_barh(Female_data[j][i], (j + 5, 0.8), facecolors=color_list[i])
#
# for i in range(len(Female_data[5])):
# plt.broken_barh(Female_data[5][i], (10, 0.8), facecolors=color_list[i])
#
# plt.axhline(y=4.9, linewidth=1.5, color='black', linestyle='--')
# plt.yticks([2.5, 7.5], ['Males', 'Females'], fontsize=12, rotation=90)
# # plt.xticks([0, 18000], ['0', '10'], fontsize=12)
# plt.xticks([0, 3600], ['0', '2'])
# # plt.xticks([0, 18000, 36000, 54000, 72000, 90000, 108000], ['0', '10', '20', '30', '40', '50', '60'])
# plt.tight_layout()
# # plt.axis('off')
# ax.spines['right'].set_visible(False)
# ax.spines['top'].set_visible(False)
# for axis in ['top', 'bottom', 'left', 'right']:
# ax.spines[axis].set_linewidth(1.5)
# plt.show()
# plt.savefig(
# 'D:/3D_behavior/Arousal_behavior/Arousal_analysis_new/Analysis/state_space/looming_{}_stage_{}_v22.tiff'
# .format(mouse_state, time), dpi=300)
#
# plt.close()
"""
test code
"""
# file_path = file_list_1[3]
# special_time_start = a['looming_time{}'.format(4)][0]
# special_time_end = a['looming_time{}'.format(4)][0] + 2 * 1800
#
# all_data = []
# for movement_label_num in range(0, 16):
# # movement_label_num = 10
#
# with open(file_path, 'rb') as f:
# csv_data = pd.read_csv(f)
#
# label = csv_data.loc[:, 'new_label']
# segBoundary = csv_data.loc[:, 'segBoundary']
#
# seg_space_list = []
# seg_before_list = []
#
# # special_time_end = 27000
# # special_time_start = 0
# # movement_label_num = 15
# for j in range(1, len(segBoundary), 1):
# if segBoundary[j] >= special_time_end > segBoundary[j - 1]:
# stop_num = j
#
# for k in range(1, len(segBoundary), 1):
# if special_time_start == 0:
# start_num = 0
#
# elif segBoundary[k] >= special_time_start > segBoundary[k - 1]:
# start_num = k - 1
#
# for i in range(start_num +1, stop_num + 1, 1):
# # for i in range(641, 671, 1):
# if label[i] == movement_label_num:
# if i == start_num:
# if i == 0:
# seg_space = segBoundary[i]
# seg_space_list.append(seg_space)
# seg_before = 0
# seg_before_list.append(seg_before)
# else:
# # print(i)
# seg_space = segBoundary[i] - special_time_start
# seg_space_list.append(seg_space)
# seg_before = special_time_start
# seg_before_list.append(seg_before)
# # print(seg_before_list, seg_space_list)
#
# elif i == stop_num:
# seg_space = special_time_end - segBoundary[i - 1]
# seg_space_list.append(seg_space)
#
# seg_before = segBoundary[i - 1]
# seg_before_list.append(seg_before)
#
# else:
# seg_space = segBoundary[i] - segBoundary[i - 1]
# seg_space_list.append(seg_space)
#
# seg_before = segBoundary[i - 1]
# seg_before_list.append(seg_before)
#
# # seg_space_list.remove(seg_space_list[1])
# # seg_before_list.remove(seg_before_list[1])
#
# # seg_before_list.insert(0, seg_before_list[0])
# # for i in range(len(seg_before_list)):
# # if seg_before_list[i] < special_time_start:
# # seg_space_list[i] =
# # seg_before_list[i] = special_time_start
#
# x_range_list = []
#
# for i in range(0, len(seg_before_list), 1):
# x_left = seg_before_list[i] - special_time_start
# x_broken = seg_space_list[i]
#
# if x_left < 0:
# x_broken = seg_space_list[i] + x_left
# x_left = 0
# else:
# x_left = seg_before_list[i] - special_time_start
# x_broken = seg_space_list[i]
#
# x_range_list.append((x_left, x_broken))
#
# all_data.append(x_range_list)
"""
单只老鼠使用
"""
mouse_state = 'Wakefulness'
a = read_csv(path=r'D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new',
name="video_info.xlsx", column="looming_time4",
state_name="Male_{}".format(mouse_state)) # Male_Wakefulness
file_list_1 = []
# for item in a['Video_name'][0:len(a['Video_name'])]:
for item in a['Video_name'][0:5]:
item = item.replace("-camera-0", "")
file_list1 = search_csv(
path=r"D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new\BeAOutputs\csv_file_output_new",
name="{}_Feature_Space".format(item))
file_list_1.append(file_list1)
file_list_1 = list(np.ravel(file_list_1))
b = read_csv(path=r'D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new',
name="video_info.xlsx", column="looming_time4",
state_name="Female_{}".format(mouse_state)) # Female_Wakefulness
file_list_2 = []
# for item in b['Video_name'][0:len(a['Video_name'])]:
for item in b['Video_name'][0:6]:
item = item.replace("-camera-0", "")
file_list1 = search_csv(
path=r"D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Arousal_result_final\looming_new\BeAOutputs\csv_file_output_new",
name="{}_Feature_Space".format(item))
file_list_2.append(file_list1)
file_list_2 = list(np.ravel(file_list_2))
"""
单只老鼠所有结果
"""
# Male_data = []
# single_data = data_combine(file_list_1[4], 0, 10*1800)
# Male_data.append(single_data)
#
# # fig = plt.figure(figsize=(12.5, 1), dpi=300)
# fig = plt.figure(figsize=(5, 1), dpi=300)
# ax = fig.add_subplot(111)
# for j in range(len(Male_data)):
# for i in range(len(Male_data[0])):
# plt.broken_barh(Male_data[j][i], (j, 0.8), facecolors=color_list[i])
# # plt.broken_barh(Female_data[j][i], (j + 5, 0.8), facecolors=color_list[i])
#
# plt.tight_layout()
# plt.axis('off')
#
# plt.savefig(r'D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Analysis\state_space\M_15/'
# 'Wake_all_v23.tiff', transparent=True, dpi=300)
# plt.close()
"""
单只老鼠looming后时刻的数据
"""
mouse_state = "Wakefulness"
# time = 1
for time in range(1, 5):
mouse_num = 4
Male_data = []
single_data = data_combine(file_list_1[mouse_num], a['looming_time{}'.format(time)][mouse_num] - 5 * 30,
a['looming_time{}'.format(time)][mouse_num] + 115 * 30)
Male_data.append(single_data)
fig = plt.figure(figsize=(5, 1), dpi=300)
ax = fig.add_subplot(111)
for j in range(len(Male_data)):
for i in range(len(Male_data[0])):
plt.broken_barh(Male_data[j][i], (j, 0.8), facecolors=color_list[i])
# plt.broken_barh(Female_data[j][i], (j + 5, 0.8), facecolors=color_list[i])
plt.tight_layout()
plt.axis('off')
plt.savefig(r'D:\3D_behavior\Arousal_behavior\Arousal_analysis_new\Analysis\state_space\M_15/'
'{}_looming_time_{}_v23.tiff'.format(mouse_state, time), transparent=True, dpi=300)
plt.close()