# Jerome, 5th November 2012: # This is a more recent version of large.py that Martin sent us by email on 31th July 2012 # He called it large2.py but I've renamed it large.py to fit the code. import os import traceback from _collections import defaultdict from numpy import * import numpy as np from scipy.misc import imsave import matplotlib.pyplot as plt MAXHARMONICS=3 FREQRANGE=375 def largeplot(matrixview, x=None, y=None, figname='largeplot'): rgbmatrix=zeros(matrixview.shape + (3,)) rgbmatrix[:, :, 0:3] = matrixview[:, :, newaxis] if y !=None and x!=None: rgbmatrix[y, x, 1] = 40 #green is call flippedmatrix=np.flipud(rgbmatrix) imsave(figname+'.png', flippedmatrix) return rgbmatrix def rgbplot(rgbmatrix, x=None, y=None, figname='rgbplot'): if y !=None and x!=None: rgbmatrix[y, x, 1] = 40 #green is call flippedmatrix=np.flipud(rgbmatrix) imsave(figname+'.png', flippedmatrix) return rgbmatrix def trajectory(spectrogram, harmonics, w, z, cutoff=0.01, dirname='.'): harm = array(np.round(np.average(harmonics, axis = 1, weights=w)), dtype=int) # consensus number of harmonics freqs = {} def average(z, axis, weights ): #find rows with zero weights and replace with a constant if np.sum(weights)==0: return 0 zerorows = np.sum(weights, axis=1)==0 numcols=weights.shape[1] weights[zerorows,:] = 1.0/numcols try: return np.average(z, axis, weights) except Exception, e: print traceback.format_exc(e) print weights return 0 for numharmo in range(1, MAXHARMONICS+1): freqs[numharmo] = zeros(z.shape[0]) #check there is at least one particle matching the consensus harmonic passed = ((harm == numharmo) & (sum((harmonics==numharmo), axis=1)!=0)) for thisharmo in range(1, numharmo+1): # fill out upper harmonics # get frequency conditional on number of harmonic freqs[thisharmo][passed, :] = average(z[passed,:] - 1, axis=1, weights=w[passed,:] * (harmonics[passed,:]==numharmo)) *thisharmo freqs[thisharmo] = where(freqs[thisharmo] >= FREQRANGE, FREQRANGE-1, freqs[thisharmo]) infavour = np.sum(ma.array(w, mask = z!=0), axis=1) notzero = freqs[1][infavour= FREQRANGE, FREQRANGE-1, freqs[thisharmo]) resx, resy = [], [] infavour = np.sum(ma.array(w, mask = z!=0), axis=1) notzero = freqs[1][infavour