Ejemplo n.º 1
0
def test_error_only_one_file(niri_adinputs, caplog):
    # With only one file
    p = NIRIImage(niri_adinputs[1:])
    p.stackFrames()
    assert caplog.records[2].message == (
        'No stacking will be performed, since at least two input AstroData '
        'objects are required for stackFrames')
Ejemplo n.º 2
0
def test_error_extension_number(niri_adinputs, caplog):
    p = NIRIImage(niri_adinputs)
    p.prepare()
    niri_adinputs[1].append(np.zeros((2, 2)))
    match = "Not all inputs have the same number of extensions"
    with pytest.raises(IOError, match=match):
        p.stackFrames()
Ejemplo n.º 3
0
    def test_addToList(self):
        filenames = [
            'N20070819S{:04d}_flatCorrected.fits'.format(i)
            for i in range(104, 109)
        ]

        adinputs = [
            astrodata.open(os.path.join(TESTDATAPATH, 'NIRI', f))
            for f in filenames
        ]

        # Add one image twice, just for laughs; it should appear only once
        adinputs.append(adinputs[0])

        p = NIRIImage(adinputs)
        p.stacks = {}
        p.addToList(purpose='forTest')

        for f in filenames:
            newfilename = f.replace('flatCorrected', 'forTest')
            assert os.path.exists(newfilename)
            os.remove(newfilename)

        # Check there's one stack of length 5
        assert len(p.stacks) == 1
        assert len(p.stacks[p.stacks.keys()[0]]) == 5
Ejemplo n.º 4
0
def niriprim2():
    file_path = download_from_archive("N20190120S0287.fits")
    ad = astrodata.open(file_path)
    ad.append(ad[0])
    p = NIRIImage([ad])
    p.addDQ()
    return p
Ejemplo n.º 5
0
def test_error_extension_shape(niri_adinputs, caplog):
    niri_adinputs[1][0].data = np.zeros((3, 3))
    p = NIRIImage(niri_adinputs)
    p.prepare()
    match = "Not all inputs images have the same shape"
    with pytest.raises(IOError, match=match):
        p.stackFrames()
Ejemplo n.º 6
0
 def test_prepare(self):
     ad = astrodata.open(
         os.path.join(TESTDATAPATH, 'NIRI', 'N20070819S0104.fits'))
     p = NIRIImage([ad])
     ad = p.prepare()[0]
     assert ad_compare(
         ad,
         os.path.join(TESTDATAPATH, 'NIRI', 'N20070819S0104_prepared.fits'))
Ejemplo n.º 7
0
 def test_addVAR(self):
     ad = astrodata.open(
         os.path.join(TESTDATAPATH, 'NIRI',
                      'N20070819S0104_ADUToElectrons.fits'))
     p = NIRIImage([ad])
     ad = p.addVAR(read_noise=True, poisson_noise=True)[0]
     assert ad_compare(
         ad,
         os.path.join(TESTDATAPATH, 'NIRI', 'N20070819S0104_varAdded.fits'))
Ejemplo n.º 8
0
def test_adu_to_electrons(astrofaker):
    ad = astrofaker.create("NIRI", "IMAGE")
    # astrodata.open(os.path.join(TESTDATAPATH, 'NIRI', 'N20070819S0104_dqAdded.fits'))
    p = NIRIImage([ad])
    ad = p.ADUToElectrons()[0]
    assert ad_compare(
        ad,
        os.path.join(TESTDATAPATH, 'NIRI',
                     'N20070819S0104_ADUToElectrons.fits'))
Ejemplo n.º 9
0
 def test_nonlinearityCorrect(self):
     ad = astrodata.open(
         os.path.join(TESTDATAPATH, 'NIRI', 'N20070819S0104_varAdded.fits'))
     p = NIRIImage([ad])
     ad = p.nonlinearityCorrect()[0]
     assert ad_compare(
         ad,
         os.path.join(TESTDATAPATH, 'NIRI',
                      'N20070819S0104_nonlinearityCorrected.fits'))
Ejemplo n.º 10
0
def test_rejmap(niri_adinputs):
    for i in (2, 3, 4):
        niri_adinputs.append(niri_adinputs[0] + i)

    p = NIRIImage(niri_adinputs)
    p.prepare()
    adout = p.stackFrames(reject_method='minmax', nlow=1, nhigh=1,
                          save_rejection_map=True)[0]
    assert_array_equal(adout[0].REJMAP, 2)  # rejected 2 values for each pixel
