Example #1
0
def test_load_globals():
    log = logging.getLogger("Aegean")
    sfinder = sf.SourceFinder(log=log)
    filename = 'tests/test_files/1904-66_SIN.fits'
    aux_files = sf.get_aux_files('tests/test_files/1904-66_SIN.fits')
    sfinder.load_globals(filename)
    if sfinder.global_data.img is None: raise AssertionError()

    del sfinder
    sfinder = sf.SourceFinder(log=log)
    sfinder.load_globals(filename, bkgin=aux_files['bkg'], rms=1, mask=aux_files['mask'])
    # region isn't available due to healpy not being installed/required
    if sfinder.global_data.region is None: raise AssertionError()

    del sfinder
    sfinder = sf.SourceFinder(log=log)
    sfinder.load_globals(filename, rmsin=aux_files['rms'], mask='derp', do_curve=False)
    if sfinder.global_data.region is not None: raise AssertionError()
    img = sfinder._load_aux_image(sfinder.global_data.img, filename)
    if img is None: raise AssertionError()

    del sfinder
    sfinder = sf.SourceFinder(log=log)
    aux_files = sf.get_aux_files('tests/test_files/1904-66_SIN.fits')
    from AegeanTools.regions import Region
    sfinder.load_globals(filename, rms=1, mask=Region())
    if sfinder.global_data.region is None: raise AssertionError()
Example #2
0
def test_find_and_prior_parallel():
    log = logging.getLogger("Aegean")
    cores = sf.check_cores(2)
    # don't bother re-running these tests if we have just 1 core
    if cores == 1:
        return
    filename = 'tests/test_files/1904-66_SIN.fits'
    # vanilla source finding
    sfinder = sf.SourceFinder(log=log)
    found = sfinder.find_sources_in_image(filename, cores=cores)
    if not (len(found) == 68): raise AssertionError()
    # now with some options
    aux_files = sf.get_aux_files(filename)

    del sfinder
    sfinder = sf.SourceFinder(log=log)
    _ = sfinder.find_sources_in_image(filename, doislandflux=True, outfile=open('dlme', 'w'), nonegative=False,
                                           rmsin=aux_files['rms'], bkgin=aux_files['bkg'],
                                           mask=aux_files['mask'], cores=cores)
    _ = sfinder.priorized_fit_islands(filename, catalogue=found, doregroup=True, cores=cores, outfile=open('dlme','w'))
    os.remove('dlme')

    del sfinder
    sfinder = sf.SourceFinder(log=log)
    sfinder.find_sources_in_image('tests/test_files/1904-66_SIN_neg.fits', doislandflux=True, nonegative=False, cores=cores)
Example #3
0
def test_island_contours():
    """Test that island contours are correct"""
    log = logging.getLogger("Aegean")
    sfinder = sf.SourceFinder(log=log)
    filename = 'tests/test_files/synthetic_test.fits'
    nsrc = 98
    nisl = 97
    ntot = nsrc + nisl

    # vanilla source finding
    found = sfinder.find_sources_in_image(filename,
                                          cores=1,
                                          rms=0.5,
                                          bkg=0,
                                          doislandflux=True)

    components, islands, simples = classify_catalog(found)
    isle_0_contour = np.array([(41, 405), (41, 406), (41, 407), (42, 407),
                               (42, 408), (42, 409), (43, 409), (43, 410),
                               (44, 410), (45, 410), (46, 410), (47, 410),
                               (47, 409), (48, 409), (48, 408), (49, 408),
                               (49, 407), (49, 406), (49, 405), (48, 405),
                               (48, 404), (48, 403), (47, 403), (46, 403),
                               (45, 403), (44, 403), (43, 403), (43, 404),
                               (42, 404), (42, 405)])
    if not np.all(np.array(islands[0].contour) == isle_0_contour):
        raise AssertionError("Island contour for island 0 is incoorect")
    return
Example #4
0
def test_esimate_lmfit_parinfo():
    """Test estimate_lmfit_parinfo"""
    log = logging.getLogger("Aegean")
    # log.setLevel(logging.DEBUG)
    sfinder = sf.SourceFinder(log=log)

    data = np.zeros(shape=(3, 3))
    rmsimg = np.ones(shape=(3, 3))
    beam = Beam(1, 1, 0)

    # should hit isnegative
    data[1, 1] = -6
    # should hit outerclip is None
    outerclip = None
    # should run error because curve is the wrong shape
    curve = np.zeros((3, 4))
    try:
        sfinder.estimate_lmfit_parinfo(data=data,
                                       rmsimg=rmsimg,
                                       curve=curve,
                                       beam=beam,
                                       innerclip=5,
                                       outerclip=outerclip)
    except AssertionError as e:
        e.message = 'Passed'
    else:
        raise AssertionError(
            "estimate_lmfit_parinfo should err when curve.shape != data.shape")

    return
