Using Python to build quick GUIs (for labelling sensor data on the iPAQ)

back to notes

 

Python

Since the Familiar distribution contains the python scripting language anyway, we might just as well use it to quickly write GUIs that interface with the terminal. See python.org for an excellent introduction.

The first script writes a character corresponding to the context in the terminal where the application should be running. When adding or changing contexts, all you need to do is change the context and action array, the rest of the script will deal with the changes.

The second script is useful for annotating sensor data in a second file, using the systems' time and date (which are also stored in the sensor file). Combining the two files gives the required, annotated, information.

The last script is a bit more reliable since it writes to the application through a file.

 

   

The first script: the basics

link to the file

#! /usr/bin/env python

from gtk import *

context = ["Sitting", "Standing up", "Walking", "Running", "Location 1", "Location 2"]
action = ["a", "b", "c", "d", "e", "f"]
contextbutton = range(len(context))

def goforit(widget, nr):
   print (action[nr])

def killme(widget,f):
   mainquit()

window = GtkWindow(WINDOW_TOPLEVEL)
window.set_name("CONTEXT")
window.set_usize(180,300)
window.connect("delete_event", killme)

vbox = GtkVBox()
window.add(vbox)
vbox.show()

for i in range(len(context)):
   contextbutton[i] = GtkButton(context[i])
   vbox.add(contextbutton[i])
   contextbutton[i].show()
   contextbutton[i].connect("clicked", goforit, i)

window.show()

mainloop()

 

   

The second script: annotation of sensor data

#! /usr/bin/env python

# UI script for sensor data annotation on the iPAQ
# by Kristof Van Laerhoven, Lancaster University

from gtk import *
from time import strftime, localtime, time

context = ['Sitting', 'Standing.up', 'Walking', 'Running', 'At.desk', 'In.hallway']
contextbutton = range(len(context))

def goforit(widget, nr):
  print (action[nr])
  fo = open('/tmp/ui_o.lnk','a')
  fo.write(context[nr])
  fo.write(' ')
  fo.write(strftime("%a, %d %b %Y %H:%M:%S +0000",  localtime(time())))
  fo.write('\n')
  fo.close()

def killme(widget,f):
  mainquit()

window = GtkWindow(WINDOW_TOPLEVEL)
window.set_name("ANNOTATE")
window.set_usize(180,300)
window.connect("delete_event", killme)

vbox = GtkVBox()
window.add(vbox)
vbox.show()

for i in range(len(context)):
   contextbutton[i] = GtkButton(context[i])
   vbox.add(contextbutton[i])
   contextbutton[i].show()
   contextbutton[i].connect("clicked", goforit, i)

window.show()

mainloop()

   

The third script: full-fledged context awareness

#! /usr/bin/env python

# UI script for context annotation on the iPAQ
# by Kristof Van Laerhoven, Lancaster University

from gtk import *
from time import strftime, localtime, time
from os import system

context = ['Sitting', 'Standing.up', 'Walking', 'Running', 'At.desk', 'In.hallway']
action = ["a", "b", "c", "d", "e", "f"]
contextbutton = range(len(context))
radiobutton = range(len(context))

this_c = ""
old_c = ""

def train(widget, nr):
  #print (action[nr])
  fo = open('/tmp/ui_o.lnk','w')
  fo.write(action[nr])
  fo.write(' ')
  fo.write(strftime("%a, %d %b %Y %H:%M:%S +0000",  localtime(time())))
  fo.write('\n')
  fo.close()
  system('/mnt/ramfs/flite -t ' + context[nr] )

def killme(widget,f):
  mainquit()

def update_ui(this_context, old_context):
  ft = open('/tmp/ui_i.lnk','r')
  for i in range(len(context)):
     str = ft.readline()
     radiobutton[i].set_text( str )
     if str == 'X\n':
        old_context = this_context
        this_context = context[i];
  ft.close()
  ft = open('/tmp/ui_o.lnk','w')
  ft.write('n');
  ft.close()
  if this_context != old_context:
     system('/mnt/ramfs/flite -t ' + this_context )
  timeout_add(1000, update_ui, this_context, old_context)

window = GtkWindow(WINDOW_TOPLEVEL)
window.set_name("CONTEXT")
window.set_usize(180,300)
window.connect("delete_event", killme)

hbox = GtkHBox()
vbox1 = GtkVBox()
hbox.add(vbox1)
vbox2 = GtkVBox()
hbox.add(vbox2)
vbox1.show()
vbox2.show()
hbox.show()
window.add(hbox)

for i in range(len(context)):
   contextbutton[i] = GtkButton(context[i])
   vbox2.add(contextbutton[i])
   contextbutton[i].show()
   contextbutton[i].connect("clicked", train, i)
   radiobutton[i] = GtkLabel("0.00 %")
   radiobutton[i].show()
   vbox1.add(radiobutton[i])

window.show()

timeout_add(1000, update_ui, this_c, old_c)

mainloop()

 

   

Screenshots

The iPAQs screen with the main program running on a terminal in the top right corner. Annotation can be done with the standard keyboard by pressing a specific key..

...but the python script makes it a lot easier.

If you want to add/change/remove contexts (and their buttons), just remove them from the context array, and the rest of the script will cope with it and change to the new settings.

 

 

 

 

Compiled by Kristof Van Laerhoven.