From 4c6bb18c4db71b736d6741a972222a2061209f2f Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 20 Apr 2021 11:39:12 +0100 Subject: [PATCH 01/13] improving doc strings and logger messages --- pesummary/core/file/formats/bilby.py | 4 +-- pesummary/core/file/read.py | 43 ++++++++++++++--------- pesummary/gw/file/formats/bilby.py | 18 +++++++++- pesummary/gw/file/formats/default.py | 3 ++ pesummary/gw/file/formats/lalinference.py | 3 ++ pesummary/io/read.py | 17 +++++++-- 6 files changed, 67 insertions(+), 21 deletions(-) diff --git a/pesummary/core/file/formats/bilby.py b/pesummary/core/file/formats/bilby.py index 6ec2fec7..1d3658fe 100644 --- a/pesummary/core/file/formats/bilby.py +++ b/pesummary/core/file/formats/bilby.py @@ -356,10 +356,10 @@ class Bilby(SingleAnalysisRead): return kwargs @staticmethod - def _grab_data_from_bilby_file(path, **kwargs): + def _grab_data_from_bilby_file(path, function=read_bilby, **kwargs): """Load the results file using the `bilby` library """ - return read_bilby(path, **kwargs) + return function(path, **kwargs) def add_marginalized_parameters_from_config_file(self, config_file): """Search the configuration file and add the marginalized parameters diff --git a/pesummary/core/file/read.py b/pesummary/core/file/read.py index 26573e2b..bc3b5402 100644 --- a/pesummary/core/file/read.py +++ b/pesummary/core/file/read.py @@ -10,26 +10,45 @@ import os __author__ = ["Charlie Hoy "] -def is_bilby_hdf5_file(path): - """Determine if the results file is a bilby hdf5 results file +def _check_bilby_file(f): + """Determine if the results file is a bilby results file Parameters ---------- - path: str - path to the results file + f: dict-like + loaded result file """ - import h5py try: - f = h5py.File(path, "r") if "bilby" in f["version"]: return True elif "bilby" in str(f["version"][0]): return True else: + standard_keys = [ + "fixed_parameter_keys", "label", "posterior", "meta_data", + "outdir", "parameter_labels" + ] + if all(key in f.keys() for key in standard_keys): + logger.info( + "This file looks like a bilby result file but unable " + "to find the string 'bilby' in the version information." + ) return False except Exception: return False - return False + + +def is_bilby_hdf5_file(path): + """Determine if the results file is a bilby hdf5 results file + + Parameters + ---------- + path: str + path to the results file + """ + import h5py + f = h5py.File(path, "r") + return _check_bilby_file(f) def is_bilby_json_file(path): @@ -43,15 +62,7 @@ def is_bilby_json_file(path): import json with open(path, "r") as f: data = json.load(f) - try: - if "bilby" in data["version"]: - return True - elif "bilby" in data["version"][0]: - return True - else: - return False - except Exception: - return False + return _check_bilby_file(data) def _is_pesummary_hdf5_file(path, check_function): diff --git a/pesummary/gw/file/formats/bilby.py b/pesummary/gw/file/formats/bilby.py index 99c5ea12..03835807 100644 --- a/pesummary/gw/file/formats/bilby.py +++ b/pesummary/gw/file/formats/bilby.py @@ -80,6 +80,12 @@ class Bilby(GWSingleAnalysisRead): distributions. Default False pe_algorithm: str name of the algorithm used to generate the posterior samples + disable_injection_conversion: Bool, optional + if True, disable the conversion module from deriving alternative + injection parameters + add_zero_likelihood: Bool, optional + if True assign 'log_likelihood=0' to every sample if no 'log_likelihood' + samples can be found. Default True Attributes ---------- @@ -265,7 +271,17 @@ class Bilby(GWSingleAnalysisRead): Complex matched filter SNRs are stored in the result file. The amplitude and angle are extracted here. """ - return read_bilby(path, disable_prior=disable_prior, **kwargs) + try: + return CoreBilby._grab_data_from_bilby_file( + path, function=read_bilby, disable_prior=disable_prior, + **kwargs + ) + except Exception as e: + logger.warn( + "Although the file '{}' passed the bilby file check, we " + "were unable to load the file because {}.".format(path, e) + ) + raise def add_marginalized_parameters_from_config_file(self, config_file): """Search the configuration file and add the marginalized parameters diff --git a/pesummary/gw/file/formats/default.py b/pesummary/gw/file/formats/default.py index 6726c6db..841b5654 100644 --- a/pesummary/gw/file/formats/default.py +++ b/pesummary/gw/file/formats/default.py @@ -17,6 +17,9 @@ class SingleAnalysisDefault(GWSingleAnalysisRead): ---------- path_to_results_file: str path to the results file you wish to load + add_zero_likelihood: Bool, optional + if True assign 'log_likelihood=0' to every sample if no 'log_likelihood' + samples can be found. Default True Attributes ---------- diff --git a/pesummary/gw/file/formats/lalinference.py b/pesummary/gw/file/formats/lalinference.py index a9773438..65df1a00 100644 --- a/pesummary/gw/file/formats/lalinference.py +++ b/pesummary/gw/file/formats/lalinference.py @@ -90,6 +90,9 @@ class LALInference(GWSingleAnalysisRead): ---------- path_to_results_file: str path to the results file you wish to load in with `LALInference` + add_zero_likelihood: Bool, optional + if True assign 'log_likelihood=0' to every sample if no 'log_likelihood' + samples can be found. Default True Attributes ---------- diff --git a/pesummary/io/read.py b/pesummary/io/read.py index 977939a2..fc8995ca 100644 --- a/pesummary/io/read.py +++ b/pesummary/io/read.py @@ -3,6 +3,7 @@ import os import importlib from pathlib import Path +from pesummary.utils.decorators import docstring_subfunction from pesummary.core.file.formats.ini import read_ini from pesummary.core.file.formats.pickle import read_pickle from pesummary.gw.file.formats.lcf import read_lcf @@ -33,6 +34,18 @@ def _fetch_from_remote_server(ff): return path +@docstring_subfunction( + [ + "pesummary.core.file.formats.bilby.Bilby", + "pesummary.core.file.formats.default.Default", + "pesummary.core.file.formats.pesummary.PESummary", + "pesummary.gw.file.formats.bilby.Bilby", + "pesummary.gw.file.formats.GWTC1.GWTC1", + "pesummary.gw.file.formats.lalinference.LALInference", + "pesummary.gw.file.formats.pesummary.PESummary", + "pesummary.gw.file.formats.pesummary.TGRPESummary" + ] +) def read( path, package="gw", file_format=None, skymap=False, cls=None, checkpoint=False, **kwargs @@ -56,8 +69,8 @@ def read( checkpoint: Bool, optional if True, treat path as the path to a checkpoint file **kwargs: dict, optional - all additional kwargs are passed to the `pesummary.{}.file.read.read` - function + all additional kwargs are passed to the + `pesummary.{}.file.formats.{}.{}` class. See below for details """ if not os.path.isfile(path) and "@" in path: path = _fetch_from_remote_server(path) -- GitLab From 96be73725b8e9e278f382a0fbef00220455a3255 Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 20 Apr 2021 11:50:47 +0100 Subject: [PATCH 02/13] typo --- pesummary/core/file/formats/bilby.py | 9 ++++++++- pesummary/gw/file/formats/bilby.py | 15 ++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pesummary/core/file/formats/bilby.py b/pesummary/core/file/formats/bilby.py index 1d3658fe..a398548a 100644 --- a/pesummary/core/file/formats/bilby.py +++ b/pesummary/core/file/formats/bilby.py @@ -359,7 +359,14 @@ class Bilby(SingleAnalysisRead): def _grab_data_from_bilby_file(path, function=read_bilby, **kwargs): """Load the results file using the `bilby` library """ - return function(path, **kwargs) + try: + return function(path, **kwargs) + except Exception as e: + raise Exception( + "Although the file passed the bilby file check, we were " + "unable to load the file because {}. This could be because " + "of a bilby version incompatability".format(e) + ) def add_marginalized_parameters_from_config_file(self, config_file): """Search the configuration file and add the marginalized parameters diff --git a/pesummary/gw/file/formats/bilby.py b/pesummary/gw/file/formats/bilby.py index 03835807..6355f559 100644 --- a/pesummary/gw/file/formats/bilby.py +++ b/pesummary/gw/file/formats/bilby.py @@ -271,17 +271,10 @@ class Bilby(GWSingleAnalysisRead): Complex matched filter SNRs are stored in the result file. The amplitude and angle are extracted here. """ - try: - return CoreBilby._grab_data_from_bilby_file( - path, function=read_bilby, disable_prior=disable_prior, - **kwargs - ) - except Exception as e: - logger.warn( - "Although the file '{}' passed the bilby file check, we " - "were unable to load the file because {}.".format(path, e) - ) - raise + return CoreBilby._grab_data_from_bilby_file( + path, function=read_bilby, disable_prior=disable_prior, + **kwargs + ) def add_marginalized_parameters_from_config_file(self, config_file): """Search the configuration file and add the marginalized parameters -- GitLab From 03b802d98e529d1ddd0d75deb05aa8f60eca5b2e Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 20 Apr 2021 11:56:34 +0100 Subject: [PATCH 03/13] typo --- pesummary/core/file/formats/bilby.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pesummary/core/file/formats/bilby.py b/pesummary/core/file/formats/bilby.py index a398548a..51c9299a 100644 --- a/pesummary/core/file/formats/bilby.py +++ b/pesummary/core/file/formats/bilby.py @@ -363,9 +363,12 @@ class Bilby(SingleAnalysisRead): return function(path, **kwargs) except Exception as e: raise Exception( - "Although the file passed the bilby file check, we were " - "unable to load the file because {}. This could be because " - "of a bilby version incompatability".format(e) + "Although the file looks like it was produced with bilby, we " + "were unable to load the file with the " + "'bilby.core.result.read_in_result' function because {}. This " + "is likely a result of a bilby version incompatability".format( + e + ) ) def add_marginalized_parameters_from_config_file(self, config_file): -- GitLab From 9bb8f3e0f733d39dc6d15c2041a62f72afdde63a Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 20 Apr 2021 14:22:20 +0100 Subject: [PATCH 04/13] change kwarg to match command line argument --- pesummary/cli/summarycombine_posteriors.py | 2 +- pesummary/cli/summaryextract.py | 3 +- pesummary/cli/summaryjscompare.py | 2 +- pesummary/core/file/formats/bilby.py | 12 ++++--- pesummary/core/inputs.py | 3 +- pesummary/gw/file/formats/bilby.py | 15 ++++---- pesummary/gw/inputs.py | 5 +-- pesummary/tests/read_test.py | 4 +-- pesummary/utils/decorators.py | 40 ++++++++++++++++++++++ 9 files changed, 66 insertions(+), 20 deletions(-) diff --git a/pesummary/cli/summarycombine_posteriors.py b/pesummary/cli/summarycombine_posteriors.py index c8cdb937..399d3eeb 100644 --- a/pesummary/cli/summarycombine_posteriors.py +++ b/pesummary/cli/summarycombine_posteriors.py @@ -76,7 +76,7 @@ def main(args=None): label: samples for label, samples in zip(opts.labels, opts.samples) } mydict = MultiAnalysisSamplesDict.from_files( - samples, disable_prior=True, disable_injection_conversion=True + samples, disable_prior_sampling=True, disable_injection_conversion=True ) combined = mydict.combine( use_all=opts.use_all, weights=opts.weights, labels=opts.labels, diff --git a/pesummary/cli/summaryextract.py b/pesummary/cli/summaryextract.py index 4d7ec9bd..75ff182c 100644 --- a/pesummary/cli/summaryextract.py +++ b/pesummary/cli/summaryextract.py @@ -51,7 +51,8 @@ def main(args=None): opts, unknown = _parser.parse_known_args(args=args) logger.info("Loading file: '{}'".format(opts.samples)) f = read( - opts.samples, disable_prior=True, disable_injection_conversion=True + opts.samples, disable_prior_sampling=True, + disable_injection_conversion=True ) posterior_samples = f.samples_dict logger.info("Writing analysis: '{}' to file".format(opts.label)) diff --git a/pesummary/cli/summaryjscompare.py b/pesummary/cli/summaryjscompare.py index fe40ba13..f3348f62 100644 --- a/pesummary/cli/summaryjscompare.py +++ b/pesummary/cli/summaryjscompare.py @@ -36,7 +36,7 @@ _check_latex_install() def load_data(data_file): """ Read in a data file and return samples dictionary """ - f = read(data_file, package="gw", disable_prior=True) + f = read(data_file, package="gw", disable_prior_sampling=True) return f.samples_dict diff --git a/pesummary/core/file/formats/bilby.py b/pesummary/core/file/formats/bilby.py index 51c9299a..84c12336 100644 --- a/pesummary/core/file/formats/bilby.py +++ b/pesummary/core/file/formats/bilby.py @@ -6,13 +6,15 @@ from pesummary.core.file.formats.base_read import SingleAnalysisRead from pesummary.core.plots.latex_labels import latex_labels from pesummary import conf from pesummary.utils.utils import logger +from pesummary.utils.decorators import deprecated_kwargs __author__ = ["Charlie Hoy "] +@deprecated_kwargs({"disable_prior": "disable_prior_sampling"}) def read_bilby( - path, disable_prior=False, complex_params=[], latex_dict=latex_labels, - nsamples_for_prior=None, **kwargs + path, disable_prior_sampling=False, complex_params=[], + latex_dict=latex_labels, nsamples_for_prior=None, **kwargs ): """Grab the parameters and samples in a bilby file @@ -20,7 +22,7 @@ def read_bilby( ---------- path: str path to the result file you wish to read in - disable_prior: Bool, optional + disable_prior_sampling: Bool, optional if True, do not collect prior samples from the `bilby` result file. Default False complex_params: list, optional @@ -95,7 +97,7 @@ def read_bilby( data["config"] = { "config": bilby_object.meta_data["command_line_args"] } - if not disable_prior: + if not disable_prior_sampling: logger.debug("Drawing prior samples from bilby result file") if nsamples_for_prior is None: nsamples_for_prior = len(samples) @@ -293,7 +295,7 @@ class Bilby(SingleAnalysisRead): ---------- path_to_results_file: str path to the results file that you wish to read in with `bilby`. - disable_prior: Bool, optional + disable_prior_sampling: Bool, optional if True, do not collect prior samples from the `bilby` result file. Default False diff --git a/pesummary/core/inputs.py b/pesummary/core/inputs.py index 26e9a123..b51c5ced 100644 --- a/pesummary/core/inputs.py +++ b/pesummary/core/inputs.py @@ -223,7 +223,8 @@ class _Input(object): `generate_all_posterior_samples` method """ f = read_function( - file, file_format=file_format, disable_prior=disable_prior_sampling, + file, file_format=file_format, + disable_prior_sampling=disable_prior_sampling, nsamples_for_prior=nsamples_for_prior, path_to_samples=path_to_samples ) if config is not None: diff --git a/pesummary/gw/file/formats/bilby.py b/pesummary/gw/file/formats/bilby.py index 6355f559..054c6ed0 100644 --- a/pesummary/gw/file/formats/bilby.py +++ b/pesummary/gw/file/formats/bilby.py @@ -11,7 +11,7 @@ __author__ = ["Charlie Hoy "] def read_bilby( - path, disable_prior=False, latex_dict=GWlatex_labels, + path, disable_prior_sampling=False, latex_dict=GWlatex_labels, complex_params=["matched_filter_snr", "optimal_snr"], **kwargs ): """Grab the parameters and samples in a bilby file @@ -20,7 +20,7 @@ def read_bilby( ---------- path: str path to the result file you wish to read in - disable_prior: Bool, optional + disable_prior_sampling: Bool, optional if True, do not collect prior samples from the `bilby` result file. Default False complex_params: list, optional @@ -35,8 +35,8 @@ def read_bilby( ) return _read_bilby( - path, disable_prior=disable_prior, latex_dict=latex_dict, - complex_params=complex_params, **kwargs + path, disable_prior_sampling=disable_prior_sampling, + latex_dict=latex_dict, complex_params=complex_params, **kwargs ) @@ -72,7 +72,7 @@ class Bilby(GWSingleAnalysisRead): ---------- path_to_results_file: str path to the results file that you wish to read in with `bilby`. - disable_prior: Bool, optional + disable_prior_sampling: Bool, optional if True, do not collect prior samples from the `bilby` result file. Default False disable_prior_conversion: Bool, optional @@ -264,7 +264,7 @@ class Bilby(GWSingleAnalysisRead): return strain_data @staticmethod - def _grab_data_from_bilby_file(path, disable_prior=False, **kwargs): + def _grab_data_from_bilby_file(path, disable_prior_sampling=False, **kwargs): """ Load the results file using the `bilby` library @@ -272,7 +272,8 @@ class Bilby(GWSingleAnalysisRead): The amplitude and angle are extracted here. """ return CoreBilby._grab_data_from_bilby_file( - path, function=read_bilby, disable_prior=disable_prior, + path, function=read_bilby, + disable_prior_sampling=disable_prior_sampling, **kwargs ) diff --git a/pesummary/gw/inputs.py b/pesummary/gw/inputs.py index 8ef012f7..4077f5b3 100644 --- a/pesummary/gw/inputs.py +++ b/pesummary/gw/inputs.py @@ -370,7 +370,7 @@ class _GWInput(_Input): if i in _opened.keys() and _opened[i] is not None: f = self._open_result_files[i] else: - f = GWRead(i, disable_prior=True) + f = GWRead(i, disable_prior_sampling=True) try: calibration_data = f.calibration_data_in_results_file except Exception: @@ -1322,7 +1322,8 @@ class IMRCTInput(_Input): def samples(self, samples): from pesummary.utils.samples_dict import MultiAnalysisSamplesDict self._read_samples = { - _label: GWRead(_path, disable_prior=True) for _label, _path in zip( + _label: GWRead(_path, disable_prior_sampling=True) for + _label, _path in zip( self.labels, self.result_files ) } diff --git a/pesummary/tests/read_test.py b/pesummary/tests/read_test.py index 1afdc36c..c9329aa5 100644 --- a/pesummary/tests/read_test.py +++ b/pesummary/tests/read_test.py @@ -654,7 +654,7 @@ class BilbyFile(BaseRead): """ for param, prior in self.result.priors["samples"].items(): assert isinstance(prior, np.ndarray) - f = read_function(self.path, disable_prior=True) + f = read_function(self.path, disable_prior_sampling=True) assert not len(f.priors["samples"]) f = read_function(self.path, nsamples_for_prior=200) params = list(f.priors["samples"].keys()) @@ -1558,7 +1558,7 @@ class TestGWJsonBilbyFile(GWBaseRead): assert "final_mass_source_non_evolved" in self.result.priors["samples"].keys() f = read_function(self.path, disable_prior_conversion=True) assert "final_mass_source_non_evolved" not in f.priors["samples"].keys() - f = read_function(self.path, disable_prior=True) + f = read_function(self.path, disable_prior_sampling=True) assert not len(f.priors["samples"]) f = read_function(self.path, nsamples_for_prior=200) params = list(f.priors["samples"].keys()) diff --git a/pesummary/utils/decorators.py b/pesummary/utils/decorators.py index 7813e057..ac30c657 100644 --- a/pesummary/utils/decorators.py +++ b/pesummary/utils/decorators.py @@ -269,3 +269,43 @@ def deprecation(warning): return func(*args, **kwargs) return wrapper_function return decorator + + +def deprecated_kwargs(deprecated): + """Print a warning indicating that a kwarg may be deprecated in future + releases + + Parameters + ---------- + deprecated: dict + dictionary of kwargs to be deprecated. The key should be the deprecated + kwarg and the value should be the new kwarg + + Examples + -------- + >>> @deprecated_kwargs({"n_samples": "nsamples"}) + ... def uniform_samples(nsamples=1000): + ... return np.random.uniform(size=nsamples) + ... + >>> np.random.seed(123456789) + >>> samples = uniform_samples(n_samples=2000) + pesummary/utils/decorators.py:302: UserWarning: n_samples will be deprecated + in future releases. Please use nsamples + "{}".format(_deprecated_kwarg, _new_kwarg) + >>> samples = uniform_samples(nsamples=2000) + """ + def decorator(func): + @functools.wraps(func) + def wrapper_function(*args, **kwargs): + import warnings + for _deprecated_kwarg, _new_kwarg in deprecated.items(): + if _deprecated_kwarg in kwargs.keys(): + warnings.warn( + "{} will be deprecated in future releases. Please use " + "{}".format(_deprecated_kwarg, _new_kwarg) + ) + val = kwargs.pop(_deprecated_kwarg) + kwargs[_new_kwarg] = val + return func(*args, **kwargs) + return wrapper_function + return decorator -- GitLab From 6116d1c3a06100efac549ccb8bc455f4933cddaf Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 20 Apr 2021 17:01:45 +0100 Subject: [PATCH 05/13] add extra kwargs to control the conversion kwargs --- pesummary/gw/file/formats/base_read.py | 27 ++++++++++++++++++-------- pesummary/gw/file/formats/bilby.py | 6 ++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/pesummary/gw/file/formats/base_read.py b/pesummary/gw/file/formats/base_read.py index ab3c2e85..18e067ae 100644 --- a/pesummary/gw/file/formats/base_read.py +++ b/pesummary/gw/file/formats/base_read.py @@ -78,7 +78,7 @@ def _add_log_likelihood(parameters, samples): def convert_injection_parameters( data, extra_kwargs={"sampler": {}, "meta_data": {}}, disable_convert=False, - sampled_parameters=None + sampled_parameters=None, **kwargs ): """Apply the conversion module to the injection data @@ -93,6 +93,8 @@ def convert_injection_parameters( sampled_parameters: list, optional optional list of sampled parameters. If there is no injection value for a given sampled parameter, add a 'nan' + **kwargs: dict, optional + all kwargs passed to the pesummary.gw.conversions.convert function """ import math @@ -113,7 +115,9 @@ def convert_injection_parameters( for i in nan_inds[::-1]: parameters.remove(parameters[i]) samples[0].remove(samples[0][i]) - inj_samples = convert(parameters, samples, extra_kwargs=extra_kwargs) + inj_samples = convert( + parameters, samples, extra_kwargs=extra_kwargs, **kwargs + ) if sampled_parameters is not None: for i in sampled_parameters: if i not in list(inj_samples.keys()): @@ -198,7 +202,10 @@ class GWRead(Read): if self.injection_parameters is not None: self.injection_parameters = self.convert_injection_parameters( self.injection_parameters, extra_kwargs=self.extra_kwargs, - disable_convert=kwargs.get("disable_injection_conversion", False) + disable_convert=kwargs.get("disable_injection_conversion", False), + **kwargs.get( + "injection_conversion_kwargs", {} + ) ) if self.priors is not None and len(self.priors): if self.priors["samples"] != {}: @@ -206,10 +213,12 @@ class GWRead(Read): self.priors["samples"] = self.convert_and_translate_prior_samples( priors, disable_convert=kwargs.get( "disable_prior_conversion", False - ) + ), **kwargs.get("prior_conversion_kwargs", {}) ) - def convert_and_translate_prior_samples(self, priors, disable_convert=False): + def convert_and_translate_prior_samples( + self, priors, disable_convert=False, **kwargs + ): """ """ default_parameters = list(priors.keys()) @@ -222,7 +231,7 @@ class GWRead(Read): ) if not disable_convert: return convert( - parameters, samples, extra_kwargs=self.extra_kwargs + parameters, samples, extra_kwargs=self.extra_kwargs, **kwargs ) return SamplesDict(parameters, samples) @@ -586,7 +595,7 @@ class GWSingleAnalysisRead(GWRead, SingleAnalysisRead): def convert_injection_parameters( self, data, extra_kwargs={"sampler": {}, "meta_data": {}}, - disable_convert=False + disable_convert=False, **kwargs ): """Apply the conversion module to the injection data @@ -598,10 +607,12 @@ class GWSingleAnalysisRead(GWRead, SingleAnalysisRead): optional kwargs to pass to the conversion module disable_convert: Bool, optional if True, do not convert injection parameters + **kwargs: dict, optional + all kwargs passed to the pesummary.gw.conversions.convert function """ return convert_injection_parameters( data, extra_kwargs=extra_kwargs, disable_convert=disable_convert, - sampled_parameters=self.parameters + sampled_parameters=self.parameters, **kwargs ) def to_lalinference(self, **kwargs): diff --git a/pesummary/gw/file/formats/bilby.py b/pesummary/gw/file/formats/bilby.py index 054c6ed0..a75dfff2 100644 --- a/pesummary/gw/file/formats/bilby.py +++ b/pesummary/gw/file/formats/bilby.py @@ -86,6 +86,12 @@ class Bilby(GWSingleAnalysisRead): add_zero_likelihood: Bool, optional if True assign 'log_likelihood=0' to every sample if no 'log_likelihood' samples can be found. Default True + injection_conversion_kwargs: dict, optional + optional kwargs to pass to the pesummary.gw.conversions.convert function + for the injection parameters + prior_conversion_kwargs: dict, optional + optional kwargs to pass to the pesummary.gw.conversions.convert function + for the prior posteriors Attributes ---------- -- GitLab From 687113cce8254fbbe73d891cf0c0f58a3574dd08 Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 20 Apr 2021 17:16:28 +0100 Subject: [PATCH 06/13] typo --- pesummary/gw/file/formats/bilby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pesummary/gw/file/formats/bilby.py b/pesummary/gw/file/formats/bilby.py index a75dfff2..f4fc3105 100644 --- a/pesummary/gw/file/formats/bilby.py +++ b/pesummary/gw/file/formats/bilby.py @@ -91,7 +91,7 @@ class Bilby(GWSingleAnalysisRead): for the injection parameters prior_conversion_kwargs: dict, optional optional kwargs to pass to the pesummary.gw.conversions.convert function - for the prior posteriors + for the prior distributions Attributes ---------- -- GitLab From 8a84b4f3bc238f596a353ba2837ea1b4ff906b44 Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Fri, 30 Apr 2021 00:07:21 +0100 Subject: [PATCH 07/13] adding generate_all_prior_samples, generate_all_posterior_samples, generate_all_injection_samples and convert methods --- pesummary/gw/file/formats/base_read.py | 309 +++++++++++++++------- pesummary/gw/file/formats/bilby.py | 9 + pesummary/gw/file/formats/default.py | 24 ++ pesummary/gw/file/formats/lalinference.py | 9 + pesummary/gw/file/formats/pesummary.py | 9 + 5 files changed, 261 insertions(+), 99 deletions(-) diff --git a/pesummary/gw/file/formats/base_read.py b/pesummary/gw/file/formats/base_read.py index b19836ef..239cc303 100644 --- a/pesummary/gw/file/formats/base_read.py +++ b/pesummary/gw/file/formats/base_read.py @@ -101,6 +101,10 @@ def convert_injection_parameters( if disable_convert: return data if all(math.isnan(data[i]) for i in data.keys()): + if sampled_parameters is not None: + for i in sampled_parameters: + if i not in list(data.keys()): + data[i] = float("nan") return data parameters = list(data.keys()) samples = [[data[i] for i in parameters]] @@ -163,6 +167,15 @@ class GWRead(Read): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values + convert: + convert either the posterior distributions, prior distribution and/or + the injection samples. """ def __init__(self, path_to_results_file, **kwargs): super(GWRead, self).__init__(path_to_results_file, **kwargs) @@ -199,41 +212,30 @@ class GWRead(Read): } ) super(GWRead, self).load(function, _data=data, **kwargs) - if self.injection_parameters is not None: - self.injection_parameters = self.convert_injection_parameters( - self.injection_parameters, extra_kwargs=self.extra_kwargs, - disable_convert=kwargs.get("disable_injection_conversion", False), - **kwargs.get( - "injection_conversion_kwargs", {} - ) - ) - if self.priors is not None and len(self.priors): - if self.priors["samples"] != {}: - priors = self.priors["samples"] - self.priors["samples"] = self.convert_and_translate_prior_samples( - priors, disable_convert=kwargs.get( - "disable_prior_conversion", False - ), **kwargs.get("prior_conversion_kwargs", {}) - ) - def convert_and_translate_prior_samples( - self, priors, disable_convert=False, **kwargs - ): - """ + def _convert_samples(self, parameters, samples, **kwargs): + """Convert a set of posterior samples via the conversion module + + Parameters + ---------- + parameters: list + list of parameter names + samples: 2d list + 2d list of samples where the columns correspond to the posterior + samples for parameters + **kwargs: dict + all kwargs passed to the conversion module """ - default_parameters = list(priors.keys()) - default_samples = [ - [priors[parameter][i] for parameter in default_parameters] for i - in range(len(priors[default_parameters[0]])) - ] - parameters, samples = self.translate_parameters( - default_parameters, default_samples - ) - if not disable_convert: - return convert( - parameters, samples, extra_kwargs=self.extra_kwargs, **kwargs - ) - return SamplesDict(parameters, samples) + from pesummary.gw.conversions import convert + + if "no_conversion" in kwargs.keys(): + no_conversion = kwargs.pop("no_conversion") + else: + no_conversion = False + if not no_conversion: + data = convert(parameters, samples, **kwargs) + return data + return def write(self, package="core", **kwargs): """Save the data to file @@ -270,6 +272,79 @@ class GWRead(Read): """ pass + def generate_all_prior_samples(self, priors, extra_kwargs, **kwargs): + """Generate all prior samples via the conversion module + + Parameters + ---------- + **kwargs: dict + all kwargs passed to the conversion module + """ + default_parameters = list(priors.keys()) + default_samples = np.array( + [priors[key] for key in default_parameters] + ).T.tolist() + parameters, samples = self.translate_parameters( + default_parameters, default_samples + ) + if "disable_prior_conversion" in kwargs.keys(): + kwargs["no_conversion"] = kwargs.get( + "disable_prior_conversion" + ) + if kwargs.get("no_conversion", False): + return SamplesDict(parameters, samples) + data = self._convert( + parameters, samples, extra_kwargs=extra_kwargs, + **kwargs + ) + return data + + def generate_all_posterior_samples( + self, parameters, samples, extra_kwargs, **kwargs + ): + """Generate all posterior samples via the conversion module + + Parameters + ---------- + **kwargs: dict + all kwargs passed to the conversion module + """ + kwargs.update({"return_dict": False}) + data = self._convert_samples( + parameters, samples, extra_kwargs=extra_kwargs, **kwargs + ) + return data + + def convert(self, quantity, **kwargs): + """Convert the stored samples + + Parameters + ---------- + quantity: str + The quantity you wish to convert. This can be 'posterior', 'prior', + 'injection' or 'all' + **kwargs: dict + all kwargs passed to the conversion module + """ + if quantity.lower() == "posterior": + logger.info("Converting the posterior samples") + self.generate_all_posterior_samples(**kwargs) + elif quantity.lower() == "prior": + logger.info("Converting the prior samples") + self.generate_all_prior_samples(**kwargs) + elif quantity.lower() == "injection": + logger.info("Converting the injection samples") + self.generate_all_injection_samples(**kwargs) + elif quantity.lower() == "all": + for _quantity in ["posterior", "prior", "injection"]: + self.convert(_quantity, **kwargs) + else: + raise ValueError( + "Unrecognised quantity '{}'. Please select either " + "'posterior', 'prior', 'injection' or 'all'".format(quantity) + ) + return + @staticmethod def check_for_calibration_data(function, path_to_results_file): """Check to see if there is any calibration data in the results file @@ -539,6 +614,12 @@ class GWSingleAnalysisRead(GWRead, SingleAnalysisRead): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values """ def __init__(self, *args, **kwargs): super(GWSingleAnalysisRead, self).__init__(*args, **kwargs) @@ -568,6 +649,23 @@ class GWSingleAnalysisRead(GWRead, SingleAnalysisRead): """ return _add_log_likelihood(parameters, samples) + def generate_all_prior_samples(self, **kwargs): + """Generate all prior samples via the conversion module + + Parameters + ---------- + **kwargs: dict + all kwargs passed to the conversion module + """ + if self.priors is not None and len(self.priors): + if self.priors["samples"] != {}: + data = super(GWSingleAnalysisRead, self).generate_all_prior_samples( + self.priors["samples"], self.extra_kwargs, **kwargs + ) + if data is not None: + self.priors["samples"] = data + return + def generate_all_posterior_samples(self, **kwargs): """Generate all posterior samples via the conversion module @@ -576,43 +674,32 @@ class GWSingleAnalysisRead(GWRead, SingleAnalysisRead): **kwargs: dict all kwargs passed to the conversion module """ - if "no_conversion" in kwargs.keys(): - no_conversion = kwargs.pop("no_conversion") - else: - no_conversion = False - if not no_conversion: - from pesummary.gw.conversions import convert - - data = convert( - self.parameters, self.samples, extra_kwargs=self.extra_kwargs, - return_dict=False, **kwargs - ) + data = super(GWSingleAnalysisRead, self).generate_all_posterior_samples( + self.parameters, self.samples, self.extra_kwargs, **kwargs + ) + if data is not None: self.parameters = data[0] self.converted_parameters = self.parameters.added self.samples = data[1] if kwargs.get("return_kwargs", False): self.extra_kwargs = data[2] - def convert_injection_parameters( - self, data, extra_kwargs={"sampler": {}, "meta_data": {}}, - disable_convert=False, **kwargs - ): - """Apply the conversion module to the injection data + def generate_all_injection_samples(self, **kwargs): + """Generate all injection samples via the conversion module Parameters ---------- - data: dict - dictionary of injection data keyed by the parameter - extra_kwargs: dict, optional - optional kwargs to pass to the conversion module - disable_convert: Bool, optional - if True, do not convert injection parameters - **kwargs: dict, optional - all kwargs passed to the pesummary.gw.conversions.convert function + **kwargs: dict + all kwargs passed to the conversion module """ - return convert_injection_parameters( - data, extra_kwargs=extra_kwargs, disable_convert=disable_convert, - sampled_parameters=self.parameters, **kwargs + if "disable_injection_conversion" in kwargs.keys(): + kwargs["no_conversion"] = kwargs.get( + "disable_injection_conversion" + ) + self.injection_parameters = convert_injection_parameters( + self.injection_parameters, extra_kwargs=self.extra_kwargs, + disable_convert=kwargs.get("no_conversion", False), + **kwargs ) def to_lalinference(self, **kwargs): @@ -687,31 +774,6 @@ class GWMultiAnalysisRead(GWRead, MultiAnalysisRead): except (KeyError, AttributeError): self.skymap = self.data["skymap"] - def convert_and_translate_prior_samples(self, priors, disable_convert=False): - """ - """ - from pesummary.utils.samples_dict import MultiAnalysisSamplesDict - - mydict = {} - for num, label in enumerate(self.labels): - if label in priors.keys() and len(priors[label]): - default_parameters = list(priors[label].keys()) - default_samples = np.array( - [priors[label][_param] for _param in default_parameters] - ).T - parameters, samples = self.translate_parameters( - [default_parameters], [default_samples] - ) - if not disable_convert: - mydict[label] = convert( - parameters[0], samples[0], extra_kwargs=self.extra_kwargs[num] - ) - else: - mydict[label] = SamplesDict(parameters[0], samples[0]) - else: - mydict[label] = {} - return MultiAnalysisSamplesDict(mydict) - def check_for_log_likelihood(self, parameters): if all("log_likelihood" in p for p in parameters): return True @@ -745,14 +807,52 @@ class GWMultiAnalysisRead(GWRead, MultiAnalysisRead): samples_logl.append(ss) return parameters_logl, samples_logl + def generate_all_prior_samples(self, labels=None, **kwargs): + """Apply the conversion module to the prior samples + + Parameters + ---------- + labels: list, optional + list of analyses to consider + **kwargs: dict + all kwargs passed to the conversion module + """ + from pesummary.utils.samples_dict import MultiAnalysisSamplesDict + + mydict = {} + for num, label in enumerate(self.labels): + _kwargs = kwargs.copy() + if labels is not None and label not in labels: + _kwargs["no_conversion"] = True + if label not in self.priors.keys() or not len(self.priors[label]): + mydict[label] = {} + continue + mydict[label] = ( + super(GWMultiAnalysisRead, self).generate_all_prior_samples( + self.priors[label]["samples"], self.extra_kwargs[num], + **_kwargs + ) + ) + elif label in self.priors.keys() and len(self.priors[label]): + mydict[label] = ( + super(GWMultiAnalysisRead, self).generate_all_prior_samples( + self.priors[label]["samples"], self.extra_kwargs[num], + **kwargs + ) + ) + else: + mydict[label] = {} + return MultiAnalysisSamplesDict(mydict) + def generate_all_posterior_samples(self, labels=None, **conversion_kwargs): + """Apply the conversion module to the posterior distributions + """ if "no_conversion" in conversion_kwargs.keys(): no_conversion = conversion_kwargs.pop("no_conversion") else: no_conversion = False if no_conversion: return - from pesummary.gw.conversions import convert converted_params, converted_samples, converted_kwargs = [], [], [] _converted_params = [] @@ -773,9 +873,8 @@ class GWMultiAnalysisRead(GWRead, MultiAnalysisRead): if _conversion_kwargs.get("evolve_spins", False): if not _conversion_kwargs.get("return_kwargs", False): _conversion_kwargs["return_kwargs"] = True - data = convert( - param, samples, extra_kwargs=kwargs, return_dict=False, - **_conversion_kwargs + data = super(GWMultiAnalysisRead, self).generate_all_posterior_samples( + param, samples, kwargs, **_conversion_kwargs ) converted_params.append(data[0]) _converted_params.append(data[0].added) @@ -792,22 +891,34 @@ class GWMultiAnalysisRead(GWRead, MultiAnalysisRead): ) } - def convert_injection_parameters( - self, data, extra_kwargs={"sampler": {}, "meta_data": {}}, - disable_convert=False - ): + def generate_all_injection_samples(self, labels=None, **kwargs): """Apply the conversion module to the injection data + + Parameters + ---------- + labels: list, optional + list of analyses to consider + **kwargs: dict + all kwargs passed to the conversion module """ + if "disable_injection_conversion" in kwargs.keys(): + kwargs["no_conversion"] = kwargs.get( + "disable_injection_conversion" + ) + data = self.injection_parameters for num, label in enumerate(self.labels): + _kwargs = kwargs.copy() + if labels is not None and label not in labels: + _kwargs["no_conversion"] = True _identifier = label - if isinstance(data, dict): - _data = data[label] + if isinstance(self.injection_parameters, dict): + _data = self.injection_parameters[label] else: - _data = data[num] + _data = self.injection_parameters[num] _identifier = num data[_identifier] = convert_injection_parameters( - _data, extra_kwargs=extra_kwargs[num], - disable_convert=disable_convert, + _data, extra_kwargs=self.extra_kwargs[num], + disable_convert=_kwargs.get("no_conversion"), sampled_parameters=self.parameters[num] ) - return data + self.injection_parameters = data diff --git a/pesummary/gw/file/formats/bilby.py b/pesummary/gw/file/formats/bilby.py index f4fc3105..7bd70c14 100644 --- a/pesummary/gw/file/formats/bilby.py +++ b/pesummary/gw/file/formats/bilby.py @@ -128,6 +128,15 @@ class Bilby(GWSingleAnalysisRead): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values + convert: + convert either the posterior distributions, prior distribution and/or + the injection samples. """ def __init__(self, path_to_results_file, **kwargs): super(Bilby, self).__init__(path_to_results_file, **kwargs) diff --git a/pesummary/gw/file/formats/default.py b/pesummary/gw/file/formats/default.py index 841b5654..378260d6 100644 --- a/pesummary/gw/file/formats/default.py +++ b/pesummary/gw/file/formats/default.py @@ -53,6 +53,15 @@ class SingleAnalysisDefault(GWSingleAnalysisRead): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values + convert: + convert either the posterior distributions, prior distribution and/or + the injection samples. """ def __init__(self, *args, _data=None, **kwargs): super(SingleAnalysisDefault, self).__init__(*args, **kwargs) @@ -96,6 +105,15 @@ class MultiAnalysisDefault(GWMultiAnalysisRead): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values + convert: + convert either the posterior distributions, prior distribution and/or + the injection samples. """ def __init__(self, *args, _data=None, **kwargs): super(MultiAnalysisDefault, self).__init__(*args, **kwargs) @@ -141,6 +159,12 @@ class Default(CoreDefault): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values """ def load_map(self): _load_map = super(Default, self).load_map(self) diff --git a/pesummary/gw/file/formats/lalinference.py b/pesummary/gw/file/formats/lalinference.py index 65df1a00..47f8b80b 100644 --- a/pesummary/gw/file/formats/lalinference.py +++ b/pesummary/gw/file/formats/lalinference.py @@ -126,6 +126,15 @@ class LALInference(GWSingleAnalysisRead): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values + convert: + convert either the posterior distributions, prior distribution and/or + the injection samples. """ def __init__(self, path_to_results_file, injection_file=None, **kwargs): super(LALInference, self).__init__(path_to_results_file, **kwargs) diff --git a/pesummary/gw/file/formats/pesummary.py b/pesummary/gw/file/formats/pesummary.py index c47e2852..c6f7198c 100644 --- a/pesummary/gw/file/formats/pesummary.py +++ b/pesummary/gw/file/formats/pesummary.py @@ -108,6 +108,15 @@ class PESummary(GWMultiAnalysisRead, CorePESummary): generate_all_posterior_samples: generate all posterior distributions that may be derived from sampled distributions + generate_all_prior_samples: + generate all prior distributions that may be derived from the + stored prior distributions + generate_all_injection_samples: + generate all injection values that may be derived from the stored + injection values + convert: + convert either the posterior distributions, prior distribution and/or + the injection samples. """ def __init__(self, path_to_results_file, **kwargs): super(PESummary, self).__init__( -- GitLab From d87fe4ee7270d5df693e87f40595173f5c156ebc Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Fri, 30 Apr 2021 13:30:31 +0100 Subject: [PATCH 08/13] call the convert method rather than generate_all_posterior_samples --- pesummary/core/file/formats/base_read.py | 5 +++++ pesummary/core/inputs.py | 4 ++-- pesummary/utils/samples_dict.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pesummary/core/file/formats/base_read.py b/pesummary/core/file/formats/base_read.py index 43f209b1..6322d728 100644 --- a/pesummary/core/file/formats/base_read.py +++ b/pesummary/core/file/formats/base_read.py @@ -317,6 +317,11 @@ class Read(object): "Unable to find a posterior samples table in '{}'".format(path) ) + def convert(self, *args, **kwargs): + """Empty function + """ + pass + def generate_all_posterior_samples(self, **kwargs): """Empty function """ diff --git a/pesummary/core/inputs.py b/pesummary/core/inputs.py index 1b8f06b4..18586c4f 100644 --- a/pesummary/core/inputs.py +++ b/pesummary/core/inputs.py @@ -126,7 +126,7 @@ class _Input(object): labels = compare if not f.mcmc_samples: - f.generate_all_posterior_samples(labels=labels, **kwargs) + f.convert(labels=labels, **kwargs) parameters = f.parameters if not f.mcmc_samples: @@ -253,7 +253,7 @@ class _Input(object): if nsamples is not None: f.downsample(nsamples) - f.generate_all_posterior_samples(**kwargs) + f.convert(**kwargs) if injection: f.add_injection_parameters_from_file( injection, conversion_kwargs=kwargs diff --git a/pesummary/utils/samples_dict.py b/pesummary/utils/samples_dict.py index 468f41be..8e787c7d 100644 --- a/pesummary/utils/samples_dict.py +++ b/pesummary/utils/samples_dict.py @@ -72,6 +72,9 @@ class SamplesDict(Dict): Remove the first N samples from each distribution plot: Generate a plot based on the posterior samples stored + convert: + Convert the posterior samples in the SamplesDict object according to + a conversion function generate_all_posterior_samples: Convert the posterior samples in the SamplesDict object according to a conversion function @@ -393,6 +396,21 @@ class SamplesDict(Dict): """ return super(SamplesDict, self).plot(*args, type=type, **kwargs) + def convert(self, function=None, **kwargs): + """Convert samples stored in the SamplesDict according to a conversion + function + + Parameters + ---------- + function: func, optional + function to use when converting posterior samples. Must take a + dictionary as input and return a dictionary of converted posterior + samples. Default `pesummary.gw.conversions.convert + **kwargs: dict, optional + All additional kwargs passed to function + """ + return self.generate_all_posterior_samples(function=function, **kwargs) + def generate_all_posterior_samples(self, function=None, **kwargs): """Convert samples stored in the SamplesDict according to a conversion function -- GitLab From 2936db3bf1b1732e0993d19cd7a42f61f67712dc Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Fri, 30 Apr 2021 14:03:35 +0100 Subject: [PATCH 09/13] pass quantity --- pesummary/core/inputs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pesummary/core/inputs.py b/pesummary/core/inputs.py index 18586c4f..c5bef65d 100644 --- a/pesummary/core/inputs.py +++ b/pesummary/core/inputs.py @@ -126,7 +126,7 @@ class _Input(object): labels = compare if not f.mcmc_samples: - f.convert(labels=labels, **kwargs) + f.convert("all", labels=labels, **kwargs) parameters = f.parameters if not f.mcmc_samples: @@ -253,7 +253,7 @@ class _Input(object): if nsamples is not None: f.downsample(nsamples) - f.convert(**kwargs) + f.convert("all", **kwargs) if injection: f.add_injection_parameters_from_file( injection, conversion_kwargs=kwargs -- GitLab From ff6740cb1500fa64893f411dbd4216622d79b710 Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Fri, 30 Apr 2021 20:42:26 +0100 Subject: [PATCH 10/13] typo --- pesummary/gw/file/formats/base_read.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pesummary/gw/file/formats/base_read.py b/pesummary/gw/file/formats/base_read.py index 239cc303..00e8d238 100644 --- a/pesummary/gw/file/formats/base_read.py +++ b/pesummary/gw/file/formats/base_read.py @@ -213,7 +213,7 @@ class GWRead(Read): ) super(GWRead, self).load(function, _data=data, **kwargs) - def _convert_samples(self, parameters, samples, **kwargs): + def _convert(self, parameters, samples, **kwargs): """Convert a set of posterior samples via the conversion module Parameters @@ -310,7 +310,7 @@ class GWRead(Read): all kwargs passed to the conversion module """ kwargs.update({"return_dict": False}) - data = self._convert_samples( + data = self._convert( parameters, samples, extra_kwargs=extra_kwargs, **kwargs ) return data -- GitLab From 5c1f3d73942ca8c73f270e9c48c1814567244be0 Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Mon, 3 May 2021 16:46:58 +0100 Subject: [PATCH 11/13] typo --- pesummary/gw/file/formats/base_read.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pesummary/gw/file/formats/base_read.py b/pesummary/gw/file/formats/base_read.py index 00e8d238..bae2cf27 100644 --- a/pesummary/gw/file/formats/base_read.py +++ b/pesummary/gw/file/formats/base_read.py @@ -696,6 +696,8 @@ class GWSingleAnalysisRead(GWRead, SingleAnalysisRead): kwargs["no_conversion"] = kwargs.get( "disable_injection_conversion" ) + if self.injection_parameters is None: + return self.injection_parameters = convert_injection_parameters( self.injection_parameters, extra_kwargs=self.extra_kwargs, disable_convert=kwargs.get("no_conversion", False), @@ -916,6 +918,8 @@ class GWMultiAnalysisRead(GWRead, MultiAnalysisRead): else: _data = self.injection_parameters[num] _identifier = num + if _data is None: + continue data[_identifier] = convert_injection_parameters( _data, extra_kwargs=self.extra_kwargs[num], disable_convert=_kwargs.get("no_conversion"), -- GitLab From d8dd407798b68482c2df3a9f05767303e045106a Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 8 Jun 2021 17:06:36 +0100 Subject: [PATCH 12/13] do not return kwargs for priors and injections --- pesummary/gw/file/formats/base_read.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pesummary/gw/file/formats/base_read.py b/pesummary/gw/file/formats/base_read.py index bae2cf27..c6bd8c3d 100644 --- a/pesummary/gw/file/formats/base_read.py +++ b/pesummary/gw/file/formats/base_read.py @@ -119,6 +119,7 @@ def convert_injection_parameters( for i in nan_inds[::-1]: parameters.remove(parameters[i]) samples[0].remove(samples[0][i]) + kwargs["return_kwargs"] = False inj_samples = convert( parameters, samples, extra_kwargs=extra_kwargs, **kwargs ) @@ -291,6 +292,7 @@ class GWRead(Read): kwargs["no_conversion"] = kwargs.get( "disable_prior_conversion" ) + kwargs["return_kwargs"] = False if kwargs.get("no_conversion", False): return SamplesDict(parameters, samples) data = self._convert( -- GitLab From 00a9527d098bcbdab86bfe803a18f5d441d9b688 Mon Sep 17 00:00:00 2001 From: Charlie Hoy Date: Tue, 8 Jun 2021 18:36:37 +0100 Subject: [PATCH 13/13] typo --- pesummary/gw/file/formats/base_read.py | 5 +++++ pesummary/gw/file/formats/bilby.py | 3 +++ pesummary/tests/read_test.py | 2 ++ 3 files changed, 10 insertions(+) diff --git a/pesummary/gw/file/formats/base_read.py b/pesummary/gw/file/formats/base_read.py index c6bd8c3d..5176d664 100644 --- a/pesummary/gw/file/formats/base_read.py +++ b/pesummary/gw/file/formats/base_read.py @@ -292,6 +292,11 @@ class GWRead(Read): kwargs["no_conversion"] = kwargs.get( "disable_prior_conversion" ) + if hasattr(self, "disable_prior_conversion"): + if self.disable_prior_conversion: + kwargs["no_conversion"] = kwargs.get( + "disable_prior_conversion" + ) kwargs["return_kwargs"] = False if kwargs.get("no_conversion", False): return SamplesDict(parameters, samples) diff --git a/pesummary/gw/file/formats/bilby.py b/pesummary/gw/file/formats/bilby.py index 7bd70c14..b3aca423 100644 --- a/pesummary/gw/file/formats/bilby.py +++ b/pesummary/gw/file/formats/bilby.py @@ -141,6 +141,9 @@ class Bilby(GWSingleAnalysisRead): def __init__(self, path_to_results_file, **kwargs): super(Bilby, self).__init__(path_to_results_file, **kwargs) self.load(self._grab_data_from_bilby_file, **kwargs) + self.disable_prior_conversion = kwargs.get( + "disable_prior_conversion", False + ) @staticmethod def grab_priors(bilby_object, nsamples=5000): diff --git a/pesummary/tests/read_test.py b/pesummary/tests/read_test.py index c9329aa5..fe028d5f 100644 --- a/pesummary/tests/read_test.py +++ b/pesummary/tests/read_test.py @@ -1555,8 +1555,10 @@ class TestGWJsonBilbyFile(GWBaseRead): assert "final_mass_source_non_evolved" not in self.result.parameters for param, prior in self.result.priors["samples"].items(): assert isinstance(prior, np.ndarray) + self.result.generate_all_prior_samples() assert "final_mass_source_non_evolved" in self.result.priors["samples"].keys() f = read_function(self.path, disable_prior_conversion=True) + self.result.generate_all_prior_samples() assert "final_mass_source_non_evolved" not in f.priors["samples"].keys() f = read_function(self.path, disable_prior_sampling=True) assert not len(f.priors["samples"]) -- GitLab