Exemplo n.º 1
0
    def __init__(self, imagename, psfname=None, sourcefinder_name='pybdsm',
	             saveformat="gaul", makeplots=True, do_psf_corr=True, 
				 do_local_var=True, psf_corr_region=5, local_var_region=10,
				 rel_excl_src=None, pos_smooth=2, neg_smooth=2, loglevel=0, 
				 thresh_pix=5, thresh_isl=3, neg_thresh_isl=3,
				 neg_thresh_pix=5, reset_rel=None, prefix=None, 
				 do_nearsources=False, savefits=False, 
				 increase_beam_cluster=False, savemask_pos=False,
				 savemask_neg=False, no_smooth=True, **kw):

        """ Takes in image and extracts sources and makes 
            reliability estimations..
           
		
        imagename: Fits image
        psfname: PSF fits image, optional. 

        sourcefinder_name: str, optional. Default 'pybdsm'.
            Uses source finder specified.

        makeplots: bool, optional. Default is True.
            Make reliability plots.

        do_psf_corr : bool, optional. Default True.
            If True, PSF correlation will be added
            as an extra parameter for density estimations.
            NB: the PSF fits image must be provided.

        do_local_var : bool, optional. Default is True.
            If True, adds local variance as an extra parameter,
            for density estimations. 
        
        do_nearsources: boolean. Default is False.
            If true it adds number of nearest neighnours as an extra
            parameter. It looks for sources around 5 beam sizes.

        psf_corr_region : int, optional. Default value is 5. 
            Data size to correlate around a source, in beam sizes.
 
        local_var_region: int, optional. Default 10.
            Data size to compute the local variance in beam sizes.

        rel_excl_src : floats, optional. Default is None. 
            Excludes sources in a specified region
            e.g ra, dec, radius in degrees. For
            2 regions: ra1, dec1, radius1: ra2, dec2, radius2, etc.

        pos_smooth : float, optional. Default 2.
            Masking threshold for the positive image.
            For default value 2, data peaks < 2 * image noise
            are masked.

        neg_smooth : float, optional. Default 2.
            Similar to pos_smooth but applied to the negative image.

        thresh_isl :  float, optional. Default is 3.
            Threshold for forming islands in the positive image

        thresh_pix : float, optional. Default is 5.
            Threshold for model fitting, in positive image.

        neg_thresh_isl : float, optional. Default is 3. 
            Simialr to thresh_isl but for negative image.

        neg_thresh_pix : float, optional. Default is 5. 
            Similar to thresh_pix but for negative image.

        savefits: boolean. Default is False.
            If True a negative image is saved.

        reset_rel: boolean. Default is False. If true then
            sources with correlation < 0.002 and rel >0.60
            have their reliabilities set to 0.

        increase_beam_cluster: boolean, optional. If True, sources
            groupings will be increase by 20% the beam size. If False,
            the actual beam size will be used. Default is False.

        savemask_pos: boolean, optional. If true the mask applied on 
            the positive side of an image after smoothing is saved.
            
        savemask_neg: Similar to savemask_pos but for the negative
            side of an image.
        
        loglevel : int, optional. Default is 0.
            Provides Pythonlogging options, 0, 1, 2 and 3 are for info, debug,
            error and critial respectively.
   
         kw : kward for source extractions. Should be a mapping e.g
            kw['thresh_isl'] = 2.0 or kw['do_polarization'] = True 
        """


        #
        self.smoothing = not no_smooth
       
        self.prefix = prefix

        # log level  
        self.loglevel = loglevel
        self.log = utils.logger(self.loglevel, prefix=self.prefix)

        
        # image, psf image
        self.imagename = imagename
        self.psfname = psfname 

        with pyfits.open(imagename) as hdu:
            self.header = hdu[0].header
            self.wcs = WCS(self.header, mode="pyfits")
            self.pixelsize = abs(self.header["cdelt1"])
      
        self.bmaj = numpy.deg2rad(self.header["BMAJ"])

        # boolean optionals    
        self.makeplots = makeplots
        self.do_local_var = do_local_var
        self.nearsources = do_nearsources
        self.do_psf_corr = do_psf_corr
        self.savemaskpos = savemask_pos
        self.savemaskneg = savemask_neg
        self.savefits = savefits
        self.derel = reset_rel
        self.log.info("Catalogues will be saved as %s, where srl is source "
					  " and gaul is Gaussians. "%saveformat)
        self.catalogue_format = "." + saveformat
        if not self.psfname:
            self.log.info(" No psf provided, do_psf_corr is set to False.")
            self.do_psf_corr = False

 
        # computing negative noise
        self.noise, self.mean = utils.negative_noise(self.imagename, self.prefix)
        
        self.log.info(" The negative noise is %e Jy/beam"%self.noise)
        if self.noise == 0: 
            self.log.debug(" The negative noise is 0, check image")

        # source finder initialization
        self.sourcefinder_name  = sourcefinder_name
        self.log.info(" Using %s source finder to extract the sources."%
                      self.sourcefinder_name)

        self.negimage = self.prefix + "_negative.fits"
        utils.invert_image(self.imagename, self.negimage)  
       
        # smoothing factors
        self.pos_smooth = pos_smooth
        self.neg_smooth = neg_smooth
        
        # region to evaluate
        self.corrstep = psf_corr_region
        self.localstep = local_var_region
        self.radiusrm = rel_excl_src
        self.do_beam = increase_beam_cluster
         
        beam_pix = int(round(numpy.rad2deg(self.bmaj)/self.pixelsize))
        self.locstep = self.localstep * beam_pix
        self.cfstep = self.corrstep * beam_pix
        self.bmin, self.bpa =  self.header["BMIN"], self.header["BPA"]

        self.opts_pos = {}
        if self.do_beam:
            bmaj = self.header["BMAJ"]
            self.opts_pos["beam"] = (1.2*bmaj, 1.2*self.bmin, self.bpa)

        # Pybdsm or source finder fitting thresholds
        self.thresh_isl = thresh_isl
        self.thresh_pix = thresh_pix
        self.opts_pos = dict(thresh_pix=self.thresh_pix,
                             thresh_isl=self.thresh_isl)
        
        self.opts_pos.update(kw)
        self.opts_neg = {}
        self.opts_neg.update(kw)
        self.neg_thresh_isl = neg_thresh_isl
        self.neg_thresh_pix = neg_thresh_pix
        self.opts_neg["thresh_isl"] = self.neg_thresh_isl
        self.opts_neg["thresh_pix"] = self.neg_thresh_pix
