Coverage for pesummary/cli/summaryextract.py: 95.5%
22 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-09 22:34 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-09 22:34 +0000
1#! /usr/bin/env python
3# Licensed under an MIT style license -- see LICENSE.md
5from pesummary.core.cli.actions import CheckFilesExistAction
6from pesummary.core.cli.parser import ArgumentParser as _ArgumentParser
7from pesummary.utils.utils import logger
8from pesummary.io import read, available_formats
10__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"]
11__doc__ = """This executable is used to extract a set of posterior samples
12from a file containing more than one set of analyses, for instance a PESummary
13metafile"""
16class ArgumentParser(_ArgumentParser):
17 def _pesummary_options(self):
18 options = super(ArgumentParser, self)._pesummary_options()
19 options.update(
20 {
21 "--label": {
22 "required": True,
23 "help": "Analysis that you wish to extract from the file"
24 },
25 "--samples": {
26 "required": True,
27 "short": "-s",
28 "action": CheckFilesExistAction,
29 "help": (
30 "Path to posterior samples file containing more than "
31 "one analysis"
32 )
33 },
34 "--file_format": {
35 "type": str,
36 "default": "dat",
37 "help": "Format of output file",
38 "choices": available_formats()[1]
39 },
40 "--filename": {
41 "type": str,
42 "help": "Name of the output file"
43 },
44 "--outdir": {
45 "type": str,
46 "default": "./",
47 "help": "Directory to save the file",
48 },
49 }
50 )
51 return options
54def main(args=None):
55 """Top level interface for `summaryextract`
56 """
57 _parser = ArgumentParser(description=__doc__)
58 _parser.add_known_options_to_parser(
59 ["--label", "--samples", "--file_format", "--filename", "--outdir"]
60 )
61 opts, unknown = _parser.parse_known_args(args=args)
62 logger.info("Loading file: '{}'".format(opts.samples))
63 f = read(
64 opts.samples, disable_prior=True, disable_injection_conversion=True
65 )
66 posterior_samples = f.samples_dict
67 logger.info("Writing analysis: '{}' to file".format(opts.label))
68 posterior_samples.write(
69 file_format=opts.file_format, labels=[opts.label], outdir=opts.outdir,
70 filename=opts.filename
71 )
74if __name__ == "__main__":
75 main()