def test_estimate_background(): """Verifies tpf.estimate_background().""" # Create a TPF with 100 electron/second in every pixel tpf = read(filename_tpf_all_zeros) + 100.0 # The resulting background should be 100 e/s/pixel bg = tpf.estimate_background(aperture_mask="all") assert_array_equal(bg.flux.value, 100) assert bg.flux.unit == tpf.flux.unit / u.pixel
def test_parse_numeric_aperture_masks(): """Regression test for #694: float or int aperture masks should be interpreted as boolean masks.""" tpf = read(filename_tpf_one_center) mask = tpf._parse_aperture_mask(np.zeros(tpf.shape[1:], dtype=float)) assert mask.dtype == bool mask = tpf._parse_aperture_mask(np.zeros(tpf.shape[1:], dtype=int)) assert mask.dtype == bool
def test_get_header(): """Test the basic functionality of ``tpf.get_header()``""" tpf = read(filename_tpf_one_center) assert tpf.get_header()["CHANNEL"] == tpf.get_keyword("CHANNEL") assert tpf.get_header(0)["MISSION"] == tpf.get_keyword("MISSION") assert tpf.get_header(ext=2)["EXTNAME"] == "APERTURE" # ``tpf.header`` is deprecated with pytest.warns(LightkurveDeprecationWarning, match="deprecated"): tpf.header
def test_read(): # define paths to k2 and tess data k2_path = os.path.join(TESTDATA, "test-tpf-star.fits") tess_path = os.path.join(TESTDATA, "tess25155310-s01-first-cadences.fits.gz") # Ensure files are read in as the correct object k2tpf = read(k2_path) assert isinstance(k2tpf, KeplerTargetPixelFile) tesstpf = read(tess_path) assert isinstance(tesstpf, TessTargetPixelFile) # Open should fail if the filetype is not recognized try: read(os.path.join(PACKAGEDIR, "data", "lightkurve.mplstyle")) except LightkurveError: pass # Can you instantiate with a path? assert isinstance(KeplerTargetPixelFile(k2_path), KeplerTargetPixelFile) assert isinstance(TessTargetPixelFile(tess_path), TessTargetPixelFile) # Can open take a quality_bitmask argument? assert read(k2_path, quality_bitmask="hard").quality_bitmask == "hard"
def test_tpf_meta(): """Can we access meta data using tpf.meta?""" tpf = read(filename_tpf_one_center) assert tpf.meta.get("MISSION") == "K2" assert tpf.meta["MISSION"] == "K2" assert tpf.meta.get("mission", None) is None # key is case in-sensitive assert tpf.meta.get("CHANNEL") == 45 # ensure meta is read-only view of the underlying self.hdu[0].header with pytest.raises(TypeError): tpf.meta["CHANNEL"] = 44 with pytest.raises(TypeError): tpf.meta["KEY-NEW"] = 44
def test_centroid_methods_consistency(): """Are the centroid methods consistent for a well behaved target?""" pixels = read(filename_synthetic_flat) centr_moments = pixels.estimate_centroids(method="moments") centr_quadratic = pixels.estimate_centroids(method="quadratic") # check that the maximum relative difference doesnt exceed 1% assert (np.max( np.abs(centr_moments[0] - centr_quadratic[0]) / centr_moments[0]) < 1e-2) assert (np.max( np.abs(centr_moments[1] - centr_quadratic[1]) / centr_moments[1]) < 1e-2)
def test_aperture_photometry_nan(): """Regression test for #648. When FLUX or FLUX_ERR is entirely NaN in a TPF, the resulting light curve should report NaNs in that cadence rather than zero.""" tpf = read(filename_tpf_one_center) tpf.hdu[1].data["FLUX"][2] = np.nan tpf.hdu[1].data["FLUX_ERR"][2] = np.nan lc = tpf.to_lightcurve(aperture_mask="all") assert ~np.isnan(lc.flux[1]) assert ~np.isnan(lc.flux_err[1]) assert np.isnan(lc.flux[2]) assert np.isnan(lc.flux_err[2])
def test_fluxmode(): """This should verify the median flux use in an aperture""" tpf = read(filename_tpf_one_center) lc_n = tpf.extract_aperture_photometry(aperture_mask="all") lc_sum = tpf.extract_aperture_photometry(aperture_mask="all", flux_method="sum") lc_med = tpf.extract_aperture_photometry(aperture_mask="all", flux_method="median") lc_mean = tpf.extract_aperture_photometry(aperture_mask="all", flux_method="mean") assert lc_n.flux.value[0] == np.nansum(tpf.flux.value[0]) assert lc_sum.flux.value[0] == np.nansum(tpf.flux.value[0]) assert lc_med.flux.value[0] == np.nanmedian(tpf.flux.value[0]) assert lc_mean.flux.value[0] == np.nanmean(tpf.flux.value[0])
def test_tpf_meta(): """Can we access meta data using tpf.meta?""" tpf = read(filename_tpf_one_center) assert tpf.meta.get("MISSION") == "K2" assert tpf.meta["MISSION"] == "K2" assert tpf.meta.get("mission", None) is None # key is case in-sensitive assert tpf.meta.get("CHANNEL") == 45 # ensure meta is read-only view of the underlying self.hdu[0].header with pytest.raises(TypeError): tpf.meta["CHANNEL"] = 44 with pytest.raises(TypeError): tpf.meta["KEY-NEW"] = 44 # needed for current internal HduToMetaMapping-based meta, ensuring it has # a friendly __repr__() and __str__() expected = collections.OrderedDict(tpf.meta).__repr__() assert tpf.meta.__repr__() == expected assert tpf.meta.__str__() == expected
def test_animate(): tpf = read(filename_tpf_one_center) tpf.animate()
def test_cutout_quality_masking(): """Regression test for #813: Does tpf.cutout() maintain the quality mask?""" tpf = read(filename_tpf_one_center, quality_bitmask=8192) tpfcut = tpf.cutout() assert len(tpf) == len(tpfcut)
def test_filenotfound(): """Regression test for #540; ensure lk.read() yields `FileNotFoundError`.""" with pytest.raises(FileNotFoundError): read("DOESNOTEXIST")