LALInference  4.1.6.1-fe68b98
lalinference_multi_pipe.py
Go to the documentation of this file.
1 # DAG generation code for running LALInference pipeline
2 # (C) 2012 John Veitch
3 # 2013 Salvatore Vitale: extended to work with several ini files
4 
5 from lalinference import lalinference_pipe_utils as pipe_utils
6 from six.moves import configparser
7 from optparse import OptionParser
8 import sys
9 
10 usage=""" %prog [options] config1.ini config2.ini ... configN.ini
11 Setup a Condor DAG file to run the LALInference pipeline based on
12 N config.ini files.
13 
14 The user must specify either an injection file to analyse, with the --inj option,
15 a list of SnglInspiralTable or CoincInspiralTable triggers with the --<x>-triggers options,
16 or an ASCII list of GPS times with the --gps-time-file option.
17 
18 The user must also specify and ini file which will contain the main analysis config.
19 
20 """
21 
22 import os
23 def vararg_callback(option, opt_str, value, parser):
24  assert value is None
25  value = []
26  def floatable(str):
27  try:
28  float(str)
29  return True
30  except ValueError:
31  return False
32  for arg in parser.rargs:
33  # stop on --foo like options
34  if arg[:2] == "--" and len(arg) > 2:
35  break
36  # stop on -a, but not on -3 or -3.0
37  if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
38  break
39  value.append(arg)
40  del parser.rargs[:len(value)]
41  setattr(parser.values, option.dest, value)
42 
43 parser=OptionParser(usage)
44 parser.add_option("-r","--run-path",default=None,action="store",type="string",help="Directory to run pipeline in (default: $PWD)",metavar="RUNDIR")
45 parser.add_option("-p","--daglog-path",default=None,action="store",type="string",help="Path to directory to contain DAG log file. SHOULD BE LOCAL TO SUBMIT NODE",metavar="LOGDIR")
46 parser.add_option("-g","--gps-time-file",action="store",type="string",default=None,help="Text file containing list of GPS times to analyse",metavar="TIMES.txt")
47 parser.add_option("-t","--single-triggers",action="store",type="string",default=None,help="SnglInspiralTable trigger list",metavar="SNGL_FILE.xml")
48 parser.add_option("-C","--coinc-triggers",action="store",type="string",default=None,help="CoinInspiralTable trigger list",metavar="COINC_FILE.xml")
49 parser.add_option("-L","--lvalert",action="store",type="string",default=None,help="LVAlert coinc file",metavar="coinc_G0000.xml")
50 parser.add_option("--gid",action="store",type="string",default=None,help="Optional GraceDB ID for submitting results")
51 parser.add_option("-I","--injections",action="store",type="string",default=None,help="List of injections to perform and analyse",metavar="INJFILE.xml")
52 parser.add_option("-P","--pipedown-db",action="store",type="string",default=None,help="Pipedown database to read and analyse",metavar="pipedown.sqlite")
53 parser.add_option("-F","--folder-names",dest="fnames",action="callback", callback=vararg_callback,help="Space separated list of folders that will be created, corresponding to the TIGER parameters that are being tested or GR. The order has to be the same used with the ini files!",default=None,metavar="GR phi1")
54 parser.add_option("--condor-submit",action="store_true",default=False,help="Automatically submit the condor dag")
55 
56 (opts,args)=parser.parse_args()
57 
58 if len(args)>1:
59  print('Using %s ini files\n'%len(args))
60 elif len(args)==1:
61  inifile=args[0]
62 
63 inits=args
64 ninits=len(inits)
65 fnames=opts.fnames
66 nfnames=len(fnames)
67 
68 if not ninits==nfnames:
69  print("You seem to be using %d parser files and %d foldernames. These two numbers must be the same. Exiting...\n"%(ninits,nfnames))
70  sys.exit(1)
71 
72 fnames_dic={}
73 for fname in fnames:
74  fnames_dic[fnames.index(fname)]=str(fname)
75 
76 glob_hyp=fnames
77 hyp_str=" "
78 
79 for hy in glob_hyp:
80  hyp_str+=hy+" "
81 
82 cp=configparser.ConfigParser()
83 cp.optionxform = str
84 
85 first_dag=True
86 common_path=opts.run_path
87 
88 for inifile in inits:
89  try:
90  cp.read_file(open(inifile))
91  except AttributeError:
92  cp.readfp(open(inifile))
93  if opts.run_path is not None:
94  cp.set('paths','basedir',opts.run_path)
95  if opts.daglog_path is not None:
96  cp.set('paths','daglogdir',opts.daglog_path)
97  else:
98  cp.set('paths','daglogdir',opts.run_path)
99  if opts.gps_time_file is not None:
100  cp.set('input','gps-time-file',opts.gps_time_file)
101  if opts.single_triggers is not None:
102  cp.set('input','sngl-inspiral-file',opts.single_triggers)
103  if opts.injections is not None:
104  cp.set('input','injection-file',opts.injections)
105  if opts.coinc_triggers is not None:
106  cp.set('input','coinc-inspiral-file',opts.coinc_triggers)
107  if opts.lvalert is not None:
108  cp.set('input','lvalert-file',opts.lvalert)
109  if opts.gid is not None:
110  cp.set('input','gid',opts.gid)
111  if opts.pipedown_db is not None:
112  cp.set('input','pipedown-db',opts.pipedown_db)
113 
114  if opts.run_path is not None:
115  cp.set('paths','basedir',os.path.join(str(common_path),str(fnames_dic[inits.index(inifile)])))
116  # Create the DAG from the configparser object
117  if first_dag:
118  dag=pipe_utils.LALInferencePipelineDAG(cp)
119  first_dag=False
120  dag.write_sub_files()
121  dag2=dag
122  else:
123  dag2=pipe_utils.LALInferencePipelineDAG(cp,first_dag=False,previous_dag=dag)
124  dag2.write_sub_files()
125  dag=dag2
126 
127 # Create the DAG from the configparser object
128 dag2.set_dag_file(os.path.join(common_path,'common_dag'))
129 dag2.write_dag()
130 dag2.write_script()
131 # End of program
132 print('Successfully created DAG file.')
133 print('Now run condor_submit_dag %s\n'%(dag2.get_dag_file()))
134 
135 if opts.condor_submit:
136  import subprocess
137 
138  x = subprocess.Popen(['condor_submit_dag',dag.get_dag_file()])
139  x.wait()
140  if x.returncode==0:
141  print('Submitted DAG file')
142  else:
143  print('Unable to submit DAG file')
def vararg_callback(option, opt_str, value, parser)