-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignframes.py
More file actions
152 lines (125 loc) · 6.08 KB
/
alignframes.py
File metadata and controls
152 lines (125 loc) · 6.08 KB
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
import pandas as pd
import numpy as np
import os
from datetime import datetime, timedelta
from ellipse import LsqEllipse
from matplotlib import pyplot as plt
import circle_fit as cf
from math import pi
def matchvid2data(animal,date,starttime,viddir):
try:
timestampfile = f'{animal}_{date}_timestamp.dat'
timestamp_df = pd.read_csv(os.path.join(viddir, timestampfile), delimiter='\t')
frametime = [(starttime+timedelta(milliseconds=framereltime)).time() for framereltime in timestamp_df['sysClock']]
except FileNotFoundError:
timestampfile = f'{animal}_{date}_timestamp.csv'
timestamp_df = pd.read_csv(os.path.join(viddir, timestampfile))
frametime = [(starttime+timedelta(milliseconds=framereltime)).time() for framereltime in timestamp_df['Time Stamp (ms)']]
timestamp_df['frametime'] = frametime
return timestamp_df
def alignframes():
pass
def fit_elipse(point_array):
# # lsq ellipse method
# lsqe = LsqEllipse()
# lsqe.fit(point_array)
# center, width, height, phi = lsqe.as_parameters()
# return center, width, height, phi
# circle fit method
# xc, yc, r, _ = cf.least_squares_circle(point_array)
xc, yc, r1, r2 = cf.hyper_fit(point_array)
return (xc,yc), r1, r2
def findfiles(startdir,filetype,datadict,animals=None,dates=None):
for root, folder, files in os.walk(startdir):
for file in files:
if file.find(filetype) != -1:
splitstr = file.split('_')
_animal = splitstr[0]
_date = splitstr[2]
if dates is None:
if _date not in datadict[_animal].keys():
datadict[_animal][_date] = dict()
datadict[_animal][_date][f'{filetype}file'] = os.path.join(root,file)
elif dates is not None and animals is not None:
if _date in dates and _animal in animals:
if _date not in datadict[_animal].keys():
datadict[_animal][_date] = dict()
datadict[_animal][_date][f'{filetype}file'] = os.path.join(root, file)
def main():
toprocess_dict = dict()
animals = [
'DO27',
'DO28',
'DO29',
'DO37',
# 'DO42',
'DO43',
# 'ES01',
# 'ES02'
# 'ES03'
]
for animal in animals:
toprocess_dict[animal] = dict()
# find h5
findfiles(r'W:\mouse_pupillometry\analysed','h5',toprocess_dict,animals,['210427','210428','210430','211022','211026','211028']) # ['211022','211026','211028','211029']['211105']
# find camstarts
findfiles(r'C:\bonsai\data','camstart',toprocess_dict,animals,['210427','210428','210430','211022','211026','211028']) # ['211022','211026','211028','211029'] ['211105']
list_h5pupils = []
timestampdir = r'W:\mouse_pupillometry\cropsessionvids'
for animal in toprocess_dict.keys():
for date in toprocess_dict[animal].keys():
if 'h5file' in toprocess_dict[animal][date].keys():
camstart = pd.read_csv(toprocess_dict[animal][date]['camstartfile'],header=None)[0][0]
h5pupildf = pd.read_hdf(toprocess_dict[animal][date]['h5file'])
camstart_dt = datetime.strptime(camstart[:-1], '%H:%M:%S.%f')
frame_timestamps = matchvid2data(animal,date,camstart_dt,timestampdir)
h5pupildf['frametime'] = frame_timestamps['frametime']
print('videofile read')
toprocess_dict[animal][date]['pupildf'] = h5pupildf
scorer = 'DLC_resnet50_pupildiamterApr28shuffle1_200000'
for animal in toprocess_dict.keys():
for date in toprocess_dict[animal].keys():
if 'pupildf' in toprocess_dict[animal][date].keys():
df = toprocess_dict[animal][date]['pupildf']
bodypoints = np.array(df)
diams_ = []
centers_ = []
for row in bodypoints:
reshaped = row[0:24].reshape([8,3])
goodpoints = reshaped[reshaped[:,2]>.3].astype(float)
if goodpoints.shape[0] < 3:
diams_.append(0.0)
centers_.append([0,0])
else:
frame_elipse = fit_elipse(goodpoints[:,[0,1]])
diams_.append((frame_elipse[1]*frame_elipse[2])*pi) # area
# diams_.append(np.array([frame_elipse[1],frame_elipse[2]]).max()) # ew
# diams_.append(frame_elipse[1])
centers_.append(frame_elipse[0])
# eyeEW_arr = np.array(df[scorer,'eyeW']-df[scorer,'eyeE'])
# diams = np.linalg.norm(eyeEW_arr,axis=1)
# toprocess_dict[animal][date]['pupildf']['diameter'] = diams
toprocess_dict[animal][date]['pupildf']['diameter'] = diams_
toprocess_dict[animal][date]['pupildf']['xcyc'] = centers_
df2save = toprocess_dict[animal][date]['pupildf']
h5filename = f'{animal}_{date}_pupildata.h5'
# df2save.to_hdf(os.path.join(r'W:\mouse_pupillometry\analysed',h5filename))
df2save.to_csv(os.path.join(r'W:\mouse_pupillometry\analysed',h5filename.replace('.h5','_hypcffit.csv')))
# bodyparts = [
# 'eyeN',
# 'eyeNE',
# 'eyeE',
# 'eyeSE',
# 'eyeS',
# 'eyeSW',
# 'eyeW',
# 'eyeNW'
# ]
# colors = cm.rainbow(np.linspace(0, 1, len(bodyparts)))
# for i, eyepart in enumerate(bodyparts):
# plt.hist(df[scorer,eyepart]['likelihood'],alpha=.25,label=eyepart,edgecolor='None',color=colors[i])
# for i, eyepart in enumerate(bodyparts):
# plt.hist(df[scorer,eyepart]['likelihood'],alpha=.25,label=eyepart,ls='dashed', lw=3, facecolor="None")
# plt.legend()
if __name__ == '__main__':
main()