Skip to content

Commit 2eabdb5

Browse files
authored
Merge pull request #265 from sot/ruff-pyright
Refresh regression testing and fix ruff / improve variable naming
2 parents 259e785 + dd2940a commit 2eabdb5

13 files changed

+317
-207
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
33
# Ruff version.
4-
rev: v0.3.3
4+
rev: v0.8.0
55
hooks:
66
# Run the linter.
77
- id: ruff

NOTES/NOTES.test

+24-94
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
##########################################################################
2-
# For py.test unit tests and general development
3-
##########################################################################
4-
skadev
5-
export KADI=/proj/sot/ska/data/kadi
6-
export ENG_ARCHIVE=/proj/sot/ska/data/eng_archive
7-
8-
python setup.py install
9-
cd doc # make sure it's picking up the installed version
10-
ipython
11-
>>> import Ska.engarchive
12-
>>> Ska.engarchive.test(args='-k -test_fetch_regr') # normally skip this
13-
14-
##########################################################################
15-
# Build and install docs
16-
##########################################################################
17-
cd doc
18-
make html
19-
cd ..
20-
su -l aca
21-
make install_doc
1+
#################################################################################
2+
# Regression test for primary (HEAD) update_archive.py and fetch.py
3+
#################################################################################
4+
5+
# This script generates a fresh cheta data archive using the current code in the
6+
# repository. It then compares the outputs for a handful of MSIDs to flight archive.
7+
# It should take a few minutes to run.
8+
9+
./regression_test_eng_archive.sh
10+
11+
# Basic sanity test with some plots (probably not useful)
12+
13+
$ env ENG_ARCHIVE=$PWD/test/eng_archive ipython --matplotlib
14+
>>> run test/make_plots.py
15+
16+
#############################################################################
17+
#############################################################################
18+
# Everything below here is likely out of date and/or not useful. #
19+
# It is included strictly for reference just in case. #
20+
#############################################################################
21+
#############################################################################
22+
2223

2324
#############################################################################
2425
# Make baseline (seed) test data using flight Ska.
@@ -44,82 +45,11 @@ tar zcf eng_archive.tar.gz eng_archive
4445
cp eng_archive.tar.gz /proj/sot/ska/data/eng_archive/regr/flight_eng_archive.tar.gz
4546
popd
4647

47-
48-
#################################################################################
49-
######## Regression test for primary (HEAD) update_archive.py and fetch.py
50-
#################################################################################
51-
52-
# First make test data. This sets ENG_ARCHIVE in that window.
53-
54-
[[ source make_test_eng_archive.csh ]]
55-
. make_test_eng_archive.sh # THIS actually works now
56-
57-
# Basic sanity test with some plots
58-
59-
[[ setenv ENG_ARCHIVE $PWD/test/eng_archive ]]
60-
export ENG_ARCHIVE=$PWD/test/eng_archive
61-
62-
test/make_plots.py
63-
64-
# Regression test. Get_regr_vals.py uses the local ../Ska/engarchive/fetch.py
65-
# if --test is given, otherwise uses the installed Ska version. It sets
66-
# ENG_ARCHIVE internally prior to importing fetch.
67-
68-
[[ unsetenv ENG_ARCHIVE ]]
69-
unset ENG_ARCHIVE
70-
71-
cd test
72-
./get_regr_vals.py --start 2012:302 --stop 2012:310
73-
./get_regr_vals.py --start 2012:302 --stop 2012:310 --test
74-
./compare_regr_vals.py
75-
cd ..
76-
[[ setenv ENG_ARCHIVE $PWD/test/eng_archive ]]
77-
export ENG_ARCHIVE=$PWD/test/eng_archive
78-
79-
## 2011-Feb-18: All tests pass for rev 56:5b12e819e1df
80-
## 2012-Jan-21: All tests pass for 0.16dev e359acd5 !!
81-
## 2012-Apr-29: All tests pass for 0.18dev efddf4d
82-
## 2012-Nov-27: All tests pass for 0.21
83-
84-
############################################################################################
85-
######## Regression test for new skare (updated Python, NumPy, tables, etc) in $ska/test/
86-
############################################################################################
87-
88-
skatest
89-
90-
. make_test_eng_archive.sh # THIS actually works now
91-
92-
# Basic sanity test with some plots
93-
test/make_plots.py
94-
95-
pushd test
96-
rm regr_vals.test
97-
./get_regr_vals.py --start 2012:302 --stop 2012:310 --test
98-
popd
99-
100-
101-
############
102-
# Regression test. Get_regr_vals.py uses the local ../Ska/engarchive/fetch.py
103-
# if --test is given, otherwise uses the installed Ska version. It sets
104-
# ENG_ARCHIVE internally prior to importing fetch.
105-
ska
106-
unset ENG_ARCHIVE
107-
pushd test
108-
rm regr_vals.flight
109-
./get_regr_vals.py --start 2012:302 --stop 2012:310
110-
./compare_regr_vals.py
111-
popd
112-
113-
## 2011-Jun-20 All tests pass for devska (skare 0.10)
114-
## 2012-Jun-12 All tests pass in skatest (skare 0.13) + new eng archive ddc63d2
115-
## 2012-Jun-20 All tests pass in skatest skare 0.13-r241-427bb9c + eng_arch ddc63d2
116-
## 2013-Apr-16 All tests pass in skatest skare 0.15-r293--e754375 + eng_arch f5db57c
117-
11848
########################################################################################################
11949
######## Regression test for new skare in /proj/sot/ska
12050
########################################################################################################
12151

