Category Archives: Programming

Splitting 4D Nifti file into a series of 3D files in Matlab

NIfTI-1 is a data format proposed by the NifTI (Neuroimaging Informatics Technology Initiative) DFWG (Data Format Working Group) that can be used for storing neuroimaging datasets (e.g. MRI, functional MRI, etc.). This format is adapted from the widely used ANALYZE 7.5 format from Mayo Clinic. A Nifti file consists of a header part containing the meta-data and the raw image data itself. The header and the image data can be stored together in the same file with an *.nii extension or can be split into two files with an *.hdr extension for the header and an *.img extension for the image data.

Matlab scripting quick tip: Dual monitor setup

I have been using Matlab for almost all of my computing needs. It is very easy to program especially when dealing with matrices. It has also a rich library of graphical interface. You can easily build a highly interactive graphical user interface using its GUI development environment or GUIDE. If you are familiar with event-based programming, you will have no difficulty using GUIDE.

Here is a quick tip you might find useful when programming in Matlab.

Regular expression

Especial Characters:

[
\
^
$
|
?
*
+
.
(
)

If you want to use any of the above characters as a literal in a regular expression, you need to escape them with a backslash. For example, 1\+1=2 will search 1+1=2.

With a character class or character set, you can tell the regex engine to match only one out of several characters. This can be done by simply placing the characters you want to match  between square brackets. For example, if you want to match ‘a’ or ‘e’, simply use [ae]. Thus, gr[ae]y will match either gray or grey,  but not graay, or graey, or similar strings.

To specify a range of characters or numbers, you can use a hyphen inside a character class. For example, [a-z] will match a single character from a to z, and [0-9] will match a single digit from 0 to 9.

Typing  a ^ after the opening bracket will negate the character class. For example, q[^u] means a ‘q’ followed by characters not a ‘u’.

Note that the only metacharacters inside a character class are the closing bracket ], the backslash \, the caret ^, and the hyphen –. To include these characters inside a character class, you need to escape them with a backslash.

Short hand character classes include \d for [0-9], \w for “word character” or [0-9a-zA-Z_], and \s for “whitespace character”. Negated short hand character classes include \D for ^\d, \W for ^\w, and \S for ^\s.

The dot (.) matches a single character without caring what that character is, except new line characters.

Anchors match position before, between, or after characters. The caret ^ matches the position before the first character in the string. For example, applying ^a to “abc” matches ‘a’. But ^b will not match “abc” at all because ‘b’ cannot be matched at the start of the string. Similarly $ matches right after the last character in the string. c$ matches ‘c’ in “abc”, while a$ does not match at all.

\b for word boundaries. For example, \bis\b will match “is” in “This island is beautiful.”

