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): """ 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): """ 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 area(curve, start=0.0, stop=1.0): """ Numerically add up the area under the given curve. The curve is a 2-D array or list of tupples. The x-axis is the first column of this array (curve[:,0]). (originally taken from Biskit.Statistics.ROCalyzer) @param curve: a list of x,y coordinates @type curve: [ (y,x), ] or N.array @param start: lower boundary (in x) (default: 0.0) @type start: float @param stop: upper boundary (in x) (default: 1.0) @type stop: float @return: the area underneath the curve between start and stop. @rtype: float """ ## convert and swap axes curve = N.array(curve) c = N.zeros(N.shape(curve), curve.dtype) c[:, 0] = curve[:, 1] c[:, 1] = curve[:, 0] assert len(N.shape(c)) == 2 ## apply boundaries ## here we have a problem with flat curves mask = N.greater_equal(c[:, 1], start) mask *= N.less_equal(c[:, 1], stop) c = N.compress(mask, c, axis=0) ## fill to boundaries -- not absolutely accurate: we actually should ## interpolate to the neighboring points instead c = N.concatenate((N.array([ [c[0, 0], start], ]), c, N.array([ [c[-1, 0], stop], ]))) x = c[:, 1] y = c[:, 0] dx = x[1:] - x[:-1] # distance on x between points dy = y[1:] - y[:-1] # distance on y between points areas1 = y[:-1] * dx # the rectangles between all points areas2 = dx * dy / 2.0 # the triangles between all points return N.sum(areas1) + N.sum(areas2)
def area(curve, start=0.0, stop=1.0 ): """ Numerically add up the area under the given curve. The curve is a 2-D array or list of tupples. The x-axis is the first column of this array (curve[:,0]). (originally taken from Biskit.Statistics.ROCalyzer) @param curve: a list of x,y coordinates @type curve: [ (y,x), ] or N.array @param start: lower boundary (in x) (default: 0.0) @type start: float @param stop: upper boundary (in x) (default: 1.0) @type stop: float @return: the area underneath the curve between start and stop. @rtype: float """ ## convert and swap axes curve = N.array( curve ) c = N.zeros( N.shape(curve), curve.dtype ) c[:,0] = curve[:,1] c[:,1] = curve[:,0] assert len( N.shape( c ) ) == 2 ## apply boundaries ## here we have a problem with flat curves mask = N.greater_equal( c[:,1], start ) mask *= N.less_equal( c[:,1], stop ) c = N.compress( mask, c, axis=0 ) ## fill to boundaries -- not absolutely accurate: we actually should ## interpolate to the neighboring points instead c = N.concatenate((N.array([[c[0,0], start],]), c, N.array([[c[-1,0],stop ],])) ) x = c[:,1] y = c[:,0] dx = x[1:] - x[:-1] # distance on x between points dy = y[1:] - y[:-1] # distance on y between points areas1 = y[:-1] * dx # the rectangles between all points areas2 = dx * dy / 2.0 # the triangles between all points return N.sum(areas1) + N.sum(areas2)
def filterRange(self, key, vLow, vHigh): """ Get indices of items where vLow <= item[ key ] <= vHigh. @param key: item attribute @type key: any @param vLow: lower bound @type vLow: any @param vHigh: upper bound @type vHigh: any @return: array of int @rtype: array """ vLst = self.valuesOf(key) maskL = N.greater_equal(vLst, vLow) maskH = N.less_equal(vLst, vHigh) return N.nonzero(maskL * maskH)
def updateSelectorInfos(self, selectorIdx=None): """updates the number of examples that match individual selectors; if selectorIdx is given, updates only the corresponding info. """ if not selectorIdx: selectorInd = range(3) else: selectorInd = [selectorIdx] alphas = [self.alphaA, self.alphaB, self.alphaI] for si in selectorInd: try: alpha = float(alphas[si]) ps = self.ps[si] except: alpha = None ps = None if ps != None and alpha != None and self.anovaType in [[0,1,3,4],[2,3,4],[4]][si]: numSelected = Numeric.add.reduce(Numeric.less_equal(self.ps[si], alpha)) self.lblNumGenes[si].setText(' (%d example%s)' % (numSelected, ['', 's'][numSelected!=1])) else: self.lblNumGenes[si].setText(' (no examples)')
def filterRange( self, infoKey, vLow, vHigh ): """ Get indices of Complexes where vLow <= c.info[ infoKey ] <= vHigh. Use:: filterRange( str_infoKey, vLow, vHigh ) @param infoKey: key for info dict @type infoKey: str @param vLow: upper value limit @type vLow: float @param vHigh: lower value limit @type vHigh: float @return: array of int @rtype: [int] """ vLst = self.valuesOf( infoKey ) maskL = N.greater_equal( vLst, vLow ) maskH = N.less_equal( vLst, vHigh ) return N.nonzero( maskL * maskH )
if (len(t1) != nsources): self.logfile.write("Catalog dimension mismatch: ", str(len(t1)), ' ', nsources) self.logfile.write( "Check patrameters in detectionCatalog.inpar and filterCatalog.inpar" ) #flux[i,:],fluxerr[i,:] = tableio.get_data(catalog,self.fluxColumns) flux[i, :], fluxerr[i, :] = t1, t2 flux[i, :] = pUtil.deNAN(flux[i, :]) # Those objects with flux equal or less than 0 are assigned a magnitude of 99 # and a limiting magnitude equal to their SExtractor photometric error. This # is interpreted by BPZ as a nondetection with zero flux and 1-sigma error # equal to the limiting magnitude nondetected = Numeric.less_equal( flux[i, :], 0.0) * Numeric.greater(fluxerr[i, :], 0.0) # Those objects with error flux and flux equal to 0 are assigned a magnitude of -99 # and a flux of 0, which is interpreted by SExtractor as a non-observed object nonobserved = Numeric.less_equal(fluxerr[i, :], 0.0) # When flux error > 100*(flux), mark as nonobserved (Benitez, 24-Oct-03). nonobserved = Numeric.where( fluxerr[i, :] > 100 * (abs(flux[i, :])), 1.0, nonobserved[:]) detected = Numeric.logical_not(nonobserved + nondetected) # Get the zero point for the final magnitudes
self.fluxColumns = tuple(fluxList) # the get_data function interface requires a tuple # Build the various columns arrays with the get_data function. # We read raw fluxes and errors into the flux,fluxerr arrays. # They are afterwards transformed to magnitudes pdb.set_trace() #xingxing flux[i,:],fluxerr[i,:] = tableio.get_data(catalog,self.fluxColumns) flux[i,:] = pUtil.deNAN(flux[i,:]) # Those objects with flux equal or less than 0 are assigned a magnitude of 99 # and a limiting magnitude equal to their SExtractor photometric error. This # is interpreted by BPZ as a nondetection with zero flux and 1-sigma error # equal to the limiting magnitude nondetected = Numeric.less_equal(flux[i,:],0.0)*Numeric.greater(fluxerr[i,:],0.0) # Those objects with error flux and flux equal to 0 are assigned a magnitude of -99 # and a flux of 0, which is interpreted by SExtractor as a non-observed object nonobserved = Numeric.less_equal(fluxerr[i,:],0.0) # When flux error > 100*(flux), mark as nonobserved (Benitez, 24-Oct-03). nonobserved = Numeric.where(fluxerr[i,:] > 100*(abs(flux[i,:])),1.0,nonobserved[:]) detected = Numeric.logical_not(nonobserved+nondetected) # Get the zero point for the final magnitudes zpoint = fUtil.zeroPoint(fitsfile) # pass the fits file to zeroPoint func
def senddata(self): """computes selectionList, partitions the examples and updates infoc; sends out selectionList and selected/other dataStructure or None; """ ## if self.dataStructure and self.ps: if self.dataStructure and self.ps.shape[1]: # set selectionList alphas = [self.alphaA, self.alphaB, self.alphaI] selectors = [self.selectorA, self.selectorB, self.selectorI] selectionList = Numeric.ones((self.numExamples,)) for si in range(3): try: if selectors[si] and self.anovaType in [[0,1,3,4],[2,3,4],[4]][si]: selectionList = Numeric.logical_and(selectionList, Numeric.less_equal(self.ps[si], float(alphas[si]))) except: pass self.infoc.setText('Sending out data...') if self.sendProbabilities: # create example table with probabilities print self.ps print Numeric.transpose(self.ps).shape etProb = orange.ExampleTable(orange.Domain([orange.FloatVariable("Factor A p-val"),orange.FloatVariable("Factor B p-val"),orange.FloatVariable("Interaction p-val")]), Numeric.transpose(self.ps)) # in etProb, convert p-val to meta attribute domProb = orange.Domain([]) domProb.addmetas(dict(zip([orange.newmetaid(),orange.newmetaid(),orange.newmetaid()], etProb.domain.variables))) etProb = orange.ExampleTable(domProb, etProb) else: # create new etProb without attributes/metas and of length equal to etProb etProb = orange.ExampleTable(orange.Domain([]), Numeric.zeros((selectionList.shape[0],0))) # partition dataStructure and send out data selectionList = selectionList.tolist() self.send("Example Selection", (self.selectorName, selectionList)) dataStructS = [] dataStructN = [] self.progressBarInit() if self.sendNotSelectedData: pbStep = 50./len(self.dataStructure) else: pbStep = 100./len(self.dataStructure) for (dsName, etList) in self.dataStructure: etListS = [et.select(selectionList) for et in etList] for i in range(len(etList)): # append probabilities (if etProb not empty) etListS[i] = orange.ExampleTable([etListS[i], etProb.select(selectionList)]) # add name etListS[i].name = etList[i].name dataStructS.append((dsName, etListS)) self.progressBarAdvance(pbStep) self.send("Selected Structured Data", dataStructS) if self.sendNotSelectedData: for (dsName, etList) in self.dataStructure: etListN = [et.select(selectionList, negate=1) for et in etList] for i in range(len(etList)): # append probabilities (if etProb not empty) etListN[i] = orange.ExampleTable([etListN[i], etProb.select(selectionList, negate=1)]) # add name etListN[i].name = etList[i].name dataStructN.append((dsName, etListN)) self.progressBarAdvance(pbStep) self.send("Other Structured Data", dataStructN) else: self.send("Other Structured Data", None) self.progressBarFinished() # report the number of selected examples numExamples = Numeric.add.reduce(Numeric.greater(selectionList, 0)) self.infoc.setText('Total of %d example%s match criteria.' % (numExamples, ['', 's'][numExamples!=1])) else: self.send("Example Selection", None) self.send("Selected Structured Data", None) self.send("Other Structured Data", None)
def decompose_classes(entry, bounds, atom_type='H',molType='protein'): printDebug = 0 if molType == 'protein': second_keys = ('C', 'E', 'H', 'G', 'T') else: second_keys = ('X',) d = {} for amino_acid, shift_dict in entry.items(): for atoms, values in shift_dict.items(): if atoms[0][0] <> atom_type: continue _shifts, _exposure, second = values for ss_key in second_keys: #if amino_acid == 'Arg' and ss_key == 'C' and atoms[0][:2] == 'HB': # printDebug = True #else: # printDebug = False mask = setMask(second,ss_key) # Doesn't work with strings #mask = Numeric.equal(second, ss_array) #print mask shifts = Numeric.compress(mask, _shifts) if not shifts.any(): continue exposure = Numeric.compress(mask, _exposure) key = amino_acid, atoms, ss_key #if printDebug: # print mask # print shifts # print exposure # print ## decompose wrt exposure. this assumes that ## exposure and shifts are sorted (ascending) wrt ## exposures. B = bounds.get(key, None) #if printDebug: # print "B = ",B if not B: #if printDebug: # print key, bounds.keys() continue listLastIndex = len(B) - 1 for i in range(len(B)): #if printDebug: # print i, B[i], exposure # Note: this line changed to less_equal, otherwise values were ignored! Wim 01/12/2009 #if printDebug: # tt = Numeric.less_equal(exposure, B[i]) # print tt # print "DONE1" # print Numeric.nonzero(tt) # print "DONE2" ind = Numeric.nonzero(Numeric.less_equal(exposure, B[i])) # Hack - reset to all elements if some of the upper ones are missing. Happens occasionally. if i == listLastIndex and ((not ind.any() and shifts.any()) or (ind[-1] + 1) != len(shifts)): ind = range(len(shifts)) if printDebug: print " Warning: resetting final class to include all values for %s, %s, %s" % (amino_acid,ss_key,atoms) #if printDebug: # print i, ind if not len(ind): #if printDebug: # print "Cont" # print continue d[key + (i,)] = Numeric.take(shifts, ind) shifts = shifts[ind[-1]+1:] exposure = exposure[ind[-1]+1:] #if printDebug: # print shifts,exposure # print d[key + (i,)] # print if not exposure.any(): #if printDebug: # print "Break" # print break return d