Пример #1
0
def measure_image_background(image):
    ''' This does not have to be sophisticated '''
    sigma_clip = SigmaClip(sigma=3., iters=3)
    bkg_estimator = BiweightLocationBackground()
    bkg = Background2D(image, (100, 100),
                       filter_size=(3, 3),
                       sigma_clip=sigma_clip,
                       bkg_estimator=bkg_estimator)
    return image - bkg.background, bkg
Пример #2
0
    def _get_background(self, image, tile_size=32):

        sigma_clip = SigmaClip(sigma=3., iters=3)
        bkg_estimator = MedianBackground()
        bkg = Background2D(image,
                           tile_size,
                           filter_size=3,
                           sigma_clip=sigma_clip,
                           bkg_estimator=bkg_estimator)

        return bkg.background
Пример #3
0
    def reference(self):
        """
        This function ...
        :return:
        """

        # Inform the user
        log.info("Estimating background with photutils ...")

        # Plot total mask
        if self.config.plot:
            plotting.plot_mask(self.total_mask, title="total mask")

        integer_aperture_radius = int(math.ceil(self.aperture_radius))
        box_shape = (integer_aperture_radius, integer_aperture_radius)
        filter_size = (3, 3)

        # Estimate the background
        sigma_clip = SigmaClip(sigma=3., iters=10)
        #bkg_estimator = MedianBackground()
        bkg_estimator = SExtractorBackground()

        bkg = Background2D(self.frame.data,
                           box_shape,
                           filter_size=filter_size,
                           sigma_clip=sigma_clip,
                           bkg_estimator=bkg_estimator,
                           mask=self.total_mask.data)

        # Statistics
        #print("median background", bkg.background_median)
        #print("rms background", bkg.background_rms_median)

        self.statistics.reference = Map()
        self.statistics.reference.median = bkg.background_median
        self.statistics.reference.rms = bkg.background_rms_median

        # Plot
        if self.config.plot:
            plotting.plot_box(bkg.background,
                              title="background from photutils")

        # Set the sky
        self.reference_sky = Frame(bkg.background)

        # Set bkg object
        self.photutils_bkg = bkg

        # Plot
        if self.config.plot:
            plotting.plot_box(self.reference_sky, title="reference sky")
Пример #4
0
def find_centroid(data):
    """
    find the centroid again after the image was rotated
    """

    sigma_clip = SigmaClip(sigma=3., iters=10)
    bkg_estimator = MedianBackground()
    bkg = Background2D(data, (25, 25),
                       filter_size=(3, 3),
                       sigma_clip=sigma_clip,
                       bkg_estimator=bkg_estimator)

    threshold = bkg.background + (3. * bkg.background_rms)

    sigma = 2.0 * gaussian_fwhm_to_sigma  # FWHM = 2.
    kernel = Gaussian2DKernel(sigma, x_size=3, y_size=3)
    kernel.normalize()
    segm = detect_sources(data, threshold, npixels=5, filter_kernel=kernel)

    props = source_properties(data, segm)
    tbl = properties_table(props)

    my_min = 100000.

    x_shape = np.float(data.shape[0])
    y_shape = np.float(data.shape[1])

    r = 3.  # approximate isophotal extent
    apertures = []
    for prop in props:
        position = (prop.xcentroid.value, prop.ycentroid.value)
        #print(position)
        a = prop.semimajor_axis_sigma.value * r
        b = prop.semiminor_axis_sigma.value * r
        theta = prop.orientation.value
        apertures.append(EllipticalAperture(position, a, b, theta=theta))
        my_dist = np.sqrt((prop.xcentroid.value - x_shape / 2.)**2 +
                          (prop.ycentroid.value - y_shape / 2.)**2)
        if (my_dist < my_min):
            my_label = prop.id - 1
            my_min = my_dist

    mytheta = props[my_label].orientation.value
    mysize = np.int(np.round(r * props[my_label].semimajor_axis_sigma.value))
    my_x = props[my_label].xcentroid.value
    my_y = props[my_label].ycentroid.value
    return my_x, my_y, mytheta, mysize
Пример #5
0
def estimate(image,
             sigma=3.0,
             sigma_clip_iters=10,
             mesh_size=(75, 75),
             filter_size=(5, 5),
             mask_value=None):

    mask = (image == mask_value)

    sigma_clip = SigmaClip(sigma=sigma, iters=sigma_clip_iters)
    bkg_estimator = MedianBackground()
    bkg = Background2D(image,
                       mesh_size,
                       filter_size=filter_size,
                       sigma_clip=sigma_clip,
                       bkg_estimator=bkg_estimator,
                       mask=mask)

    return bkg.background_median, bkg.background_rms_median, bkg, mask
