def getSingleEventTable(self, event_name): """ Returns pandas table containing all found events for given event name' * Primary mouse' meta data * Event start time * Event end time * Event duration Args: event_name (str): Event name e. g. Rearing Returns: DataFrame """ data = defaultdict(list) for animal in self.getAnimalList(): with mute_prints(): eventTimeLine = EventTimeLine(self.conn, event_name, idA=animal.baseId, minFrame=self.detectionStartFrame, maxFrame=self.detectionEndFrame) for e in eventTimeLine.getEventList(): data["RFID"] .append(f"{animal.name}_{animal.RFID}") data["name"] .append(f"{animal.name}") data["genotype"] .append(f"{animal.genotype}") data["event_name"] .append(event_name) data["start_sec"] .append(e.startFrame / oneSecond) data["end_sec"] .append(e.endFrame / oneSecond) data["duration"] .append(e.duration() / oneSecond) df = pd.DataFrame(data) df.insert(2, "time", pd.to_timedelta(df["start_sec"], unit="s")) return df.sort_values("time").reset_index(drop=True)
def getDyadicGenotypeGroupedEventTable(self, event_list): """ Returns pandas table containing all found events for given event name and the involved mice' genotype * Primary / secondary mice genotype * Event start time * Event end time * Event duration Args:computeEventFeatures event_name (str): Event name e. g. Rearing Returns: DataFrame """ data = defaultdict(list) animal_list = self.getAnimalList() for a in animal_list: if a.genotype is None: print("Genotype for all mice needs to be set... (aborting)") for event_name in tqdm(event_list, desc="Event..."): for A in animal_list: for B in animal_list: if A.baseId == B.baseId: continue with mute_prints(): eventTimeLine = EventTimeLine( self.conn, event_name, idA=A.baseId, idB=B.baseId, minFrame=self.detectionStartFrame, maxFrame=self.detectionEndFrame, ) event_list = eventTimeLine.getEventList() for e in event_list: data["genotype_primary"].append(f"{A.genotype}") data["genotype_secondary"].append(f"{B.genotype}") data["event_name"].append(event_name) data["start_sec"].append(e.startFrame / oneSecond) data["end_sec"].append(e.endFrame / oneSecond) data["duration"].append(e.duration() / oneSecond) df = pd.DataFrame(data) if len(df) == 0: return df df.insert(2, "time", pd.to_timedelta(df["start_sec"], unit="s")) return df.sort_values("time").reset_index(drop=True)
def plotNight(self, show=True, saveFile=None): fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(12, 4)) nightTimeLine = EventTimeLine(self.conn, "night", None, None, None, None) for nightEvent in nightTimeLine.eventList: ax.axvspan(nightEvent.startFrame, nightEvent.endFrame, alpha=0.1, color='black') ax.text((nightEvent.startFrame + nightEvent.endFrame) / 2, 0.5, "dark phase", fontsize=8, ha='center') ''' set x axis ''' formatter = ticker.FuncFormatter(self.frameToTimeTicker) ax.xaxis.set_major_formatter(formatter) ax.tick_params(labelsize=6) ax.xaxis.set_major_locator(ticker.MultipleLocator(30 * 60 * 60 * 12)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(30 * 60 * 60)) if saveFile != None: print("Saving figure : " + saveFile) fig.savefig(saveFile, dpi=100) if (show): plt.show() plt.close()
def EventTimeLineCached(connection, file, eventName, idA=None, idB=None, idC=None, idD=None, minFrame=None, maxFrame=None, loadEventIndependently=False): global eventCacheDico_ if eventCacheEnable_ == True: #print ("Cache debug:", connection, file, eventName, "ids:" , idA, idB, idC, idD, "frames:", minFrame, maxFrame , loadEventWithoutOverlapCheck ) if (file, eventName, idA, idB, idC, idD, minFrame, maxFrame, loadEventIndependently) in eventCacheDico_: eventTimeLine = eventCacheDico_[file, eventName, idA, idB, idC, idD, minFrame, maxFrame, loadEventIndependently] print(eventName, " Id(", idA, ",", idB, ",", idC, ",", idD, ") Loaded from cache (", len(eventTimeLine.eventList), " records. )") return eventTimeLine eventTimeLine = EventTimeLine( connection, eventName, idA=idA, idB=idB, idC=idC, idD=idD, minFrame=minFrame, maxFrame=maxFrame, loadEventIndependently=loadEventIndependently) if eventCacheEnable_ == True: print("Caching eventTimeLine") eventCacheDico_[file, eventName, idA, idB, idC, idD, minFrame, maxFrame, loadEventIndependently] = eventTimeLine else: print("Loading event from base (Cache event disabled)") return eventTimeLine
def plotSensorData(self, sensor="TEMPERATURE", show=True, saveFile=None, minValue=0, autoNight=False): """ plots data for temperature """ print("plotting sensor data. ", sensor) cursor = self.conn.cursor() # Check the number of row available in base query = "SELECT FRAMENUMBER," + sensor + " FROM FRAME" try: cursor.execute(query) except: print("plot sensor data: can't access data for ", sensor) return rows = cursor.fetchall() cursor.close() frameNumberList = [] sensorValueList = [] for row in rows: sensorValue = row[1] if (sensorValue > minValue): if sensorValue != None and minValue != None: frameNumberList.append(row[0]) sensorValueList.append(sensorValue) fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(24, 8)) ''' set x axis ''' formatter = ticker.FuncFormatter(self.frameToTimeTicker) ax.xaxis.set_major_formatter(formatter) ax.tick_params(labelsize=20) ax.xaxis.set_major_locator(ticker.MultipleLocator(30 * 60 * 60 * 12)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(30 * 60 * 60)) ax.plot(frameNumberList, sensorValueList, color="black", linestyle='-', linewidth=1, label=sensor) ax.legend(loc=1) # plot night nightTimeLine = EventTimeLine(self.conn, "night", None, None, None, None) mean = 0 std = 0 if len(sensorValueList) > 1: mean = np.mean(sensorValueList) std = np.std(sensorValueList) for nightEvent in nightTimeLine.eventList: ax.axvspan(nightEvent.startFrame, nightEvent.endFrame, alpha=0.1, color='black', ymin=0, ymax=1) ax.text((nightEvent.startFrame + nightEvent.endFrame) / 2, mean + std, "dark phase", fontsize=20, ha='center') autoNightList = [] if autoNight: # compute mean value: plt.axhline(y=mean, color='r', linestyle='--') ax.text(0, mean, "auto night threshold", fontsize=20, ha='left', va='bottom') inNight = False start = None end = None for i in range(len(frameNumberList)): frame = frameNumberList[i] value = sensorValueList[i] if value < mean: # night if inNight: continue else: inNight = True start = frame else: if inNight: end = frame inNight = False if end - start > 300: autoNightList.append((start, end)) else: print( "Skipping very short night phase. (less than 10 seconds)" ) start = None end = None for autoNight in autoNightList: ax.axvspan(autoNight[0], autoNight[1], alpha=0.1, color='red', ymin=0.2, ymax=0.8) ax.text((autoNight[0] + autoNight[1]) / 2, mean, "auto dark phase", fontsize=20, ha='center', color='red') plt.xlabel('time') if sensor == "TEMPERATURE": plt.ylabel('Temperature (C)') plt.ylim(17, 28) if sensor == "SOUND": plt.ylabel('Sound level (indice)') if sensor == "HUMIDITY": plt.ylabel('Humidity %') plt.ylim(0, 100) if sensor == "LIGHTVISIBLE": plt.ylabel('Visible light (indice)') if sensor == "LIGHTVISIBLEANDIR": plt.ylabel('Visible and infrared light (indice)') if saveFile != None: print("Saving figure : " + saveFile) fig.savefig(saveFile, dpi=100) ''' print("TEST 1") import os os.startfile( saveFile, 'open') print( "TEST 2") ''' if (show): plt.show() plt.close() if autoNight: return autoNightList
for file in files: # connect to database connection = sqlite3.connect(file) # create an animalPool, which basically contains your animals animalPool = AnimalPool() # load infos about the animals animalPool.loadAnimals(connection) # load all detection (positions) of all animals for the first hour animalPool.loadDetection(start=0, end=oneHour) eventTimeLine = EventTimeLine(connection, "Oral-genital Contact", idA=1, idB=2, minFrame=0, maxFrame=oneHour) print("Event list for label ", eventTimeLine.eventNameWithId) print("for animal 1:", animalPool.getAnimalDictionnary()[1].RFID) print("for animal 2:", animalPool.getAnimalDictionnary()[2].RFID) print("Number of events:", len(eventTimeLine.getEventList())) print("start frame", "end frame", "duration(in frame)") for event in eventTimeLine.eventList: print(event.startFrame, event.endFrame, event.duration())
if __name__ == '__main__': #ask the user for database to process files = getFilesToProcess() for file in files: # connect to database connection = sqlite3.connect( file ) # create an animalPool, which basically contains your animals animalPool = AnimalPool() # load infos about the animals animalPool.loadAnimals( connection ) # load all detection (positions) of all animals for the first hour animalPool.loadDetection( start = 0, end = oneHour ) eventTimeLineList = [] for a in animalPool.getAnimalDictionnary(): for b in animalPool.getAnimalDictionnary(): eventTimeLine = EventTimeLine( connection, "Oral-genital Contact", idA = a, idB = b, minFrame = 0, maxFrame = oneHour ) eventTimeLineList.append( eventTimeLine ) plotMultipleTimeLine( eventTimeLineList )
from lmtanalysis.FileUtil import getFilesToProcess from lmtanalysis.Animal import AnimalPool from lmtanalysis.Measure import * from lmtanalysis.Event import EventTimeLine if __name__ == '__main__': #ask the user for database to process files = getFilesToProcess() for file in files: # connect to database connection = sqlite3.connect(file) # create an animalPool, which basically contains your animals animalPool = AnimalPool() # load infos about the animals animalPool.loadAnimals(connection) # load all detection (positions) of all animals for the first hour animalPool.loadDetection(start=0, end=oneHour) eventTimeLine = EventTimeLine(connection, "Oral-genital Contact", minFrame=0, maxFrame=oneHour) eventTimeLine.plotTimeLine()
min = start*oneHour max = (start+1)*oneHour for animal in animalPool.getAnimalList(): bt = animal.getBodyThreshold( tmin= min, tmax = max ) bs = animal.getMedianBodyHeight( tmin= min , tmax = max ) print ( "min:\t" , str(min), "\tmax:\t", str(max), "\tAnimal:\t" , str(animal.baseId), "\tBT:\t " , str(bt) , "\tBS:\t" , str(bs) ) quit() ''' eventTimeLineList = [] naiveMoveTimeLine = EventTimeLine(connection, "Stop", idA=1, minFrame=minT, maxFrame=maxT, inverseEvent=True) print("MOVE") print("Naive move Total time: ", str(naiveMoveTimeLine.getTotalLength())) naiveMoveTimeLine.plotTimeLine() detectionMoveTimeLine = EventTimeLine(connection, "Detection", idA=1, minFrame=minT, maxFrame=maxT) eventTimeLineList.append(detectionMoveTimeLine) eventTimeLineList.append(naiveMoveTimeLine)
files = getFilesToProcess() for file in files: # connect to database connection = sqlite3.connect(file) # create an animalPool, which basically contains your animals animalPool = AnimalPool() # load infos about the animals animalPool.loadAnimals(connection) # load all detection (positions) of all animals for the first hour animalPool.loadDetection(start=0, end=oneHour) eventTimeLine1 = EventTimeLine(connection, "Oral-genital Contact", idA=1, idB=2, minFrame=0, maxFrame=oneHour) eventTimeLine2 = EventTimeLine(connection, "Oral-genital Contact", idA=2, idB=1, minFrame=0, maxFrame=oneHour) plotMultipleTimeLine([eventTimeLine1, eventTimeLine2])