Debian Codegraph @

doc/functions_manager.rst

raw
1 .. _Functions manager:
2
3 **********************
4 Functions manager view
5 **********************
6
7 Virtual channels (computed channels) can be defined in the Plot windows. The user will
8 define the functions used for the virtual channels using Python language syntax; the extra rules will be described lower. After this the user
9 can insert virtual channels in the Plot window (keyboard shortcut ``Ins``) and will map an existing function to the virtual channel, as well as
10 a list of alternative channel names (ASAPs) for each function argument.
11
12 Function manager dialog
13 =======================
14 The user defined functions are associated with the display file. This means that there will be separate function managers for
15 each opened measurement file. When a display file (.dspf) is saved by the user it will also contain the associated user defined functions.
16 The functions manager dialog can be invoked using the keyboard shortcut ``F6``.
17
18 .. image:: images/functions_manager_dialog.png
19
20
21 ======================================= ================================================================================
22 Option Description
23 ======================================= ================================================================================
24 ``Add`` Adds a new function
25 ``Delete all`` Deletes all the defined functions
26 ``Load definitions`` Imports definitions found in ``.def`` files
27 ``Save definitions`` Saves all the define function to a new file with the extension ``.def``
28 ``Function definition`` Python language function definition
29 ``Check syntax`` Check if the current function definition syntax is valid
30 ``Store function changes`` Updates the definition after the user has made changes. If this is not pressed
31 and the user switches to another function then the changes will be lost.
32 ``Apply`` Accept all the changes made by the user
33 ``Cancel`` Rejects all the changes made by the user
34 ======================================= ================================================================================
35
36
37 Python function rules
38 =====================
39 The user defines the functions using Python syntax; this means that the function definition must be valid in Python code.
40
41 Furthermore the following rules and restrictions must be followed:
42
43 * all function arguments must have default numeric values: this means that the definition `def Function1(a, b, t=0)` is not
44 accepted, instead the user must provide default values such as `def Function1(a=1.5, b=0, t=0)`
45 * the last argument must be ``t=0``: the argument `t` is reserved for the time stamp and must not be otherwise used by the user.
46 This means that the definition `def Function1(a=1.5, b=0)` is not valid, instead the user can write something like this
47 `def Function1(a=1.5, b=0, t=0)`
48 * always return numeric values on all the branches: Python functions automatically return `None` if no `return` is explicitly
49 written. The user must take care that all possible code branches return numeric values.
50
51
52 .. code:: python
53
54 "this function will produce errors because not all branches have explicit return statements with numeric values"
55 def Function1(a=0, t=0):
56 if a > 5:
57 return 1
58
59 "this function will produce errors because it returns non-numeric values"
60 def Function1(a=0, t=0):
61 if a > 5:
62 return "Activated"
63 else:
64 return "Disabled"
65
66 "this function is OK because it has explicit return statements with numeric values"
67 def Function1(a=0, t=0):
68 if a > 5:
69 return 1
70 else:
71 return 0
72
73
74 * the modules `math`, `pandas` and `numpy` are available inside the function as `math`, `pd` and `np`
75 * functions can be cross-referenced; one function can call another user defined function
76
77
78 .. code:: python
79
80 def Function1(a=0, t=0):
81 if a > 5:
82 return 1
83 else:
84 return 0
85
86 " Function2 can call Function 1"
87 def Function2(b=1, c=7, t=0):
88 if b != 0:
89 return Function1(c)
90 else:
91 return c / 2
92
93
94 Virtual channel dialog
95 ======================
96 Virtual channels can be added to Plot windows using the keyboard shortcut ``Ins`` (insert). The precondition for this is to
97 have at least one function defined in the Functions manager.
98
99 In the `Define channel` dialog window the user can select one of the available function, and then for each of the function's
100 arguments a list of alternative channel names can be filed by the user. The argument `t=0` is reserved for the time-stamp and is not
101 shown in the dialog window. The alternative names must be placed on separate lines in the text field found next to the argument name.
102
103 .. image:: images/virtual_channel_dialog.png
104
105
106 ======================================= ================================================================================
107 Option Description
108 ======================================= ================================================================================
109 ``Triggering`` the virtual channel samples will be calculated in the time stamps defined by the
110 selected choice
111 ``Computation mode`` the virtual channel can be computed passing the signal arguments samples by
112 sample or passing the signal arguments directly as numpy ndarrays
113 ``Function name`` select one of the available functions
114 ``Function info`` displays the function definition code
115 ``Arguments`` alternative channel names for each argument. In the example argument `b` will
116 use the first existing ASAP from the choices `MAIN_CLOCK` or `MAIN_CLOCK_5MS`.
117 If no alternative exist in the measurement file then the argument's default value
118 will be used (see the function definition)
119 ``Argument search`` opens a search dialog were channel names cane be searched from the measurement
120 file
121 ``Computed channel name`` virtual channel name
122 ``Computed channel comment`` virtual channel comment
123 ``Computed channel unit`` virtual channel unit
124 ``Function definition`` Python language function definition
125 ``Check syntax`` Check if the current function definition syntax is valid
126 ``Store function changes`` Updates the definition after the user has made changes. If this is not pressed
127 and the user switches to another function then the changes will be lost.
128 ``Apply`` Accept the changes and inserts the new virtual channel
129 ``Cancel`` Rejects all the changes made by the user
130 ======================================= ================================================================================
131
132
133 When the computation mode is set to `sample by sample` the following steps are executed:
134
135 * the signals linked to the arguments are searched in the measurement file
136 * using the signals found in the measurement, the union of all time stamps is computed (resulting an ndarray of length N)
137 * all found signals are interpolated using the union time base
138 * the function is called N times (once for each of the N time stamps) passing the signal value for the current time stamp and the time stamp (in the `t` argument).
139 The missing linked signals will be replaced by the default value specified in the function definition
140 * the returned value are stored and finally the resulting Signal is constructed
141
142
143 When the computation mode is set to `complete signal` the following steps are executed:
144
145 * the signals linked to the arguments are searched in the measurement file
146 * using the signals found in the measurement, the union of all time stamps is computed (resulting an ndarray of length N)
147 * all found signals are interpolated using the union time base
148 * the function is called only 1 time passing the complete signal ndarray's and the union of the time stamps (in the `t` argument).
149 The missing linked signals will be replaced by an ndarray of length N filled with the default value specified in the function definition
150 * the result Signal is constructed using the ndarray returned by the function
151
152
153 Example functions
154 =================
155
156 Average of 3 channels
157 ---------------------
158
159 The same definition works for both `sample by sample` and `complete signal` computation modes
160
161 .. code:: python
162
163 def average(channel1=0, channel2=-1, channel3=0, t=0):
164 return (channel1 + channel2 + channel3) / 3
165
166
167 Channel clipping
168 ----------------
169
170 Using `sample by sample` computation mode
171
172 .. code:: python
173
174 def clip(channel1=0, t=0):
175 if channel1 > 100:
176 return 100
177 elif channe1 < 0:
178 return 0
179 else:
180 return channel1
181
182
183 Using `complete signal` computation mode
184
185 .. code:: python
186
187 def clip(channel1=0, t=0):
188 return np.clip(cahnnel1, 0, 100)
189
190
191 Grey code to decimal
192 --------------------
193
194 The same definition works for both `sample by sample` and `complete signal` computation modes
195
196 .. code:: python
197
198 def gray2dec(position_sensor_value=0, t=0):
199
200 for shift in (8, 4, 2, 1):
201 position_sensor_value = position_sensor_value ^ (position_sensor_value >> shift)
202
203 return position_sensor_value
204
205
206
207 Maximum of 3 channels
208 ---------------------
209
210 Using `sample by sample` computation mode
211
212 .. code:: python
213
214 def maximum(channel1=0, channel2=-1, channel3=0, t=0):
215 return max(channel1, channel2, channel3)
216
217 Using `complete signal` computation mode
218
219 .. code:: python
220
221 def maximum(channel1=0, channel2=-1, channel3=0, t=0):
222 return np.maximum.reduce([channel1, channel2, channel3])
223
224 rpm to rad/s
225 ------------
226
227 The same definition works for both `sample by sample` and `complete signal` computation modes
228
229 .. code:: python
230
231 def rpm_to_rad_per_second(speed=0, t=0):
232 return 2 * np.pi * speed / 60
233
234
235 gradient
236 --------
237
238 In the `Define channel` dialog the computation mode must be set to `complete signal`
239
240 .. code:: python
241
242 def rpm_to_rad_per_second(speed=0, t=0):
243 return np.diff(speed) / np.diff(t)
244
245 It is not possible to implement this function in the `sample by sample` mode because only the current samples are
246 forwarded to the function.
247
248