Пример #6
0
def get_background_level(image, npix=50, sextractor=False, ordinary=False):
    " Extimate the background level using photutils 0.3"

    from photutils.background import Background2D, SExtractorBackground
    from photutils import SigmaClip
    if npix > image.shape[0]:
        print "Image shape", image.shape
        npix = image.shape[0] - 1
    if sextractor:
        sigma_clip = SigmaClip(sigma=2.)
        bkg = SExtractorBackground(sigma_clip)
        bkg_value = bkg.calc_background(image)
    elif ordinary:
        from scipy.ndimage import filters
        pix = image.ravel()
        bkg_value = filters.median_filter(pix, 50).reshape(image.shape)
    else:
        bkg = Background2D(image, box_size=npix)
        bkg_value = bkg.background
    return bkg_value
Пример #7
0
def A3d(data, **kwargs):
    which = kwargs.get('chosen_by', 'prox')
    data_sum = np.nansum(data, axis=0)
    Npixels = kwargs.get('N_pixels_max',
                         data_sum.shape[0] * data_sum.shape[1] / 2.)
    sigma = kwargs.get('sigma', 2.)
    data_sum_noneg = np.copy(data_sum)
    data_sum_noneg[data_sum_noneg <= 0.] = 0.0
    bkg2D = Background2D(data_sum_noneg,
                         data_sum_noneg.shape,
                         filter_size=(2, 2),
                         sigma_clip=SigmaClip(sigma=3., iters=2),
                         bkg_estimator=MedianBackground())
    bkg = np.unique(bkg2D.background)
    tbl = find_peaks(data_sum, threshold=bkg)
    x0 = int(data[int(data.shape[0] * 2 / 3)].shape[1] / 2.)
    y0 = int(data[int(data.shape[0] * 2 / 3)].shape[0] / 2.)
    dist = np.array([])
    for i in range(len(tbl['x_peak'])):
        dist = np.append(
            dist,
            np.sqrt((x0 - tbl['x_peak'][i])**2 + (y0 - tbl['y_peak'][i])**2))
    if which == 'prox':
        chosen = np.where(dist == min(dist))[0]
        if len(chosen) > 1:
            for i in chosen:
                vec_chos = np.array(
                    [tbl['peak_value'][i], tbl['peak_value'][i]])
            chosen = np.where(tbl['peak_value'] == max(vec_chos))[0]
    elif which == 'mxpeak':
        chosen = np.where(tbl['peak_value'] == max(tbl['peak_value']))[0]
    cx = int(tbl['x_peak'][chosen])
    cy = int(tbl['y_peak'][chosen])
    aperture = np.zeros_like(data_sum)
    aperture[cy, cx] = 1.
    c = 1
    x = cy
    y = cx
    for n in count(start=1, step=1):
        if n > data_sum.shape[0] and n > data_sum.shape[1]: break
        if n % 2:
            x += 1
            try:
                if data_sum[x, y] >= sigma * bkg:
                    try:
                        A0 = aperture[x + 1, y] == 1
                    except:
                        A0 = False
                    try:
                        A1 = aperture[x - 1, y] == 1
                    except:
                        A1 = False
                    try:
                        A2 = aperture[x, y + 1] == 1
                    except:
                        A2 = False
                    try:
                        A3 = aperture[x, y - 1] == 1
                    except:
                        A3 = False
                    if A0 or A1 or A2 or A3:
                        aperture[x, y] = 1
                        c += 1
                        if c >= Npixels: break
            except:
                pass
            for i in range(n):
                y -= 1
                try:
                    if data_sum[x, y] >= sigma * bkg:
                        try:
                            B0 = aperture[x + 1, y] == 1
                        except:
                            B0 = False
                        try:
                            B1 = aperture[x - 1, y] == 1
                        except:
                            B1 = False
                        try:
                            B2 = aperture[x, y + 1] == 1
                        except:
                            B2 = False
                        try:
                            B3 = aperture[x, y - 1] == 1
                        except:
                            B3 = False
                        if B0 or B1 or B2 or B3:
                            aperture[x, y] = 1
                            c += 1
                            if c >= Npixels: break
                except:
                    pass
            for i in range(n):
                x -= 1
                try:
                    if data_sum[x, y] >= sigma * bkg:
                        try:
                            C0 = aperture[x + 1, y] == 1
                        except:
                            C0 = False
                        try:
                            C1 = aperture[x - 1, y] == 1
                        except:
                            C1 = False
                        try:
                            C2 = aperture[x, y + 1] == 1
                        except:
                            C2 = False
                        try:
                            C3 = aperture[x, y - 1] == 1
                        except:
                            C3 = False
                        if C0 or C1 or C2 or C3:
                            aperture[x, y] = 1
                            c += 1
                            if c >= Npixels: break
                except:
                    pass
        else:
            x -= 1
            try:
                if data_sum[x, y] >= sigma * bkg:
                    try:
                        D0 = aperture[x + 1, y] == 1
                    except:
                        D0 = False
                    try:
                        D1 = aperture[x - 1, y] == 1
                    except:
                        D1 = False
                    try:
                        D2 = aperture[x, y + 1] == 1
                    except:
                        D2 = False
                    try:
                        D3 = aperture[x, y - 1] == 1
                    except:
                        D3 = False
                    if D0 or D1 or D2 or D3:
                        aperture[x, y] = 1
                        c += 1
                        if c >= Npixels: break
            except:
                pass
            for i in range(n):
                y += 1
                try:
                    if data_sum[x, y] >= sigma * bkg:
                        try:
                            E0 = aperture[x + 1, y] == 1
                        except:
                            E0 = False
                        try:
                            E1 = aperture[x - 1, y] == 1
                        except:
                            E1 = False
                        try:
                            E2 = aperture[x, y + 1] == 1
                        except:
                            E2 = False
                        try:
                            E3 = aperture[x, y - 1] == 1
                        except:
                            E3 = False
                        if E0 or E1 or E2 or E3:
                            aperture[x, y] = 1
                            c += 1
                            if c >= Npixels: break
                except:
                    pass
            for i in range(n):
                x += 1
                try:
                    if data_sum[x, y] >= sigma * bkg:
                        try:
                            F0 = aperture[x + 1, y] == 1
                        except:
                            F0 = False
                        try:
                            F1 = aperture[x - 1, y] == 1
                        except:
                            F1 = False
                        try:
                            F2 = aperture[x, y + 1] == 1
                        except:
                            F2 = False
                        try:
                            F3 = aperture[x, y - 1] == 1
                        except:
                            F3 = False
                        if F0 or F1 or F2 or F3:
                            aperture[x, y] = 1
                            c += 1
                            if c >= Npixels: break
                except:
                    pass
    if which == 'prox':
        no_chosen = np.where(dist != min(dist))[0]
    elif which == 'mxpeak':
        no_chosen = np.where(tbl['peak_value'] != max(tbl['peak_value']))[0]

    for k in no_chosen:
        ncy = int(tbl['x_peak'][k])
        ncx = int(tbl['y_peak'][k])

        aperture[ncx, ncy] = 0
        try:
            aperture[ncx + 1, ncy] = 0
        except:
            pass
        try:
            aperture[ncx, ncy + 1] = 0
        except:
            pass
        try:
            aperture[ncx - 1, ncy] = 0
        except:
            pass
        try:
            aperture[ncx, ncy - 1] = 0
        except:
            pass
        try:
            aperture[ncx + 1, ncy + 1] = 0
        except:
            pass
        try:
            aperture[ncx + 1, ncy - 1] = 0
        except:
            pass
        try:
            aperture[ncx - 1, ncy - 1] = 0
        except:
            pass
        try:
            aperture[ncx - 1, ncy - 1] = 0
        except:
            pass
        try:
            aperture[ncx - 1, ncy + 1] = 0
        except:
            pass
        try:
            aperture[ncx - 1, ncy - 1] = 0
        except:
            pass

    return aperture, c
