/
test_targetpixelfile.py
871 lines (750 loc) · 31.5 KB
/
test_targetpixelfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
import os
import tempfile
import warnings
import matplotlib.pyplot as plt
import numpy as np
from numpy.testing import assert_array_equal
import pytest
from astropy.utils.data import get_pkg_data_filename
from astropy.io.fits.verify import VerifyWarning
from astropy.coordinates import SkyCoord
from astropy.io import fits
from astropy import wcs
from astropy.io.fits.card import UNDEFINED
import astropy.units as u
from astropy.utils.exceptions import AstropyWarning
from lightkurve.targetpixelfile import KeplerTargetPixelFile, TargetPixelFileFactory
from lightkurve.targetpixelfile import TessTargetPixelFile, TargetPixelFile
from lightkurve.lightcurve import TessLightCurve
from lightkurve.utils import LightkurveWarning, LightkurveDeprecationWarning
from lightkurve.io import read
from lightkurve.search import search_tesscut
from .test_synthetic_data import filename_synthetic_flat
filename_tpf_all_zeros = get_pkg_data_filename("data/test-tpf-all-zeros.fits")
filename_tpf_one_center = get_pkg_data_filename("data/test-tpf-non-zero-center.fits")
filename_tess = get_pkg_data_filename("data/tess25155310-s01-first-cadences.fits.gz")
TABBY_Q8 = (
"https://archive.stsci.edu/missions/kepler/lightcurves"
"/0084/008462852/kplr008462852-2011073133259_llc.fits"
)
TABBY_TPF = (
"https://archive.stsci.edu/missions/kepler/target_pixel_files"
"/0084/008462852/kplr008462852-2011073133259_lpd-targ.fits.gz"
)
TESS_SIM = (
"https://archive.stsci.edu/missions/tess/ete-6/tid/00/000"
"/004/176/tess2019128220341-0000000417699452-0016-s_tp.fits"
)
asteroid_TPF = get_pkg_data_filename("data/asteroid_test.fits")
@pytest.mark.remote_data
def test_load_bad_file():
"""Test if a light curve can be opened without exception."""
with pytest.raises(ValueError) as exc:
KeplerTargetPixelFile(TABBY_Q8)
assert "is this a target pixel file?" in exc.value.args[0]
with pytest.raises(ValueError) as exc:
TessTargetPixelFile(TABBY_Q8)
assert "is this a target pixel file?" in exc.value.args[0]
def test_tpf_shapes():
"""Are the data array shapes of the TargetPixelFile object consistent?"""
with warnings.catch_warnings():
# Ignore the "TELESCOP is not equal to TESS" warning
warnings.simplefilter("ignore", LightkurveWarning)
tpfs = [
KeplerTargetPixelFile(filename_tpf_all_zeros),
TessTargetPixelFile(filename_tpf_all_zeros),
]
for tpf in tpfs:
assert tpf.quality_mask.shape == tpf.hdu[1].data["TIME"].shape
assert tpf.flux.shape == tpf.flux_err.shape
def test_tpf_math():
"""Can you add, subtract, multiply and divide?"""
with warnings.catch_warnings():
# Ignore the "TELESCOP is not equal to TESS" warning
warnings.simplefilter("ignore", LightkurveWarning)
tpfs = [
KeplerTargetPixelFile(filename_tpf_all_zeros),
TessTargetPixelFile(filename_tpf_all_zeros),
]
# These should work
for tpf in tpfs:
for other in [1, np.ones(tpf.flux.shape[1:]), np.ones(tpf.shape)]:
tpf + other
tpf - other
tpf * other
tpf / other
tpf += other
tpf -= other
tpf *= other
tpf /= other
# These should fail with a value error because their shape is wrong.
for tpf in tpfs:
for other in [
np.asarray([1, 2]),
np.arange(len(tpf.time) - 1),
np.ones([100, 1]),
np.ones([1, 2, 3]),
]:
with pytest.raises(ValueError):
tpf + other
# Check the values are correct
assert np.all(
((tpf.flux.value + 2) == (tpf + 2).flux.value)[np.isfinite(tpf.flux)]
)
assert np.all(
((tpf.flux.value - 2) == (tpf - 2).flux.value)[np.isfinite(tpf.flux)]
)
assert np.all(
((tpf.flux.value * 2) == (tpf * 2).flux.value)[np.isfinite(tpf.flux)]
)
assert np.all(
((tpf.flux.value / 2) == (tpf / 2).flux.value)[np.isfinite(tpf.flux)]
)
assert np.all(
((tpf.flux_err.value * 2) == (tpf * 2).flux_err.value)[
np.isfinite(tpf.flux)
]
)
assert np.all(
((tpf.flux_err.value / 2) == (tpf / 2).flux_err.value)[
np.isfinite(tpf.flux)
]
)
def test_tpf_plot():
"""Sanity check to verify that tpf plotting works"""
with warnings.catch_warnings():
# Ignore the "TELESCOP is not equal to TESS" warning
warnings.simplefilter("ignore", LightkurveWarning)
tpfs = [
KeplerTargetPixelFile(filename_tpf_one_center),
TessTargetPixelFile(filename_tpf_one_center),
]
for tpf in tpfs:
tpf.plot()
tpf.plot(aperture_mask=tpf.pipeline_mask)
tpf.plot(aperture_mask="all")
tpf.plot(frame=3)
with pytest.raises(ValueError):
tpf.plot(frame=999999)
tpf.plot(cadenceno=125250)
with pytest.raises(ValueError):
tpf.plot(cadenceno=999)
tpf.plot(bkg=True)
tpf.plot(scale="sqrt")
tpf.plot(scale="log")
with pytest.raises(ValueError):
tpf.plot(scale="blabla")
tpf.plot(column="FLUX")
tpf.plot(column="FLUX_ERR")
tpf.plot(column="FLUX_BKG")
tpf.plot(column="FLUX_BKG_ERR")
tpf.plot(column="RAW_CNTS")
tpf.plot(column="COSMIC_RAYS")
with pytest.raises(ValueError):
tpf.plot(column="not a column")
plt.close("all")
def test_tpf_zeros():
"""Does the LightCurve of a zero-flux TPF make sense?"""
tpf = KeplerTargetPixelFile(filename_tpf_all_zeros, quality_bitmask=None)
with warnings.catch_warnings():
# Ignore "LightCurve contains NaN times" warnings triggered by the liberal mask
warnings.simplefilter("ignore", LightkurveWarning)
lc = tpf.to_lightcurve()
# If you don't mask out bad data, time contains NaNs
assert np.any(
lc.time.value != tpf.time
) # Using the property that NaN does not equal NaN
# When you do mask out bad data everything should work.
assert (tpf.time.value == 0).any()
tpf = KeplerTargetPixelFile(filename_tpf_all_zeros, quality_bitmask="hard")
lc = tpf.to_lightcurve(aperture_mask="all")
assert len(lc.time) == len(lc.flux)
assert np.all(lc.time == tpf.time)
assert np.all(np.isnan(lc.flux)) # we expect all NaNs because of #874
# The default QUALITY bitmask should have removed all NaNs in the TIME
assert ~np.any(np.isnan(tpf.time.value))
@pytest.mark.parametrize("centroid_method", [("moments"), ("quadratic")])
def test_tpf_ones(centroid_method):
"""Does the LightCurve of a one-flux TPF make sense?"""
with warnings.catch_warnings():
# Ignore the "TELESCOP is not equal to TESS" warning
warnings.simplefilter("ignore", LightkurveWarning)
tpfs = [
KeplerTargetPixelFile(filename_tpf_one_center),
TessTargetPixelFile(filename_tpf_one_center),
]
for tpf in tpfs:
lc = tpf.to_lightcurve(aperture_mask="all", centroid_method=centroid_method)
assert np.all(lc.flux.value == 1)
assert np.all(
(lc.centroid_col.value < tpf.column + tpf.shape[1]).all()
* (lc.centroid_col.value > tpf.column).all()
)
assert np.all(
(lc.centroid_row.value < tpf.row + tpf.shape[2]).all()
* (lc.centroid_row.value > tpf.row).all()
)
@pytest.mark.parametrize(
"quality_bitmask,answer",
[
(None, 1290),
("none", 1290),
("default", 1233),
("hard", 1101),
("hardest", 1101),
(1, 1290),
(100, 1278),
(2096639, 1101),
],
)
def test_bitmasking(quality_bitmask, answer):
"""Test whether the bitmasking behaves like it should"""
tpf = KeplerTargetPixelFile(
filename_tpf_one_center, quality_bitmask=quality_bitmask
)
with warnings.catch_warnings():
# Ignore "LightCurve contains NaN times" warnings triggered by liberal masks
warnings.simplefilter("ignore", LightkurveWarning)
lc = tpf.to_lightcurve()
assert len(lc.flux) == answer
def test_wcs():
"""Test the wcs property."""
for tpf in [
KeplerTargetPixelFile(filename_tpf_one_center),
TessTargetPixelFile(filename_tess),
]:
w = tpf.wcs
ra, dec = tpf.get_coordinates()
assert ra.shape == tpf.shape
assert dec.shape == tpf.shape
assert type(w).__name__ == "WCS"
@pytest.mark.remote_data
@pytest.mark.parametrize("method", [("moments"), ("quadratic")])
def test_wcs_tabby(method):
"""Test the centroids from Tabby's star against simbad values"""
tpf = KeplerTargetPixelFile(TABBY_TPF)
tpf.wcs
ra, dec = tpf.get_coordinates(0)
col, row = tpf.estimate_centroids(method=method)
col = col.value - tpf.column
row = row.value - tpf.row
y, x = int(np.round(col[0])), int(np.round(row[1]))
# Compare with RA and Dec from Simbad
assert np.isclose(ra[x, y], 301.5643971, 1e-4)
assert np.isclose(dec[x, y], 44.4568869, 1e-4)
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_properties():
"""Test the short-hand properties."""
tpf = KeplerTargetPixelFile(filename_tpf_all_zeros)
assert tpf.channel == tpf.hdu[0].header["CHANNEL"]
assert tpf.module == tpf.hdu[0].header["MODULE"]
assert tpf.output == tpf.hdu[0].header["OUTPUT"]
assert tpf.ra == tpf.hdu[0].header["RA_OBJ"]
assert tpf.dec == tpf.hdu[0].header["DEC_OBJ"]
assert_array_equal(tpf.flux.value, tpf.hdu[1].data["FLUX"][tpf.quality_mask])
assert_array_equal(
tpf.flux_err.value, tpf.hdu[1].data["FLUX_ERR"][tpf.quality_mask]
)
assert_array_equal(
tpf.flux_bkg.value, tpf.hdu[1].data["FLUX_BKG"][tpf.quality_mask]
)
assert_array_equal(
tpf.flux_bkg_err.value, tpf.hdu[1].data["FLUX_BKG_ERR"][tpf.quality_mask]
)
assert_array_equal(tpf.quality, tpf.hdu[1].data["QUALITY"][tpf.quality_mask])
assert tpf.campaign == tpf.hdu[0].header["CAMPAIGN"]
assert tpf.quarter is None
def test_repr():
"""Do __str__ and __repr__ work?"""
for tpf in [
KeplerTargetPixelFile(filename_tpf_all_zeros),
TessTargetPixelFile(filename_tess),
]:
str(tpf)
repr(tpf)
def test_to_lightcurve():
for tpf in [
KeplerTargetPixelFile(filename_tpf_all_zeros),
TessTargetPixelFile(filename_tess),
]:
tpf.to_lightcurve()
tpf.to_lightcurve(aperture_mask=None)
tpf.to_lightcurve(aperture_mask="all")
lc = tpf.to_lightcurve(aperture_mask="threshold")
assert lc.time.scale == "tdb"
assert lc.label == tpf.hdu[0].header["OBJECT"]
if np.any(tpf.pipeline_mask):
tpf.to_lightcurve(aperture_mask="pipeline")
else:
with pytest.raises(ValueError):
tpf.to_lightcurve(aperture_mask="pipeline")
def test_bkg_lightcurve():
for tpf in [
KeplerTargetPixelFile(filename_tpf_all_zeros),
TessTargetPixelFile(filename_tess),
]:
lc = tpf.get_bkg_lightcurve()
lc = tpf.get_bkg_lightcurve(aperture_mask=None)
lc = tpf.get_bkg_lightcurve(aperture_mask="all")
assert lc.time.scale == "tdb"
assert lc.flux.shape == lc.flux_err.shape
assert len(lc.time) == len(lc.flux)
def test_aperture_photometry():
for tpf in [
KeplerTargetPixelFile(filename_tpf_all_zeros),
TessTargetPixelFile(filename_tess),
]:
tpf.extract_aperture_photometry()
for mask in [None, "all", "default", "threshold", "background"]:
tpf.extract_aperture_photometry(aperture_mask=mask)
if np.any(tpf.pipeline_mask):
tpf.extract_aperture_photometry(aperture_mask="pipeline")
else:
with pytest.raises(ValueError):
tpf.extract_aperture_photometry(aperture_mask="pipeline")
def test_tpf_to_fits():
"""Can we write a TPF back to a fits file?"""
for tpf in [
KeplerTargetPixelFile(filename_tpf_all_zeros),
TessTargetPixelFile(filename_tess),
]:
# `delete=False` is necessary to enable writing to the file on Windows
# but it means we have to clean up the tmp file ourselves
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
tpf.to_fits(tmp.name)
finally:
tmp.close()
os.remove(tmp.name)
def test_tpf_factory():
"""Can we create TPFs using TargetPixelFileFactory?"""
from lightkurve.targetpixelfile import FactoryError
factory = TargetPixelFileFactory(n_cadences=10, n_rows=6, n_cols=8)
flux_0 = np.ones((6, 8))
factory.add_cadence(frameno=0, flux=flux_0, header={"TSTART": 0, "TSTOP": 10})
flux_9 = 3 * np.ones((6, 8))
factory.add_cadence(frameno=9, flux=flux_9, header={"TSTART": 90, "TSTOP": 100})
# You shouldn't be able to build a TPF like this...because TPFs shouldn't
# have extensions where time stamps are duplicated (here frames 1-8 will have)
# time stamp zero
with pytest.warns(LightkurveWarning, match="identical TIME values"):
tpf = factory.get_tpf()
[
factory.add_cadence(
frameno=i, flux=flux_0, header={"TSTART": i * 10, "TSTOP": (i * 10) + 10}
)
for i in np.arange(2, 9)
]
# This should fail because the time stamps of the images are not in order...
with pytest.warns(LightkurveWarning, match="chronological order"):
tpf = factory.get_tpf()
[
factory.add_cadence(
frameno=i, flux=flux_0, header={"TSTART": i * 10, "TSTOP": (i * 10) + 10}
)
for i in np.arange(1, 9)
]
# This should pass
tpf = factory.get_tpf(hdu0_keywords={"TELESCOP": "TESS"})
assert_array_equal(tpf.flux[0].value, flux_0)
assert_array_equal(tpf.flux[9].value, flux_9)
tpf = factory.get_tpf(hdu0_keywords={"TELESCOP": "Kepler"})
assert_array_equal(tpf.flux[0].value, flux_0)
assert_array_equal(tpf.flux[9].value, flux_9)
assert tpf.time[0].value == 5
assert tpf.time[9].value == 95
# Can you add the WRONG sized frame?
flux_wrong = 3 * np.ones((6, 9))
with pytest.raises(FactoryError):
factory.add_cadence(
frameno=2, flux=flux_wrong, header={"TSTART": 90, "TSTOP": 100}
)
# Can you add the WRONG cadence?
flux_wrong = 3 * np.ones((6, 8))
with pytest.raises(FactoryError):
factory.add_cadence(
frameno=11, flux=flux_wrong, header={"TSTART": 90, "TSTOP": 100}
)
# Can we add our own keywords?
tpf = factory.get_tpf(
hdu0_keywords={"creator": "Christina TargetPixelFileWriter", "TELESCOP": "TESS"}
)
assert tpf.get_keyword("CREATOR") == "Christina TargetPixelFileWriter"
def _create_image_array(header=None, shape=(5, 5)):
"""Helper function for tests below."""
if header is None:
header = fits.Header()
images = []
for i in range(5):
header["TSTART"] = i
header["TSTOP"] = i + 1
images.append(fits.ImageHDU(data=np.ones(shape), header=header))
return images
def test_tpf_from_images():
"""Basic tests of tpf.from_fits_images()"""
# Not without a wcs...
with pytest.raises(Exception):
TargetPixelFile.from_fits_images(
_create_image_array(),
size=(3, 3),
position=SkyCoord(-234.75, 8.3393, unit="deg"),
)
# Make a fake WCS based on astropy.docs...
w = wcs.WCS(naxis=2)
w.wcs.crpix = [-234.75, 8.3393]
w.wcs.cdelt = np.array([-0.066667, 0.066667])
w.wcs.crval = [0, -90]
w.wcs.ctype = ["RA---AIR", "DEC--AIR"]
w.wcs.set_pv([(2, 1, 45.0)])
pixcrd = np.array([[0, 0], [24, 38], [45, 98]], np.float_)
header = w.to_header()
header["CRVAL1P"] = 10
header["CRVAL2P"] = 20
ra, dec = 268.21686048, -73.66991904
# Now this should work.
images = _create_image_array(header=header)
with warnings.catch_warnings():
# Ignore "LightkurveWarning: Could not detect filetype as TESSTargetPixelFile or KeplerTargetPixelFile, returning generic TargetPixelFile instead."
warnings.simplefilter("ignore", LightkurveWarning)
tpf = TargetPixelFile.from_fits_images(
images, size=(3, 3), position=SkyCoord(ra, dec, unit=(u.deg, u.deg))
)
assert isinstance(tpf, TargetPixelFile)
with warnings.catch_warnings():
# Some cards are too long -- to be investigated.
warnings.simplefilter("ignore", VerifyWarning)
# Can we write the output to disk?
# `delete=False` is necessary below to enable writing to the file on Windows
# but it means we have to clean up the tmp file ourselves
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
tpf.to_fits(tmp.name)
finally:
tmp.close()
os.remove(tmp.name)
# Can we read in a list of file names or a list of HDUlists?
hdus = []
tmpfile_names = []
for im in images:
tmpfile = tempfile.NamedTemporaryFile(delete=False)
tmpfile_names.append(tmpfile.name)
hdu = fits.HDUList([fits.PrimaryHDU(), im])
hdu.writeto(tmpfile.name)
hdus.append(hdu)
with warnings.catch_warnings():
# Ignore "LightkurveWarning: Could not detect filetype as TESSTargetPixelFile or KeplerTargetPixelFile, returning generic TargetPixelFile instead."
warnings.simplefilter("ignore", LightkurveWarning)
# Should be able to run with a list of file names
tpf_tmpfiles = TargetPixelFile.from_fits_images(
tmpfile_names,
size=(3, 3),
position=SkyCoord(ra, dec, unit=(u.deg, u.deg)),
)
# Should be able to run with a list of HDUlists
tpf_hdus = TargetPixelFile.from_fits_images(
hdus, size=(3, 3), position=SkyCoord(ra, dec, unit=(u.deg, u.deg))
)
# Clean up the temporary files we created
for filename in tmpfile_names:
try:
os.remove(filename)
except PermissionError:
pass # This appears to happen on Windows
def test_tpf_wcs_from_images():
"""Test to see if tpf.from_fits_images() output a tpf with WCS in the header"""
# Not without a wcs...
with pytest.raises(Exception):
TargetPixelFile.from_fits_images(
_create_image_array(),
size=(3, 3),
position=SkyCoord(-234.75, 8.3393, unit="deg"),
)
# Make a fake WCS based on astropy.docs...
w = wcs.WCS(naxis=2)
w.wcs.crpix = [0.0, 0.0]
w.wcs.cdelt = np.array([0.001111, 0.001111])
w.wcs.crval = [23.2334, 45.2333]
w.wcs.ctype = ["RA---TAN", "DEC--TAN"]
header = w.to_header()
header["CRVAL1P"] = 10
header["CRVAL2P"] = 20
ra, dec = 23.2336, 45.235
with warnings.catch_warnings():
# Ignore "LightkurveWarning: Could not detect filetype as TESSTargetPixelFile or KeplerTargetPixelFile, returning generic TargetPixelFile instead."
warnings.simplefilter("ignore", LightkurveWarning)
# Now this should work.
tpf = TargetPixelFile.from_fits_images(
_create_image_array(header=header),
size=(3, 3),
position=SkyCoord(ra, dec, unit=(u.deg, u.deg)),
)
assert tpf.hdu[1].header["1CRPX5"] != UNDEFINED
assert tpf.hdu[1].header["1CTYP5"] == "RA---TAN"
assert tpf.hdu[1].header["2CTYP5"] == "DEC--TAN"
assert tpf.hdu[1].header["1CRPX5"] != UNDEFINED
assert tpf.hdu[1].header["2CRPX5"] != UNDEFINED
assert tpf.hdu[1].header["1CUNI5"] == "deg"
assert tpf.hdu[1].header["2CUNI5"] == "deg"
with warnings.catch_warnings():
# Ignore the warning: "PC1_1 = a floating-point value was expected."
warnings.simplefilter("ignore", AstropyWarning)
assert tpf.wcs.to_header()["CDELT1"] == w.wcs.cdelt[0]
def test_properties2(capfd):
"""Test if the describe function produces an output.
The output is 1870 characters at the moment, but we might add more properties."""
tpf = KeplerTargetPixelFile(filename_tpf_all_zeros)
tpf.show_properties()
out, err = capfd.readouterr()
assert len(out) > 1000
def test_interact():
"""Test the Jupyter notebook interact() widget."""
for tpf in [
KeplerTargetPixelFile(filename_tpf_one_center),
TessTargetPixelFile(filename_tess),
]:
tpf.interact()
@pytest.mark.remote_data
def test_interact_sky():
"""Test the Jupyter notebook interact() widget."""
for tpf in [
KeplerTargetPixelFile(filename_tpf_one_center),
TessTargetPixelFile(filename_tess),
]:
tpf.interact_sky()
def test_get_models():
"""Can we obtain PRF and TPF models?"""
tpf = KeplerTargetPixelFile(filename_tpf_all_zeros, quality_bitmask=None)
with warnings.catch_warnings():
# Ignore "RuntimeWarning: All-NaN slice encountered"
warnings.simplefilter("ignore", RuntimeWarning)
tpf.get_model()
tpf.get_prf_model()
@pytest.mark.remote_data
def test_tess_simulation():
"""Can we read simulated TESS data?"""
tpf = TessTargetPixelFile(TESS_SIM)
assert tpf.mission == "TESS"
assert tpf.time.scale == "tdb"
assert tpf.flux.shape == tpf.flux_err.shape
tpf.wcs
col, row = tpf.estimate_centroids()
# Regression test for https://github.com/lightkurve/lightkurve/pull/236
assert (tpf.time.value == 0).sum() == 0
def test_threshold_aperture_mask():
"""Does the threshold mask work?"""
tpf = KeplerTargetPixelFile(filename_tpf_one_center)
tpf.plot(aperture_mask="threshold")
lc = tpf.to_lightcurve(aperture_mask=tpf.create_threshold_mask(threshold=1))
assert (lc.flux.value == 1).all()
# The TESS file shows three pixel regions above a 2-sigma threshold;
# let's make sure the `reference_pixel` argument allows them to be selected.
tpf = TessTargetPixelFile(filename_tess)
assert tpf.create_threshold_mask(threshold=2.0).sum() == 25
assert (
tpf.create_threshold_mask(threshold=2.0, reference_pixel="center").sum() == 25
)
assert tpf.create_threshold_mask(threshold=2.0, reference_pixel=None).sum() == 28
assert tpf.create_threshold_mask(threshold=2.0, reference_pixel=(5, 0)).sum() == 2
# A mask which contains zero-flux pixels should work without crashing
tpf = KeplerTargetPixelFile(filename_tpf_all_zeros)
assert tpf.create_threshold_mask().sum() == 9
def test_tpf_tess():
"""Does a TESS Sector 1 TPF work?"""
tpf = TessTargetPixelFile(filename_tess, quality_bitmask=None)
assert tpf.mission == "TESS"
assert tpf.targetid == 25155310
assert tpf.sector == 1
assert tpf.camera == 4
assert tpf.ccd == 1
assert tpf.pipeline_mask.sum() == 9
assert tpf.background_mask.sum() == 30
lc = tpf.to_lightcurve()
assert isinstance(lc, TessLightCurve)
assert_array_equal(lc.time, tpf.time)
assert tpf.time.scale == "tdb"
assert tpf.flux.shape == tpf.flux_err.shape
tpf.wcs
col, row = tpf.estimate_centroids()
@pytest.mark.parametrize("tpf_type", [KeplerTargetPixelFile, TessTargetPixelFile])
def test_tpf_slicing(tpf_type):
"""Test indexing and slicing of TargetPixelFile objects."""
with warnings.catch_warnings():
# Ignore "LightkurveWarning: A Kepler data product is being opened using the `TessTargetPixelFile` class. Please use `KeplerTargetPixelFile` instead."
warnings.simplefilter("ignore", LightkurveWarning)
tpf = tpf_type(filename_tpf_one_center)
assert tpf[0].time == tpf.time[0]
assert tpf[-1].time == tpf.time[-1]
assert tpf[5:10].shape == tpf.flux[5:10].shape
assert tpf[0].targetid == tpf.targetid
assert_array_equal(tpf[tpf.time < tpf.time[5]].time, tpf.time[0:5])
frame = tpf[5]
assert frame.shape[0] == 1
assert frame.shape[1:] == tpf.shape[1:]
assert_array_equal(frame.time[0], tpf.time[5])
assert_array_equal(frame.flux[0], tpf.flux[5])
frames = tpf[100:200]
assert frames.shape[0] == 100
assert frames.shape[1:] == tpf.shape[1:]
assert_array_equal(frames.time, tpf.time[100:200])
assert_array_equal(frames.flux, tpf.flux[100:200])
def test_endianness():
"""Regression test for https://github.com/lightkurve/lightkurve/issues/188"""
tpf = KeplerTargetPixelFile(filename_tpf_one_center)
tpf.to_lightcurve().to_pandas().describe()
def test_get_keyword():
tpf = KeplerTargetPixelFile(filename_tpf_one_center)
assert tpf.get_keyword("TELESCOP") == "Kepler"
assert tpf.get_keyword("TTYPE1", hdu=1) == "TIME"
assert tpf.get_keyword("DOESNOTEXIST", default=5) == 5
def test_cutout():
"""Test tpf.cutout() function."""
for tpf in [
KeplerTargetPixelFile(filename_tpf_one_center),
TessTargetPixelFile(filename_tess, quality_bitmask=None),
]:
ntpf = tpf.cutout(size=2)
assert ntpf.flux[0].shape == (2, 2)
assert ntpf.flux_err[0].shape == (2, 2)
assert ntpf.flux_bkg[0].shape == (2, 2)
ntpf = tpf.cutout((0, 0), size=3)
ntpf = tpf.cutout(size=(1, 2))
assert ntpf.flux.shape[1] == 2
assert ntpf.flux.shape[2] == 1
ntpf = tpf.cutout(SkyCoord(tpf.ra, tpf.dec, unit="deg"), size=2)
ntpf = tpf.cutout(size=2)
assert np.product(ntpf.flux.shape[1:]) == 4
assert ntpf.targetid == "{}_CUTOUT".format(tpf.targetid)
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])
@pytest.mark.remote_data
def test_SSOs():
# TESS test
tpf = TessTargetPixelFile(asteroid_TPF)
result = tpf.query_solar_system_objects() # default cadence_mask = 'outliers'
assert (
result is None
) # the TPF has only data for 1 epoch. The lone time is removed as outlier
result = tpf.query_solar_system_objects(cadence_mask="all", cache=False)
assert len(result) == 1
result = tpf.query_solar_system_objects(
cadence_mask=np.asarray([True]), cache=False
)
assert len(result) == 1
result = tpf.query_solar_system_objects(cadence_mask=[True], cache=False)
assert len(result) == 1
result = tpf.query_solar_system_objects(cadence_mask=(True), cache=False)
assert len(result) == 1
result, mask = tpf.query_solar_system_objects(
cadence_mask=np.asarray([True]), cache=True, return_mask=True
)
assert len(mask) == len(tpf.flux)
try:
result = tpf.query_solar_system_objects(
cadence_mask="str-not-supported", cache=False
)
pytest.fail("Unsupported cadence_mask should have thrown Error")
except ValueError:
pass
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_plot_pixels():
tpf = KeplerTargetPixelFile(filename_tpf_one_center)
tpf.plot_pixels()
tpf.plot_pixels(normalize=True)
tpf.plot_pixels(periodogram=True)
tpf.plot_pixels(periodogram=True, nyquist_factor=0.5)
tpf.plot_pixels(aperture_mask="all")
tpf.plot_pixels(aperture_mask=tpf.pipeline_mask)
tpf.plot_pixels(aperture_mask=tpf.create_threshold_mask())
tpf.plot_pixels(show_flux=True)
tpf.plot_pixels(corrector_func=lambda x: x)
plt.close("all")
@pytest.mark.remote_data
def test_missing_pipeline_mask():
"""Regression test for #791.
TPFs produced by TESSCut contain an empty pipeline mask. When the pipeline
mask is missing or empty, we want `to_lightcurve()` to fall back on the
'threshold' mask by default, to avoid creating a light curve based on zero pixels."""
tpf = search_tesscut("Proxima Cen", sector=12).download(cutout_size=1)
lc = tpf.to_lightcurve()
assert np.isfinite(lc.flux).any()
assert lc.meta.get("APERTURE_MASK", None) == "threshold"
with pytest.raises(ValueError):
# if aperture_mask is explicitly set as pipeline,
# the logic will throw an error as it is missing in the TPF
lc = tpf.to_lightcurve(aperture_mask="pipeline")
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_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_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_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_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_animate():
tpf = read(filename_tpf_one_center)
tpf.animate()