Repository navigation
Expand file tree
/
Copy pathpoolander.py
More file actions
338 lines (271 loc) · 9.97 KB
/
Copy pathpoolander.py
File metadata and controls
338 lines (271 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""Find matching JWST associations pool file.
Examples
--------
>>> from poolander import do_match, sneakpeek
>>> filename_1 = '/path/to/pool_002_image_miri.csv'
>>> patt = '/another/path/to/jw*.csv'
>>> score, details = do_match(filename_1, candidates_patt=patt)
This took 10.0 seconds.
>>> score.most_common(5)
[('/another/path/to/jw05204_20250308t202944_pool.csv', 6560),
('/another/path/to/jw06123_20250316t014216_pool.csv', 6554),
('/another/path/to/jw05842_20250316t011943_pool.csv', 6552),
('/another/path/to/jw04093_20250316t063820_pool.csv', 6540),
('/another/path/to/jw01052_20250316t101635_pool.csv', 6533)]
>>> len(score)
771
>>> details['/another/path/to/jw05204_20250308t202944_pool.csv']
{nrows': (8, 28),
'ACT_ID': ([1, 2, 3, 4, 6], ['01', '03', '05', '07', '09', '0B', '0D']),
'APERNAME': (['MIRIM_FULL_ILLCNTR'], ['MIRIM_FULL']),
'ASN_CANDIDATE': (["@!FMT_CAND([(OBSNUM.VALUE, 'OBSERVATION')])"],
["[('O001', 'OBSERVATION'), ('C1000', 'GROUP'), ('C1001', 'GROUP')]"]),
'BAND': (['NULL'], ['NULL']),
'CHANNEL': (['NULL'], ['NULL']),
'DETECTOR': (['MIRIMAGE'], ['MIRIMAGE']),
'DITHERID': (['NULL'], [1]), ...,
}
>>> sneakpeek(score, details, 'nrows', most_common=5)
{'original': 8,
'jw05204_20250308t202944_pool.csv': 28,
'jw06123_20250316t014216_pool.csv': 36,
'jw05842_20250316t011943_pool.csv': 36,
'jw04093_20250316t063820_pool.csv': 48,
'jw01052_20250316t101635_pool.csv': 57}
"""
import os
import re
import time
from collections import Counter
from glob import iglob
import numpy as np
from astropy.table import Table
__all__ = ["do_match", "match_criteria", "sneakpeek", "apply_filters"]
def do_match(fn_old, candidates_patt="jw*.csv", match_type="exact",
max_nrows=500, verbose=True, debug=False):
"""Given pool file to replace from candidates, find best match.
Parameters
----------
fn_old : str
Filename of the CSV to be replaced.
Provide full path if not in working directory.
candidates_patt : str
Search pattern as accepted by :py:func:`glob.iglob`,
or if a single ``.txt`` file is provided, it is assumed
to contain a list of CSV files to match against.
Provide full path is not in working directory.
match_type : {'exact', 'subset'}
Type of matching to be done for values found in shared columns:
* ``'exact'``: Values must be the same exactly.
* ``'subset'``: Replacement table may contain other stuff
as long as it also contains all old values.
max_nrows : int
Skip candidates with more than this number of rows to avoid
choosing a large program.
verbose : bool
Print informational text.
debug : bool
Print debugging text.
Returns
-------
d_scores : :py:class:`~collections.Counter`
Ranked matches (filenames with scores).
d_details : dict
Maps each possible replacement filename to full comparison
details by common column names.
"""
t_old = Table.read(fn_old, delimiter="|", format="ascii")
d_scores = Counter()
d_details = {}
# Force uppercase column names to ensure match with inflight data.
t_old.rename_columns(
t_old.colnames, list(map(str.upper, t_old.colnames)))
# Known irrelevant pools or calibration programs to ignore.
is_cal_old, progs_to_ignore = _get_progs_to_ignore(
t_old["EXP_TYPE"], verbose=verbose)
if candidates_patt.endswith(".txt") and os.path.isfile(candidates_patt):
with open(candidates_patt) as flist_in:
fn_list = [s.strip() for s in flist_in.readlines()]
else:
fn_list = iglob(candidates_patt)
if verbose:
t_start = time.time()
for fn_cur in fn_list:
if fn_cur == fn_old:
continue
prognum = _get_prog(fn_cur)
if prognum <= 1000 or prognum >= 10000:
if debug:
print(f"Skipping non-flight program {prognum}: {fn_cur}")
continue
ignore_this = False
for bad_prog in progs_to_ignore:
if bad_prog in fn_cur:
ignore_this = True
if debug:
print(f"Skipping bad program {bad_prog}: {fn_cur}")
break
if ignore_this:
continue
t_cur = Table.read(fn_cur, delimiter="|", format="ascii")
nrows = len(t_cur)
if nrows == 0 or nrows > max_nrows:
if debug:
print(f"Skipping nrows={nrows}: {fn_cur}")
continue
# Force uppercase column names to ensure match with input data.
t_cur.rename_columns(
t_cur.colnames, list(map(str.upper, t_cur.colnames)))
is_cal_cur = _is_cal(t_cur["EXP_TYPE"])
if is_cal_cur is not is_cal_old:
if debug:
print(f"Skipping {is_cal_cur}!={is_cal_old}: {fn_cur}")
continue
score, details = match_criteria(t_old, t_cur, match_type=match_type)
d_scores[fn_cur] = score
d_details[fn_cur] = details
if verbose:
t_end = time.time()
print(f"This took {t_end - t_start:.1f} seconds.")
return d_scores, d_details
def match_criteria(t_old, t_candidate, match_type="exact"):
"""Higher number is better."""
score = len(t_old) - len(t_candidate)
details = {"nrows": (len(t_old), len(t_candidate))}
common_colnames = sorted(set(t_old.colnames) & set(t_candidate.colnames))
asn_cand_old = _unique_asn_cand_types(t_old["ASN_CANDIDATE"])
scoreboard = {
"ASN_CANDIDATE": 1000,
"BAND": 500,
"CHANNEL": 500,
"DETECTOR": 1000,
"EXP_TYPE": 1500,
"FILTER": 500,
"FXD_SLIT": 500,
"GRATING": 500,
"INSTRUME": 1000,
"PATTTYPE": 500,
"PUPIL": 500,
"SPAT_NUM": 100,
"SPEC_NUM": 100,
"SUBARRAY": 100,
"TEMPLATE": 1000,
"TSOVISIT": 1000,
}
no_match_penalty = {
"DETECTOR": 1000,
"EXP_TYPE": 1000,
"INSTRUME": 1000,
"TEMPLATE": 1000,
"TSOVISIT": 1000,
}
for colname in common_colnames:
# Not useful and clutter output.
if colname in ("FILENAME", "OBS_ID", "VISIT_ID"):
continue
if colname == "ASN_CANDIDATE":
s1 = asn_cand_old
s2 = _unique_asn_cand_types(t_candidate[colname])
else:
if t_old[colname].dtype.type is np.str_:
c_old = list(map(str.upper, t_old[colname]))
else:
c_old = t_old[colname].tolist()
if t_candidate[colname].dtype.type is np.str_:
c_cur = list(map(str.upper, t_candidate[colname]))
else:
c_cur = t_candidate[colname].tolist()
s1 = set(c_old)
s2 = set(c_cur)
if ((match_type == "exact" and s1 == s2) or
(match_type == "subset" and s1 <= s2)):
score += scoreboard.get(colname, 1)
else:
score -= no_match_penalty.get(colname, 1)
details[colname] = (sorted(s1), sorted(s2))
return score, details
def sneakpeek(score, details, key, most_common=5):
"""Quick details inspection of given key."""
d = {}
for match_tuple in score.most_common(most_common):
fn = match_tuple[0]
detail = details[fn][key]
if "original" not in d:
d["original"] = detail[0]
d[os.path.basename(fn)] = detail[1]
return d
def apply_filters(score, details, filters):
"""Filters are a list of ``(key, val)``
where ``val`` is in one of the values.
Examples
--------
>>> apply_filters(score, details, [
... ('ASN_CANDIDATE', 'BACKGROUND'),
... ('APERNAME', 'MIRIM_FULL_SLITCNTR'),
... ('EXP_TYPE', 'MIR_LRS-FIXEDSLIT')])
"""
matches = []
for match_tuple in score.most_common():
scoreboard = []
fn = match_tuple[0]
d = details[fn]
for key, val in filters:
status = False
detail = d[key][1]
if key == 'nrows':
if val == detail:
status = True
elif val in detail:
status = True
scoreboard.append(status)
if scoreboard and np.all(scoreboard):
matches.append(fn)
return matches
def _unique_asn_cand_types(t_col):
rpatt_asn_cand = r", '(\w*)'"
output_set = set()
for cell in t_col:
m = re.findall(rpatt_asn_cand, cell)
if m:
output_set |= set(sorted(m))
return output_set
def _is_cal(exptype_col):
# EXP_TYPE for calibration programs.
cal_exptype_by_ins = [
# FGS
"FGS_ACQ1", "FGS_ACQ2", "FGS_FINEGUIDE", "FGS_ID-IMAGE",
"FGS_ID-STACK", "FGS_TRACK",
# MIRI
"MIR_4QPM", "MIR_CORONCAL", "MIR_DARKALL", "MIR_DARKIMG",
"MIR_DARKMRS", "MIR_FLATIMAGE", "MIR_FLATIMAGE-EXT",
"MIR_FLATMRS", "MIR_FLATMRS-EXT",
# NIRCAM
"NRC_DARK", "NRC_FLAT", "NRC_FOCUS", "NRC_LED", "NRC_WFSC",
# NIRISS
"NIS_DARK", "NIS_EXTCAL", "NIS_FOCUS", "NIS_LAMP",
# NIRSPEC
"NRS_AUTOFLAT", "NRS_AUTOWAVE", "NRS_CONFIRM", "NRS_DARK",
"NRS_FOCUS", "NRS_IMAGE", "NRS_LAMP", "NRS_MIMF", "NRS_VERIFY"
]
is_cal = False
for val in exptype_col:
if val.upper() in cal_exptype_by_ins:
is_cal = True
break
return is_cal
def _get_progs_to_ignore(exptype_col, verbose=True):
# Known irrelevant pools or calibration programs to ignore
# for science pools.
progs_to_ignore = ["jw0153", "jw02741", "jw04492", "jw06620"]
is_cal = _is_cal(exptype_col)
if is_cal:
if verbose:
print("Calibration pool; not ignoring any programs."
f"\n{sorted(set(exptype_col.tolist()))}")
return is_cal, [] # Do not ignore any program
if verbose:
print(f"Science pool; ignoring {progs_to_ignore}")
return is_cal, progs_to_ignore # Science!
def _get_prog(fn):
# jwNNNNN_blah_blah.csv
return int(os.path.basename(fn)[2:7])