Plotting Functions

Mumodo Demo Notebook -- Updated on 24.04.2015

Summary: This notebook presents plotting functions available in mumodo.

(c) Dialogue Systems Group, University of Bielefeld


In [1]:
%matplotlib inline
from mumodo.plotting import plot_annotations, plot_reco, plot_scalar, plot_vector
from IPython.html.widgets import fixed, FloatSliderWidget, interact, CheckboxWidget, TextWidget
from mumodo.mumodoIO import open_intervalframe_from_increco, \
                            open_streamframe_from_xiofile, \
                            open_intervalframe_from_textgrid
import pandas as pd

Plotting annotations

A very convenient function for plotting annotations is the homonymous function. You can always directly plot an imported textgrid:


In [2]:
myannotation = open_intervalframe_from_textgrid("sampledata/test.TextGrid")
plot_annotations(myannotation)


However, the plot_annotations function has many customizations (check the docstring!), e.g:


In [3]:
plot_annotations(myannotation, time_begin=0, time_end=14,
                 colorindex=['b', 'g', 'y'],
                 hscale=4, vscale=1.5, 
                 linespan=5, text_kwargs={'color': 'w', 'fontsize': '18'},
                 tierorder=['S', 'O', 'CLAPS'])


Note how the CLAPS point tier is drawn with some thickness, in order to become visible. Also, how the text_kwargs is a matplotlib fontdict in order to make the text appear (check the matplotlib documentation for all the options)

It is also possible to plot color-coded finite label sets:


In [4]:
myannotatedlabels = pd.DataFrame([(0.0, 1.0, 'A'),
                                  (1.0, 2.0, 'B'),
                                  (4.0, 5.0, 'C'),
                                  (6.0, 7.0, 'A'),
                                  (8.0, 10.0, 'C'),
                                  (11.0, 12.0, 'B')], columns=['start_time', 'end_time', 'text'])
myannotatedlabels


Out[4]:
start_time end_time text
0 0 1 A
1 1 2 B
2 4 5 C
3 6 7 A
4 8 10 C
5 11 12 B

In [5]:
plot_annotations({'labels': myannotatedlabels}, colorindex={'A': 'r', 'B': 'b', 'C': 'g'})


It is possible to combine the two schemes of colorindexing, by making the dict an item in the list, e.g.


In [6]:
myannotation['labels'] = myannotatedlabels
plot_annotations(myannotation, time_begin=0, time_end=12,
                 tierorder=['O', 'S', 'labels'],
                 colorindex=['b', 'r', {'A': 'r', 'B': 'b', 'C': 'g'}])


Plotting Tracking Data


In [7]:
myframe = open_streamframe_from_xiofile("sampledata/test.xio.gz", 
                                        'VeniceHubReplay/Venice/Body1',
                                        timestamp_offset=-9616)
myframe.dropna(inplace=True)


opening compressed file ...
opening file without indexing

In [8]:
myframe['righthand'] = myframe['JointPositions3'].map(lambda x: x[11])

In [9]:
myannotation['CLAPS']


Out[9]:
time mark
0 11.654230 First Clap
1 33.824485 Second Clap
2 47.672685 Third Clap

We want to look at the trajectory of the right hand around a "high-five" clap gesture (see the demo video)

plot_vector() 

can plot 3D data for us. The color codes the time at which each position is reached


In [10]:
plot_vector(myframe, 'righthand', 11000, 13000)


If the above is not clear enough, we can look at each axis separately:


In [11]:
myframe['righthandX'] = myframe['righthand'].map(lambda x: x.x)
myframe['righthandY'] = myframe['righthand'].map(lambda x: x.y)
myframe['righthandZ'] = myframe['righthand'].map(lambda x: x.z)

In [12]:
plot_scalar(myframe, ['righthandX', 'righthandY', 'righthandZ'], 11000, 13000)


We can also plot both people's hands


In [13]:
myframe['righthand2'] = myframe['JointPositions4'].map(lambda x: x[11])
myframe['righthandX2'] = myframe['righthand2'].map(lambda x: x.x)
myframe['righthandY2'] = myframe['righthand2'].map(lambda x: x.y)
myframe['righthandZ2'] = myframe['righthand2'].map(lambda x: x.z)

In [14]:
plot_scalar(myframe, ['righthandY', 'righthandY2'], 11000, 13000)



In [15]:
plot_scalar(myframe, ['righthandZ', 'righthandZ2'], 11000, 13000)



In [16]:
plot_scalar(myframe, ['righthandX', 'righthandX2'], 11000, 13000)


We see how the hands of the two people come together during the clap gesture

Plotting IncRecos


In [17]:
myinc_reco = open_intervalframe_from_increco('sampledata/test.inc_reco')

A special function plot_reco can be used to plot incremental results (see the Basic Types notebooks) up to the current time, denoted by the red line. The diamonds represent updates. The customizations allow the latest K updates to be shown as tiers while the times of updates further in the past are shown as grey diamonds


In [18]:
plot_reco(iframe=myinc_reco, latest_tiers=3, current_time=4, max_history=3, hscale=3)


With the help of IPYthon widgets, inc_reco plots can be altered interactively


In [19]:
i = interact(plot_reco,
             current_time=FloatSliderWidget(min=0, 
                                            max=max([float(x) for x in myinc_reco.keys()]),
                                            step=0.01,
                                            description='current_time',
                                            value=4.34),
             latest_tiers=FloatSliderWidget(min=1, 
                                            max=len(myinc_reco.keys()),
                                            step=1,
                                            value=3,
                                            description='latest_tiers'),
             printing=CheckboxWidget(description='printing'),
             iframe=fixed(myinc_reco), hscale=fixed(1.5), max_history=fixed(1))


Saving plots to files

All of the plotting functions have a filepath keyword argument, that - if set - saves the plot as you see it to a file. The filename you give MUST have a valid extension signifying the file format such as png, pdf, bmp, etc


In [20]:
plot_scalar(myframe, ['righthandX', 'righthandY', 'righthandZ'], 11000, 13000, filepath="scalars.png")