Monday, September 3, 2012

Windows Woes.

I installed Py2exe, and successfully produced a hello.exe executable. Apparently, Py2exe only works with Python2.6.

I then installed WinPython, which uses Python2.7, because it offers Numpy and Scipy, because I couldn't find a 64-bit NumPy installation for Python 2.6.

This raises the question, "Can I make an executable of a script importing NumPy, that uses Python2.7, using Py2exe, which uses Python2.6?" Stupid windows.

Monday, July 30, 2012

Non-negative Matrix Factorization

I found some Python code for Non-negative Matrix Factorization. I've altered it to run more like the NMF code in Programming Collective Intelligence. Since the factorization depends on two random matrices, it might be best to factorize several times, and then cluster a set of H matrices using Affinity Propagation, or K-Means.

Tuesday, July 24, 2012

One-Liner for Finding Peaks or Troughs in Data

To find the indices of the peaks or troughs in a data set x, using Python of MATLAB, we use
pks = find( diff( sign( diff( x ) ) ) < 0 ) + 1
trs = find( diff( sign( diff( x ) ) ) > 0 ) + 1 
 

Sunday, July 8, 2012

Arduino and Processing

Arduino is a type of microcontroller. It is also the name of the programming language used for said microcontroller. Processing is a verb and a programming language for use along with Arduinos running an Arduino sketch. Confusing? Both Arduino and Processing programs are called sketches, and have a .pde suffix. Generally, we'll have an Arduino sketch running on the Arguing device, while a Processing sketch is running on the computer. Whew.

Friday, July 6, 2012

Exercise 2.4 from the Arduino Cookbook

I used two push-button switches with four prongs. It only worked when the prongs were attached diagonally.



Monday, May 14, 2012

Hierarchical Difference

The idea is that larger regions should have more weight than smaller regions. Let R=0 be the largest scale, where the picture is represented by one pixel. Then R=1 is the next smallest scale, where the picture is represented by four pixels, and R=2 is the scale where the picture is represented by 16 pixels, and so on. Then we can calculate the difference between two images at multiple scales. We can also weight the different scales so that larger scales are more important. Here is some code:

def hierarchical_scale( x ):
    scales = list()
    rows, cols = x.shape
    dim = np.max( x.shape )
    R = int( np.log2( dim ) )
    for r in range( R ):
        means = list()
        for i in range( 2**r ):
            for j in range( 2**r ):
                ibin =   i   * rows / 2**r
                ifin = (i+1) * rows / 2**r
                jbin =   j   * cols / 2**r
                jfin = (j+1) * cols / 2**r
                means.append(np.mean(x[ibin:ifin,jbin:jfin]))
        scales.append( means )
    return scales

We then calculate the difference using the function:

def hierarchical_difference( x, y, scaled=True ):
    hsx, hsy = hierarchical_scale( x ), hierarchical_scale( y )
    s = 0
    for i in range( min( [ len( hsx ), len( hsy ) ] ) ):
        d = [ (hsx[i][j]-hsy[i][j])**2 for j in range(len(hsx[i])) ]
        if scaled:
            d = np.array( d ) * ( 1.0 / 2**(i) )
        else:
            d = np.array( d ) * ( 1.0 / 2**(0) )
        m = np.mean( d )
        s += m
    return s

 We can view all scales of an image using this function:

def view_all_scales( data ):
    scales = hierarchical_scale( data )
    no_scales = len( scales )
    for i in range( no_scales ):
        s = scales[ i ]
        a = np.array(s).reshape((np.sqrt(len(s)), np.sqrt(len(s)) ))
        matshow( a, cmap='gray' ) ;

Saturday, February 25, 2012

Defining functions for TikZ

The "easiest" way I've found to define "functions" in TikZ is to use the LaTeX \newcommand outside of the \tikzpicture environment.
\newcommand{\cir}[3]{ \def \x{ (#1,#2) circle (#3cm) } \draw \x ; }
And then use this inside the tikzpicture as:
\cir{0}{0}{1}
However, since the circle is drawn within the \newcommand, I can't rotate the figure inside the \tikzpicture environment.