Example #1
0
def test_setting_nones_to_known_pairs_expect_fail():

    lhs6328_spectrum1D = Spectrum1D(log_file_name=None)
    wavecal = WavelengthCalibration(log_file_name=None)
    lhs6328_spectrum1D.add_arc_spec(arc_spec)
    wavecal.from_spectrum1D(lhs6328_spectrum1D)
    # Find the peaks of the arc
    wavecal.find_arc_lines(
        save_fig=True,
        fig_type="iframe+png",
        filename=os.path.join(
            HERE, "test_output", "test_wavecal_find_arc_lines"
        ),
        display=False,
        return_jsonstring=True,
    )
    wavecal.initialise_calibrator()
    wavecal.set_known_pairs([None], [None])
Example #2
0
def test_setting_known_pairs(mock_show):

    lhs6328_spectrum1D = Spectrum1D(log_file_name=None)
    wavecal = WavelengthCalibration(log_file_name=None)
    lhs6328_spectrum1D.add_arc_spec(arc_spec)
    wavecal.from_spectrum1D(lhs6328_spectrum1D)
    # Find the peaks of the arc
    wavecal.find_arc_lines(
        save_fig=True,
        fig_type="iframe+png",
        filename=os.path.join(
            HERE, "test_output", "test_wavecal_find_arc_lines"
        ),
        display=True,
        return_jsonstring=True,
    )
    wavecal.initialise_calibrator()
    wavecal.set_known_pairs([123, 234], [456, 567])
    assert len(wavecal.spectrum1D.calibrator.pix_known) == 2
    assert len(wavecal.spectrum1D.calibrator.wave_known) == 2
Example #3
0
def test_setting_a_known_pair():

    lhs6328_spectrum1D = SpectrumOneD(log_file_name=None)
    wavecal = WavelengthCalibration(log_file_name=None)
    lhs6328_spectrum1D.add_arc_spec(arc_spec)
    wavecal.from_spectrum1D(lhs6328_spectrum1D)
    # Find the peaks of the arc
    wavecal.find_arc_lines(
        save_fig=True,
        fig_type="iframe+png",
        filename=os.path.join(
            HERE, "test_output", "test_wavecal_find_arc_lines"
        ),
        display=False,
        return_jsonstring=True,
    )
    wavecal.initialise_calibrator()
    wavecal.set_known_pairs(123, 456)
    assert wavecal.spectrum1D.calibrator.pix_known == 123
    assert wavecal.spectrum1D.calibrator.wave_known == 456
Example #4
0
def test_overwritten_copy_of_spectrum1Ds_are_different():

    lhs6328_spectrum1D = Spectrum1D(log_file_name=None)
    wavecal_1 = WavelengthCalibration(log_file_name=None)
    wavecal_1.from_spectrum1D(lhs6328_spectrum1D)
    memory_1 = id(wavecal_1.spectrum1D)
    wavecal_1.from_spectrum1D(copy.copy(lhs6328_spectrum1D), overwrite=True)
    memory_2 = id(wavecal_1.spectrum1D)

    assert memory_1 != memory_2
Example #5
0
def test_quadratic_fit_chebyshev():

    # Initialise the calibrator
    wavecal = WavelengthCalibration(log_file_name=None)
    wavecal.initialise_calibrator(peaks)

    wavecal.set_calibrator_properties(num_pix=1000)
    wavecal.set_hough_properties(
        num_slopes=500,
        range_tolerance=200.0,
        xbins=100,
        ybins=100,
        min_wavelength=3000.0,
        max_wavelength=8000.0,
    )
    wavecal.add_user_atlas(
        elements=elements_quadratic, wavelengths=wavelengths_quadratic
    )
    wavecal.set_ransac_properties(sample_size=10, minimum_matches=20)
    wavecal.do_hough_transform(brute_force=False)

    # Run the wavelength calibration
    (
        best_p,
        matched_peaks,
        matched_atlas,
        rms,
        residual,
        peak_utilisation,
        atlas_utilisation,
    ) = wavecal.fit(
        max_tries=2000,
        fit_tolerance=5.0,
        candidate_tolerance=2.0,
        fit_deg=2,
        fit_type="chebyshev",
    )
Example #6
0
def test_manual_refit_add_points():

    # Initialise the calibrator
    wavecal = WavelengthCalibration(log_file_name=None)
    wavecal.initialise_calibrator(peaks)

    wavecal.set_calibrator_properties(num_pix=1000)
    wavecal.set_hough_properties(
        num_slopes=1000,
        range_tolerance=500.0,
        xbins=200,
        ybins=200,
        min_wavelength=3000.0,
        max_wavelength=8000.0,
    )
    wavecal.add_user_atlas(
        elements=elements_linear, wavelengths=wavelengths_linear
    )
    wavecal.set_ransac_properties(minimum_matches=25)
    wavecal.do_hough_transform(brute_force=False)

    # Run the wavelength calibration
    (
        best_p,
        atched_peaks,
        matched_atlas,
        rms,
        residual,
        peak_utilisation,
        atlas_utilisation,
    ) = wavecal.fit(max_tries=500, fit_deg=1)

    # Refine solution
    (
        best_p,
        matched_peaks,
        matched_atlas,
        rms,
        residual,
        peak_utilisation,
        atlas_utilisation,
    ) = wavecal.robust_refit(best_p, refine=False, robust_refit=True)

    wavecal.add_pix_wave_pair(
        2000.0, 3000.0 + 4 * 2000.0 + 1.0e-3 * 2000.0**2.0
    )
    (
        best_p_manual,
        matched_peaks,
        matched_atlas,
        rms,
        residuals,
    ) = wavecal.manual_refit(matched_peaks, matched_atlas)

    assert np.allclose(best_p_manual, best_p)
