def vPrep(fname, saveDir, check=True): fileName = va.getFileName(fname) aviProps = va.getAVIinfo(fname) plt.ion() # fBackground frames # ----------------- # Default values nFrames = 10 bgOK = False bgFile = saveDir + "/bg.npy" if os.path.isfile(bgFile): print("Background file exists, loading...") bg = np.load(bgFile) else: print("Generating background with default settings...") bg = va.getBg(fname, aviProps, nFrames) if check: while not bgOK: plt.figure() plt.imshow(bg) plt.set_cmap('gray') plt.show() uDecision = raw_input("Background OK? [y]es; [n]o ") if uDecision == 'y': bgOK = True else: uframes = raw_input("Select number of frames ") bg = va.getBg(fname, aviProps, int(uframes)) np.save(saveDir + 'bg', bg) print('Background file saved') # Threshold # -------------- # Default values ths = 40 morphDiameter = 10 pmtsFileExists = False thsOK = False pmtsFile = saveDir + "/pmts.npy" if os.path.isfile(pmtsFile): print("Parameters file exists, loading...") pmtsFileExists = True filePmts = np.load(pmtsFile) ths, morphDiameter = va.setThreshold(fname, aviProps, bg, filePmts[0][0], filePmts[1][0]) else: print("Generating threshold with default settings...") ths, morphDiameter = va.setThreshold(fname, aviProps, bg, ths, morphDiameter) if check: while not thsOK: uDecision = raw_input("Threshold OK? [y]es; [n]o ") if uDecision == 'y': thsOK = True else: while True: try: print "Current threshold is", ths uths = int(raw_input("Select new threshold ")) print "Current diameter is", morphDiameter umorph = int( raw_input( "Select new diameter to erode and dilate ")) break except ValueError: print("Invalid number, please try again ") ths, morphDiameter = va.setThreshold(fname, aviProps, bg, uths, umorph) # Arena areas # -------------- # Default values nestPos = [125, 220] nestArea = [(1, 320), (225, 320), (120, 125)] arenaCenter = [600, 244] foodArea = [(785, 360), (960, 360), (860, 185)] plt.ioff() arenaOk = False if pmtsFileExists: print("Using parameters file...") va.plotArena(aviProps, filePmts, bg) pmts = [[ths], [morphDiameter], filePmts[2], filePmts[3], filePmts[4], filePmts[5]] else: print("Generating arena with default settings...") pmts = [[ths], [morphDiameter], nestPos, nestArea, arenaCenter, foodArea] va.plotArena(aviProps, pmts, bg) if check: while not arenaOk: uDecision = raw_input("Arena OK? [y]es; [n]o ") if uDecision == 'y': arenaOk = True else: print("Select new arena ") points = va.setPoints(fname, aviProps, bg) pmts = [[ths], [morphDiameter], points[0], points[1], points[2], points[3]] #print pmts[0], pmts[1] #fsaveName = saveDir + fileName.rstrip(".avi") + "_pmts" np.save(saveDir + 'pmts', pmts) print('Parameters file saved')
def vPrep(fname, saveDir, check=True): fileName = va.getFileName(fname) aviProps = va.getAVIinfo(fname) plt.ion() # fBackground frames # ----------------- # Default values nFrames = 10 bgOK = False bgFile = saveDir + "/bg.npy" if os.path.isfile(bgFile): print("Background file exists, loading...") bg = np.load(bgFile) else: print("Generating background with default settings...") bg = va.getBg(fname, aviProps, nFrames) if check: while not bgOK: plt.figure() plt.imshow(bg) plt.set_cmap('gray') plt.show() uDecision = raw_input("Background OK? [y]es; [n]o ") if uDecision=='y': bgOK = True else: uframes = raw_input("Select number of frames ") bg = va.getBg(fname, aviProps, int(uframes)) np.save(saveDir + 'bg', bg) print('Background file saved') # Threshold # -------------- # Default values ths = 40 morphDiameter = 10 pmtsFileExists = False thsOK = False pmtsFile = saveDir + "/pmts.npy" if os.path.isfile(pmtsFile): print("Parameters file exists, loading...") pmtsFileExists = True filePmts = np.load(pmtsFile) ths, morphDiameter = va.setThreshold(fname, aviProps, bg, filePmts[0][0], filePmts[1][0]) else: print("Generating threshold with default settings...") ths, morphDiameter = va.setThreshold(fname, aviProps, bg, ths, morphDiameter) if check: while not thsOK: uDecision = raw_input("Threshold OK? [y]es; [n]o ") if uDecision=='y': thsOK = True else: while True: try: print "Current threshold is", ths uths = int(raw_input("Select new threshold ")) print "Current diameter is", morphDiameter umorph = int(raw_input("Select new diameter to erode and dilate ")) break except ValueError: print("Invalid number, please try again ") ths, morphDiameter = va.setThreshold(fname, aviProps, bg, uths, umorph) # Arena areas # -------------- # Default values nestPos = [125, 220] nestArea = [(1,320),(225,320),(120,125)] arenaCenter = [600,244] foodArea = [(785,360),(960,360),(860,185)] plt.ioff() arenaOk = False if pmtsFileExists: print("Using parameters file...") va.plotArena(aviProps, filePmts, bg) pmts = [[ths], [morphDiameter], filePmts[2], filePmts[3], filePmts[4], filePmts[5]] else: print("Generating arena with default settings...") pmts = [[ths], [morphDiameter], nestPos, nestArea, arenaCenter, foodArea] va.plotArena(aviProps, pmts, bg) if check: while not arenaOk: uDecision = raw_input("Arena OK? [y]es; [n]o ") if uDecision=='y': arenaOk = True else: print("Select new arena ") points = va.setPoints(fname, aviProps, bg) pmts = [[ths], [morphDiameter], points[0], points[1], points[2], points[3]] #print pmts[0], pmts[1] #fsaveName = saveDir + fileName.rstrip(".avi") + "_pmts" np.save(saveDir + 'pmts', pmts) print('Parameters file saved')
if len(tAxis)>len(trackData): tAxis = np.delete(tAxis, -1) if len(tAxis)<len(trackData): trackData = np.delete(trackData, -1) smooth_trackData = smooth.smooth(trackData[:,1], window_len=50) ax[ax2].plot(tAxis, trackData[:,1],'k',lw=1) ax[ax2].plot(tAxis, smooth_trackData, colors[d], lw=1.5) # Plot histogram ax[ax3].hist(trackData[:,1], bins=30, orientation='horizontal', histtype='stepfilled', normed=True, color=colors[d]) ax[ax3].set_xlim([0,0.008]) # get and plot looms #loomOnsets = da.getLoomOnsets(trackData, aviProps) #for l in loomOnsets: ax[ax2].plot(l, 1150, marker='o', markerfacecolor='k', markeredgecolor='k') plt.show() return dataOut # ------------------------------------------ data, props = [], [] for d in dirList: fname = d + '/trackingData.txt' movieFname = d + '/trackingMovie.avi' data.append(np.loadtxt(fname)) props.append(va.getAVIinfo(fname)) dataOut = plotTrackingData(data, props)
times = [line.strip() for line in f] except IOError: print('times.txt does not exist') minCpuTime = 10 #totalCpus = 100 cpus = int(totalCpus/len(fileList[1:])) nfile = 1 print 'Using', cpus, 'cpus per file' os.system('rm -f '+activeDir+'/file*.*') os.system('rm -f '+activeDir+'/tracking*') for f in fileList[1:]: f = fileList[0]+'/'+f aviProps = va.getAVIinfo(f) if nfile==1: startFrame = va.time2frame(int(times[0]), aviProps) else: startFrame = va.time2frame(0, aviProps) if nfile==len(fileList[1:]): endFrame = va.time2frame(int(times[1]), aviProps) if endFrame>aviProps[6]: endFrame=aviProps[6] else: endFrame = aviProps[6] if mode=='local': framesPerCpu = np.ceil(float(endFrame-startFrame)) secPerCpu = va.frame2time(framesPerCpu, aviProps) else: framesPerCpu = np.ceil(float(endFrame-startFrame)/cpus)
if len(tAxis) < len(trackData): trackData = np.delete(trackData, -1) smooth_trackData = smooth.smooth(trackData[:, 1], window_len=50) ax[ax2].plot(tAxis, trackData[:, 1], 'k', lw=1) ax[ax2].plot(tAxis, smooth_trackData, colors[d], lw=1.5) # Plot histogram ax[ax3].hist(trackData[:, 1], bins=30, orientation='horizontal', histtype='stepfilled', normed=True, color=colors[d]) ax[ax3].set_xlim([0, 0.008]) # get and plot looms #loomOnsets = da.getLoomOnsets(trackData, aviProps) #for l in loomOnsets: ax[ax2].plot(l, 1150, marker='o', markerfacecolor='k', markeredgecolor='k') plt.show() return dataOut # ------------------------------------------ data, props = [], [] for d in dirList: fname = d + '/trackingData.txt' movieFname = d + '/trackingMovie.avi' data.append(np.loadtxt(fname)) props.append(va.getAVIinfo(fname)) dataOut = plotTrackingData(data, props)
startFrame = int(sys.argv[3]) #tEnd = int(sys.argv[4]) nfile = sys.argv[4] ncall = sys.argv[5] if len(nfile) == 1: nfile = '00' + nfile elif len(nfile) == 2: nfile = '0' + nfile if len(ncall) == 1: ncall = '00' + ncall elif len(ncall) == 2: ncall = '0' + ncall secPerCpu = float(sys.argv[6]) nestThreshold = 10 aviProps = va.getAVIinfo(fname) print fname, aviProps fileName = va.getFileName(fname) trackFname = saveDir + 'file' + str(nfile) + '_' + ncall + '.txt' aviFname = saveDir + 'file' + str(nfile) + '_' + ncall + '.avi' bgSubFname = saveDir + 'file' + str(nfile) + '_' + ncall tStart = va.frame2time(startFrame, aviProps) + (int(ncall) - 1) * secPerCpu tEnd = tStart + secPerCpu print tStart, tEnd while True: try: print(saveDir + "bg.npy") bg = np.load(saveDir + "bg.npy") print("Background file loaded") except IOError:
with open(activeDir + '/times.txt', 'r') as f: times = [line.strip() for line in f] except IOError: print('times.txt does not exist') minCpuTime = 10 #totalCpus = 100 cpus = int(totalCpus / len(fileList[1:])) nfile = 1 print 'Using', cpus, 'cpus per file' os.system('rm -f ' + activeDir + '/file*.*') os.system('rm -f ' + activeDir + '/tracking*') for f in fileList[1:]: f = fileList[0] + '/' + f aviProps = va.getAVIinfo(f) if nfile == 1: startFrame = va.time2frame(int(times[0]), aviProps) else: startFrame = va.time2frame(0, aviProps) if nfile == len(fileList[1:]): endFrame = va.time2frame(int(times[1]), aviProps) if endFrame > aviProps[6]: endFrame = aviProps[6] else: endFrame = aviProps[6] if mode == 'local': framesPerCpu = np.ceil(float(endFrame - startFrame)) secPerCpu = va.frame2time(framesPerCpu, aviProps) else: framesPerCpu = np.ceil(float(endFrame - startFrame) / cpus)
import numpy as np import matplotlib.pylab as plt import matplotlib.cm as colormap import os.path import tkFileDialog import sys import videoAnalysisLib as va import dataAnalysisLib as da #activeDir = '/lmb/home/tbranco/analysis/114491a_foraging' activedir = sys.argv[1] datafname = activedir+'/trackingData.txt' avifname = activedir+'/trackingMovie.avi' aviProps = va.getAVIinfo(avifname) try: bg = np.load(activedir+'/bg.npy') print("Background file loaded") except IOError: print("Cannot find background file ") try: pmts = np.load(activedir+'/pmts.npy') print("Parameters file loaded") except IOError: print("Cannot find parameters file ") # Basic tracking data print("Processing data...") data, resultsDict, fig = va.analyseData(datafname, aviProps, bg, pmts) va.writeDict(resultsDict, activedir+'/trackingAnalysis.txt')