Skip to content

Commit

Permalink
Merge pull request #35 from SpeysideHEP/connection
Browse files Browse the repository at this point in the history
Connection
  • Loading branch information
jackaraz authored Feb 14, 2024
2 parents 857ec10 + a7a6dce commit 4eb6a25
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
12 changes: 6 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
# },
],
}

Expand Down
4 changes: 2 additions & 2 deletions docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'``
Expand Down
80 changes: 48 additions & 32 deletions src/spey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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":
Expand Down
16 changes: 12 additions & 4 deletions src/spey/system/webutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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}."
)

Expand Down

0 comments on commit 4eb6a25

Please sign in to comment.