Alternation or | is used to match one regular expression out of several regular expressions. For example, apple|banana|oranges. Note, you can use round brackets for regex grouping (e.g., \b(cat|dog)\b.

The question mark ? makes the preceding token in a regular expression optional. For example, colou?r  matches both colour and color. You can make several tokens optional by grouping them together and attaching the question mark at the closing round bracket, such as Nov(ember)? which will match “Nov” and “November”.

The asterisk or star or * tells the engine to match the preceding token zero or more times. The plus + tells the engine to match the preceding token once or more times. You can limit the repetition by using {min,max} where min is a positive integer number indicating the minimum number of matches and max is an integer equal to or greater than min indicating the maximum number of matches. If comma is present but max is omitted, the maximum number of matches is infinite.

Debugging with gdb

GDB is the GNU project debugger which can be very helpful when you’re trying to figure out what goes wrong in the application you’re developing. With gdb, you can start a program, specify points or conditions where the program’s execution will be stopped so that you can examine its current condition, and experiment correcting values to discover bugs that maybe lurking in your code. gdb’s online documentation can be found here.

To use gdb, you need to compile your program with the -g option to include debugging information. For instance, using gcc, you can compile program myprog.c as follows:

[user] gcc -g myprog.c

To start gdb, run gdb program in the command prompt, where program is the name of your executable file. For instance,

[user] gdb a.out

First, this will display gdb’s front material describing its copyright and non-warranty, among other information. Then you will be put in gdb prompt (gdb).

Start the program by issuing the command run in the (gdb) prompt. If you have not yet issued any break points, the program will continue running until it finishes or it encounters fatal error. To quit gdb, just issue the quit command.

The main purpose of gdb is to allow programmers to stop the program before it terminates so that its current condition can be examined and potential sources of trouble can be known. This is where breakpoints, catchpoints, and watchpoints become handy. A breakpoint makes your program stops whenever a certain point is reached. On the other hand, a watchpoint is a special breakpoint that stops the program when the value of an expression changes. A catchpoint is another kind of breakpoint that stops the program when a certain kind of event occurs.

To setup a breakpoint, use the break command followed by the location. The location can be a function name, a line number, or an address of an instruction. For example,

(gdb) break 10

will set a breakpoint at line number 10, and

(gdb) break myfunc

will set a breakpoint at the start of function myfunc.

To examine your source file within gdb, you can use the list command. list prints 10 lines by default from the source file. It also prints the line numbers which you can use in the break command. Some of the useful list options are as follows:

(gdb) list linenum

will print lines centered around linenum.

(gdb) list startline, numlines

will print numlines starting from startline in the current source file.

(gdb) list function

will print lines centered around the beginning of function

(gdb) list

will print the next 10 lines.

After a break, you can continue the program execution using the continue or c command. You can also run the program until the next line is reached with the step or s command. To step by machine execution instead of source line, use the stepi or si command. The next or n command will execute the next line including any function call. To execute just the next machine instruction, use nexti or ni.

The above commands should keep you started. If you need help, just type help in the (gdb) prompt. You can further refine help by appending any command after help, such as

(gdb) help break

That’s it! And happy debugging.

Search Philippines! A Google custom search engine

Interested in creating your own search engine? Try google custom search engine (CSE)! With google’s CSE, you can customized your search experience, including in the search only sites that are relevant to you. You can also host both the search box and search results in your site. You can even create custom search engine on the fly. (For example, creating a search engine for sites included in your blogroll.) For those interested, you can visit this webpage on CSE’s API.

It’s really easy to configure but you need to have a google account. If you already have one, follow the link above, login, and start building your own search engine. I already made one and called it Search Philippines! SP! can be found in this URL: http://search.philippinestoday.net/ This search engine only includes Philippine-related websites (well, that’s the target). For now, I have only a couple of websites included. I plan to include more Philippine news sites, pinoy blogs, etc.  Here is the image of the site.

searchphilippines

What is this for users? Since the number of sites to search are limited and focused (say, only for Philippine news sites), users will be able to get more relevant search results as compared to using the entire google search engine. For website owners, this will give more visibility to your website. For instance, search the term ‘phpwebsite’ in google. This blog (myguide.bagarinao.com) appears in the 5th page. Search it again in my custom search engine Search Philippines! This blog is now on the first page.  This is because I included my blog in the list of websites for Search Philippines.

Any way, if you are interested in helping build Search Philippines! or want to include an SP! search box in your website or blog, drop me a note! It will be great to have our very own custom search engine.

Windows live writer’s "Invalid response document" error

In my previous post, I outlined my unsuccessful attempt to use windows live writer (WLW) to connect to the XmlRpc server of PhpWebsite‘s article manager module. I experimented using both the Metaweblog API and the Movable Type API but with no success. When attempting to post an entry, WLW always returned the following error message “The response to the metaWeblog.newPost method received from the weblog server was invalid: Invalid response document returned from XmlRpc server.” However, the entry did get posted. Republishing the same entry would create a new post instead of replacing the one posted earlier. And each time, WLW gave the same error. 

I searched the Internet for possible solutions. I found out that the problem is quite common and not unique to PhpWebsite (PWS).  The same problem has also been observed in other blogging platforms such as wordpress. Luckily for wordpress users, there are already solutions posted in several blogs for some of the problems with WLW. Unfortunately for me, the solutions were not applicable for PWS. I could not even find other sites or blogs talking about PWS and WLW except this one. It seems I am the only soul trying to do this in the entire world wide web.

Since most of the websites I’m maintaining are powered by PWS, I thought it is worth the time and effort to figure out the cause of the problem. I am also in need of a good editing application that I can use with PWS. The provided text editor is only good for basic editing and not for more advanced formatting. For instance, using tables in entries can really take a lot of time. Anyway, since I am using WLW in my other blogs, I thought it would be great if I could also use it in my PWS-based websites.

News Ticker for PhpWebSite

In this post, we will combine the two scripts described in the last two entries, namely, A Javascript News Ticker and Extending phpWebSite for mobile access, into a single script that will display in a news ticker the most recent articles of your (phpWebSite) PWS-based website. The functions defined in these two articles will be reused here with minimal modifications. So if you have not yet read these articles, this is the right time to do so. Simply click the link provided above and return here afterwards.

To incorporate the new script into phpWebSite, we will use theme.php and theme.tpl included in every phpWebSite theme. theme.tpl defines the overall layout or template of your site, while theme.php facilitates the inclusion of php files into phpWebSite. We will be manipulating these two files in order to include the php script for our news ticker. But before this, we will first describe the script itself.

The basic idea is to use the javascript described in the news ticker article for the news ticker part. But instead of hard-coding the title and link arrays, as we did before, we will use the script in Extending phpWebSite for mobile access article to extract the titles and links of the most recent articles in your PWS-based website. We then output everything into a <script> html tag and insert this into phpWebSite using theme.tpl.

A Javascript News Ticker

News tickers are common in news websites. A news ticker is usually used to draw user’s attention to the site’s latest news headlines. It can also provide a direct link to the displayed news item so that readers can immediately access the full content from anywhere within the site. One such ticker can be seen at the Philippines Today website. This post describes how to create a simple news ticker you can use on your own website using javascript.

To start with, we define two arrays. One contains the titles of the news items to be displayed and the other the links to access these items. Here, we will explicitly specify the titles and the links within the code for simplicity. Other methods, such as via RSS, can be used to implement automatic retrieval of titles and links that will be displayed in the ticker. But this will not be covered here. Our first javascript code will be like this:

Extending phpWebSite for mobile access

I have been using phpWebsite in a couple of websites I am administering. It provides a complete content management system and has several modules you can use in order to customize the contents of your site. You can exclude features you don’t need and install only the ones you will use. Most of all, phpWebsite is free and developed using PHP, an HTML-embedded scripting language that I am familiar with.

Recently, I have been thinking of providing a text-only version of my websites so that the contents can also be viewed using web-enabled mobile phones or text-only browsers. What I want for the text-only version is to have an index page that will display the title of the most recent entries with links to the full article and an entry page that will display the full content of a given article.

I started searching the Internet for a third party phpWebSite module that would do the trick, but to no avail. Because of this, I decided to make one myself. Since I am not still familiar with the inner workings of phpWebSite and how its modules are organized, I decided to make a script that is independent from it, not a phpWebSite module. However, the script will still be pulling out data from the phpWebSite database and possibly recycle some of the pre-defined functions already available in the package. Hopefully, I can rewrite the script as a full-pledged module in the future. The script’s organization and code is summarized next.