Tuesday, 22 March 2011

Change primary display in Ubuntu 10.10

My desktop at my office doesn't use NVidia graphics card, but uses ATI Radeon. So I don't have the monitor setting application from NVidia I usually use for my laptop. So I searched a bit, and found a way to do it through command line.

The command to use is xrandr. It is the command line interface to the RandR X extension which is used to configure which display ports are enabled, and to configure display modes and properties such as orientation, reflection and DPI.

Usage:
# To query what screens are connected, type the following:
$ xrandr -q
# Using the name of the output from the query, you can set 
# a display output as the primary display:
$ xrandr --output CRT2 --primary
To make this run at the startup, go to System=>Preferences=>Startup Applications
Click Add
Name: XRANDR
Command: xrandr --output CRT2 --primary
Source: http://www.thinkwiki.org/wiki/Xorg_RandR_1.2

Monday, 21 March 2011

Ruby: block

Blocks can be closures
In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Blocks are closures, which means variables in the surrounding scope that are referenced in a block remain accessible for the life of that block and the life of and Proc object created from that block.

Example:
def n_times(thing)
  lambda {|n| thing * n}
end

p1 = n_times(23)
p1.call(3) # => 69
p1.call(4) # => 92

Compare with Python closures:
def generate_power_func(n):
  def nth_power(x): 
    return x**n
  return nth_power
end

raised_to_4 = generate_power_func(4)
raised_to_4(2)
Blocks can be objects
Block can be converted to an object of class Proc. There are several ways where blocks are converted to objects.
  • If the last parameter in a method definition is prefixed with an ampersand (such as &action), Ruby looks for a code block whenever that method is called. 
  • Use lambda or its alternative -> form. For example:
    bo = lambda { |param| puts "You called me with #{param}" }
    bo.call 99
    bo.call "cat"
    # produces:
    # You called me with 99
    # You called me with cat
    
    lam = ->(p1, p2) { p1 + p2 }
    lam.call(4, 3) # => 7
    Compare this with the "bound function operator" in CoffeeScript:
    callback = (message) => @voicement.push message
    
  • Use Proc.new

  • The call method on a proc object invokes the code in the original block.

The Symbol.to_proc trick
Ruby implements the to_proc for objects of class symbol.
names = %w{ant bee cat}
result = names.map {|name| name.upcase}
result = names.map {&:upcase}
The last line means: apply the upcase method to each element of names. This works by relying on Ruby's type coercion. When you say names.map(&xxx), you're telling Ruby to pass the Proc object in xxx to the map method as a block. If xxx isn't already a Proc object, Ruby tries to coerce it into one by sending it a to_proc message. If it was written Ruby, it would look something like this:
def to_proc
  proc { |obj, *args| obj.send(self, *args) } # same as lambda
end
It's an incredibly elegant use of coercion and of closures. However, the use of dynamic method invocations mean that the version of code that uses &:upcase is about half as fast as the more explicitly coded block. This doesn't matter so much unless in the performance-critical section of your code.

Sunday, 20 March 2011

Depth of field

Hyperfocal distance (超焦距)
The hyperfocal distance is the nearest focus distance at which the DOF extends to infinity; focusing the camera at the hyperfocal distance results in the largest possible depth of field for a given f-number.  Let f be the lens focal lengthN be the lens f-number, and c be the circle of confusion for a given image format. The hyperfocal distance H is given by
H \approx \frac {f^2} {N c} \,.

Formula for DOF:
\mathrm {DOF} \approx \frac {2 N c f^2 s^2} {f^4 - N^2 c^2 s^2} \,.
This means:
  • DOF increases as f-stop increases or aperture decreases
  • DOF increases as subject distance (distance at which the camera is focused) increases
  • DOF increases as focal length decreases. 

Monday, 14 March 2011

JNI header file generation using Eclipse


Here is an example of using the External Tools Configuration in Eclipse to generate the JNI header file under WindowXP.

1) Choose External Tools Configuration in the tool bar:


2) Under the Main tab of a new configuration, setup the following parameters:
Location: [location of javah.exe (e.g. C:\Program Files\Java\jdk1.6.0_14\bin\javah.exe)]
Working Directory: [project directory (e.g. ${workspace_loc:/camera})]
Arguments:
-jni
-classpath .;[directory of the .class file (e.g. ${workspace_loc:/camera}/bin)]
-o [output file name (e.g. ${workspace_loc:/camera}/pointgrey_trigger/pointgrey_trigger/firefly_driver_jni.h)] [java file with the native methods declaration (e.g. edu.mit.yingyin.camera.WebcamDriverFirefly)]

