def loadPD(filename, maxTime = 1650): import meaRecording import numpy as np # load the PD header = meaRecording.getRecordingHeader(filename) fileLength = header['nscans']/header['scanRate'] pd_array = meaRecording.getChannel(0, maxTime, filename) return pd_array
def getChannel(chan,length, filename): ''' load a channel form an igor generated binary experimental file Inputs ------ chan: channel number to be loaded 0, photodiode length: amount of time to load in seconds filename: the file to load from output ------ channel: 1D ndarray ''' from numpy import fromfile from numpy import zeros import meaRecording as mea header = mea.getRecordingHeader(filename) blockSize = header['blockSize'] outputLength = int(length*header['scanRate']) samplesNeeded = min(outputLength, header['nscans']) outputLength = samplesNeeded # Generate output, an ndarray of blockTime = [] output = zeros(outputLength) f = open(filename) block = 0 while samplesNeeded>0: # get ready to read next block corresponding to channel in question f.seek(header['headerSize']+block*blockSize*header['numberOfChannels']*2+chan*blockSize*2) # figure out if we are going to pull down the whole block or just a fraction samplesAdded = min(samplesNeeded, blockSize) currentSamples = fromfile(f, '>f2', samplesAdded) output[block*blockSize:block*blockSize+len(currentSamples)] = currentSamples samplesNeeded -= samplesAdded block += 1 f.close() return output
def loadAllWhiteFrames(wildcard, whiteThresholdFactor): ''' For all files in current directory that match wildcard, open one at a time and extract all the scans corresponding to PD where whiteThreshold is crossed. For each file, load PD, compute the maximum value in the PD and define as threshold anything that crosses that value * whiteThresholdFactor *********************** VERY IMPORTANT, this will produce random white frames if there are absolutely no white frames in the PD recording *********************** Parameters: ----------- whildcard: a string matching all files to be analyzed, see fnmatch for syntax but (* and ? are accepted) whiteThresholdFactor: a number between 0 and 1, although the code is not checking for it and any number will work Output: ------- whiteFrameScans: ndarray with the scans corresponding to the white frames scansperframe: estimated number of scans per frame scanRate: from the file's header, how many samples per second are we recording. ''' import os import fnmatch from numpy import append # init the ndarray to hold all white frames detected across all files. allWhiteScans = np.arange(0) accumulatedScans = 0 # init a ndarray to hold the average number of scans per frame, will have one item per file loaded scansperframe = np.arange(0) for file in os.listdir(os.getcwd()): if fnmatch.fnmatch(file, wildcard): print 'Working on file', file header = meaRecording.getRecordingHeader(file) nscans = header['nscans'] scanRate = header['scanRate'] # load PD, each item corresponds to 1/scanrate seconds pd = loadPD(file) # Get the number of scans per frame scansperframe = np.append(scansperframe, getScansPerFrame(pd)) # detect the maximum value in pd_array pd_max = np.max(pd) # define threshold that has to be crossed to define a white pd. whiteThreshold = pd_max*whiteThresholdFactor # detect crossings of threshold # all crossings are detected, if stimulus is apdating every n frames, each white frame will be detected n times whiteFrameScans = findThresholdCrossing(pd, whiteThreshold) # this is ndarray # shift the scans to take into account that we are not loading the 1st file whiteFrameScans += accumulatedScans # update accumulatedScans accumulatedScans += nscans # append white frames for current file to list with all white frames allWhiteScans = np.append(allWhiteScans, whiteFrameScans) return allWhiteScans, scansperframe, scanRate