Ejemplo n.º 1
0
def test_regression_on_extract_1d_spectra(ad, ref_ad_factory,
                                          change_working_dir):
    """
    Regression test for the :func:`~geminidr.gmos.GMOSSpect.extract1DSpectra`
    primitive.

    Parameters
    ----------
    ad : pytest.fixture (AstroData)
        Fixture that reads the filename and loads as an AstroData object.
    change_working_dir : pytest.fixture
        Fixture that changes the working directory
        (see :mod:`astrodata.testing`).
    reference_ad : pytest.fixture
        Fixture that contains a function used to load the reference AstroData
        object (see :mod:`recipe_system.testing`).
    """

    with change_working_dir():

        logutils.config(
            file_name='log_regression_{:s}.txt'.format(ad.data_label()))

        p = primitives_gmos_spect.GMOSSpect([ad])
        p.viewer = geminidr.dormantViewer(p, None)
        p.extract1DSpectra(method="standard", width=None, grow=10)
        extracted_ad = p.writeOutputs().pop()

    ref_ad = ref_ad_factory(extracted_ad.filename)

    for ext, ref_ext in zip(extracted_ad, ref_ad):
        assert ext.data.ndim == 1
        np.testing.assert_allclose(ext.data, ref_ext.data, atol=1e-3)
Ejemplo n.º 2
0
def test_regression_on_flux_calibration(ad, ref_ad_factory,
                                        change_working_dir):
    """
    Regression test for the :func:`~geminidr.gmos.GMOSSpect.fluxCalibrate`
    primitive.

    Parameters
    ----------
    ad : pytest.fixture (AstroData)
        Fixture that reads the filename and loads as an AstroData object.
    change_working_dir : pytest.fixture
        Fixture that changes the working directory
        (see :mod:`astrodata.testing`).
    reference_ad : pytest.fixture
        Fixture that contains a function used to load the reference AstroData
        object (see :mod:`recipe_system.testing`).
    """

    with change_working_dir():
        logutils.config(
            file_name='log_regression_{:s}.txt'.format(ad.data_label()))
        p = primitives_gmos_spect.GMOSSpect([ad])
        p.fluxCalibrate(standard=ad)
        flux_calibrated_ad = p.writeOutputs().pop()

    ref_ad = ref_ad_factory(flux_calibrated_ad.filename)

    for flux_cal_ext, ref_ext in zip(flux_calibrated_ad, ref_ad):
        np.testing.assert_allclose(flux_cal_ext.data, ref_ext.data, atol=1e-4)
Ejemplo n.º 3
0
def test_regression_for_determine_distortion_using_models_coefficients(
        ad, change_working_dir, ref_ad_factory, request):
    """
    Runs the `determineDistortion` primitive on a preprocessed data and compare
    its model with the one in the reference file.

    Parameters
    ----------
    ad : pytest.fixture (AstroData)
        Fixture that reads the filename and loads as an AstroData object.
    change_working_dir : pytest.fixture
        Fixture that changes the working directory
        (see :mod:`astrodata.testing`).
    reference_ad : pytest.fixture
        Fixture that contains a function used to load the reference AstroData
        object (see :mod:`recipe_system.testing`).
    request : pytest.fixture
        PyTest built-in containing command line options.
    """
    with change_working_dir():
        logutils.config(file_name='log_model_{:s}.txt'.format(ad.data_label()))
        p = primitives_gmos_spect.GMOSSpect([ad])
        p.viewer = geminidr.dormantViewer(p, None)
        p.determineDistortion(**fixed_parameters_for_determine_distortion)
        distortion_determined_ad = p.writeOutputs().pop()

    ref_ad = ref_ad_factory(distortion_determined_ad.filename)
    for ext, ext_ref in zip(distortion_determined_ad, ref_ad):
        c = np.ma.masked_invalid(ext.FITCOORD["coefficients"])
        c_ref = np.ma.masked_invalid(ext_ref.FITCOORD["coefficients"])
        np.testing.assert_allclose(c, c_ref, atol=2)
        
    if request.config.getoption("--do-plots"):
        do_plots(distortion_determined_ad, ref_ad)