Exemplo n.º 2
0
    def __init__(self,
                 imagename,
                 psfname=None,
                 sourcefinder_name='pybdsm',
                 saveformat="gaul",
                 makeplots=True,
                 do_psf_corr=True,
                 do_local_var=True,
                 psf_corr_region=5,
                 local_var_region=10,
                 rel_excl_src=None,
                 pos_smooth=2,
                 neg_smooth=2,
                 loglevel=0,
                 thresh_pix=5,
                 thresh_isl=3,
                 neg_thresh_isl=3,
                 neg_thresh_pix=5,
                 reset_rel=None,
                 prefix=None,
                 do_nearsources=False,
                 savefits=False,
                 increase_beam_cluster=False,
                 savemask_pos=False,
                 savemask_neg=False,
                 no_smooth=True,
                 **kw):
        """ Takes in image and extracts sources and makes 
            reliability estimations..
           
		
        imagename: Fits image
        psfname: PSF fits image, optional. 

        sourcefinder_name: str, optional. Default 'pybdsm'.
            Uses source finder specified.

        makeplots: bool, optional. Default is True.
            Make reliability plots.

        do_psf_corr : bool, optional. Default True.
            If True, PSF correlation will be added
            as an extra parameter for density estimations.
            NB: the PSF fits image must be provided.

        do_local_var : bool, optional. Default is True.
            If True, adds local variance as an extra parameter,
            for density estimations. 
        
        do_nearsources: boolean. Default is False.
            If true it adds number of nearest neighnours as an extra
            parameter. It looks for sources around 5 beam sizes.

        psf_corr_region : int, optional. Default value is 5. 
            Data size to correlate around a source, in beam sizes.
 
        local_var_region: int, optional. Default 10.
            Data size to compute the local variance in beam sizes.

        rel_excl_src : floats, optional. Default is None. 
            Excludes sources in a specified region
            e.g ra, dec, radius in degrees. For
            2 regions: ra1, dec1, radius1: ra2, dec2, radius2, etc.

        pos_smooth : float, optional. Default 2.
            Masking threshold for the positive image.
            For default value 2, data peaks < 2 * image noise
            are masked.

        neg_smooth : float, optional. Default 2.
            Similar to pos_smooth but applied to the negative image.

        thresh_isl :  float, optional. Default is 3.
            Threshold for forming islands in the positive image

        thresh_pix : float, optional. Default is 5.
            Threshold for model fitting, in positive image.

        neg_thresh_isl : float, optional. Default is 3. 
            Simialr to thresh_isl but for negative image.

        neg_thresh_pix : float, optional. Default is 5. 
            Similar to thresh_pix but for negative image.

        savefits: boolean. Default is False.
            If True a negative image is saved.

        reset_rel: boolean. Default is False. If true then
            sources with correlation < 0.002 and rel >0.60
            have their reliabilities set to 0.

        increase_beam_cluster: boolean, optional. If True, sources
            groupings will be increase by 20% the beam size. If False,
            the actual beam size will be used. Default is False.

        savemask_pos: boolean, optional. If true the mask applied on 
            the positive side of an image after smoothing is saved.
            
        savemask_neg: Similar to savemask_pos but for the negative
            side of an image.
        
        loglevel : int, optional. Default is 0.
            Provides Pythonlogging options, 0, 1, 2 and 3 are for info, debug,
            error and critial respectively.
   
         kw : kward for source extractions. Should be a mapping e.g
            kw['thresh_isl'] = 2.0 or kw['do_polarization'] = True 
        """

        #
        self.smoothing = not no_smooth

        self.prefix = prefix

        # log level
        self.loglevel = loglevel
        self.log = utils.logger(self.loglevel, prefix=self.prefix)

        # image, psf image
        self.imagename = imagename
        self.psfname = psfname

        with pyfits.open(imagename) as hdu:
            self.header = hdu[0].header
            self.wcs = WCS(self.header, mode="pyfits")
            self.pixelsize = abs(self.header["cdelt1"])

        self.bmaj = numpy.deg2rad(self.header["BMAJ"])

        # boolean optionals
        self.makeplots = makeplots
        self.do_local_var = do_local_var
        self.nearsources = do_nearsources
        self.do_psf_corr = do_psf_corr
        self.savemaskpos = savemask_pos
        self.savemaskneg = savemask_neg
        self.savefits = savefits
        self.derel = reset_rel
        self.log.info("Catalogues will be saved as %s, where srl is source "
                      " and gaul is Gaussians. " % saveformat)
        self.catalogue_format = "." + saveformat
        if not self.psfname:
            self.log.info(" No psf provided, do_psf_corr is set to False.")
            self.do_psf_corr = False

        # computing negative noise
        self.noise, self.mean = utils.negative_noise(self.imagename,
                                                     self.prefix)

        self.log.info(" The negative noise is %e Jy/beam" % self.noise)
        if self.noise == 0:
            self.log.debug(" The negative noise is 0, check image")

        # source finder initialization
        self.sourcefinder_name = sourcefinder_name
        self.log.info(" Using %s source finder to extract the sources." %
                      self.sourcefinder_name)

        self.negimage = self.prefix + "_negative.fits"
        utils.invert_image(self.imagename, self.negimage)

        # smoothing factors
        self.pos_smooth = pos_smooth
        self.neg_smooth = neg_smooth

        # region to evaluate
        self.corrstep = psf_corr_region
        self.localstep = local_var_region
        self.radiusrm = rel_excl_src
        self.do_beam = increase_beam_cluster

        beam_pix = int(round(numpy.rad2deg(self.bmaj) / self.pixelsize))
        self.locstep = self.localstep * beam_pix
        self.cfstep = self.corrstep * beam_pix
        self.bmin, self.bpa = self.header["BMIN"], self.header["BPA"]

        self.opts_pos = {}
        if self.do_beam:
            bmaj = self.header["BMAJ"]
            self.opts_pos["beam"] = (1.2 * bmaj, 1.2 * self.bmin, self.bpa)

        # Pybdsm or source finder fitting thresholds
        self.thresh_isl = thresh_isl
        self.thresh_pix = thresh_pix
        self.opts_pos = dict(thresh_pix=self.thresh_pix,
                             thresh_isl=self.thresh_isl)

        self.opts_pos.update(kw)
        self.opts_neg = {}
        self.opts_neg.update(kw)
        self.neg_thresh_isl = neg_thresh_isl
        self.neg_thresh_pix = neg_thresh_pix
        self.opts_neg["thresh_isl"] = self.neg_thresh_isl
        self.opts_neg["thresh_pix"] = self.neg_thresh_pix
