Пример #1
0
def setup_roq(cp):
    """
    Generates cp objects with the different ROQs applied
    """
    use_roq = False
    if cp.has_option('paths', 'roq_b_matrix_directory') or cp.has_option(
            'paths', 'computeroqweights'):
        if not cp.has_option('analysis', 'roq'):
            print(
                "Warning: If you are attempting to enable ROQ by specifying roq_b_matrix_directory or computeroqweights,\n\
            please use analysis.roq in your config file in future. Enabling ROQ."
            )
            cp.set('analysis', 'roq', True)
    if not cp.getboolean('analysis', 'roq'):
        yield cp
        raise StopIteration()
    from numpy import genfromtxt, array
    path = cp.get('paths', 'roq_b_matrix_directory')
    if not os.path.isdir(path):
        print("The ROQ directory %s does not seem to exist\n" % path)
        sys.exit(1)
    use_roq = True
    roq_paths = os.listdir(path)
    roq_params = {}
    roq_force_flow = None

    if cp.has_option('lalinference', 'roq_force_flow'):
        roq_force_flow = cp.getfloat('lalinference', 'roq_force_flow')
        print("WARNING: Forcing the f_low to ", str(roq_force_flow), "Hz")
        print(
            "WARNING: Overwriting user choice of flow, srate, seglen, and (mc_min, mc_max and q-min) or (mass1_min, mass1_max, mass2_min, mass2_max)"
        )

    def key(item):  # to order the ROQ bases
        return float(item[1]['seglen'])

    coinc_xml_obj = None
    row = None

    # Get file object of coinc.xml
    if opts.gid is not None:
        from ligo.gracedb.rest import GraceDb
        gid = opts.gid
        cwd = os.getcwd()
        if cp.has_option('analysis', 'service-url'):
            client = GraceDb(cp.get('analysis', 'service-url'))
        else:
            client = GraceDb()
        coinc_xml_obj = ligolw_utils.load_fileobj(
            client.files(gid, "coinc.xml"),
            contenthandler=lsctables.use_in(ligolw.LIGOLWContentHandler))[0]
    elif cp.has_option('input', 'coinc-xml'):
        coinc_xml_obj = ligolw_utils.load_fileobj(
            open(cp.get('input', 'coinc-xml'), "rb"),
            contenthandler=lsctables.use_in(ligolw.LIGOLWContentHandler))[0]

    # Get sim_inspiral from injection file
    if cp.has_option('input', 'injection-file'):
        print(
            "Only 0-th event in the XML table will be considered while running with ROQ\n"
        )
        row = lsctables.SimInspiralTable.get_table(
            ligolw_utils.load_filename(cp.get('input', 'injection-file'),
                                       contenthandler=lsctables.use_in(
                                           ligolw.LIGOLWContentHandler)))[0]

    roq_bounds = pipe_utils.Query_ROQ_Bounds_Type(path, roq_paths)
    if roq_bounds == 'chirp_mass_q':
        print('ROQ has bounds in chirp mass and mass-ratio')
        mc_priors, trigger_mchirp = pipe_utils.get_roq_mchirp_priors(
            path,
            roq_paths,
            roq_params,
            key,
            coinc_xml_obj=coinc_xml_obj,
            sim_inspiral=row)
    elif roq_bounds == 'component_mass':
        print('ROQ has bounds in component masses')
        # get component mass bounds, then compute the chirp mass that can be safely covered
        # further below we pass along the component mass bounds to the sampler, not the tighter chirp-mass, q bounds
        m1_priors, m2_priors, trigger_mchirp = pipe_utils.get_roq_component_mass_priors(
            path,
            roq_paths,
            roq_params,
            key,
            coinc_xml_obj=coinc_xml_obj,
            sim_inspiral=row)
        mc_priors = {}
        for (roq, m1_prior), (roq2, m2_prior) in zip(m1_priors.items(),
                                                     m2_priors.items()):
            mc_priors[roq] = sorted([
                pipe_utils.mchirp_from_components(m1_prior[1], m2_prior[0]),
                pipe_utils.mchirp_from_components(m1_prior[0], m2_prior[1])
            ])

    if cp.has_option('lalinference', 'trigger_mchirp'):
        trigger_mchirp = float(cp.get('lalinference', 'trigger_mchirp'))
    roq_mass_freq_scale_factor = pipe_utils.get_roq_mass_freq_scale_factor(
        mc_priors, trigger_mchirp, roq_force_flow)
    if roq_mass_freq_scale_factor != 1.:
        print(
            'WARNING: Rescaling ROQ basis, please ensure it is allowed with the model used.'
        )

    # If the true chirp mass is unknown, add variations over the mass bins
    if opts.gid is not None or (opts.injections is not None or cp.has_option(
            'input', 'injection-file')) or cp.has_option(
                'lalinference', 'trigger_mchirp') or cp.has_option(
                    'input', 'coinc-xml'):

        for mc_prior in mc_priors:
            mc_priors[mc_prior] = array(mc_priors[mc_prior])
        # find mass bin containing the trigger
        trigger_bin = None
        for roq in roq_paths:
            if mc_priors[roq][
                    0] * roq_mass_freq_scale_factor <= trigger_mchirp <= mc_priors[
                        roq][1] * roq_mass_freq_scale_factor:
                trigger_bin = roq
                print('Prior in Mchirp will be [' +
                      str(mc_priors[roq][0] * roq_mass_freq_scale_factor) +
                      ',' +
                      str(mc_priors[roq][1] * roq_mass_freq_scale_factor) +
                      '] to contain the trigger Mchirp ' + str(trigger_mchirp))
                break
        roq_paths = [trigger_bin]
    else:
        for mc_prior in mc_priors:
            mc_priors[mc_prior] = array(
                mc_priors[mc_prior]) * roq_mass_freq_scale_factor

    # write the master configparser
    cur_basedir = cp.get('paths', 'basedir')
    masterpath = os.path.join(cur_basedir, 'config.ini')
    with open(masterpath, 'w') as cpfile:
        cp.write(cpfile)

    for roq in roq_paths:
        this_cp = configparser.ConfigParser()
        this_cp.optionxform = str
        this_cp.read(masterpath)
        basedir = this_cp.get('paths', 'basedir')
        for dirs in 'basedir', 'daglogdir', 'webdir':
            val = this_cp.get('paths', dirs)
            newval = os.path.join(val, roq)
            mkdirs(newval)
            this_cp.set('paths', dirs, newval)
        this_cp.set(
            'paths', 'roq_b_matrix_directory',
            os.path.join(cp.get('paths', 'roq_b_matrix_directory'), roq))
        flow = roq_params[roq]['flow'] / roq_mass_freq_scale_factor
        srate = 2. * roq_params[roq]['fhigh'] / roq_mass_freq_scale_factor
        #if srate > 8192:
        #    srate = 8192

        seglen = roq_params[roq]['seglen'] * roq_mass_freq_scale_factor
        # params.dat uses the convention q>1 so our q_min is the inverse of their qmax
        this_cp.set('engine', 'srate', str(srate))
        this_cp.set('engine', 'seglen', str(seglen))
        if this_cp.has_option('lalinference', 'flow'):
            tmp = this_cp.get('lalinference', 'flow')
            tmp = eval(tmp)
            ifos = tmp.keys()
        else:
            tmp = {}
            ifos = eval(this_cp.get('analysis', 'ifos'))
        for i in ifos:
            tmp[i] = flow
            this_cp.set('lalinference', 'flow', str(tmp))
        if roq_bounds == 'chirp_mass_q':
            mc_min = mc_priors[roq][0] * roq_mass_freq_scale_factor
            mc_max = mc_priors[roq][1] * roq_mass_freq_scale_factor
            # params.dat uses the convention q>1 so our q_min is the inverse of their qmax
            q_min = 1. / float(roq_params[roq]['qmax'])
            this_cp.set('engine', 'chirpmass-min', str(mc_min))
            this_cp.set('engine', 'chirpmass-max', str(mc_max))
            this_cp.set('engine', 'q-min', str(q_min))
            this_cp.set(
                'engine', 'comp-min',
                str(
                    max(
                        roq_params[roq]['compmin'] *
                        roq_mass_freq_scale_factor,
                        mc_min * pow(1 + q_min, 1. / 5.) *
                        pow(q_min, 2. / 5.))))
            this_cp.set(
                'engine', 'comp-max',
                str(mc_max * pow(1 + q_min, 1. / 5.) * pow(q_min, -3. / 5.)))
        elif roq_bounds == 'component_mass':
            m1_min = m1_priors[roq][0]
            m1_max = m1_priors[roq][1]
            m2_min = m2_priors[roq][0]
            m2_max = m2_priors[roq][1]
            this_cp.set('engine', 'mass1-min', str(m1_min))
            this_cp.set('engine', 'mass1-max', str(m1_max))
            this_cp.set('engine', 'mass2-min', str(m2_min))
            this_cp.set('engine', 'mass2-max', str(m2_max))
        yield this_cp
    raise StopIteration()