Ejemplo n.º 11
0
def niri_image(astrofaker):
    ad = astrofaker.create('NIRI', 'IMAGE')
    ad.init_default_extensions()

    # SExtractor struggles if the background is noiseless
    ad.add_read_noise()

    for x, y in STAR_POSITIONS:
        ad[0].add_star(amplitude=500, x=x, y=y)

    return NIRIImage([ad])
Ejemplo n.º 12
0
def niri_images(astrofaker):
    """Create two NIRI images, one all 1s, the other all 2s"""
    adinputs = []
    for i in (1, 2):
        ad = astrofaker.create('NIRI', 'IMAGE')
        ad.init_default_extensions()
        ad[0].data += i

    adinputs.append(ad)

    return NIRIImage(adinputs)
Ejemplo n.º 13
0
def test_apply_dq_plane(astrofaker):
    ad = astrofaker.create("NIRI", "IMAGE")

    # astrodata.open(os.path.join(TESTDATAPATH, 'NIRI', 'N20070819S0104_nonlinearityCorrected.fits'))

    p = NIRIImage([ad])
    ad = p.applyDQPlane()[0]

    assert ad_compare(
        ad,
        os.path.join(TESTDATAPATH, 'NIRI',
                     'N20070819S0104_dqPlaneApplied.fits'))
Ejemplo n.º 14
0
def test_shift_images(astrofaker, trim_data, file_write, path_to_outputs):
    """
    Creates several fake AD objects with a single source in different
    locations and then shifts them. Checks that the output image sizes are
    correct and shifts are correct from the location of the brightest pixel

    This tests trimming and not trimming images, and passing the shifts
    via a string and a file on disk.
    """
    offsets = ((-10, -10), (-10, 10), (5, 10), (10, -20), (0, 0))
    min_xoff = min(off[0] for off in offsets)
    min_yoff = min(off[1] for off in offsets)
    max_xoff = max(off[0] for off in offsets)
    max_yoff = max(off[1] for off in offsets)

    orig_adinputs = []
    coords = []
    for i, (xoff, yoff) in enumerate(offsets, start=1):
        ad = astrofaker.create('NIRI', filename=f'test{i}.fits')
        ad.init_default_extensions()
        x, y = 512 - xoff, 512 - yoff
        ad[0].add_star(amplitude=1000, x=x, y=y)
        orig_adinputs.append(ad)
        coords.append(SkyCoord(*ad[0].wcs(x, y), unit=u.deg))

    if file_write:
        shifts_par = os.path.join(path_to_outputs, 'shifts.lis')
        f = open(shifts_par, 'w')
        for xoff, yoff in offsets:
            f.write(f'{xoff} {yoff}\n')
        f.close()
    else:
        shifts_par = ':'.join([f'{xoff},{yoff}' for xoff, yoff in offsets])

    adinputs = [deepcopy(ad) for ad in orig_adinputs]
    p = NIRIImage(adinputs)
    p.shiftImages(shifts=shifts_par, trim_data=trim_data)

    for ad, coord in zip(p.streams['main'], coords):
        shape = (1024, 1024) if trim_data else (1024 + max_yoff - min_yoff,
                                                1024 + max_xoff - min_xoff)
        assert ad[0].shape == shape
        peak_loc = (512, 512) if trim_data else (512 - min_yoff,
                                                 512 - min_xoff)
        y, x = np.unravel_index(ad[0].data.argmax(), ad[0].shape)
        assert (y, x) == peak_loc
        new_coord = SkyCoord(*ad[0].wcs(x, y), unit=u.deg)
        assert coord.separation(
            new_coord) < 1e-6 * u.arcsec  # should be identical

    if file_write:
        os.remove(shifts_par)
Ejemplo n.º 15
0
def test_stackframes_refcat_propagation(niri_adinputs):
    refcat = Table([[1, 2], ['a', 'b']], names=('Id', 'Cat_Id'))
    for i, ad in enumerate(niri_adinputs):
        if i > 0:
            refcat['Cat_Id'] = ['b', 'c']
        ad.REFCAT = refcat

    p = NIRIImage(niri_adinputs)
    p.prepare()
    adout = p.stackFrames()[0]

    # The merged REFCAT should contain 'a', 'b', 'c'
    assert len(adout.REFCAT) == 3
    np.testing.assert_equal(adout.REFCAT['Id'], np.arange(1, 4))
    assert all(adout.REFCAT['Cat_Id'] == ['a', 'b', 'c'])