Ejemplo n.º 4
0
def test_regression_for_determine_distortion_using_wcs(ad, change_working_dir,
                                                       ref_ad_factory):
    """
    Runs the `determineDistortion` primitive on a preprocessed data and compare
    its model with the one in the reference file. The distortion model needs to
    be reconstructed because different coefficients might return same results.

    Parameters
    ----------
    ad : pytest.fixture (AstroData)
        Fixture that reads the filename and loads as an AstroData object.
    change_working_dir : pytest.fixture
        Fixture that changes the working directory
        (see :mod:`astrodata.testing`).
    reference_ad : pytest.fixture
        Fixture that contains a function used to load the reference AstroData
        object (see :mod:`recipe_system.testing`).
    """
    with change_working_dir():
        logutils.config(
            file_name='log_fitcoord_{:s}.txt'.format(ad.data_label()))
        p = GMOSLongslit([ad])
        p.viewer = geminidr.dormantViewer(p, None)
        p.determineDistortion(**fixed_parameters_for_determine_distortion)
        distortion_determined_ad = p.writeOutputs().pop()

    ref_ad = ref_ad_factory(distortion_determined_ad.filename)
    model = distortion_determined_ad[0].wcs.get_transform(
        "pixels", "distortion_corrected")[1]
    ref_model = ref_ad[0].wcs.get_transform("pixels",
                                            "distortion_corrected")[1]

    X, Y = np.mgrid[:ad[0].shape[0], :ad[0].shape[1]]

    np.testing.assert_allclose(model(X, Y), ref_model(X, Y), atol=1)
Ejemplo n.º 5
0
def test_make_processed_slit_illum(processed_slit_illum, ref_ad_factory):

    assert "_slitIllum" in processed_slit_illum.filename

    ref_ad = ref_ad_factory(processed_slit_illum.filename)

    for ext, ext_ref in zip(processed_slit_illum, ref_ad):
        np.testing.assert_allclose(ext.data, ext_ref.data, atol=1)
def test_regression_determine_wavelength_solution(ad, fwidth, order, min_snr,
                                                  caplog, change_working_dir,
                                                  ref_ad_factory):
    """
    Make sure that the wavelength solution gives same results on different
    runs.
    """
    caplog.set_level(logging.INFO, logger="geminidr")

    with change_working_dir():
        logutils.config(
            file_name='log_regress_{:s}.txt'.format(ad.data_label()))
        p = primitives_gmos_spect.GMOSSpect([ad])
        p.viewer = geminidr.dormantViewer(p, None)

        p.determineWavelengthSolution(
            order=order,
            min_snr=min_snr,
            fwidth=fwidth,
            **determine_wavelength_solution_parameters)

        wcalibrated_ad = p.writeOutputs().pop()

        for record in caplog.records:
            if record.levelname == "WARNING":
                assert "No acceptable wavelength solution found" not in record.message

    ref_ad = ref_ad_factory(wcalibrated_ad.filename)
    table = wcalibrated_ad[0].WAVECAL
    table_ref = ref_ad[0].WAVECAL

    model = astromodels.dict_to_chebyshev(
        dict(zip(table["name"], table["coefficients"])))

    ref_model = astromodels.dict_to_chebyshev(
        dict(zip(table_ref["name"], table_ref["coefficients"])))

    x = np.arange(wcalibrated_ad[0].shape[1])
    wavelength = model(x)
    ref_wavelength = ref_model(x)

    pixel_scale = wcalibrated_ad[0].pixel_scale()  # arcsec / px
    slit_size_in_arcsec = float(wcalibrated_ad[0].focal_plane_mask().replace(
        'arcsec', ''))
    slit_size_in_px = slit_size_in_arcsec / pixel_scale
    dispersion = abs(
        wcalibrated_ad[0].dispersion(asNanometers=True))  # nm / px

    tolerance = 0.5 * (slit_size_in_px * dispersion)
    np.testing.assert_allclose(wavelength, ref_wavelength, rtol=tolerance)
Ejemplo n.º 7
0
def test_regression_on_calculate_sensitivity(ad, change_working_dir, ref_ad_factory):

    with change_working_dir():
        logutils.config(file_name='log_regression_{:s}.txt'.format(ad.data_label()))
        p = primitives_gmos_spect.GMOSSpect([ad])
        p.calculateSensitivity(bandpass=5, order=6)
        calc_sens_ad = p.writeOutputs().pop()

    assert hasattr(calc_sens_ad[0], 'SENSFUNC')

    ref_ad = ref_ad_factory(ad.filename)

    for calc_sens_ext, ref_ext in zip(ad, ref_ad):
        np.testing.assert_allclose(
            calc_sens_ext.data, ref_ext.data, atol=1e-4)
