def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): pool = AnimalPool() pool.loadAnimals(connection) #pool.loadDetection( start = tmin, end = tmax ) for animal in range(1, 5): eventName = "Detection" print(eventName) detectionTimeLine = EventTimeLine(None, eventName, animal, None, None, None, loadEvent=False) result = loadDetectionMap(connection, animal, tmin, tmax) #animal = pool.animalDictionnary[animal] #animal.loadDetection() detectionTimeLine.reBuildWithDictionnary(result) detectionTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Detection", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def correct(connection, tmin=None, tmax=None): pool = AnimalPool() pool.loadAnimals(connection) #pool.loadDetection( start = tmin, end = tmax ) ''' get the number of expected animals if there is not all detections expected, switch all to anonymous ''' validDetectionTimeLine = EventTimeLine(None, "IDs integrity ok", None, None, None, None, loadEvent=False) validDetectionTimeLineDictionnary = {} detectionTimeLine = {} for idAnimal in pool.getAnimalDictionnary(): detectionTimeLine[idAnimal] = loadDetectionMap(connection, idAnimal, tmin, tmax) for t in range(tmin, tmax + 1): valid = True for idAnimal in detectionTimeLine.keys(): if not (t in detectionTimeLine[idAnimal]): valid = False if (valid): validDetectionTimeLineDictionnary[t] = True ''' rebuild detection set ''' cursor = connection.cursor() for idAnimal in detectionTimeLine.keys(): for t in range(tmin, tmax + 1): if (t in detectionTimeLine[idAnimal]): if not (t in validDetectionTimeLineDictionnary): query = "UPDATE `DETECTION` SET `ANIMALID`=NULL WHERE `FRAMENUMBER`='{}';".format( t) #print ( query ) cursor.execute(query) connection.commit() cursor.close() validDetectionTimeLine.reBuildWithDictionnary( validDetectionTimeLineDictionnary) validDetectionTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Correct detection integrity", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None ): pool = AnimalPool( ) pool.loadAnimals( connection ) #pool.loadDetection( start = tmin, end = tmax ) rear = {} for animal in range( 1 , 5 ): rear[animal] = EventTimeLineCached( connection, file, "Rear isolated", animal, minFrame=tmin, maxFrame=tmax ) centerWindow = {} for animal in range( 1 , 5 ): centerWindow[animal] = EventTimeLineCached(connection, file, "Center Zone", animal, minFrame=tmin, maxFrame=tmax ) periphery = {} for animal in range( 1 , 5 ): periphery[animal] = EventTimeLineCached(connection, file, "Periphery Zone", animal, minFrame=tmin, maxFrame=tmax ) for animal in range( 1 , 5 ): eventName1 = "Rear in centerWindow" print ( eventName1 ) eventName2 = "Rear at periphery" print ( eventName2 ) rearCenterTimeLine = EventTimeLine( None, eventName1 , animal , None , None , None , loadEvent=False ) rearPeripheryTimeLine = EventTimeLine( None, eventName2 , animal , None , None , None , loadEvent=False ) resultCenter={} resultPeriphery={} dicRear = rear[ animal ].getDictionnary() dicCenter = centerWindow[ animal ].getDictionnary() dicPeriphery = periphery[ animal ].getDictionnary() for t in dicRear.keys(): if ( t in dicCenter ): resultCenter[t] = True if (t in dicPeriphery ): resultPeriphery[t] = True rearCenterTimeLine.reBuildWithDictionnary( resultCenter ) rearPeripheryTimeLine.reBuildWithDictionnary( resultPeriphery ) rearCenterTimeLine.endRebuildEventTimeLine(connection) rearPeripheryTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Rear in centerWindow" , tmin=tmin, tmax=tmax ) t.addLog( "Build Event Rear at periphery" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): ''' use the pool provided or create it''' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax) ''' deleteEventTimeLineInBase(connection, "Oral-oral Contact" ) ''' ''' contactDico = {} approachDico = {} for idAnimalA in range( 1 , pool.getNbAnimals()+1 ): for idAnimalB in range( 1 , pool.getNbAnimals()+1 ): if ( idAnimalA == idAnimalB ): continue contactDico[idAnimalA, idAnimalB] = EventTimeLine( connection, "Contact", idAnimalA, idAnimalB, minFrame=tmin, maxFrame=tmax ) approachDico[idAnimalA, idAnimalB] = EventTimeLine( connection, "Social approach", idAnimalA, idAnimalB, minFrame=tmin, maxFrame=tmax ) #fait une matrice de toutes les aproches à deux possibles ''' for idAnimalA in range(1, pool.getNbAnimals() + 1): for idAnimalB in range(1, pool.getNbAnimals() + 1): if (idAnimalA == idAnimalB): continue ''' MAX_DISTANCE_HEAD_HEAD_GENITAL_THRESHOLD ''' eventName = "Oral-oral Contact" print(eventName) result = {} animalA = pool.animalDictionnary.get(idAnimalA) animalB = pool.animalDictionnary.get(idAnimalB) OralOralTimeLine = EventTimeLine(None, eventName, idAnimalA, idAnimalB, loadEvent=False) for t in animalA.detectionDictionnary.keys(): if (t in animalB.detectionDictionnary): detA = animalA.detectionDictionnary[t] detB = animalB.detectionDictionnary[t] if distHeadHead( detA, detB) < MAX_DISTANCE_HEAD_HEAD_GENITAL_THRESHOLD: result[t] = True OralOralTimeLine.reBuildWithDictionnary(result) OralOralTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Oral Oral Contact", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None ): ''' use the pool provided or create it''' if ( pool == None ): pool = AnimalPool( ) pool.loadAnimals( connection ) pool.loadDetection( start = tmin, end = tmax ) ''' Event Water Zone - the animal is in the zone around the water source ''' for idAnimalA in pool.animalDictionnary.keys(): print(pool.animalDictionnary[idAnimalA]) eventName = "Water Zone" print ( "A is in the zone around the water source") print ( eventName ) waterZoneTimeLine = EventTimeLine( None, eventName , idAnimalA , None , None , None , loadEvent=False ) result={} animalA = pool.animalDictionnary[idAnimalA] #print ( animalA ) dicA = animalA.detectionDictionnary for t in dicA.keys(): if (dicA[t].getDistanceToPoint(xPoint = 398, yPoint = 353) == None): continue if (dicA[t].getDistanceToPoint(xPoint = 398, yPoint = 353) <= MAX_DISTANCE_TO_POINT): result[t] = True waterZoneTimeLine.reBuildWithDictionnary( result ) waterZoneTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Water Point" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent( connection, tmin, tmax , pool = None ): ''' Event Floor sniffing: - the animal is sniffing the floor ''' pool = AnimalPool( ) pool.loadAnimals( connection ) pool.loadDetection( start = tmin, end = tmax ) for animal in pool.animalDictionnary.keys(): print(pool.animalDictionnary[animal]) eventName = "Floor sniffing" print ( "A sniffs the floor") print ( eventName ) trainTimeLine = EventTimeLine( None, eventName , animal , None , None , None , loadEvent=False ) result={} animalA = pool.animalDictionnary[animal] #print ( animalA ) dicA = animalA.detectionDictionnary for t in dicA.keys(): if (animalA.getBodySlope(t) == None): continue if (animalA.getBodySlope(t) >= -25 and animalA.getBodySlope(t) <= -15): #print("floor sniffing") result[t] = True trainTimeLine.reBuildWithDictionnary( result ) trainTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Floor sniffing" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None, showGraph=False): ''' use the pool provided or create it''' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) #pool.loadDetection( start = tmin, end = tmax ) for idAnimalA in pool.animalDictionnary: threshold = 0.5 animal = pool.animalDictionnary[idAnimalA] print("Computing huddling for animal ", animal) huddlingTimeLine = EventTimeLine(connection, "Huddling", idAnimalA, minFrame=tmin, maxFrame=tmax, loadEvent=False) result = {} for t in range(tmin, tmax + 1): if t % 10000 == 0: print("current t", t) mask = animal.getBinaryDetectionMask(t) if mask == None: continue roundness = mask.getRoundness() if roundness == None: continue print(idAnimalA, t, roundness) #if roundness > 1-threshold and roundness < 1+threshold: if roundness < 1.85: # and roundness > 1: result[t] = True huddlingTimeLine.reBuildWithDictionnary(result) huddlingTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Huddling", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax, lightLoad=True) ''' load the contact timeline for each pair of animals ''' anogenitalSniffTimeLine = {} passiveAnogenitalSniffTimeLine = {} for animal in range(1, pool.getNbAnimals() + 1): for idAnimalB in range(1, pool.getNbAnimals() + 1): if (animal == idAnimalB): continue anogenitalSniffTimeLine[animal, idAnimalB] = EventTimeLineCached( connection, file, "Oral-genital Contact", animal, idAnimalB, minFrame=tmin, maxFrame=tmax) # create an empty timeline for other contacts passiveAnogenitalSniffTimeLine[idAnimalB, animal] = EventTimeLine( None, 'Passive oral-genital Contact', idAnimalB, animal, loadEvent=False) anogenitalSniffDico = {} for animal in range(1, pool.getNbAnimals() + 1): for idAnimalB in range(1, pool.getNbAnimals() + 1): if (animal == idAnimalB): continue anogenitalSniffDico[animal, idAnimalB] = anogenitalSniffTimeLine[ animal, idAnimalB].getDictionary(minFrame=tmin, maxFrame=tmax) passiveAnogenitalSniffTimeLine[idAnimalB, animal].reBuildWithDictionnary( anogenitalSniffDico[animal, idAnimalB]) passiveAnogenitalSniffTimeLine[ idAnimalB, animal].endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Passive oral-genital Contact", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None ): ''' use the pool provided or create it''' if ( pool == None ): pool = AnimalPool( ) pool.loadAnimals( connection ) pool.loadDetection( start = tmin, end = tmax ) for animal in range( 1 , pool.getNbAnimals()+1 ): for idAnimalB in range( 1 , pool.getNbAnimals()+1 ): if( animal == idAnimalB ): continue eventName = "Side by side Contact, opposite way" print ( eventName ) result ={} animalA = pool.animalDictionnary.get( animal ) animalB = pool.animalDictionnary.get( idAnimalB ) SideBySideTimeLine = EventTimeLine( None, eventName , animal , idAnimalB , loadEvent=False ) for t in animalA.detectionDictionnary.keys() : if ( t in animalB.detectionDictionnary ): detA = animalA.detectionDictionnary[t] detB = animalB.detectionDictionnary[t] if ( isSideBySide( detA, detB ) == True ): result[t] = True SideBySideTimeLine.reBuildWithDictionnary( result ) SideBySideTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Side by side opposite" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): ''' use the pool provided or create it''' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax) for animal in range(1, pool.getNbAnimals() + 1): for idAnimalB in range(1, pool.getNbAnimals() + 1): if (animal == idAnimalB): continue ''' MAX_DISTANCE_HEAD_HEAD_GENITAL_THRESHOLD ''' eventName = "Oral-genital Contact" print(eventName) result = {} animalA = pool.animalDictionnary.get(animal) animalB = pool.animalDictionnary.get(idAnimalB) OralGenitalTimeLine = EventTimeLine(None, eventName, animal, idAnimalB, loadEvent=False) for t in animalA.detectionDictionnary.keys(): if (t in animalB.detectionDictionnary): detA = animalA.detectionDictionnary[t] detB = animalB.detectionDictionnary[t] if distHeadBack( detA, detB) < MAX_DISTANCE_HEAD_HEAD_GENITAL_THRESHOLD: result[t] = True OralGenitalTimeLine.reBuildWithDictionnary(result) OralGenitalTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Oral Genital Contact", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): eventUndetected = 'Undetected exclusive' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) ''' load the existing detection timeline for animal ''' detectionTimeLine = {} dicoDetection = {} undetectedTimeLine = {} for animal in range(1, pool.getNbAnimals() + 1): detectionTimeLine[animal] = EventTimeLineCached(connection, file, 'Detection', animal, minFrame=tmin, maxFrame=tmax) dicoDetection[animal] = detectionTimeLine[animal].getDictionary( minFrame=tmin, maxFrame=tmax) undetectedTimeLine[animal] = EventTimeLine(None, eventUndetected, animal, loadEvent=False) '''select the frames where the animals are not detected''' undetectedDico = {} for animal in range(1, pool.getNbAnimals() + 1): undetectedDico[animal] = {} for t in range(tmin, tmax + 1): if t not in dicoDetection[animal]: undetectedDico[animal][t] = True ##################################################### #reduild all events based on dictionary for animal in range(1, pool.getNbAnimals() + 1): undetectedTimeLine[animal].reBuildWithDictionnary( undetectedDico[animal]) undetectedTimeLine[animal].endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Exclusive undetected events", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None, showGraph=False): ''' use the pool provided or create it''' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax) ''' Animal A is stopped (built-in event): Move social: animal A is stopped and in contact with any other animal. Move isolated: animal A is stopped and not in contact with any other animal. ''' for animal in pool.animalDictionnary: animal = pool.animalDictionnary[animal] SAPTimeLine = EventTimeLine(connection, "SAP", animal, minFrame=tmin, maxFrame=tmax, loadEvent=False) #f = animal.getCountFramesSpecZone( start , start+oneMinute*30 , xa=143, ya=190, xb=270, yb=317 ) result = animal.getSapDictionnary(tmin, tmax) SAPTimeLine.reBuildWithDictionnary(result) SAPTimeLine.endRebuildEventTimeLine(connection) #animal.clearDetection() # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event SAP", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def check(connection, tmin=None, tmax=None): pool = AnimalPool() pool.loadAnimals(connection) ''' get the number of expected animals ''' nbAnimals = pool.getNbAnimals() print("nb animals: ", nbAnimals) for animal in pool.getAnimalList(): if (animal.name == None): print("!!!! None animal detected with lmtanalysis id: ", animal.baseId) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Correct wrong animal", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def buildDataBaseIndex( connection , force=False): print( "Creating lmtanalysis indexes...") c = connection.cursor() ''' if ( force == False ): if( getNumberOfIndexOfDatabase( connection ) > 0 ): print( "Index already exists (maybe not all)... no re-indexing. Use buildDataBaseIndex force parameter to force index build.") return ''' executeLog( c , "CREATE INDEX `animalIndex` ON `ANIMAL` (`ID` );" ) executeLog( c , "CREATE INDEX `detectionIndex` ON `DETECTION` (`ID` ASC,`FRAMENUMBER` ASC);" ) executeLog( c , "CREATE INDEX `detetIdIndex` ON `DETECTION` (`ID` ASC);" ) executeLog( c , "CREATE INDEX `detframenumberIndex` ON `DETECTION` (`FRAMENUMBER` ASC);" ) executeLog( c , "CREATE INDEX `eventEndFrameIndex` ON `EVENT` (`ENDFRAME` ASC);" ) executeLog( c , "CREATE INDEX `eventIndex` ON `EVENT` (`ID` ASC,`STARTFRAME` ASC,`ENDFRAME` ASC);" ) executeLog( c , "CREATE INDEX `eventStartFrameIndex` ON `EVENT` (`STARTFRAME` ASC);" ) executeLog( c , "CREATE INDEX `eventstartendIndex` ON `EVENT` (`STARTFRAME` ASC,`ENDFRAME` ASC);" ) executeLog( c , "CREATE INDEX `indexeventidIndex` ON `EVENT` (`ID` ASC);" ) executeLog( c , "CREATE INDEX 'detectionFastLoadXYIndex' ON 'DETECTION' ('ANIMALID' ,'FRAMENUMBER' ASC,'MASS_X' ,'MASS_Y' );" ) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build DataBase Index" )
def process(file): print(file) chronoFullFile = Chronometer("File " + file) connection = sqlite3.connect(file) try: animalPool = AnimalPool() animalPool.loadAnimals(connection) animalPool.buildSensorData(file) ''' animalPool.createAutomaticDayNightEvent( ) animalPool.plotNight( False , saveFile = file+"_auto day night.pdf" ) animalPool.plotSensorData( sensor = "TEMPERATURE" , minValue = 10, saveFile = file+"_log_temperature.pdf", show = False ) animalPool.plotSensorData( sensor = "SOUND" , saveFile = file+"_log_sound level.pdf" , show = False ) animalPool.plotSensorData( sensor = "HUMIDITY" , minValue = 5 , saveFile = file+"_log_humidity.pdf" ,show = False ) animalPool.plotSensorData( sensor = "LIGHTVISIBLE" , minValue = 40 , saveFile = file+"_log_light visible.pdf", show = False ) animalPool.plotSensorData( sensor = "LIGHTVISIBLEANDIR" , minValue = 50 , saveFile = file+"_log_light visible and infra.pdf", show = False ) ''' except: exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) error = ''.join('!! ' + line for line in lines) t = TaskLogger(connection) t.addLog(error) print(error, file=sys.stderr) raise FileProcessException()
def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None ): pool = AnimalPool( ) pool.loadAnimals( connection ) #pool.loadDetection( start = tmin, end = tmax ) contact = {} for idAnimalA in range( 1 , 5 ): for idAnimalB in range( 1 , 5 ): if ( idAnimalA == idAnimalB ): continue contact[idAnimalA, idAnimalB] = EventTimeLineCached( connection, file, "Contact", idAnimalA, idAnimalB, minFrame=tmin, maxFrame=tmax ) #fait une matrice de tous les contacts à deux possibles for idAnimalA in range( 1 , 5 ): for idAnimalB in range( 1 , 5 ): if( idAnimalA == idAnimalB ): continue for idAnimalC in range( 1 , 5 ): if( idAnimalA == idAnimalC ): continue if( idAnimalB == idAnimalC ): continue for idAnimalD in range( 1 , 5 ): if( idAnimalA == idAnimalD ): continue if( idAnimalB == idAnimalD ): continue if( idAnimalC == idAnimalD ): continue eventName = "Group2" print ( eventName ) groupTimeLine = EventTimeLine( None, eventName , idAnimalA , idAnimalB , None , None , loadEvent=False ) result={} dicAB = contact[ idAnimalA , idAnimalB ].getDictionnary() dicAC = contact[ idAnimalA , idAnimalC ].getDictionnary() dicAD = contact[ idAnimalA , idAnimalD ].getDictionnary() dicBC = contact[ idAnimalB , idAnimalC ].getDictionnary() dicBD = contact[ idAnimalB , idAnimalD ].getDictionnary() for t in dicAB.keys(): if ( t in dicAC or t in dicAD or t in dicBC or t in dicBD ): continue else: result[t]=True groupTimeLine.reBuildWithDictionnary( result ) groupTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Group 2" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): ''' Animal A is stopped (built-in event): revert the event to have move events Move social: animal A is moving and in contact with another animal. Move isolated: animal A is moving and not in contact with any other animal. ''' pool = AnimalPool() pool.loadAnimals(connection) if len(pool.animalDictionnary.keys()) == 1: print('Only one animal in database.') '''if the animal has been tested alone, only the move isolated event will be computed.''' moveSourceTimeLine = {} animal = 1 ''' Load source stop timeLine and revert it to get the move timeline If the animal is not detected, this will result in a move. To avoid this we mask with the detection. ''' moveSourceTimeLine[animal] = EventTimeLine(connection, "Stop", animal, minFrame=tmin, maxFrame=tmax, inverseEvent=True) detectionTimeLine = EventTimeLine(connection, "Detection", animal, minFrame=tmin, maxFrame=tmax) moveSourceTimeLine[animal].keepOnlyEventCommonWithTimeLine( detectionTimeLine) # initialisation of a dic for isolated move moveIsolatedResult = {} ''' loop over eventlist''' for moveEvent in moveSourceTimeLine[animal].eventList: for t in range(moveEvent.startFrame, moveEvent.endFrame + 1): moveIsolatedResult[t] = True ''' save move isolated at the end of the complete process for animalA''' moveIsolatedResultTimeLine = EventTimeLine(None, "Move isolated", idA=animal, idB=None, idC=None, idD=None, loadEvent=False) moveIsolatedResultTimeLine.reBuildWithDictionnary(moveIsolatedResult) moveIsolatedResultTimeLine.endRebuildEventTimeLine(connection) else: print('More than one animal in database.') isInContactSourceDictionnary = {} moveSourceTimeLine = {} moveIsolatedTimeLine = {} for animal in pool.animalDictionnary.keys(): ''' Load source stop timeLine and revert it to get the move timeline If the animal is not detected, this will result in a move. To avoid this we mask with the detection. ''' moveSourceTimeLine[animal] = EventTimeLine(connection, "Stop", animal, minFrame=tmin, maxFrame=tmax, inverseEvent=True) detectionTimeLine = EventTimeLine(connection, "Detection", animal, minFrame=tmin, maxFrame=tmax) moveSourceTimeLine[animal].keepOnlyEventCommonWithTimeLine( detectionTimeLine) #by default let's say that all move are isolated; next we will extract frames which are in contact from this time line. moveIsolatedTimeLine[animal] = moveSourceTimeLine[animal] ''' load contact dictionary with another animal ''' for animalB in pool.animalDictionnary.keys(): if animal == animalB: print('Same identity') continue else: isInContactSourceDictionnary[( animal, animalB)] = EventTimeLineCached( connection, file, "Contact", animal, animalB, minFrame=tmin, maxFrame=tmax).getDictionnary() moveIsolatedDic = {} for animal in pool.animalDictionnary.keys(): #initialisation of a dic for isolated move moveIsolatedDic[animal] = moveIsolatedTimeLine[ animal].getDictionary() for animalB in pool.animalDictionnary.keys(): # initialisation of a dic for move in contact for each individual of the cage framesToRemoveFromMoveIsolatedTimeLine = [] moveSocialResult = {} if animal == animalB: print('Same identity') continue else: ''' loop over eventlist''' for moveEvent in moveIsolatedTimeLine[animal].eventList: ''' for each event we seek in t and search a match in isInContactDictionnary between animal and animalB ''' for t in range(moveEvent.startFrame, moveEvent.endFrame + 1): if t in isInContactSourceDictionnary[(animal, animalB)]: moveSocialResult[t] = True framesToRemoveFromMoveIsolatedTimeLine.append( t) ''' save move social at the end of the search for each animal''' # clean the dictionary of the move and stop events from frames that are overlapping with exclusive contacts for t in framesToRemoveFromMoveIsolatedTimeLine: moveIsolatedDic[animal].pop(t, None) moveSocialResultTimeLine = EventTimeLine(None, "Move in contact", idA=animal, idB=animalB, idC=None, idD=None, loadEvent=False) moveSocialResultTimeLine.reBuildWithDictionnary( moveSocialResult) moveSocialResultTimeLine.endRebuildEventTimeLine( connection) ''' save move isolated at the end of the complete process for animalA''' moveIsolatedResultTimeLine = EventTimeLine(None, "Move isolated", idA=animal, idB=None, idC=None, idD=None, loadEvent=False) moveIsolatedResultTimeLine.reBuildWithDictionnary( moveIsolatedDic[animal]) moveIsolatedResultTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Move", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent( connection, tmin=None, tmax=None ): ''' Event FollowZone: - the two animals are moving at a speed >5 cm/s (SPEED_THRESHOLD_LOW) - the angles between the two animals are less than 45° apart - the mass center of the follower is within a follow zone of one mean body length of width and two mean body lengths of length - either the animal B is in contact with another one (FollowZone Social) or the animal B is not in contact with any other animal (except A at the end of the follow event; FollowZone Isolated) ''' pool = AnimalPool( ) pool.loadAnimals( connection ) pool.loadDetection( start = tmin, end = tmax ) contact = {} for idAnimalB in pool.animalDictionnary.keys(): print(pool.animalDictionnary[idAnimalB]) meanSizeB = pool.animalDictionnary[idAnimalB].getMeanBodyLength( tmax = tmax ) for idAnimalA in pool.animalDictionnary.keys(): if( idAnimalB == idAnimalA ): continue contact[idAnimalA, idAnimalB] = EventTimeLine( connection, "Contact", idAnimalA, idAnimalB, minFrame=tmin, maxFrame=tmax ) #contact[idAnimalB] = EventTimeLine( connection, "Contact", idAnimalB ) for idAnimalB in range( 1 , 5 ): for idAnimalA in range( 1 , 5 ): if( idAnimalA == idAnimalB ): continue for idAnimalC in range( 1 , 5 ): if( idAnimalA == idAnimalC ): continue if( idAnimalB == idAnimalC ): continue for idAnimalD in range( 1 , 5 ): if( idAnimalA == idAnimalD ): continue if( idAnimalB == idAnimalD ): continue if( idAnimalC == idAnimalD ): continue eventName = "FollowZone Isolated" print ( "A follow B") print ( eventName ) followIsolatedTimeLine = EventTimeLine( None, eventName , idAnimalA , idAnimalB , None , None , loadEvent=False ) print( followIsolatedTimeLine.eventNameWithId ) resultIso={} animalA = pool.animalDictionnary[idAnimalA] animalB = pool.animalDictionnary[idAnimalB] dicA = animalA.detectionDictionnary dicB = animalB.detectionDictionnary dicContactBC = contact[ idAnimalB, idAnimalC ].getDictionnary() dicContactBD = contact[ idAnimalB, idAnimalD ].getDictionnary() for t in dicB.keys(): if ( t in dicContactBC.keys() or t in dicContactBD ): continue if ( t in dicA.keys() ): speedA = pool.animalDictionnary[idAnimalA].getSpeed(t) speedB = pool.animalDictionnary[idAnimalB].getSpeed(t) #angleB = pool.animalList[idAnimalB].getDirection(t) angleA = pool.animalDictionnary[idAnimalA].getDirection(t) if (speedA == None or speedB == None): continue if ( speedA>SPEED_THRESHOLD_LOW and speedB>SPEED_THRESHOLD_LOW ): time = t angleB = pool.animalDictionnary[idAnimalB].getDirection(time) ''' try: if( animalA.getDistanceTo( t, animalB ) > 80 ): continue except: continue ''' for time in range( t-15, t+1, 2): #time = t try: angleB = pool.animalDictionnary[idAnimalB].getDirection(time) if ( checkZone( dicA[t].massX, dicA[t].massY, angleA, meanSizeB, dicB[time].massX, dicB[time].massY, angleB ) == True): resultIso[t] = True break except: pass followIsolatedTimeLine.reBuildWithDictionnary( resultIso ) followIsolatedTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event FollowZone old" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def process( file ): print(file) mem = virtual_memory() availableMemoryGB = mem.total / 1000000000 print( "Total memory on computer: (GB)", availableMemoryGB ) if availableMemoryGB < 10: print( "Not enough memory to use cache load of events.") disableEventTimeLineCache() chronoFullFile = Chronometer("File " + file ) connection = sqlite3.connect( file ) # update missing fields try: connection = sqlite3.connect( file ) c = connection.cursor() query = "ALTER TABLE EVENT ADD METADATA TEXT"; c.execute( query ) connection.commit() except: print( "METADATA field already exists" , file ) BuildDataBaseIndex.buildDataBaseIndex( connection, force=False ) # build sensor data animalPool = AnimalPool( ) animalPool.loadAnimals( connection ) #animalPool.buildSensorData(file) currentT = minT try: flushEvents( connection ) while currentT < maxT: currentMinT = currentT currentMaxT = currentT+ windowT if ( currentMaxT > maxT ): currentMaxT = maxT chronoTimeWindowFile = Chronometer("File "+ file+ " currentMinT: "+ str(currentMinT)+ " currentMaxT: " + str(currentMaxT) ); processTimeWindow( connection, file, currentMinT, currentMaxT ) chronoTimeWindowFile.printTimeInS() currentT += windowT print("Full file process time: ") chronoFullFile.printTimeInS() TEST_WINDOWING_COMPUTATION = False if ( TEST_WINDOWING_COMPUTATION ): print("*************") print("************* TEST START SECTION") print("************* Test if results are the same with or without the windowing.") # display and record to a file all events found, checking with rolling idA from None to 4. Save nbEvent and total len eventTimeLineList = [] eventList = getAllEvents( connection ) file = open("outEvent"+str(windowT)+".txt","w") file.write( "Event name\nnb event\ntotal duration" ) for eventName in eventList: for animal in range( 0,5 ): idA = animal if idA == 0: idA = None timeLine = EventTimeLineCached( connection, file, eventName, idA, minFrame=minT, maxFrame=maxT ) eventTimeLineList.append( timeLine ) file.write( timeLine.eventNameWithId+"\t"+str(len(timeLine.eventList))+"\t"+str(timeLine.getTotalLength())+"\n" ) file.close() #plotMultipleTimeLine( eventTimeLineList ) print("************* END TEST") flushEventTimeLineCache() except: exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) error = ''.join('!! ' + line for line in lines) t = TaskLogger( connection ) t.addLog( error ) flushEventTimeLineCache() print( error, file=sys.stderr ) raise FileProcessException()
def process( file ): print(file) chronoFullFile = Chronometer("File " + file ) connection = sqlite3.connect( file ) BuildDataBaseIndex.buildDataBaseIndex( connection, force=False ) # TODO: flush events, # TODO: recompute per segment of windowT. currentT = minT try: flushEvents( connection ) while currentT < maxT: currentMinT = currentT currentMaxT = currentT+ windowT if ( currentMaxT > maxT ): currentMaxT = maxT chronoTimeWindowFile = Chronometer("File "+ file+ " currentMinT: "+ str(currentMinT)+ " currentMaxT: " + str(currentMaxT) ); processTimeWindow( connection, currentMinT, currentMaxT ) chronoTimeWindowFile.printTimeInS() currentT += windowT print("Full file process time: ") chronoFullFile.printTimeInS() TEST_WINDOWING_COMPUTATION = False if ( TEST_WINDOWING_COMPUTATION ): print("*************") print("************* TEST START SECTION") print("************* Test if results are the same with or without the windowing.") # display and record to a file all events found, checking with rolling idA from None to 4. Save nbEvent and total len eventTimeLineList = [] eventList = getAllEvents( connection ) file = open("outEvent"+str(windowT)+".txt","w") file.write( "Event name\nnb event\ntotal duration" ) for eventName in eventList: for idAnimalA in range( 0,5 ): idA = idAnimalA if idA == 0: idA = None timeLine = EventTimeLineCached( connection, file, eventName, idA, minFrame=minT, maxFrame=maxT ) eventTimeLineList.append( timeLine ) file.write( timeLine.eventNameWithId+"\t"+str(len(timeLine.eventList))+"\t"+str(timeLine.getTotalLength())+"\n" ) file.close() #plotMultipleTimeLine( eventTimeLineList ) print("************* END TEST") except: exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) error = ''.join('!! ' + line for line in lines) t = TaskLogger( connection ) t.addLog( error ) print( error, file=sys.stderr ) raise FileProcessException()
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): ''' use pool cache if available ''' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax) ''' three animals are following each others with nose-to-anogenital contacts animals are moving this is a combination of Train2 events (train2 events must be calculated before this event) ''' #deleteEventTimeLineInBase(connection, "Train3" ) ''' build a list of train 2 for each time point ''' time = {} train3 = {} for idAnimalA in range(1, 5): for idAnimalB in range(1, 5): if (idAnimalA == idAnimalB): continue train2TimeLine = EventTimeLineCached(connection, file, "Train2", idAnimalA, idAnimalB, minFrame=tmin, maxFrame=tmax) for t in train2TimeLine.getDictionnary(): train = Train2(idAnimalA, idAnimalB) if (not t in time): time[t] = [] #print ( t , ":" , train.idA , " -> ", train.idB , "*" ) time[t].append(train) for t in time: trainList = time[t] for trainSource in trainList: for trainTarget in trainList: if (trainSource == trainTarget): continue isValid = "" ''' test chain link between train2 events ''' if trainSource.idB == trainTarget.idA: id1 = trainSource.idA id2 = trainSource.idB id3 = trainTarget.idB if not (id1, id2, id3) in train3: train3[id1, id2, id3] = {} train3[id1, id2, id3][t] = True isValid = ": validated train 3" #print ( t , ":" , trainSource.idA , " -> ", trainSource.idB, "--->", trainTarget.idA , " -> ", trainTarget.idB , isValid ) ''' save data ''' for idAnimalA in range(1, 5): for idAnimalB in range(1, 5): for idAnimalC in range(1, 5): if (idAnimalA, idAnimalB, idAnimalC) in train3: trainTimeLine = EventTimeLine(None, "Train3", idAnimalA, idAnimalB, idAnimalC, loadEvent=False) trainTimeLine.reBuildWithDictionnary(train3[idAnimalA, idAnimalB, idAnimalC]) #trainTimeLine.removeEventsBelowLength( 5 ) trainTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Train3", tmin=tmin, tmax=tmax) print("Rebuild event finished.") return
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): ''' Animal A is stopped (built-in event): Move social: animal A is stopped and in contact with any other animal. Move isolated: animal A is stopped and not in contact with any other animal. ''' pool = AnimalPool() pool.loadAnimals(connection) isInContactSourceDictionnary = {} moveSourceTimeLine = {} for idAnimalA in range(1, pool.getNbAnimals() + 1): ''' Load source stop timeLine and revert it to get the move timeline If the animal is not detected, this will result in a move. To avoid this we mask with the detection. ''' moveSourceTimeLine[idAnimalA] = EventTimeLine(connection, "Stop", idAnimalA, minFrame=tmin, maxFrame=tmax, inverseEvent=True) detectionTimeLine = EventTimeLine(connection, "Detection", idAnimalA, minFrame=tmin, maxFrame=tmax) moveSourceTimeLine[idAnimalA].keepOnlyEventCommonWithTimeLine( detectionTimeLine) ''' load contact dictionnary with whatever animal ''' isInContactSourceDictionnary[idAnimalA] = EventTimeLineCached( connection, file, "Contact", idAnimalA, minFrame=tmin, maxFrame=tmax).getDictionnary() for idAnimalA in range(1, pool.getNbAnimals() + 1): moveSocialResult = {} moveIsolatedResult = {} ''' loop over eventlist''' for moveEvent in moveSourceTimeLine[idAnimalA].eventList: ''' for each event we seek in t and search a match in isInContactDictionnary ''' for t in range(moveEvent.startFrame, moveEvent.endFrame + 1): if t in isInContactSourceDictionnary[idAnimalA]: moveSocialResult[t] = True else: moveIsolatedResult[t] = True ''' save move ''' moveResultTimeLine = EventTimeLine(None, "Move", idAnimalA, None, None, None, loadEvent=False) moveResultTimeLine.reBuildWithDictionnary( moveSourceTimeLine[idAnimalA].getDictionnary()) moveResultTimeLine.endRebuildEventTimeLine(connection) ''' save move isolated ''' moveIsolatedResultTimeLine = EventTimeLine(None, "Move isolated", idAnimalA, None, None, None, loadEvent=False) moveIsolatedResultTimeLine.reBuildWithDictionnary(moveIsolatedResult) moveIsolatedResultTimeLine.endRebuildEventTimeLine(connection) ''' save move social ''' moveSocialResultTimeLine = EventTimeLine(None, "Move in contact", idAnimalA, None, None, None, loadEvent=False) moveSocialResultTimeLine.reBuildWithDictionnary(moveSocialResult) moveSocialResultTimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Move", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None, showGraph=False): ''' use the pool provided or create it''' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax) ''' centerX = 512/2 centerY = 424/2 if ( showGraph ): plt.figure( 1 ) ''' # find threshold massZs = [] headZs = [] backZs = [] ''' for animal in pool.animalDictionnary.keys(): animalA = pool.animalDictionnary[animal] dicA = animalA.detectionDictionnary for t in dicA.keys(): massZs.append( dicA[t].massZ ) if dicA[t].frontZ > 0: headZs.append( dicA[t].frontZ ) if dicA[t].backZ > 0: backZs.append( dicA[t].backZ ) headZThreshold = np.percentile( headZs, 95 ) massZThreshold = np.percentile( massZs, 95 ) backZThreshold = np.percentile( backZs, 95 ) ''' headZThreshold = 50 - 20 massZThreshold = 148 - 20 backZThreshold = 30 - 20 eventName = "onHouse" ''' for animal in pool.animalDictionnary.keys(): print(pool.animalDictionnary[animal]) t = 335794 animalA = pool.animalDictionnary[animal] dicA = animalA.detectionDictionnary print( "mass: " , dicA[t].massZ ) print( "head: " , dicA[t].frontZ ) print( "back: " , dicA[t].backZ ) quit() ''' #deleteEventTimeLineInBase(connection, "onHouse" ) for animal in pool.animalDictionnary.keys(): print(pool.animalDictionnary[animal]) print("X is on the house") print(eventName) onHouseTimeLine = EventTimeLine(None, eventName, animal, None, None, None, loadEvent=False) centerZoneTimeLineDic = EventTimeLine(connection, "Center Zone", animal).getDictionary() result = {} animalA = pool.animalDictionnary[animal] dicA = animalA.detectionDictionnary for t in dicA.keys(): if not t in centerZoneTimeLineDic: # avoid rearing on wall continue massOk = False headOk = False backOk = False if dicA[t].massZ > massZThreshold: massOk = True if dicA[t].frontZ > 0: if dicA[t].frontZ > headZThreshold: headOk = True if dicA[t].backZ > 0: if dicA[t].backZ > backZThreshold: backOk = True if massOk and headOk: # and backOk result[t] = True #print ( t , dicA[t].massZ , dicA[t].frontZ ) ''' print( np.percentile( allVals, 95 ) ) print( np.percentile( allVals, 99 ) ) plt.figure( 1 ) plt.hist( allVals , bins = 1000 ) plt.show() ''' onHouseTimeLine.reBuildWithDictionnary(result) onHouseTimeLine.removeEventsBelowLength(5) onHouseTimeLine.mergeCloseEvents(30) onHouseTimeLine.endRebuildEventTimeLine(connection) print(onHouseTimeLine) #quit() ''' if ( showGraph ): 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.")
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): ''' use the pool provided or create it''' if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax) ''' Event OralSide Sequence ''' nbAnimal = pool.getNbAnimals() contact = {} oralOral = {} oralGenital = {} oralOralDico = {} oralGenitalDico = {} EVENT_MIN_LEN = 10 for idAnimalB in pool.animalDictionnary.keys(): print(pool.animalDictionnary[idAnimalB]) meanSizeB = pool.animalDictionnary[idAnimalB].getMeanBodyLength( tmax=tmax) for animal in pool.animalDictionnary.keys(): if (idAnimalB == animal): continue ''' contact[animal, idAnimalB] = EventTimeLine( connection, "Contact", animal, idAnimalB, minFrame=tmin, maxFrame=tmax ) ''' oralOral[animal, idAnimalB] = EventTimeLineCached(connection, file, "Oral-oral Contact", animal, idAnimalB, minFrame=tmin, maxFrame=tmax) oralGenital[animal, idAnimalB] = EventTimeLineCached( connection, file, "Oral-genital Contact", animal, idAnimalB, minFrame=tmin, maxFrame=tmax) oralOralDico[animal, idAnimalB] = oralOral[animal, idAnimalB].getDictionnary() oralGenitalDico[animal, idAnimalB] = oralGenital[ animal, idAnimalB].getDictionnary() window = 60 ''' oral oral followed by a oral-genital ''' eventName = "seq oral oral - oral genital" print(eventName) for animal in range(1, nbAnimal + 1): for idAnimalB in range(1, nbAnimal + 1): if (animal == idAnimalB): continue selOO_OG_TimeLine = EventTimeLine(None, eventName, animal, idAnimalB, None, None, loadEvent=False) print(selOO_OG_TimeLine.eventNameWithId) oralOralTimeLine = oralOral[animal, idAnimalB] oralGeniDico = oralGenitalDico[animal, idAnimalB] oralGeniTimeLine = oralGenital[animal, idAnimalB] for event in oralOralTimeLine.eventList: endFrame = event.endFrame if (event.duration() >= EVENT_MIN_LEN): continue for t in range(endFrame, endFrame + window + 1): if (t in oralGeniDico): event2 = oralGeniTimeLine.getEventAt(t) if (event2.duration() >= EVENT_MIN_LEN): start = event.startFrame end = event2.endFrame selOO_OG_TimeLine.addEvent(Event(start, end)) break selOO_OG_TimeLine.endRebuildEventTimeLine(connection) window = 60 ''' oral A genital B followed by oral oral ''' eventName = "seq oral geni - oral oral" print(eventName) for animal in range(1, nbAnimal + 1): for idAnimalB in range(1, nbAnimal + 1): if (animal == idAnimalB): continue selOG_OO_TimeLine = EventTimeLine(None, eventName, animal, idAnimalB, None, None, loadEvent=False) print(selOG_OO_TimeLine.eventNameWithId) oralGenitalTimeLine = oralGenital[animal, idAnimalB] ooDico = oralOralDico[animal, idAnimalB] oralOralTimeLine = oralOral[animal, idAnimalB] for event in oralGenitalTimeLine.eventList: endFrame = event.endFrame if (event.duration() >= EVENT_MIN_LEN): continue for t in range(endFrame, endFrame + window + 1): if (t in ooDico): event2 = oralOralTimeLine.getEventAt(t) if (event2.duration() >= EVENT_MIN_LEN): start = event.startFrame end = event2.endFrame selOG_OO_TimeLine.addEvent(Event(start, end)) break selOG_OO_TimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Oral Side Sequence", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None ): ''' use the pool provided or create it''' if ( pool == None ): pool = AnimalPool( ) pool.loadAnimals( connection ) pool.loadDetection( start = tmin, end = tmax ) ''' Event Rear5: - the animal is rearing - distinction between rearing in contact with one or several animals and rearing isolated from the others ''' contact = {} for idAnimalA in pool.animalDictionnary.keys(): print(pool.animalDictionnary[idAnimalA]) contact[idAnimalA] = EventTimeLineCached( connection, file, "Contact", idAnimalA, minFrame=tmin, maxFrame=tmax ) contactDico = contact[idAnimalA].getDictionnary() eventName1 = "Rear isolated" eventName2 = "Rear in contact" print ( "A rears") rearSocialTimeLine = EventTimeLine( None, eventName2 , idAnimalA , None , None , None , loadEvent=False ) rearIsolatedTimeLine = EventTimeLine( None, eventName1 , idAnimalA , None , None , None , loadEvent=False ) resultSocial={} resultIsolated={} animalA = pool.animalDictionnary[idAnimalA] #print ( animalA ) dicA = animalA.detectionDictionnary for t in dicA.keys(): slope = dicA[t].getBodySlope() if ( slope == None): continue if ( abs( slope ) < BODY_SLOPE_THRESHOLD ): continue; if (t in contactDico.keys()): #print("social") resultSocial[t] = True else: #print("isolated") resultIsolated[t] = True rearSocialTimeLine.reBuildWithDictionnary( resultSocial ) rearIsolatedTimeLine.reBuildWithDictionnary( resultIsolated ) rearSocialTimeLine.endRebuildEventTimeLine( connection ) rearIsolatedTimeLine.endRebuildEventTimeLine( connection ) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Rear5" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): pool = AnimalPool() pool.loadAnimals(connection) #pool.loadDetection( start = tmin, end = tmax ) ''' load contact matrix ''' contact = {} group3In = {} group3Out = {} for idAnimalA in range(1, 5): contact[idAnimalA] = EventTimeLineCached(connection, file, "Contact", idAnimalA, minFrame=tmin, maxFrame=tmax) group3In[idAnimalA] = EventTimeLine(connection, "Group 3 make", idAnimalA, loadEvent=False) group3Out[idAnimalA] = EventTimeLine(connection, "Group 3 break", idAnimalA, loadEvent=False) ''' process group ''' for idAnimalA in range(1, 5): for idAnimalB in range(1, 5): for idAnimalC in range(1, 5): ''' check impossible combination (avoid 2 animals with same id) ''' test = {} test[idAnimalA] = True test[idAnimalB] = True test[idAnimalC] = True if not len(test.keys()) == 3: continue ''' process group ''' group3 = EventTimeLineCached(connection, file, "Group3", idAnimalA, idAnimalB, idAnimalC, minFrame=tmin, maxFrame=tmax) eventList = group3.getEventList() for event in eventList: t = event.startFrame - 1 if (not contact[idAnimalA].hasEvent(t)): group3In[idAnimalA].addPunctualEvent(t) if (not contact[idAnimalB].hasEvent(t)): group3In[idAnimalB].addPunctualEvent(t) if (not contact[idAnimalC].hasEvent(t)): group3In[idAnimalC].addPunctualEvent(t) t = event.endFrame + 1 if (not contact[idAnimalA].hasEvent(t)): group3Out[idAnimalA].addPunctualEvent(t) if (not contact[idAnimalB].hasEvent(t)): group3Out[idAnimalB].addPunctualEvent(t) if (not contact[idAnimalC].hasEvent(t)): group3Out[idAnimalC].addPunctualEvent(t) ''' save all ''' for idAnimal in range(1, 5): group3In[idAnimal].endRebuildEventTimeLine(connection) group3Out[idAnimal].endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Group3 Make Break", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def reBuildEvent( connection, file, tmin=None, tmax=None , pool = None ): ''' Nest 3 ''' print("[NEST 3] : Assume that there is no occlusion, does not work with anonymous animals") if ( pool == None ): pool = AnimalPool( ) pool.loadAnimals( connection ) pool.loadDetection( start = tmin, end = tmax , lightLoad=True ) if ( len ( pool.getAnimalList() ) != 4 ): print( "[NEST3 Cancelled] 4 animals are required to build nest3.") return contact = {} for idAnimalA in range( 1 , 5 ): for idAnimalB in range( 1 , 5 ): if idAnimalA != idAnimalB: contact[idAnimalA,idAnimalB] = EventTimeLineCached( connection, file, "Contact", idAnimalA, idAnimalB, minFrame=tmin, maxFrame=tmax ).getDictionnary() stopDictionnary = {} for idAnimalA in range( 1 , 5 ): stopDictionnary[idAnimalA] = EventTimeLineCached( connection, file, "Stop", idAnimalA, minFrame=tmin, maxFrame=tmax ).getDictionnary() nest3TimeLine = {} for idAnimalA in range( 1 , 5 ): # the id will be the one excluded from nest. nest3TimeLine[idAnimalA] = EventTimeLine( None, "Nest3" , idAnimalA, loadEvent=False ) animalList = pool.getAnimalList() result = {} for idAnimalA in range( 1 , 5 ): result[idAnimalA] = {} for t in range( tmin, tmax+1 ): isNest = False nbAnimalAtT = 0 animalDetectedList = [] for animal in animalList: if t in animal.detectionDictionnary: nbAnimalAtT+=1 animalDetectedList.append( animal ) #print( str(t) + " : " + str( nbAnimalAtT ) ) #print("TEST") graph = nx.Graph(); # add nodes for animal in animalDetectedList: graph.add_node( animal ) for animalA in animalDetectedList: for animalB in animalDetectedList: if animalA != animalB: # add an edge if t in contact[animalA.baseId,animalB.baseId]: graph.add_edge( animalA, animalB ) # list of CC from the biggest to the smallest listCC = sorted(nx.connected_components( graph ), key=len, reverse=True) if ( len( listCC ) == 2 ): # we have 2 groups # check if animals in the biggest group are stopped. allStoppedInBiggestGroup = True for animal in list( listCC[0] ): if not ( t in stopDictionnary[animal.baseId] ): allStoppedInBiggestGroup = False break if allStoppedInBiggestGroup: if ( len( listCC[1] ) == 1 ): # the 2nd group (and the smallest) has only one mouse animal = list(listCC[1])[0] result[ animal.baseId ][ t ] = True ''' largestCC = len ( max(nx.connected_components( graph ), key=len) ) #print( str( t ) + " : " + str ( len( largestCC ) ) ) print( str( t ) + " : " + str ( largestCC ) + " / " + str( nbAnimalAtT ) ) if largestCC == nbAnimalAtT : isNest= True if isNest == True: print( "ADD PUNCTUAL") result[t] = True; ''' for idAnimalA in range( 1 , 5 ): # the id will be the one excluded from nest. nest3TimeLine[idAnimalA].reBuildWithDictionnary( result[idAnimalA] ) # remove very small events nest3TimeLine[idAnimalA].removeEventsBelowLength( 2 ) nest3TimeLine[idAnimalA].mergeCloseEvents( 3 ) nest3TimeLine[idAnimalA].endRebuildEventTimeLine(connection) #nest4TimeLine.reBuildWithDictionnary( result ) #nest4TimeLine.endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Nest3" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent( connection, file, tmin=None, tmax=None , pool = None ): moveEventList = ['Move isolated', 'Stop isolated'] moveEventListExclusive = exclusiveEventList[-3:-1] eventPairs = {'Move isolated': 'Move isolated exclusive', 'Stop isolated': 'Stop isolated exclusive'} if ( pool == None ): pool = AnimalPool( ) pool.loadAnimals( connection ) ''' load the existing timelines for animal ''' timeLine = {} dicoContact = {} for event in moveEventList+exclusiveEventList[:-3]: timeLine[event] = {} dicoContact[event] = {} for animal in range(1, pool.getNbAnimals() + 1): timeLine[event][animal] = EventTimeLineCached( connection, file, event, animal, minFrame=tmin, maxFrame=tmax ) dicoContact[event][animal] = timeLine[event][animal].getDictionary(minFrame=tmin, maxFrame=tmax) ''' create the timelines of the different exclusive moves ''' timeLineExclusive = {} for event in moveEventListExclusive: timeLineExclusive[event] = {} for animal in range(1, pool.getNbAnimals() + 1): timeLineExclusive[event][animal] = EventTimeLine(None, event, animal, loadEvent=False) #generate the dico of the different move events and initiate the list of frames to remove from each timeline moveDicoExclusive = {} framesToRemove = {} for event in moveEventList: moveDicoExclusive[eventPairs[event]] = {} framesToRemove[eventPairs[event]] = {} for animal in range(1, pool.getNbAnimals() + 1): moveDicoExclusive[eventPairs[event]][animal] = timeLine[event][animal].getDictionary(minFrame=tmin, maxFrame=tmax) framesToRemove[eventPairs[event]][animal] = [] ########################################################################### #exclude the move and stop events where animals are in contacts for animal in range(1, pool.getNbAnimals() + 1): for moveEvent in exclusiveEventList[-3:-1]: for t in moveDicoExclusive[moveEvent][animal].keys(): for contactEvent in exclusiveEventList[:-3]: if t in dicoContact[contactEvent][animal].keys(): print('t = ', t, 'in', moveEvent, ' and in ', contactEvent) framesToRemove[moveEvent][animal].append(t) ########################################################################### # clean the dictionary of the move and stop events from frames that are overlapping with exclusive contacts for animal in range(1, pool.getNbAnimals() + 1): for moveEvent in exclusiveEventList[-3:-1]: for t in framesToRemove[moveEvent][animal]: moveDicoExclusive[moveEvent][animal].pop(t, None) ##################################################### #reduild all events based on dictionary for moveEvent in exclusiveEventList[-3:-1]: for animal in range(1, pool.getNbAnimals() + 1): timeLineExclusive[moveEvent][animal].reBuildWithDictionnary(moveDicoExclusive[moveEvent][animal]) timeLineExclusive[moveEvent][animal].endRebuildEventTimeLine(connection) # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger( connection ) t.addLog( "Build Event Exclusive move and stop events" , tmin=tmin, tmax=tmax ) print( "Rebuild event finished." )
def reBuildEvent(connection, file, tmin=None, tmax=None, pool=None): ''' Nest 3 Nest 4 Group 2 Group 3 Group 4 ''' print( "[NEST 4] : Assume that there is no occlusion, does not work with anonymous animals" ) if (pool == None): pool = AnimalPool() pool.loadAnimals(connection) pool.loadDetection(start=tmin, end=tmax, lightLoad=True) # check if given max is more than available detection ''' maxT = pool.getMaxDetectionT() if ( tmax > maxT ): tmax = maxT ''' #pool.loadDetection( start = tmin, end = tmax ) if (len(pool.getAnimalList()) != 4): print("[NEST4 Cancelled] 4 animals are required to build nest 4.") return contact = {} for idAnimalA in range(1, 5): for idAnimalB in range(1, 5): if idAnimalA != idAnimalB: contact[idAnimalA, idAnimalB] = EventTimeLineCached( connection, file, "Contact", idAnimalA, idAnimalB, minFrame=tmin, maxFrame=tmax).getDictionnary( ) #fait une matrice de tous les contacts à deux possibles stopDictionnary = {} for idAnimalA in range(1, 5): stopDictionnary[idAnimalA] = EventTimeLineCached( connection, file, "Stop", idAnimalA, minFrame=tmin, maxFrame=tmax).getDictionnary() ''' nest3TimeLine = {} for idAnimalA in range( 1 , 5 ): nest3TimeLine = EventTimeLine( None, "Nest3" , idAnimalA, loadEvent=False ) ''' nest4TimeLine = EventTimeLine(None, "Nest4", loadEvent=False) ''' group2TimeLine = {} for idAnimalA in range( 1 , 5 ): for idAnimalB in range( 1 , 5 ): if ( idAnimalA != idAnimalB ): group2TimeLine[idAnimalA,idAnimalB] = EventTimeLine( None, "Group2" , idAnimalA , idAnimalB , loadEvent=False ) group3TimeLine = {} for idAnimalA in range( 1 , 5 ): for idAnimalB in range( 1 , 5 ): if( idAnimalA != idAnimalB ): for idAnimalC in range( 1 , 5 ): if ( idAnimalA != idAnimalC and idAnimalB != idAnimalC ): group3TimeLine[idAnimalA,idAnimalB] = EventTimeLine( None, "Group3" , idAnimalA , idAnimalB , idAnimalC, loadEvent=False ) group4TimeLine = EventTimeLine( None, "Group4" , loadEvent=False ) ''' animalList = pool.getAnimalList() result = {} for t in range(tmin, tmax + 1): isNest = False nbAnimalAtT = 0 animalDetectedList = [] for animal in animalList: if t in animal.detectionDictionnary: nbAnimalAtT += 1 animalDetectedList.append(animal) #print( str(t) + " : " + str( nbAnimalAtT ) ) if nbAnimalAtT == 0: isNest = True if not isNest: #print("TEST") graph = nx.Graph() # add nodes for animal in animalDetectedList: graph.add_node(animal) for animalA in animalDetectedList: for animalB in animalDetectedList: if animalA != animalB: # add an edge if t in contact[animalA.baseId, animalB.baseId]: graph.add_edge(animalA, animalB) # check connected components. If the biggest group gets all animal, we got a nest4 largestCC = len(max(nx.connected_components(graph), key=len)) #print( str( t ) + " : " + str ( len( largestCC ) ) ) #print( str( t ) + " : " + str ( largestCC ) + " / " + str( nbAnimalAtT ) ) if largestCC == nbAnimalAtT: # check if animals in the nest are stopped. allStoppedInBiggestGroup = True for animal in animalDetectedList: if not (t in stopDictionnary[animal.baseId]): allStoppedInBiggestGroup = False break if allStoppedInBiggestGroup: isNest = True if isNest == True: #print( "ADD PUNCTUAL") result[t] = True nest4TimeLine.reBuildWithDictionnary(result) # remove very small events nest4TimeLine.removeEventsBelowLength(2) # merge flashing events nest4TimeLine.mergeCloseEvents(3) nest4TimeLine.endRebuildEventTimeLine(connection) ''' for idAnimalA in range( 1 , 5 ): for idAnimalB in range( 1 , 5 ): if( idAnimalA == idAnimalB ): continue for idAnimalC in range( 1 , 5 ): if( idAnimalA == idAnimalC ): continue if( idAnimalB == idAnimalC ): continue for idAnimalD in range( 1 , 5 ): if( idAnimalA == idAnimalD ): continue if( idAnimalB == idAnimalD ): continue if( idAnimalC == idAnimalD ): continue eventName = "Group4" print ( eventName ) groupTimeLine = EventTimeLine( None, eventName , idAnimalA , idAnimalB , idAnimalC , idAnimalD , loadEvent=False ) result={} dicA = contact[ idAnimalA ].getDictionnary() dicB = contact[ idAnimalB ].getDictionnary() dicC = contact[ idAnimalC ].getDictionnary() dicD = contact[ idAnimalD ].getDictionnary() dicGroup2A = group2[ idAnimalA ].getDictionnary() dicGroup2B = group2[ idAnimalB ].getDictionnary() dicGroup2C = group2[ idAnimalC ].getDictionnary() dicGroup2D = group2[ idAnimalD ].getDictionnary() for t in dicA.keys(): if ( t in dicB and t in dicC and t in dicD ): if ( t in dicGroup2A or t in dicGroup2B or t in dicGroup2C or t in dicGroup2D): continue else: result[t]=True groupTimeLine.reBuildWithDictionnary( result ) groupTimeLine.endRebuildEventTimeLine(connection) ''' # log process from lmtanalysis.TaskLogger import TaskLogger t = TaskLogger(connection) t.addLog("Build Event Nest4", tmin=tmin, tmax=tmax) print("Rebuild event finished.")
def process(file): print(file) chronoFullFile = Chronometer("File " + file) connection = sqlite3.connect(file) try: animalPool = None print("Caching load of animal detection...") animalPool = AnimalPool() animalPool.loadAnimals(connection) animalPool.loadDetection(start=0, end=maxT) print("Caching load of animal detection done.") text_file = open("bad orientation.txt", "a") nb = {} totalLen = {} for animal in animalPool.getAnimalList(): badOrientationTimeLine = EventTimeLine(None, "bad orientation auto", animal.baseId, None, None, None, loadEvent=False) print("Processing", animal) for t in range(0, maxT): orientation = animal.getOrientationVector(t) speedVector = animal.getSpeedVector(t) speed = animal.getSpeed(t) if (orientation != None and speedVector != None and speed != None): if (speed > SPEED_THRESHOLD_HIGH): scalar = getScalarProduct(orientation, speedVector) if (scalar > 0): badOrientationTimeLine.addPunctualEvent(t) # we remove small durations event as we cannot be sure they are not artefact, so we keep only events longer than 1s badOrientationTimeLine.removeEventsBelowLength(30) print("Total len in frame: ", badOrientationTimeLine.getTotalLength()) totalLen[animal] = badOrientationTimeLine.getTotalLength() nb[animal] = badOrientationTimeLine.getNbEvent() badOrientationTimeLine.endRebuildEventTimeLine(connection) text_file.write("{}\t".format(file)) for resKey in totalLen.keys(): text_file.write("animal\t{}\tnb\t{}\ttotalLen\t{}\t".format( animal.RFID, nb[resKey], totalLen[resKey])) text_file.write("\n") text_file.close() chronoFullFile.printTimeInS() except: exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) error = ''.join('!! ' + line for line in lines) t = TaskLogger(connection) t.addLog(error) print(error, file=sys.stderr) raise FileProcessException()