diff --git a/docs/conf.py b/docs/conf.py index 845c6a5..623e5c9 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -142,12 +142,12 @@ "icon": "https://img.shields.io/pypi/v/spey?style=plastic", "type": "url", }, - { - "name": "PyPI", - "url": "https://pypi.org/project/spey/", - "icon": "https://img.shields.io/pypi/dm/spey?style=plastic", - "type": "url", - }, + # { + # "name": "PyPI", + # "url": "https://pypi.org/project/spey/", + # "icon": "https://img.shields.io/pypi/dm/spey?style=plastic", + # "type": "url", + # }, ], } diff --git a/docs/plugins.rst b/docs/plugins.rst index 4618392..db6b8bf 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -74,7 +74,7 @@ given as where :math:`\mu, \theta` are the parameter of interest (signal strength) and nuisance parameters, the signal and background yields are given as :math:`n_s^i` and :math:`n_b^i\pm\sigma_b^i` respectively. -Additionally, absolute uncertainties are modelled as Gaussian distributions. This model can be +Additionally, absolute uncertainties are modeled as Gaussian distributions. This model can be used as follows .. code-block:: python3 @@ -107,7 +107,7 @@ This particular example implements a two-bin histogram with uncorrelated bins. T >>> statistical_model.exclusion_confidence_level() >>> # [0.9701795436411219] -For all the properties of :obj:`~spey.StatisticalModel` class we refer the reader to the corresponding +For all the properties of :obj:`~spey.StatisticalModel` class, we refer the reader to the corresponding API description. ``'default_pdf.correlated_background'`` diff --git a/src/spey/__init__.py b/src/spey/__init__.py index 7f95c8a..4bd7fad 100644 --- a/src/spey/__init__.py +++ b/src/spey/__init__.py @@ -14,7 +14,7 @@ from spey.interface.statistical_model import StatisticalModel, statistical_model_wrapper from spey.system import logger from spey.system.exceptions import PluginError -from spey.system.webutils import get_bibtex, check_updates +from spey.system.webutils import get_bibtex, check_updates, ConnectionError from ._version import __version__ from .about import about @@ -257,7 +257,7 @@ def get_backend_metadata(name: Text) -> Dict[Text, Any]: ) -def get_backend_bibtex(name: Text) -> List[Text]: +def get_backend_bibtex(name: Text) -> Dict[Text, List[Text]]: """ Retreive BibTex entry for backend plug-in if available. @@ -273,47 +273,63 @@ def get_backend_bibtex(name: Text) -> List[Text]: that prescribes likelihood function. Returns: - ``List[Text]``: - BibTex entries for the backend. + ``Dict[Text, List[Text]]``: + BibTex entries for the backend. Keywords include inspire, doi.org and zenodo. + + .. versionchanged:: 0.1.7 + + In the previous version, function was returning ``List[Text]`` now it returns + a ``Dict[Text, List[Text]]`` indicating the source of BibTeX entry. + """ # pylint: disable=import-outside-toplevel, W1203 - txt = [] + out = {"inspire": [], "doi.org": [], "zenodo": []} meta = get_backend_metadata(name) - for arxiv_id in meta.get("arXiv", []): - tmp = get_bibtex("inspire/arxiv", arxiv_id) - if tmp != "": - txt.append(textwrap.indent(tmp, " " * 4)) - else: - log.debug(f"Can not find {arxiv_id} in Inspire") - for doi in meta.get("doi", []): - tmp = get_bibtex("inspire/doi", doi) - if tmp == "": - log.debug(f"Can not find {doi} in Inspire, looking at doi.org") - tmp = get_bibtex("doi", doi) + try: + for arxiv_id in meta.get("arXiv", []): + tmp = get_bibtex("inspire/arxiv", arxiv_id) if tmp != "": - txt.append(tmp) + out["inspire"].append(textwrap.indent(tmp, " " * 4)) else: - log.debug(f"Can not find {doi} in doi.org") - else: - txt.append(textwrap.indent(tmp, " " * 4)) - for zenodo_id in meta.get("zenodo", []): - tmp = get_bibtex("zenodo", zenodo_id) - if tmp != "": - txt.append(textwrap.indent(tmp, " " * 4)) - else: - log.debug(f"{zenodo_id} is not a valid zenodo identifier") + log.debug(f"Can not find {arxiv_id} in Inspire") + for doi in meta.get("doi", []): + tmp = get_bibtex("inspire/doi", doi) + if tmp == "": + log.debug(f"Can not find {doi} in Inspire, looking at doi.org") + tmp = get_bibtex("doi", doi) + if tmp != "": + out["doi.org"].append(tmp) + else: + log.debug(f"Can not find {doi} in doi.org") + else: + out["inspire"].append(textwrap.indent(tmp, " " * 4)) + for zenodo_id in meta.get("zenodo", []): + tmp = get_bibtex("zenodo", zenodo_id) + if tmp != "": + out["zenodo"].append(textwrap.indent(tmp, " " * 4)) + else: + log.debug(f"{zenodo_id} is not a valid zenodo identifier") + except ConnectionError as err: + log.error("Can not connect to the internet. Please check your connection.") + log.debug(str(err)) + return out - return txt + return out def cite() -> List[Text]: """Retreive BibTex information for Spey""" - arxiv = textwrap.indent(get_bibtex("inspire/arxiv", "2307.06996"), " " * 4) - zenodo = get_bibtex("zenodo", "10156353") - linker = re.search("@software{(.+?),\n", zenodo).group(1) - zenodo = textwrap.indent(zenodo.replace(linker, "spey_zenodo"), " " * 4) - return arxiv + "\n\n" + zenodo + try: + arxiv = textwrap.indent(get_bibtex("inspire/arxiv", "2307.06996"), " " * 4) + zenodo = get_bibtex("zenodo", "10156353") + linker = re.search("@software{(.+?),\n", zenodo).group(1) + zenodo = textwrap.indent(zenodo.replace(linker, "spey_zenodo"), " " * 4) + return arxiv + "\n\n" + zenodo + except ConnectionError as err: + log.error("Can not connect to the internet. Please check your connection.") + log.debug(str(err)) + return "" if os.environ.get("SPEY_CHECKUPDATE", "ON").upper() != "OFF": diff --git a/src/spey/system/webutils.py b/src/spey/system/webutils.py index b8e7058..b67c5ac 100644 --- a/src/spey/system/webutils.py +++ b/src/spey/system/webutils.py @@ -7,13 +7,17 @@ log = logging.getLogger("Spey") -__all__ = ["get_bibtex", "check_updates"] +__all__ = ["get_bibtex", "check_updates", "ConnectionError"] def __dir__(): return __all__ +class ConnectionError(Exception): + """No internet connection""" + + def get_bibtex( home: Literal["inspire/arxiv", "inspire/doi", "doi", "zenodo"], identifier: Text, @@ -60,6 +64,10 @@ def get_bibtex( return "" response.encoding = "utf-8" return response.text + except (requests.ConnectionError, requests.ConnectTimeout) as err: + raise ConnectionError( + "Can not retreive BibTeX information: No internet connection." + ) from err except Exception as err: # pylint: disable=W0718 log.debug(str(err)) return "" @@ -98,12 +106,12 @@ def check_updates() -> None: log.debug(f"Curernt version {version}, latest version {pypi_version}.") if Version(version) < Version(pypi_version): log.warning( - f"An update is available. Current version of spey is {version}, " - f"available version is {pypi_version}." + f"A newer version ({pypi_version}) of Spey is available. " + f"Current version is {version}." ) elif Version(version) > Version(pypi_version): log.warning( - f"An unstable version of spey ({version}) is being used." + f"An unstable version of Spey ({version}) is being used." f" Latest stable version is {pypi_version}." )