• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LSSTDESC / Spectractor / 16367188041

18 Jul 2025 09:29AM UTC coverage: 83.006% (+8.7%) from 74.269%
16367188041

Pull #167

github

web-flow
Merge ad4c2a75f into 9db9e1feb
Pull Request #167: Debug

590 of 682 new or added lines in 20 files covered. (86.51%)

18 existing lines in 6 files now uncovered.

7854 of 9462 relevant lines covered (83.01%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.55
/tests/test_extractor.py
1
import matplotlib as mpl
1✔
2
mpl.use('Agg')  # must be run first! But therefore requires noqa E402 on all other imports
1✔
3

4
from spectractor import parameters  # noqa: E402
1✔
5
from spectractor.extractor.extractor import Spectractor  # noqa: E402
1✔
6
from spectractor.logbook import LogBook  # noqa: E402
1✔
7
from spectractor.config import load_config, apply_rebinning_to_parameters  # noqa: E402
1✔
8
import os  # noqa: E402
1✔
9
import sys  # noqa: E402
1✔
10
import numpy as np  # noqa: E402
1✔
11
import unittest  # noqa: E402
1✔
12
import astropy.config  # noqa: E402
1✔
13

14

15
def test_logbook():
1✔
16
    logbook = LogBook('./tests/data/ctiofulllogbook_jun2017_v5.csv')
1✔
17
    # target, xpos, ypos = logbook.search_for_image('reduc_20170529_085.fits')
18
    # assert xpos is None
19
    disperser_label, target, xpos, ypos = logbook.search_for_image('reduc_20170603_020.fits')
1✔
20
    assert target == "PKS1510-089"
1✔
21
    assert xpos == 830
1✔
22
    assert ypos == 590
1✔
23
    # logbook = LogBook('./ctiofulllogbook_jun2017_v5.csv')
24
    # logbook.plot_columns_vs_date(['T', 'seeing', 'W'])
25

26

27
@astropy.config.set_temp_cache(os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "cache"))
1✔
28
def test_extractor_ctio():
1✔
29
    file_names = ['tests/data/reduc_20170530_134.fits']
1✔
30
    output_directory = "./outputs"
1✔
31

32
    logbook = LogBook(logbook='./tests/data/ctiofulllogbook_jun2017_v5.csv')
1✔
33
    parameters.VERBOSE = True
1✔
34
    load_config("ctio.ini")
1✔
35
    parameters.VERBOSE = True
1✔
36
    parameters.DEBUG = False
1✔
37
    parameters.CCD_REBIN = 1  # rebin=1 to build tests/data spectrum
1✔
38
    apply_rebinning_to_parameters()
1✔
39

40
    for file_name in file_names:
1✔
41
        tag = file_name.split('/')[-1].replace("sim", "reduc")
1✔
42
        disperser_label, target_label, xpos, ypos = logbook.search_for_image(tag)
1✔
43
        if target_label is None or xpos is None or ypos is None:
1✔
44
            continue
×
45
        spectrum = Spectractor(file_name, output_directory, target_label, [xpos, ypos], disperser_label, config="")
1✔
46
        assert spectrum.data is not None
