Root Mean Square Error tutorial – MATLAB

Here’s how to calculate the root mean square error.

Assume you have one set of numbers that represent the Actual values you want to predict.

Actual = [1 2 3 4];

Then assume you have another set of numbers that Predicted the actual values.

Predicted = [1 3 1 4];

How do you evaluate how close Predicted values are to the Actual values?

Well you could use the root mean square error (RMSE) to give a sense of the Predicted values error.

Here’s some MATLAB code that does exactly that.
Continue reading “Root Mean Square Error tutorial – MATLAB”

LaTeX – how to add a new reference to the BibTeX

How to add a BibTeX entry to LaTeX

This assumes,

  1. you already have a BibTeX file linked to your .tex file
  2. you are using Texmaker (if you’re not, you probably should be)

It seems to me you must go through a series of arcane steps to be able to add a reference to your BibTeX bibliography and get it to work in your \LaTeX file.

Here’s the steps and their order that have worked for me. I have included my a few comments that hopefully help to give you a sense what is going on.

Continue reading “LaTeX – how to add a new reference to the BibTeX”

MATLAB – how to calculate the Jaccard similarity coefficient/index and distance between images

Here’s how to calculate the Jaccard similarity coefficient and Jaccard distance between two or more images.

But first, some quick definitions

The Jaccard index is the same thing as the Jaccard similarity coefficient. We call it a similarity coefficient since we want to measure how similar two things are.

The Jaccard distance is a measure of how dis-similar two things are. We can calculate the Jaccard distance as 1 – the Jaccard index.

For this to make sense, let’s first set up our scenario.

We have Alice, RobotBob and Carol. Alice draws a white line. RobotBob and Carol try to copy Alice. We want to measure how similar RobotBob’s and Carol’s lines are to Alice’s line.
Continue reading “MATLAB – how to calculate the Jaccard similarity coefficient/index and distance between images”

Cluster commands

Often I find myself running jobs on the cluster and I can never remember the basic commands. So here’s some useful commands that you can use to run jobs on the cluster.

*** note that I’m running jobs on Simon Fraser University’s (SFU) cluster and I have no idea if these will work on your specific configuration ***

Assume your userid is billybob
Assume you have a cluster script called test.pbs
Continue reading “Cluster commands”

Unity3D – example online game

A few months ago, I romanticized about developing video games and spent an evening trying out Unity3D.

I have to say I’m pretty impressed. With zero prior experience using it and barely going through any sort of tutorials, in a couple of hours I managed to put together a little world to run around in!

Jer's World
An actual screenshot of the 3D world

Continue reading “Unity3D – example online game”

Spinal Cord Segmentation using an A* search

Our paper was accepted in ISBI 2013! 🙂

The paper is called:

Globally Optimal Spinal Cord Segmentation Using a Minimal Path in High Dimensions

I think the title really summarizes the work pretty nicely so I’ll break down each part of the title to give you a brief overview of the paper.

“Spinal Cord Segmentation” = this is the goal of the work. Given a 3D MRI, we want a method that can mark those voxels that belong to the spinal cord. Segmentation means to label the voxels as spinal cord or background.

“High Dimensions” = we represented the segmented spinal cord by a list of 6 numbers. I’ll explain this a bit more since this a bit tricky to understand. We used principle component analysis (PCA) to represent the “shape” of a 2D slice of the spinal cord. PCA allows us to represent each shape by its center point (x,y,z) and three principle components (giving us a total of 6 numbers per 2D slice). We can represent our spinal cord as a stack of these 2D shapes, or equivalently, a list of 6 numbers.

Now the question is, how to find this list of 6 numbers? Well we can search for them. This is similar to finding the shortest path in between 2 points, but instead of searching over 2 or 3 (spatial) dimensions, we search over the 6 dimensions (3 spatial and 3 shape).

“Minimal Path” = a minimal path finds the shortest path in between two points. In our methods, a user enters the start and end point of the spinal cord, and we find the minimal path in 6D between these two points. We use a slightly modified version of the A* search to find this minimal path. A path is defined to be “short” if the shapes “fits” well (e.g. the borders match) with what is in the MRI.

