示例#1
0
def gen_segmap_tresh_lum(img,
                         xc,
                         yc,
                         thresh=5.0,
                         Amin=5,
                         pixscale=None,
                         all_detection=False,
                         structure=None):
    """Generate a segmentation map by selecting pixels above sky+thresh*std
    and then select the regions within 1arcsec to belong to the object. Objects
    with a number of pixels less than Amin are non detections and are not present
    in the final segmentation map.
    The final segmentation map is dilated to smooth the detections"""
    N, M = img.shape
    MAP = np.zeros([N, M])

    MAP[img > thresh] = 1
    Regions, Nregions = mi.sci_nd.label(MAP)
    for n in range(1, Nregions + 1):
        npix = len(Regions[Regions == n])
        if npix < Amin:
            Regions[Regions == n] = 0
        else:
            continue

    if structure == None:
        SIZE = 6
        structure = np.zeros([SIZE, SIZE])
        dmat, d = distance_matrix(SIZE / 2. - 0.5, SIZE / 2. - 0.5, structure)
        structure[dmat < SIZE / 2] = 1

    if all_detection:
        return Regions
    else:
        segmap = mi.select_object_map(xc, yc, Regions, pixscale=pixscale)
        return mi.sci_nd.binary_dilation(segmap, structure=structure)
示例#2
0
def compute_size(image,ra,dec,galmag,hsize,threshold,fractions,sblimit,size=6,safedist=1.0):

    Sizes = np.zeros(len(fractions)+1)
    Fluxes = np.zeros(len(fractions)+1)

    SizesPSF = np.zeros(len(fractions)+1)
    FluxesPSF = np.zeros(len(fractions)+1)


    image_stamp = load_data(image,ra,dec,hsize)
    if np.amax(image_stamp)==np.amin(image_stamp):
        return Sizes - 99,-9


    dilate = define_structure(size)
    sky_med,sky_std = mi.sky_value(image_stamp,args.ksky)

    psf_image = simulate_psf(galmag,hsize,sky_med)

    segmap_psf = mi.gen_segmap_sbthresh(psf_image-sky_med,hsize,hsize,sblimit,args.pixscale,thresh=threshold,Amin=args.areamin,all_detection=True)
    single_source_map_psf = mi.select_object_map(hsize,hsize,segmap_psf,pixscale=args.pixscale)

    segmap = mi.gen_segmap_sbthresh(image_stamp-sky_med,hsize,hsize,sblimit,args.pixscale,thresh=threshold,Amin=args.areamin,all_detection=True)
    single_source_map = mi.select_object_map(hsize,hsize,segmap,pixscale=args.pixscale)
    image_stamp,single_source_map,imglag,segflag = mi.image_validation(image_stamp,single_source_map,args.pixscale,safedist)

    if segflag==1:
        return Sizes - 99,segflag

    if args.nodilation:
        dilated_map = single_source_map
        dilated_map_psf = single_source_map_psf
    else:
        dilated_map = mi.sci_nd.binary_dilation(single_source_map,structure=dilate).astype(np.int32)
        dilated_map_psf = mi.sci_nd.binary_dilation(single_source_map_psf,structure=dilate).astype(np.int32)


    for i in range(len(fractions)):
        Sizes[i],TotalArea,Fluxes[i],TotalFlux = compute_fraction_area(image_stamp,dilated_map,fractions[i])
        SizesPSF[i],TotalAreaPSF,FluxesPSF[i],TotalFluxPSF = compute_fraction_area(psf_image,dilated_map_psf,fractions[i])

    Sizes[i+1]=TotalArea
    Fluxes[i+1]=TotalFlux

    SizesPSF[i+1]=TotalAreaPSF
    FluxesPSF[i+1]=TotalFluxPSF

    Real_Sizes  = Sizes - SizesPSF

    if args.nosaving:
        mpl.rcParams['image.cmap']='gist_stern_r'
        segmap[segmap!=0]/=1
        fig,ax=mpl.subplots(2,3,figsize=(20.6,12))
        mpl.subplots_adjust(wspace=0.2,hspace=0.02)
        ax=ax.reshape(np.size(ax))
        ax[0].imshow(np.sqrt(np.abs(image_stamp)),cmap='hot')
        mi.gen_circle(ax[1],hsize,hsize,hsize*(2*np.sqrt(2)*0.5/args.size),color='red')
        ax[1].imshow(segmap)
        ax[2].imshow(single_source_map)

        flux_map = np.zeros(image_stamp.shape)
        flux_map[dilated_map==1]=3
        for j in range(len(Fluxes)-2,-1,-1):
            flux_map[image_stamp>Fluxes[j]]=(len(Fluxes)-j)*4
        flux_map*=dilated_map
        imax,imin,jmax,jmin = mi.find_ij(dilated_map)
        ax[3].imshow(flux_map[imin:imax,jmin:jmax],aspect='equal',cmap='gist_heat_r')
        ax[3].text(12,9,'20',color='white',va='center',ha='center',weight='heavy')
        ax[3].text(10,7.5,'50',color='white',va='center',ha='center',weight='heavy')
        ax[3].text(8.5,5.5,'80',color='white',va='center',ha='center',weight='heavy')
        ax[3].text(6,3,'100',color='white',va='center',ha='center',weight='heavy')