122-
# Make baseline (seed) test data using flight Ska.
52+
# Make baseline (seed) test data using flight Ska.
12353

12454
ska
12555

@@ -178,7 +108,7 @@ test/make_plots.py
178108

179109
unsetenv ENG_ARCHIVE
180110
pushd test
181-
./get_regr_vals.py --start 2010:265 --stop 2010:285 --test
111+
./get_regr_vals.py --start 2010:265 --stop 2010:285 --test
182112
./get_regr_vals.py --start 2010:265 --stop 2010:285
183113
./compare_regr_vals.py
184114
popd

cheta/derived/comps.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ def get_msid_attrs(self, tstart: float, tstop: float, msid: str, msid_args: tupl
435435
)
436436
quat = Quat(q=normalize(q))
437437
bads = np.zeros_like(q1, dtype=bool)
438-
for msid in msids:
439-
bads |= dat[msid].bads
438+
for msid_ in msids:
439+
bads |= dat[msid_].bads
440440

441441
out = {"vals": quat, "bads": bads, "times": dat.times, "unit": None}
442442
return out

cheta/tests/test_orbit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ def test_orbital_elements():
3232
"mean_anomaly": 354.971325,
3333
} # 354.9724 in reference (roundoff in example)
3434

35-
for key in expected:
35+
for key in expected: # noqa: PLC0206
3636
assert np.allclose(out[key], expected[key], atol=0.0, rtol=1e-6)

cheta/update_archive.py

+66-23
Original file line numberDiff line numberDiff line change
@@ -920,21 +920,43 @@ def truncate_archive(filetype, date):
920920
logger.verbose(cmd)
921921

922922

