Skip to content

Commit

Permalink
Update config.se_options
Browse files Browse the repository at this point in the history
  • Loading branch information
gtca committed Sep 24, 2021
1 parent 9d40ca0 commit 85fcfe5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 24 deletions.
98 changes: 89 additions & 9 deletions docs/source/notebooks/quickstart_mudata.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,7 @@
}
],
"source": [
"import mudata\n",
"with mudata.set_options(display_style = \"html\", display_html_expand = 0b000):\n",
"with mu.set_options(display_style = \"html\", display_html_expand = 0b000):\n",
" display(mdata)"
]
},
Expand Down Expand Up @@ -604,13 +603,13 @@
{
"data": {
"text/html": [
"<pre>MuData object with n_obs × n_vars = 999 × 104 backed at &#x27;/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_mrwzqfcs.h5mu&#x27;\n",
"<pre>MuData object with n_obs × n_vars = 999 × 104 backed at &#x27;/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_4b8mn4v8.h5mu&#x27;\n",
" 2 modalities\n",
" A:\t999 x 54\n",
" B:\t999 x 50</pre>"
],
"text/plain": [
"MuData object with n_obs × n_vars = 999 × 104 backed at '/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_mrwzqfcs.h5mu'\n",
"MuData object with n_obs × n_vars = 999 × 104 backed at '/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_4b8mn4v8.h5mu'\n",
" 2 modalities\n",
" A:\t999 x 54\n",
" B:\t999 x 50"
Expand Down Expand Up @@ -674,13 +673,94 @@
{
"data": {
"text/html": [
"<pre>MuData object with n_obs × n_vars = 999 × 104 backed at &#x27;/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_mrwzqfcs.h5mu&#x27;\n",
" 2 modalities\n",
" A:\t999 x 54\n",
" B:\t999 x 50</pre>"
"<style>\n",
".hl-dim, .hl-size, .hl-values, .hl-types, .hl-dims {\n",
" color: #777777;\n",
"}\n",
".hl-dim::before, .hl-size::before {\n",
" content: \"\\00a0\\00a0\\00a0\";\n",
"}\n",
".hl-values {\n",
" font-family: monospace;\n",
"}\n",
".hl-file {\n",
" background-color: #EEEEEE;\n",
" border-radius: .5rem;\n",
" padding: .2rem .4rem;\n",
" color: #555555;\n",
"}\n",
".hl-empty {\n",
" color: #999999;\n",
"}\n",
".hl-import {\n",
" color: #777777;\n",
"}\n",
".block-mod {\n",
" display: block;\n",
" margin: 0 2rem;\n",
"}\n",
".title {\n",
" display: inline-block;\n",
" font-weight: 600;\n",
" color: #555555;\n",
"}\n",
".title-mod {\n",
" font-size: 1.2rem;\n",
" color: #04b374;\n",
" padding: 0 .5rem;\n",
"}\n",
".title-attr {\n",
" font-size: 1.0rem;\n",
" padding-top: .2rem;\n",
"}\n",
"summary {\n",
" cursor: pointer;\n",
" list-style: none;\n",
"}\n",
"summary::-webkit-details-marker {\n",
" display: none;\n",
"}\n",
"details > summary::before {\n",
" content: '⊕';\n",
"}\n",
"details[open] > summary::before {\n",
" content: '⊖';\n",
"}\n",
"table tr {\n",
" background-color: transparent !important;\n",
"}\n",
"table tr:hover {\n",
" background-color: #04b37433 !important;\n",
"}\n",
".col-index {\n",
" text-align: left !important;\n",
"}\n",
".summary-mod {\n",
" margin-left: -2rem;\n",
"}\n",
".summary-mod:hover {\n",
" background-color: #04b37411;\n",
"}\n",
".summary-mod::before {\n",
" color: #04b374;\n",
" content: '●';\n",
"}\n",
"details[open] > .summary-mod::before {\n",
" content: '○';\n",
"}\n",
"</style><span>MuData object <span class='hl-dim'>999 obs &times; 104 var in 2 modalities</span></span><br>&#8627; <span>backed at <span class='hl-file'>/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_4b8mn4v8.h5mu</span></span><br><details><summary><div class='title title-attr'>Metadata</div><span class='hl-dim'>.obs</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No metadata</span></details><details><summary><div class='title title-attr'>Embeddings & mappings</div><span class='hl-dim'>.obsm</span><span class='hl-size'>2 elements</span></summary><div><table><tr>\n",
" <td class='col-index'>A</td> <td class='hl-types'>bool</td> <td><span class='hl-import'>numpy.</span>ndarray</td> <td class='hl-dims'></td>\n",
" </tr>\n",
"<tr>\n",
" <td class='col-index'>B</td> <td class='hl-types'>bool</td> <td><span class='hl-import'>numpy.</span>ndarray</td> <td class='hl-dims'></td>\n",
" </tr></table></div></details><details><summary><div class='title title-attr'>Distances</div><span class='hl-dim'>.obsp</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No distances</span></details><div class='block-mod'><div><details><summary class='summary-mod'><div class='title title-mod'>A</div><span class='hl-dim'>999 &times 54</span></summary><span>AnnData object <span class='hl-dim'>999 obs &times; 54 var</span></span><br>&#8627; <span>backed at <span class='hl-file'>/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_4b8mn4v8.h5mu</span></span><br><div class='title title-attr'>Matrix</div><span class='hl-dim'>.X</span><div>\n",
" <span class='hl-types'>float32</span> <span>&nbsp;&nbsp;&nbsp;<span class='hl-import'>h5py._hl.dataset.</span>Dataset</span>\n",
" </table></div><details><summary><div class='title title-attr'>Layers</div><span class='hl-dim'>.layers</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No layers</span></details><details><summary><div class='title title-attr'>Metadata</div><span class='hl-dim'>.obs</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No metadata</span></details><details><summary><div class='title title-attr'>Embeddings</div><span class='hl-dim'>.obsm</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No embeddings</span></details><details><summary><div class='title title-attr'>Distances</div><span class='hl-dim'>.obsp</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No distances</span></details><details><summary><div class='title title-attr'>Miscellaneous</div><span class='hl-dim'>.uns</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No miscellaneous</span></details></details></div></div><div class='block-mod'><div><details><summary class='summary-mod'><div class='title title-mod'>B</div><span class='hl-dim'>999 &times 50</span></summary><span>AnnData object <span class='hl-dim'>999 obs &times; 50 var</span></span><br>&#8627; <span>backed at <span class='hl-file'>/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_4b8mn4v8.h5mu</span></span><br><div class='title title-attr'>Matrix</div><span class='hl-dim'>.X</span><div>\n",
" <span class='hl-types'>float32</span> <span>&nbsp;&nbsp;&nbsp;<span class='hl-import'>h5py._hl.dataset.</span>Dataset</span>\n",
" </table></div><details><summary><div class='title title-attr'>Layers</div><span class='hl-dim'>.layers</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No layers</span></details><details><summary><div class='title title-attr'>Metadata</div><span class='hl-dim'>.obs</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No metadata</span></details><details><summary><div class='title title-attr'>Embeddings</div><span class='hl-dim'>.obsm</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No embeddings</span></details><details><summary><div class='title title-attr'>Distances</div><span class='hl-dim'>.obsp</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No distances</span></details><details><summary><div class='title title-attr'>Miscellaneous</div><span class='hl-dim'>.uns</span><span class='hl-size'>0 elements</span></summary><span class='hl-empty'>No miscellaneous</span></details></details></div></div><br/>"
],
"text/plain": [
"MuData object with n_obs × n_vars = 999 × 104 backed at '/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_mrwzqfcs.h5mu'\n",
"MuData object with n_obs × n_vars = 999 × 104 backed at '/var/folders/xt/tvy3s7w17vn1b700k_351pz00000gp/T/muon_getting_started_4b8mn4v8.h5mu'\n",
" 2 modalities\n",
" A:\t999 x 54\n",
" B:\t999 x 50"
Expand Down
26 changes: 11 additions & 15 deletions muon/_core/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from mudata._core import config as mudata_config
import logging as log

OPTIONS = {
"display_style": "text",
"display_html_expand": 0b010,
}
OPTIONS = {}

_VALID_OPTIONS = {
"display_style": lambda x: x in ("text", "html"),
"display_html_expand": lambda x: isinstance(x, int) and len(bin(x or 0b111)) == 5,
}
_VALID_OPTIONS = {}


class set_options:
Expand All @@ -36,7 +30,7 @@ class set_options:

def __init__(self, **kwargs):
self.opts = {}
mudata_opts = {}
self.mudata_opts = {}
for k, v in kwargs.items():
if k not in OPTIONS and k not in mudata_config.OPTIONS:
raise ValueError(f"There is no option '{k}' available")
Expand All @@ -47,18 +41,20 @@ def __init__(self, **kwargs):
self.opts[k] = OPTIONS[k]
else:
# For mudata options, there validity is going to be checked by mudata
mudata_opts[k] = mudata_config.OPTIONS[k]
mudata_config.set_options(**mudata_opts)
# Muon options take precedence
self._apply(kwargs)
self.mudata_opts[k] = mudata_config.OPTIONS[k]
mudata_config.set_options(**{k: v for k, v in kwargs.items() if k in mudata_config.OPTIONS})
if self.opts:
self._apply(**{k: v for k, v in kwargs.items() if k in OPTIONS})

def _apply(self, opts):
OPTIONS.update(opts)

def __enter__(self):
log.info("Using custom muon options in the new context...")
return OPTIONS
return {**mudata_config.OPTIONS, **OPTIONS}

def __exit__(self, exc_type, exc_val, exc_tb):
log.info(f"Returning to the previously defined options: {self.opts}")
log.info(f"Returning to the previously defined mudata options: {self.mudata_opts}")
log.info(f"Returning to the previously defined muon options: {self.opts}")
mudata_config.set_options(**self.mudata_opts)
self._apply(self.opts)

0 comments on commit 85fcfe5

Please sign in to comment.