예제 #1
0
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]))
예제 #2
0
    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 = N0.ones(n_centers, N0.Int) * int(len(a_indices) / n_centers)
        i = 0
        while N0.sum(nAtoms) != len(a_indices):
            nAtoms[i] += 1
            i += 1

        ## distribute atom indices into groups
        result = []
        pos = 0
        for n in nAtoms:
            result += [N0.take(a_indices, N0.arange(n) + pos)]
            pos += n

        return result
예제 #3
0
파일: gnuplot.py 프로젝트: tybiot/biskit
    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 Biskit.oldnumeric as N0
        x = N0.arange(10)
        y1 = x**2
        y2 = (10 - x)**2
        plot(N0.transpose(N0.array([x, y1])), N0.transpose(N0.array([x, y2])))
예제 #4
0
    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 N0.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)
예제 #5
0
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. * N0.arange( 0, nColors ) / nColors

    return c.colors( r )
예제 #6
0
    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 N0.arange(*v)

        return self.__float_int(v)
예제 #7
0
def logConfidence(x, R, clip=1e-32):
    """
    Estimate the probability of x NOT beeing a random observation from a
    lognormal distribution that is described by a set of random values.
    The exact solution to this problem is in L{Biskit.Statistics.lognormal}.

    @param x: observed value
    @type  x: float
    @param R: sample of random values; 0 -> don't clip (default: 1e-32)
    @type  R: [float]
    @param clip: clip zeros at this value
    @type  clip: float

    @return:  confidence that x is not random, mean of random distrib.
    @rtype: (float, float)
    """
    if clip and 0 in R:
        R = N0.clip(R, clip, max(R))
    ## get mean and stdv of log-transformed random sample
    mean = N0.average(N0.log(R))

    n = len(R)

    stdv = N0.sqrt(N0.sum(N0.power(N0.log(R) - mean, 2)) / (n - 1.))

    ## create dense lognormal distribution representing the random sample
    stop = max(R) * 50.0
    step = stop / 100000
    start = step / 10.0

    X = [(v, p_lognormal(v, mean, stdv)) for v in N0.arange(start, stop, step)]

    ## analyse distribution
    d = Density(X)

    return d.findConfidenceInterval(x * 1.0)[0], d.average()
예제 #8
0
    def test_hist(self):
        """hist test"""
        self.x = N0.arange(4, 12, 1.2)
        self.data = density(self.x, 3, hist=1)

        self.assert_(N.all(self.data == self.EXPECT))