923-
def read_archfile(i, f, filetype, row, colnames, archfiles, db):
924-
"""Read filename ``f`` with index ``i`` (position within list of filenames). The
925-
file has type ``filetype`` and will be added to MSID file at row index ``row``.
926-
``colnames`` is the list of column names for the content type (not used here).
923+
def read_archfile(idx_archfile, archfile, filetype, row, colnames, archfiles, db):
924+
"""Read a FITS file which has been retrieved from the CXCDS archive.
925+
926+
Parameters
927+
----------
928+
idx_archfile : int
929+
Index of the archfile in the list of archfiles
930+
archfile : str
931+
Full path of FITS file to read
932+
filetype : dict
933+
Filetype dictionary with keys, level, instrum, content, arc5gl_query, fileglob.
934+
row : int
935+
Row number in the MSID file to start writing data
936+
colnames : list of str
937+
List of column names for the content type
938+
archfiles : numpy structured array
939+
Array of archfiles
940+
941+
Returns
942+
-------
943+
dat : numpy structured array
944+
Data read from the FITS file
945+
archfiles_row : dict
946+
Row of info about this FILE to insert into archfiles table for content type
927947
"""
928948
# Check if filename is already in archfiles. If so then abort further processing.
929-
filename = os.path.basename(f)
949+
filename = os.path.basename(archfile)
930950
if db.fetchall("SELECT filename FROM archfiles WHERE filename=?", (filename,)):
931-
logger.verbose("File %s already in archfiles - unlinking and skipping" % f)
932-
os.unlink(f)
951+
logger.verbose(
952+
"File %s already in archfiles - unlinking and skipping" % archfile
953+
)
954+
os.unlink(archfile)
933955
return None, None
934956

935957
# Read FITS archive file and accumulate data into dats list and header into headers dict
936-
logger.info("Reading (%d / %d) %s" % (i, len(archfiles), filename))
937-
hdus = pyfits.open(f, character_as_bytes=True)
958+
logger.info("Reading (%d / %d) %s" % (idx_archfile, len(archfiles), filename))
959+
hdus = pyfits.open(archfile, character_as_bytes=True)
938960
hdu = hdus[1]
939961

940962
try:
@@ -975,14 +997,33 @@ def read_archfile(i, f, filetype, row, colnames, archfiles, db):
975997
return dat, archfiles_row
976998

977999

978-
def read_derived(i, filename, filetype, row, colnames, archfiles, db):
1000+
def read_derived(idx_archfile, filename, filetype, row, colnames, archfiles, db):
9791001
"""Read derived data using eng_archive and derived computation classes.
980-
``filename`` has format <content>_<index0>_<index1> where <content>
981-
is the content type (e.g. "dp_thermal128"), <index0> is the start index for
982-
the new data and index1 is the end index (using Python slicing convention
983-
index0:index1). Args ``i``, ``filetype``, and ``row`` are as in
984-
read_archive(). ``row`` must equal <index0>. ``colnames`` is the list of
985-
column names for the content type.
1002+
1003+
Parameters
1004+
----------
1005+
idx_archfile : int
1006+
Index of the archfile in the list of archfiles
1007+
filename : str
1008+
File to read with format <content>_<index0>_<index1> where <content> is the
1009+
content type (e.g. "dp_thermal128"), <index0> is the start index for the new
1010+
data and <index1> is the end index (using Python slicing convention
1011+
index0:index1).
1012+
filetype : dict
1013+
Filetype dictionary with keys, level, instrum, content, arc5gl_query, fileglob.
1014+
row : int
1015+
Row number in the MSID file to start writing data (must equal <index0>)
1016+
colnames : list of str
1017+
List of column names for the content type
1018+
archfiles : numpy structured array
1019+
Array of archfiles
1020+
1021+
Returns
1022+
-------
1023+
dat : numpy structured array
1024+
Data read from the FITS file
1025+
archfiles_row : dict
1026+
Row of info about this FILE to insert into archfiles table for content type
9861027
"""
9871028
# Check if filename is already in archfiles. If so then abort further processing.
9881029

@@ -999,20 +1040,20 @@ def read_derived(i, filename, filetype, row, colnames, archfiles, db):
9991040
time_step = mnf_step * cheta.derived.MNF_TIME
10001041
times = time_step * np.arange(index0, index1)
10011042

1002-
logger.info("Reading (%d / %d) %s" % (i, len(archfiles), filename))
1043+
logger.info("Reading (%d / %d) %s" % (idx_archfile, len(archfiles), filename))
10031044
vals = {}
10041045
bads = np.zeros((len(times), len(colnames)), dtype=bool)
1005-
for i, colname in enumerate(colnames):
1046+
for ii, colname in enumerate(colnames):
10061047
if colname == "TIME":
10071048
vals[colname] = times
1008-
bads[:, i] = False
1049+
bads[:, ii] = False
10091050
else:
10101051
dp_class = getattr(cheta.derived, colname.upper())
10111052
dp = dp_class()
10121053
dataset = dp.fetch(times[0] - 1000, times[-1] + 1000)
10131054
ok = (index0 <= dataset.indexes) & (dataset.indexes < index1)
10141055
vals[colname] = dp.calc(dataset)[ok]
1015-
bads[:, i] = dataset.bads[ok]
1056+
bads[:, ii] = dataset.bads[ok]
10161057

10171058
vals["QUALITY"] = bads
10181059
dat = Ska.Numpy.structured_array(vals, list(colnames) + ["QUALITY"])
@@ -1059,9 +1100,11 @@ def update_msid_files(filetype, archfiles):
10591100

10601101
content_is_derived = filetype["instrum"] == "DERIVED"
10611102

1062-
for i, f in enumerate(archfiles):
1103+
for idx_archfile, archfile in enumerate(archfiles):
10631104
get_data = read_derived if content_is_derived else read_archfile
1064-
dat, archfiles_row = get_data(i, f, filetype, row, colnames, archfiles, db)
1105+
dat, archfiles_row = get_data(
1106+
idx_archfile, archfile, filetype, row, colnames, archfiles, db
1107+
)
10651108
if dat is None:
10661109
continue
10671110

@@ -1154,7 +1197,7 @@ def update_msid_files(filetype, archfiles):
11541197
# subsequent relocation into arch_files archive. In the case of a gap
11551198
# where ingest is stopped before all archfiles are processed, this will
11561199
# leave files in a tmp dir.
1157-
archfiles_processed.append(f)
1200+
archfiles_processed.append(archfile)
11581201
if not opt.dry_run:
11591202
db.insert(archfiles_row, "archfiles")
11601203

cheta/update_server_sync.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ def _get_stat_data_from_archive(filename, stat, tstart, tstop, last_row1, logger
446446

447447
# For rarely sampled data like CPE1ENG, delta_rows can end up being larger than the
448448
# table due to the gaps. Therefore clip to the length of the table.
449-
if delta_rows > len(table):
450-
delta_rows = len(table)
449+
delta_rows = min(delta_rows, len(table))
451450

452451
times = (table[-delta_rows:]["index"] + 0.5) * dt
453452

make_test_eng_archive.csh

-42
This file was deleted.

make_test_eng_archive.sh

-34
This file was deleted.

0 commit comments

Comments
 (0)