Ejemplo n.º 16
0
def niri_images():
    """Create two NIRI images, one all 1s, the other all 2s"""
    try:
        import astrofaker
    except ImportError:
        pytest.skip("astrofaker not installed")

    adinputs = []
    for i in (1, 2):
        ad = astrofaker.create('NIRI', 'IMAGE')
        ad.init_default_extensions()
        ad[0].data += i

    adinputs.append(ad)

    return NIRIImage(adinputs)
Ejemplo n.º 17
0
def niri_image():
    try:
        import astrofaker
    except ImportError:
        pytest.skip("astrofaker is not installed")

    ad = astrofaker.create('NIRI', 'IMAGE')
    ad.init_default_extensions()

    # SExtractor struggles if the background is noiseless
    ad.add_read_noise()

    for x, y in STAR_POSITIONS:
        ad[0].add_star(amplitude=500, x=x, y=y)

    return NIRIImage([ad])
Ejemplo n.º 18
0
def test_stackframes_refcat_propagation(niri_adinputs):
    refcat = Table([[1, 2], [0.0, 1.0], [40.0, 40.0]],
                   names=('Id', 'RAJ2000', 'DEJ2000'))
    for i, ad in enumerate(niri_adinputs):
        if i > 0:
            refcat['RAJ2000'] = [1.0, 2.0]
        ad.REFCAT = refcat

    p = NIRIImage(niri_adinputs)
    p.prepare()
    adout = p.stackFrames()[0]

    # The merged REFCAT should contain 3 sources as follows
    assert len(adout.REFCAT) == 3
    np.testing.assert_equal(adout.REFCAT['Id'], np.arange(1, 4))
    assert all(adout.REFCAT['RAJ2000'] == [0.0, 1.0, 2.0])
Ejemplo n.º 19
0
 def test_writeOutputs(self):
     filenames = [
         'N20070819S{:04d}_flatCorrected.fits'.format(i)
         for i in range(104, 106)
     ]
     adinputs = [
         astrodata.open(os.path.join(TESTDATAPATH, 'NIRI', f))
         for f in filenames
     ]
     p = NIRIImage(adinputs)
     p.writeOutputs(prefix='test', suffix='_blah', strip=True)
     # Check renamed files are on disk and the filenames have been
     # changed for the adinputs
     for f, ad in zip(filenames, p.streams['main']):
         newfilename = 'test' + f.replace('flatCorrected', 'blah')
         assert os.path.exists(newfilename)
         os.remove(newfilename)
         assert newfilename == ad.filename
Ejemplo n.º 20
0
def test_associateSky():
    filenames = [
        'N20070819S{:04d}_flatCorrected.fits'.format(i)
        for i in range(104, 109)
    ]

    adinputs = [
        astrodata.open(os.path.join(TESTDATAPATH, 'NIRI', f))
        for f in filenames
    ]

    p = NIRIImage(adinputs)
    p.separateSky()  # Difficult to construct this by hand
    p.associateSky()
    filename_set = {ad.phu['ORIGNAME'] for ad in adinputs}

    # Test here is that each science frame has all other frames as skies
    for k, v in p.sky_dict.items():
        v = [ad.phu['ORIGNAME'] for ad in v]
        assert len(v) == len(filenames) - 1
        assert set([k] + v) == filename_set
Ejemplo n.º 21
0
def test_adjust_wcs_to_reference(astrofaker, no_wcs, rotate, scale):
    scale = False
    mods = [models.Identity(2)]
    for params in MODEL_PARMS:
        m = models.Shift(params[0]) & models.Shift(params[1])
        if rotate:
            m |= models.Rotation2D(params[2])
        if scale:
            m |= models.Scale(params[3]) & models.Scale(params[3])
        mods.append(m)
    adinputs = make_images(astrofaker, mods=mods)
    if no_wcs:
        for ad in adinputs:
            del ad[0].hdr['CD*']
            ad[0].wcs = None
    p = NIRIImage(adinputs)
    p.detectSources()
    p.adjustWCSToReference(first_pass=45 if no_wcs else 5,
                           rotate=rotate,
                           scale=scale)

    # We're going to confirm that a grid of input points are transformed
    # correctly, rather than comparing the components of the registration
    # model with the original model
    yin, xin = np.mgrid[:1000:100, :1000:100]
    for i, (ad, m) in enumerate(zip(p.streams['main'], mods)):
        if i == 0:  # the Identity model: nothing to check
            continue
        m_regist = ad[0].wcs.forward_transform[:m.n_submodels]
        print(m_regist)

        xref, yref = m(xin, yin)
        xout, yout = m_regist(xin, yin)
        np.testing.assert_allclose(xref, xout, atol=0.1)
        np.testing.assert_allclose(yref, yout, atol=0.1)
        rms = np.sqrt(((xref - xout)**2 + (yref - yout)**2).mean())
        assert rms < 0.05
