Debian Codegraph @

doc/source/tutorials/NetworkSet.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "raw",
5 "metadata": {
6 "raw_mimetype": "text/restructuredtext"
7 },
8 "source": [
9 ".. _networkset:\n",
10 "| \n",
11 "|\n",
12 "Download This Notebook: :download:`NetworkSet.ipynb`"
13 ]
14 },
15 {
16 "cell_type": "markdown",
17 "metadata": {},
18 "source": [
19 "# NetworkSet\n",
20 "\n",
21 "\n",
22 "## Introduction\n",
23 "\n",
24 "\n",
25 "\n",
26 "The [NetworkSet](../api/networkSet.rst) object represents an unordered set of networks. It \n",
27 "provides methods iterating and slicing the set, sorting by datetime, calculating statistical quantities, and displaying uncertainty bounds on plots. \n",
28 "\n",
29 "## Creating a [NetworkSet](../api/networkSet.rst)"
30 ]
31 },
32 {
33 "cell_type": "markdown",
34 "metadata": {},
35 "source": [
36 "Lets take a look in the `data/` folder, there are some redundant measurements of a network called `ro`, which is a *radiating open* waveguide. "
37 ]
38 },
39 {
40 "cell_type": "markdown",
41 "metadata": {},
42 "source": [
43 "```\n",
44 "ls data/ro*\n",
45 "\n",
46 "-a---- 14/02/2021 12:35 8031 ro,1.s1p\n",
47 "-a---- 14/02/2021 12:35 8030 ro,2.s1p\n",
48 "-a---- 14/02/2021 12:35 8031 ro,3.s1p\n",
49 "-a---- 14/02/2021 12:35 46592 ro_spreadsheet.xls\n",
50 "```"
51 ]
52 },
53 {
54 "cell_type": "markdown",
55 "metadata": {
56 "raw_mimetype": "text/markdown"
57 },
58 "source": [
59 "The files `ro,1.s1p` , `ro,2.s1p`, ... are redundant measurements on \n",
60 "which we would like to calculate statistics using the [NetworkSet](../api/networkSet.rst)\n",
61 "class.\n",
62 "\n",
63 "A [NetworkSet](../api/networkSet.rst) is created from a list or dict of \n",
64 "[Network](../api/network.rst)'s. So first we need to load all of the \n",
65 "touchstone files into `Networks`. This can be done quickly with \n",
66 "`rf.read_all`, The argument `contains` is used to load only files \n",
67 "which match a given substring. "
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": null,
73 "metadata": {},
74 "outputs": [],
75 "source": [
76 "import skrf as rf\n",
77 "\n",
78 "rf.read_all(rf.data.pwd, contains='ro')"
79 ]
80 },
81 {
82 "cell_type": "markdown",
83 "metadata": {},
84 "source": [
85 "This can be passed directly to the [NetworkSet](../api/networkSet.rst) constructor, "
86 ]
87 },
88 {
89 "cell_type": "code",
90 "execution_count": null,
91 "metadata": {},
92 "outputs": [],
93 "source": [
94 "from skrf import NetworkSet\n",
95 "\n",
96 "ro_dict = rf.read_all(rf.data.pwd, contains='ro')\n",
97 "ro_ns = NetworkSet(ro_dict, name='ro set')\n",
98 "ro_ns"
99 ]
100 },
101 {
102 "cell_type": "markdown",
103 "metadata": {},
104 "source": [
105 "A NetworkSet can also be constructed directly from:\n",
106 " - a directory containing Touchstone files: `NetworkSet.from_dir()`,\n",
107 " - a zipfile of touchstones files: `NetworkSet.from_zip()`, \n",
108 " - a dictionary of s-parameters: `NetworkSet.from_s_dict()`,\n",
109 " - a (G)MDIF (.mdf) file: `NetworkSet.from_mdif()`,\n",
110 " - a CITI (.cti) file: `NetworkSet.from_citi()`. "
111 ]
112 },
113 {
114 "cell_type": "markdown",
115 "metadata": {},
116 "source": [
117 "## Accessing Network Methods \n",
118 "The [Network](../api/network.rst) elements in a [NetworkSet](../api/networkSet.rst) can be accessed like the elements of list, "
119 ]
120 },
121 {
122 "cell_type": "code",
123 "execution_count": null,
124 "metadata": {},
125 "outputs": [],
126 "source": [
127 "ro_ns[0]"
128 ]
129 },
130 {
131 "cell_type": "markdown",
132 "metadata": {},
133 "source": [
134 "Most [Network](../api/network.rst) methods are also methods of \n",
135 "[NetworkSet](../api/networkSet.rst). These methods are called on each \n",
136 "[Network](../api/network.rst) element individually. For example to \n",
137 "plot the log-magnitude of the s-parameters of each Network."
138 ]
139 },
140 {
141 "cell_type": "code",
142 "execution_count": null,
143 "metadata": {},
144 "outputs": [],
145 "source": [
146 "%matplotlib inline\n",
147 "import skrf as rf\n",
148 "\n",
149 "rf.stylely()\n",
150 "\n",
151 "ro_ns.plot_s_db()"
152 ]
153 },
154 {
155 "cell_type": "markdown",
156 "metadata": {},
157 "source": [
158 "## Statistical Properties\n",
159 "\n",
160 "\n",
161 "Statistical quantities can be calculated by accessing \n",
162 "properties of the NetworkSet. To calculate the complex \n",
163 "average of the set, access the `mean_s` property"
164 ]
165 },
166 {
167 "cell_type": "code",
168 "execution_count": null,
169 "metadata": {},
170 "outputs": [],
171 "source": [
172 "ro_ns.mean_s"
173 ]
174 },
175 {
176 "cell_type": "raw",
177 "metadata": {
178 "raw_mimetype": "text/restructuredtext"
179 },
180 "source": [
181 " \n",
182 ".. note:: \n",
183 "\n",
184 " Because the statistical operator methods are generated upon initialization\n",
185 " their API is not explicitly documented in this manual. "
186 ]
187 },
188 {
189 "cell_type": "markdown",
190 "metadata": {},
191 "source": [
192 " \n",
193 "The naming convention of the statistical operator properties are `NetworkSet.{function}_{parameter}`, where `function` is the name of the \n",
194 "statistical function, and `parameter` is the Network parameter to operate \n",
195 "on. These methods return a [Network](../api/network.rst) object, so they can be \n",
196 "saved or plotted in the same way as you would with a Network.\n",
197 "To plot the log-magnitude of the complex mean response "
198 ]
199 },
200 {
201 "cell_type": "code",
202 "execution_count": null,
203 "metadata": {},
204 "outputs": [],
205 "source": [
206 "ro_ns.mean_s.plot_s_db(label='ro')"
207 ]
208 },
209 {
210 "cell_type": "markdown",
211 "metadata": {},
212 "source": [
213 "Or to plot the standard deviation of the complex s-parameters,"
214 ]
215 },
216 {
217 "cell_type": "code",
218 "execution_count": null,
219 "metadata": {},
220 "outputs": [],
221 "source": [
222 "ro_ns.std_s.plot_s_re(y_label='Standard Deviations')"
223 ]
224 },
225 {
226 "cell_type": "markdown",
227 "metadata": {},
228 "source": [
229 "Using these properties it is possible to calculate statistical quantities on the scalar \n",
230 "components of the complex network parameters. To calculate the \n",
231 "mean of the phase component,"
232 ]
233 },
234 {
235 "cell_type": "code",
236 "execution_count": null,
237 "metadata": {},
238 "outputs": [],
239 "source": [
240 "ro_ns.mean_s_deg.plot_s_re()"
241 ]
242 },
243 {
244 "cell_type": "markdown",
245 "metadata": {},
246 "source": [
247 "## Plotting Uncertainty Bounds\n",
248 "\n",
249 "\n",
250 "Uncertainty bounds can be plotted through the methods "
251 ]
252 },
253 {
254 "cell_type": "code",
255 "execution_count": null,
256 "metadata": {},
257 "outputs": [],
258 "source": [
259 "ro_ns.plot_uncertainty_bounds_s_db()"
260 ]
261 },
262 {
263 "cell_type": "code",
264 "execution_count": null,
265 "metadata": {},
266 "outputs": [],
267 "source": [
268 "ro_ns.plot_uncertainty_bounds_s_deg()"
269 ]
270 },
271 {
272 "cell_type": "raw",
273 "metadata": {
274 "raw_mimetype": "text/restructuredtext"
275 },
276 "source": [
277 ".. note::\n",
278 "\n",
279 " The uncertainty bounds plotted above are calculated **after** \n",
280 " the complex number has been projected onto the specified scalar component.\n",
281 " Thus, the first plot represents uncertainty in the magnitude component **only**."
282 ]
283 },
284 {
285 "cell_type": "markdown",
286 "metadata": {
287 "vscode": {
288 "languageId": "raw"
289 }
290 },
291 "source": [
292 "## Plotting Violin Plots\n",
293 "\n",
294 "Violin plots are used to get an idea of the distribution at a glance by displaying a probability density at each point,\n",
295 "along with the minimum, maximum, and average values by default. Let's interpolate the frequencies to reduce clutter."
296 ]
297 },
298 {
299 "cell_type": "code",
300 "execution_count": null,
301 "metadata": {},
302 "outputs": [],
303 "source": [
304 "ro_ns_interp = ro_ns.interpolate_frequency(rf.Frequency(500, 600, 15, \"GHz\"))"
305 ]
306 },
307 {
308 "cell_type": "markdown",
309 "metadata": {},
310 "source": [
311 "Violin plots are available for most common network parameters, except for those in the time domain. To plot the log-magnitude and phase of the s-parameters"
312 ]
313 },
314 {
315 "cell_type": "code",
316 "execution_count": null,
317 "metadata": {},
318 "outputs": [],
319 "source": [
320 "ro_ns_interp.plot_violin(\"s_db\")"
321 ]
322 },
323 {
324 "cell_type": "code",
325 "execution_count": null,
326 "metadata": {},
327 "outputs": [],
328 "source": [
329 "ro_ns_interp.plot_violin(\"s_deg\")"
330 ]
331 },
332 {
333 "cell_type": "markdown",
334 "metadata": {},
335 "source": [
336 "\n",
337 "## Reading and Writing"
338 ]
339 },
340 {
341 "cell_type": "markdown",
342 "metadata": {},
343 "source": [
344 "To write all [Network](../api/network.rst)s of a [NetworkSet](../api/networkSet.rst) out to individual touchstones,"
345 ]
346 },
347 {
348 "cell_type": "code",
349 "execution_count": null,
350 "metadata": {},
351 "outputs": [],
352 "source": [
353 "ro_ns.write_touchstone(dir='data/')"
354 ]
355 },
356 {
357 "cell_type": "markdown",
358 "metadata": {},
359 "source": [
360 "For temporary data storage, [NetworkSet](../api/networkSet.rst)s can be saved and read from disk \n",
361 "using the functions `rf.read` and `rf.write`\n",
362 "\n",
363 " "
364 ]
365 },
366 {
367 "cell_type": "code",
368 "execution_count": null,
369 "metadata": {},
370 "outputs": [],
371 "source": [
372 "rf.write('ro set.ns', ro_ns)"
373 ]
374 },
375 {
376 "cell_type": "code",
377 "execution_count": null,
378 "metadata": {},
379 "outputs": [],
380 "source": [
381 "ro_ns = rf.read('ro set.ns')\n",
382 "ro_ns"
383 ]
384 },
385 {
386 "cell_type": "markdown",
387 "metadata": {},
388 "source": [
389 "## Export to Excel, csv, or html"
390 ]
391 },
392 {
393 "cell_type": "markdown",
394 "metadata": {},
395 "source": [
396 "[NetworkSet](../api/networkSet.rst)s can also be exported to other filetypes. The format of the output; real/imag, mag/phase is adjustable, as is the output type; csv, excel, html. For example to export mag/phase for each network into an Excel spreadsheet for your boss[s]"
397 ]
398 },
399 {
400 "cell_type": "code",
401 "execution_count": null,
402 "metadata": {},
403 "outputs": [],
404 "source": [
405 "ro_ns.write_spreadsheet('data/ro_spreadsheet.xls', form='db')"
406 ]
407 },
408 {
409 "cell_type": "markdown",
410 "metadata": {},
411 "source": [
412 "More info on this can be found in the function, `skrf.io.general.network_2_spreadsheet`"
413 ]
414 },
415 {
416 "cell_type": "markdown",
417 "metadata": {},
418 "source": [
419 "## Named Parameters\n",
420 "If all the `Network` objects of a `NetworkSet` have a `params` property containing a dictionary of the named parameters and values associated to each Network, it is possible to select the Networks corresponding to a subset of named parameters using the `.sel()` method. \n",
421 "\n",
422 "The following example illustrates this feature."
423 ]
424 },
425 {
426 "cell_type": "code",
427 "execution_count": null,
428 "metadata": {},
429 "outputs": [],
430 "source": [
431 "# dummy named parameters and values 'a', 'X' and 'c'\n",
432 "import numpy as np\n",
433 "\n",
434 "params = [\n",
435 " {'a':0, 'X':10, 'c':'A'},\n",
436 " {'a':1, 'X':10, 'c':'A'},\n",
437 " {'a':2, 'X':10, 'c':'A'},\n",
438 " {'a':1, 'X':20, 'c':'A'},\n",
439 " {'a':0, 'X':20, 'c':'A'},\n",
440 " ]\n",
441 "# create a NetworkSet made of dummy Networks, each define for set of parameters\n",
442 "freq1 = rf.Frequency(75, 110, 101, 'ghz')\n",
443 "rng = np.random.default_rng()\n",
444 "ntwks_params = [rf.Network(frequency=freq1, s=rng.uniform(size=(len(freq1),2,2)),\n",
445 " name=f'ntwk_{m}', comment=f'ntwk_{m}', params=params)\n",
446 " for (m, params) in enumerate(params) ]\n",
447 "ns = rf.NetworkSet(ntwks_params)\n",
448 "print(ns)"
449 ]
450 },
451 {
452 "cell_type": "markdown",
453 "metadata": {},
454 "source": [
455 "Selecting the sub-NetworkSet matching scalar parameters can be made as:"
456 ]
457 },
458 {
459 "cell_type": "code",
460 "execution_count": null,
461 "metadata": {},
462 "outputs": [],
463 "source": [
464 "ns.sel({'a': 1})"
465 ]
466 },
467 {
468 "cell_type": "code",
469 "execution_count": null,
470 "metadata": {},
471 "outputs": [],
472 "source": [
473 "ns.sel({'a': 0, 'X': 10})"
474 ]
475 },
476 {
477 "cell_type": "markdown",
478 "metadata": {},
479 "source": [
480 "Selecting the sub-NetworkSet matching a range of parameters also works:"
481 ]
482 },
483 {
484 "cell_type": "code",
485 "execution_count": null,
486 "metadata": {},
487 "outputs": [],
488 "source": [
489 "ns.sel({'a': 0, 'X': [10,20]})"
490 ]
491 },
492 {
493 "cell_type": "code",
494 "execution_count": null,
495 "metadata": {},
496 "outputs": [],
497 "source": [
498 "ns.sel({'a': [0,1], 'X': [10,20]})"
499 ]
500 },
501 {
502 "cell_type": "markdown",
503 "metadata": {},
504 "source": [
505 "The various named parameter keys and values of the NetworkSet can be retrieved using the `dims` and `coords` properties: "
506 ]
507 },
508 {
509 "cell_type": "code",
510 "execution_count": null,
511 "metadata": {},
512 "outputs": [],
513 "source": [
514 "ns.dims"
515 ]
516 },
517 {
518 "cell_type": "code",
519 "execution_count": null,
520 "metadata": {},
521 "outputs": [],
522 "source": [
523 "ns.coords"
524 ]
525 },
526 {
527 "cell_type": "markdown",
528 "metadata": {},
529 "source": [
530 "## Interpolating between the Networks of a NetworkSet\n",
531 "It is possible to create new Networks interpolated from the Networks contained in a `NetworkSet`. If no `params` properties have been defined for each Network of the NetworkSet, the `interpolate_from_network()` method can be used to specify a interpolating parameter. "
532 ]
533 },
534 {
535 "cell_type": "code",
536 "execution_count": null,
537 "metadata": {},
538 "outputs": [],
539 "source": [
540 "param_x = [1, 2, 3] # a parameter associated to each Network\n",
541 "x0 = 1.5 # parameter value to interpolate for\n",
542 "interp_ntwk = ro_ns.interpolate_from_network(param_x, x0)\n",
543 "print(interp_ntwk)"
544 ]
545 },
546 {
547 "cell_type": "markdown",
548 "metadata": {},
549 "source": [
550 "An illustrated example is given in the [Examples section](../examples/index.rst#networksets) of the documentation.\n",
551 "\n",
552 "It is also possible to interpolate using a named parameter when they have been defined: "
553 ]
554 },
555 {
556 "cell_type": "code",
557 "execution_count": null,
558 "metadata": {},
559 "outputs": [],
560 "source": [
561 "# Interpolated Network for a=1.2 within X=10 Networks:\n",
562 "ns.interpolate_from_params('a', 1.2, {'X': 10})"
563 ]
564 }
565 ],
566 "metadata": {
567 "anaconda-cloud": {},
568 "celltoolbar": "Raw Cell Format",
569 "kernelspec": {
570 "display_name": "Python 3 (ipykernel)",
571 "language": "python",
572 "name": "python3"
573 },
574 "language_info": {
575 "codemirror_mode": {
576 "name": "ipython",
577 "version": 3
578 },
579 "file_extension": ".py",
580 "mimetype": "text/x-python",
581 "name": "python",
582 "nbconvert_exporter": "python",
583 "pygments_lexer": "ipython3",
584 "version": "3.8.10"
585 }
586 },
587 "nbformat": 4,
588 "nbformat_minor": 1
589 }