Skip to content
Snippets Groups Projects

Commit generic notebook and docs

Merged Steffen Hauf requested to merge doc/paramter_gen_function into master
1 unresolved thread
2 files
+ 284
0
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 94
0
@@ -108,4 +108,98 @@ This can be useful to add user requests while running. For this:
@@ -108,4 +108,98 @@ This can be useful to add user requests while running. For this:
manually, assuming the correction notebook allows run and overwrite paramters::
manually, assuming the correction notebook allows run and overwrite paramters::
xfel-calibrate ...... --run XYZ,ZXY-YYS --overwrite
xfel-calibrate ...... --run XYZ,ZXY-YYS --overwrite
 
 
 
Using a Parameter Generator Function
 
------------------------------------
 
 
By default, the parameters to be exposed to the command line are deduced from the
 
first code cell of the notebook, after resolving the notebook itself from the
 
detector and characterization type. For some applications it might be beneficial
 
to define a context-specific parameter range within the same notebook, based on
 
additional user input. This can be done via a parameter generation function which
 
is defined in one of the code cell::
 
 
def extend_parms(detector_instance):
 
from iCalibrationDB import Conditions
 
import inspect
 
existing = set()
 
def extract_parms(cls):
 
args, varargs, varkw, defaults = inspect.getargspec(cls.__init__)
 
pList = []
 
for i, arg in enumerate(args[1:][::-1]):
 
if arg in existing:
 
continue
 
 
existing.add(arg)
 
 
if i < len(defaults):
 
default = defaults[::-1][i]
 
if str(default).isdigit():
 
pList.append("{} = {}".format(arg, default))
 
elif default is None or default == "None":
 
pList.append("{} = \"None\"".format(arg))
 
else:
 
pList.append("{} = \"{}\"".format(arg, default))
 
else:
 
pList.append("{} = 0. # required".format(arg))
 
return set(pList[::-1]) # mandatories first
 
dtype = "LPD" if "LPD" in detector_instance.upper() else "AGIPD"
 
all_conditions = set()
 
for c in dir(Conditions):
 
if c[:2] != "__":
 
condition = getattr(Conditions, c)
 
parms = extract_parms(getattr(condition, dtype))
 
[all_conditions.add(p) for p in parms]
 
return "\n".join(all_conditions)
 
 
 
.. note::
 
 
Note how all imports are inlined, as the function is executed outside the
 
notebook context.
 
 
In the example, the function generates a list of additional parameters depending
 
on the `detector_instance` given. Here, `detector_instance` is defined in the first
 
code cell the usual way. Any other parameters defined such, that have names matching
 
those of the generator function signature are passed to this function. The function
 
should then return a string containing additional code to be appended to the first
 
code cell.
 
 
To make use of this functionality, the parameter generator function needs to be
 
configured in `notebooks.py`, e.g. ::
 
 
...
 
"GENERIC": {
 
"DBTOH5": {
 
"notebook": "notebooks/generic/DB_Constants_to_HDF5_NBC.ipynb",
 
"concurrency": {"parameter": None,
 
"default concurrency": None,
 
"cluster cores": 32},
 
"extend parms": "extend_parms",
 
},
 
}
 
...
 
To generically query which parameters are defined in the first code cell, the
 
code execution history feature of iPython can be used::
 
 
ip = get_ipython()
 
session = ip.history_manager.get_last_session_id()
 
first_cell = next(ip.history_manager.get_range(session, 1, 2, raw=True))
 
_, _, code = first_cell
 
code = code.split("\n")
 
parms = {}
 
for c in code:
 
n, v = c.split("=")
 
n = n.strip()
 
v = v.strip()
 
try:
 
parms[n] = float(v)
 
except:
 
parms[n] = str(v) if not isinstance(v, str) else v
 
if parms[n] == "None" or parms[n] == "'None'":
 
parms[n] = None
 
 
This will create a dictionary `parms` which contains all parameters either
 
as `float` or `str` values.
 
\ No newline at end of file
Loading