forked from StingraySoftware/stingray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_io.py
183 lines (146 loc) · 6.51 KB
/
test_io.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
import numpy as np
import os
import matplotlib.pyplot as plt
import pytest
from astropy.utils.exceptions import AstropyUserWarning
from ..io import split_numbers
from ..io import ref_mjd
from ..io import high_precision_keyword_read
from ..io import load_events_and_gtis, read_mission_info
from ..io import read_header_key
import warnings
curdir = os.path.abspath(os.path.dirname(__file__))
datadir = os.path.join(curdir, "data")
_H5PY_INSTALLED = True
try:
import h5py
except ImportError:
_H5PY_INSTALLED = False
skip_condition = pytest.mark.skipif(not _H5PY_INSTALLED, reason="H5PY not installed.")
class TestIO(object):
def test_common_name(self):
"""Test the common_name function."""
from ..io import common_name
a = "A_3-50_A"
b = "B_3-50_B"
assert common_name(a, b) == "3-50"
def test_high_precision_keyword(self):
"""Test high precision FITS keyword read."""
hdr = {"MJDTESTI": 100, "MJDTESTF": np.longdouble(0.5), "CIAO": np.longdouble(0.0)}
assert high_precision_keyword_read(hdr, "MJDTEST") == np.longdouble(
100.5
), "Keyword MJDTEST read incorrectly"
assert high_precision_keyword_read(hdr, "MJDTESTA") == np.longdouble(
100.5
), "Keyword MJDTESTA read incorrectly"
assert high_precision_keyword_read(hdr, "CIAO") == np.longdouble(
0.0
), "Keyword CIAO read incorrectly"
assert high_precision_keyword_read(hdr, "BU") is None, "Inexistent key read incorrectly"
def test_xselect_mdb_is_found_headas(self, monkeypatch, tmp_path):
"""Test event file reading."""
path = tmp_path / "bin"
path.mkdir()
f = path / "xselect.mdb"
f.write_text("MAXI:submkey NONE\nMAXI:instkey INSTRUME")
monkeypatch.setenv("HEADAS", str(tmp_path))
info = read_mission_info()
assert "NUSTAR" not in info
def test_read_whole_mission_info(self):
"""Test event file reading."""
info = read_mission_info()
assert "NUSTAR" in info
assert "XMM" in info
assert "NICER" in info
def test_event_file_read_and_automatic_sort(self):
"""Test event file reading."""
fname = os.path.join(datadir, "monol_testA_calib.evt")
with pytest.warns(AstropyUserWarning, match="No extensions found with a"):
evdata = load_events_and_gtis(fname)
fname_unsrt = os.path.join(datadir, "monol_testA_calib_unsrt.evt")
with pytest.warns(UserWarning, match="not sorted. Sorting them for you"):
evdata_unsrt = load_events_and_gtis(fname_unsrt)
for attr in "ev_list", "energy_list", "pi_list":
assert np.allclose(getattr(evdata, attr), getattr(evdata_unsrt, attr))
def test_event_file_read_additional_warns_uncal(self):
"""Test event file reading."""
fname = os.path.join(datadir, "monol_testA.evt")
with pytest.warns(UserWarning) as record:
vals = load_events_and_gtis(fname, additional_columns=["energy"])
assert np.any(["Column energy not found" in r.message.args[0] for r in record])
# This is the default calibration for nustar data, as returned
# from rough_calibration
assert np.allclose(vals.energy_list, vals.pi_list * 0.04 + 1.6)
def test_event_file_read_additional_energy_cal(self):
"""Test event file reading."""
fname = os.path.join(datadir, "monol_testA_calib.evt")
with pytest.warns(AstropyUserWarning, match="No extensions found with a"):
vals = load_events_and_gtis(fname, additional_columns=["energy"])
# These energies were calibrated with a different calibration than
# returned from rough_calibration, on purpose! (notice the +1.)
assert np.allclose(vals.energy_list, vals.pi_list * 0.04 + 1.6 + 1.0)
def test_event_file_read_xmm(self):
"""Test event file reading."""
fname = os.path.join(datadir, "xmm_test.fits")
with pytest.warns(UserWarning) as record:
load_events_and_gtis(fname, additional_columns=["PRIOR"])
assert np.any(["Trying first extension" in r.message.args[0] for r in record])
def test_event_file_read_no_mission(self):
"""Test event file reading."""
fname = os.path.join(datadir, "nomission.evt")
with pytest.warns(UserWarning, match="Sorting them"):
load_events_and_gtis(fname)
def test_event_file_read_no_additional(self):
"""Test event file reading."""
fname = os.path.join(datadir, "monol_testA.evt")
load_events_and_gtis(fname)
def test_event_file_read_no_pi(self):
"""Test event file reading."""
fname = os.path.join(datadir, "monol_testA.evt")
load_events_and_gtis(fname)
def test_read_header_key(self):
"""Test event file reading."""
fname = os.path.join(datadir, "monol_testA.evt")
assert read_header_key(fname, "INSTRUME") == "FPMA"
assert read_header_key(fname, "BU") == ""
def test_read_mjdref(self):
"""Test event file reading."""
fname = os.path.join(datadir, "monol_testA.evt")
assert ref_mjd(fname) is not None
def test_split_number(self):
"""Test split with high precision numbers."""
numbers = np.array(
[57401.0000003423423400453453, 0.00000574010000003426646], dtype=np.longdouble
)
number_I, number_F = split_numbers(numbers)
r_numbers = np.longdouble(number_I) + np.longdouble(number_F)
assert (numbers == r_numbers).all()
n = [1234.567, 12.345]
shift = -2
n_i, n_f = split_numbers(n, shift)
assert np.allclose(n_i, [1200, 0])
r_n = n_i + n_f
assert (n == r_n).all()
class TmpIOReadWrite(object):
"""A temporary helper class to test all the read and write functions."""
def __init__(self):
self.number = 10
self.str = "Test"
self.list = [1, 2, 3]
self.array = np.array([1, 2, 3])
self.long_number = np.longdouble(1.25)
self.long_array = np.longdouble([1, 2, 3])
def test_operation(self):
return self.number * 10
class TestFileFormats(object):
def test_savefig_without_plot(self):
from ..io import savefig
plt.close("all")
with pytest.warns(UserWarning, match="plot the image first"):
savefig("test.png")
os.unlink("test.png")
def test_savefig(self):
from ..io import savefig
plt.plot([1, 2, 3])
savefig("test.png")
os.unlink("test.png")