-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_fd.py
More file actions
329 lines (275 loc) · 13.1 KB
/
Copy pathparse_fd.py
File metadata and controls
329 lines (275 loc) · 13.1 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
#! /usr/bin/env python
from __future__ import print_function
import pdb
import numpy as np
from astropy.io import fits as pf
from six import string_types
import os
import argparse
"""
Parse the output of fitsdiff and store the results in a dictionary.
If desired, calculate the quantitative differences in the HDU tables
and HDU images (this can take ~hours when there are >100 datasets).
Usage:
From the command line:
python parse_fd.py -l fitsdiff.log --pkl
(by default, it will compare the HDU tables & images)
From ipython (recommended):
import parse_fd
fd_dict, dir1, dir2 = parse_fd.parse_fd(logfile)
fd_dict = parse_fd.diff_tables(fd_dict, dir1, dir2)
fd_dict = parse_fd.diff_images(fd_dict, dir1, dir2)
parse_fd.pickle_diffs(fd_dict, "mydiffs.p")
"""
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
def diff_tables(fd_dict, dir1, dir2):
"""
Calculate quantitative differences in HDU tables.
Arguments:
fd_dict (dict): Dictionary with fitsdiff info.
dir1 (str): Directory 1 of data to compare.
dir2 (str): Directory 2 of data to compare.
Returns:
fd_dict (dcit): Updated dictionary with fitsdiff info including
quantitative table differences.
"""
print("Comparing HDU Tables in fitsdiff log... this may take several minutes")
for filename in fd_dict.keys():
opened = False
for str_ext in fd_dict[filename]:
ext = int(str_ext[-1])
if "Columns" in fd_dict[filename][str_ext]:
if not opened:
file1 = os.path.join(dir1, filename)
file2 = os.path.join(dir2, filename)
hdu1 = pf.open(file1)
hdu2 = pf.open(file2)
hdr1 = hdu1[1].header
opened = True
# If the exposure time is 0, don't continue!
try:
if hdr1["exptime"] == 0.0:
continue
except KeyError:
pass
for column in fd_dict[filename][str_ext]["Columns"].keys():
if len(hdu1[ext].data[column]) != len(hdu2[ext].data[column]):
print("SOMETHING BAD HAPPENED IN DIFF_TABLES")
continue
diff = hdu1[ext].data[column] - hdu2[ext].data[column]
fd_dict[filename][str_ext]["Columns"][column]["MaxDiff"] = max(diff.flatten())
fd_dict[filename][str_ext]["Columns"][column]["MinDiff"] = min(diff.flatten())
fd_dict[filename][str_ext]["Columns"][column]["MedDiff"] = np.median(diff.flatten())
fd_dict[filename][str_ext]["Columns"][column]["MeanDiff"] = np.average(diff.flatten())
percdiff = max(abs((hdu2[ext].data[column].flatten() - hdu1[ext].data[column].flatten())/((hdu2[ext].data[column].flatten() + hdu2[ext].data[column].flatten())/2.)))
fd_dict[filename][str_ext]["Columns"][column]["PercDiff"] = percdiff
fd_dict[filename][str_ext]["Columns"][column]["NumDiff"] = len(np.where(abs(diff.flatten()) > 0.0))
fd_dict[filename][str_ext]["Columns"][column]["Mean1"] = np.average(hdu1[ext].data[column])
fd_dict[filename][str_ext]["Columns"][column]["Mean2"] = np.average(hdu2[ext].data[column])
if opened:
hdu1.close()
hdu2.close()
return fd_dict
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
def diff_images(fd_dict, dir1, dir2):
"""
Calculate quantitative differences in counts or flt images.
Arguments:
fd_dict (dict): Dictionary with fitsdiff info.
dir1 (str): Directory 1 of data to compare.
dir2 (str): Directory 2 of data to compare.
Returns:
fd_dict (dcit): Updated dictionary with fitsdiff info including
quantitative image differences.
"""
print("Comparing HDU images in fitsdiff log... this may take several minutes...")
for filename in fd_dict.keys():
opened = False
for str_ext in fd_dict[filename].keys():
ext = int(str_ext[-1])
if "ImageDiff" in fd_dict[filename][str_ext].keys():
if not opened:
file1 = os.path.join(dir1, filename)
file2 = os.path.join(dir2, filename)
hdu1 = pf.open(file1)
hdu2 = pf.open(file2)
hdr1 = hdu1[1].header
opened = True
if hdr1["exptime"] == 0.0:
continue
if np.shape(hdu1[ext].data) != np.shape(hdu2[ext].data):
print("SOMETHING BAD HAPPENED IN DIFF_TABLES")
continue
diff = hdu1[ext].data - hdu2[ext].data
fd_dict[filename][str_ext]["ImageDiff"]["MaxDiff"] = max(diff.flatten())
fd_dict[filename][str_ext]["ImageDiff"]["MinDiff"] = min(diff.flatten())
fd_dict[filename][str_ext]["ImageDiff"]["MedDiff"] = np.median(diff.flatten())
fd_dict[filename][str_ext]["ImageDiff"]["MeanDiff"] = np.average(diff.flatten())
fd_dict[filename][str_ext]["ImageDiff"]["PercDiff"] = max(abs((hdu2[ext].data.flatten() - hdu1[ext].data.flatten())/((hdu2[ext].data.flatten() + hdu2[ext].data.flatten())/2.)))
fd_dict[filename][str_ext]["ImageDiff"]["NumDiff"] = len(np.where(abs(diff.flatten()) > 0.0))
fd_dict[filename][str_ext]["ImageDiff"]["Mean1"] = np.average(hdu1[ext].data)
fd_dict[filename][str_ext]["ImageDiff"]["Mean2"] = np.average(hdu2[ext].data)
if opened:
hdu1.close()
hdu2.close()
return fd_dict
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
def parse_fd(logfile):
"""
Parse fitsdiff output, returning a dictionary sorted by filename.
Arguments:
logfile (str): Path of text output from fitsdiff.
Returns:
fd_dict (dict): Dictionary with fitsdiff info.
dir1 (str): Directory 1 of data being compared.
dir2 (str): Directory 2 of data being compared.
"""
print("Parsing results of fitsdiff log {} and storing results in dictionary".format(logfile))
filename = "dummy"
fd_dict = {}
lines = open(logfile).read().splitlines()
dir1 = os.path.dirname(lines[2].split()[1])
dir2 = os.path.dirname(lines[3].split()[1])
for i in range(len(lines)-1):
# First of all, determine the filename.
if dir1 in lines[i]:
#if "a: " in lines[i]:
full_filename = lines[i].split()[1]
# If file doesn't exist, it migiht be because it's been zipped.
if not os.path.exists(full_filename):
full_filename += ".gz"
if not os.path.exists(full_filename):
print("WARNING: File does not exist (zipped or unzipped): {0}".format(full_filename))
filename = os.path.basename(full_filename)
continue
# Skip trailer files.
if "trl.fits" in filename:
continue
# Keep track of which FITS extension we're looking at.
if "HDU" in lines[i]:
if "Primary HDU" in lines[i]:
ext = "Ext0"
else:
extnum = lines[i].split()[-1][0]
ext = "Ext" + str(extnum)
if not filename in fd_dict.keys():
fd_dict[filename] = {}
fd_dict[filename][ext] = {}
continue
# Check for keywords that are only present in one file
if "Extra keyword" in lines[i]:
if "Keywords" not in fd_dict[filename][ext].keys():
fd_dict[filename][ext]["Keywords"] = {}
keyword = lines[i].split("'")[1]
extra_file = lines[i].split()[4]
value = lines[i].split(": ")[-1]
if value.startswith("'") and value.endswith("'"):
value = value.split("'")[1]
if extra_file == "a:":
fd_dict[filename][ext]["Keywords"][keyword] = [value, "#NOT PRESENT#"]
else:
fd_dict[filename][ext]["Keywords"][keyword] = ["#NOT PRESENT#", value]
# Check for keywords that differ between the two files.
if "Keyword " in lines[i]:
if "Keywords" not in fd_dict[filename][ext].keys():
fd_dict[filename][ext]["Keywords"] = {}
# If there is only info for one file, e.g.:
# Keyword GLOBRT_A has different comments:
# b> global count rate
# Keyword GLOBRT_B has different comments:
if "comments" in lines[i]:
if "Comments" not in fd_dict[filename][ext].keys():
fd_dict[filename][ext]["Comments"] = {}
if " b>" not in lines[i+2]:
present_file = lines[i].split()[0]
present_comm = lines[i+1].split("> ")[1]
if present_file == "b>":
bval = present_comm
aval = "#NOT PRESENT#"
else:
aval = present_comm
bval = "#NOT PRESENT#"
else:
aval = lines[i+1].split("> ")[1]
bval = lines[i+2].split("> ")[1]
# Given a keyword that differs, loop through lines until you
# find the a and b values. Determine if they are floats or not.
else:
keyword = lines[i].split()[1]
j = 1
while "a>" not in lines[i+j]:
j += 1
else:
try:
aval = float(lines[i+j].split("a> ")[1])
except ValueError:
aval = lines[i+j].split("a> ")[1]
while "b>" not in lines[i+j]:
j += 1
else:
try:
bval = float(lines[i+j].split("b> ")[1])
except ValueError:
bval = lines[i+j].split("b> ")[1]
try:
fd_dict[filename][ext]["Keywords"][keyword] = [aval,bval]
except:
print("ruh roh")
continue
# If Columns differ, make a key for it and it will be quantified
# later in diff_table().
if "Column" in lines[i]:
col = lines[i].split()[1]
if not "Columns" in fd_dict[filename][ext].keys():
fd_dict[filename][ext]["Columns"] = {}
elif col not in fd_dict[filename][ext]["Columns"].keys():
fd_dict[filename][ext]["Columns"][col] = {}
continue
# If images differ, make a key for it and it will be quantified
# later in diff_image().
if "pixels" in lines[i]:
fd_dict[filename][ext]["ImageDiff"] = {}
return fd_dict, dir1, dir2
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
def get_diffs(logfile):
"""
Run workhorse functions.
Arguments:
logfile (str): Path of text output from fitsdiff.
Returns:
fd_dict (dict): Dictionary with fitsdiff info.
dir1 (str): Directory 1 of data being compared.
dir2 (str): Directory 2 of data being compared.
"""
fd_dict, dir1, dir2 = parse_fd(logfile)
fd_dict = diff_tables(fd_dict, dir1, dir2)
fd_dict = diff_images(fd_dict, dir1, dir2)
return fd_dict, dir1, dir2
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
def pickle_diffs(fd_dict, pkl_file=None):
import pickle
import datetime
if pkl_file is None:
now = datetime.datetime.now()
pkl_file = "fd_{}.p".format(now.strftime("%Y%m%d_%M%S"))
with open(pkl_file, "wb") as f:
pickle.dump(fd_dict, f)
print("Wrote pickle file {}".format(pkl_file))
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-l", dest="logfile", default="new_retrieval.log",
help="Name of output ASCII files")
parser.add_argument("--pkl", dest="pkl_data", action="store_true",
default=False, help="Switch to pickle FD results")
args = parser.parse_args()
logfile = args.logfile
fd_dict, dir1, dir2 = get_diffs(logfile)
if args.pkl_data:
pickle_diffs()