Skip to content

Bilby Pipe Interface

ptfp.bilby_pipe_interface

get_analysis_from_dag

get_analysis_from_dag(path_to_dag, data_case=0, analysis_interferometers='H1L1', data_analysis_input_class=DataAnalysisInput, parser_generator=create_parser)

Given a dag (and located in the correct directory) gets the bilby_pipe.data_analysis.DataAnalysisInput object corresponding to that bilby pipe run.

Parameters

path_to_dag : str
    The path to the dag, absolute or relative to
    directory from which it could be run
    (i.e. outdir/submit/dagname.dag)
data_case : int
    The case of the data to use (i.e. the index 0
    in a ..._data0_... file name).
    For multiple injection runs, this is the injection
    index.
analysis_interferometers : str
    The interferometers used in the analysis you wish to load,
    used to search for the appropriate files. Defaults to H1L1
data_analysis_input_class: Callable = bilby_pipe.data_analysis.DataAnalysisInput
    The input class from which to generate the analysis object.
    Note that this has only been tested with the standard DataAnalysisInput
parser_generator: Callable = bilby_pipe.parser.create_parser,
    The input class from which to generate the analysis object.
    Note that this has only been tested with the standard bilby_pipe parser

Returns

bibly_pipe.data_analysis.DataAnalysisInput
    The analysis input object corresponding to this run
Source code in ptfp/bilby_pipe_interface.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def get_analysis_from_dag(
    path_to_dag: str,
    data_case: int = 0,
    analysis_interferometers: Optional[str] = "H1L1",
    data_analysis_input_class: Callable = bilby_pipe.data_analysis.DataAnalysisInput,
    parser_generator: Callable = bilby_pipe.parser.create_parser,
) -> bilby_pipe.data_analysis.DataAnalysisInput:
    """Given a dag (and located in the correct directory)
    gets the bilby_pipe.data_analysis.DataAnalysisInput object
    corresponding to that bilby pipe run.

    Parameters
    ==========
        path_to_dag : str
            The path to the dag, absolute or relative to
            directory from which it could be run
            (i.e. outdir/submit/dagname.dag)
        data_case : int
            The case of the data to use (i.e. the index 0
            in a ..._data0_... file name).
            For multiple injection runs, this is the injection
            index.
        analysis_interferometers : str
            The interferometers used in the analysis you wish to load,
            used to search for the appropriate files. Defaults to H1L1
        data_analysis_input_class: Callable = bilby_pipe.data_analysis.DataAnalysisInput
            The input class from which to generate the analysis object.
            Note that this has only been tested with the standard DataAnalysisInput
        parser_generator: Callable = bilby_pipe.parser.create_parser,
            The input class from which to generate the analysis object.
            Note that this has only been tested with the standard bilby_pipe parser
    Returns
    =======
        bibly_pipe.data_analysis.DataAnalysisInput
            The analysis input object corresponding to this run
    """
    with open(path_to_dag, "r") as f:
        lines = f.readlines()
        analysis_line = [
            x
            for x in lines
            if "VARS" in x
            and f"analysis_{analysis_interferometers}" in x
            and f"data{data_case}_" in x
        ][0]
        args, unknown_args = bilby_pipe.utils.parse_args(
            analysis_line.split('"')[1].split(" "),
            parser_generator(top_level=False),
        )
    analysis = data_analysis_input_class(args, unknown_args)
    return analysis

draw_dict_sample_from_posterior

draw_dict_sample_from_posterior(posterior)

Gets a correctly formatted dict corresponding to a single sample in the posterior.

Parameters

posterior : pd.DataFrame
    The dataframe representation of the posterior
    to draw a sample from

Returns

Dict[str, float]
    The dictionary of {param_name : param_value} for a given sample
