-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_core.py
1327 lines (1117 loc) · 40.8 KB
/
test_core.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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import warnings
import numpy as np
import pytest
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.io import ascii, fits
from astropy.nddata import CCDData
from astropy.table import Table, vstack
from astropy.time import Time
from astropy.utils.data import get_pkg_data_filename
from astropy.wcs import WCS
from astropy.wcs.wcs import FITSFixedWarning
from stellarphot.core import (
BaseEnhancedTable,
CatalogData,
PhotometryData,
SourceListData,
apass_dr9,
vsx_vizier,
)
from stellarphot.settings import Camera, Observatory, PassbandMap
# Create several test descriptions for use in base_enhanced_table tests.
test_descript = {
"id": None,
"ra": u.deg,
"dec": u.deg,
"sky_per_pix_avg": u.adu,
"sky_per_pix_med": u.adu,
"sky_per_pix_std": u.adu,
"fwhm_x": u.pix,
"fwhm_y": u.pix,
"width": u.pix,
}
# Define a realistic table of astronomical data containing one row
data = np.array(
[
[
1,
78.17278712191920,
22.505771480719400,
31.798216414544900,
31.658750534057600,
9.294325523269860,
13.02511260943810,
13.02511260943810,
13.02511260943810,
]
]
)
colnames = [
"id",
"ra",
"dec",
"sky_per_pix_avg",
"sky_per_pix_med",
"sky_per_pix_std",
"fwhm_x",
"fwhm_y",
"width",
]
coltypes = [
"int",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
]
colunits = [None, u.deg, u.deg, u.adu, u.adu, u.adu, u.pix, u.pix, u.pix]
testdata = Table(data, names=colnames, dtype=coltypes, units=colunits)
# Define some configuration information assuming Feder telescope
@pytest.fixture
def feder_cg_16m():
return Camera(
data_unit=u.adu,
gain=1.5 * u.electron / u.adu,
name="Andor Aspen CG16M",
read_noise=10.0 * u.electron,
dark_current=0.01 * u.electron / u.second,
pixel_scale=0.563 * u.arcsec / u.pix,
max_data_value=50000 * u.adu,
)
@pytest.fixture
def feder_passbands():
return PassbandMap(
name="Feder Passbands",
your_filter_names_to_aavso={
"up": "SU",
"gp": "SG",
"rp": "SR",
"zp": "SZ",
"ip": "SI",
},
)
@pytest.fixture
def feder_obs():
return Observatory(
name="Feder Observatory",
latitude=46.86678,
longitude=-96.45328,
elevation="311 m",
)
def test_base_enhanced_table_blank():
# This should just return a blank BaseEnhancedTable
test_base = BaseEnhancedTable()
assert isinstance(test_base, BaseEnhancedTable)
assert len(test_base) == 0
def test_base_enhanced_table_from_existing_table():
# Should create a populated dataset properly and display the astropy data
test_base2 = BaseEnhancedTable(table_description=test_descript, input_data=testdata)
assert len(test_base2["ra"]) == 1
assert len(test_base2["dec"]) == 1
def test_base_enhanced_table_clean():
# Check that the clean method exists
test_base = BaseEnhancedTable(table_description=test_descript, input_data=testdata)
# Add a row so that we can clean something
test_base_two = test_base.copy()
test_base_two.add_row(test_base[0])
test_base_two["ra"][1] = -test_base_two["ra"][1]
test_cleaned = test_base_two.clean(ra=">0.0")
assert len(test_cleaned) == 1
assert test_cleaned == test_base
def a_table(masked=False):
test_table = Table([(1, 2, 3), (1, -1, -2)], names=("a", "b"), masked=masked)
test_table = BaseEnhancedTable(
table_description={"a": None, "b": None}, input_data=test_table
)
return test_table
def test_bet_clean_criteria_none_removed():
"""
If all rows satisfy the criteria, none should be removed.
"""
inp = a_table()
criteria = {"a": ">0"}
out = inp.clean(**criteria)
assert len(out) == len(inp)
assert (out == inp).all()
@pytest.mark.parametrize(
"condition,input_row", [(">0", 0), ("=1", 0), (">=1", 0), ("<-1", 2), ("=-1", 1)]
)
def test_bet_clean_criteria_some_removed(condition, input_row):
"""
Try a few filters which leave only one row and make sure that row is
returned.
"""
inp = a_table()
criteria = {"b": condition}
out = inp.clean(**criteria)
assert len(out) == 1
assert (out[0] == inp[input_row]).all()
@pytest.mark.parametrize(
"criteria,error_msg",
[({"a": "5"}, "not understood"), ({"a": "<foo"}, "could not convert string")],
)
def test_clean_bad_criteria(criteria, error_msg):
"""
Make sure the appropriate error is raised when bad criteria are used.
"""
inp = a_table(masked=False)
with pytest.raises(ValueError, match=error_msg):
inp.clean(**criteria)
@pytest.mark.parametrize("clean_masked", [False, True])
def test_clean_masked_handled_correctly(clean_masked):
inp = a_table(masked=True)
# Mask negative values
inp["b"].mask = inp["b"] < 0
out = inp.clean(remove_rows_with_mask=clean_masked)
if clean_masked:
assert len(out) == 1
assert (np.array(out[0]) == np.array(inp[0])).all()
else:
assert len(out) == len(inp)
assert (out == inp).all()
def test_clean_masked_and_criteria():
"""
Check whether removing masked rows and using a criteria work
together.
"""
inp = a_table(masked=True)
# Mask the first row.
inp["b"].mask = inp["b"] > 0
inp_copy = inp.copy()
# This should remove the third row.
criteria = {"a": "<=2"}
out = inp.clean(remove_rows_with_mask=True, **criteria)
# Is only one row left?
assert len(out) == 1
# Is the row that is left the same as the second row of the input?
assert (np.array(out[0]) == np.array(inp[1])).all()
# Is the input table unchanged?
assert (inp == inp_copy).all()
def test_clean_criteria_none_removed():
"""
If all rows satisfy the criteria, none should be removed.
"""
inp = a_table()
criteria = {"a": ">0"}
out = inp.clean(**criteria)
assert len(out) == len(inp)
assert (out == inp).all()
def test_base_enhanced_table_missing_column():
# Should raise exception because the RA data is missing from input data
testdata_nora = testdata.copy()
testdata_nora.remove_column("ra")
with pytest.raises(ValueError):
_ = BaseEnhancedTable(table_description=test_descript, input_data=testdata_nora)
def test_base_enhanced_table_missing_badunits():
# This will fail due to RA being in units of hours
bad_ra_descript = test_descript.copy()
bad_ra_descript[1, 2] = u.hr
with pytest.raises(ValueError):
_ = BaseEnhancedTable(table_description=bad_ra_descript, input_data=testdata)
def test_base_enhanced_table_recursive():
# Should create a populated dataset properly and display the astropy data
test_base2 = BaseEnhancedTable(table_description=test_descript, input_data=testdata)
assert len(test_base2["ra"]) == 1
assert len(test_base2["dec"]) == 1
# Attempt recursive call
with pytest.raises(TypeError):
_ = BaseEnhancedTable(table_description=test_descript, input_data=test_base2)
def test_base_enhanced_table_no_desription():
# A missing description should raise a TypeError
with pytest.raises(TypeError, match="You must provide a dict as table_description"):
BaseEnhancedTable(input_data=testdata)
def test_base_enhanced_table_no_data():
# A missing description should raise a TypeError
with pytest.raises(TypeError, match="You must provide an astropy Table"):
BaseEnhancedTable(table_description=test_descript)
# Define a realistic table of photometry data (a bit corrupted)
photdata = np.array(
[
[
1,
2049.145245206124,
2054.0849947477964,
109070.60831212997,
154443.9371254444, # has wrong units
78.17278712191924,
22.505771480719375,
31.798216414544864, # has wrong units
31.658750534057617, # has wrong units
9.294325523269857, # has wrong units
13.02511260943813, # has wrong units
13.02511260943813, # has wrong units
13.02511260943813, # has wrong units
29.0,
2642.079421669016,
44.0,
59.0,
4853.760649796231,
120.0,
"2022-11-27T06:26:29.620", # created as a string, not a Time object
59909,
25057.195077483062,
2459910.7754060575,
-6.239606167785804,
1.115,
"ip",
"TIC_467615239.01-S001-R001-C001-ip.fit",
1,
0.02320185643388203,
803.1970935659333,
535.4647290439556, # has wrong units
46.795229859903905, # has wrong units -- maybe??
]
]
)
photcolnames = [
"id",
"xcenter",
"ycenter",
"aperture_sum",
"annulus_sum",
"ra",
"dec",
"sky_per_pix_avg",
"sky_per_pix_med",
"sky_per_pix_std",
"fwhm_x",
"fwhm_y",
"width",
"aperture",
"aperture_area",
"annulus_inner",
"annulus_outer",
"annulus_area",
"exposure",
"date-obs",
"night",
"aperture_net_cnts",
"bjd",
"mag_inst",
"airmass",
"passband",
"file",
"star_id",
"mag_error",
"noise_electrons",
"noise_cnts",
"snr",
]
photcoltypes = [
"int",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"float",
"str",
"int",
"float",
"float",
"float",
"float",
"str",
"str",
"int",
"float",
"float",
"float",
"float",
]
photcolunits = [
None,
u.pix,
u.pix,
u.adu,
None,
u.deg,
u.deg,
u.adu,
u.adu,
u.adu,
None,
None,
None,
u.pix,
u.pix,
u.pix,
u.pix,
u.pix,
u.s,
None,
None,
u.adu,
None,
None,
None,
None,
None,
None,
None,
u.electron,
None,
u.adu,
]
# Define initial bad table
testphot_data = Table(
photdata, names=photcolnames, dtype=photcoltypes, units=photcolunits
)
# Convert times to correct time format but leave bad units
testphot_goodTime = testphot_data.copy()
# The way this was originally written used Column(Time, ...) which
# led to a column of generic objects rather that Time objects. This
# prevented Table from being able to write the table to a file because it
# couldn't figure out how to write the column.
testphot_goodTime["date-obs"] = Time(
testphot_goodTime["date-obs"], format="isot", scale="utc"
)
# Fix all the units for PhotometryData
phot_descript = {
"star_id": None,
"ra": u.deg,
"dec": u.deg,
"xcenter": u.pix,
"ycenter": u.pix,
"fwhm_x": u.pix,
"fwhm_y": u.pix,
"width": u.pix,
"aperture": u.pix,
"annulus_inner": u.pix,
"annulus_outer": u.pix,
"aperture_sum": None,
"annulus_sum": None,
"sky_per_pix_avg": None,
"sky_per_pix_med": None,
"sky_per_pix_std": None,
"aperture_net_cnts": None,
"noise_cnts": None,
"noise_electrons": u.electron,
"exposure": u.second,
"date-obs": None,
"airmass": None,
"passband": None,
"file": None,
}
testphot_goodUnits = testphot_goodTime.copy()
for this_col, this_unit in phot_descript.items():
testphot_goodUnits[this_col].unit = this_unit
# Fix the units for the counts-related columns
counts_columns = ["aperture_sum", "annulus_sum", "aperture_net_cnts", "noise_cnts"]
counts_per_pixel_sqr_columns = ["sky_per_pix_avg", "sky_per_pix_med", "sky_per_pix_std"]
for this_col in counts_columns:
testphot_goodUnits[this_col].unit = u.adu
for this_col in counts_per_pixel_sqr_columns:
testphot_goodUnits[this_col].unit = u.adu * u.pixel**-1
# Remove calculated columns from the test data to produce clean data
computed_columns = ["bjd", "night"]
testphot_clean = testphot_goodUnits.copy()
for this_col in computed_columns:
del testphot_clean[this_col]
def test_photometry_blank():
# This should just return a blank PhotometryData
test_base = PhotometryData()
assert isinstance(test_base, PhotometryData)
assert len(test_base) == 0
assert test_base.camera is None
assert test_base.observatory is None
def test_photometry_with_colname_map(feder_cg_16m, feder_passbands, feder_obs):
# Rename one of the columns in the test data to something else
# and provide a colname_map that should fix it.
# Regression test for #469
this_phot_data = testphot_clean.copy()
this_phot_data.rename_column("aperture_net_cnts", "bad_column")
colname_map = {"bad_column": "aperture_net_cnts"}
pd = PhotometryData(
input_data=this_phot_data,
colname_map=colname_map,
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
)
assert "bad_column" not in pd.columns
@pytest.mark.parametrize("bjd_coordinates", [None, "custom"])
def test_photometry_data(feder_cg_16m, feder_passbands, feder_obs, bjd_coordinates):
# Create photometry data instance
custom_coord = SkyCoord(30.0, "22:30:20.77733059", unit="degree")
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_clean,
)
# Check some aspects of that data are sound
assert phot_data.camera.gain == 1.5 * u.electron / u.adu
assert phot_data.camera.read_noise == 10.0 * u.electron
assert phot_data.camera.dark_current == 0.01 * u.electron / u.second
assert phot_data.camera.pixel_scale == 0.563 * u.arcsec / u.pix
np.testing.assert_almost_equal(
phot_data.observatory.earth_location.lat.value, 46.86678
)
assert phot_data.observatory.earth_location.lat.unit == u.deg
np.testing.assert_almost_equal(
phot_data.observatory.earth_location.lon.value, -96.45328
)
assert phot_data.observatory.earth_location.lon.unit == u.deg
assert round(phot_data.observatory.earth_location.height.value) == 311
assert phot_data.observatory.earth_location.height.unit == u.m
assert phot_data["night"][0] == 59909
if bjd_coordinates is None:
# BJD calculation is done based on the locations of individual stars
phot_data.add_bjd_col()
# Checking the BJD computation against Ohio State online calculator for
# UTC 2022 11 27 06 27 29.620
# Latitude 46.86678
# Longitude -96.45328
# Elevation 311
# RA 78.17278712191924
# Dec 22 30 20.77733059
# which returned 2459910.775405664 (Uses custom IDL, astropy is SOFA checked).
# Demand a difference of less than 1/20 of a second.
assert (phot_data["bjd"][0].value - 2459910.775405664) * 86400 < 0.05
else:
# Do the BJD calculation based on the provided coordinates, same for
# all strs in the image.
phot_data.add_bjd_col(bjd_coordinates=custom_coord)
# Checking the BJD computation against Ohio State online calculator for
# UTC 2022 11 27 06 27 29.620
# Latitude 46.86678
# Longitude -96.45328
# Elevation 311
# RA 30
# Dec 22 30 20.77733059
# which returned 2459910.774772048 (Uses custom IDL, astropy is SOFA checked).
# Demand a difference of less than 1/20 of a second.
assert (phot_data["bjd"][0].value - 2459910.774772048) * 86400 < 0.05
def test_photometry_data_short_filter_name(feder_cg_16m, feder_obs):
# Regression test for #279.
# If the final passband name in the passband_map is longer than the
# longest passband name in the input data, the updated passband was
# truncated. That should not happen.
data = testphot_clean.copy()
# Delete the original passband column, just in case it is longer than
# the new passband name.
del data["passband"]
# Make a new 1-character column for passband
data["passband"] = "i"
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=PassbandMap(
name="Test map", your_filter_names_to_aavso={"i": "SI"}
),
input_data=data,
)
# Make sure the passband name is not truncated.
assert phot_data["passband"][0] == "SI"
def test_photometry_data_filter_name_map_preserves_original_names(
feder_cg_16m, feder_obs
):
# If a passband is not in the passband map it should be preserved.
data = testphot_clean.copy()
# Delete the original passband column
del data["passband"]
# Make a new 1-character column for passband
data["passband"] = "i"
# Add a second (identical) row
data.add_row(data[0])
# Change the first entry to a value not in the passband map
data["passband"][0] = "Q"
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=PassbandMap(
name="Test map", your_filter_names_to_aavso={"i": "SI"}
),
input_data=data,
)
# Make sure the passband names are correct
assert phot_data["passband"][0] == "Q"
assert phot_data["passband"][1] == "SI"
def test_photometry_roundtrip_ecsv(tmp_path, feder_cg_16m, feder_passbands, feder_obs):
# Check that we can save the test data to ECSV and restore it
file_path = tmp_path / "test_photometry.ecsv"
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_clean,
)
phot_data.write(file_path)
phot_data2 = PhotometryData.read(file_path)
# Check a couple of the columns that are not standard types
assert phot_data["date-obs"] == phot_data2["date-obs"]
assert phot_data["ra"] == phot_data2["ra"]
def test_photometry_slicing(feder_cg_16m, feder_passbands, feder_obs):
# Create photometry data instance
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_clean,
)
# Test slicing works as expected, leaving attributes intact
two_cols = phot_data[["ra", "dec"]]
assert two_cols.camera.gain == 1.5 * u.electron / u.adu
assert two_cols.camera.read_noise == 10.0 * u.electron
assert two_cols.camera.dark_current == 0.01 * u.electron / u.second
assert two_cols.camera.pixel_scale == 0.563 * u.arcsec / u.pix
np.testing.assert_almost_equal(
two_cols.observatory.earth_location.lat.value, 46.86678
)
assert two_cols.observatory.earth_location.lat.unit == u.deg
np.testing.assert_almost_equal(
two_cols.observatory.earth_location.lon.value, -96.45328
)
assert two_cols.observatory.earth_location.lon.unit == u.deg
assert round(two_cols.observatory.earth_location.height.value) == 311
assert two_cols.observatory.earth_location.height.unit == u.m
def test_photometry_recursive(feder_cg_16m, feder_passbands, feder_obs):
# Create photometry data instance
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_clean,
)
# Attempt recursive call
with pytest.raises(TypeError):
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=phot_data,
)
def test_photometry_badtime(feder_cg_16m, feder_passbands, feder_obs):
with pytest.raises(ValueError):
_ = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_data,
)
def test_photometry_inconsistent_count_units(feder_cg_16m, feder_passbands, feder_obs):
with pytest.raises(ValueError):
_ = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_goodTime,
)
def test_photometry_inconsistent_computed_col_exists(
feder_cg_16m, feder_passbands, feder_obs
):
with pytest.raises(ValueError):
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_goodUnits,
)
phot_data = PhotometryData(
observatory=feder_obs,
camera=feder_cg_16m,
passband_map=feder_passbands,
input_data=testphot_goodUnits,
retain_user_computed=True,
)
# This keeps a bad user column for 'snr' which has bogus units, so check the units
# cause a crash in the math.
with pytest.raises(u.core.UnitConversionError):
assert np.abs(phot_data["snr"][0] - 46.795229859903905) < 1e-6
assert np.abs(phot_data["snr"][0].value - 46.795229859903905) < 1e-6
# Load test catalog
test_cat = ascii.read(
get_pkg_data_filename("data/test_vsx_table.ecsv"), format="ecsv", fast_reader=False
)
def test_catalog_missing_col():
# Fails with ValueError due to not having 'ra' column
with pytest.raises(ValueError):
_ = CatalogData(
input_data=test_cat, catalog_name="VSX", catalog_source="Vizier"
)
def test_catalog_colname_map():
# Map column names
vsx_colname_map = {
"Name": "id",
"RAJ2000": "ra",
"DEJ2000": "dec",
"max": "mag",
"n_max": "passband",
}
catalog_dat = CatalogData(
input_data=test_cat,
catalog_name="VSX",
catalog_source="Vizier",
colname_map=vsx_colname_map,
no_catalog_error=True,
)
assert catalog_dat["id"][0] == "ASASSN-V J000052.03+002216.6"
assert np.abs(catalog_dat["mag"][0].value - 12.660) < 2e-7
assert catalog_dat["passband"][0] == "g"
assert catalog_dat.catalog_name == "VSX"
assert catalog_dat.catalog_source == "Vizier"
catalog_dat.add_row(catalog_dat[0])
catalog_dat.add_row(catalog_dat[0])
catalog_dat.add_row(catalog_dat[0])
catalog_slice = catalog_dat[0:2]
assert catalog_slice.catalog_name == "VSX"
assert catalog_slice.catalog_source == "Vizier"
def test_catalog_bandpassmap():
# Map column and bandpass names
vsx_colname_map = {
"Name": "id",
"RAJ2000": "ra",
"DEJ2000": "dec",
"max": "mag",
"n_max": "passband",
}
passband_map = {"g": "SG", "r": "SR"}
catalog_dat = CatalogData(
input_data=test_cat,
catalog_name="VSX",
catalog_source="Vizier",
colname_map=vsx_colname_map,
passband_map=passband_map,
no_catalog_error=True,
)
assert catalog_dat["passband"][0] == "SG"
assert catalog_dat.catalog_name == "VSX"
assert catalog_dat.catalog_source == "Vizier"
# Also test that we can read the bandpass map
assert catalog_dat.passband_map["g"] == "SG"
def test_catalog_recursive():
# Construct good objects
vsx_colname_map = {
"Name": "id",
"RAJ2000": "ra",
"DEJ2000": "dec",
"max": "mag",
"n_max": "passband",
}
catalog_dat = CatalogData(
input_data=test_cat,
catalog_name="VSX",
catalog_source="Vizier",
colname_map=vsx_colname_map,
no_catalog_error=True,
)
# Attempt recursive call
with pytest.raises(TypeError):
_ = CatalogData(
input_data=catalog_dat,
catalog_name="VSX",
catalog_source="Vizier",
colname_map=vsx_colname_map,
)
def test_tidy_vizier_catalog():
# Test just the part of the code that converts the table returned by Vizier
# into a table that can be used by CatalogData.
apass_input = Table.read(get_pkg_data_filename("data/test_apass_subset.ecsv"))
result = CatalogData._tidy_vizier_catalog(
apass_input,
r"^([a-zA-Z]+|[a-zA-Z]+-[a-zA-Z]+)_?mag$",
r"^([a-zA-Z]+-[a-zA-Z]+)$",
)
assert len(result) == 6
# Check some column names
assert "passband" in result.colnames
assert "mag" in result.colnames
assert "mag_error" in result.colnames
# Spot check a couple of values
one_star = 16572870
one_Vmag = 13.399
one_Vmag_error = 0.075
just_one = result[(result["recno"] == one_star) & (result["passband"] == "V")]
assert np.abs(just_one["mag"][0] - one_Vmag) < 1e-6
assert np.abs(just_one["mag_error"][0] - one_Vmag_error) < 1e-6
def test_tidy_vizier_catalog_several_mags():
# Test table conversion when there are several magnitude columns.
apass_input = Table.read(get_pkg_data_filename("data/test_apass_subset.ecsv"))
# Make sure the columns we expect in the test data exists before proceeding
assert "Vmag" in apass_input.colnames
assert "i_mag" in apass_input.colnames
assert "B-V" in apass_input.colnames
# Add a B magnitude column, and an r-i color. The values are nonsense.
apass_input["Bmag"] = apass_input["Vmag"]
apass_input["r-i"] = apass_input["B-V"]
result = CatalogData._tidy_vizier_catalog(
apass_input,
r"^([a-zA-Z]+|[a-zA-Z]+-[a-zA-Z]+)_?mag$",
r"^([a-zA-Z]+-[a-zA-Z]+)$",
)
assert set(result["passband"]) == {"V", "B", "i", "r-i", "B-V"}
def test_catalog_to_passband_columns():
# Test that the passband columns are correctly identified
# even if the passband names are not in the passband map.
apass_input = Table.read(get_pkg_data_filename("data/test_apass_subset.ecsv"))
input_data = CatalogData._tidy_vizier_catalog(
apass_input,
r"^([a-zA-Z]+|[a-zA-Z]+-[a-zA-Z]+)_?mag$",
r"^([a-zA-Z]+-[a-zA-Z]+)$",
)
print(input_data.colnames)
input_data["RAJ2000"].unit = u.deg
input_data["DEJ2000"].unit = u.deg
cat = CatalogData(
input_data=input_data,
colname_map=dict(recno="id", RAJ2000="ra", DEJ2000="dec"),
catalog_name="APASS",
catalog_source="Vizier",
passband_map=dict(g="SG", r="SR", i="SI"),
)
# Check that calling with a bad filter name raises an error
with pytest.raises(ValueError, match="not found in catalog"):
cat.passband_columns(["not a filter"])
# These are the only passbands in the input test data
passbands = ["V", "SI"]
new_cat = cat.passband_columns(passbands=passbands)
# Check the column names
for pb in passbands:
assert f"mag_{pb}" in new_cat.colnames
assert f"mag_error_{pb}" in new_cat.colnames
# Check the data
assert set(new_cat["mag_V"]) == set(apass_input["Vmag"])
assert set(new_cat["mag_error_V"]) == set(apass_input["e_Vmag"])
assert set(new_cat["mag_SI"]) == set(apass_input["i_mag"])
assert set(new_cat["mag_error_SI"]) == set(apass_input["e_i_mag"])
# Check that calling with no passbands returns all of the passbands
new_cat = cat.passband_columns()
cat_pbs = set(cat["passband"])
for pb in cat_pbs:
assert f"mag_{pb}" in new_cat.colnames
assert f"mag_error_{pb}" in new_cat.colnames
@pytest.mark.remote_data
def test_catalog_from_vizier_search_apass():
# Nothing special about this point...
sc = SkyCoord(ra=0, dec=0, unit="deg")
# Small enough radius to get only one star
radius = 0.03 * u.deg
# Use the APASS class factory to get the catalog
apass = apass_dr9(sc, radius=radius)
assert len(apass) == 6
# Calculated the value below by constructing a coordinate-based
# designation following IAU guidelines.
assert apass["id"][0] == "APASSSP J+359.9896+00.0122"
just_V = apass[apass["passband"] == "V"]
assert np.abs(just_V["mag"][0] - 15.559) < 1e-6
@pytest.mark.remote_data
@pytest.mark.parametrize("location_method", ["coord", "header", "wcs"])
def test_catalog_from_vizier_search_vsx(location_method):
# Do a cone search with a small enough radius to return exactly one star,
# DQ Psc, which happens to already be in the test data.
coordinate = SkyCoord(ra=359.94371 * u.deg, dec=-0.2801 * u.deg)
if location_method == "coord":
center = coordinate
else:
# We need a WCS for each of these methods, so make that first.
# The projection doesn't matter because we only need the center
# coordinate. The code below is more or less copied from
# https://docs.astropy.org/en/stable/wcs/example_create_imaging.html
wcs = WCS(naxis=2)
# Put the center in the right place
wcs.wcs.crpix = [100, 100]
wcs.wcs.crval = [coordinate.ra.degree, coordinate.dec.degree]
wcs.array_shape = [200, 200]
# The rest of these values shouldn't matter for this test
wcs.wcs.ctype = ["RA---AIR", "DEC--AIR"]
wcs.wcs.set_pv([(2, 1, 45.0)])
wcs.wcs.cdelt = np.array([-0.066667, 0.066667])
if location_method == "header":
center = wcs.to_header()
center["NAXIS1"] = wcs.array_shape[0]
center["NAXIS2"] = wcs.array_shape[1]
else:
center = wcs
vsx_map = dict(
Name="id",
RAJ2000="ra",
DEJ2000="dec",
)
# This one is easier -- it already has the passband in a column name.
# We'll use the maximum magnitude as the magnitude column.
def prepare_cat(cat):
cat.rename_column("max", "mag")
cat.rename_column("n_max", "passband")
return cat
my_cat = CatalogData.from_vizier(
center,
"B/vsx/vsx",
radius=0.1 * u.arcmin,
clip_by_frame=False,
colname_map=vsx_map,
prepare_catalog=prepare_cat,