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 = N0.array(data, N0.Float) if range is None: min = N0.minimum.reduce(data) max = N0.maximum.reduce(data) else: min, max = range data = N0.repeat( data, N0.logical_and(N0.less_equal(data, max), N0.greater_equal(data, min))) bin_width = (max - min) / nbins data = N0.floor((data - min) / bin_width).astype(N0.Int) histo = N0.add.reduce(N0.equal(N0.arange(nbins)[:, N0.NewAxis], data), -1) histo[-1] = histo[-1] + N0.add.reduce(N0.equal(nbins, data)) bins = min + bin_width * (N0.arange(nbins) + 0.5) return N0.transpose(N0.array([bins, histo]))
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 = N0.greater_equal(vLst, vLow) maskH = N0.less_equal(vLst, vHigh) return N0.nonzero(maskL * maskH)
def confidenceInterval(self, level): """ confidenceInterval(self, level) @param level: confidence level (e.g. 0.68 for stdev interval) @type level: float @return: start and end of the confidence interval containing |level|*100 % of the probability @rtype: float, float """ order = N0.argsort(self.p).tolist() cumulative = N0.add.accumulate(N0.take(self.p, order)) * self.delta_x ind = N0.nonzero(N0.greater_equal(cumulative, 1. - level)) sub_set = order[ind[0]:] intervals = self.__find_intervals(sub_set) boundaries = [(self.x[i[0]], self.x[i[-1]]) for i in intervals] return tuple(boundaries)
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 = N0.greater_equal( vLst, vLow ) maskH = N0.less_equal( vLst, vHigh ) return N0.nonzero( maskL * maskH )
def findConfidenceInterval(self, x): """ findConfidenceInterval(self, x) Find the smallest possible density interval that still includes x. @param x: value @type x: float @return: convidence level, interval start and end @rtype: float, (float,float) """ closest = N0.argmin(abs(self.x - x)) ind = N0.nonzero(N0.greater_equal(self.p, self.p[closest])).tolist() intervals = self.__find_intervals(ind) ## lens = N0.array([len(i) for i in intervals]) levels = [N0.sum(N0.take(self.p, i)) for i in intervals] level = N0.sum(levels) * self.delta_x boundaries = [(self.x[i[0]], self.x[i[-1]]) for i in intervals] return level, tuple(boundaries)