def spot_mask(self):
     from numpy import zeros,sum,uint32
     from peak_integration import spot_mask
     from numimage import numimage
     images = self.images
     sum_image = sum(images.astype(uint32),axis=0)
     info("Peak mask of summed image...")
     mask = spot_mask(sum_image)
     mask = numimage(mask)
     mask.filename = self.spot_mask_filename
     return mask
    def diffraction_spots_of_image(self,image_file):  
        from peak_integration import spot_mask 
        from numimage import numimage
        from scipy.ndimage.measurements import label
        from os.path import basename

        I = numimage(image_file)
        ROIX,ROIY,WIDTH = self.ROIX,self.ROIY,self.WIDTH
        I = I[ROIX:ROIX+WIDTH,ROIY:ROIY+WIDTH]  # part of image
        mask = spot_mask(I+20)
        ##total_spots = mask.sum()
        labelled_mask,total_spots = label(mask)
        if total_spots > 0: debug("%s: %s spots" % (basename(image_file),total_spots))
        return total_spots
def show_image(image_file):
    from time import time
    from pylab import figure,imshow,title,show,cm
    from numimage import numimage
    from peak_integration import spot_mask,peak_integration_mask
    from numpy import minimum
    from scipy.ndimage.measurements import label

    I0 = numimage(image_file)
    ROIX,ROIY,WIDTH = sample_frozen.ROIX,sample_frozen.ROIY,sample_frozen.WIDTH
    I = I0[ROIX:ROIX+WIDTH,ROIY:ROIY+WIDTH]

    # Time the 'peak_integration_mask' function.
    t0 = time()
    mask = spot_mask(I+20)
    info('spot_mask = %.3f [s]' %(time()-t0))

    ##N_spots = mask.sum()
    labelled_mask,N_spots = label(mask)

    # Display the image.
    chart = figure(figsize=(8,8))
    title("%s: %d spots" % (image_file,N_spots))
    imshow(minimum(I,1000).T,cmap=cm.jet,origin='upper',interpolation='nearest')
 
    if N_spots != 0: 
        # Spot integration
        t0 = time()
        SB_mask = peak_integration_mask(I)    
        t1 = time()
        print "Time to find Spots and generate S_mask (s):",t1-t0

        # Perform the spot integration.
        print "Integrated intensity: ",sum(I*SB_mask)
        
        # Display 'mask'
        chart = figure(figsize=(8,8))
        title('SB_mask')
        imshow(SB_mask.T,cmap=cm.jet,origin='upper',interpolation='nearest')

    show()    
 def crystal_mask(self):
     """bitmap showing location of crystals. 1 = crystal, 0 = no crystal"""
     from peak_integration import spot_mask
     FOM = self.FOM_image
     mask = spot_mask(FOM,self.peak_detection_threshold)
     return mask