-
Notifications
You must be signed in to change notification settings - Fork 0
/
phcx.py
302 lines (258 loc) · 11 KB
/
phcx.py
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
import re
import xml.etree.cElementTree as ET
import numpy
################################################################################
def readDataBlock(xmlnode):
""" Turn any 'DataBlock' XML node into a numpy array of floats
"""
vmin = float(xmlnode.get('min'))
vmax = float(xmlnode.get('max'))
string = xmlnode.text
string = re.sub("[\t\s\n]", "", string)
data = numpy.asarray(
bytearray.fromhex(string),
dtype=float
)
return data * (vmax - vmin) / 255. + vmin
class Candidate(object):
def __init__(self, fname):
""" Build a new Candidate object from a PHCX file path.
"""
xmlroot = ET.parse(fname).getroot()
# Read Coordinates
coordNode = xmlroot.find('head').find('Coordinate')
self.rajd = float(coordNode.find('RA').text)
self.decjd = float(coordNode.find('Dec').text)
# Separate PDMP & FFT sections
for section in xmlroot.findall('Section'):
if 'pdmp' in section.get('name').lower():
opt_section = section
else:
fft_section = section
# Best values as returned by PDMP
opt_values = {
node.tag: float(node.text)
for node in opt_section.find('BestValues').getchildren()
}
self.bary_period = opt_values['BaryPeriod']
self.topo_period = opt_values['TopoPeriod']
self.dm = opt_values['Dm']
self.snr = opt_values['Snr']
self.width = opt_values['Width']
##### P-DM plane #####
pdmNode = opt_section.find('SnrBlock')
# DmIndex
string = pdmNode.find('DmIndex').text
# dm_index = numpy.asarray(map(float, string.split()))
dm_index = []
dm_index_str = string.split()
for dm_index_str_element in dm_index_str:
dm_index_element = float(dm_index_str_element)
dm_index.append(dm_index_element)
dm_index = numpy.asarray(dm_index)
# print (dm_index.size)
# PeriodIndex
string = pdmNode.find('PeriodIndex').text
# period_index = numpy.asarray(map(float, string.split()))
period_index_str = string.split()
period_index = []
for period_index_str_element in period_index_str:
period_index_element = float(period_index_str_element)
period_index_element = period_index_element / 1.0e12
period_index.append(period_index_element)
period_index = numpy.asarray(period_index)
# print (period_index.size)
# period_index /= 1.0e12 # Picoseconds to seconds
# S/N data
pdmDataNode = pdmNode.find('DataBlock')
# print (readDataBlock(pdmDataNode).size)
# print (dm_index)
pdm_plane = readDataBlock(pdmDataNode).reshape(
dm_index.size,
period_index.size
)
# Pack all P-DM plane arrays into a tuple
self.pdm_plane = (period_index, dm_index, pdm_plane)
### Sub-Integrations
subintsNode = opt_section.find('SubIntegrations')
nsubs = int(subintsNode.get('nSub'))
nbins = int(subintsNode.get('nBins'))
self.subints = readDataBlock(subintsNode).reshape(nsubs, nbins)
### Sub-Bands
subbandsNode = opt_section.find('SubBands')
nsubs = int(subbandsNode.get('nSub'))
nbins = int(subbandsNode.get('nBins'))
self.subbands = readDataBlock(subbandsNode).reshape(nsubs, nbins)
### Profile
profileNode = opt_section.find('Profile')
self.profile = readDataBlock(profileNode)
##### Parse FFT Section (PEASOUP Data) #####
fft_values = {
node.tag: float(node.text)
for node in fft_section.find('BestValues').getchildren()
}
self.accn = fft_values['Accn']
self.hits = fft_values['Hits']
self.rank = fft_values['Rank']
self.fftsnr = fft_values['SpectralSnr']
### DmCurve: FFT S/N vs. PEASOUP Trial DM, at best candidate acceleration
dmcurve_node = fft_section.find('DmCurve')
text = dmcurve_node.find('DmValues').text
dm_values = numpy.asarray([float(value) for value in text.split()])
# dm_values = numpy.asarray(map(float, text.split()))
text = dmcurve_node.find('SnrValues').text
snr_values = numpy.asarray([float(value) for value in text.split()])
# snr_values = numpy.asarray(map(float, text.split()))
# Pack the DM curve into a tuple of arrays
self.dm_curve = (dm_values, snr_values)
### AccnCurve: FFT S/N vs. PEASOUP Trial Acc, at best candidate DM
accncurve_node = fft_section.find('AccnCurve')
text = accncurve_node.find('AccnValues').text
# accn_values = numpy.asarray(map(float, text.split()))
accn_values = numpy.asarray([float(value) for value in text.split()])
text = accncurve_node.find('SnrValues').text
snr_values = numpy.asarray([float(value) for value in text.split()])
# snr_values = numpy.asarray(map(float, text.split()))
# Pack the Accn curve into a tuple of arrays
self.accn_curve = (accn_values, snr_values)
################################################################################
def phase_plots(cand, desFile):
import pylab as plt
plt.figure(1, figsize=(9, 7), dpi=70)
plt.subplot(311)
plt.imshow(cand.subbands, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.cm.Greys)
plt.title('Sub-Bands')
plt.ylabel('Band Index')
plt.subplot(312)
plt.imshow(cand.subints, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.cm.Greys)
plt.title('Sub-Integrations')
plt.ylabel('Integration Index')
plt.subplot(313)
plt.bar(range(cand.profile.size), cand.profile, width=1)
plt.xlim(0, cand.profile.size)
plt.xlabel('Phase Bin Index')
plt.tight_layout()
# plt.show()
plt.savefig(desFile, format='png')
plt.close()
def bullseye_plot(cand, desFile):
import pylab as plt
p, dm, snr = cand.pdm_plane
plt.figure(2, figsize=(7, 5), dpi=80)
# IMPORTANT NOTE: imshow() must be called with origin='lower' here, otherwise
# the DM values on the Y axis are reversed (and therefore wrong).
plt.imshow(
snr,
extent=[p.min(), p.max(), dm.min(), dm.max()],
aspect='auto',
origin='lower',
interpolation='nearest'
)
plt.xlabel('Period Correction (s)')
plt.ylabel('Trial DM')
cb = plt.colorbar()
cb.set_label('Folded S/N')
plt.tight_layout()
# plt.show()
plt.savefig(desFile, format='png')
plt.close()
################################################################################
if __name__ == '__main__':
import os
import numpy as np
import csv
# Load example.phcx file (must be in the same directory as this python script)
'''
directory, fname = os.path.split(
os.path.abspath(__file__)
)
cand = Candidate(
os.path.join(directory, 'example.phcx')
)
'''
candidate_path_base = 'G:\\暑期集训\\MedlatTrainingData\\'
max = 0
for child_path in os.listdir(candidate_path_base):
if "negatives_" in child_path and "tar.gz" not in child_path:
candidate_path = os.path.join(candidate_path_base, child_path)
for file_path in os.listdir(candidate_path):
path = os.path.join(candidate_path, file_path)
cand = Candidate(path)
attrs = list()
sub_bands = cand.subbands.flatten().tolist()
attrs.extend(sub_bands)
sub_ins = cand.subints.flatten().tolist()
attrs.extend(sub_ins)
# sub_bands.append(1)
profile = cand.profile # 1-d [64, ]
attrs.extend(profile)
dm = cand.dm # a float
attrs.append(dm)
dm_curve = cand.dm_curve # tuple(ndarray, ndarray) (2, 1344)
attrs.extend(dm_curve[0].tolist())
attrs.extend(dm_curve[-1].tolist())
topo = cand.topo_period # a float
attrs.append(topo)
bary = cand.bary_period # a float
attrs.append(bary)
accn = cand.accn # a float
attrs.append(accn)
accn_curve = cand.accn_curve # tuple(ndarray, ndarray) (2, 71)
attrs.extend(accn_curve[0].tolist())
attrs.extend(accn_curve[-1].tolist())
attrs.append(0)
ra = cand.rajd # a float
dec = cand.decjd # a float
attrs.append(ra)
attrs.append(dec)
for i in range(len(attrs)):
name = str(type(attrs[i]))
if 'float' in name or 'int' in name:
continue
else:
print(i, " not a number")
break
num = np.shape(attrs)[0]
if(num > max):
max = num
print(max)
# for path in os.listdir(candidate_path_base):
# path = candidate_path_base + path
# name = path.split('\\')[-1].split('.')[0]
# # path = path.replace('\\', '\\\\')
# cand = Candidate(path)
# attrs = list()
# sub_bands = cand.subbands.flatten().tolist()
# attrs.extend(sub_bands)
# sub_ins = cand.subints.flatten().tolist()
# attrs.extend(sub_ins)
# # sub_bands.append(1)
# profile = cand.profile # 1-d [64, ]
# attrs.extend(profile)
# dm = cand.dm # a float
# attrs.append(dm)
# dm_curve = cand.dm_curve # tuple(ndarray, ndarray) (2, 1344)
# attrs.extend(dm_curve[0].tolist())
# attrs.extend(dm_curve[-1].tolist())
# topo = cand.topo_period # a float
# attrs.append(topo)
# bary = cand.bary_period # a float
# attrs.append(bary)
# accn = cand.accn # a float
# attrs.append(accn)
# accn_curve = cand.accn_curve # tuple(ndarray, ndarray) (2, 71)
# attrs.extend(accn_curve[0].tolist())
# attrs.extend(accn_curve[-1].tolist())
# attrs.append(0)
# ra = cand.rajd # a float
# dec = cand.decjd # a float
# print("Processing: ", name)
# print(np.shape(attrs))
# with open('F:\\PycharmProjects\\DBN-python\\Data\\no_pulsar.csv', 'a', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(attrs)
# Make some cool plots
# phase_plots(cand, 'F:\\PycharmProjects\\DBN-python\\Data\\phase_plot')
# bullseye_plot(cand, 'F:\\PycharmProjects\\DBN-python\\Data\\phase_plot')
# print(cand.subints.shape)
# print(cand.subbands.shape)