Extending Scientific.Statistics.Histogram...

bleh google.pobox at pobox.com
Wed Nov 12 22:10:33 CET 2003


google.pobox at pobox.com (bleh) wrote in message news:<bb58e676.0311111429.68f3fd2f at posting.google.com>...
> ...to include a removeData(datatoremove) function, to mirror the
> existing addData(datatoadd) function.  If anybody knows of somewhere
> where this has been done already, if you could point me in that
> direction I'd be much obliged...
> 
> TIA

NM -- I figured out the (embarrassingly, ridiculously easy) solution
on my own.  Here it is, in case anybody wants it...




import Numeric; N = Numeric
from Scientific.Statistics.Histogram import Histogram

class ExtendedHistogram(Histogram):
  def __init__(self, data, nbins, range=None):
    Histogram.__init__(self, data, nbins, range)
    self._setup(data, nbins, range)
    self.addData(data)

  def removeData(self, data):
    """
    Remove the values in |data| (a sequence of numbers) from the
    originally supplied data. Note that this does not affect the
    default range of the histogram, which is fixed when the
    histogram is created.
    """
    n = (len(data)+999)/1000
    for i in range(n):
        self._removeData(data[1000*i:1000*(i+1)])

  def _removeData(self, data):
    data = N.array(data, N.Float)
    data = N.repeat(data, N.logical_and(N.less_equal(data, self.max), 
                                     N.greater_equal(data, self.min)))
    data = N.floor((data - self.min)/self.bin_width).astype(N.Int)
    nbins = self.array.shape[0]
    histo = N.add.reduce(N.equal(N.arange(nbins)[:,N.NewAxis],
                                                          data), -1)
    histo[-1] = histo[-1] + N.add.reduce(N.equal(nbins, data))
    # this next line is the only change... changed the "+" to a "-"
    self.array[:, 1] =  self.array[:, 1] - histo




More information about the Python-list mailing list