Note that in *nix OS, the classpaths are concatenated using ":".

Friday, 11 March 2011

Making jar file using ant

Today I tested with making a jar file using ant according to a tutorial. Below are the simple build.xml I used and it works.


  
    
  
  
    
  


But I wasn't sure what "**" means in the includes file pattern. Turns out that this is a special feature in the ant syntax which makes it possible to match multiple directory levels. This can be used to match a complete directory tree, or a file anywhere in the directory tree. To do this, ** must be used as the name of a directory. When ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/** matches all files/directories under /test/, such as /test/x.java, or /test/foo/bar/xyz.html, but not /xyz.xml. Details can be found here.

Thursday, 10 March 2011

RESTful Web Services

How the HTTP methods are typically used to implement a web service:
Resource GET PUT POST DELETE
Collection URI List the URIs and perhaps other details of the collection's members. Replace the entire collection with another collection. Create a new entry in the collection. The new entry's URL is assigned automatically and is usually returned by the operation. Delete the entire collection.
Element URI Retrieve a representation of the addressed member of the collection, expressed in an appropriate Internet media type. Replace the addressed member of the collection, or if it doesn't exist, create it. Treat the addressed member as a collection in its own right and create a new entry in it. Delete the addressed member of the collection.


Difference between GET and POST as form submission methods
  • GET: With the HTTP "GET" method, the form data set is appended to the URI specified by the action attribute (with a question-mark ("?") as separator) and this new URI is sent to the processing agent.
  • POST: With the HTTP "POST" method, the form data set is included in the body of the form and sent to the processing agent.
The "GET" method should be used when the form is idempotent (i.e. causes no side-effects). Many database searches have no visible side-effects and make ideal applications for the "GET" method. Note. The "GET" method restricts form data set values to ASCII characters. Only the "POST" method is specified to cover the entire character set.

 Sources: http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1

Monday, 7 March 2011

On generative and discriminative models

In a supervised learning problem, we want to find the function f: X -> Y, or P(Y|X). Discriminative models directly estimate P(Y|X). It is given the name because given X, we can directly discriminate (or determine) the value of the target Y. Generative models, on the other hand, estimate the joint probability P(Y, X) by finding P(X|Y) and P(Y). It is called generative because given the parameters P(X|Y) and P(Y), we can generate observable samples from the model.

Discriminative models include: logistic regression, conditional random fields, support vector machine
Generative models include: HMM, naive Bayes, Gaussian mixture models.

For solving a classification problem, a discriminative classifier is almost always preferred because discriminative models do not need to model the distribution of the observed variables, and they can generally express more complex relationships between the observed and target variables. However, Ng et al. also demonstrated in their experiments that generative methods may also converge more quickly to its (higher) asymptotic error as number of training examples increases. The generative model may also approach its asymptotic error with a number of training examples that is only logarithmic, rather than linear, in the number of parameters. This means that with smaller number of training examples, generative models may do better.

Sunday, 6 March 2011

Libraries needed for RoR development

For a RoR project that has a Gemfile, you can install all the required gems using the bundle command. You need to install bundler first. You may also install some other apps/libraries by using the following commands:
sudo apt-get install build-essential ruby1.8-dev
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
sudo apt-get install sqlite3 libsqlite3-dev
sudo gem update --system 
sudo gem install bundler

Saturday, 5 March 2011

Clutter

We made Clutter, a Chrome extension that allows you to view multiple websites in the same tab. You can find it in Google's webstore:  http://bit.ly/eBIPZW

We attended the Chrome hackathon on February 19 at HubSpot's office in Cambridge. Jan Kleinert from Google gave us a tutorial on Chrome extension and then we began the hacking.

We decided what extension to make before the hackathon. Victor came up with the idea and it was inspired by the small hack he did during the IAP. During our Battlecode hacking, he made a split-pane display to show three websites (Pandora, google doc, and IRC chat) in the same tab as a dashboard and projected it. This is especially useful for the Chrome notebook because all it has is a browser window, and you cannot have multiple windows.

The one he made was a static layout, what we added, in addition to making it a Chrome extension, is allowing adding and removing panes at whatever position you want. We won the hackathon in the end and Jan wrote a blog post about this event as well.

Friday, 4 March 2011

Command to remove all .svn folders

find ./ -name ".svn" | xargs rm -Rf
Explanations:
xargs is used to build and execute command lines from standard input. It breaks list of arguments into sublists small enough to be acceptable.