Пример #2
0
  else:
    gid=None
    row=None

  roq_bounds = pipe_utils.Query_ROQ_Bounds_Type(path, roq_paths)
  if roq_bounds == 'chirp_mass_q':
    print 'ROQ has bounds in chirp mass and mass-ratio'
    mc_priors, trigger_mchirp = pipe_utils.get_roq_mchirp_priors(path, roq_paths, roq_params, key, gid=gid, sim_inspiral=row)
  elif roq_bounds == 'component_mass':
    print 'ROQ has bounds in component masses'
    # get component mass bounds, then compute the chirp mass that can be safely covered
    # further below we pass along the component mass bounds to the sampler, not the tighter chirp-mass, q bounds
    m1_priors, m2_priors, trigger_mchirp = pipe_utils.get_roq_component_mass_priors(path, roq_paths, roq_params, key, gid=gid, sim_inspiral=row)
    mc_priors = {}
    for (roq,m1_prior), (roq2,m2_prior) in zip(m1_priors.items(), m2_priors.items()):
      mc_priors[roq] = sorted([pipe_utils.mchirp_from_components(m1_prior[1], m2_prior[0]), pipe_utils.mchirp_from_components(m1_prior[0], m2_prior[1])])

  if cp.has_option('lalinference','trigger_mchirp'):
      trigger_mchirp=float(cp.get('lalinference','trigger_mchirp'))
  roq_mass_freq_scale_factor = pipe_utils.get_roq_mass_freq_scale_factor(mc_priors, trigger_mchirp, roq_force_flow)
  if roq_mass_freq_scale_factor != 1.:
    print 'WARNING: Rescaling ROQ basis, please ensure it is allowed with the model used.'

  if opts.gid is not None or (opts.injections is not None or cp.has_option('input','injection-file')):

    for mc_prior in mc_priors:
      mc_priors[mc_prior] = array(mc_priors[mc_prior])
    # find mass bin containing the trigger
    trigger_bin = None
    for roq in roq_paths:
      if mc_priors[roq][0]*roq_mass_freq_scale_factor <= trigger_mchirp <= mc_priors[roq][1]*roq_mass_freq_scale_factor:
Пример #3
0
  else:
    gid=None
    row=None

  roq_bounds = pipe_utils.Query_ROQ_Bounds_Type(path, roq_paths)
  if roq_bounds == 'chirp_mass_q':
    print 'ROQ has bounds in chirp mass and mass-ratio'
    mc_priors, trigger_mchirp = pipe_utils.get_roq_mchirp_priors(path, roq_paths, roq_params, key, gid=gid, sim_inspiral=row)
  elif roq_bounds == 'component_mass':
    print 'ROQ has bounds in component masses'
    # get component mass bounds, then compute the chirp mass that can be safely covered
    # further below we pass along the component mass bounds to the sampler, not the tighter chirp-mass, q bounds
    m1_priors, m2_priors, trigger_mchirp = pipe_utils.get_roq_component_mass_priors(path, roq_paths, roq_params, key, gid=gid, sim_inspiral=row)
    mc_priors = {}
    for (roq,m1_prior), (roq2,m2_prior) in zip(m1_priors.items(), m2_priors.items()):
      mc_priors[roq] = sorted([pipe_utils.mchirp_from_components(m1_prior[1], m2_prior[0]), pipe_utils.mchirp_from_components(m1_prior[0], m2_prior[1])])

  if cp.has_option('lalinference','trigger_mchirp'):
      trigger_mchirp=float(cp.get('lalinference','trigger_mchirp'))
  roq_mass_freq_scale_factor = pipe_utils.get_roq_mass_freq_scale_factor(mc_priors, trigger_mchirp, roq_force_flow)
  if roq_mass_freq_scale_factor != 1.:
    print 'WARNING: Rescaling ROQ basis, please ensure it is allowed with the model used.'

  if opts.gid is not None or (opts.injections is not None or cp.has_option('input','injection-file')):

    for mc_prior in mc_priors:
      mc_priors[mc_prior] = array(mc_priors[mc_prior])
    # find mass bin containing the trigger
    trigger_bin = None
    for roq in roq_paths:
      if mc_priors[roq][0]*roq_mass_freq_scale_factor <= trigger_mchirp <= mc_priors[roq][1]*roq_mass_freq_scale_factor:
Пример #4
0
    roq_bounds = pipe_utils.Query_ROQ_Bounds_Type(path, roq_paths)
    if roq_bounds == 'chirp_mass_q':
        print 'ROQ has bounds in chirp mass and mass-ratio'
        mc_priors, trigger_mchirp = pipe_utils.get_roq_mchirp_priors(
            path, roq_paths, roq_params, key, gid=gid, sim_inspiral=row)
    elif roq_bounds == 'component_mass':
        print 'ROQ has bounds in component masses'
        # get component mass bounds, then compute the chirp mass that can be safely covered
        # further below we pass along the component mass bounds to the sampler, not the tighter chirp-mass, q bounds
        m1_priors, m2_priors, trigger_mchirp = pipe_utils.get_roq_component_mass_priors(
            path, roq_paths, roq_params, key, gid=gid, sim_inspiral=row)
        mc_priors = {}
        for (roq, m1_prior), (roq2, m2_prior) in zip(m1_priors.items(),
                                                     m2_priors.items()):
            mc_priors[roq] = sorted([
                pipe_utils.mchirp_from_components(m1_prior[1], m2_prior[0]),
                pipe_utils.mchirp_from_components(m1_prior[0], m2_prior[1])
            ])

    if cp.has_option('lalinference', 'trigger_mchirp'):
        trigger_mchirp = float(cp.get('lalinference', 'trigger_mchirp'))
    roq_mass_freq_scale_factor = pipe_utils.get_roq_mass_freq_scale_factor(
        mc_priors, trigger_mchirp, roq_force_flow)
    if roq_mass_freq_scale_factor != 1.:
        print 'WARNING: Rescaling ROQ basis, please ensure it is allowed with the model used.'

    if opts.gid is not None or (opts.injections is not None or cp.has_option(
            'input', 'injection-file')) or cp.has_option(
                'lalinference', 'trigger_mchirp'):

        for mc_prior in mc_priors:
Пример #5
0
def setup_roq(cp):
    """
    Generates cp objects with the different ROQs applied
    """
    use_roq=False
    if cp.has_option('paths','roq_b_matrix_directory') or cp.has_option('paths','computeroqweights'):
        if not cp.has_option('analysis','roq'):
            print("Warning: If you are attempting to enable ROQ by specifying roq_b_matrix_directory or computeroqweights,\n\
            please use analysis.roq in your config file in future. Enabling ROQ.")
            cp.set('analysis','roq',True)
    if not cp.getboolean('analysis','roq'):
        yield cp
        raise StopIteration()
    from numpy import genfromtxt, array
    path=cp.get('paths','roq_b_matrix_directory')
    if not os.path.isdir(path):
        print("The ROQ directory %s does not seem to exist\n"%path)
        sys.exit(1)
    use_roq=True
    roq_paths=os.listdir(path)
    roq_params={}
    roq_force_flow = None

    if cp.has_option('lalinference','roq_force_flow'):
        roq_force_flow = cp.getfloat('lalinference','roq_force_flow')
        print("WARNING: Forcing the f_low to ", str(roq_force_flow), "Hz")
        print("WARNING: Overwriting user choice of flow, srate, seglen, and (mc_min, mc_max and q-min) or (mass1_min, mass1_max, mass2_min, mass2_max)")

    def key(item): # to order the ROQ bases
        return float(item[1]['seglen'])

    coinc_xml_obj = None
    row=None

    # Get file object of coinc.xml
    if opts.gid is not None:
        from ligo.gracedb.rest import GraceDb
        gid=opts.gid
        cwd=os.getcwd()
        if cp.has_option('analysis', 'service-url'):
            client = GraceDb(cp.get('analysis', 'service-url'))
        else:
            client = GraceDb()
        coinc_xml_obj = ligolw_utils.load_fileobj(client.files(gid, "coinc.xml"), contenthandler = lsctables.use_in(ligolw.LIGOLWContentHandler))[0]
    elif cp.has_option('input', 'coinc-xml'):
        coinc_xml_obj = ligolw_utils.load_fileobj(open(cp.get('input', 'coinc-xml'), "rb"), contenthandler = lsctables.use_in(ligolw.LIGOLWContentHandler))[0]

    # Get sim_inspiral from injection file
    if cp.has_option('input','injection-file'):
        print("Only 0-th event in the XML table will be considered while running with ROQ\n")
        row = lsctables.SimInspiralTable.get_table(
                  ligolw_utils.load_filename(cp.get('input','injection-file'),contenthandler=lsctables.use_in(ligolw.LIGOLWContentHandler))
              )[0]

    roq_bounds = pipe_utils.Query_ROQ_Bounds_Type(path, roq_paths)
    if roq_bounds == 'chirp_mass_q':
        print('ROQ has bounds in chirp mass and mass-ratio')
        mc_priors, trigger_mchirp = pipe_utils.get_roq_mchirp_priors(
            path, roq_paths, roq_params, key, coinc_xml_obj=coinc_xml_obj, sim_inspiral=row
        )
    elif roq_bounds == 'component_mass':
        print('ROQ has bounds in component masses')
        # get component mass bounds, then compute the chirp mass that can be safely covered
        # further below we pass along the component mass bounds to the sampler, not the tighter chirp-mass, q bounds
        m1_priors, m2_priors, trigger_mchirp = pipe_utils.get_roq_component_mass_priors(
            path, roq_paths, roq_params, key, coinc_xml_obj=coinc_xml_obj, sim_inspiral=row
        )
        mc_priors = {}
        for (roq,m1_prior), (roq2,m2_prior) in zip(m1_priors.items(), m2_priors.items()):
            mc_priors[roq] = sorted([pipe_utils.mchirp_from_components(m1_prior[1], m2_prior[0]), pipe_utils.mchirp_from_components(m1_prior[0], m2_prior[1])])

    if cp.has_option('lalinference','trigger_mchirp'):
        trigger_mchirp=float(cp.get('lalinference','trigger_mchirp'))
    roq_mass_freq_scale_factor = pipe_utils.get_roq_mass_freq_scale_factor(mc_priors, trigger_mchirp, roq_force_flow)
    if roq_mass_freq_scale_factor != 1.:
        print('WARNING: Rescaling ROQ basis, please ensure it is allowed with the model used.')

    # If the true chirp mass is unknown, add variations over the mass bins
    if opts.gid is not None or (opts.injections is not None or cp.has_option('input','injection-file')) or cp.has_option('lalinference','trigger_mchirp') or cp.has_option('input', 'coinc-xml'):

        for mc_prior in mc_priors:
            mc_priors[mc_prior] = array(mc_priors[mc_prior])
        # find mass bin containing the trigger
        trigger_bin = None
        for roq in roq_paths:
            if mc_priors[roq][0]*roq_mass_freq_scale_factor <= trigger_mchirp <= mc_priors[roq][1]*roq_mass_freq_scale_factor:
                trigger_bin = roq
                print('Prior in Mchirp will be ['+str(mc_priors[roq][0]*roq_mass_freq_scale_factor)+','+str(mc_priors[roq][1]*roq_mass_freq_scale_factor)+'] to contain the trigger Mchirp '+str(trigger_mchirp))
                break
        roq_paths = [trigger_bin]
    else:
        for mc_prior in mc_priors:
            mc_priors[mc_prior] = array(mc_priors[mc_prior])*roq_mass_freq_scale_factor

    # write the master configparser
    cur_basedir = cp.get('paths','basedir')
    masterpath=os.path.join(cur_basedir,'config.ini')
    with open(masterpath,'w') as cpfile:
        cp.write(cpfile)

    for roq in roq_paths:
        this_cp = configparser.ConfigParser()
        this_cp.optionxform = str
        this_cp.read(masterpath)
        basedir = this_cp.get('paths','basedir')
        for dirs in 'basedir','daglogdir','webdir':
            val = this_cp.get('paths',dirs)
            newval = os.path.join(val,roq)
            mkdirs(newval)
            this_cp.set('paths',dirs,newval)
        this_cp.set('paths','roq_b_matrix_directory',os.path.join(cp.get('paths','roq_b_matrix_directory'),roq))
        flow=roq_params[roq]['flow'] / roq_mass_freq_scale_factor
        srate=2.*roq_params[roq]['fhigh'] / roq_mass_freq_scale_factor
        #if srate > 8192:
        #    srate = 8192

        seglen=roq_params[roq]['seglen'] * roq_mass_freq_scale_factor
        # params.dat uses the convention q>1 so our q_min is the inverse of their qmax
        this_cp.set('engine','srate',str(srate))
        this_cp.set('engine','seglen',str(seglen))
        if this_cp.has_option('lalinference','flow'):
            tmp=this_cp.get('lalinference','flow')
            tmp=eval(tmp)
            ifos=tmp.keys()
        else:
            tmp={}
            ifos=eval(this_cp.get('analysis','ifos'))
        for i in ifos:
            tmp[i]=flow
            this_cp.set('lalinference','flow',str(tmp))
        if roq_bounds == 'chirp_mass_q':
            mc_min=mc_priors[roq][0]*roq_mass_freq_scale_factor
            mc_max=mc_priors[roq][1]*roq_mass_freq_scale_factor
            # params.dat uses the convention q>1 so our q_min is the inverse of their qmax
            q_min=1./float(roq_params[roq]['qmax'])
            this_cp.set('engine','chirpmass-min',str(mc_min))
            this_cp.set('engine','chirpmass-max',str(mc_max))
            this_cp.set('engine','q-min',str(q_min))
            this_cp.set('engine','comp-min', str(max(roq_params[roq]['compmin'] * roq_mass_freq_scale_factor, mc_min * pow(1+q_min, 1./5.) * pow(q_min, 2./5.))))
            this_cp.set('engine','comp-max', str(mc_max * pow(1+q_min, 1./5.) * pow(q_min, -3./5.)))
        elif roq_bounds == 'component_mass':
            m1_min = m1_priors[roq][0]
            m1_max = m1_priors[roq][1]
            m2_min = m2_priors[roq][0]
            m2_max = m2_priors[roq][1]
            this_cp.set('engine','mass1-min',str(m1_min))
            this_cp.set('engine','mass1-max',str(m1_max))
            this_cp.set('engine','mass2-min',str(m2_min))
            this_cp.set('engine','mass2-max',str(m2_max))
        yield this_cp
    raise StopIteration()