Ejemplo n.º 8
0
def test_regression_determine_wavelength_solution(ad, fwidth, order, min_snr,
                                                  caplog, change_working_dir,
                                                  ref_ad_factory, request):
    """
    Make sure that the wavelength solution gives same results on different
    runs.
    """
    caplog.set_level(logging.INFO, logger="geminidr")

    with change_working_dir():
        logutils.config(
            file_name='log_regress_{:s}.txt'.format(ad.data_label()))
        p = GMOSLongslit([ad])
        p.viewer = geminidr.dormantViewer(p, None)

        p.determineWavelengthSolution(
            order=order,
            min_snr=min_snr,
            fwidth=fwidth,
            **determine_wavelength_solution_parameters)

        wcalibrated_ad = p.writeOutputs().pop()

        for record in caplog.records:
            if record.levelname == "WARNING":
                assert "No acceptable wavelength solution found" not in record.message

    ref_ad = ref_ad_factory(wcalibrated_ad.filename)
    model = am.get_named_submodel(wcalibrated_ad[0].wcs.forward_transform,
                                  "WAVE")
    ref_model = am.get_named_submodel(ref_ad[0].wcs.forward_transform, "WAVE")

    x = np.arange(wcalibrated_ad[0].shape[1])
    wavelength = model(x)
    ref_wavelength = ref_model(x)

    pixel_scale = wcalibrated_ad[0].pixel_scale()  # arcsec / px
    slit_size_in_arcsec = float(wcalibrated_ad[0].focal_plane_mask().replace(
        'arcsec', ''))
    slit_size_in_px = slit_size_in_arcsec / pixel_scale
    dispersion = abs(
        wcalibrated_ad[0].dispersion(asNanometers=True))  # nm / px

    tolerance = 0.5 * (slit_size_in_px * dispersion)
    np.testing.assert_allclose(wavelength, ref_wavelength, rtol=tolerance)

    if request.config.getoption("--do-plots"):
        do_plots(wcalibrated_ad)
Ejemplo n.º 9
0
def test_regression_processed_flat(processed_flat, ref_ad_factory):
    """
    Tests if the processed flat contains values around one.

    Parameters
    ----------
    processed_flat : pytest.fixture
        Fixture containing an instance of the processed flat.
    ref_ad_factory : pytest.fixture
        Fixture containing a function that will receive the input file an return
        the path to the reference data.
    """
    ref_flat = ref_ad_factory(processed_flat.filename)
    for ext, ext_ref in zip(processed_flat, ref_flat):
        astrodata.testing.assert_most_equal(ext.mask, ext_ref.mask, 10)
        astrodata.testing.assert_most_close(ext.data, ext_ref.data, 10, atol=0.05)
def test_regression_extract_1d_spectra(ad, change_working_dir,
                                       ref_ad_factory):

    with change_working_dir():

        logutils.config(
            file_name='log_regression_{}.txt'.format(ad.data_label()))

        p = primitives_gmos_spect.GMOSSpect([ad])
        p.viewer = geminidr.dormantViewer(p, None)
        p.skyCorrectFromSlit(order=5, grow=0)
        sky_subtracted_ad = p.writeOutputs().pop()

    ref_ad = ref_ad_factory(sky_subtracted_ad.filename)

    for ext, ref_ext in zip(sky_subtracted_ad, ref_ad):
        np.testing.assert_allclose(ext.data, ref_ext.data, atol=0.01)
Ejemplo n.º 11
0
def test_regression_on_qe_correct(ad, arc_ad, change_working_dir,
                                  ref_ad_factory):

    with change_working_dir():
        logutils.config(
            file_name='log_test_regression{}.txt'.format(ad.data_label()))
        p = primitives_gmos_longslit.GMOSLongslit([ad])
        p.QECorrect(arc=arc_ad)
        qe_corrected_ad = p.writeOutputs().pop()

    assert 'QECORR' in qe_corrected_ad.phu.keys()

    ref_ad = ref_ad_factory(qe_corrected_ad.filename)

    for qe_corrected_ext, ref_ext in zip(qe_corrected_ad, ref_ad):
        np.testing.assert_allclose(np.ma.masked_array(
            qe_corrected_ext.data, mask=qe_corrected_ext.mask),
                                   np.ma.masked_array(ref_ext.data,
                                                      mask=ref_ext.mask),
                                   atol=0.05)
