Пример #1
0
 def testSubtractNoBeta(self):
     D, P, S_corr, mix_mask = ps.diff(self.si_ref, self.si_new,
                                      beta=False)
     self.assertIsInstance(D, np.ndarray)
     self.assertIsInstance(P, np.ndarray)
     self.assertIsInstance(S_corr, np.ndarray)
     self.assertIsInstance(mix_mask, np.ndarray)
Пример #2
0
 def testSubtractNoFitPSF(self):
     D, P, S_corr, mix_mask = ps.diff(self.si_ref, self.si_new,
                                      beta=False, iterative=False,
                                      shift=False, fitted_psf=False)
     self.assertIsInstance(D, np.ndarray)
     self.assertIsInstance(P, np.ndarray)
     self.assertIsInstance(S_corr, np.ndarray)
     self.assertIsInstance(mix_mask, np.ndarray)
Пример #3
0
 def testSubtractOnlyShift(self):
     D, P, S_corr, mix_mask = ps.diff(self.si_ref, self.si_new,
                                      beta=False, iterative=False,
                                      shift=True)
     self.assertIsInstance(D, np.ndarray)
     self.assertIsInstance(P, np.ndarray)
     self.assertIsInstance(S_corr, np.ndarray)
     self.assertIsInstance(mix_mask, np.ndarray)
Пример #4
0
def main(args):
    imgsdir = '/home/bruno/Documentos/Data/SNiPTF/imgs'
    imgsdir = '/home/bruno/Data/SNiPTF/imgs'

    #imgsdir = '/home/bruno/Documentos/Data/LIGO_O2/20171116/ESO202-009'
    dest_dir = './test/test_images/test_sub_sniptf'
    imgs = glob.glob(imgsdir + '/*sci*.fits')
    mask = glob.glob(imgsdir + '/*mask*.fits')

    imgs.sort()
    mask.sort()

    images = [
        s.SingleImage(animg, mask=amask) for animg, amask in zip(imgs, mask)
    ]

    #images = [s.SingleImage(animg) for animg in imgs]
    for i, animg in enumerate(images[1:]):
        #~ t, _ = aa.find_transform(animg.data, images[0].data)

        #~ if abs(t.rotation)>5e-4:
        #~ k = t.__class__
        #~ t = k(np.round(t.params, decimals=5))

        #~ reg = aa.apply_transform(t, animg.data, images[0].data)
        reg = aa.register(animg.data, images[0].data)
        new = s.SingleImage(reg.data, mask=reg.mask)

        fits.writeto('/home/bruno/aligned_{}.fits'.format(i),
                     reg.data,
                     overwrite=True)

        D, P, S_corr, mask = ps.diff(images[0],
                                     new,
                                     align=True,
                                     iterative=False,
                                     shift=True,
                                     beta=True)

        D = np.ma.MaskedArray(D.real, mask).filled(np.median(D.real))
        fits.writeto(os.path.join(dest_dir, 'Diff_{}.fits'.format(i)),
                     D.real,
                     overwrite=True)
        fits.writeto(os.path.join(dest_dir, 'P_{}.fits'.format(i)),
                     P.real,
                     overwrite=True)
        fits.writeto(os.path.join(dest_dir, 'Scorr_{}.fits'.format(i)),
                     S_corr,
                     overwrite=True)
        new._clean()
    for an_img in images:
        an_img._clean()

    return