Example #7
0
def test_wavecal():

    lhs6328_spectrum1D = Spectrum1D(log_file_name=None)
    wavecal = WavelengthCalibration(log_file_name=None)

    # Science arc_spec
    lhs6328_spectrum1D.add_arc_spec(arc_spec)
    wavecal.from_spectrum1D(lhs6328_spectrum1D)

    # Find the peaks of the arc
    wavecal.find_arc_lines(
        save_fig=True,
        fig_type="iframe+png",
        filename=os.path.join(
            HERE, "test_output", "test_wavecal_find_arc_lines"
        ),
        display=False,
        return_jsonstring=True,
    )

    # Configure the wavelength calibrator
    wavecal.initialise_calibrator()
    wavecal.set_hough_properties(
        num_slopes=1000,
        xbins=200,
        ybins=200,
        min_wavelength=3500,
        max_wavelength=8500,
    )
    wavecal.set_ransac_properties(filter_close=True)

    wavecal.add_user_atlas(elements=element, wavelengths=atlas)

    # Remove all lines between 3500 and 4000
    wavecal.remove_atlas_lines_range(wavelength=3750, tolerance=250)
    wavecal.do_hough_transform()

    # Solve for the pixel-to-wavelength solution
    wavecal.fit(max_tries=500, display=False)

    # Getting the calibrator
    wavecal.get_calibrator()

    # Save a FITS file
    wavecal.save_fits(
        output="wavecal",
        filename=os.path.join(HERE, "test_output", "test_wavecal"),
        overwrite=True,
    )

    # Save a CSV file
    wavecal.save_csv(
        output="wavecal",
        filename=os.path.join(HERE, "test_output", "test_wavecal"),
        overwrite=True,
    )

    # Getting the calibrator
    wavecal.get_spectrum1D()

    wavecal.list_atlas()
    wavecal.clear_atlas()
    wavecal.list_atlas()
Example #8
0
def test_linear_fit():

    wavecal = WavelengthCalibration(log_file_name=None)
    wavecal.initialise_calibrator(peaks)

    wavecal.set_calibrator_properties(num_pix=1000)
    wavecal.set_hough_properties(
        num_slopes=1000,
        range_tolerance=500.0,
        xbins=200,
        ybins=200,
        min_wavelength=3000.0,
        max_wavelength=8000.0,
    )
    wavecal.add_user_atlas(
        elements=elements_linear, wavelengths=wavelengths_linear
    )
    wavecal.set_ransac_properties(minimum_matches=20)
    wavecal.do_hough_transform(brute_force=False)

    # Run the wavelength calibration
    (
        best_p,
        matched_peaks,
        matched_atlas,
        rms,
        residual,
        peak_utilisation,
        atlas_utilisation,
    ) = wavecal.fit(max_tries=500, fit_deg=1)
    # Refine solution
    (
        best_p,
        matched_peaks,
        matched_atlas,
        rms,
        residual,
        peak_utilisation,
        atlas_utilisation,
    ) = wavecal.robust_refit(best_p, refine=False, robust_refit=True)

    assert np.abs(best_p[1] - 5.0) / 5.0 < 0.001
    assert np.abs(best_p[0] - 3000.0) / 3000.0 < 0.001
    assert peak_utilisation > 0.8
    assert atlas_utilisation > 0.0
Example #9
0
def test_user_supplied_arc_lines(mock_show):

    wavecal = WavelengthCalibration(log_file_name=None)

    # Find the peaks of the arc
    wavecal.add_arc_lines(arc_lines)

    # Configure the wavelength calibrator
    wavecal.initialise_calibrator()
    wavecal.set_hough_properties(
        num_slopes=200,
        xbins=40,
        ybins=40,
        min_wavelength=3500,
        max_wavelength=8500,
    )
    wavecal.set_ransac_properties(filter_close=True)

    wavecal.add_user_atlas(elements=element, wavelengths=atlas)
    wavecal.do_hough_transform()

    # Solve for the pixel-to-wavelength solution
    wavecal.fit(max_tries=500, display=True)

    # Save a FITS file
    wavecal.save_fits(
        output="wavecal",
        filename=os.path.join(
            HERE, "test_output", "test_wavecal_user_supplied_arc_spec"
        ),
        overwrite=True,
    )

    wavecal.remove_arc_lines()
Example #10
0
    7642.02,
    7740.31,
    7802.65,
    7887.40,
    7967.34,
    8057.258,
]
element = ["Xe"] * len(atlas)

arc_spec = np.loadtxt(
    os.path.join(HERE, "test_data", "test_full_run_science_0_arc_spec.csv"),
    delimiter=",",
    skiprows=1,
)

wavecal = WavelengthCalibration(log_file_name=None)
wavecal.add_arc_spec(arc_spec)

# Find the peaks of the arc
wavecal.find_arc_lines()

arc_lines = wavecal.spectrum1D.peaks

np.random.seed(0)


def test_wavecal():

    lhs6328_spectrum1D = Spectrum1D(log_file_name=None)
    wavecal = WavelengthCalibration(log_file_name=None)