Skip to content

Commit

Permalink
Allow use of xedocs context configs (#1125)
Browse files Browse the repository at this point in the history
* Adds option to context creation function that loads config from xedocs

* Adds context method that applies configs from xedocs

* update docs

Co-authored-by: Yossi Mosbacher <joe.mosbacher>
  • Loading branch information
jmosbacher authored Jan 5, 2023
1 parent 379e63a commit 07f90c3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Straxen is the analysis framework for XENONnT, built on top of the generic `stra
config_storage
online_monitor
cmt
context_configs
url_configs
scada_interface
tutorials/ScadaInterfaceExample.ipynb
Expand Down
23 changes: 23 additions & 0 deletions docs/source/xedocs_configs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Xedocs context configs
======================

The xedocs package manages a versioned collection of context configs.
This is useful for applying a predefined set of configs to the context in a reproducible way.
Straxen registers a context method `context.apply_xedocs_configs` which can be used to load and apply
the configs from a given version. Example:

.. code-block:: python
import straxen
st = straxen.contexts.xenonnt()
st.apply_xedocs_configs('v9')
The straxen context builder function `straxen.contexts.xenonnt` also accepts a `xedocs_version` argument,
if passed, straxen will attempt to load all configs from the given version from the xedocs context_configs database.
Example:

.. code-block:: python
import straxen
st = straxen.contexts.xenonnt(xedocs_version='v9')
2 changes: 2 additions & 0 deletions extra_requirements/requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ git+https://github.com/XENONnT/ax_env
wfsim==1.0.2
nbmake
pytest-xdist
xedocs==0.2.4

7 changes: 6 additions & 1 deletion straxen/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,18 @@
##


def xenonnt(cmt_version='global_ONLINE', _from_cutax=False, **kwargs):
def xenonnt(cmt_version='global_ONLINE', xedocs_version=None,
_from_cutax=False, **kwargs):
"""XENONnT context"""
if not _from_cutax and cmt_version != 'global_ONLINE':
warnings.warn('Don\'t load a context directly from straxen, '
'use cutax instead!')
st = straxen.contexts.xenonnt_online(**kwargs)
st.apply_cmt_version(cmt_version)

if xedocs_version is not None:
st.apply_xedocs_configs(xedocs_version)

return st


Expand Down
17 changes: 17 additions & 0 deletions straxen/corrections_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,23 @@ def apply_cmt_version(context: strax.Context, cmt_global_version: str) -> None:
context.set_config(cmt_config)


@strax.Context.add_method
def apply_xedocs_configs(context: strax.Context,
version: str, db='straxen_db') -> None:
import xedocs

docs = xedocs.find_docs('context_configs',
datasource=db,
version=version)

global_config = {doc.config_name: doc.value for doc in docs}

if len(global_config):
context.set_config(global_config)
else:
warnings.warn(f"Could not find any context configs for version {version}")


def replace_url_version(url, version):
"""Replace the local version of a correction in a CMT config"""
kwargs = {k: v[0] for k, v in parse_qs(urlparse(url).query).items()}
Expand Down

0 comments on commit 07f90c3

Please sign in to comment.