def getobs2(snum,stime,observables): if len(Numeric.shape(observables)) == 0: return Numeric.asarray(map(observables,Numeric.arange(0,snum*stime,stime)),dtype='d') else: def mapfun(x): return map(lambda y: y(x),observables) return Numeric.asarray(map(mapfun,Numeric.arange(0,snum*stime,stime)),dtype='d')
def wpdg(series,detrend=0,win='triangle'): samples = Numeric.shape(series)[0] wrange = Numeric.arange(0,samples,dtype='d') / (samples - 1.0); if win == 'blackman': window = 0.42 - 0.5 * Numeric.cos(2*math.pi*wrange) + 0.08 * Numeric.cos(4*math.pi*wrange) elif win == 'sin4': window = Numeric.sin(math.pi*wrange)**4.0 else: # if we don't recognize a window, default to triangle pdlen = (samples - 1) / 2.0 window = 1.0 - abs(Numeric.arange(0,samples,dtype='d') - pdlen) / (pdlen) wseries = series.copy() if detrend == 1: leastsquares(wseries,detrend=1) wseries *= window weight = samples * Numeric.sum(window ** 2) wpdgram = pdg(wseries) * (1.0 * samples**2 / weight) return wpdgram
def histogram(data, nbins, range = None): """ Create a histogram. Comes from Konrad Hinsen: Scientific Python @param data: data list or array @type data: [any] @param nbins: number of bins @type nbins: int @param range: data range to create histogram from (min val, max val) @type range: (float, float) OR None @return: array (2 x len(data) ) with start of bin and witdh of bin. @rtype: array """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat(data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) bin_width = (max-min)/nbins data = Numeric.floor((data - min)/bin_width).astype(Numeric.Int) histo = Numeric.add.reduce(Numeric.equal( Numeric.arange(nbins)[:,Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width*(Numeric.arange(nbins)+0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def histogram(data, nbins, range=None): """ Comes from Konrad Hinsen: Scientific Python """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat( data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) # end if bin_width = (max - min) / nbins data = Numeric.floor((data - min) / bin_width).astype(Numeric.Int) histo = Numeric.add.reduce( Numeric.equal(Numeric.arange(nbins)[:, Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width * (Numeric.arange(nbins) + 0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def histogram(data, nbins, range=None): """ Create a histogram. Comes from Konrad Hinsen: Scientific Python @param data: data list or array @type data: [any] @param nbins: number of bins @type nbins: int @param range: data range to create histogram from (min val, max val) @type range: (float, float) OR None @return: array (2 x len(data) ) with start of bin and witdh of bin. @rtype: array """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat( data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) bin_width = (max - min) / nbins data = Numeric.floor((data - min) / bin_width).astype(Numeric.Int) histo = Numeric.add.reduce( Numeric.equal(Numeric.arange(nbins)[:, Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width * (Numeric.arange(nbins) + 0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def histogram(data, nbins, range = None): """ Comes from Konrad Hinsen: Scientific Python """ data = Numeric.array(data, Numeric.Float) if range is None: min = Numeric.minimum.reduce(data) max = Numeric.maximum.reduce(data) else: min, max = range data = Numeric.repeat(data, Numeric.logical_and(Numeric.less_equal(data, max), Numeric.greater_equal(data, min))) # end if bin_width = (max-min)/nbins data = Numeric.floor((data - min)/bin_width).astype(Numeric.Int) histo = Numeric.add.reduce(Numeric.equal( Numeric.arange(nbins)[:,Numeric.NewAxis], data), -1) histo[-1] = histo[-1] + Numeric.add.reduce(Numeric.equal(nbins, data)) bins = min + bin_width*(Numeric.arange(nbins)+0.5) return Numeric.transpose(Numeric.array([bins, histo]))
def draw_ecg_obj(self, data,start_x): if len(data) != 0: count = 2*(len(data)+1) np_data = _Numeric.arange(count)/1. np_data.shape = (count/2,2) np_data[:,0] = _Numeric.arange(start_x, start_x+count/2) data.insert(0,data[0]) np_data[:,1] = data self.lines = PolySpline(np_data, legend= 'Red Line', colour = self.clr) self.SetShowScrollbars(True) self.Draw(PlotGraphics([self.lines],"Graph Title", "0.2s", "mv"), yAxis = self.yAxis,xAxis = (0,self.sr*5))
def kNNimputeMA(arr2d, K=20, callback=None): """Returns a new 2D MA.array with missing values imputed from K nearest neighbours. Find K rows (axis 0) with the most similar values where similarity measure corresponds to weighted Euclidean distance. Imputed value = weighted average of the corresponding values of K nearest neighbours, where weights equal to tricubic distribution of distances to all rows. Impute missing rows by average over all rows. Version: 30.8.2005 """ arr2d = MA.asarray(arr2d) assert len(arr2d.shape) == 2, "2D array expected" # make a copy for imputation aImp2 = MA.array(arr2d) # leave out columns with 0 known values (columnInd: non-zero columns) columnCond = Numeric.greater(MA.count(arr2d, axis=0), 0) columnIndAll = Numeric.arange(arr2d.shape[1]) columnInd = Numeric.compress(columnCond, columnIndAll) # impute the rows where 0 < #known_values < #non_zero_columns, i.e. exclude the rows with 0 and all (non-zero-column) values countByRows = MA.count(arr2d, axis=1) for rowIdx in Numeric.compress(Numeric.logical_and(Numeric.greater(countByRows, 0), Numeric.less(countByRows, columnInd.shape[0])), Numeric.arange(arr2d.shape[0])): rowResized = MA.resize(arr2d[rowIdx], arr2d.shape) diff = arr2d - rowResized distances = MA.sqrt(MA.add.reduce((diff)**2, 1) / MA.count(diff, axis=1)) # nearest neighbours row indices (without the current row index) indSorted = MA.argsort(distances)[1:] distSorted = distances.take(indSorted) # number of distances different from MA.masked numNonMasked = distSorted.shape[0] - Numeric.add.reduce(Numeric.asarray(MA.getmaskarray(distSorted), Numeric.Int)) # number of distances to account for (K or less) if numNonMasked > 1: weightsSorted = MA.power(1-MA.power(distSorted/distSorted[numNonMasked-1],3),3) # tricubic distribution of all weights else: weightsSorted = Numeric.ones(distSorted.shape[0]) # compute average for each column separately in order to account for K non-masked values colInd4CurrRow = Numeric.compress(Numeric.logical_and(MA.getmaskarray(arr2d[rowIdx]), columnCond), columnIndAll) for colIdx in colInd4CurrRow: # column values sorted by distances columnVals = arr2d[:,colIdx].take(indSorted) # take only those weights where columnVals does not equal MA.masked weightsSortedCompressed = MA.compress(1-MA.getmaskarray(columnVals), weightsSorted) # impute from K (or possibly less) values aImp2[rowIdx,colIdx] = MA.average(columnVals.compressed()[:K], weights=weightsSortedCompressed[:K]) if callback: callback() # impute the unknown rows with average profile avrgRow = MA.average(arr2d, 0) for rowIdx in Numeric.compress(Numeric.equal(countByRows, 0), Numeric.arange(arr2d.shape[0])): aImp2[rowIdx] = avrgRow if callback: callback() return aImp2
def __init__(self, pnts): pnts = numerix.transpose(numerix.asarray(pnts, numerix.Float)) npnts = pnts.shape[1] if npnts < 3: raise NURBSError, 'Point sequence error' cntrl = numerix.zeros((pnts.shape[0], 2 * npnts - 2), numerix.Float) cntrl[:,0] = pnts[:,0] cntrl[:,-1] = pnts[:,-1] cntrl[:,1:-2:2] = pnts[:,1:-1] cntrl[:,2:-1:2] = pnts[:,1:-1] uknots = numerix.zeros(npnts * 2, numerix.Float) uknots[0::2] = numerix.arange(npnts) uknots[1::2] = numerix.arange(npnts) Crv.__init__(self, cntrl, uknots)
def __init__(self, pnts): pnts = numerix.transpose(numerix.asarray(pnts, numerix.Float)) npnts = pnts.shape[1] if npnts < 3: raise NURBSError, 'Point sequence error' cntrl = numerix.zeros((pnts.shape[0], 2 * npnts - 2), numerix.Float) cntrl[:, 0] = pnts[:, 0] cntrl[:, -1] = pnts[:, -1] cntrl[:, 1:-2:2] = pnts[:, 1:-1] cntrl[:, 2:-1:2] = pnts[:, 1:-1] uknots = numerix.zeros(npnts * 2, numerix.Float) uknots[0::2] = numerix.arange(npnts) uknots[1::2] = numerix.arange(npnts) Crv.__init__(self, cntrl, uknots)
def MaxInnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod1 = Numeric.zeros(pdlen+1, dtype='d') prod2 = Numeric.zeros(pdlen+1, dtype='d') prod1[0] = 0.0 prod1[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod1[pdlen] = fourier1[pdlen]*fourier2[pdlen] prod2[0] = 0.0 prod2[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen]*1.j)) + numpy.multiply((fourier1[-1:pdlen:-1]),numpy.conjugate(fourier2[-1:pdlen:-1]*(-1.j))) prod2[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod1[1:], PSD, prod1[1:]) Numeric.divide(prod2[1:], PSD, prod2[1:]) olap0 = 0.0 olappiby2 = 0.0 for i in xrange(pdlen): if (freqs[i] > 1.e-5 and freqs[i]<0.01): olap0 += prod1[i] olappiby2 += prod2[i] olap0 = 2.0*olap0/float(size) olappiby2 = 2.0*olappiby2/float(size) # olap0 = 2.0*(numpy.sum(prod1[1:]))/float(size) #it must be scaled by dt # olappiby2 = 2.0*(numpy.sum(prod2[1:]))/float(size) #it must be scaled by dt print "angle of maxim. = ", math.atan(olappiby2/olap0) return sqrt(olap0**2 + olappiby2**2)
def afterInit(self): # 1000 points cos function, plotted as blue line self.appendLineFromEquation("2*_Numeric.cos(%s)", 5, 10, Caption="Blue Line", LineWidth=2, LineColor="blue") line = [] for i in range(10): line.append((i, float(i) / 2)) self.appendLineFromPoints(line) data1 = 2. * _Numeric.pi * _Numeric.arange(200) / 200. data1.shape = (100, 2) data1[:, 1] = _Numeric.sin(data1[:, 0]) self.appendMarkerFromPoints(data1, Caption='Green Markers', Color='green', MarkerShape='circle', MarkerSize=1) # A few more points... points = [(0., 0.), (_Numeric.pi / 4., 1.), (_Numeric.pi / 2, 0.), (3. * _Numeric.pi / 4., -1)] self.appendMarkerFromPoints(points, Caption='Cross Legend', Color='blue', MarkerShape='cross')
def MaxInnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod1 = Numeric.zeros(pdlen+1, dtype='d') prod2 = Numeric.zeros(pdlen+1, dtype='d') prod1[0] = 0.0 prod1[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod1[pdlen] = fourier1[pdlen]*fourier2[pdlen] prod2[0] = 0.0 prod2[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen]*1.j)) + numpy.multiply((fourier1[-1:pdlen:-1]),numpy.conjugate(fourier2[-1:pdlen:-1]*(-1.j))) prod2[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod1[1:], PSD, prod1[1:]) Numeric.divide(prod2[1:], PSD, prod2[1:]) olap0 = 0.0 olappiby2 = 0.0 for i in xrange(pdlen): if (freqs[i] > fLow and freqs[i]<= fHigh): olap0 += prod1[i] olappiby2 += prod2[i] olap0 = 2.0*olap0/float(size) olappiby2 = 2.0*olappiby2/float(size) # olap0 = 2.0*(numpy.sum(prod1[1:]))/float(size) #it must be scaled by dt # olappiby2 = 2.0*(numpy.sum(prod2[1:]))/float(size) #it must be scaled by dt print "angle of maxim. = ", math.atan(olappiby2/olap0) return sqrt(olap0**2 + olappiby2**2)
def group( self, a_indices, maxPerCenter ): """ Group a bunch of integers (atom indices in PDBModel) so that each group has at most maxPerCenter items. @param a_indices: atom indices @type a_indices: [int] @param maxPerCenter: max entries per group @type maxPerCenter: int @return: list of lists of int @rtype: [[int],[int]..] """ ## how many groups are necessary? n_centers = len( a_indices ) / maxPerCenter if len( a_indices ) % maxPerCenter: n_centers += 1 ## how many items/atoms go into each group? nAtoms = N.ones(n_centers, N.Int) * int(len( a_indices ) / n_centers) i=0 while N.sum(nAtoms) != len( a_indices ): nAtoms[i] += 1 i += 1 ## distribute atom indices into groups result = [] pos = 0 for n in nAtoms: result += [ N.take( a_indices, N.arange(n) + pos) ] pos += n return result
def InnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod = Numeric.zeros(pdlen+1, dtype='d') prod[0] = 0.0 prod[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod[1:], PSD, prod[1:]) olap0 = 0.0 for i in xrange(pdlen): if (freqs[i] > fLow and freqs[i]<= fHigh): olap0 += prod[i] olap0 = 2.0*olap0/float(size) # olap0 = 2.0*(numpy.sum(prod[1:]))/float(size) #it must be scaled by dt return olap0
def noiseproduct(signal1,signal2,noise,stime,npatches): """Compute the noise inner product for signal1 and signal2, where the two signals are sampled at intervals stime, and the product is computed for the total duration represented in the array, against noise represented by the time series noise; npatches overlapping periods are used to estimate the PSD of the noise.""" # compute signal FFT without windowing or averaging # this definition of the FFT satisfied Parseval's theorem, with # sum(signal**2) * stime == sum(abs(sfour)**2) / (stime*length(signal)) # [since deltaf = 1 / (totaltime)] sfour1 = stime * FFT.fft(signal1) sfour2 = stime * FFT.fft(signal2) # compute the noise spectrum, using segment averaging, # and interpolate the noise to be defined on the same frequencies # of the signal's spectrum nspec = spect(noise,stime,npatches) siglen = len(signal1) deltaf = 1.0 / (stime * siglen) fourlen = siglen/2 + 1 ispec = Numeric.zeros([fourlen,2],dtype='d') ispec[:,0] = deltaf * Numeric.arange(0,fourlen) # ispec[:,1] = arrayfns.interp(nspec[:,1],nspec[:,0],ispec[:,0]) ispec[:,1] = linearinterpolate(nspec[:,1],nspec[:,0],ispec[:,0]) return 4.0 * real(sum(sfour1[1:fourlen] * conjugate(sfour2[1:fourlen]) / ispec[1:,1])) * deltaf
def InnerProd(ser1, ser2, PSD): size = Numeric.shape(ser1)[0] pdlen = size/2 nyquistf = 0.5/15.0 # !!! hardcoded !!!! freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) if(Numeric.shape(ser2)[0] != size): print "size of time series must be the same" sys.exit(1) if(Numeric.shape(PSD)[0] != pdlen): print "wrong size of psd: ", pdlen, Numeric.shape(PSD) sys.exit(1) fourier1 = FFT.fft(ser1) fourier2 = FFT.fft(ser2) prod = Numeric.zeros(pdlen+1, dtype='d') prod[0] = 0.0 prod[1:pdlen] = numpy.multiply(fourier1[1:pdlen],numpy.conjugate(fourier2[1:pdlen])) + numpy.multiply(fourier1[-1:pdlen:-1],numpy.conjugate(fourier2[-1:pdlen:-1])) prod[pdlen] = fourier1[pdlen]*fourier2[pdlen] Numeric.divide(prod[1:], PSD, prod[1:]) olap0 = 0.0 for i in xrange(pdlen): if (freqs[i] > 1.e-5 and freqs[i]<0.01): olap0 += prod[i] olap0 = 2.0*olap0/float(size) # olap0 = 2.0*(numpy.sum(prod[1:]))/float(size) #it must be scaled by dt return olap0
def triangularPut(m1d, upper=1, lower=0): """Returns 2D masked array with elements of the given 1D array in the strictly upper (lower) triangle. Elements of the 1D array should be ordered according to the upper triangular part of the 2D matrix. The lower triangular part (if requested) equals to the transposed upper triangular part. If upper == lower == 1 a symetric matrix is returned. """ assert upper in [0,1] and lower in [0,1], "[0|1] expected for upper / lower" m1d = MA.asarray(m1d) assert MA.rank(m1d) == 1, "1D masked array expected" m2dShape0 = math.ceil(math.sqrt(2*m1d.shape[0])) assert m1d.shape[0] == m2dShape0*(m2dShape0-1)/2, "the length of m1d does not correspond to n(n-1)/2" if upper: if lower: mask = Numeric.fromfunction(lambda i,j: i==j, (m2dShape0, m2dShape0)) else: mask = Numeric.fromfunction(lambda i,j: i>=j, (m2dShape0, m2dShape0)) else: if lower: mask = Numeric.fromfunction(lambda i,j: i<=j, (m2dShape0, m2dShape0)) else: mask = Numeric.ones((m2dShape0, m2dShape0)) m2d = MA.ravel(MA.zeros((m2dShape0, m2dShape0), m1d.dtype.char)) condUpperTriang = Numeric.fromfunction(lambda i,j: i<j, (m2dShape0, m2dShape0)) putIndices = Numeric.compress(Numeric.ravel(condUpperTriang), Numeric.arange(0, m2dShape0**2, typecode=Numeric.Int)) MA.put(m2d, putIndices, m1d) m2d = MA.reshape(m2d, (m2dShape0, m2dShape0)) m2d = MA.where(condUpperTriang, m2d, MA.transpose(m2d)) return MA.array(m2d, mask=Numeric.logical_or(mask, MA.getmaskarray(m2d)))
def spect(series,sampling,patches=1,detrend=0,overlap=1,win='triangle'): nyquistf = 0.5 / sampling if patches==0: period = pdg(series) elif patches==1: period = wpdg(series,detrend,win) else: if overlap==0: period = nopwpdg(series,patches,detrend,win) else: period = opwpdg(series,patches,detrend,win) pdlen = Numeric.shape(period)[0]-1 freqs = Numeric.arange(0,pdlen+1,dtype='d') * (nyquistf / pdlen) deltaf = nyquistf / pdlen period[0] = 2 * period[0] / deltaf period[1:pdlen] = period[1:pdlen] / deltaf period[pdlen] = 2 * period[pdlen] / deltaf spectrum = Numeric.zeros((pdlen+1,2),dtype='d') spectrum[:,0] = freqs[:] spectrum[:,1] = period[:] return spectrum
def createLinePlot(self): linePlot = dabo.ui.dLinePlot(self) linePlot.XAxisLabel = "X Axis" linePlot.YAxisLabel = "Y Axis" linePlot.Caption = "Title of Graph" # 1000 points cos function, plotted as blue line linePlot.appendLineFromEquation("2*_Numeric.cos(%s)", 5, 10, Caption="Blue Line", LineWidth=2, LineColor='blue') line = [] for i in range(10): line.append((i, float(i)/2)) linePlot.appendLineFromPoints(line) data1 = 2.*_Numeric.pi*_Numeric.arange(200)/200. data1.shape = (100, 2) data1[:,1] = _Numeric.sin(data1[:,0]) linePlot.appendMarkerFromPoints(data1, Caption='Green Markers', Color='green', MarkerShape='circle', MarkerSize=1) # A few more points... points = [(0., 0.), (_Numeric.pi/4., 1.), (_Numeric.pi/2, 0.), (3.*_Numeric.pi/4., -1)] linePlot.appendMarkerFromPoints(points, Caption='Cross Legend', Color='blue', MarkerShape='cross') linePlot.Draw(linePlot._plotManager) return linePlot
def chipdata(self, data): """Input data: [(dirname0, [et0, et1, ...]), ...] """ self.numRowsMissingChipData = 0 self._chipdataMA = [] if data != None: self._chipdata = data numValsAll = 0 numValsNonMasked = 0 numFiles = 0 numExamplesList = [] attribDict = {} numColMissing = 0 for (name, etList) in data: numFiles += len(etList) self._chipdataMA.append((name, [])) for et in etList: attribDict.update( dict( zip(map(lambda x: x.name, et.domain.attributes), et.domain.attributes))) numExamplesList.append(len(et)) etm = et.toNumpyMA("a")[0] colNonMissingInd = Numeric.compress( Numeric.not_equal(MA.count(etm, 0), 0), Numeric.arange(etm.shape[1]) ) # indices of columns that are not completely missing numColMissing += etm.shape[1] - colNonMissingInd.shape[0] self.numRowsMissingChipData += int( Numeric.add.reduce( Numeric.less( MA.count(etm.take(colNonMissingInd, 1), 1), etm.shape[1]))) numValsAll += int(Numeric.multiply.reduce(etm.shape)) numValsNonMasked += int(MA.count(etm)) self._chipdataMA[-1][1].append(etm) # info text self.infoc.setText( "Structured Data: %i data files with %i profiles on %i points" % (numFiles, numExamplesList[0], len(attribDict))) numTotalMissing = numValsAll - numValsNonMasked if numTotalMissing > 0: print numTotalMissing, numColMissing, self.numRowsMissingChipData print type(numTotalMissing), type(numColMissing), type( self.numRowsMissingChipData) self.infod.setText( "missing %i values, %i column%s completely, %i row%s partially" % (numTotalMissing, numColMissing, [ "", "s" ][numColMissing != 1], self.numRowsMissingChipData, ["", "s"][self.numRowsMissingChipData != 1])) else: self.infod.setText("") else: self._chipdata = None self.infoc.setText("No structured data on input") self.infod.setText("") self.setGuiCommonExpChip() if self.commitOnChange: self.senddata(2)
def loessMA(m, windowSize, axis=0, approxMasked=True, verbose=False, callback=None): """Returns a new array with values at the given axis smoothed by loess; if approxMasked==True: the masked values are approximated by loess; assumes equidistant spacing of points on the given axis. """ assert 0 < windowSize <= m.shape[axis]+0.1, "0 < windowSize[%s] <= 1 OR windowSize in range(1.1,m.shape[axis]+1) expected, got %f" % ("%", windowSize) m = MA.asarray(m) if m.dtype.char <> Numeric.Float: m = m.astype(Numeric.Float) shp_other = list(m.shape) shp_other.pop(axis) # get a transposed and reshaped mask and data from m; if m.mask() == None, construct a new array of zeros mask = Numeric.reshape(Numeric.transpose(MA.getmaskarray(m), [axis] + range(0,axis) + range(axis+1,len(m.shape))), (m.shape[axis], Numeric.multiply.reduce(shp_other))) data = MA.reshape(MA.transpose(m, [axis] + range(0,axis) + range(axis+1,len(m.shape))), (m.shape[axis], Numeric.multiply.reduce(shp_other))) maskInv = -1*(mask-1) xall = Numeric.arange(data.shape[0]) xallList = xall.tolist() for ii in Numeric.compress(Numeric.add.reduce(maskInv,0) > 1, range(data.shape[1])): # run loess if the profile contains more than 2 values try: data[:,ii] = MA.array(statc.loess(zip(MA.compress(maskInv[:,ii], xall).tolist(), MA.compress(maskInv[:,ii], data[:,ii]).tolist()), xallList, windowSize))[:,1] except: if verbose: print "Warning: loessMA: could not loess axis %i index %i" % (axis, ii) if callback: callback() if not approxMasked: data = MA.array(data, mask=mask) return MA.transpose(MA.reshape(data, [m.shape[axis]] + shp_other), [axis] + range(0,axis) + range(axis+1,len(m.shape)))
def group(self, a_indices, maxPerCenter): """ Group a bunch of integers (atom indices in PDBModel) so that each group has at most maxPerCenter items. @param a_indices: atom indices @type a_indices: [int] @param maxPerCenter: max entries per group @type maxPerCenter: int @return: list of lists of int @rtype: [[int],[int]..] """ ## how many groups are necessary? n_centers = len(a_indices) / maxPerCenter if len(a_indices) % maxPerCenter: n_centers += 1 ## how many items/atoms go into each group? nAtoms = N.ones(n_centers, N.Int) * int(len(a_indices) / n_centers) i = 0 while N.sum(nAtoms) != len(a_indices): nAtoms[i] += 1 i += 1 ## distribute atom indices into groups result = [] pos = 0 for n in nAtoms: result += [N.take(a_indices, N.arange(n) + pos)] pos += n return result
def getCTCSS(tone, sampleRate=44100, peak=0.9): if tone in CTCSSTones: tone = CTCSSTones[tone] length = sampleRate / float(tone) omega = Numeric.pi * 2 / length xvalues = Numeric.arange(int(length)) * omega oneCycle = ((peak * 32767) * Numeric.sin(xvalues)).astype(Numeric.Int16) return Numeric.transpose(Numeric.array((oneCycle,oneCycle)))
def getobscount(snum,stime,observables,zerotime=0.0): fullinittime = time() inittime = int(fullinittime) lasttime = 0 print "Processing...", sys.stdout.flush() try: if len(Numeric.shape(observables)) == 0: array = Numeric.zeros(snum,dtype='d') for i in Numeric.arange(0,snum): array[i] = observables(zerotime+i*stime) if i % 1024 == 0: lasttime = dotime(i,snum,inittime,lasttime) else: obslen = Numeric.shape(observables)[0] array = Numeric.zeros((snum,obslen),dtype='d') for i in Numeric.arange(0,snum): for j in xrange(0,obslen): array[i,j] = observables[j](zerotime+i*stime) if i % 1024 == 0: lasttime = dotime(i,snum,inittime,lasttime) except IndexError: print "lisautils::getobsc: I have trouble accessing time ", zerotime+i*stime, print "; you may try to reset your objects and repeat..." raise # there was good stuff here, but it's better to let the user deal with this # in particular, if observables is a noise-like variable, then it should # have the 'reset' method ['reset' in dir(observables)]; if observables is # a method of a TDI class, which should have the 'reset' method, the test # is ['reset' in dir(observables.im_self)], where "im_self" returns the # class instance for a given instance method currenttime = time() - fullinittime if currenttime > 0: vel = snum/currenttime print "\r...completed in %d s [%d (multi)samples/s]. " % (int(currenttime),int(vel)) else: print "\r...completed. " return array
def myrange(a, b, n): from numpy.oldnumeric import arange step = (b - a) / (n - 1) x = arange(a, b + step, step) return x[:n]
def compressIndices(ma): """Returns 1D compressed Numeric array and the indices of the non-masked places. usage: nu,ind = compressIndices(ma) nu = Numeric.elementwise_function(nu) ma = MA.put(ma, ind, nu) """ ma = MA.asarray(ma) nonMaskedInd = Numeric.compress(1-Numeric.ravel(MA.getmaskarray(ma)), Numeric.arange(Numeric.multiply.reduce(ma.shape))) return MA.filled(ma.compressed()), nonMaskedInd
def appendLineFromEquation(self, equation, Start, End, points=1000.0, LineColor='black', LineStyle='solid', LineWidth=1, Caption=""): spacing = (float(End) - float(Start))/float(points) pointList = Start + (_Numeric.arange(points) * spacing) coordinates = [] s = "value = %s" % equation for index in range(len(pointList)): exec(s % pointList[index]) coordinates.append((pointList[index], value)) self.appendLineFromPoints(coordinates, LineColor, LineStyle, LineWidth, Caption)
def rankData(n, inverse=False): """Returns ranks of 1D Numeric array in range 1...shape[0]. """ n = Numeric.asarray(n) assert Numeric.rank(n) == 1 r = Numeric.zeros(n.shape[0], Numeric.Float) Numeric.put(r, Numeric.argsort(n), Numeric.arange(n.shape[0])) if inverse: return -1*r+n.shape[0] else: return r+1
def getobs(snum,stime,observables,zerotime=0.0,display=0,forcepython=0): if len(Numeric.shape(observables)) == 0: obsobj = checkobs([observables]) if obsobj and (not forcepython): array = Numeric.zeros(snum,dtype='d') if display: lisaswig.fastgetobsc(array,snum,stime,obsobj,zerotime) else: lisaswig.fastgetobs(array,snum,stime,obsobj,zerotime) else: if display: return getobscount(snum,stime,observables,zerotime) else: array = Numeric.zeros(snum,dtype='d') for i in Numeric.arange(0,snum): array[i] = observables(zerotime+i*stime) else: obsobj = checkobs(observables) if obsobj and (not forcepython): obslen = Numeric.shape(observables)[0] array = Numeric.zeros((snum,obslen),dtype='d') if display: lisaswig.fastgetobsc(array,snum,stime,obsobj,zerotime) else: lisaswig.fastgetobs(array,snum,stime,obsobj,zerotime) else: if display: return getobscount(snum,stime,observables,zerotime) else: obslen = Numeric.shape(observables)[0] array = Numeric.zeros((snum,obslen),dtype='d') for i in Numeric.arange(0,snum): for j in xrange(0,obslen): array[i,j] = observables[j](zerotime+i*stime) return array
def test_plot(self): """gnuplot.plot test""" # List of (x, y) pairs # plot([(0.,1),(1.,5),(2.,3),(3.,4)]) # plot( zip( range(10), range(10) ) ) # Two plots; each given by a 2d array import numpy.oldnumeric as N x = N.arange(10) y1 = x**2 y2 = (10 - x)**2 plot(N.transpose(N.array([x, y1])), N.transpose(N.array([x, y2])))
def test_plot( self ): """gnuplot.plot test""" # List of (x, y) pairs # plot([(0.,1),(1.,5),(2.,3),(3.,4)]) # plot( zip( range(10), range(10) ) ) # Two plots; each given by a 2d array import numpy.oldnumeric as N x = N.arange(10) y1 = x**2 y2 = (10-x)**2 plot( N.transpose(N.array([x, y1])), N.transpose(N.array([x, y2])))
def loessMA(m, windowSize, axis=0, approxMasked=True, verbose=False, callback=None): """Returns a new array with values at the given axis smoothed by loess; if approxMasked==True: the masked values are approximated by loess; assumes equidistant spacing of points on the given axis. """ assert 0 < windowSize <= m.shape[ axis] + 0.1, "0 < windowSize[%s] <= 1 OR windowSize in range(1.1,m.shape[axis]+1) expected, got %f" % ( "%", windowSize) m = MA.asarray(m) if m.dtype.char <> Numeric.Float: m = m.astype(Numeric.Float) shp_other = list(m.shape) shp_other.pop(axis) # get a transposed and reshaped mask and data from m; if m.mask() == None, construct a new array of zeros mask = Numeric.reshape( Numeric.transpose(MA.getmaskarray(m), [axis] + range(0, axis) + range(axis + 1, len(m.shape))), (m.shape[axis], Numeric.multiply.reduce(shp_other))) data = MA.reshape( MA.transpose(m, [axis] + range(0, axis) + range(axis + 1, len(m.shape))), (m.shape[axis], Numeric.multiply.reduce(shp_other))) maskInv = -1 * (mask - 1) xall = Numeric.arange(data.shape[0]) xallList = xall.tolist() for ii in Numeric.compress( Numeric.add.reduce(maskInv, 0) > 1, range(data.shape[1]) ): # run loess if the profile contains more than 2 values try: data[:, ii] = MA.array( statc.loess( zip( MA.compress(maskInv[:, ii], xall).tolist(), MA.compress(maskInv[:, ii], data[:, ii]).tolist()), xallList, windowSize))[:, 1] except: if verbose: print "Warning: loessMA: could not loess axis %i index %i" % ( axis, ii) if callback: callback() if not approxMasked: data = MA.array(data, mask=mask) return MA.transpose(MA.reshape(data, [m.shape[axis]] + shp_other), [axis] + range(0, axis) + range(axis + 1, len(m.shape)))
def test_Density(self): """Statistics.Density test""" import random ## a lognormal density distribution the log of which has mean 1.0 ## and stdev 0.5 self.X = [(x, p_lognormal(x, 1.0, 0.5)) for x in N.arange(0.00001, 50, 0.001)] alpha = 2. beta = 0.6 self.R = [random.lognormvariate(alpha, beta) for i in range(10000)] p = logConfidence(6.0, self.R)[0] #, area(6.0, alpha, beta)
def colorRange( nColors, palette='plasma2' ): """Quick access to a range of colors. @param nColors: number of colors needed @type nColors: int @param palette: type of color spectrum @type palette: str @return: a range of color values @rtype: [ int ] """ c = ColorSpectrum( palette=palette, vmin=0, vmax=1. ) r = 1. * N.arange( 0, nColors ) / nColors return c.colors( r )
def rankDataMA(m, inverse=False): """Returns ranks of 1D masked array; masked values ignored, range 1...#non-masked_values. """ m = MA.asarray(m) assert MA.rank(m) == 1 fill_val = m.fill_value() m.set_fill_value(MA.maximum(m) + 1) r = MA.zeros(m.shape[0], Numeric.Float) MA.put(r, MA.argsort(m), Numeric.arange(m.shape[0])) m.set_fill_value(fill_val) r = MA.array(r, mask=MA.getmaskarray(m)) if inverse: return -1*r+MA.count(m) else: return r+1
def test_Density(self): """Statistics.Density test""" import random ## a lognormal density distribution the log of which has mean 1.0 ## and stdev 0.5 self.X = [ (x, p_lognormal(x, 1.0, 0.5)) for x in N.arange(0.00001, 50, 0.001)] alpha = 2. beta = 0.6 self.R = [ random.lognormvariate( alpha, beta ) for i in range( 10000 )] p = logConfidence( 6.0, self.R )[0]#, area(6.0, alpha, beta)
def resetAll_cb(self): """Resetting curve as slant line 0 to 255""" self.reset() self.curline=self.canvas.create_line([self.startpoint,self.endpoint],width=1,fill='black') for p in [self.startpoint,self.endpoint]: self.curoval=self.canvas.create_oval(p[0]-2,p[1]-2,p[0]+2,p[1]+2,width=1,outline='black',fill='black') self.curovals.append(self.curoval) self.oldpoints=[self.startpoint,self.endpoint] (self.curx,self.cury)=self.endpoint self.d1scalewheel.set(0.013) if self.continuous or self.mousebuttonup: self.newd1ramp=Numeric.arange(0,256,1,'f') self.callbacks.CallCallbacks(self.newd1ramp) #self.histvar.set(0) self.history=[]
def __vrange( self, v ): """ Interprete the vrange option -> [ int ] or [ float ] @param v: vrange option @type v: lst OR str @return: range option @rtype: [int] OR [float] """ if type( v ) is list: return [ self.__float_int(x) for x in v ] if type( v ) is str and ':' in v: v = tuple( [ self.__float_int(x) for x in v.split(':') ] ) return N.arange( *v ) return self.__float_int( v )
def afterInit(self): # 1000 points cos function, plotted as blue line self.appendLineFromEquation("2*_Numeric.cos(%s)", 5, 10, Caption="Blue Line", LineWidth=2, LineColor="blue") line = [] for i in range(10): line.append((i, float(i)/2)) self.appendLineFromPoints(line) data1 = 2.*_Numeric.pi*_Numeric.arange(200)/200. data1.shape = (100, 2) data1[:,1] = _Numeric.sin(data1[:,0]) self.appendMarkerFromPoints(data1, Caption='Green Markers', Color='green', MarkerShape='circle', MarkerSize=1) # A few more points... points = [(0., 0.), (_Numeric.pi/4., 1.), (_Numeric.pi/2, 0.), (3.*_Numeric.pi/4., -1)] self.appendMarkerFromPoints(points, Caption='Cross Legend', Color='blue', MarkerShape='cross')
def __vrange(self, v): """ Interprete the vrange option -> [ int ] or [ float ] @param v: vrange option @type v: lst OR str @return: range option @rtype: [int] OR [float] """ if type(v) is list: return [self.__float_int(x) for x in v] if type(v) is str and ':' in v: v = tuple([self.__float_int(x) for x in v.split(':')]) return N.arange(*v) return self.__float_int(v)
def getAppxCoef2d_significant(self, arr2d, maxNumCoef, alpha): """Returns[:,j] approx. coeffcients with p-value < alpha; subsequent coef. are equal to 0. Reference: Ott, pp.606. The significance of the coef. estimated by comparing the variance drop of the model2 with the variance of the model1, where: model 1: complete model with maxNumCoef coef. different from 0 model 2: reduced model with coef. in range (0,k) different from 0 null hypothesis (H0): coef. k,k+1,...,maxNumCoef are equal to 0 if H0 rejected (pval below some alpha (e.g. 0.05) -> there exist an important coef. among coef. k,k+1,...,maxNumCoef repeat the test for k=0,1,...maxNumCoef-1 """ assert len(arr2d.shape) == 2, "2d array expected" assert 0 < maxNumCoef <= self.k coefMax = self.getAppxCoef(arr2d, maxNumCoef) curveMax = self.getAppxCurve(coefMax) SSE1 = Numeric.add.reduce((arr2d - curveMax)**2, 1) MSE1 = SSE1 / (arr2d.shape[1] - maxNumCoef) #print "SSE1[0], MSE1[0]",SSE1[0], MSE1[0] pvals = Numeric.zeros((arr2d.shape[0], maxNumCoef), Numeric.Float) for k in range( maxNumCoef): # test cofInd: [maxNum-1, maxNum-2, ..., minNum] #print "Keeping %i coeff" % (k) shpk = list(coefMax.shape) shpk[1] -= k coefk = Numeric.concatenate( (coefMax[:, :k], Numeric.zeros((shpk), Numeric.Float)), 1) curvek = self.getAppxCurve(coefk) SSE2 = Numeric.add.reduce((arr2d - curvek)**2, 1) MSdrop = (SSE2 - SSE1) / (maxNumCoef - k) F = MSdrop / MSE1 #2007-10-11: F -> F.filled(???) pvals[:, k] = scipy.stats.fprob( (maxNumCoef - k), arr2d.shape[1] - maxNumCoef, F.filled(ApproxOrthPolyBasis._F_fillValue)) pvals = Numeric.where( pvals > alpha, Numeric.resize(Numeric.arange(pvals.shape[1]), pvals.shape), pvals.shape[1]) # MAX where significant, idx where nonsignificant firstNonSignIdx = MLab.min(pvals, 1) # idx of the first non-significant coef. coefSign = Numeric.zeros(coefMax.shape, Numeric.Float) for idx in range(coefSign.shape[1]): coefSign[:, idx] = Numeric.where(idx < firstNonSignIdx, coefMax[:, idx], 0) return coefSign
def appendLineFromEquation(self, equation, Start, End, points=1000.0, LineColor='black', LineStyle='solid', LineWidth=1, Caption=""): spacing = (float(End) - float(Start)) / float(points) pointList = Start + (_Numeric.arange(points) * spacing) coordinates = [] s = "value = %s" % equation for index in range(len(pointList)): exec(s % pointList[index]) coordinates.append((pointList[index], value)) self.appendLineFromPoints(coordinates, LineColor, LineStyle, LineWidth, Caption)
def __init__(self, numPoints, k): """numPoints: number of approximation points; k: number of basis functions [2,...,numPoints]""" self.numPoints = numPoints self.k = k ## assert k > 1, "Error TrigonomerticBasis: k <= 1" assert k <= numPoints, "Error TrigonomerticBasis: k > numPoints" # evaluate trigonometric basis functions on the given number of points from [-pi,pi] self.x = Numeric.arange(-1*math.pi, math.pi+0.0000001, 2*math.pi/(numPoints-1)) self.y = Numeric.ones((k, numPoints), Numeric.Float) for kk in range(1, k, 2): ## print "kk, cos %ix" % ((kk+1)/2.) self.y[kk] = MLab.cos(self.x*(kk+1)/2) for kk in range(2, k, 2): ## print "kk, sin %ix" % (kk/2.) self.y[kk] = MLab.sin(self.x*kk/2) # approx. matrix self.Ainv = LinearAlgebra.inverse(Numeric.matrixmultiply(self.y, Numeric.transpose(self.y))) self.yyTinvy = Numeric.matrixmultiply(LinearAlgebra.inverse(Numeric.matrixmultiply(self.y, Numeric.transpose(self.y))), self.y)
def leastsquares(array,detrend=0): S = len(array) x = Numeric.arange(0,S,dtype='d') Sx = Numeric.sum(x) Sy = Numeric.sum(array) Sxx = Numeric.sum(x**2) Sxy = Numeric.sum(x * array) delta = S * Sxx - Sx**2 a = (Sxx * Sy - Sx * Sxy) / delta b = (S * Sxy - Sx * Sy) / delta if detrend: array -= a + b * x return (a,b)
def findNearestAtoms(mol, vertices, **kw): """None <- color(mol,vertices2,**kw) mol: reference molecule vertices: list of lists(coordinates): the first three items in each list must be coordinates x,y,z of a point. atomIndices is the index of the nearest atom to the vertex, such that mol.allAtoms[atomIndices[x]] is the nearest atom to vertices[x] vertexIndices is the list of nearest vertices to an atom, such that vertexIndices[x] = [vertex1,vertex2,...] are the vertices associated with mol.allAtoms[x] """ coords = mol.allAtoms.coords if not hasattr(mol, 'bhtree'): print "Building bhtree for ", mol ids = Numeric.arange(len(coords)).astype('i') bhtree = bhtreelib.TBHTree(coords, ids, 10, 10, 9999.0) mol.bhtree = bhtree vertexIndices = {} atomIndices = {} for x in range(len(coords)): vertexIndices[x + 1] = [] cutoff = 5. for x in range(len(vertices)): xyz = vertices[x] result = Numeric.zeros((len(vertices), )).astype('i') dist = Numeric.zeros((len(vertices), )).astype('f') nb2 = mol.bhtree.ClosePointsDist2(tuple(xyz[:3]), cutoff, result, dist) while nb2 == 0: cutoff = cutoff + 5. nb2 = mol.bhtree.ClosePointsDist2(tuple(xyz[:3]), cutoff, result, dist) result = result[:nb2] dist = dist[:nb2] idx = dist.tolist().index(min(dist)) atnum = result[idx] + 1 atomIndices[x] = atnum vertexIndices[atnum].append(x) return atomIndices, vertexIndices
def data(self, data): if data != None: self._data = data ## self._dataMA = chipstat.orng2ma(data) self._dataMA = data.toNumpyMA("a")[0] # info text self.infoa.setText("Examples: %i profiles on %i points" % (self._dataMA.shape[0], self._dataMA.shape[1])) numTotalMissing = int( Numeric.multiply.reduce(self._dataMA.shape) - MA.count(self._dataMA)) if numTotalMissing > 0: numValsByCol = MA.count(self._dataMA, 0) numEmptyCol = Numeric.add.reduce( Numeric.where(numValsByCol == 0, 1, 0)) colNonEmpty = Numeric.compress( numValsByCol != 0, Numeric.arange(self._dataMA.shape[1])) dataRemEmptyCol = self._dataMA.take(colNonEmpty, 1) self.numRowsMissing = Numeric.add.reduce( Numeric.where( MA.count(dataRemEmptyCol, 1) < dataRemEmptyCol.shape[1], 1, 0)) s1 = "" s2 = "" if numEmptyCol > 0: s1 = "s" if self.numRowsMissing > 0: s2 = "s" self.infob.setText( "missing %i values, %i column%s completely, %i row%s partially" % (numTotalMissing, numEmptyCol, s1, self.numRowsMissing, s2)) else: self.infob.setText("") else: self._data = None self._dataMA = None self.infoa.setText("No examples on input") self.infob.setText("") self.numRowsMissing = 0 self.setGuiCommonExpChip() if self.commitOnChange: self.senddata(1)
def __init__(self, numPoints, k): """numPoints: number of approximation points; k: number of basis functions [2,...,numPoints]""" self.numPoints = numPoints self.k = k ## assert k > 1, "Error TrigonomerticBasis: k <= 1" assert k <= numPoints, "Error TrigonomerticBasis: k > numPoints" # evaluate trigonometric basis functions on the given number of points from [-pi,pi] self.x = Numeric.arange(-1 * math.pi, math.pi + 0.0000001, 2 * math.pi / (numPoints - 1)) self.y = Numeric.ones((k, numPoints), Numeric.Float) for kk in range(1, k, 2): ## print "kk, cos %ix" % ((kk+1)/2.) self.y[kk] = MLab.cos(self.x * (kk + 1) / 2) for kk in range(2, k, 2): ## print "kk, sin %ix" % (kk/2.) self.y[kk] = MLab.sin(self.x * kk / 2) # approx. matrix self.Ainv = LinearAlgebra.inverse( Numeric.matrixmultiply(self.y, Numeric.transpose(self.y))) self.yyTinvy = Numeric.matrixmultiply( LinearAlgebra.inverse( Numeric.matrixmultiply(self.y, Numeric.transpose(self.y))), self.y)