Ejemplo n.º 22
0
def test_not_prepared(adinputs):
    p = NIRIImage(adinputs)
    with pytest.raises(IOError):
        p.stackFrames()
Ejemplo n.º 23
0
def test_associateSky():

    filenames = [
        'N20070819S{:04d}_flatCorrected.fits'.format(i)
        for i in range(104, 109)
    ]

    adinputs = [
        astrodata.open(os.path.join(TESTDATAPATH, 'NIRI', f))
        for f in filenames
    ]

    p = NIRIImage(adinputs)
    p.separateSky()  # Difficult to construct this by hand
    p.associateSky()
    filename_set = set([ad.phu['ORIGNAME'] for ad in adinputs])

    # Test here is that each science frame has all other frames as skies
    for k, v in p.sky_dict.items():
        v = [ad.phu['ORIGNAME'] for ad in v]
        assert len(v) == len(filenames) - 1
        assert set([k] + v) == filename_set


# def test_correctBackgroundToReference(self):
#     pass

# def test_darkCorrect(self):
#     ad = astrodata.open(os.path.join(TESTDATAPATH, 'NIRI',
#                             'N20070819S0104_nonlinearityCorrected.fits'))
#     p = NIRIImage([ad])
#     ad = p.darkCorrect()[0]
#     assert ad_compare(ad, os.path.join(TESTDATAPATH, 'NIRI',
#                             'N20070819S0104_darkCorrected.fits'))

# def test_darkCorrect_with_af(self):
#     science = AstroFaker.create('NIRI', 'IMAGE')
#     dark = AstroFaker.create('NIRI', 'IMAGE')
#     p = NIRIImage([science])
#     p.darkCorrect([science], dark=dark)
#     science.subtract(dark)
#     assert ad_compare(science, dark)

# af.init_default_extensions()
# af[0].mask = np.zeros_like(af[0].data, dtype=np.uint16)
# def test_flatCorrect(self):
#     ad = astrodata.open(os.path.join(TESTDATAPATH, 'NIRI',
#                             'N20070819S0104_darkCorrected.fits'))
#     p = NIRIImage([ad])
#     ad = p.flatCorrect()[0]
#     assert ad_compare(ad, os.path.join(TESTDATAPATH, 'NIRI',
#                             'N20070819S0104_flatCorrected.fits'))
#
# def test_makeSky(self):
#     pass
#
# def test_nonlinearityCorrect(self):
#     # Don't use NIRI data; NIRI has its own primitive
#     pass
#
# def test_normalizeFlat(self):
#     flat_file = os.path.join(TESTDATAPATH, 'NIRI',
#                             'N20070913S0220_flat.fits')
#     ad = astrodata.open(flat_file)
#     ad.multiply(10.0)
#     del ad.phu['NORMLIZE']  # Delete timestamp of previous processing
#     p = NIRIImage([ad])
#     ad = p.normalizeFlat(suffix='_flat', strip=True)[0]
#     assert ad_compare(ad, flat_file)
#
# def test_separateSky(self):
#     pass
#
# def test_skyCorrect(self):
#     pass
#
# def test_subtractSky(self):
#     pass
#
# def test_subtractSkyBackground(self):
#     ad = astrodata.open(os.path.join(TESTDATAPATH, 'NIRI',
#                             'N20070819S0104_flatCorrected.fits'))
#     ad.hdr['SKYLEVEL'] = 1000.0
#     orig_data = ad[0].data.copy()
#     p = NIRIImage([ad])
#     ad = p.subtractSkyBackground()[0]
#     assert (orig_data - ad[0].data).min() > 999.99
#     assert (orig_data - ad[0].data).max() < 1000.01
#
# def test_thresholdFlatfield(self):
#     ad = astrodata.open(os.path.join(TESTDATAPATH, 'NIRI',
#                                      'N20070913S0220_flat.fits'))
#     del ad.phu['TRHFLAT']  # Delete timestamp of previous processing
#     ad[0].data[100, 100] = 20.0
#     p = NIRIImage([ad])
#     ad = p.thresholdFlatfield()[0]
#     assert ad[0].mask[100, 100] == 64