Пример #8
0
            # Locate regions outside of a 5% deviation
            tmpSuperskyData = superskyImage.data
            maskedPix = np.abs(tmpSuperskyData - 1.0) > 0.05

            # Get rid of the small stuff and expand the big stuff
            maskedPix = ndimage.binary_opening(maskedPix, iterations=2)
            maskedPix = ndimage.binary_closing(maskedPix, iterations=2)
            maskedPix = ndimage.binary_dilation(maskedPix, iterations=4)

            # TODO: Make the box_size and filter_size sensitive to binning.
            binningArray = np.array(superskyImage.binning)
            box_size = tuple((100 / binningArray).astype(int))
            filter_size = tuple((10 / binningArray).astype(int))

            # Setup the sigma clipping and median background estimators
            sigma_clip = SigmaClip(sigma=3., iters=10)
            bkg_estimator = MedianBackground()

            # Compute a smoothed background image
            bkgData = Background2D(superskyImage.data,
                                   box_size=box_size,
                                   filter_size=filter_size,
                                   mask=maskedPix,
                                   sigma_clip=sigma_clip,
                                   bkg_estimator=bkg_estimator)

            # Construct a smoothed supersky image object
            smoothedSuperskyImage = ai.reduced.ReducedScience(
                bkgData.background / bkgData.background_median,
                uncertainty=bkgData.background_rms,
                properties={'unit': u.dimensionless_unscaled})
