def calculateNorm(self,brightarray,darkarray,scale=None):
                '''
                caculating norm array from bright and dark array.  A scale
                for the dark array can be specified or calculated.  For most
                case, scale of 1 is good enough if the exposure time of the
                bright and dark are equal.  If Gram-Schmidt process is used
                to calculate dark_scale on the data, normarray need to be
                scaled the same by specifying it.
                '''
                if scale:
                                dark_scale = scale
                else:
                        dark_scale = 1.0
                normarray = brightarray - dark_scale * darkarray
                normarray = numpy.asarray(normarray, numpy.float32)

                # division may result infinity or zero division
                # so make sure there are no zeros in norm
                normarray = numpy.clip(normarray, 0.001, sys.maxint)
                stats = numextension.allstats(normarray, mean=True)
                normavg = stats['mean']
                normarray = normavg / normarray
                # Avoid over correcting dead pixels
                normarray = numpy.ma.masked_greater(normarray,20).filled(1)
                return normarray
 def calculateDarkScale(self,rawarray,darkarray):
         '''
         Calculate the scale used for darkarray using
         Gram-Schmidt process for very low signal-to-noise
         ratio such as DD raw frames as suggested by Niko Grigoreiff.
         Need to apply the same factor used in data dark subtraction as
         in dark subtraction for the normarray
         '''
         onedshape = rawarray.shape[0] * rawarray.shape[1]
         a = rawarray.reshape(onedshape)
         b = darkarray.reshape(onedshape)
         a_std = numextension.allstats(a,std=True)['std']
         b_std = numextension.allstats(b,std=True)['std']
         if b_std == 0:
                 return 1.0
         ab_corr_coef = numpy.corrcoef(a,b)[(0,1)]
         dark_scale = ab_corr_coef * a_std / b_std
         return dark_scale
 def wrapped(a):
     # we cannot remove the numextension requirement here, see Bug #1105
     # which is unfortunate, because our simple mrc.py requires numextension
     try:
         import numextension
         result = numextension.allstats(a, **kwargs)
     except:
         b = numpy.asanyarray(a, dtype=numpy.float64)
         result = allstats(b, **kwargs)
     if stat != 'all':
         result = result[stat]
     return result
 def wrapped(a):
         # we cannot remove the numextension requirement here, see Bug #1105
         # which is unfortunate, because our simple mrc.py requires numextension
         try:
                 import numextension
                 result = numextension.allstats(a, **kwargs)
         except:
                 b = numpy.asanyarray(a, dtype=numpy.float64)
                 result = allstats(b, **kwargs)
         if stat != 'all':
                 result = result[stat]
         return result
'''

import numpy
import numextension
import os

print 'Demonstration of how allstats is better:'
print ''

a = 500 * numpy.ones((1000,1000), dtype=numpy.float32)
print 'Here is a type float32 array where all elements are set to 500.0:'
print a
print ''

numpy_mean = a.mean()
allstats_result = numextension.allstats(a, mean=True)
allstats_mean = allstats_result['mean']
print 'mean calculated by numpy:  %s' % (numpy_mean,)
print 'mean calculated by allstats:  %s' % (allstats_mean,)
print ''

pid = os.getpid()

print 'Now we will test the memory usage issue.'
print 'You should begin monitoring the memory usage of this process (pid=%d).' % (pid,)
print 'For example, in another terminal, run this:'
print '    top -d 0.5 -b -p %d | grep %d' % (pid,pid)
raw_input('Hit enter to create a large array in memory...')

a = numpy.ones((200, 1024, 1024), dtype=numpy.int16)
memsize = a.size * a.itemsize