Ejemplo n.º 12
0
def test_regression_in_distortion_correct(ad, change_working_dir, ref_ad_factory):
    """
    Runs the `distortionCorrect` primitive on a preprocessed data and compare
    its model with the one in the reference file.
    """
    with change_working_dir():

        logutils.config(
            file_name='log_regression_{:s}.txt'.format(ad.data_label()))

        p = primitives_gmos_longslit.GMOSLongslit([deepcopy(ad)])
        p.viewer = geminidr.dormantViewer(p, None)
        p.distortionCorrect(arc=deepcopy(ad), order=3, subsample=1)
        dist_corrected_ad = p.writeOutputs()[0]

    ref_ad = ref_ad_factory(dist_corrected_ad.filename)
    for ext, ext_ref in zip(dist_corrected_ad, ref_ad):
        data = np.ma.masked_invalid(ext.data)
        ref_data = np.ma.masked_invalid(ext_ref.data)
        np.testing.assert_allclose(data, ref_data, atol=1)
Ejemplo n.º 13
0
def test_regression_trace_apertures(ad, change_working_dir, ref_ad_factory):

    with change_working_dir():
        logutils.config(file_name="log_regression_{}.txt".format(ad.data_label()))
        p = primitives_gmos_spect.GMOSSpect([ad])
        p.viewer = geminidr.dormantViewer(p, None)
        p.traceApertures()
        aperture_traced_ad = p.writeOutputs().pop()

    ref_ad = ref_ad_factory(aperture_traced_ad.filename)

    for ext, ref_ext in zip(aperture_traced_ad, ref_ad):
        input_table = ext.APERTURE
        reference_table = ref_ext.APERTURE

        assert input_table['aper_lower'][0] <= 0
        assert input_table['aper_upper'][0] >= 0

        keys = ext.APERTURE.colnames
        actual = np.array([input_table[k] for k in keys])
        desired = np.array([reference_table[k] for k in keys])
        np.testing.assert_allclose(desired, actual, atol=0.05)
Ejemplo n.º 14
0
def test_regression_for_determine_distortion_using_fitcoord_table(
        ad, change_working_dir, ref_ad_factory):
    """
    Runs the `determineDistortion` primitive on a preprocessed data and compare
    its model with the one in the reference file. The distortion model needs to
    be reconstructed because different coefficients might return same results.

    Parameters
    ----------
    ad : pytest.fixture (AstroData)
        Fixture that reads the filename and loads as an AstroData object.
    change_working_dir : pytest.fixture
        Fixture that changes the working directory
        (see :mod:`astrodata.testing`).
    reference_ad : pytest.fixture
        Fixture that contains a function used to load the reference AstroData
        object (see :mod:`recipe_system.testing`).
    """
    with change_working_dir():
        logutils.config(file_name='log_fitcoord_{:s}.txt'.format(ad.data_label()))
        p = primitives_gmos_spect.GMOSSpect([ad])
        p.viewer = geminidr.dormantViewer(p, None)
        p.determineDistortion(**fixed_parameters_for_determine_distortion)
        distortion_determined_ad = p.writeOutputs().pop()

    ref_ad = ref_ad_factory(distortion_determined_ad.filename)

    table = ad[0].FITCOORD
    model_dict = dict(zip(table['name'], table['coefficients']))
    model = astromodels.dict_to_chebyshev(model_dict)

    ref_table = ref_ad[0].FITCOORD
    ref_model_dict = dict(zip(ref_table['name'], ref_table['coefficients']))
    ref_model = astromodels.dict_to_chebyshev(ref_model_dict)

    X, Y = np.mgrid[:ad[0].shape[0], :ad[0].shape[1]]

    np.testing.assert_allclose(model(X, Y), ref_model(X, Y), atol=1)
Ejemplo n.º 15
0
def test_regression_on_qe_correct(ad, arc_ad, change_working_dir,
                                  ref_ad_factory):

    # The GMOS-N tests need to be run with `use_iraf=False` because the
    # reference files for those use the DRAGONS spline models.
    is_gmos_s = ad.instrument() == 'GMOS-S'

    with change_working_dir():
        logutils.config(
            file_name='log_test_regression{}.txt'.format(ad.data_label()))
        p = primitives_gmos_longslit.GMOSLongslit([ad])
        p.QECorrect(arc=arc_ad, use_iraf=is_gmos_s)
        qe_corrected_ad = p.writeOutputs().pop()

    assert 'QECORR' in qe_corrected_ad.phu.keys()

    ref_ad = ref_ad_factory(qe_corrected_ad.filename)

    for qe_corrected_ext, ref_ext in zip(qe_corrected_ad, ref_ad):
        np.testing.assert_allclose(np.ma.masked_array(
            qe_corrected_ext.data, mask=qe_corrected_ext.mask),
                                   np.ma.masked_array(ref_ext.data,
                                                      mask=ref_ext.mask),
                                   atol=0.05)