Пример #5
0
def main(imgs_dir, ref_path, new_path, details):

    if not os.path.isdir(imgs_dir):
        os.makedirs(imgs_dir)

    ref_dest = os.path.join(imgs_dir, 'ref.fits')
    new_dest = os.path.join(imgs_dir, 'new.fits')

    os.rename(ref_path, ref_dest)
    os.rename(new_path, new_dest)

    ref = si.SingleImage(ref_dest, borders=False) #, crop=((150,150), (150, 150)))
    new = si.SingleImage(new_dest, borders=False) #, crop=((150,150), (150, 150)))

    #~ ##  Adding stars
    foo = new.cov_matrix
    srcs = new.best_sources

    rows = []
    for i in range(15):
        j = np.random.choice(len(srcs), 1, replace=False)
        flux = srcs['flux'][j][0]
        xs = srcs['x'][j][0]
        ys = srcs['y'][j][0]
        position = (ys, xs)
        sx, sy = new.stamp_shape
        sx += 3
        sy += 3
        star = extract_array(new.pixeldata.data,
                             (sx, sy), position,
                             mode='partial',
                             fill_value=new._bkg.globalrms)
        #print flux
        #~ star = flux*new.db.load(j)[0]

        x = np.random.choice(new.pixeldata.shape[0]-3*sx, 1)[0] + sx#* np.random.random())
        y = np.random.choice(new.pixeldata.shape[1]-3*sy, 1)[0] + sy
        # np.int((new.pixeldata.shape[1]-star.shape[1]) * np.random.random())
        #~ if new.pixeldata.data[x:x+sx, y:y+sy].shape != star.shape:
            #~ import ipdb; ipdb.set_trace()
        new.pixeldata.data[x:x+sx, y:y+sy] = star
            #~ except:
                #~ continue
        xc = x+sx/2.
        yc = y+sy/2.
        app_mag = -2.5*np.log10(flux)+25.

        rows.append([yc, xc, app_mag, flux])

    newcat = Table(rows=rows, names=['x', 'y', 'app_mag', 'flux'])
    fits.writeto(filename=new_dest, header=fits.getheader(new_dest),
                 data=new.pixeldata.data, overwrite=True)
    new._clean()
    new = si.SingleImage(new_dest, borders=False) #, crop=((150,150), (150, 150)))
    newcat.write(os.path.join(imgs_dir, 'transient.list'),
                           format='ascii.fast_no_header',
                           overwrite=True)

    fits.writeto(filename=os.path.join(imgs_dir, 'interp_ref.fits'), data=ref.interped, overwrite=True)
    fits.writeto(filename=os.path.join(imgs_dir, 'interp_new.fits'), data=new.interped, overwrite=True)

    try:
        print 'Images to be subtracted: {} {}'.format(ref_dest, new_dest)
        import time
        t0 = time.time()
        D, P, S, mask = ps.diff(ref, new, align=False,
                               iterative=False, shift=False, beta=True)
        dt_z = time.time() - t0
        new._clean()
        ref._clean()
        mea, med, std = sigma_clipped_stats(D.real)
        D = np.ma.MaskedArray(D.real, mask).filled(mea)

        fits.writeto(os.path.join(imgs_dir,'diff.fits'), D, overwrite=True)
        #~ utils.encapsule_R(D, path=os.path.join(imgs_dir, 'diff.fits'))
        utils.encapsule_R(P, path=os.path.join(imgs_dir, 'psf_d.fits'))
        utils.encapsule_R(S, path=os.path.join(imgs_dir, 's_diff.fits'))

        scorrdetected = utils.find_S_local_maxima(S, threshold=3.5)
        print 'S_corr found thath {} transients were above 3.5 sigmas'.format(len(scorrdetected))
        ascii.write(table=np.asarray(scorrdetected),
                output=os.path.join(imgs_dir, 's_corr_detected.csv'),
                names=['X_IMAGE', 'Y_IMAGE', 'SIGNIFICANCE'],
                format='csv')

        S = np.ascontiguousarray(S)
        #~ s_bkg = sep.Background(S)
        mean, median, std = sigma_clipped_stats(S)
        sdetected = sep.extract(S-median, 3.5*std,
                                filter_kernel=None)
        print 'S_corr with sep found thath {} transients were above 3.5 sigmas'.format(len(sdetected))
        ascii.write(table=sdetected,
                    output=os.path.join(imgs_dir, 'sdetected.csv'),
                    format='csv')

    ##  With OIS
        t0 = time.time()
        ois_d = ois.optimal_system(fits.getdata(new_dest), fits.getdata(ref_dest))[0]
        dt_o = time.time() - t0
        utils.encapsule_R(ois_d, path=os.path.join(imgs_dir, 'diff_ois.fits'))

    ##  With HOTPANTS
        t0 = time.time()
        os.system('hotpants -v 0 -inim {} -tmplim {} -outim {} -r 15 -tu 40000 -tl -100 -il -100 -iu 40000'.format(new_dest, ref_dest,
            os.path.join(imgs_dir, 'diff_hot.fits')))
        dt_h = time.time() - t0

        return [newcat.to_pandas(), [dt_z, dt_o, dt_h]]
    except:
        raise
