Coverage for tests/test_extractor.py: 13%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from numpy.testing import run_module_suite
3from spectractor import parameters
4from spectractor.extractor.extractor import Spectractor
5from spectractor.logbook import LogBook
6from spectractor.config import load_config
7import os
8import numpy as np
9import unittest
12def test_logbook():
13 logbook = LogBook('./ctiofulllogbook_jun2017_v5.csv')
14 # target, xpos, ypos = logbook.search_for_image('reduc_20170529_085.fits')
15 # assert xpos is None
16 disperser_label, target, xpos, ypos = logbook.search_for_image('reduc_20170603_020.fits')
17 assert target == "PKS1510-089"
18 assert xpos == 830
19 assert ypos == 590
20 # logbook = LogBook('./ctiofulllogbook_jun2017_v5.csv')
21 # logbook.plot_columns_vs_date(['T', 'seeing', 'W'])
24def test_extractor_ctio():
25 file_names = ['tests/data/reduc_20170530_134.fits']
26 output_directory = "./outputs"
28 logbook = LogBook(logbook='./ctiofulllogbook_jun2017_v5.csv')
29 load_config("./config/ctio.ini")
30 parameters.VERBOSE = True
31 parameters.DEBUG = True
32 parameters.CCD_REBIN = 2
34 for file_name in file_names:
35 tag = file_name.split('/')[-1].replace("sim", "reduc")
36 disperser_label, target_label, xpos, ypos = logbook.search_for_image(tag)
37 if target_label is None or xpos is None or ypos is None:
38 continue
39 spectrum = Spectractor(file_name, output_directory, target_label, [xpos, ypos], disperser_label,
40 atmospheric_lines=True)
41 assert spectrum.data is not None
42 spectrum.my_logger.warning(f"\n\tQuantities to test:"
43 f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
44 f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
45 f"\n\t\tspectrum.x0={spectrum.x0}"
46 f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
47 f"\n\t\tspectrum total flux={np.sum(spectrum.data) * parameters.CCD_REBIN ** 2}"
48 f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
49 f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
50 assert np.sum(spectrum.data) * parameters.CCD_REBIN**2 > 2e-11 / parameters.CCD_REBIN
51 if parameters.CCD_REBIN == 1:
52 if parameters.PSF_EXTRACTION_MODE == "PSD_2D":
53 assert np.isclose(spectrum.lambdas[0], 343, atol=1)
54 assert np.isclose(spectrum.lambdas[-1], 1084.0, atol=1)
55 elif parameters.PSF_EXTRACTION_MODE == "PSF_1D":
56 assert np.isclose(spectrum.lambdas[0], 347, atol=1)
57 assert np.isclose(spectrum.lambdas[-1], 1085.0, atol=1)
58 assert np.isclose(spectrum.spectrogram_x0, -280, atol=1)
59 assert np.isclose(spectrum.x0[0] * parameters.CCD_REBIN, 743.6651370068676, atol=0.5 * parameters.CCD_REBIN)
60 assert np.isclose(spectrum.x0[1] * parameters.CCD_REBIN, 683.0577836601408, atol=1 * parameters.CCD_REBIN)
61 assert 2 < np.mean(spectrum.chromatic_psf.table['gamma']) * parameters.CCD_REBIN < 3.5
62 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrum.fits'))) is True
63 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrogram.fits'))) is True
64 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_lines.csv'))) is True
67# TODO: DM-33441 Fix broken spectractor tests
68@unittest.skip('Skipping due broken test (TypeError: argument of type bool is not iterable)')
69def test_extractor_ctio_planetary_nebula():
70 file_names = ['tests/data/reduc_20170605_028.fits']
71 output_directory = "./outputs"
73 logbook = LogBook(logbook='./ctiofulllogbook_jun2017_v5.csv')
74 load_config("./config/ctio.ini")
75 parameters.VERBOSE = True
76 parameters.DEBUG = True
77 parameters.CCD_REBIN = 1 # do not work with other values
78 parameters.LAMBDA_MIN = 450
79 parameters.LAMBDA_MAX = 1000
81 for file_name in file_names:
82 tag = file_name.split('/')[-1]
83 disperser_label, target_label, xpos, ypos = logbook.search_for_image(tag)
84 if target_label is None or xpos is None or ypos is None:
85 continue
86 spectrum = Spectractor(file_name, output_directory, target_label, [xpos, ypos], disperser_label,
87 atmospheric_lines=True)
88 assert spectrum.data is not None
89 spectrum.my_logger.warning(f"\n\tQuantities to test:"
90 f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
91 f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
92 f"\n\t\tspectrum.x0={spectrum.x0}"
93 f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
94 f"\n\t\tspectrum total flux={np.sum(spectrum.data) * parameters.CCD_REBIN ** 2}"
95 f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
96 f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
97 if parameters.PSF_EXTRACTION_MODE == "PSD_2D":
98 assert np.isclose(spectrum.lambdas[0], 449, atol=1)
99 assert np.isclose(spectrum.lambdas[-1], 996, atol=1)
100 elif parameters.PSF_EXTRACTION_MODE == "PSF_1D":
101 assert np.isclose(spectrum.lambdas[0], 443, atol=1)
102 assert np.isclose(spectrum.lambdas[-1], 981, atol=1)
103 assert np.isclose(spectrum.spectrogram_x0, -368, atol=1)
104 assert np.sum(spectrum.data) * parameters.CCD_REBIN ** 2 > 1e-11 / parameters.CCD_REBIN
105 assert np.isclose(spectrum.x0[0] * parameters.CCD_REBIN, 816.75, atol=0.5 * parameters.CCD_REBIN)
106 assert np.isclose(spectrum.x0[1] * parameters.CCD_REBIN, 587.67, atol=1 * parameters.CCD_REBIN)
107 assert 1 < np.mean(spectrum.chromatic_psf.table['gamma']) * parameters.CCD_REBIN < 2.5
108 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrum.fits'))) is True
109 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrogram.fits'))) is True
110 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_lines.csv'))) is True
113def extractor_auxtel():
114 file_names = ['tests/data/calexp_2020031500162-EMPTY_ronchi90lpmm-det000.fits'] # image 1
115 # file_names = ['tests/data/calexp_2020031200313-EMPTY_ronchi90lpmm-det000.fits'] # image 2
116 # file_names = ['tests/data/calexp_2020022100767-EMPTY_ronchi90lpmm-det000.fits'] # image 3
117 # file_names = ['tests/data//calexp_2020021800154-EMPTY_ronchi90lpmm-det000.fits'] # image 4
118 # tests/data/auxtel_first_light-1.fits']
120 # logbook = LogBook(logbook='./ctiofulllogbook_jun2017_v5.csv')
121 parameters.VERBOSE = True
122 parameters.DEBUG = True
123 xpos = 1600
124 ypos = 2293
125 target_label = "HD107696"
127 for config in ['./config/auxtel_quicklook.ini', './config/auxtel.ini']:
128 for file_name in file_names:
129 # tag = file_name.split('/')[-1]
130 # disperser_label, target, xpos, ypos = logbook.search_for_image(tag)
131 spectrum = Spectractor(file_name, './outputs/', target_label=target_label, guess=[xpos, ypos],
132 config=config, atmospheric_lines=True)
133 assert spectrum.data is not None
134 assert np.sum(spectrum.data) > 1e-14
135 # spectrum.my_logger.warning(f"\n\tQuantities to test:"
136 # f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
137 # f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
138 # f"\n\t\tspectrum.x0={spectrum.x0}"
139 # f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
140 # f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
141 # f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
142 # assert np.isclose(spectrum.lambdas[0], 296, atol=1)
143 # assert np.isclose(spectrum.lambdas[-1], 1083.5, atol=1)
144 # assert np.isclose(spectrum.x0[0], 743.6651370068676, atol=0.5)
145 # assert np.isclose(spectrum.x0[1], 683.0577836601408, atol=1)
146 # assert np.isclose(spectrum.spectrogram_x0, -240, atol=1)
147 # assert 2 < np.mean(spectrum.chromatic_psf.table['gamma']) < 3
148 # assert os.path.isfile('./outputs/' + tag.replace('.fits', '_spectrum.fits')) is True
149 # assert os.path.isfile('./outputs/' + tag.replace('.fits', '_spectrogram.fits')) is True
152if __name__ == "__main__": 152 ↛ 154line 152 didn't jump to line 154, because the condition on line 152 was never true
154 run_module_suite()