Regular expressions

Posted on Mon 09 December 2013 in Notes

pypix advanced REGEX tips

And here's a nice cheat-sheet curtesy of HN's comments REGEX cheat sheet

Even better is [pypix advanced REGEX tips

And here's a nice cheat-sheet curtesy of HN's comments REGEX cheat sheet

Even better is](http://www.regex101.com " regular expressions 101 — an online regex tester for javascript, php, pcre and python. ") – this web-app is nothing short of amazing! You can type in all sorts of regular expression flavours and can live see how the search (and replace!) will work. More over it will interpret your regular expression into human readable instructions and give you a reason for why each matched string matched.


Decorators and python objects demystified

Posted on Thu 05 December 2013 in Notes

This article explains very well how decorators works, and a lot about how everything in Python is an object (last time I'll be making a variable with the same name as a function!)

http://yasoob.github.io/blog/python-decorators-demystified/

The jist is that any function is an object and can be parsed into another function, and the @-decorator is shorthand for doing just that!

def func_a(func):
    print "func a"
    func()
def func_b():
    print "func b"

func_b = func_a(func_b)
func_b()
# func a
# func b

is the same as

def func_a(func):
    print "func a"
    func()
@func_a
def func_b():
    print "func b"

func_b()
# func a
# func b

Finding coordinates in a picture

Posted on Sun 01 December 2013 in Notes

This is a very handy one liner I found in Programming Computer Vision, that can give you the coordinates of non-black pixels in an image.

In [25]: a = np.random.randint(10, size =(5,5))

In [26]: a[a>6] = 0

In [27]: a
Out[27]: 
array([[8, 0, 0, 0, 6],
       [0, 6, 8, 0, 0],
       [9, 0, 0, 6, 6],
       [0, 0, 0, 8, 0],
       [0, 0, 0, 0, 0]])

In [28]: coord = np.array(a.nonzero()).T

In [29]: coord
Out[29]: 
array([[0, 0],
       [0, 4],
       [1, 1],
       [1, 2],
       [2, 0],
       [2, 3],
       [2, 4],
       [3, 3]])

It's the np.array(a.nonzero()).T where the magic happens.


Iteration count in python

Posted on Sun 01 December 2013 in Notes

This is a quite nifty "trick" in Python. Often times you want to iterate an object (array or list, or something else) and you want to use each item you iterate over while keeping count of how many iterations you've been through. The enumerate() function does this!

In [14]: datapoints = np.random.randint(10, size = (4,4))

In [15]: targets = np.random.randint(10, size = (4,4))

In [16]: for i, data in enumerate(datapoints):
   ....:     print i, '\t', data, data == targets[i]
   ....:     
0     [9 8 4 1]    [False False False False]
1     [2 1 9 1]    [False False False False]
2     [1 9 8 1]    [False False False False]
3     [5 5 0 4]    [False False False False]

Hidden Markov Model

Posted on Tue 26 November 2013 in Notes

Bayes theorem / understanding conditional probability P(A|B)

And two newer videoes show the usage

Hidden Markov Model