Ejemplo n.º 1
0
    def test_linear(self):
        '''Test linear fitting.'''

        order = 1
        results0 = numpy.array([456.0, 0.3, -0.9])

        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid0))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)

        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid1))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)
Ejemplo n.º 2
0
    def test_cubic(self):
        '''Test cubic fitting.'''

        order = 3

        results0 = numpy.array([456.0, 0.3, -0.9, 0.03, -0.01, 0.07,
                                0.0, -10, 0.0, 0.04])
        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid0))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)

        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid1))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)
Ejemplo n.º 3
0
    def test_cuadratic(self):
        '''Test cuadratic fitting.'''

        order = 2

        results0 = numpy.array([1.0, 0.1, 12, 3.0, -11.8, 9.2])

        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid0))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)

        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid1))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)
Ejemplo n.º 4
0
    def test_cuartic(self):
        '''Test cuartic fitting.'''

        order = 4

        results0 = numpy.array([-11.0, -1.5, -0.1, 0.14, -15.03, 0.07,
                                0.448, -0.28, 1.4, 1.24,
                                -3.2, -1.2, 2.24, -8.1, -0.03])
        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid0))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)

        z = numpy.sum(cf * v for cf, v in zip(results0.flat, self.grid1))
        results, = imsurfit(z, order=order)

        for i0, i in zip(results0.flat, results.flat):
            self.assertAlmostEqual(i0, i)
Ejemplo n.º 5
0
def offset_from_crosscor(arr0,
                         arr1,
                         region,
                         refine=True,
                         refine_box=3,
                         order='ij'):
    # import astropy.io.fits as fits
    # allowed values for order
    if order not in ['xy', 'ij']:
        raise ValueError("'order' must be either 'ij' or 'xy'")

    ref_array = arr0
    shape = ref_array.shape
    data1 = filter_region(ref_array[region])
    d1 = standarize(data1)
    # fits.writeto('cutout_%d.fits' % 0, d1, overwrite=True)

    dcenter = numpy.asarray(d1.shape) // 2
    # Correlate

    data2 = filter_region(arr1[region])
    d2 = standarize(data2)
    # fits.writeto('cutout_%d.fits' % idx, d2, overwrite=True)
    #corr = scipy.signal.correlate2d(d1, d2, mode='same', boundary='fill', fillvalue=fillvalue)
    # correlation is equivalent to convolution with inverted image
    corr = scipy.signal.fftconvolve(d1, d2[::-1, ::-1], mode='same')
    # normalize
    corr /= corr.max()
    # fits.writeto('corr_%d.fits' % idx, corr, overwrite=True)
    # fits.writeto('corr2_%d.fits' % idx, corr2 / corr2.max(), overwrite=True)
    # Find peak in cross-cor
    maxindex = numpy.unravel_index(corr.argmax(), corr.shape)

    # Check the peak is above n times the background
    median_corr = numpy.median(corr)
    std_corr = numpy.std(corr)
    peakvalue = corr[maxindex]
    threshold = median_corr + 5 * std_corr
    _logger.debug('The peak value is %f, threshold %f', peakvalue, threshold)
    if peakvalue < threshold:
        raise ValueError('Peak below threshold')

    #baseoff = dcenter - numpy.asarray(maxindex)
    # Pixel (0,0) in reference corresponds to baseoff in image
    # print("Pixel (0,0) in reference corresponds to %s in image" % baseoff)

    if refine:
        # Refine to subpixel
        # Fit a 2D surface to the peak of the crosscorr
        region_ref = utils.image_box(maxindex,
                                     shape,
                                     box=(refine_box, refine_box))

        coeffs, = imsurfit.imsurfit(corr[region_ref], order=2)
        # coefss are a + b *x + c * y + d*x**2 + e * x* y + f * y**2

        try:
            # get peak from coeffs
            xm, ym = vertex_of_quadratic(coeffs)
            # xm ym are offsets

            if abs(xm) > 1 or abs(ym) > 1:
                # probably bad fit
                # dont apply
                final = maxindex
            else:
                final = maxindex + numpy.asarray([ym, xm])
        except ValueError as error:
            _logger.debug('Error fitting peak, %s', error)
            final = maxindex
    else:
        final = maxindex

    refoff = dcenter - final
    # Pixel (0,0) in reference corresponds to baseoff in image
    # print("Pixel (0,0) in reference corresponds to %s in image" % refoff)
    # result xy
    if order == 'xy':
        return refoff[::-1]
    else:
        return refoff
Ejemplo n.º 6
0
def offset_from_crosscor(arr0, arr1, region, refine=True, refine_box=3, order='ij'):
    # import astropy.io.fits as fits
    # allowed values for order
    if order not in ['xy', 'ij']:
        raise ValueError("'order' must be either 'ij' or 'xy'")

    ref_array = arr0
    shape = ref_array.shape
    data1 = filter_region(ref_array[region])
    d1 = standarize(data1)
    # fits.writeto('cutout_%d.fits' % 0, d1, overwrite=True)

    dcenter = numpy.asarray(d1.shape) // 2
    # Correlate

    data2 = filter_region(arr1[region])
    d2 = standarize(data2)
    # fits.writeto('cutout_%d.fits' % idx, d2, overwrite=True)
    #corr = scipy.signal.correlate2d(d1, d2, mode='same', boundary='fill', fillvalue=fillvalue)
    # correlation is equivalent to convolution with inverted image
    corr = scipy.signal.fftconvolve(d1, d2[::-1, ::-1], mode='same')
    # normalize
    corr /= corr.max()
    # fits.writeto('corr_%d.fits' % idx, corr, overwrite=True)
    # fits.writeto('corr2_%d.fits' % idx, corr2 / corr2.max(), overwrite=True)
    # Find peak in cross-cor
    maxindex = numpy.unravel_index(corr.argmax(), corr.shape)

    # Check the peak is above n times the background
    median_corr = numpy.median(corr)
    std_corr = numpy.std(corr)
    peakvalue = corr[maxindex]
    threshold = median_corr + 5 * std_corr
    _logger.debug('The peak value is %f, threshold %f', peakvalue, threshold)
    if peakvalue < threshold:
        raise ValueError('Peak below threshold')

    #baseoff = dcenter - numpy.asarray(maxindex)
    # Pixel (0,0) in reference corresponds to baseoff in image
    # print("Pixel (0,0) in reference corresponds to %s in image" % baseoff)

    if refine:
        # Refine to subpixel
        # Fit a 2D surface to the peak of the crosscorr
        region_ref = utils.image_box(maxindex, shape, box=(refine_box, refine_box))

        coeffs, = imsurfit.imsurfit(corr[region_ref], order=2)
        # coefss are a + b *x + c * y + d*x**2 + e * x* y + f * y**2

        try:
            # get peak from coeffs
            xm, ym = vertex_of_quadratic(coeffs)
            # xm ym are offsets

            if abs(xm) > 1 or abs(ym) > 1:
                # probably bad fit
                # dont apply
                final = maxindex
            else:
                final = maxindex + numpy.asarray([ym, xm])
        except ValueError as error:
            _logger.debug('Error fitting peak, %s', error)
            final = maxindex
    else:
        final = maxindex

    refoff = dcenter - final
    # Pixel (0,0) in reference corresponds to baseoff in image
    # print("Pixel (0,0) in reference corresponds to %s in image" % refoff)
    # result xy
    if order == 'xy':
        return refoff[::-1]
    else:
        return refoff