Exemplo n.º 1
0
def image_chisqr(modelresults, observations, wavelength=None, write=True,
        normalization='total', registration='integer_pixel_shift'):
    """
    Not written yet - this is just a placeholder

    Parameters
    ------------
    wavelength : float
        Wavelength of the image to compute the chi squared of. 
    write : bool
        If set, write output to a file, in addition to displaying
        on screen. 

    """
    image = np.asarray(observations.image)
    psf = np.asarray(observations.psf)
    model = np.asarray(model.image)
    
    # Convolve the model image with the appropriate psf
    model = image_registration.fft_tools.convolve_nd.convolvend(model,psf)
    # Determine the shift between model image and observations via fft cross correlation
    dy,dx,xerr,yerr = image_registration.chi2_shift_iterzoom(model,image)
    # Shift the model image to the same location as observations
    model = scipy.ndimage.interpolation.shift(model,np.asarray((dx,dy)))

    # Normalize model to observed image and calculate chisqrd
    weightgd=total(image)/total(model)
    gd=gd*weightgd
    subgd=image-model

    chisqr=(image-model)^2.0/noise^2.0
    chisqr=total(chisqr)


    return chisqr
Exemplo n.º 2
0
    def find_optimal_coo(self):
        psf_fn = self.psf_fn
        scr_fn = self.scr_fn
        pixX = self.pixX
        pixY = self.pixY
        imgpath = self.imgpath

        hd = fits.open(imgpath)[1].header
        w = wcs.WCS(hd)
        xoff, yoff, exoff, eyoff = chi2_shift_iterzoom(psf_fn, scr_fn)
        pixX_cor = pixX + xoff
        pixY_cor = pixY + yoff
        pixel = np.array([[pixX_cor, pixY_cor]], np.float_)
        newcrd = w.wcs_pix2world(pixel, 0)
        ra_cor = newcrd[0][0]
        dec_cor = newcrd[0][1]
        self.ra_cor = ra_cor
        self.dec_cor = dec_cor
Exemplo n.º 3
0
def image_chisqr(modelresults,
                 observations,
                 wavelength=None,
                 write=True,
                 normalization='total',
                 registration='integer_pixel_shift'):
    """
    Not written yet - this is just a placeholder

    Parameters
    ------------
    wavelength : float
        Wavelength of the image to compute the chi squared of. 
    write : bool
        If set, write output to a file, in addition to displaying
        on screen. 

    """
    image = np.asarray(observations.image)
    psf = np.asarray(observations.psf)
    model = np.asarray(model.image)

    # Convolve the model image with the appropriate psf
    model = image_registration.fft_tools.convolve_nd.convolvend(model, psf)
    # Determine the shift between model image and observations via fft cross correlation
    dy, dx, xerr, yerr = image_registration.chi2_shift_iterzoom(model, image)
    # Shift the model image to the same location as observations
    model = scipy.ndimage.interpolation.shift(model, np.asarray((dx, dy)))

    # Normalize model to observed image and calculate chisqrd
    weightgd = total(image) / total(model)
    gd = gd * weightgd
    subgd = image - model

    chisqr = (image - model) ^ 2.0 / noise ^ 2.0
    chisqr = total(chisqr)

    return chisqr
