-
Notifications
You must be signed in to change notification settings - Fork 1
/
extractBehaviorCameraTiming.py
95 lines (86 loc) · 6.31 KB
/
extractBehaviorCameraTiming.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
from oauth2client import tools
tools.argparser.add_argument("-m","--mouse", help="specify name of the mouse", required=False)
tools.argparser.add_argument("-d","--date", help="specify name of the mouse", required=False)
args = tools.argparser.parse_args()
import tools.extractSaveData as extractSaveData
import tools.dataAnalysis as dataAnalysis
import tools.openCVImageProcessingTools as openCVImageProcessingTools
import pdb
import numpy as np
import sys
mouseD = '210126_f70'
#mouseD = '201017_m99' #'200801_m58' # id of the mouse to analyze
#mouseD = '190108_m24'
expDateD = 'some' # specific date e.g. '180214', 'some' for manual selection or 'all'
recordings='some' # 'all or 'some'
# further analaysis parameter
assumePerfectRecording = False # that means a recording without any flash-back - or double frames
startRecording = 4 # each session/day per animal is composed of 5 recordings, this index allows chose with which recording to start, default is 0
endRecording = None # in case only specify recording will be analyzed, otherwise set to None
DetermineAgainLEDcoordinates = True # whether or not to determine LED coordinates even though they exist already for current or previous recording
DetermineAgainErronousFrames = False # whether or not to determine errnonous frames even though the are already exist for current recording
recordingWithTail = False
# in case mouse, and date were specified as input arguments
if args.mouse == None:
mouse = mouseD
else:
mouse = args.mouse
if args.date == None:
try:
expDate = expDateD
except :
expDate = 'all'
else:
expDate = args.date
#print mouse, expDate
#sys.exit(0) #pdb.set_trace()
eSD = extractSaveData.extractSaveData(mouse) # find data folder of specific mouse, create data folder, and hdf5 handle
#pdb.set_trace()
#(foldersRecordings,dataFolder) = eSD.getRecordingsList(mouse,expDate=expDate,recordings=recordings) # get recordings for specific mouse and date
(foldersRecordings,dataFolder) = eSD.getRecordingsList(expDate=expDate,recordings=recordings) # get recordings for specific mouse and date
openCVtools = openCVImageProcessingTools.openCVImageProcessingTools(eSD.analysisLocation,eSD.figureLocation,eSD.f,showI=True)
#print(len(foldersRecordings))
#pdb.set_trace()
# loop over all folders, mostly days but sometimes there were two recording sessions per day
for f in range(len(foldersRecordings)):
# loop over all recordings in that folder
for r in range((0 if startRecording is None else startRecording),(len(foldersRecordings[f][2]) if endRecording is None else endRecording)):
#pdb.set_trace()
(existenceFrames,fileHandleFrames) = eSD.checkIfDeviceWasRecorded(foldersRecordings[f][0],foldersRecordings[f][1],foldersRecordings[f][2][r],'CameraGigEBehavior')
(existenceFTimes,fileHandleFTimes) = eSD.checkIfDeviceWasRecorded(foldersRecordings[f][0],foldersRecordings[f][1],foldersRecordings[f][2][r],'frameTimes')
(existenceLEDControl, fileHandleLED) = eSD.checkIfDeviceWasRecorded(foldersRecordings[f][0], foldersRecordings[f][1], foldersRecordings[f][2][r], 'PreAmpInput')
(currentCoodinatesExist,SavedLEDcoordinates) = eSD.checkForLEDPositionCoordinates(foldersRecordings[f][0], foldersRecordings[f][1], foldersRecordings[f][2], r)
(erroneousFramesExist,idxToExclude,canBeUsed) = eSD.checkForErroneousFramesIdx(foldersRecordings[f][0], foldersRecordings[f][1], foldersRecordings[f][2], r,determineAgain=DetermineAgainErronousFrames)
# if camera was recorded
if existenceFrames:
#print('exists',foldersRecordings[f][0],foldersRecordings[f][2][r])
(frames,softFrameTimes,imageMetaInfo) = eSD.readRawData(foldersRecordings[f][0],foldersRecordings[f][1],foldersRecordings[f][2][r],'CameraGigEBehavior',fileHandleFrames)
# determine the
if (SavedLEDcoordinates is None) or DetermineAgainLEDcoordinates :
LEDcoordinates = openCVtools.findLEDNumberArea(frames,coordinates=SavedLEDcoordinates,currentCoordExist=currentCoodinatesExist,determineAgain=True,verbose=False)
eSD.saveLEDPositionCoordinates([foldersRecordings[f][0], foldersRecordings[f][2][r], 'LEDinVideo'],LEDcoordinates)
else:
LEDcoordinates = SavedLEDcoordinates
eSD.saveLEDPositionCoordinates([foldersRecordings[f][0], foldersRecordings[f][2][r], 'LEDinVideo'], LEDcoordinates)
LEDtraces = openCVtools.extractLEDtraces(frames,LEDcoordinates,verbose=False)
#pdb.set_trace()
if existenceFTimes:
(exposureDAQArray,exposureDAQArrayTimes) = eSD.readRawData(foldersRecordings[f][0],foldersRecordings[f][1],foldersRecordings[f][2][r],'frameTimes',fileHandleFTimes)
if existenceLEDControl:
(ledDAQControlArray, ledDAQControlArrayTimes) = eSD.readRawData(foldersRecordings[f][0], foldersRecordings[f][1], foldersRecordings[f][2][r], 'PreAmpInput', fileHandleLED)
# save data
#idxToExclude = np.array([], dtype=np.int64)
if assumePerfectRecording:
idxToExclude = np.array([], dtype=np.int64)
canBeUsed = True
else:
if (not erroneousFramesExist) and canBeUsed:
(idxToExclude,canBeUsed) = dataAnalysis.determineErroneousFrames(frames)
eSD.saveErroneousFramesIdx([foldersRecordings[f][0], foldersRecordings[f][2][r], 'erroneousFrames'],idxToExclude,canBeUsed=canBeUsed)
#pdb.set_trace()
if existenceFrames and existenceFTimes and existenceLEDControl and canBeUsed:
#(idxIllumFinal, frameTimes, frameStartStopIdx, videoIdx, frameSummary)
(idxTimePoints,startEndExposureTime,startEndExposurepIdx,videoIdx,frameSummary) = dataAnalysis.determineFrameTimesBasedOnLED([LEDtraces,LEDcoordinates,frames,softFrameTimes,imageMetaInfo,idxToExclude],[exposureDAQArray,exposureDAQArrayTimes],[ledDAQControlArray, ledDAQControlArrayTimes],eSD.recordingMachine,verbose=True,tail=recordingWithTail)
#framesDuringRecording = frames[recordedFramesIdx]
eSD.saveBehaviorVideoTimeData([foldersRecordings[f][0], foldersRecordings[f][2][r], 'behaviorVideo'],idxTimePoints,startEndExposureTime,startEndExposurepIdx,videoIdx,frameSummary, imageMetaInfo)
#eSD.saveBehaviorVideo(mouse, foldersRecordings[f][0], foldersRecordings[f][2][r], framesDuringRecording, expStartTime, expEndTime, imageMetaInfo)