Exemplo n.º 3
0
    def __init__(self, imagename, psfname=None, sourcefinder_name='pybdsm',
                 makeplots=True, do_psf_corr=True, do_local_var=True,
                 psf_corr_region=2, local_var_region=10, rel_excl_src=None, 
                 pos_smooth=1.6, neg_smooth=1.6, loglevel=0, thresh_pix=5,
                 thresh_isl=3, neg_thresh_isl=3, neg_thresh_pix=5,
                 prefix=None, do_nearsources=False, **kw):

        """ Takes in image and extracts sources and makes 
            reliability estimations..
           
 
        imagename: Fits image
        psfname: PSF fits image, optional. 

        sourcefinder_name: str, optional. Default 'pybdsm'.
            Uses source finder specified by the users.

        makeplots: bool, optional. Default is True.
            Make reliability plots.

        do_psf_corr : bool, optional. Default True.
            If True, correlation of sources with PSF will be added
            as an extra source parameter in reliability estimation.
            But the PSF fits image must be provided.

        do_local_var : bool, optional. Default is True.
            Adds local variance as an extra source parameter,
            similar to do_psf_corr but independent of the PSF image. 

        psf_corr_region : int, optional. Default value is 2. 
            Data size to correlate around a source in beam sizes.
 
        local_var_region: int, optional. Default 10.
            Data size to compute the local variance in beam sizes.

        rel_excl_src : float numbers, optional. Default is None. 
            Excludes sources in this region from the reliability
            estimations, e.g ra, dec, radius in degrees. For
            many regions: ra1, dec1, radius1: ra2, dec2, radius2.

        pos_smooth : float, optional. Default 1.6
            Data smoothing threshold in the positive side of an image.
            For default value 1.6, data peaks < 1.6 * image noise
            will be averaged out.

        neg_smooth : float, optional. Default 1.6.
            Similar to pos_smooth but applied to the negative side of
            an image.

        loglevel :  int, optional. Default is 0.
            Provides Pythonlogging options, 0, 1, 2 and 3 for info, debug,
            error and critial respectively.

        thresh_isl :  float, optional. Default is 3.
            Threshold for the island boundary in number of sigma above
            the mean. Determines extent of island used for fitting 
            [pybdsm]. For positive pixels.

        thresh_pix : float, optional. Default is 5.
            Source detection threshold: threshold for the island 
            peak in number of sigma above the mean. For positive pixels.

        neg_thresh_isl : float, optional. Default is 3. 
            Simialr to thresh_isl but applied to negative side 
            of the image.

        neg_thresh_pix : float, optional. Default is 5. 
            Similar to thresh_pix but applied to the negative
            side of an image.

        do_nearsources: boolean. Default is False.
            If true it adds number of nearest neighnours as an extra
            parameter. It looks for sources around 5 beam sizes.
   
         kw : kward for source extractions. Should be a mapping e.g
            kw['thresh_isl'] = 2.0 or kw['do_polarization'] = True 
        """


       
        # image, psf image
        self.imagename = imagename
        self.psfname = psfname 
        # setting output file names  
     
        self.prefix = prefix
        self.poslsm = self.prefix + "_positive.lsm.html"
        self.neglsm = self.prefix + "_negative.lsm.html"

        # log level  
        self.loglevel = loglevel
        self.log = utils.logger(self.loglevel, prefix=self.prefix)

        self.log.info("Loading Image data")

        # reading imagename data
        self.imagedata, self.wcs, self.header, self.pixelsize =\
            utils.reshape_data(self.imagename, prefix=self.prefix)

        self.bmaj = numpy.deg2rad(self.header["BMAJ"])

        self.do_psf_corr = do_psf_corr
        if not self.psfname:
            self.log.info("No psf provided, do_psf_corr = False.")
            self.do_psf_corr = False
           
        # computing negative noise
        self.noise = utils.negative_noise(self.imagedata)
        
        self.log.info("The negative noise is %e"%self.noise)

        if self.noise == 0: 
            self.log.debug("The negative noise is 0, check image")

        # source finder initialization
        self.sourcefinder_name  = sourcefinder_name
        self.log.info("Using %s source finder to extract sources."%
                      self.sourcefinder_name)


        # making negative image
        self.negativeimage = utils.invert_image(
                               self.imagename, self.imagedata,
                               self.header, self.prefix)

        # boolean optionals    
        self.makeplots = makeplots
        self.do_local_var = do_local_var
        self.nearsources = do_nearsources

        # smoothing factors
        self.pos_smooth = pos_smooth
        self.neg_smooth = neg_smooth
        
        # region to evaluate
        self.psf_corr_region = psf_corr_region
        self.local_var_region = local_var_region
        self.rel_excl_src = rel_excl_src
 
        # Pybdsm or source finder fitting thresholds
        self.thresh_isl = thresh_isl
        self.thresh_pix = thresh_pix
        self.opts_pos = dict(thresh_pix=self.thresh_pix,
                             thresh_isl=self.thresh_isl)
        self.opts_pos.update(kw)
        self.opts_neg = {}
        self.neg_thresh_isl = neg_thresh_isl
        self.neg_thresh_pix = neg_thresh_pix
        self.opts_neg["thresh_isl"] = self.neg_thresh_isl
        self.opts_neg["thresh_pix"] = self.neg_thresh_pix
Exemplo n.º 4
0

# Load the trained model
#model = get_model('test_model')
model = load_model('test_model.model')

# Define classes - {0: 'zero', 1: 'one', 2: 'two', ...}
classes = dict(
    enumerate([
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
        "nine"
    ]))

# Predict output based on input image
if (IMG_PATH):
    image = cv2.imread(IMG_PATH)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # Invert image: black becomes white and white becomes black
    image = utils.invert_image(image)
    image = np.resize(image, (28, 28, 1))
    predicted_label = predict_input(model, image, classes)
    print('Predicted output = ' + predicted_label)
else:
    print('Please give path of image: IMG_PATH=<path> python3 predict.py')

# Testing purposes
#mnist = tf.keras.datasets.mnist
#(X_train, y_train), (X_test, y_test) = mnist.load_data()
#X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
#predicted_label = predict_input(model, X_test[1], classes)