Coverage for tests/test_extractor.py: 14%
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, apply_rebinning_to_parameters
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
33 apply_rebinning_to_parameters()
35 for file_name in file_names:
36 tag = file_name.split('/')[-1].replace("sim", "reduc")
37 disperser_label, target_label, xpos, ypos = logbook.search_for_image(tag)
38 if target_label is None or xpos is None or ypos is None:
39 continue
40 spectrum = Spectractor(file_name, output_directory, target_label, [xpos, ypos], disperser_label,
41 atmospheric_lines=True)
42 assert spectrum.data is not None
43 spectrum.my_logger.warning(f"\n\tQuantities to test:"
44 f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
45 f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
46 f"\n\t\tspectrum.x0={spectrum.x0}"
47 f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
48 f"\n\t\tspectrum total flux={np.sum(spectrum.data) * parameters.CCD_REBIN ** 2}"
49 f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
50 f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
51 assert np.sum(spectrum.data) * parameters.CCD_REBIN**2 > 2e-11 / parameters.CCD_REBIN
52 if parameters.CCD_REBIN == 1:
53 if parameters.SPECTRACTOR_DECONVOLUTION_PSF2D or parameters.SPECTRACTOR_DECONVOLUTION_FFM:
54 assert np.isclose(spectrum.lambdas[0], 343, atol=1)
55 assert np.isclose(spectrum.lambdas[-1], 1084.0, atol=1)
56 else:
57 assert np.isclose(spectrum.lambdas[0], 347, atol=1)
58 assert np.isclose(spectrum.lambdas[-1], 1085.0, atol=1)
59 assert np.isclose(spectrum.spectrogram_x0, -280, atol=1)
60 assert np.isclose(spectrum.x0[0] * parameters.CCD_REBIN, 743.6651370068676, atol=0.5 * parameters.CCD_REBIN)
61 assert np.isclose(spectrum.x0[1] * parameters.CCD_REBIN, 683.0577836601408, atol=1 * parameters.CCD_REBIN)
62 assert 2 < np.mean(spectrum.chromatic_psf.table['gamma']) * parameters.CCD_REBIN < 3.5
63 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrum.fits'))) is True
64 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrogram.fits'))) is True
65 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_lines.csv'))) is True
68# TODO: DM-33441 Fix broken spectractor tests
69@unittest.skip('Skipping due broken test (TypeError: argument of type bool is not iterable)')
70def test_extractor_ctio_planetary_nebula():
71 file_names = ['tests/data/reduc_20170605_028.fits']
72 output_directory = "./outputs"
74 logbook = LogBook(logbook='./ctiofulllogbook_jun2017_v5.csv')
75 load_config("./config/ctio.ini")
76 parameters.VERBOSE = True
77 parameters.DEBUG = True
78 parameters.CCD_REBIN = 1 # do not work with other values
79 parameters.LAMBDA_MIN = 450
80 parameters.LAMBDA_MAX = 1000
82 for file_name in file_names:
83 tag = file_name.split('/')[-1]
84 disperser_label, target_label, xpos, ypos = logbook.search_for_image(tag)
85 if target_label is None or xpos is None or ypos is None:
86 continue
87 spectrum = Spectractor(file_name, output_directory, target_label, [xpos, ypos], disperser_label,
88 atmospheric_lines=True)
89 assert spectrum.data is not None
90 spectrum.my_logger.warning(f"\n\tQuantities to test:"
91 f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
92 f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
93 f"\n\t\tspectrum.x0={spectrum.x0}"
94 f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
95 f"\n\t\tspectrum total flux={np.sum(spectrum.data) * parameters.CCD_REBIN ** 2}"
96 f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
97 f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
98 if parameters.SPECTRACTOR_DECONVOLUTION_PSF2D or parameters.SPECTRACTOR_DECONVOLUTION_FFM:
99 assert np.isclose(spectrum.lambdas[0], 449, atol=1)
100 assert np.isclose(spectrum.lambdas[-1], 996.5, atol=1)
101 else:
102 assert np.isclose(spectrum.lambdas[0], 443, atol=1)
103 assert np.isclose(spectrum.lambdas[-1], 981, atol=1)
104 assert np.isclose(spectrum.spectrogram_x0, -368, atol=1)
105 assert np.sum(spectrum.data) * parameters.CCD_REBIN ** 2 > 1e-11 / parameters.CCD_REBIN
106 assert np.isclose(spectrum.x0[0] * parameters.CCD_REBIN, 816.75, atol=0.5 * parameters.CCD_REBIN)
107 assert np.isclose(spectrum.x0[1] * parameters.CCD_REBIN, 587.67, atol=1 * parameters.CCD_REBIN)
108 assert 1 < np.mean(spectrum.chromatic_psf.table['gamma']) * parameters.CCD_REBIN < 2.5
109 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrum.fits'))) is True
110 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrogram.fits'))) is True
111 assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_lines.csv'))) is True
114def extractor_auxtel():
115 file_names = ['tests/data/calexp_2020031500162-EMPTY_ronchi90lpmm-det000.fits'] # image 1
116 # file_names = ['tests/data/calexp_2020031200313-EMPTY_ronchi90lpmm-det000.fits'] # image 2
117 # file_names = ['tests/data/calexp_2020022100767-EMPTY_ronchi90lpmm-det000.fits'] # image 3
118 # file_names = ['tests/data//calexp_2020021800154-EMPTY_ronchi90lpmm-det000.fits'] # image 4
119 file_names = ['tests/data/Cor_holo4_003_empty_HD60753_2021-02-18_585.fits'] # ronchi170lpmm
120 # tests/data/auxtel_first_light-1.fits']
122 # logbook = LogBook(logbook='./ctiofulllogbook_jun2017_v5.csv')
123 parameters.VERBOSE = True
124 parameters.DEBUG = True
125 #xpos = 1600
126 #ypos = 2293
127 #target_label = "HD107696"
129 for config in ['./config/auxtel.ini']: #'./config/auxtel_quicklook.ini',
130 for file_name in file_names:
131 # tag = file_name.split('/')[-1]
132 # disperser_label, target, xpos, ypos = logbook.search_for_image(tag)
133 #spectrum = Spectractor(file_name, './outputs/', target_label=target_label, guess=[xpos, ypos],
134 # config=config, atmospheric_lines=True)
135 spectrum = Spectractor(file_name, './outputs/', target_label="", guess=None,
136 config=config, atmospheric_lines=True)
137 assert spectrum.data is not None
138 assert np.sum(spectrum.data) > 1e-14
139 # spectrum.my_logger.warning(f"\n\tQuantities to test:"
140 # f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
141 # f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
142 # f"\n\t\tspectrum.x0={spectrum.x0}"
143 # f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
144 # f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
145 # f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
146 # assert np.isclose(spectrum.lambdas[0], 296, atol=1)
147 # assert np.isclose(spectrum.lambdas[-1], 1083.5, atol=1)
148 # assert np.isclose(spectrum.x0[0], 743.6651370068676, atol=0.5)
149 # assert np.isclose(spectrum.x0[1], 683.0577836601408, atol=1)
150 # assert np.isclose(spectrum.spectrogram_x0, -240, atol=1)
151 # assert 2 < np.mean(spectrum.chromatic_psf.table['gamma']) < 3
152 # assert os.path.isfile('./outputs/' + tag.replace('.fits', '_spectrum.fits')) is True
153 # assert os.path.isfile('./outputs/' + tag.replace('.fits', '_spectrogram.fits')) is True
156if __name__ == "__main__": 156 ↛ 158line 156 didn't jump to line 158, because the condition on line 156 was never true
158 run_module_suite()