processing_components/skycomponent/operations.py
def find_skycomponents(im: Image, fwhm=1.0, threshold=1.0, npixels=5) -> List[Skycomponent]:
    """ Find gaussian components in Image above a certain threshold as Skycomponent

    :param im: Image to be searched
    :param fwhm: Full width half maximum of gaussian in pixels
    :param threshold: Threshold for component detection. Default: 1 Jy.
    :param npixels: Number of connected pixels required
    :return: list of sky components
    """
    
    assert isinstance(im, Image)
    log.info("find_skycomponents: Finding components in Image by segmentation")
    
    # We use photutils segmentation - this first segments the image
    # into pieces that are thought to contain individual sources, then
    # identifies the concrete source properties. Having these two
    # steps makes it straightforward to extract polarisation and
    # spectral information.
    
    # Make filter kernel
    sigma = fwhm * gaussian_fwhm_to_sigma
    kernel = Gaussian2DKernel(sigma, x_size=int(1.5 * fwhm), y_size=int(1.5 * fwhm))
    kernel.normalize()
    
    # Segment the average over all channels of Stokes I
    image_sum = numpy.sum(im.data, axis=0)[0, ...] / float(im.shape[0])
    segments = segmentation.detect_sources(image_sum, threshold, npixels=npixels, filter_kernel=kernel)
    log.info("find_skycomponents: Identified %d segments" % segments.nlabels)
    
    # Now compute source properties for all polarisations and frequencies
    comp_tbl = [[segmentation.source_properties(im.data[chan, pol], segments,
                                                filter_kernel=kernel,
                                                wcs=im.wcs.sub([1,2])).to_table()
                 for pol in [0]]
                for chan in range(im.nchan)]
    
    def comp_prop(comp, prop_name):
        return [[comp_tbl[chan][pol][comp][prop_name]
                 for pol in [0]]
                for chan in range(im.nchan)]
    
    # Generate components
    comps = []
    for segment in range(segments.nlabels):
        # Get flux and position. Astropy's quantities make this
        # unnecessarily complicated.
        flux = numpy.array(comp_prop(segment, "max_value"))
        # These values seem inconsistent with the xcentroid, and ycentroid values
        # ras = u.Quantity(list(map(u.Quantity,
        #         comp_prop(segment, "ra_icrs_centroid"))))
        # decs = u.Quantity(list(map(u.Quantity,
        #         comp_prop(segment, "dec_icrs_centroid"))))
        xs = u.Quantity(list(map(u.Quantity,
                                 comp_prop(segment, "xcentroid"))))
        ys = u.Quantity(list(map(u.Quantity,
                                 comp_prop(segment, "ycentroid"))))
        
        sc = pixel_to_skycoord(xs, ys, im.wcs, 0)
        ras = sc.ra
        decs = sc.dec
        
        # Remove NaNs from RA/DEC (happens if there is no flux in that
        # polarsiation/channel)
        # ras[numpy.isnan(ras)] = 0.0
        # decs[numpy.isnan(decs)] = 0.0
        
        # Determine "true" position by weighting
        flux_sum = numpy.sum(flux)
        ra = numpy.sum(flux * ras) / flux_sum
        dec = numpy.sum(flux * decs) / flux_sum
        xs = numpy.sum(flux * xs) / flux_sum
        ys = numpy.sum(flux * ys) / flux_sum
        
        point_flux = im.data[:, :, numpy.round(ys.value).astype('int'), numpy.round(xs.value).astype('int')]
        
        # Add component
        comps.append(Skycomponent(
            direction=SkyCoord(ra=ra, dec=dec),
            frequency=im.frequency,
            name="Segment %d" % segment,
            flux=point_flux,
            shape='Point',
            polarisation_frame=im.polarisation_frame,
            params={}))
    
    return comps
 Find gaussian components in Image above a certain threshold as Skycomponent

    :param im: Image to be searched
    :param fwhm: Full width half maximum of gaussian in pixels
    :param threshold: Threshold for component detection. Default: 1 Jy.
    :param npixels: Number of connected pixels required
    :return: list of sky components
    
Find gaussian components in Image above a certain threshold as Skycomponent
Find gaussian components in Image above a certain threshold as Skycomponent
def find_skycomponents(im: Image, fwhm=1.0, threshold=1.0, npixels=5) -> List[Skycomponent]:
    assert isinstance(im, Image)
    log.info("find_skycomponents: Finding components in Image by segmentation")
    sigma = fwhm * gaussian_fwhm_to_sigma
    kernel = Gaussian2DKernel(sigma, x_size=int(1.5 * fwhm), y_size=int(1.5 * fwhm))
    kernel.normalize()
    image_sum = numpy.sum(im.data, axis=0)[0, ...] / float(im.shape[0])
    segments = segmentation.detect_sources(image_sum, threshold, npixels=npixels, filter_kernel=kernel)
    log.info("find_skycomponents: Identified %d segments" % segments.nlabels)
    comp_tbl = [[segmentation.source_properties(im.data[chan, pol], segments,
                                                filter_kernel=kernel,
                                                wcs=im.wcs.sub([1,2])).to_table()
                 for pol in [0]]
                for chan in range(im.nchan)]
    def comp_prop(comp, prop_name):
        return [[comp_tbl[chan][pol][comp][prop_name]
                 for pol in [0]]
                for chan in range(im.nchan)]
    comps = []
    for segment in range(segments.nlabels):
        flux = numpy.array(comp_prop(segment, "max_value"))
        xs = u.Quantity(list(map(u.Quantity,
                                 comp_prop(segment, "xcentroid"))))
        ys = u.Quantity(list(map(u.Quantity,
                                 comp_prop(segment, "ycentroid"))))
        sc = pixel_to_skycoord(xs, ys, im.wcs, 0)
        ras = sc.ra
        decs = sc.dec
        flux_sum = numpy.sum(flux)
        ra = numpy.sum(flux * ras) / flux_sum
        dec = numpy.sum(flux * decs) / flux_sum
        xs = numpy.sum(flux * xs) / flux_sum
        ys = numpy.sum(flux * ys) / flux_sum
        point_flux = im.data[:, :, numpy.round(ys.value).astype('int'), numpy.round(xs.value).astype('int')]
        comps.append(Skycomponent(
            direction=SkyCoord(ra=ra, dec=dec),
            frequency=im.frequency,
            name="Segment %d" % segment,
            flux=point_flux,
            shape='Point',
            polarisation_frame=im.polarisation_frame,
            params={}))
    return comps
