Esempio n. 1
0
    def test_parser_with_default_prior_file(self):
        known_args_list = ["4s", "--n-injection", "3"]
        parser = bilby_pipe.create_injections.create_parser()
        args, unknown_args = parse_args(known_args_list, parser)

        self.assertEqual(args.prior_file, "4s")
        self.assertEqual(args.n_injection, 3)
def write_ini_file(args):
    """
    Takes the parser args and writes the complete ini file in the job output directory

    :param args: Args as generated by the bilby_pipe parser
    :return: The updated Args, and the MainInput object representing the complete bilby_pipe input object
    """
    # Create an argument parser
    parser = create_parser()

    # Because we don't know the correct ini file name yet, we need to save the ini data to a temporary file
    # and then read the data back in to create a MainInput object, which we can then use to get the name of the ini
    # file
    with NamedTemporaryFile() as f:
        # Write the temporary ini file
        parser.write_to_file(f.name, args, overwrite=True)

        # Make sure the data is flushed
        f.flush()

        # Read the data from the ini file
        args, unknown_args = parse_args([f.name], parser)

        # Generate the Input object so that we can determine the correct ini file
        inputs = MainInput(args, unknown_args)

    # Write the real ini file
    parser.write_to_file(inputs.complete_ini_file, args, overwrite=True)

    return args, inputs
Esempio n. 3
0
def bilby_ini_string_to_args(ini):
    """
    Parses an ini string in to an argument Namespace

    :params ini: The ini string to parse
    :return: An ArgParser Namespace of the parsed arguments from the ini
    """

    # Create an bilby argument parser
    parser = create_parser()

    # Bilby pipe requires a real file in order to parse the ini file
    with NamedTemporaryFile() as f:
        # Write the temporary ini file
        f.write(ini)

        # Make sure the data is written to the temporary file
        f.flush()

        # Read the data from the ini file
        args, unknown_args = parse_args([f.name], parser)

    # ini and verbose are not kept in the ini file, so remove them
    delattr(args, 'ini')
    delattr(args, 'verbose')

    return args
Esempio n. 4
0
    def test_parser_with_json(self):
        example_prior_file = "tests/example_prior.prior"

        known_args_list = [example_prior_file, "--n-injection", "3", "-e", "json"]
        parser = bilby_pipe.create_injections.create_parser()
        args, unknown_args = parse_args(known_args_list, parser)

        self.assertEqual(args.extension, "json")
Esempio n. 5
0
    def test_parser_with_prior_file(self):
        example_prior_file = "tests/example_prior.prior"

        known_args_list = [example_prior_file, "--n-injection", "3", "-s", "1234"]
        parser = bilby_pipe.create_injections.create_parser()
        args, unknown_args = parse_args(known_args_list, parser)

        self.assertEqual(args.prior_file, example_prior_file)
        self.assertEqual(args.n_injection, 3)
        self.assertEqual(args.generation_seed, 1234)
Esempio n. 6
0
    def test_parser_defaults(self):
        example_prior_file = "tests/example_prior.prior"

        known_args_list = [example_prior_file, "-n", "1"]
        parser = bilby_pipe.create_injections.create_parser()
        args, unknown_args = parse_args(known_args_list, parser)

        self.assertEqual(args.prior_file, example_prior_file)
        self.assertEqual(args.n_injection, 1)
        self.assertEqual(args.extension, "dat")