Пример #6
0
def main(args):
    imgsdir = '/home/bruno/Data/LIGO_O2/Jan04/newstacks/PGC073926'
    dest_dir = './test/test_images/test_propersubtract'
    imgs = glob.glob(imgsdir + '/pgc073926_17*.fits')
    #mask = glob.glob(imgsdir+'/*mask*.fits')

    imgs.sort()
    #mask.sort()

    #images = [s.SingleImage(animg, mask=amask) for animg, amask in zip(imgs, mask)]
    images = []
    for animg in imgs:
        print(animg)
        try:
            o = s.SingleImage(animg,
                              borders=True,
                              crop=((150, 150), (150, 150)))
            images.append(o)
        except:
            pass
        #~ hdu = fits.open(animg)
        #~ data = hdu[0].data

    #~ images = [s.SingleImage(animg, borders=True, crop=((250,250), (250, 250)))
    #~ for animg in imgs]
    fits.writeto(os.path.join(dest_dir, 'InterpedRef.fits'),
                 images[0].interped,
                 overwrite=True)

    for i, animg in enumerate(images[1:]):
        #~ ## Erasing stars
        srcs = animg.best_sources
        f60 = np.percentile(srcs['cflux'], q=60)
        jj = np.random.choice(len(srcs), len(srcs) / 3, replace=False)
        mea, med, std = sigma_clipped_stats(animg.data)
        sx, sy = animg.stamp_shape
        st = animg._bkg.globalrms
        for aj in jj:
            star = srcs[aj]
            x = star['x']
            y = star['y']
            if x < sx or animg.data.shape[0] - x < sx:
                continue
            if y < sy or animg.data.shape[1] - y < sy:
                continue
            if star['cflux'] < f60:
                continue
            print(x, y)
            noise = np.random.normal(loc=med, scale=st, size=animg.stamp_shape)
            animg.data.data[np.int(x - sx / 2.):np.int(x + sx / 2.),
                            np.int(y - sy / 2.):np.int(y + sy / 2.)] = noise

        ##  Adding stars
        #~ foo = animg.cov_matrix
        #~ srcs = animg.best_sources

        #~ jj = np.random.choice(len(srcs), 12, replace=False)
        #~ for aj in jj:
        #~ star = animg.db.load(aj)[0]
        #~ x, y = np.random.choice(np.min(animg.data.shape)-np.max(star.shape),
        #~ 2, replace=True)
        #~ print star.shape
        #~ animg.data.data[x:x+star.shape[0], y:y+star.shape[1]] = star
        #~ xc, yc = x+star.shape[0]/2., y+star.shape[1]/2.
        #~ print xc, yc
        fits.writeto(os.path.join(dest_dir, 'InterpedNew_{}.fits'.format(i)),
                     animg.interped,
                     overwrite=True)
        try:
            D, P, S_corr, mask = ps.diff(animg,
                                         images[0],
                                         align=True,
                                         iterative=False,
                                         shift=False,
                                         beta=True)
            mea, med, std = sigma_clipped_stats(D.real, mask)
            D = np.ma.MaskedArray(D.real, mask).filled(mea)
            fits.writeto(os.path.join(dest_dir, 'Diff_{}.fits'.format(i)),
                         D,
                         overwrite=True)
            fits.writeto(os.path.join(dest_dir, 'P_{}.fits'.format(i)),
                         P.real,
                         overwrite=True)
            fits.writeto(os.path.join(dest_dir, 'Scorr_{}.fits'.format(i)),
                         S_corr,
                         overwrite=True)
        except:
            print('subtraction failed')
            print('ref: ', images[0])
            print('new: ', animg)

    for an_img in images:
        an_img._clean()

    return