Skip to content
Snippets Groups Projects
Commit 0cf93ff6 authored by Colm Talbot's avatar Colm Talbot
Browse files

Merge branch 'env_package_list' into 'master'

Add env_package_list function to capture full environment listing

See merge request !1166
parents 7ee82430 4b0845a3
No related branches found
No related tags found
1 merge request!1166Add env_package_list function to capture full environment listing
Pipeline #468731 passed
......@@ -6,7 +6,7 @@ import bilby
from bilby.bilby_mcmc import Bilby_MCMC
from ..prior import DeltaFunction, PriorDict
from ..utils import command_line_args, loaded_modules_dict, logger
from ..utils import command_line_args, env_package_list, loaded_modules_dict, logger
from . import proposal
from .base_sampler import Sampler, SamplingMarginalisedParameterError
from .cpnest import Cpnest
......@@ -175,6 +175,7 @@ def run_sampler(
likelihood.outdir = outdir
meta_data["likelihood"] = likelihood.meta_data
meta_data["loaded_modules"] = loaded_modules_dict()
meta_data["environment_packages"] = env_package_list(as_dataframe=True)
if command_line_args.bilby_zero_likelihood_mode:
from bilby.core.likelihood import ZeroLikelihood
......
import json
import logging
from pathlib import Path
import subprocess
import sys
logger = logging.getLogger('bilby')
......@@ -70,3 +72,60 @@ def loaded_modules_dict():
if "." not in str(key):
vdict[key] = str(getattr(sys.modules[key], "__version__", "N/A"))
return vdict
def env_package_list(as_dataframe=False):
"""Get the list of packages installed in the system prefix.
If it is detected that the system prefix is part of a Conda environment,
a call to ``conda list --prefix {sys.prefix}`` will be made, otherwise
the call will be to ``{sys.executable} -m pip list installed``.
Parameters
----------
as_dataframe: bool
return output as a `pandas.DataFrame`
Returns
-------
pkgs : `list` of `dict`, or `pandas.DataFrame`
If ``as_dataframe=False`` is given, the output is a `list` of `dict`,
one for each package, at least with ``'name'`` and ``'version'`` keys
(more if `conda` is used).
If ``as_dataframe=True`` is given, the output is a `DataFrame`
created from the `list` of `dicts`.
"""
prefix = sys.prefix
# if a conda-meta directory exists, this is a conda environment, so
# use conda to print the package list
if (Path(prefix) / "conda-meta").is_dir():
pkgs = json.loads(subprocess.check_output([
"conda",
"list",
"--prefix", prefix,
"--json"
]))
# otherwise try and use Pip
else:
try:
import pip # noqa: F401
except ModuleNotFoundError: # no pip?
# not a conda environment, and no pip, so just return
# the list of loaded modules
modules = loaded_modules_dict()
pkgs = [{"name": x, "version": y} for x, y in modules.items()]
else:
pkgs = json.loads(subprocess.check_output([
sys.executable,
"-m", "pip",
"list", "installed",
"--format", "json",
]))
# convert to recarray for storage
if as_dataframe:
from pandas import DataFrame
return DataFrame(pkgs)
return pkgs
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment