def locate_label(self, linecontour, labelwidth): """find a good place to plot a label (relatively flat part of the contour) and the angle of rotation for the text object """ nsize= len(linecontour) if labelwidth > 1: xsize = int(ceil(nsize/labelwidth)) else: xsize = 1 if xsize == 1: ysize = nsize else: ysize = labelwidth XX = resize(asarray(linecontour)[:,0],(xsize, ysize)) YY = resize(asarray(linecontour)[:,1],(xsize,ysize)) yfirst = YY[:,0] ylast = YY[:,-1] xfirst = XX[:,0] xlast = XX[:,-1] s = ( (reshape(yfirst, (xsize,1))-YY) * (reshape(xlast,(xsize,1)) - reshape(xfirst,(xsize,1))) - (reshape(xfirst,(xsize,1))-XX) * (reshape(ylast,(xsize,1)) - reshape(yfirst,(xsize,1))) ) L=sqrt((xlast-xfirst)**2+(ylast-yfirst)**2) dist = add.reduce(([(abs(s)[i]/L[i]) for i in range(xsize)]),-1) x,y,ind = self.get_label_coords(dist, XX, YY, ysize, labelwidth) #print 'ind, x, y', ind, x, y angle = arctan2(ylast - yfirst, xlast - xfirst) rotation = angle[ind]*180/pi if rotation > 90: rotation = rotation -180 if rotation < -90: rotation = 180 + rotation # There must be a more efficient way... lc = [tuple(l) for l in linecontour] dind = lc.index((x,y)) #print 'dind', dind #dind = list(linecontour).index((x,y)) return x,y, rotation, dind
def locate_label(self, linecontour, labelwidth): """find a good place to plot a label (relatively flat part of the contour) and the angle of rotation for the text object """ nsize = len(linecontour) if labelwidth > 1: xsize = int(ceil(nsize / labelwidth)) else: xsize = 1 if xsize == 1: ysize = nsize else: ysize = labelwidth XX = resize(array(linecontour)[:, 0], (xsize, ysize)) YY = resize(array(linecontour)[:, 1], (xsize, ysize)) yfirst = YY[:, 0] ylast = YY[:, -1] xfirst = XX[:, 0] xlast = XX[:, -1] s = (reshape(yfirst, (xsize, 1)) - YY) * (reshape(xlast, (xsize, 1)) - reshape( xfirst, (xsize, 1))) - (reshape(xfirst, (xsize, 1)) - XX) * ( reshape(ylast, (xsize, 1)) - reshape(yfirst, (xsize, 1))) L = sqrt((xlast - xfirst)**2 + (ylast - yfirst)**2) dist = add.reduce(([(abs(s)[i] / L[i]) for i in range(xsize)]), -1) x, y, ind = self.get_label_coords(dist, XX, YY, ysize, labelwidth) angle = arctan2(ylast - yfirst, xlast - xfirst) rotation = angle[ind] * 180 / pi if rotation > 90: rotation = rotation - 180 if rotation < -90: rotation = 180 + rotation dind = list(linecontour).index((x, y)) return x, y, rotation, dind
def cohere_pairs( X, ij, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning, noverlap=0, preferSpeedOverMemory=True, progressCallback=donothing_callback, returnPxx=False): """ Cxy, Phase, freqs = cohere_pairs( X, ij, ...) Compute the coherence for all pairs in ij. X is a numSamples,numCols Numeric array. ij is a list of tuples (i,j). Each tuple is a pair of indexes into the columns of X for which you want to compute coherence. For example, if X has 64 columns, and you want to compute all nonredundant pairs, define ij as ij = [] for i in range(64): for j in range(i+1,64): ij.append( (i,j) ) The other function arguments, except for 'preferSpeedOverMemory' (see below), are explained in the help string of 'psd'. Return value is a tuple (Cxy, Phase, freqs). Cxy -- a dictionary of (i,j) tuples -> coherence vector for that pair. Ie, Cxy[(i,j) = cohere(X[:,i], X[:,j]). Number of dictionary keys is len(ij) Phase -- a dictionary of phases of the cross spectral density at each frequency for each pair. keys are (i,j). freqs -- a vector of frequencies, equal in length to either the coherence or phase vectors for any i,j key. Eg, to make a coherence Bode plot: subplot(211) plot( freqs, Cxy[(12,19)]) subplot(212) plot( freqs, Phase[(12,19)]) For a large number of pairs, cohere_pairs can be much more efficient than just calling cohere for each pair, because it caches most of the intensive computations. If N is the number of pairs, this function is O(N) for most of the heavy lifting, whereas calling cohere for each pair is O(N^2). However, because of the caching, it is also more memory intensive, making 2 additional complex arrays with approximately the same number of elements as X. The parameter 'preferSpeedOverMemory', if false, limits the caching by only making one, rather than two, complex cache arrays. This is useful if memory becomes critical. Even when preferSpeedOverMemory is false, cohere_pairs will still give significant performace gains over calling cohere for each pair, and will use subtantially less memory than if preferSpeedOverMemory is true. In my tests with a 43000,64 array over all nonredundant pairs, preferSpeedOverMemory=1 delivered a 33% performace boost on a 1.7GHZ Athlon with 512MB RAM compared with preferSpeedOverMemory=0. But both solutions were more than 10x faster than naievly crunching all possible pairs through cohere. See test/cohere_pairs_test.py in the src tree for an example script that shows that this cohere_pairs and cohere give the same results for a given pair. """ numRows, numCols = X.shape # zero pad if X is too short if numRows < NFFT: tmp = X X = zeros( (NFFT, numCols), X.typecode()) X[:numRows,:] = tmp del tmp numRows, numCols = X.shape # get all the columns of X that we are interested in by checking # the ij tuples seen = {} for i,j in ij: seen[i]=1; seen[j] = 1 allColumns = seen.keys() Ncols = len(allColumns) del seen # for real X, ignore the negative frequencies if X.typecode()==Complex: numFreqs = NFFT else: numFreqs = NFFT//2+1 # cache the FFT of every windowed, detrended NFFT length segement # of every channel. If preferSpeedOverMemory, cache the conjugate # as well windowVals = window(ones((NFFT,), X.typecode())) ind = range(0, numRows-NFFT+1, NFFT-noverlap) numSlices = len(ind) FFTSlices = {} FFTConjSlices = {} Pxx = {} slices = range(numSlices) normVal = norm(windowVals)**2 for iCol in allColumns: progressCallback(i/Ncols, 'Cacheing FFTs') Slices = zeros( (numSlices,numFreqs), Complex) for iSlice in slices: thisSlice = X[ind[iSlice]:ind[iSlice]+NFFT, iCol] thisSlice = windowVals*detrend(thisSlice) Slices[iSlice,:] = fft(thisSlice)[:numFreqs] FFTSlices[iCol] = Slices if preferSpeedOverMemory: FFTConjSlices[iCol] = conjugate(Slices) Pxx[iCol] = divide(mean(absolute(Slices)**2), normVal) del Slices, ind, windowVals # compute the coherences and phases for all pairs using the # cached FFTs Cxy = {} Phase = {} count = 0 N = len(ij) for i,j in ij: count +=1 if count%10==0: progressCallback(count/N, 'Computing coherences') if preferSpeedOverMemory: Pxy = FFTSlices[i] * FFTConjSlices[j] else: Pxy = FFTSlices[i] * conjugate(FFTSlices[j]) if numSlices>1: Pxy = mean(Pxy) Pxy = divide(Pxy, normVal) Cxy[(i,j)] = divide(absolute(Pxy)**2, Pxx[i]*Pxx[j]) Phase[(i,j)] = arctan2(Pxy.imag, Pxy.real) freqs = Fs/NFFT*arange(numFreqs) if returnPxx: return Cxy, Phase, freqs, Pxx else: return Cxy, Phase, freqs
def cohere_pairs(X, ij, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning, noverlap=0, preferSpeedOverMemory=True, progressCallback=donothing_callback, returnPxx=False): """ Cxy, Phase, freqs = cohere_pairs( X, ij, ...) Compute the coherence for all pairs in ij. X is a numSamples,numCols Numeric array. ij is a list of tuples (i,j). Each tuple is a pair of indexes into the columns of X for which you want to compute coherence. For example, if X has 64 columns, and you want to compute all nonredundant pairs, define ij as ij = [] for i in range(64): for j in range(i+1,64): ij.append( (i,j) ) The other function arguments, except for 'preferSpeedOverMemory' (see below), are explained in the help string of 'psd'. Return value is a tuple (Cxy, Phase, freqs). Cxy -- a dictionary of (i,j) tuples -> coherence vector for that pair. Ie, Cxy[(i,j) = cohere(X[:,i], X[:,j]). Number of dictionary keys is len(ij) Phase -- a dictionary of phases of the cross spectral density at each frequency for each pair. keys are (i,j). freqs -- a vector of frequencies, equal in length to either the coherence or phase vectors for any i,j key. Eg, to make a coherence Bode plot: subplot(211) plot( freqs, Cxy[(12,19)]) subplot(212) plot( freqs, Phase[(12,19)]) For a large number of pairs, cohere_pairs can be much more efficient than just calling cohere for each pair, because it caches most of the intensive computations. If N is the number of pairs, this function is O(N) for most of the heavy lifting, whereas calling cohere for each pair is O(N^2). However, because of the caching, it is also more memory intensive, making 2 additional complex arrays with approximately the same number of elements as X. The parameter 'preferSpeedOverMemory', if false, limits the caching by only making one, rather than two, complex cache arrays. This is useful if memory becomes critical. Even when preferSpeedOverMemory is false, cohere_pairs will still give significant performace gains over calling cohere for each pair, and will use subtantially less memory than if preferSpeedOverMemory is true. In my tests with a 43000,64 array over all nonredundant pairs, preferSpeedOverMemory=1 delivered a 33% performace boost on a 1.7GHZ Athlon with 512MB RAM compared with preferSpeedOverMemory=0. But both solutions were more than 10x faster than naievly crunching all possible pairs through cohere. See test/cohere_pairs_test.py in the src tree for an example script that shows that this cohere_pairs and cohere give the same results for a given pair. """ numRows, numCols = X.shape # zero pad if X is too short if numRows < NFFT: tmp = X X = zeros((NFFT, numCols), typecode(X)) X[:numRows, :] = tmp del tmp numRows, numCols = X.shape # get all the columns of X that we are interested in by checking # the ij tuples seen = {} for i, j in ij: seen[i] = 1 seen[j] = 1 allColumns = seen.keys() Ncols = len(allColumns) del seen # for real X, ignore the negative frequencies if typecode(X) == Complex: numFreqs = NFFT else: numFreqs = NFFT // 2 + 1 # cache the FFT of every windowed, detrended NFFT length segement # of every channel. If preferSpeedOverMemory, cache the conjugate # as well windowVals = window(ones((NFFT, ), typecode(X))) ind = range(0, numRows - NFFT + 1, NFFT - noverlap) numSlices = len(ind) FFTSlices = {} FFTConjSlices = {} Pxx = {} slices = range(numSlices) normVal = norm(windowVals)**2 for iCol in allColumns: progressCallback(i / Ncols, 'Cacheing FFTs') Slices = zeros((numSlices, numFreqs), Complex) for iSlice in slices: thisSlice = X[ind[iSlice]:ind[iSlice] + NFFT, iCol] thisSlice = windowVals * detrend(thisSlice) Slices[iSlice, :] = fft(thisSlice)[:numFreqs] FFTSlices[iCol] = Slices if preferSpeedOverMemory: FFTConjSlices[iCol] = conjugate(Slices) Pxx[iCol] = divide(mean(absolute(Slices)**2), normVal) del Slices, ind, windowVals # compute the coherences and phases for all pairs using the # cached FFTs Cxy = {} Phase = {} count = 0 N = len(ij) for i, j in ij: count += 1 if count % 10 == 0: progressCallback(count / N, 'Computing coherences') if preferSpeedOverMemory: Pxy = FFTSlices[i] * FFTConjSlices[j] else: Pxy = FFTSlices[i] * conjugate(FFTSlices[j]) if numSlices > 1: Pxy = mean(Pxy) Pxy = divide(Pxy, normVal) Cxy[(i, j)] = divide(absolute(Pxy)**2, Pxx[i] * Pxx[j]) Phase[(i, j)] = arctan2(Pxy.imag, Pxy.real) freqs = Fs / NFFT * arange(numFreqs) if returnPxx: return Cxy, Phase, freqs, Pxx else: return Cxy, Phase, freqs