1✔
47
        spectrum.my_logger.warning(f"\n\tQuantities to test:"
1✔
48
                                   f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
49
                                   f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
50
                                   f"\n\t\tspectrum.x0={spectrum.x0}"
51
                                   f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
52
                                   f"\n\t\tspectrum total flux={np.sum(spectrum.data) * parameters.CCD_REBIN ** 2}"
53
                                   f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
54
                                   f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
55
        assert np.sum(spectrum.data) * parameters.CCD_REBIN**2 > 2e-11 / parameters.CCD_REBIN
1✔
56
        if parameters.CCD_REBIN == 1:
1✔
57
            if parameters.SPECTRACTOR_DECONVOLUTION_PSF2D or parameters.SPECTRACTOR_DECONVOLUTION_FFM:
1✔
58
                assert np.isclose(spectrum.lambdas[0], 345, atol=1)
1✔
59
                assert np.isclose(spectrum.lambdas[-1], 1083.0, atol=1)
1✔
60
            else:
61
                assert np.isclose(spectrum.lambdas[0], 347, atol=1)
×
62
                assert np.isclose(spectrum.lambdas[-1], 1085.0, atol=1)
×
63
            assert np.isclose(spectrum.spectrogram_x0, -280, atol=1)
1✔
64
        assert np.isclose(spectrum.x0[0] * parameters.CCD_REBIN, 743.6651370068676, atol=0.5 * parameters.CCD_REBIN)
1✔
65
        assert np.isclose(spectrum.x0[1] * parameters.CCD_REBIN, 683.0577836601408, atol=1 * parameters.CCD_REBIN)
1✔
66
        assert 2 < np.mean(spectrum.chromatic_psf.table['gamma']) * parameters.CCD_REBIN < 3.5
1✔
67
        assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrum.fits'))) is True
1✔
68

69

70
# TODO: DM-33441 Fix broken spectractor tests
71
@unittest.skip('Skipping due broken test (TypeError: argument of type bool is not iterable)')
1✔
72
def test_extractor_ctio_planetary_nebula():
1✔
73
    file_names = ['tests/data/reduc_20170605_028.fits']
×
74
    output_directory = "./outputs"
×
75

76
    logbook = LogBook(logbook='./tests/data/ctiofulllogbook_jun2017_v5.csv')
×
NEW
77
    load_config("ctio.ini")
×
78
    parameters.VERBOSE = True
×
79
    parameters.DEBUG = False
×
80
    parameters.CCD_REBIN = 1  # do not work with other values
×
81
    parameters.LAMBDA_MIN = 450
×
82
    parameters.LAMBDA_MAX = 1000
×
83

84
    for file_name in file_names:
×
85
        tag = file_name.split('/')[-1]
×
86
        disperser_label, target_label, xpos, ypos = logbook.search_for_image(tag)
×
87
        if target_label is None or xpos is None or ypos is None:
×
88
            continue
×
89
        spectrum = Spectractor(file_name, output_directory, target_label, [xpos, ypos], disperser_label)
×
90
        assert spectrum.data is not None
×
91
        spectrum.my_logger.warning(f"\n\tQuantities to test:"
×
92
                                   f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
93
                                   f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
94
                                   f"\n\t\tspectrum.x0={spectrum.x0}"
95
                                   f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
96
                                   f"\n\t\tspectrum total flux={np.sum(spectrum.data) * parameters.CCD_REBIN ** 2}"
97
                                   f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
98
                                   f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
99
        if parameters.SPECTRACTOR_DECONVOLUTION_PSF2D or parameters.SPECTRACTOR_DECONVOLUTION_FFM:
×
100
            assert np.isclose(spectrum.lambdas[0], 449, atol=1)
×
101
            assert np.isclose(spectrum.lambdas[-1], 996.5, atol=1)
×
102
        else:
103
            assert np.isclose(spectrum.lambdas[0], 443, atol=1)
×
104
            assert np.isclose(spectrum.lambdas[-1], 981, atol=1)
×
105
        assert np.isclose(spectrum.spectrogram_x0, -368, atol=1)
×
106
        assert np.sum(spectrum.data) * parameters.CCD_REBIN ** 2 > 1e-11 / parameters.CCD_REBIN
×
107
        assert np.isclose(spectrum.x0[0] * parameters.CCD_REBIN, 816.75, atol=0.5 * parameters.CCD_REBIN)
×
108
        assert np.isclose(spectrum.x0[1] * parameters.CCD_REBIN, 587.67, atol=1 * parameters.CCD_REBIN)
×
109
        assert 1 < np.mean(spectrum.chromatic_psf.table['gamma']) * parameters.CCD_REBIN < 2.5
×
110
        assert os.path.isfile(os.path.join(output_directory, tag.replace('.fits', '_spectrum.fits'))) is True
×
111

112

113
def extractor_auxtel():
1✔
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
    file_names = ['tests/data/Cor_holo4_003_empty_HD60753_2021-02-18_585.fits']  # ronchi170lpmm
×
119
    # tests/data/auxtel_first_light-1.fits']
120

121
    # logbook = LogBook(logbook='./ctiofulllogbook_jun2017_v5.csv')
122
    parameters.VERBOSE = True
×
123
    parameters.DEBUG = True
×
124
    #xpos = 1600
125
    #ypos = 2293
126
    #target_label = "HD107696"
127

128
    for config in ['./config/auxtel.ini']:  #'./config/auxtel_quicklook.ini',
×
129
        for file_name in file_names:
×
130
            # tag = file_name.split('/')[-1]
131
            # disperser_label, target, xpos, ypos = logbook.search_for_image(tag)
132
            #spectrum = Spectractor(file_name, './outputs/', target_label=target_label, guess=[xpos, ypos],
133
            #                       config=config)
134
            spectrum = Spectractor(file_name, './outputs/', target_label="", guess=None, config=config)
×
135
            assert spectrum.data is not None
×
136
            assert np.sum(spectrum.data) > 1e-14
×
137
            # spectrum.my_logger.warning(f"\n\tQuantities to test:"
138
            #                            f"\n\t\tspectrum.lambdas[0]={spectrum.lambdas[0]}"
139
            #                            f"\n\t\tspectrum.lambdas[-1]={spectrum.lambdas[-1]}"
140
            #                            f"\n\t\tspectrum.x0={spectrum.x0}"
141
            #                            f"\n\t\tspectrum.spectrogram_x0={spectrum.spectrogram_x0}"
142
            #                            f"\n\t\tnp.mean(spectrum.chromatic_psf.table['gamma']="
143
            #                            f"{np.mean(spectrum.chromatic_psf.table['gamma'])}")
144
            # assert np.isclose(spectrum.lambdas[0], 296, atol=1)
145
            # assert np.isclose(spectrum.lambdas[-1], 1083.5, atol=1)
146
            # assert np.isclose(spectrum.x0[0], 743.6651370068676, atol=0.5)
147
            # assert np.isclose(spectrum.x0[1], 683.0577836601408, atol=1)
148
            # assert np.isclose(spectrum.spectrogram_x0, -240, atol=1)
149
            # assert 2 < np.mean(spectrum.chromatic_psf.table['gamma']) < 3
150
            # assert os.path.isfile('./outputs/' + tag.replace('.fits', '_spectrum.fits')) is True
151
            # assert os.path.isfile('./outputs/' + tag.replace('.fits', '_spectrogram.fits')) is True
152

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc