def loadDetectionMap(connection, animal, start=None, end=None): chrono = Chronometer("Correct detection integrity: Load detection map") print("processing animal ID: {}".format(animal)) result = {} cursor = connection.cursor() query = "SELECT FRAMENUMBER FROM DETECTION WHERE ANIMALID={}".format( animal) if (start != None): query += " AND FRAMENUMBER>={}".format(start) if (end != None): query += " AND FRAMENUMBER<={}".format(end) print(query) cursor.execute(query) rows = cursor.fetchall() cursor.close() for row in rows: frameNumber = row[0] result[frameNumber] = True print(" detections loaded in {} seconds.".format(chrono.getTimeInS())) return result
def getLengthDistanceWithTimeLine(self, timeLineCandidate): ''' provides correlation of current event considering overlap with candidate event. ''' foundEventList = [] print("correlation started") chrono = Chronometer("getLengthDistanceWithTimeLine " + timeLineCandidate.eventName) maxT = self.getMaxT() dico = self.getDictionnary(0, maxT) dicoCandidate = timeLineCandidate.getDictionnary(0, maxT) mergedDico = {} for k in dico.keys(): if (k in dicoCandidate): mergedDico[k] = True mergedTimeLine = EventTimeLine(None, eventName="Merged", loadEvent=False) mergedTimeLine.reBuildWithDictionnary(mergedDico) nbEvent = len(self.getEventList()) #nbMatch = len( newEvent.getEventList() ) relativityDico = { } # relativity provide an histogram of the relative location of the even compared to the candidate. 0 is beginning, 1 is end for i in range(100): # zero fill relativityDico[i] = 0 nbMatch = 0 for event in self.eventList: for eventCandidate in mergedTimeLine.eventList: if event.overlapEvent(eventCandidate): nbMatch += 1 meanEventT = event.startFrame + (event.endFrame - event.startFrame) / 2 foundEvent = timeLineCandidate.getEventAt(meanEventT) if (foundEvent != None): foundEventList.append(foundEvent) if (foundEvent.duration() > event.duration() * 2): try: a = (meanEventT - foundEvent.startFrame ) / foundEvent.duration() a = int(a * 100) if (a in relativityDico): relativityDico[a] += 1 else: relativityDico[a] = 1 except: pass break print("NB match with " + timeLineCandidate.eventName + " " + str(nbMatch) + " / " + str(nbEvent) + " time to compute: " + str(chrono.getTimeInMS())) v = 0 if nbMatch != 0: v = nbMatch / nbEvent return [v, foundEventList, relativityDico]
def __init__(self, conn, eventName, idA=None, idB=None, idC=None, idD=None, loadEvent=True, minFrame=None, maxFrame=None, inverseEvent=False, loadEventWithoutOverlapCheck=False): ''' load events where t>=minFrame and t<=maxFrame if applicable inverseEvent: inverse all timeline (used to make stop becomes move for instance) ''' #check id at 0 to transform them as None ( id will not be considered in query ) (added for USV support). if (idA == 0): idA = None if (idB == 0): idB = None if (idC == 0): idC = None if (idD == 0): idD = None #store parameters self.idA = idA self.idB = idB self.idC = idC self.idD = idD self.eventName = str(eventName) self.eventNameWithId = "{} idA:{} idB:{} idC:{} idD:{}".format( eventName, idA, idB, idC, idD) # build events self.eventList = [] if (loadEvent == False): print("Event " + str(eventName) + " created. eventNameWithId = " + str(self.eventNameWithId) + " loadEvent: False") return chrono = Chronometer("Load event " + str(self.eventName)) c = conn.cursor() query = "SELECT * FROM EVENT WHERE NAME='{0}'".format(self.eventName) if (idA != None): query += " AND IDANIMALA={0}".format(idA) if (idB != None): query += " AND IDANIMALB={0}".format(idB) if (idC != None): query += " AND IDANIMALC={0}".format(idC) if (idD != None): query += " AND IDANIMALD={0}".format(idD) ''' if ( minFrame != None ): query += " AND STARTFRAME>={0}".format( minFrame ) if ( maxFrame != None ): query += " AND ENDFRAME<={0}".format( maxFrame ) ''' if (minFrame != None): query += " AND ENDFRAME>={0}".format(minFrame) if (maxFrame != None): query += " AND STARTFRAME<={0}".format(maxFrame) ''' print( query ) ''' c.execute(query) all_rows = c.fetchall() if loadEventWithoutOverlapCheck == True: if inverseEvent == True: print( "Warning: inverseEvent option not compatible in loadEventWithoutOverlapCheck" ) for row in all_rows: start = row[3] end = row[4] if (minFrame != None): if (start < minFrame or end < minFrame): continue if (maxFrame != None): if (start > maxFrame or end > maxFrame): continue self.eventList.append(Event(start, end)) else: eventBool = {} for row in all_rows: start = row[3] end = row[4] for t in range(start, end + 1): if (minFrame != None): if (t < minFrame): continue if (maxFrame != None): if (t > maxFrame): continue eventBool[t] = True if (inverseEvent == True): if (minFrame == None): print("To inverse event, need a minFrame") return if (maxFrame == None): print("To inverse event, need a maxFrame") return for t in range(minFrame, maxFrame + 1): if (t in eventBool): eventBool.pop(t) else: eventBool[t] = True self.reBuildWithDictionnary(eventBool) #keyList = sorted(eventBool.keys()) #start = -1 #for key in keyList: # if ( start == -1 ): # start = key # if ( eventBool.get( key+1 ) == None ): # self.eventList.append( Event( start, key ) ) # start = -1 print(eventName, " Id(", idA, ",", idB, ",", idC, ",", idD, ") Min/maxFrame: (", minFrame, "/", maxFrame, ") Loaded (", len(self.eventList), " records loaded in ", chrono.getTimeInS(), "S )")
plt.show() ''' # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Wall Jump", tmin=tmin, tmax=tmax) print("Rebuild event finished.") if __name__ == '__main__': files = getFilesToProcess() chronoFullBatch = Chronometer("Full batch") if (files != None): for file in files: print("Processing file", file) connection = sqlite3.connect(file) animalPool = AnimalPool() animalPool.loadAnimals(connection) ''' currentMinT = 335794-30 currentMaxT = 335794+30 ''' currentMinT = 0