Exemplo n.º 4
0
def image_likelihood(modelresults,
                     observations,
                     image_covariance,
                     wavelength=None,
                     write=True,
                     normalization='total',
                     registration='sub_pixel'):
    """
    Not written yet - this is just a placeholder

    Parameters
    ------------
    wavelength : float
        Wavelength of the image to compute the log likelihood of a model image.
    write : bool
        If set, write output to a file, in addition to displaying
        on screen.

    """

    mod_inclinations = modelresults.parameters.inclinations

    im = observations.images
    mask = im[wavelength].mask
    image = im[wavelength].image
    noise = im[wavelength].uncertainty
    psf = im[wavelength].psf
    model = modelresults.images[wavelength].data

    #mask[:,:]=1
    sz = len(mod_inclinations)
    chisqr = np.zeros(sz)
    loglikelihood = np.zeros(sz)

    # Unpack covariance structure
    covariance = image_covariance[0]
    covariance_inv = image_covariance[1]
    logdet = image_covariance[2]
    sign = image_covariance[3]

    for n in np.arange(sz):
        model_n = np.asarray(model[0, 0, n, :, :])

        # Convolve the model image with the appropriate psf
        model_n = np.asarray(
            image_registration.fft_tools.convolve_nd.convolvend(model_n, psf))
        # Determine the shift between model image and observations via fft cross correlation

        # Normalize model to observed image and calculate chisqrd
        weightgd = image.sum() / model_n.sum()
        model_n *= weightgd

        #model_n=np.multiply(model_n,mask)
        #image=np.multiply(image,mask)
        dy, dx, xerr, yerr = image_registration.chi2_shift_iterzoom(
            model_n, image)

        if registration == 'integer_pixel':
            dx = np.round(dx)
            dy = np.round(dy)
        #if registration == 'sub_pixel':
        #print dx, dy
        # Shift the model image to the same location as observations
        model_n = scipy.ndimage.interpolation.shift(model_n,
                                                    np.asarray((dx, dy)))

        matrix_model = model_n[mask != 0]
        matrix_obs = image[mask != 0]
        residual = matrix_obs - matrix_model
        nx = residual.shape[0]

        residual = residual[:, None]  # Changing this to a column vector
        #print residual.min()
        #print residual.max()
        #print np.transpose(residual).shape
        matrix_product = np.dot(np.dot(np.transpose(residual), covariance_inv),
                                residual)
        loglikelihood[n] = -0.5 * (matrix_product[0][0] + sign * logdet +
                                   nx * np.log(2.0 * np.pi))
        #print 'matrix_product',matrix_product[0][0]
        #print 'logdet',logdet

        chisquared = (image - model_n)**2.0 / noise**2.0
        chisqr[n] = chisquared[mask != 0].sum()  #/(mask[mask != 0].shape[0])
        chisqr = np.asarray(chisqr)

        _log.info("inclination {0} : {1:4.1f} deg has chi2 = {2:7f}".format(
            n, mod_inclinations[n], chisqr[n]))
        _log.info("inclination {0} : {1:4.1f} deg has loglike = {2:7f}".format(
            n, mod_inclinations[n], loglikelihood[n]))

    return loglikelihood
Exemplo n.º 5
0
def image_chisqr(modelresults,
                 observations,
                 wavelength=None,
                 write=True,
                 normalization='total',
                 registration='sub_pixel',
                 inclinationflag=True,
                 convolvepsf=True):
    """
    Not written yet - this is just a placeholder

    Parameters
    ------------
    wavelength : float
        Wavelength of the image to compute the chi squared of.
    write : bool
        If set, write output to a file, in addition to displaying
        on screen.

    """

    if inclinationflag:
        mod_inclinations = modelresults.parameters.inclinations
    else:
        mod_inclination = ['0.0']

    im = observations.images
    mask = im[wavelength].mask
    image = im[wavelength].image
    noise = im[wavelength].uncertainty
    if convolvepsf:
        psf = im[wavelength].psf
    model = modelresults.images[wavelength].data

    #mask[:,:]=1
    sz = len(mod_inclinations)
    chisqr = np.zeros(sz)

    for n in np.arange(sz):
        if inclinationflag:
            model_n = np.asarray(model[0, 0, n, :, :])
        else:
            model_n = np.asarray(model)

        # Convolve the model image with the appropriate psf
        if convolvepsf:
            model_n = np.asarray(
                image_registration.fft_tools.convolve_nd.convolvend(
                    model_n, psf))
        # Determine the shift between model image and observations via fft cross correlation

        # Normalize model to observed image and calculate chisqrd
        weightgd = image.sum() / model_n.sum()
        model_n *= weightgd
        subgd = image - model_n

        #model_n=np.multiply(model_n,mask)
        #image=np.multiply(image,mask)
        dy, dx, xerr, yerr = image_registration.chi2_shift_iterzoom(
            model_n, image)

        if registration == 'integer_pixel':
            dx = np.round(dx)
            dy = np.round(dy)
        #if registration == 'sub_pixel':
        #print dx, dy
        # Shift the model image to the same location as observations
        model_n = scipy.ndimage.interpolation.shift(model_n,
                                                    np.asarray((dx, dy)))

        chisquared = (image - model_n)**2.0 / noise**2.0
        chisqr[n] = chisquared[mask != 0].sum()  #/2500.0
        if dx == 0 or dy == 0:
            chisqr[n] = chisqr[n - 1] + 1.0