Esempio n. 7
0
def main():
    args, unknown_args = parse_args(sys.argv[1:], create_parser(__prog__))
    logger = logging.getLogger(__prog__)

    result = read_in_result(args.result)
    label = result.label

    # Test if the result was generated by parallel_bilby
    result_from_pbilby = False
    try:
        if result.meta_data["command_line_args"][
                "sampler"] == "parallel_bilby":
            result_from_pbilby = True
    except:
        pass

    if args.generate_samples_for_marginalized_parameters:
        single_trigger_likelihoods = []
        if result_from_pbilby:
            for i in range(args.n_triggers):
                l, _, _ = load_run_from_pbilby(args.data_dump_files[i])
                single_trigger_likelihoods.append(l)
        else:
            for i in range(args.n_triggers):
                l, _, _ = load_run_from_bilby(args.data_dump_files[i],
                                              args.trigger_ini_files[i])
                single_trigger_likelihoods.append(l)

        generate_joint_posterior_samples_from_marginalized_likelihood(
            result, single_trigger_likelihoods, ncores=args.ncores)
        label += "_marginalized_parameter_reconstructed"

    if args.flat_in_component_masses:
        result = reweight_flat_in_component_masses(result)
        label += "_reweighted"

    if args.uniform_in_comoving_volume:
        result = reweight_uniform_in_comoving_volume(result)
        label += "_reweighted"

    # Save to file
    logger.info("Done. Saving to file")
    result.label = label
    result.save_to_file(outdir=".")
def bilby_ini_to_args(ini):
    """
    Parses an ini string in to an argument Namespace

    :params ini: The ini string to parse
    :return: An ArgParser Namespace of the parsed arguments from the ini
    """

    # Create a bilby argument parser
    parser = create_parser()

    # Bilby pipe requires a real file in order to parse the ini file
    with NamedTemporaryFile() as f:
        # Write the temporary ini file
        f.write(ini.encode('utf-8'))

        # Make sure the data is written to the temporary file
        f.flush()

        # Read the data from the ini file
        args, unknown_args = parse_args([f.name], parser)

    return args
Esempio n. 9
0
def main():
    args, unknown_args = parse_args(sys.argv[1:], create_parser(__prog__))
    logger = logging.getLogger(__prog__)

    result = read_in_result(args.result)
    label = result.label

    # Test if the result was generated by parallel_bilby
    result_from_pbilby = False
    try:
        if result.meta_data["command_line_args"][
                "sampler"] == "parallel_bilby":
            result_from_pbilby = True
    except:
        pass

    if args.generate_samples_for_marginalized_parameters:
        single_trigger_likelihoods = reconstruct_likelihoods(
            n_triggers=args.n_triggers,
            trigger_ini_files=args.trigger_ini_files,
            data_dump_files=args.data_dump_files,
            result_from_pbilby=result_from_pbilby,
        )

        if not args.not_from_hanabi:
            result = generate_joint_posterior_samples_from_marginalized_likelihood(
                result, single_trigger_likelihoods, ncores=args.ncores)
        else:
            if args.n_triggers != 1:
                raise ValueError("Does not understand input")
            result = generate_posterior_samples_from_marginalized_likelihood(
                result, single_trigger_likelihoods[0], ncores=args.ncores)
        label += "_marginalized_parameter_reconstructed"

    if args.generate_component_mass_parameters:
        # Edit file **in-place**
        result = generate_component_mass_parameters(result)

    if args.generate_snrs:
        single_trigger_likelihoods = reconstruct_likelihoods(
            n_triggers=args.n_triggers,
            trigger_ini_files=args.trigger_ini_files,
            data_dump_files=args.data_dump_files,
            result_from_pbilby=result_from_pbilby,
        )

        # Edit file **in-place**
        if not args.not_from_hanabi:
            result = generate_joint_snrs(result,
                                         single_trigger_likelihoods,
                                         ncores=args.ncores)
        else:
            if args.n_triggers != 1:
                raise ValueError("Does not understand input")
            result = generate_snrs(result,
                                   single_trigger_likelihoods[0],
                                   ncores=args.ncores)

    if args.reweight_to_prior is not None:
        result = reweight_to_prior(
            result,
            bilby.core.prior.PriorDict(filename=args.reweight_to_prior))
        label += "_reweighted"

    if args.flat_in_component_masses:
        result = reweight_flat_in_component_masses(result)
        label += "_reweighted"

    if args.uniform_in_comoving_volume:
        result = reweight_uniform_in_comoving_volume(result)
        label += "_reweighted"

    # Save to file
    logger.info("Done. Saving to file")
    result.label = label
    result.save_to_file(outdir=".", filename=args.output_filename)