“Globally Optimal” = means that we can find the best segmentation that exists based on how we defined how well the shape “fits”. Our method will always find the global minimum and will not get stuck in any local minima.

You can take a look at the paper here.

Here’s the poster we did for the ISBI conference.

ISBI 2013 poster
Spinal Cord Segmentation Poster

The nice PDF can be viewed here.

MATLAB – Calculate L2 Euclidean distance

Here’s how to calculate the L2 Euclidean distance between points in MATLAB.

The whole kicker is you can simply use the built-in MATLAB function, pdist2(p1, p2, ‘euclidean’) and be done with it. p1 is a matrix of points and p2 is another matrix of points (or they can be a single point).

However, initially I wasn’t really clear about what was going on. So if you are still a bit confused, let’s chat about it…

The scenario: You have one point, aPoint, that you wish to compare against a bunch of other points bunchOfPoints. For simplicity, let’s work in two-dimensional (2D) space. Note that 2D means each point is composed of two pieces of information (i.e. has 2 components).

We start by defining our points, then we calculate the L2 distance by hand, then we use the built-in pdist2() function to show we get the same result. And then finally, as a little bonus, we show how to get the minimum L2 Euclidean distance at the end.

Here’s how to calculate the equation by hand if you’re interested.

% disL2
% Define our points.
aPoint = [1,4]; % A single point with 2 components.
bunchOfPoints = [2,3; 1,4; 0,1]; % A bunch of other points.
% Make 'aPoint' the same size as a 'bunchOfPoints'.
aPointMatrix = repmat(aPoint,size(bunchOfPoints,1),1);
% Calculate by hand.
%% L2 Euclidean Norm.
% 1) Take the difference between the two -> aPointMatrix-bunchOfPoints
% 2) Square the difference to get rid of positive/negative: 
%       (aPointMatrix-bunchOfPoints).^2
% 3) Sum this up along the rows.
%       (sum(((aPointMatrix-bunchOfPoints).^2), 2))
% 4) Take the square root of this.
%       (sum(((aPointMatrix-bunchOfPoints).^2), 2)).^0.5
pointsDifSquare = (sum(((aPointMatrix-bunchOfPoints).^2), 2)).^0.5
% output = 1.4142 0 3.1623
%% Or we can just use this handy built-in function...
d = pdist2(aPoint,bunchOfPoints,'euclidean') 
% same output! = 1.4142 0 3.1623
%% Bonus, how to find the min distance!
[theMinDistance, indexOftheMinDistance] = min(d)
% Yeah! As expected theMinDistance = 0 and indexOftheMinDistance = 2.

LaTeX – Detexify ~ a shout out

Here’s how to easily find symbols in LaTeX.

Okay the most handiness (is that really a word?) little web app that I’ve come across since starting work in $\LaTeX$ is this little tool found here.

$\text{detextify}^2$ has seriously saved me hours of my life.

This beautiful app lets you draw any symbol into a box, then it will return the corresponding $\LaTeX$ command. So freakin’ handy! And it works. That obscure symbol you saw in some paper $\zeta$, and you can’t think of the Greek name of the top of your head? Simple! Just draw it in and bam, $\text{detextify}^2$ throws the command back (\zeta in case you are wondering).

I use this all the time when I’m writing anything sort of equations in LaTeX. A definite must know about.

Hope this saves you countless sleepless nights.

MATLAB – how to make a movie of plots

Making a video of your moving graphs/charts is surprisingly easy to do in MATLAB. However most of my online searches gave me old outdated methods to do it. Here’s how to make a movie or a video in MATLAB.

I kept getting this freakin’ error using the old methods (i.e. the avifile() function):
“Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.”

So I found MATLAB recommends you use the VideoWriter() class…

They have a nice little example in the documentation, but for the impatient, here’s my quick and dirty implementation of it (with some modifications/additions of course).
Continue reading “MATLAB – how to make a movie of plots”