Example #5
0
def test_psf_with_nans():
    """Test that a psf map with nans doesn't create a crash"""
    log = logging.getLogger("Aegean")
    sfinder = sf.SourceFinder(log=log)
    filename = "tests/test_files/synthetic_test.fits"
    psf = "tests/test_files/synthetic_test_psf.fits"
    # create a test psf map with all major axis being nans
    hdu = fits.open(psf)
    print(hdu[0].data.shape)
    hdu[0].data[0, :, :] = np.nan
    hdu.writeto('dlme_psf.fits')

    try:
        found = sfinder.find_sources_in_image(filename,
                                              cores=1,
                                              rms=0.5,
                                              bkg=0,
                                              imgpsf='dlme_psf.fits')
    except AssertionError as e:
        os.remove('dlme_psf.fits')
        if 'major' in e.args[0]:
            raise AssertionError("Broken on psf maps with nans")
        else:
            raise
    else:
        os.remove('dlme_psf.fits')
    return
Example #6
0
def test_find_and_prior_sources():
    log = logging.getLogger("Aegean")
    sfinder = sf.SourceFinder(log=log)
    filename = 'tests/test_files/small.fits'
    # vanilla source finding
    found = sfinder.find_sources_in_image(filename, cores=1)
    if not (len(found) == 2): raise AssertionError()
    # now with some options
    aux_files = sf.get_aux_files(filename)
    found2 = sfinder.find_sources_in_image(filename, doislandflux=True, outfile=open('dlme', 'w'), nonegative=False,
                                           rmsin=aux_files['rms'], bkgin=aux_files['bkg'],
                                           mask=aux_files['mask'], cores=1, docov=False)
    if not (len(found2) == 4): raise AssertionError()
    isle1 = found2[1]
    if not (isle1.int_flux > 0): raise AssertionError()
    if not (isle1.max_angular_size > 0): raise AssertionError()
    # we should have written some output file
    if not (os.path.exists('dlme')): raise AssertionError()
    os.remove('dlme')

    # pprocess is broken in python3 at the moment so just use 1 core.
    if six.PY3:
        cores = 1
    else:
        cores = 2
    # this should find one less source as one of the source centers is outside the image.
    priorized = sfinder.priorized_fit_islands(filename, catalogue=found, doregroup=False, ratio=1.2, cores=cores, docov=False)
    if not (len(priorized) == 2): raise AssertionError()
    # this also gives 62 sources even though we turn on regroup
    priorized = sfinder.priorized_fit_islands(filename, catalogue=found, doregroup=True, cores=1, outfile=open('dlme','w'), stage=1)
    if not (len(priorized) == 2): raise AssertionError()
    if not (len(sfinder.priorized_fit_islands(filename, catalogue=[])) == 0): raise AssertionError()
    # we should have written some output file
    if not (os.path.exists('dlme')): raise AssertionError()
    os.remove('dlme')
Example #7
0
def find_sources_in_image(imagename,
                          max_summits=5,
                          nsigma=10,
                          usebane=True,
                          region=None,
                          cores=16):
    """
    sources,rmsimage,bgimage=find_sources_in_image(imagename, max_summits=5, nsigma=10, usebane=True, region=None, cores=16)
    runs aegean.find_sources_in_image
    but first runs BANE to get the BG/rms estimates
    if region is supplied (.mim format) only sources inside that will be identified
    """
    if usebane:
        rmsimage, bgimage = get_rms_background(imagename, cores=cores)
    else:
        rmsimage, bgimage = None, None

    sf = source_finder.SourceFinder(log=logging.getLogger('fluxmatch'))
    sources = sf.find_sources_in_image(imagename,
                                       max_summits=max_summits,
                                       innerclip=nsigma,
                                       rmsin=rmsimage,
                                       bkgin=bgimage,
                                       mask=region,
                                       cores=cores)
    return sources, rmsimage, bgimage
Example #8
0
def dont_test_find_and_prior_parallel():
    """Test find/piroirze with parallel operation"""
    log = logging.getLogger("Aegean")
    cores = 1

    filename = 'tests/test_files/synthetic_test.fits'
    # vanilla source finding
    log.info("basic fitting (no bkg/rms")
    sfinder = sf.SourceFinder(log=log)
    found = sfinder.find_sources_in_image(filename,
                                          cores=cores,
                                          bkg=0,
                                          rms=0.5)
    if not (len(found) == 98):
        raise AssertionError('found {0} sources'.format(len(found)))
    # now with some options
    aux_files = sf.get_aux_files(filename)

    del sfinder
    log.info("fitting with supplied bkg/rms and 2 cores")
    cores = 2
    sfinder = sf.SourceFinder(log=log)
    _ = sfinder.find_sources_in_image(filename,
                                      doislandflux=True,
                                      outfile=open('dlme', 'w'),
                                      nonegative=False,
                                      rmsin=aux_files['rms'],
                                      bkgin=aux_files['bkg'],
                                      mask=aux_files['mask'],
                                      cores=cores)

    log.info('now priorised fitting')
    _ = sfinder.priorized_fit_islands(filename,
                                      catalogue=found,
                                      doregroup=True,
                                      cores=cores,
                                      outfile=open('dlme', 'w'))
    os.remove('dlme')

    del sfinder
    log.info('fitting negative sources')
    sfinder = sf.SourceFinder(log=log)
    sfinder.find_sources_in_image('tests/test_files/1904-66_SIN_neg.fits',
                                  doislandflux=True,
                                  nonegative=False,
                                  cores=cores)