#        modelresults.images.closeimage
        _log.info("inclination {0} : {1:4.1f} deg has chi2 = {2:5g}".format(
            n, mod_inclinations[n], chisqr[n]))

    return chisqr
Exemplo n.º 6
0
def image_likelihood(modelresults, observations, image_covariance, wavelength=None, write=True,
        normalization='total', registration='sub_pixel'):
    """
    Not written yet - this is just a placeholder

    Parameters
    ------------
    wavelength : float
        Wavelength of the image to compute the log likelihood of a model image.
    write : bool
        If set, write output to a file, in addition to displaying
        on screen.

    """

    mod_inclinations = modelresults.parameters.inclinations

    im = observations.images
    mask = im[wavelength].mask
    image = im[wavelength].image
    noise = im[wavelength].uncertainty
    psf = im[wavelength].psf
    model = modelresults.images[wavelength].data


    #mask[:,:]=1
    sz = len(mod_inclinations)
    chisqr = np.zeros(sz)
    loglikelihood = np.zeros(sz)


    # Unpack covariance structure
    covariance = image_covariance[0]
    covariance_inv = image_covariance[1]
    logdet = image_covariance[2]
    sign = image_covariance[3]



    for n in np.arange(sz):
        model_n = np.asarray(model[0,0,n,:,:])

        # Convolve the model image with the appropriate psf
        model_n = np.asarray(image_registration.fft_tools.convolve_nd.convolvend(model_n,psf))
        # Determine the shift between model image and observations via fft cross correlation

        # Normalize model to observed image and calculate chisqrd
        weightgd=image.sum()/model_n.sum()
        model_n*=weightgd



        #model_n=np.multiply(model_n,mask)
        #image=np.multiply(image,mask)
        dy,dx,xerr,yerr = image_registration.chi2_shift_iterzoom(model_n,image)

        if registration == 'integer_pixel':
            dx = np.round(dx)
            dy = np.round(dy)
        #if registration == 'sub_pixel':
            #print dx, dy
        # Shift the model image to the same location as observations
        model_n = scipy.ndimage.interpolation.shift(model_n,np.asarray((dx,dy)))

        matrix_model = model_n[mask != 0]
        matrix_obs = image[mask != 0]
        residual = matrix_obs - matrix_model
        nx = residual.shape[0]

        residual = residual[:,None] # Changing this to a column vector
        #print residual.min()
        #print residual.max()
        #print np.transpose(residual).shape
        matrix_product = np.dot(np.dot(np.transpose(residual),covariance_inv),residual)
        loglikelihood[n] = -0.5*(matrix_product[0][0] + sign*logdet + nx*np.log(2.0*np.pi))
        #print 'matrix_product',matrix_product[0][0]
        #print 'logdet',logdet

        chisquared=(image-model_n)**2.0/noise**2.0
        chisqr[n]=chisquared[mask !=0].sum()#/(mask[mask != 0].shape[0])
        chisqr = np.asarray(chisqr)

        _log.info( "inclination {0} : {1:4.1f} deg has chi2 = {2:7f}".format(n, mod_inclinations[n], chisqr[n]))
        _log.info( "inclination {0} : {1:4.1f} deg has loglike = {2:7f}".format(n, mod_inclinations[n], loglikelihood[n]))

    return loglikelihood