def find_skycomponents ( im : Image , fwhm = 1.0 , threshold = 1.0 , npixels = 5 ) -> List [ Skycomponent ] : assert isinstance ( im , Image ) log . info ( "find_skycomponents: Finding components in Image by segmentation" ) sigma = fwhm * gaussian_fwhm_to_sigma kernel = Gaussian2DKernel ( sigma , x_size = int ( 1.5 * fwhm ) , y_size = int ( 1.5 * fwhm ) ) kernel . normalize ( ) image_sum = numpy . sum ( im . data , axis = 0 ) [ 0 , ... ] / float ( im . shape [ 0 ] ) segments = segmentation . detect_sources ( image_sum , threshold , npixels = npixels , filter_kernel = kernel ) log . info ( "find_skycomponents: Identified %d segments" % segments . nlabels ) comp_tbl = [ [ segmentation . source_properties ( im . data [ chan , pol ] , segments , filter_kernel = kernel , wcs = im . wcs . sub ( [ 1 , 2 ] ) ) . to_table ( ) for pol in [ 0 ] ] for chan in range ( im . nchan ) ] def comp_prop ( comp , prop_name ) : return [ [ comp_tbl [ chan ] [ pol ] [ comp ] [ prop_name ] for pol in [ 0 ] ] for chan in range ( im . nchan ) ] comps = [ ] for segment in range ( segments . nlabels ) : flux = numpy . array ( comp_prop ( segment , "max_value" ) ) xs = u . Quantity ( list ( map ( u . Quantity , comp_prop ( segment , "xcentroid" ) ) ) ) ys = u . Quantity ( list ( map ( u . Quantity , comp_prop ( segment , "ycentroid" ) ) ) ) sc = pixel_to_skycoord ( xs , ys , im . wcs , 0 ) ras = sc . ra decs = sc . dec flux_sum = numpy . sum ( flux ) ra = numpy . sum ( flux * ras ) / flux_sum dec = numpy . sum ( flux * decs ) / flux_sum xs = numpy . sum ( flux * xs ) / flux_sum ys = numpy . sum ( flux * ys ) / flux_sum point_flux = im . data [ : , : , numpy . round ( ys . value ) . astype ( 'int' ) , numpy . round ( xs . value ) . astype ( 'int' ) ] comps . append ( Skycomponent ( direction = SkyCoord ( ra = ra , dec = dec ) , frequency = im . frequency , name = "Segment %d" % segment , flux = point_flux , shape = 'Point' , polarisation_frame = im . polarisation_frame , params = { } ) ) return comps
Find gaussian components in Image above a certain threshold as Skycomponent
Find gaussian components in Image above a certain threshold as Skycomponent
""" Find gaussian components in Image above a certain threshold as Skycomponent

    :param im: Image to be searched
    :param fwhm: Full width half maximum of gaussian in pixels
    :param threshold: Threshold for component detection. Default: 1 Jy.
    :param npixels: Number of connected pixels required
    :return: list of sky components
    """ # We use photutils segmentation - this first segments the image # into pieces that are thought to contain individual sources, then # identifies the concrete source properties. Having these two # steps makes it straightforward to extract polarisation and # spectral information. # Make filter kernel # Segment the average over all channels of Stokes I # Now compute source properties for all polarisations and frequencies # Generate components # Get flux and position. Astropy's quantities make this # unnecessarily complicated. # These values seem inconsistent with the xcentroid, and ycentroid values # ras = u.Quantity(list(map(u.Quantity, #         comp_prop(segment, "ra_icrs_centroid")))) # decs = u.Quantity(list(map(u.Quantity, #         comp_prop(segment, "dec_icrs_centroid")))) # Remove NaNs from RA/DEC (happens if there is no flux in that # polarsiation/channel) # ras[numpy.isnan(ras)] = 0.0 # decs[numpy.isnan(decs)] = 0.0 # Determine "true" position by weighting # Add component
list of sky components
list of sky components
im
Image
Image to be searched
Image to be searched
fwhm
Full width half maximum of gaussian in pixels
Full width half maximum of gaussian in pixels
threshold
Threshold for component detection. Default: 1 Jy.
Threshold for component detection . Default : 1 Jy .
npixels
Number of connected pixels required
Number of connected pixels required