Пример #9
0
def prepare_data(file, data_dir, folder):
    """
    prepare_data picks a file with the image of the galaxy, detect the central object, rotate it to the major axis, and returns 
    the data and errors ready to fit a warp curve, along with the maximum distance from the center 

    """

    # check if there is a folder for the figures, if not create it

    if not os.path.isdir('../figs/' + str(folder)):
        os.mkdir('../figs/' + str(folder))

    # check if there is a folder for the text output, if not create it

    if not os.path.isdir('../output/' + str(folder)):
        os.mkdir('../output/' + str(folder))

    print(data_dir + '/' + str(file))

    hdu = fits.open(data_dir + '/' + str(file[:-1]))[
        0]  #fits.open(data_dir+'/'+str(file[:-1]))[0]
    wcs = WCS(hdu.header)

    data = hdu.data

    sigma_clip = SigmaClip(sigma=3., iters=10)
    bkg_estimator = MedianBackground()
    bkg = Background2D(data, (25, 25),
                       filter_size=(3, 3),
                       sigma_clip=sigma_clip,
                       bkg_estimator=bkg_estimator)

    if (cfg.DATA_TYPE == 'REAL'):
        weight = fits.open(data_dir + '/' + str(file[:-1]))[1].data
    else:
        weight = bkg.background_rms

    threshold = bkg.background + (3. * bkg.background_rms)

    sigma = 2.0 * gaussian_fwhm_to_sigma  # FWHM = 2.
    kernel = Gaussian2DKernel(sigma, x_size=3, y_size=3)
    kernel.normalize()
    segm = detect_sources(data, threshold, npixels=5, filter_kernel=kernel)

    rand_cmap = random_cmap(segm.max + 1, random_state=12345)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 8))
    ax1.imshow(data, origin='lower', cmap='Greys_r')
    ax2.imshow(segm, origin='lower', cmap=rand_cmap)
    plt.savefig('../figs/' + str(folder) + '/' + str(file[:-5]) + 'fig2.png')
    plt.close()

    props = source_properties(data, segm)
    tbl = properties_table(props)

    my_min = 100000.

    x_shape = np.float(data.shape[0])
    y_shape = np.float(data.shape[1])

    r = 3.  # approximate isophotal extent
    apertures = []
    for prop in props:
        position = (prop.xcentroid.value, prop.ycentroid.value)
        a = prop.semimajor_axis_sigma.value * r
        b = prop.semiminor_axis_sigma.value * r
        theta = prop.orientation.value
        apertures.append(EllipticalAperture(position, a, b, theta=theta))
        my_dist = np.sqrt((prop.xcentroid.value - x_shape / 2.)**2 +
                          (prop.ycentroid.value - y_shape / 2.)**2)
        if (my_dist < my_min):
            my_label = prop.id - 1
            my_min = my_dist

    mytheta = props[my_label].orientation.value
    mysize = np.int(np.round(r * props[my_label].semimajor_axis_sigma.value))
    my_x = props[my_label].xcentroid.value
    my_y = props[my_label].ycentroid.value

    mask_obj = np.ones(data.shape, dtype='bool')
    mask_obj[(segm.data != 0) * (segm.data != props[my_label].id)] = 0

    weigth = weight[mask_obj]

    rand_cmap = random_cmap(segm.max + 1, random_state=12345)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 8))
    ax1.imshow(data, origin='lower', cmap='Greys_r')
    ax2.imshow(segm, origin='lower', cmap=rand_cmap)
    for aperture in apertures:
        aperture.plot(color='blue', lw=1.5, alpha=0.5, ax=ax1)
        aperture.plot(color='white', lw=1.5, alpha=1.0, ax=ax2)
    plt.savefig('../figs/' + str(folder) + '/' + str(file[:-5]) + 'fig3.png')
    plt.close()

    data_rot = rotate(data, np.rad2deg(mytheta))
    data_rot = data_rot[data_rot.shape[0] / 2 - 100:data_rot.shape[0] / 2 +
                        100, data_rot.shape[1] / 2 -
                        100:data_rot.shape[1] / 2 + 100]

    w_rot = rotate(weight, np.rad2deg(mytheta))
    w = w_rot[w_rot.shape[0] / 2 - 100:w_rot.shape[0] / 2 + 100,
              w_rot.shape[1] / 2 - 100:w_rot.shape[1] / 2 + 100]

    plt.figure()
    plt.imshow(data_rot, origin='lower', cmap='Greys_r')
    plt.savefig('../figs/' + str(folder) + '/' + str(file[:-5]) + 'fig4.png')
    plt.close()

    newx, newy, newtheta, newsize = find_centroid(data_rot)
    print('old center = ', my_x, my_y, mysize)
    print('new center = ', newx, newy, np.rad2deg(newtheta), newsize)

    x_shape2 = np.float(data_rot.shape[0])
    y_shape2 = np.float(data_rot.shape[1])

    np.savetxt(
        '../output/' + str(folder) + '/' + str(file[:-5]) +
        '_size_xcent_ycent_xy_shape.txt',
        np.array([newsize, newx, newy, x_shape2, y_shape2]))

    return data_rot, w, newsize, newx, newy, x_shape2, y_shape2