LALPulsar 7.1.1.1-eeff03c
test_lineFileParser.py
Go to the documentation of this file.
1# Copyright (C) 2021 Rodrigo Tenorio
2#
3# This program is free software; you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation; either version 2 of the License, or (at your
6# option) any later version.
7#
8# This program is distributed in the hope that it will be useful, but
9# WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11# Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17"""
18Test code for lineFileParser.py
19"""
20
21import sys
22import tempfile
23
24try:
25 from pathlib import Path
26except ImportError as exc: # probably macports
27 import warnings
28
29 warnings.warn(str(exc))
30 sys.exit(77)
31
32import numpy as np
33from numpy.testing import assert_allclose
34
35from lalpulsar.lineFileParser import LineFileParser
36
37import pytest
38
39
40@pytest.fixture
42 return {
43 "left_side": np.array(
44 [
45 15.95,
46 0.95,
47 1.95,
48 2.95,
49 3.95,
50 4.95,
51 9.95,
52 19.9,
53 29.85,
54 39.8,
55 49.75,
56 109.0,
57 209.0,
58 309.0,
59 ]
60 ),
61 "right_side": np.array(
62 [
63 16.05,
64 1.05,
65 2.05,
66 3.05,
67 4.05,
68 5.05,
69 10.05,
70 20.1,
71 30.15,
72 40.2,
73 50.25,
74 111.0,
75 211.0,
76 311.0,
77 ]
78 ),
79 }
80
81
82@pytest.fixture
84 return {
85 "left_side": np.array([10.0, 10.2, 10.4]),
86 "right_side": np.array([10.2, 10.4, 10.6]),
87 }
88
89
90@pytest.yield_fixture
92 filename = "IdentifiedLines.csv"
93 line_data = (
94 "# git repo: <url-to-repo>\n# git tag: <tag>\n# git hash: <hash>\n"
95 "Frequency or frequency spacing [Hz],Type (0:line; 1:comb; 2:comb with scaling width),"
96 "Frequency offset [Hz],First visible harmonic, Last visible harmonic, "
97 "Left width [Hz], Right width [Hz], Comments\n"
98 "16.0,0,0,1,1,0.05,0.05,Calibration line\n"
99 "1.0,1,0,1,5,0.05,0.05,Comb\n"
100 "10.0,2,0,1,5,0.05,0.05,Scaling Comb\n"
101 "100.0,1,10,1,3,1.0,1.0,Comb with offset\n"
102 )
103 with tempfile.TemporaryDirectory() as tmpdir:
104 filepath = Path(tmpdir) / filename
105 filepath.write_text(line_data)
106 yield filepath
107
108
109@pytest.yield_fixture
111 filename = "UnidentifiedLines.csv"
112 line_data = (
113 "# git repo: <url-to-repo>\n# git tag: <tag>\n# git hash: <hash>\n"
114 "Frequency [Hz], Comments\n"
115 "10.1,Unidentified line\n"
116 "10.3,Unidentified line\n"
117 "10.5,Unidentified line\n"
118 )
119 with tempfile.TemporaryDirectory() as tmpdir:
120 filepath = Path(tmpdir) / filename
121 filepath.write_text(line_data)
122 yield filepath
123
124
125@pytest.fixture
127 return {
128 "frequency": 0,
129 "left_wing": 5,
130 "right_wing": 6,
131 "first_harmonic": 3,
132 "last_harmonic": 4,
133 "comb_type": 1,
134 "offset": 2,
135 }
136
137
138@pytest.fixture
140 return {"frequency": 0}
141
142
144 identified_lines, identified_lines_file, identified_lines_header
145):
146 """
147 Test that identified line files are properly read and expanded
148 by comparing left, right limits to the expected values.
149 """
150
151 for columns in [identified_lines_header, None]:
152 line_parser = LineFileParser()
153 line_parser.parse_identified_lines_csv(
154 lines_file=identified_lines_file,
155 columns=columns,
156 genfromtxt_kwargs={"delimiter": ",", "skip_header": 4},
157 )
158
159 assert_allclose(identified_lines["left_side"], line_parser.lines_left_side)
160 assert_allclose(identified_lines["right_side"], line_parser.lines_right_side)
161
162
164 unidentified_lines, unidentified_lines_file, unidentified_lines_header
165):
166 """
167 Test that unidentified line files are properly read and expanded
168 by comparing left, right limits to the expected values.
169 """
170
171 for columns in [unidentified_lines_header, None]:
172 line_parser = LineFileParser()
173 line_parser.parse_unidentified_lines_csv(
174 lines_file=unidentified_lines_file,
175 columns=columns,
176 extra_wing_Hz=0.1,
177 genfromtxt_kwargs={"delimiter": ",", "skip_header": 4},
178 )
179
180 assert_allclose(unidentified_lines["left_side"], line_parser.lines_left_side)
181 assert_allclose(unidentified_lines["right_side"], line_parser.lines_right_side)
182
183
185 identified_lines,
186 identified_lines_file,
187 identified_lines_header,
188 unidentified_lines,
189 unidentified_lines_file,
190 unidentified_lines_header,
191):
192 """
193 Test that files are properly read and concatenated.
194 """
195
196 line_parser = LineFileParser()
197 line_parser.parse_identified_lines_csv(
198 lines_file=identified_lines_file,
199 columns=identified_lines_header,
200 genfromtxt_kwargs={"delimiter": ",", "skip_header": 4},
201 )
202 line_parser.parse_unidentified_lines_csv(
203 lines_file=unidentified_lines_file,
204 columns=unidentified_lines_header,
205 extra_wing_Hz=0.1,
206 genfromtxt_kwargs={"delimiter": ",", "skip_header": 4},
207 )
208
209 assert_allclose(
210 np.hstack([identified_lines["left_side"], unidentified_lines["left_side"]]),
211 line_parser.lines_left_side,
212 )
213 assert_allclose(
214 np.hstack([identified_lines["right_side"], unidentified_lines["right_side"]]),
215 line_parser.lines_right_side,
216 )
217
218
219if __name__ == "__main__":
220 args = sys.argv[1:] or ["-v", "-rs"]
221 sys.exit(pytest.main(args=[__file__] + args))
def test_concatenation(identified_lines, identified_lines_file, identified_lines_header, unidentified_lines, unidentified_lines_file, unidentified_lines_header)
Test that files are properly read and concatenated.
def test_unidentified_lines_parsing(unidentified_lines, unidentified_lines_file, unidentified_lines_header)
Test that unidentified line files are properly read and expanded by comparing left,...
def test_identified_lines_parsing(identified_lines, identified_lines_file, identified_lines_header)
Test that identified line files are properly read and expanded by comparing left, right limits to the...