Beispiel #1
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
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
Beispiel #3
0
def bilby_args_to_ini_string(args):
    # Create an argument parser
    parser = create_parser()

    # Use a tempfile to write the args as an ini file, then read the ini content back from 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 ini content
        return f.read().decode('utf-8')
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
Beispiel #5
0
def args_to_bilby_ini(args):
    """
    Generates an ini string from the provided args

    :params args: The args to add to the ini string
    :return: A string containing the ini file content
    """

    # 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
        parser.write_to_file(f.name, args, overwrite=True)

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

        # Read the ini file from the file
        ini_string = f.read()

    return ini_string
Beispiel #6
0
def create_bilby_job(user, params):
    # First check the ligo permissions and ligo job status
    is_ligo_job = False

    # Check that non-ligo users only have access to GWOSC channels for real data
    # Note that we're checking for not simulated here, rather than for real, because the backend
    # explicitly checks for `if input_params["data"]["type"] == "simulated":`, so any value other than
    # `simulation` would result in a real data job
    if params.data.data_choice != "simulated" and (
            params.data.channels.hanford_channel != "GWOSC"
            or params.data.channels.livingston_channel != "GWOSC"
            or params.data.channels.virgo_channel != "GWOSC"):
        # This is a real job, with a channel that is not GWOSC
        if not user.is_ligo:
            # User is not a ligo user, so they may not submit this job
            raise Exception(
                "Non-LIGO members may only run real jobs on GWOSC channels")
        else:
            # User is a ligo user, so they may submit the job, but we need to track that the job is a
            # ligo "proprietary" job
            is_ligo_job = True

    # todo: request_cpus

    # Generate the detector choice
    detectors = []
    maximum_frequencies = {}
    minimum_frequencies = {}
    channels = {}
    for k, v in {('hanford', 'H1'), ('livingston', 'L1'), ('virgo', 'V1')}:
        if getattr(params.detector, k):
            detectors.append(v)
            maximum_frequencies[v] = str(
                getattr(params.detector, k + "_maximum_frequency"))
            minimum_frequencies[v] = str(
                getattr(params.detector, k + "_minimum_frequency"))
            channels[v] = getattr(params.data.channels, k + "_channel")

    # Set the run type as simulation if required
    num_simulated = 0
    gaussian_noise = False
    if params.data.data_choice == "simulated":
        num_simulated = 1
        gaussian_noise = True

    # Set the waveform generator (Always fall back to BBH if invalid parameter provided)
    frequency_domain_source_model = "lal_binary_black_hole"
    if params.waveform.model == "binaryNeutronStar":
        frequency_domain_source_model = "lal_binary_neutron_star"

    sampler_kwargs = {
        'nlive': params.sampler.nlive,
        'nact': params.sampler.nact,
        'maxmcmc': params.sampler.maxmcmc,
        'walks': params.sampler.walks,
        'dlogz': str(params.sampler.dlogz)
    }

    # Parse the input parameters in to an argument dict
    data = {
        ################################################################################
        # Calibration arguments
        # Which calibration model and settings to use.
        ################################################################################

        ################################################################################
        # Data generation arguments
        # How to generate the data, e.g., from a list of gps times or simulated Gaussian noise.
        ################################################################################

        # The trigger time
        "trigger-time": params.data.trigger_time or "None",

        # If true, use simulated Gaussian noise
        "gaussian-noise": gaussian_noise,

        # Number of simulated segments to use with gaussian-noise Note, this must match the number of injections
        # specified
        "n-simulation": num_simulated,

        # Channel dictionary: keys relate to the detector with values the channel name, e.g. 'GDS-CALIB_STRAIN'.
        # For GWOSC open data, set the channel-dict keys to 'GWOSC'. Note, the dictionary should follow basic python
        # dict syntax.
        "channel-dict": repr(channels),

        ################################################################################
        # Detector arguments
        # How to set up the interferometers and power spectral density.
        ################################################################################

        # The names of detectors to use. If given in the ini file, detectors are specified by `detectors=[H1, L1]`. If
        # given at the command line, as `--detectors H1 --detectors L1`
        "detectors": repr(detectors),

        # The duration of data around the event to use
        "duration": params.detector.duration,

        # None
        "sampling-frequency": params.detector.sampling_frequency,

        # The maximum frequency, given either as a float for all detectors or as a dictionary (see minimum-frequency)
        "maximum-frequency": repr(maximum_frequencies),

        # The minimum frequency, given either as a float for all detectors or as a dictionary where all keys relate
        # the detector with values of the minimum frequency, e.g. {H1: 10, L1: 20}. If the waveform generation should
        # start the minimum frequency for any of the detectors, add another entry to the dictionary,
        # e.g., {H1: 40, L1: 60, waveform: 20}.
        "minimum-frequency": repr(minimum_frequencies),

        ################################################################################
        # Injection arguments
        # Whether to include software injections and how to generate them.
        ################################################################################

        ################################################################################
        # Job submission arguments
        # How the jobs should be formatted, e.g., which job scheduler to use.
        ################################################################################

        # Output label
        "label": params.details.name,

        # Use multi-processing. This options sets the number of cores to request. To use a pool of 8 threads on an
        # 8-core CPU, set request-cpus=8. For the dynesty, ptemcee, cpnest, and bilby_mcmc samplers, no additional
        # sampler-kwargs are required
        "request-cpus": params.sampler.cpus,

        # Some parameters set by job controller client

        ################################################################################
        # Likelihood arguments
        # Options for setting up the likelihood.
        ################################################################################

        ################################################################################
        # Output arguments
        # What kind of output/summary to generate.
        ################################################################################

        # Some parameters set by job controller client

        ################################################################################
        # Prior arguments
        # Specify the prior settings.
        ################################################################################

        # The prior file
        "prior-file": params.prior.prior_default,

        ################################################################################
        # Post processing arguments
        # What post-processing to perform.
        ################################################################################

        ################################################################################
        # Sampler arguments
        # None
        ################################################################################

        # Sampler to use
        "sampler": params.sampler.sampler_choice,

        # Dictionary of sampler-kwargs to pass in, e.g., {nlive: 1000} OR pass pre-defined set of sampler-kwargs
        # {Default, FastTest}
        "sampler-kwargs": repr(sampler_kwargs),

        ################################################################################
        # Waveform arguments
        # Setting for the waveform generator
        ################################################################################

        # Turns on waveform error catching
        "catch-waveform-errors": True,

        # Name of the frequency domain source model. Can be one of[lal_binary_black_hole, lal_binary_neutron_star,
        # lal_eccentric_binary_black_hole_no_spins, sinegaussian, supernova, supernova_pca_model] or any python path
        # to a bilby  source function the users installation, e.g. examp.source.bbh
        "frequency-domain-source-model": frequency_domain_source_model
    }

    # 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, data, overwrite=True)

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

        ini_string = f.read().decode('utf-8')

    bilby_job = BilbyJob.objects.create(user_id=user.user_id,
                                        name=params.details.name,
                                        description=params.details.description,
                                        private=params.details.private,
                                        is_ligo_job=is_ligo_job,
                                        ini_string=ini_string,
                                        cluster=params.details.cluster)

    # Submit the job to the job controller
    bilby_job.submit()

    return bilby_job
def create_generation_parser():
    """ Data generation parser creation """
    return create_parser(top_level=False)
 def setUp(self):
     self.test_ini_filename = os.path.join(
         os.path.abspath(os.path.dirname(__file__)), "TEST_CONFIG.ini")
     self.parser = create_parser(top_level=True)
def create_analysis_parser():
    """ Data analysis parser creation """
    return create_parser(top_level=False)