Source code in ptfp/bilby_pipe_interface.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def draw_dict_sample_from_posterior(posterior: pd.DataFrame) -> Dict[str, float]:
    """Gets a correctly formatted dict corresponding to a single
    sample in the posterior.

    Parameters
    ==========
        posterior : pd.DataFrame
            The dataframe representation of the posterior
            to draw a sample from

    Returns
    =======
        Dict[str, float]
            The dictionary of {param_name : param_value} for a given sample
    """
    sample = posterior.sample()
    return {
        k: [v for v in sample_dict.values()][0]
        for k, sample_dict in sample.to_dict().items()
    }

get_all_analysis_components

get_all_analysis_components(base_directory, analysis_interferometers='H1L1', data_case=0)

Reconstruct the objects used in the bilby pipe run by parsing out necessary information in the output directory, using various dag and file parsing methods. Requires the original run output directory to function

Parameters

base_directory : str
    The directory in which the analysis was run
analysis_interferometers : str
    The interferometers used for the analysis
    (as appears in bilby filenames)
data_case : Optional[int]=0
    If there are multiple indexed data objects (e.g. from injections),
    this indexes which to return

Returns

bilby.core.result.Result
    The analysis result
bilby_pipe.utils.DataDump
    The data dump object
bilby_pipe.data_analysis.DataAnalysisInput
    The reconsructed analysis object
bilby.gw.likelihood.GravitationalWaveTransient
    The likelihood used in the analysis
Source code in ptfp/bilby_pipe_interface.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def get_all_analysis_components(
    base_directory: str,
    analysis_interferometers: Optional[str] = "H1L1",
    data_case: Optional[int] = 0,
) -> Tuple[
    bilby.core.result.Result,
    bilby_pipe.utils.DataDump,
    bilby_pipe.data_analysis.DataAnalysisInput,
    bilby.gw.likelihood.GravitationalWaveTransient,
]:
    """Reconstruct the objects used in the bilby pipe run
    by parsing out necessary information in the output directory,
    using various dag and file parsing methods.
    ***Requires the original run output directory to function***

    Parameters
    ==========
        base_directory : str
            The directory in which the analysis was run
        analysis_interferometers : str
            The interferometers used for the analysis
            (as appears in bilby filenames)
        data_case : Optional[int]=0
            If there are multiple indexed data objects (e.g. from injections),
            this indexes which to return

    Returns
    =======
        bilby.core.result.Result
            The analysis result
        bilby_pipe.utils.DataDump
            The data dump object
        bilby_pipe.data_analysis.DataAnalysisInput
            The reconsructed analysis object
        bilby.gw.likelihood.GravitationalWaveTransient
            The likelihood used in the analysis
    """
    merge_result_file = [
        os.path.join(base_directory, "final_result", x)
        for x in os.listdir(os.path.join(base_directory, "final_result"))
        if f"data{data_case}_" in x
        and f"_{analysis_interferometers}_" in x
        and "result.hdf5" in x
    ][0]
    result = bilby.core.result.read_in_result(merge_result_file)

    data_file = [
        os.path.join(base_directory, "data", x)
        for x in os.listdir(os.path.join(base_directory, "data"))
        if f"data{data_case}_" in x and "data_dump.pickle" in x
    ][0]
    data = bilby_pipe.utils.DataDump.from_pickle(data_file)

    owd = os.getcwd()
    os.chdir(os.path.join(base_directory, ".."))
    submit_dag = [
        os.path.join(base_directory, "submit", x)
        for x in os.listdir(os.path.join(base_directory, "submit"))
        if "dag" in x and x.split(".")[-1] == "submit"
    ][0]
    analysis = get_analysis_from_dag(
        submit_dag,
        data_case=data_case,
        analysis_interferometers=analysis_interferometers,
    )
    os.chdir(owd)

    likelihood_args = [
        analysis.interferometers,
        analysis.waveform_generator,
    ]
    likelihood_kwargs = dict(
        priors=copy.deepcopy(result.priors), distance_marginalization=False
    )

    # We'll assume for this that it's always a GravitationalWaveTransient likelihood
    likelihood = bilby.gw.likelihood.GravitationalWaveTransient(
        *likelihood_args, **likelihood_kwargs
    )

    return result, data, analysis, likelihood