#        masked_neighbours = mi.sci_nd.binary_dilation(segmap - single_source_map,structure=dilate)
        compute_fraction_area(image_stamp,dilated_map,1.0,flux_sampling=500,draw_ax=ax[4])

        ax[5].imshow(dilated_map)

        for i in range(len(ax)):
            if i!=4:
                ax[i].set_xticks([])
                ax[i].set_yticks([])

        fig.text(0.95,0.50,r'$\curvearrowright$',va='center',ha='center',weight='heavy',rotation=-90,fontsize=150)
#        fig.savefig('size_thresholding_example.png')
        def exit_code(event):
            if event.key=='escape':
                sys.exit()
            if event.key=='q':
                mpl.close('all')

        fig.canvas.mpl_connect('key_press_event',exit_code)
        mpl.show()


    return Real_Sizes,segflag
def compute_size(image_list,
                 pixscales,
                 ra,
                 dec,
                 hsize,
                 threshold,
                 fractions,
                 sblimit,
                 color,
                 size=6,
                 safedist=1.0):

    nimages = len(image_list)
    Sizes = np.zeros([len(fractions) + 1, nimages])
    Fluxes = np.zeros([len(fractions) + 1, nimages])
    segflags = np.zeros(nimages)

    hsize_pix = [int(hsize / pixscales[k]) / 2 for k in range(nimages)]

    image_stamps = [
        load_data(image, ra, dec, hs)
        for image, hs in zip(image_list, hsize_pix)
    ]
    headers = [pyfits.getheader(image) for image in image_list]

    #    if np.amax(image_stamp)==np.amin(image_stamp):
    #        return Sizes - 99,-9

    zp = np.zeros(nimages)
    sblimits = np.zeros(nimages)
    for k in range(nimages):
        PHOTFLAM = float(headers[k]['PHOTFLAM'])
        PHOTPLAM = float(headers[k]['PHOTPLAM'])
        zp[k] = -2.5 * np.log10(PHOTFLAM) - 5 * np.log10(PHOTPLAM) - 2.408

    sblimits[0] = sblimit
    for k in range(nimages - 1):
        ColorCorretion = 10**(0.4 * (color[k] - (zp[0] - zp[k + 1])))
        sblimits[k + 1] = sblimit * ColorCorretion
        if args.verbose:
            print("Color Correction Term: %.5f" % ColorCorretion)
            print("New Surface Brightness Limit: %.5f counts/s/arcsec**2" %
                  sblimits[k + 1])

    dilate = define_structure(size)

    for k in range(nimages):
        if np.amax(image_stamps[k]) == np.amin(image_stamps[k]):
            segflag = -9
        else:
            segmap = mi.gen_segmap_sbthresh(image_stamps[k],
                                            hsize_pix[k],
                                            hsize_pix[k],
                                            sblimits[k],
                                            pixscales[k],
                                            thresh=threshold,
                                            Amin=args.areamin,
                                            all_detection=True)
            single_source_map = mi.select_object_map(hsize_pix[k],
                                                     hsize_pix[k],
                                                     segmap,
                                                     pixscale=pixscales[k])
            image_stamps[
                k], single_source_map, imglag, segflag = mi.image_validation(
                    image_stamps[k], single_source_map, pixscales[k], safedist)

        if np.abs(segflag) != 0:
            Sizes[:, k] -= 99
            segflags[k] = segflag
        else:
            if args.nodilation:
                dilated_map = single_source_map
            else:
                dilated_map = mi.sci_nd.binary_dilation(
                    single_source_map, structure=dilate).astype(np.int32)

            for i in range(len(fractions)):
                Sizes[i, k], TotalArea, Fluxes[
                    i, k], TotalFlux = compute_fraction_area(
                        image_stamps[k], dilated_map, fractions[i])
            Sizes[i + 1, k] = TotalArea
            Fluxes[i + 1, k] = TotalFlux

            if args.nosaving:
                mpl.rcParams['image.cmap'] = 'gist_stern_r'
                segmap[segmap != 0] /= 1
                fig, ax = mpl.subplots(2, 3, figsize=(20.6, 12))
                mpl.subplots_adjust(wspace=0.2, hspace=0.02)
                ax = ax.reshape(np.size(ax))
                ax[0].imshow(np.sqrt(np.abs(image_stamps[k])), cmap='hot')
                mi.gen_circle(ax[1],
                              hsize_pix[k],
                              hsize_pix[k],
                              hsize_pix[k] *
                              (2 * np.sqrt(2) * 0.5 / args.size),
                              color='red')
                ax[1].imshow(segmap)
                ax[2].imshow(single_source_map)

                flux_map = np.zeros(image_stamps[k].shape)
                flux_map[dilated_map == 1] = 3
                for j in range(len(Fluxes[:, k]) - 2, -1, -1):
                    flux_map[image_stamps[k] > Fluxes[j, k]] = (
                        len(Fluxes[:, k]) - j) * 4
                flux_map *= dilated_map
                imax, imin, jmax, jmin = mi.find_ij(dilated_map)
                ax[3].imshow(flux_map[imin:imax, jmin:jmax],
                             aspect='equal',
                             cmap='gist_heat_r')
                ax[3].text(12,
                           9,
                           '20',
                           color='white',
                           va='center',
                           ha='center',
                           weight='heavy')
                ax[3].text(10,
                           7.5,
                           '50',
                           color='white',
                           va='center',
                           ha='center',
                           weight='heavy')
                ax[3].text(8.5,
                           5.5,
                           '80',
                           color='white',
                           va='center',
                           ha='center',
                           weight='heavy')
                ax[3].text(6,
                           3,
                           '100',
                           color='white',
                           va='center',
                           ha='center',
                           weight='heavy')

                #        masked_neighbours = mi.sci_nd.binary_dilation(segmap - single_source_map,structure=dilate)
                compute_fraction_area(image_stamps[k],
                                      dilated_map,
                                      1.0,
                                      flux_sampling=500,
                                      draw_ax=ax[4])

                ax[5].imshow(dilated_map)

                for i in range(len(ax)):
                    if i != 4:
                        ax[i].set_xticks([])
                        ax[i].set_yticks([])

                fig.text(0.95,
                         0.50,
                         r'$\curvearrowright$',
                         va='center',
                         ha='center',
                         weight='heavy',
                         rotation=-90,
                         fontsize=150)
                #        fig.savefig('size_thresholding_example.png')
                fig.canvas.mpl_connect('key_press_event', exit_code)

    if args.nosaving:
        mpl.show()

    return Sizes, segflags