Example #9
0
def test_save_files():
    log = logging.getLogger("Aegean")
    sfinder = sf.SourceFinder(log=log)
    filename = 'tests/test_files/small.fits'
    sfinder.save_background_files(image_filename=filename, outbase='dlme')
    for ext in ['bkg', 'rms', 'snr', 'crv']:
        if not (os.path.exists("dlme_{0}.fits".format(ext))): raise AssertionError()
        os.remove("dlme_{0}.fits".format(ext))
Example #10
0
def test_save_image():
    log = logging.getLogger("Aegean")
    sfinder = sf.SourceFinder(log=log)
    filename = 'tests/test_files/small.fits'
    _ = sfinder.find_sources_in_image(filename, cores=1, max_summits=0, blank=True)
    bfile = 'dlme_blanked.fits'
    sfinder.save_image(bfile)
    if not (os.path.exists(bfile)): raise AssertionError()
    os.remove(bfile)
Example #11
0
def test_misc():
    """Test some random things"""
    sf.IslandFittingData()
    sf.DummyLM()
    sf.SourceFinder(ignored=None, log=log)
Example #12
0
def test_find_and_prior_sources():
    """Test find sources and prior sources"""
    log = logging.getLogger("Aegean")
    sfinder = sf.SourceFinder(log=log)
    filename = 'tests/test_files/synthetic_test.fits'
    nsrc = 98
    nisl = 97
    ntot = nsrc + nisl

    # vanilla source finding
    found = sfinder.find_sources_in_image(filename, cores=1, rms=0.5, bkg=0)
    if not (len(found) == nsrc):
        raise AssertionError("Found the wrong number of sources {0}".format(
            len(found)))

    # source finding but not fitting
    found = sfinder.find_sources_in_image(filename,
                                          cores=1,
                                          max_summits=0,
                                          rms=0.5,
                                          bkg=0)
    if not (len(found) == nsrc):
        raise AssertionError("Found the wrong number of sources {0}".format(
            len(found)))

    # now with some options
    aux_files = sf.get_aux_files(filename)
    found2 = sfinder.find_sources_in_image(filename,
                                           doislandflux=True,
                                           outfile=open('dlme', 'w'),
                                           nonegative=False,
                                           rmsin=aux_files['rms'],
                                           bkgin=aux_files['bkg'],
                                           mask=aux_files['mask'],
                                           cores=1,
                                           docov=False)
    if not (len(found2) == ntot):
        raise AssertionError("Found the wrong number of sources {0}".format(
            len(found2)))
    isle1 = found2[1]
    if not (isle1.int_flux > 0):
        raise AssertionError()
    if not (isle1.max_angular_size > 0):
        raise AssertionError()
    # we should have written some output file
    if not (os.path.exists('dlme')):
        raise AssertionError()
    os.remove('dlme')

    # some more tests, now using multiple cores
    cores = 2

    priorized = sfinder.priorized_fit_islands(filename,
                                              catalogue=found,
                                              doregroup=False,
                                              ratio=1.2,
                                              cores=cores,
                                              rmsin=aux_files['rms'],
                                              bkgin=aux_files['bkg'],
                                              docov=False)
    if not (len(priorized) == nsrc):
        raise AssertionError("Found the wrong number of sources {0}".format(
            len(priorized)))

    priorized = sfinder.priorized_fit_islands(filename,
                                              catalogue=found,
                                              doregroup=True,
                                              cores=1,
                                              rmsin=aux_files['rms'],
                                              bkgin=aux_files['bkg'],
                                              outfile=open('dlme', 'w'),
                                              stage=1)
    if not (len(priorized) == nsrc):
        raise AssertionError("Found the wrong number of sources {0}".format(
            len(priorized)))
    if not (len(sfinder.priorized_fit_islands(filename, catalogue=[])) == 0):
        raise AssertionError()
    # we should have written some output file
    if not (os.path.exists('dlme')):
        raise AssertionError("Failed to creat outputfile")
    os.remove('dlme')
Example #13
0
def test_misc():
    sf.IslandFittingData()
    sf.DummyLM()
    sf.SourceFinder(ignored=None, log=log)