Sorting Network Sets

Frequently a set of Networks is recorded while changing some other variable; like voltage, or current or time. So… now you have this set of data and you want to look at how some feature evolves, or calculate some representative statics. This example demonstrates how to do this using NetworkSets.

Generate some Data

For the purpose of this example we use a predefined skrf.Media object to generate some networks, and save them as a series of touchstone files. Each file is named with a timestamp, generated with the convenience function rf.now_string().

In [1]:
from time import sleep
import skrf as rf
%matplotlib inline
from pylab import *
rf.stylely()


!rm -rf tmp
!mkdir tmp

wg = rf.wr10 # just a dummy media object to generate data
wg.frequency.npoints = 101


for k in range(10):
    # timestamp generated with `rf.now_string()`
    ntwk = wg.random(name=rf.now_string()+'.s1p')
    ntwk.s = k*ntwk.s
    ntwk.write_touchstone(dir='tmp')
    sleep(.1)

Lets take a look at what we made

In [2]:
ls tmp
2018.03.26.13.26.59.143715.s1p  2018.03.26.13.26.59.668674.s1p
2018.03.26.13.26.59.250038.s1p  2018.03.26.13.26.59.774930.s1p
2018.03.26.13.26.59.355774.s1p  2018.03.26.13.26.59.878803.s1p
2018.03.26.13.26.59.460680.s1p  2018.03.26.13.26.59.986551.s1p
2018.03.26.13.26.59.564717.s1p  2018.03.26.13.27.00.090384.s1p

Not sorted (default)

When created using NetworkSet.from_dir(), the NetworkSet’s stores each Network randomly

In [3]:
ns = rf.NS.from_dir('tmp')
ns.ntwk_set
Out[3]:
[1-Port Network: '2018.03.26.13.26.59.668674',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.878803',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.460680',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.250038',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.774930',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.564717',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.355774',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.27.00.090384',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.143715',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.986551',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j]]

Sort it

In [4]:
ns.sort()
ns.ntwk_set
Out[4]:
[1-Port Network: '2018.03.26.13.26.59.143715',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.250038',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.355774',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.460680',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.564717',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.668674',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.774930',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.878803',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.986551',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.27.00.090384',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j]]

Sorting using key argument

You can also pass a function through the key argument, which allows you to sort on arbitrary properties. For example, we could sort based on the sub-second field of the name,

In [5]:
ns = rf.NetworkSet.from_dir('tmp')
ns.sort(key = lambda x: x.name.split('.')[0])
ns.ntwk_set
Out[5]:
[1-Port Network: '2018.03.26.13.26.59.668674',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.878803',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.460680',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.250038',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.774930',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.564717',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.355774',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.27.00.090384',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.143715',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j],
 1-Port Network: '2018.03.26.13.26.59.986551',  75.0-110.0 GHz, 101 pts, z0=[50.+0.j]]

Extracting Datetimes

You can also convert the ntwk names to datetime objects, in case you want to plot something with pandas or do some other processing. There is a companion function to rf.now_string() which is rf.now_string_2_dt(). How creative..

In [6]:
ns.sort()
dt_idx = [rf.now_string_2_dt(k.name ) for k in ns]
dt_idx
Out[6]:
[datetime.datetime(2018, 3, 26, 13, 26, 59, 143715),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 250038),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 355774),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 460680),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 564717),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 668674),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 774930),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 878803),
 datetime.datetime(2018, 3, 26, 13, 26, 59, 986551),
 datetime.datetime(2018, 3, 26, 13, 27, 0, 90384)]

Put into a Pandas DataFrame and Plot

The next step is to slice the network set along the time axis. For example we may want to look at S11 phase, at a few different frequencies. This can be done with the following script. Note that NetworkSets can be sliced by frequency with human readable strings, just like Networks.

In [7]:
import pandas as pd
dates = pd.DatetimeIndex(dt_idx)

# create a function to pull out S11 in degrees at a specific frequency

s_deg_at = lambda s:{s: [k[s].s_deg[0,0,0] for k in ns]}

for f in ['80ghz', '90ghz','100ghz']:
    df =pd.DataFrame(s_deg_at(f), index=dates)
    df.plot(ax=gca())
title('Phase Evolution in Time')
ylabel('S11 (deg)')
Out[7]:
Text(0,0.5,'S11 (deg)')
../../_images/examples_networksets_Sorting_Network_Sets_20_1.png

Visualizing Behavoir with signature

It may be of use to visualize the evolution of a scalar component of the network set over all frequencies. This can be done with a little bit of array manipulation and imshow. For example if we take the magnitude in dB for each network, and create 2D matrix from this,

In [8]:
mat = array([k.s_db.flatten() for k in ns])
mat.shape
/home/docs/checkouts/readthedocs.org/user_builds/scikit-rf/conda/stable/lib/python3.5/site-packages/scikit_rf-0.14.9-py3.5.egg/skrf/mathFunctions.py:163: RuntimeWarning: divide by zero encountered in log10
  out = 20 * npy.log10(input)
Out[8]:
(10, 101)

This array has shape ( ‘Number of Networks’ , ‘Number frequency points’). This can be visualized with imshow. Most of the code below just adds labels, and axis-scales.

In [9]:
freq = ns[0].frequency

# creates x and y scales
extent = [freq.f_scaled[0], freq.f_scaled[-1], len(ns) ,0]

#make the image
imshow(mat, aspect='auto',extent=extent,interpolation='nearest')

# label things
grid(0)
freq.labelXAxis()
ylabel('Network #')
cbar = colorbar()
cbar.set_label('Magntidue (dB)')
../../_images/examples_networksets_Sorting_Network_Sets_25_0.png

This process is automated with the method NetworkSet.signature(). It even has a vs_time parameter which will automatically create the DateTime index from the Network’s names, if they were written by rf.now_string()

In [10]:
ns.signature(component='s_db', vs_time=True,cbar_label='Magnitude (dB)')
/home/docs/checkouts/readthedocs.org/user_builds/scikit-rf/conda/stable/lib/python3.5/site-packages/scikit_rf-0.14.9-py3.5.egg/skrf/mathFunctions.py:163: RuntimeWarning: divide by zero encountered in log10
  out = 20 * npy.log10(input)
Out[10]:
<matplotlib.image.AxesImage at 0x7fc223c11390>
../../_images/examples_networksets_Sorting_Network_Sets_27_2.png