Commit 5dc69c2b authored by Matias Guijarro's avatar Matias Guijarro
Browse files

Merge branch 'expression_based_calc_counter' into 'master'

Expression based calc counter

See merge request !1918
parents 73f8243c 23e9b66b
Pipeline #20317 passed with stages
in 4 minutes and 19 seconds
from bliss.common.counter import CalcCounter
from bliss.controllers.counter import CalcCounterController
import numexpr
class ExpressionCalcCounter(CalcCounter):
def __init__(self, name, config):
self._expression = config["expression"]
self._constants = config.get("constants").to_dict()
calc_ctrl_config = {
"inputs": config["inputs"],
"outputs": [{"name": name, "tags": name}],
}
self.__controller = CalcCounterController(name + "_ctrl", calc_ctrl_config)
def _calc_function(input_dict):
exp_dict = self._constants.copy()
for cnt in self.__controller.inputs:
exp_dict.update(
{
self.__controller.tags[cnt.name]: input_dict[
self.__controller.tags[cnt.name]
]
}
)
return {
self.__controller.tags[
self.__controller.outputs[0].name
]: numexpr.evaluate(
self._expression, global_dict={}, local_dict=exp_dict
)
}
self.__controller.calc_function = _calc_function
super().__init__(name, self.__controller)
class ExpressionCalcCounterController(CalcCounterController):
def __init__(self, name, config):
self._constants = config.get("constants").to_dict()
self._expressions = dict()
for o in config["outputs"]:
self._expressions[o["name"]] = o["expression"]
super().__init__(name, config)
def _calc_function(input_dict):
exp_dict = self._constants.copy()
for cnt in self.inputs:
exp_dict.update({self.tags[cnt.name]: input_dict[self.tags[cnt.name]]})
return {
self.tags[out]: numexpr.evaluate(
expression, global_dict={}, local_dict=exp_dict
)
for out, expression in self._expressions.items()
}
self.calc_function = _calc_function
......@@ -325,6 +325,52 @@ A controller exporting N counters.
Calculation counters can be use to do some computation on raw values
of real counters.
### Expression based calc counter
Do define calculational counters directly in the *YAML* it is possible to use
`ExpressionCalcCounter` or `ExpressionCalcCounterController`. These two classes extend the Calculation Counter framework such that expressions defined in the *YAML* are evaluated during the calculation.
Here are some example configurations:
For a single counter
```
- plugin: bliss
module: expression_based_calc
class: ExpressionCalcCounter
name: simu_expr_calc
expression: m*x+b
inputs:
- counter : $diode
tags: x
- counter : $diode2
tags: b
constants:
m : 10
```
For multiple counters
```
- plugin: bliss
module: expression_based_calc
class: ExpressionCalcCounterController
name: simu_expr_calc_ctrl
inputs:
- counter: $simu1.counters.deadtime_det0
tags: x
- counter: $diode2
tags: y
constants:
m : 10
n : 100
outputs:
- name: out3
expression: m*x
- name: out4
expression: n*y
```
### Simple example
In this example the calculation counter will return the mean of two
......@@ -368,6 +414,7 @@ mean = CalcCounter("mean",Mean(),diode,diode2)
```
#Tutorial
Use-case examples to export new counters in BLISS.
......
......@@ -44,6 +44,7 @@ cerberus
typeguard == 2.6.1
typing_extensions
tailon
numexpr
# Flint
qt
......
......@@ -26,3 +26,36 @@
outputs:
- name: out2
- plugin: bliss
module: expression_based_calc
class: ExpressionCalcCounter
name: simu_expr_calc
expression: m*x+b
inputs:
- counter : $diode
tags: x
- counter : $diode2
tags: b
constants:
m : 10
- plugin: bliss
module: expression_based_calc
class: ExpressionCalcCounterController
name: simu_expr_calc_ctrl
inputs:
- counter: $simu1.counters.deadtime_det0
tags: x
- counter: $diode2
tags: y
constants:
m : 10
n : 100
outputs:
- name: out3
expression: m*x
- name: out4
expression: n*y
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment