def solint_numpy_countbin(dsref):
start = time.time()
dsref.as_numarray()
# get the unique time stamps
tms = numpy.unique(dsref.x)
# check if there is something to be averaged at all
if len(tms)==len(dsref.x):
return time.time() - start
# "bins" will be the destination bin where the quantity
# will be summed into for each unique time stamp
# i.e. all data having time stamp tms[0] will be summed into
# bin 0, all data having time stamp tms[x] will be summed
# into bin x
#bins = range( len(tms) )
# Now we must transform the array of times (dsref.x) into an
# array with bin indices
dests = reduce(lambda acc, (ix, tm): \
numpy.put(acc, numpy.where(dsref.x==tm), ix) or acc, \
enumerate(tms), \
numpy.empty(dsref.x.shape, dtype=numpy.int32))
# Good, now that we have that ...
sums = numpy.bincount(dests, weights=dsref.y)
count = numpy.bincount(dests)
dsref.y = sums/count
dsref.x = tms
return time.time() - start
评论列表
文章目录