Exemplo n.º 7
0
def image_chisqr(modelresults, observations, wavelength=None, write=True,
        normalization='total', registration='sub_pixel',
        inclinationflag=True, convolvepsf=True):
    """
    Not written yet - this is just a placeholder

    Parameters
    ------------
    wavelength : float
        Wavelength of the image to compute the chi squared of.
    write : bool
        If set, write output to a file, in addition to displaying
        on screen.

    """

    if inclinationflag:
        mod_inclinations = modelresults.parameters.inclinations
    else:
        mod_inclination = ['0.0']

    im = observations.images
    mask = im[wavelength].mask
    image = im[wavelength].image
    noise = im[wavelength].uncertainty
    if convolvepsf:
        psf = im[wavelength].psf
    model = modelresults.images[wavelength].data


    #mask[:,:]=1
    sz = len(mod_inclinations)
    chisqr = np.zeros(sz)

    for n in np.arange(sz):
        if inclinationflag:
            model_n = np.asarray(model[0,0,n,:,:])
        else:
            model_n = np.asarray(model)

        # Convolve the model image with the appropriate psf
        if convolvepsf:
            model_n = np.asarray(image_registration.fft_tools.convolve_nd.convolvend(model_n,psf))
        # Determine the shift between model image and observations via fft cross correlation

        # Normalize model to observed image and calculate chisqrd
        weightgd=image.sum()/model_n.sum()
        model_n*=weightgd
        subgd=image-model_n

        #model_n=np.multiply(model_n,mask)
        #image=np.multiply(image,mask)
        dy,dx,xerr,yerr = image_registration.chi2_shift_iterzoom(model_n,image)

        if registration == 'integer_pixel':
            dx = np.round(dx)
            dy = np.round(dy)
        #if registration == 'sub_pixel':
            #print dx, dy
        # Shift the model image to the same location as observations
        model_n = scipy.ndimage.interpolation.shift(model_n,np.asarray((dx,dy)))

        chisquared=(image-model_n)**2.0/noise**2.0
        chisqr[n]=chisquared[mask !=0].sum()#/2500.0
        if dx == 0 or dy == 0:
            chisqr[n]=chisqr[n-1]+1.0

#        modelresults.images.closeimage
        _log.info( "inclination {0} : {1:4.1f} deg has chi2 = {2:5g}".format(n, mod_inclinations[n], chisqr[n]))

    return chisqr
    b = crpb3 * (pixb4 / pixb3)**2 - (almaimf_b3.spectral_axis * m)
    return (m * nu + b).decompose().value


alma_b4_interp = interp_almaimf(
    pdbi_b4.with_spectral_unit(u.GHz).spectral_axis[0])

fits.writeto('ALMAIMF_B4interp_IRS2_proj_to_PBDI.fits',
             data=alma_b4_interp.decompose().value,
             header=pdbi_b4[0].header,
             overwrite=True)

slc = (slice(310, 329), slice(528, 553))

im1 = alma_b4_interp[slc]
im2 = pdbi_b4[0].value[slc]

print(image_registration.chi2_shift_iterzoom(im1, im2))
print(
    image_registration.chi2_shift_iterzoom(im1,
                                           im2,
                                           verbose=True,
                                           zeromean=True))
print(image_registration.chi2_shift(im1, im2))
print(image_registration.chi2_shift(im1, im2, zeromean=True))
print(image_registration.chi2_shift(alma_b4_interp, pdbi_b4[0].value))
print(
    image_registration.chi2_shift(alma_b4_interp,
                                  pdbi_b4[0].value,
                                  zeromean=True))
chi2shift = image_registration.chi2_shift(proj_7mmto3mm,
                                          cutout_3mm.data,
                                          err=errest,
                                          upsample_factor=1000)
print(chi2shift)
print(chi2shift[:2] * cutout_3mm.wcs.wcs.cdelt * 3600)
print(chi2shift[:2] * cutout_3mm.wcs.wcs.cdelt)
print((((chi2shift[:2] * cutout_3mm.wcs.wcs.cdelt * 3600)**2).sum())**0.5)
"""
[-4.295500000000004, 3.9625000000000057, 0.0034999999999999892, 0.003500000000000003]
[0.0214775 0.0198125]
[5.96597222e-06 5.50347222e-06]
"""
ichi2shift = image_registration.chi2_shift_iterzoom(proj_7mmto3mm,
                                                    cutout_3mm.data,
                                                    err=errest,
                                                    upsample_factor=1000)
print(ichi2shift)

from image_registration.fft_tools import shift
xoff, yoff = chi2shift[:2]
corrected_7mm = shift.shiftnd(proj_7mmto3mm, (yoff, xoff))

import pylab as pl
pl.figure(1).clf()
pl.subplot(2, 2, 1).imshow(proj_7mmto3mm, origin='lower')
pl.subplot(2, 2, 2).imshow(cutout_3mm.data, origin='lower')
pl.subplot(2, 2, 3).imshow(proj_7mmto3mm * 3 - cutout_3mm.data, origin='lower')
pl.subplot(2, 2, 4).imshow(corrected_7mm * 3 - cutout_3mm.data, origin='lower')