HackMIT: Polyglass

I attended HackMIT 2013 and had an absolute blast! Kartik Talwar, Spencer Hewett and I make a fantastic team.

Photo by Kevin Yiming Chen © Copyright 2013 Kevin Yiming Chen

In the turmoil that followed the hackathon (taking a weekend off of schoolwork and research has consequences), it slipped my mind to post our project!

We created Polyglass. A Google Glass application that analyzes the video in slow motion using the Eulerian magnification algorithm to calculate the human pulse from a video frame in quasi real-time. Using video alone, we achieve the same functionality as a polygraph.

The picture below is a demo of our project; it compares the original video frame to the frame processed by Eulerian Magnification. This processing is an implementation of the Eulerian Magnification framework published by MIT.

Portions of our source are on Kartik’s github.

Braille Cell with VPython

The internet went down at my house, and I decided to play with vpython again!

#type individual brl cell using numkeys
from visual import sphere
R = 0.2 #filled dot radius
r = 0.1 #empty dot radius

#corresponds to numkeys
dotdict = {
'7': [1,3], #dot 1 
'4': [1,2], #dot 2
'1': [1,1], #dot 3
'8': [2,3], #dot 4
'5': [2,2], #dot 5
'2': [2,1] #dot 6

fulldot = dotdict.keys()

def draw(dots):
 [sphere(pos=dotdict[dot], radius=r) for dot in fulldot] #create empty dot matrix to represent empty cell
 [sphere(pos=dotdict[dot], radius=R) for dot in dots] #fill appropriate dots

print "Please enter stringn7t8n4t5n1t2n: "
string = raw_input()
if string.isdigit(): draw(str(string))

Let’s see it in action! The letter “j” (or “just” in G2):

Simple Cubic Lattice

Today, let’s have some fun playing with perspective rendering in Python! My graphics package of choice is VPython:

<code>sudo apt-get install python-visual</code>

Suppose we want to represent a simple cubic lattice: Using the visual package, we can create a collection of spheres at positions $(i,j,k)$ with $i,j,k = -L…L$.

from visual import sphere
L = 5
R = 0.3
for i in range(-L,L+1):
 for j in range(-L,L+1):
  for k in range(-L,L+1):

Resulting in this beautiful rendering:

But what if we want to change the color? Simply alter the last line:

<code>sphere(pos=[i,j,k],radius=R).color = color.blue</code>

Less atoms? Alter the value of L.

L = 1

Same number of atoms with smaller radii? Alter the value of R.

English to Morse Translator

Today, I wanted to code an efficient letter to Morse code translator in Python, and whipped this up. I’ve found that a familiarity with many of Python’s lesser known built-in functions is quite useful in situations such as this!

Just for fun, I encourage the reader to use this script to leave their comments in Morse.

#Catherine Ray

morse.py {string} [{string} ... ]:
 Translates string of english letters and spaces to morse code.

import string
import sys

if len(sys.argv)>=2:
 if sys.argv[1].isalpha():
  text = ''.join(sys.argv[1:]).lower()
  morse = ["01","1000","1010","100","0", "0010", "110", "0000", "00", "0111", "101", "0100", "11", "10", "111", "0110", "1101", "010", "000", "1", "001", "0001", "011", "1001", "1011", "1100"]  letter = map(chr, range(97, 123))
  LETTER_TO_MORSE = dict(zip(letter, morse))
  morse_out = [LETTER_TO_MORSE[x] for x in text]  print ' '.join(morse_out).replace("1","-").replace("0",".")
  "Restrict yourself to the english alphabet."
 print "Enter a string to translate, friend!"

Consequent Challenge

Today, as I was writing a post on sorting in vim, I issued myself a challenge.
The challenge: without using a bash script, write a one liner that reads through all the lines in a file, sorts them and printed these sorted lines to stdout. Do so in under a minute without using the internet.

Before you continue, I encourage you to try it yourself. Get out a timer … Ready? Go!

In about 35 seconds, I had this:
print 'n'.join(sorted([line.strip() for line in open("file.txt")]))
Although this one-liner works, the filename is hardcorded. Quick fix (finished at 52 seconds):
import sys; print 'n'.join(sorted([line.strip() for line in open(sys.argv[1])]))
I find that self-issuing pseudorandom timed challenges is a fun way to train yourself to work under pressure. ‘Tis one of the many ways to gamify everyday life!

Sidenote: Do not fall into the trap of using one-liners in actual code. The Pythonic way to do this is:

import sys
 with open(sys.argv[1]) as f:
  for line in f:
   print 'n'.join(sorted(line.strip())) 
except IOError:
 print "File does not exist."