LALPulsar  6.1.0.1-b72065a
lalpulsar_combine_crosscorr_toplists.py
Go to the documentation of this file.
1 # script to combine toplists from lalpulsar_crosscorr_v2
2 
3 # Copyright (C) 2014 John Whelan
4 
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 
15 # You should have received a copy of the GNU General Public License
16 # along with with program; see the file COPYING. If not, write to the
17 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA 02110-1301 USA
19 
20 from __future__ import division
21 from argparse import ArgumentParser
22 import numpy as np
23 
24 freq_col = 0
25 tp_col = 1
26 argp_col = 2
27 asini_col = 3
28 ecc_col = 4
29 period_col = 5
30 estSens_col = 6
31 evSquared_col = 7
32 rho_col = 8
33 
34 
36  filename, min_snr=float("inf"), min_cands=-1, dropped_rho=float("-inf")
37 ):
38  data = np.loadtxt(filename)
39  rho = data[:, rho_col]
40  sorted_inds = rho.argsort()[::-1]
41  # If the toplist is longer than the maximum number of candidates,
42  # truncate it and record the highest SNR value we discarded
43  # unless that would bring us below the minimum snr to keep
44  if min_cands > 0 and min_cands < len(sorted_inds):
45  rho_to_drop = rho[sorted_inds[min_cands]]
46  if rho_to_drop > min_snr:
47  # count how many candidates are over the minimum
48  keep_cands = np.sum(rho > min_snr)
49  rho_to_drop = rho[sorted_inds[keep_cands - 1]]
50  else:
51  keep_cands = min_cands
52  if dropped_rho < rho[sorted_inds[keep_cands]]:
53  dropped_rho = rho[sorted_inds[keep_cands]]
54  sorted_inds = sorted_inds[:keep_cands]
55 
56  return data[sorted_inds, :], dropped_rho
57 
58 
59 parser = ArgumentParser()
60 parser.add_argument(
61  "--input-toplist-files",
62  action="store",
63  nargs="+",
64  required=True,
65  help="A space-separated list of toplist files to combine",
66 )
67 parser.add_argument(
68  "--output-toplist-file",
69  action="store",
70  required=True,
71  help="Filename for output toplist",
72 )
73 parser.add_argument(
74  "--min-cands-per-toplist",
75  action="store",
76  type=int,
77  default=-1,
78  help="Keep at least his many candidates from each toplist",
79 )
80 parser.add_argument(
81  "--min-snr-to-keep",
82  action="store",
83  type=float,
84  default=float("inf"),
85  help="Keep all candidates with at least this SNR",
86 )
87 
88 args = parser.parse_args()
89 
90 dropped_rho = float("-inf")
91 
92 outfile = open(args.output_toplist_file, "w")
93 datatuple = tuple()
94 for filename in args.input_toplist_files:
95  (newdata, dropped_rho) = read_and_sort_toplist(
96  filename=filename,
97  min_snr=args.min_snr_to_keep,
98  min_cands=args.min_cands_per_toplist,
99  dropped_rho=dropped_rho,
100  )
101  datatuple += (newdata,)
102 
103 data = np.concatenate(datatuple)
104 sorted_inds = data[:, rho_col].argsort()[::-1]
105 data = data[sorted_inds]
106 
107 for line in data:
108  outfile.write(
109  "%.10f %.10f %.10g %.10f %.10g %.5f %.10g %.10g %.10g\n" % tuple(line)
110  )
111 print("Highest discarded candidate SNR was %f" % dropped_rho)
112 outfile.close()
def read_and_sort_toplist(filename, min_snr=float("inf"), min_cands=-1, dropped_rho=float("-inf"))