-
Notifications
You must be signed in to change notification settings - Fork 3
/
candcsvmaker.py
executable file
·192 lines (174 loc) · 5.01 KB
/
candcsvmaker.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
#!/usr/bin/env python3
import glob
import pandas as pd
import os
import tqdm
import logging
import argparse
logger = logging.getLogger(__name__)
def gencandcsv(
candsfiles,
filelist,
snr_th=6,
clustersize_th=2,
dm_min=10,
dm_max=5000,
label=1,
outname=None,
chan_mask=None,
):
if len(candsfiles) == 0:
raise ValueError("No candidate files provided")
if len(filelist) == 0:
raise ValueError("No fits/fil files provided")
filelist.sort()
for files in filelist:
if not os.path.isfile(files):
raise FileNotFoundError(f"{files} not found")
if outname is None:
ext = filelist[0].split(".")[-1]
if ext == "fits" or ext == "sf" or ext == "fil":
outname = os.path.splitext(os.path.basename(filelist[0]))[0]
else:
raise TypeError("Can only work with list of fits file or filterbanks")
if outname[-4:] != ".csv":
outname = outname + ".csv"
cands_out = pd.DataFrame(
columns=[
"file",
"snr",
"stime",
"width",
"dm",
"label",
"chan_mask_path",
"num_files",
]
)
cands_out.to_csv(outname, mode="w", header=True, index=False)
for file in tqdm.tqdm(candsfiles, position=0, leave=True):
cands_out = pd.DataFrame(
columns=[
"file",
"snr",
"stime",
"width",
"dm",
"label",
"chan_mask_path",
"num_files",
]
)
cands = pd.read_csv(
file,
header=None,
comment="#",
delim_whitespace=True,
names=[
"snr",
"ssample",
"stime",
"width",
"dmidx",
"dm",
"cluster_size",
"startsamp",
"endsamp",
],
)
cands_filtered = cands[
(cands["dm"] >= dm_min)
& (cands["dm"] <= dm_max)
& (cands["snr"] >= snr_th)
& (cands["cluster_size"] >= clustersize_th)
]
if len(cands_filtered) == 0:
logger.info(f"No candidate passes the threshold criterion in {file}")
else:
cands_out["dm"] = cands_filtered["dm"]
cands_out["snr"] = cands_filtered["snr"]
cands_out["width"] = cands_filtered["width"]
cands_out["stime"] = cands_filtered["stime"]
cands_out["file"] = os.path.abspath(filelist[0])
cands_out["label"] = label
cands_out["chan_mask_path"] = chan_mask
cands_out["num_files"] = len(filelist)
logger.debug(f"Writing candidates in {file} to {outname}")
cands_out.to_csv(outname, mode="a", header=False, index=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Your heimdall candidate csv maker",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("-v", "--verbose", help="Be verbose", action="store_true")
parser.add_argument(
"-o",
"--fout",
help="Output file directory for candidate csv file",
type=str,
required=False,
default=None,
)
parser.add_argument(
"-f",
"--fin",
help="Input files, can be *fits or *fil or *sf",
nargs="+",
required=True,
)
parser.add_argument(
"-c", "--heim_cands", help="Heimdall cand files", nargs="+", required=True
)
parser.add_argument(
"-k",
"--channel_mask_path",
help="Path of channel flags mask",
required=False,
type=str,
default=None,
)
parser.add_argument(
"-s", "--snr_th", help="SNR Threshold", required=False, type=float, default=6
)
parser.add_argument(
"-dl",
"--dm_min_th",
help="Minimum DM allowed",
required=False,
type=float,
default=10,
)
parser.add_argument(
"-du",
"--dm_max_th",
help="Maximum DM allowed",
required=False,
type=float,
default=5000,
)
parser.add_argument(
"-g",
"--clustersize_th",
help="Minimum cluster size allowed",
required=False,
type=float,
default=2,
)
values = parser.parse_args()
logging_format = (
"%(asctime)s - %(funcName)s -%(name)s - %(levelname)s - %(message)s"
)
if values.verbose:
logging.basicConfig(level=logging.DEBUG, format=logging_format)
else:
logging.basicConfig(level=logging.INFO, format=logging_format)
gencandcsv(
values.heim_cands,
values.fin,
outname=values.fout,
chan_mask=values.channel_mask_path,
snr_th=values.snr_th,
clustersize_th=values.clustersize_th,
dm_min=values.dm_min_th,
dm_max=values.dm_max_th,
)