Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Myspace button
Linkedin button
Webonews button
Delicious button
Digg button
Flickr button
Stumbleupon button
Newsvine button
Youtube button

Notes:

  • The site will be morphing over the next little while.
  • I am having some issues with tabs/spaces in the Python code. Sometimes they are correct, sometimes they get eaten. I am trying to figure it out.

Please feel free to leave a comment on a post if it interests you or if you have questions

Feb 012010

In a similar vein to reading raw data into Matlab, I created a similar type of function in Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def readraw(filename, shape, intype='int16', byteSwap=False):
        """ readraw - To read in a raw file and reformat it to the right shape """

        #  Read in the file
        if filename.endswith('gz'):
                fp = gzip.open(filename, 'rb')
        else:
                fp = open(filename, 'rb')

        d = fromfile(file=fp, dtype=intype).reshape(shape)

        d.byteswap(byteSwap)

        return d
Feb 012010

One of the most common things I do in Matlab almost always involves reading in binary data.  For a few years I went through the typical fp=fopen('filename.dat'....  After typing the fopen, fread, reshape and fclose too many times, I finally made it into an all-in-one Matlab function called readraw() which will do all the reading and reformatting in one function call.

1
2
3
4
5
6
7
8
9
10
11
function [d] = readraw(filename, type, ds, endian)

if( nargin == 3 )
    endian = 'b';
end

fp=fopen(filename, 'rb', endian);
d = fread(fp, prod(ds), type);
fclose(fp);

d = reshape(d, ds);

There isn’t much magic here, just a simple idea that I use almost daily.

Jan 272010

I have been working on some numerical problems in the last few weeks.  Mostly related to curve fitting and interpolation.  I am slowly sliding back to Java bit-by-bit though I am not sure if I will give up Python as the syntax is so tight and it is actually very fast.

Anyway, I was looking around for some curve fitting and interpolation code on the Net and found Michael Flannigan’s website which has a great resource of math, stats, optimization and some other  more subtle items.  His Java code is here.

I can’t even do it justice by showing the list of packages as it is about twice as long as the screen capture shown to the right.

Jan 082010

I found this code on the internet. This is a nice bit of code to show an arbitrary table with headers and all the entries. There is probably lots more one could do with it, but it works well for general testing and is completely independent of what is in the table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function display($table)
{
    $db_host = 'localhost';
    $db_user = '<userid here>';
    $db_pwd = '<password here>';
 
    $database = '<sql database here>';
 
    if (!mysql_connect($db_host, $db_user, $db_pwd))
        die("Can't connect to database");
 
    if (!mysql_select_db($database))  ||  die("Can't select database");
 
    // sending query
    $result = mysql_query("SELECT * FROM {$table}");
    if (!$result) {
        die("Query to show fields from table failed");
    }
 
    $fields_num = mysql_num_fields($result);
 
    echo "<h1>;Table: {$table}</h1>";
    echo "<table border='1'><tr>";
    // printing table headers
    for($i=0; $i<$fields_num; $i++)
    {
        $field = mysql_fetch_field($result);
        echo "<td>{$field->name}</td>";
    }
    echo "</tr>\n";
    // printing table rows
    while($row = mysql_fetch_row($result))
    {
        echo "<tr>";
 
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        foreach($row as $cell)
        echo "<td>$cell</td>";
 
        echo "</tr>\n";
    }
        echo "</table>";
    mysql_free_result($result);
}
Jan 062010

I seem to be always looking for a reasonable way to use revision control on my home directory (lots of binary files and text files). There is a great comparison of different revision control systems, including Git. I like Git, a little complex, but it is nice. There is a great writeup about using Git for revision control which includes automating the add/commit cycle reasonably often.

Jun 132009

For approximately 16 years I have been using Matlab faithfully.  Back when I started (1993) all it had were 2D matrices and I remember having to use a package someone wrote up to have 3D volume capabilities (which are typically required for processing MRI datasets).  Matlab is great, don’t get me wrong.  There are few things that I don’t like about it.

So, sitting at a conference in Hawaii (tough life, I know) I had some ideas that I wanted to try out and quickly figured out that I could not get to the site license hosts back at my university.  Argh.  So, I started looking around to see if there were other options which would provide me a similar feel and processing capabilities.  There were a few options, but the one that stuck out was to use Python, Scipy and Numpy.  They were almost tailor made for what I wanted.

So, over the last little while I have been using the Python, Scipy, Numpy triad for most of my research and am feeling like I want to shift over completely the triad rather than using Matlab.

May 152009

Computer c1 – connect from this computer

Computer c2 – connect to this computer

1. Connect to c1
2. type: ssh-keygen -t rsa -b 2048
* default directory for keyfiles will be ~/.ssh/
* if you do not want to be prompted, leave passphrase blank
3. copy the contents of .ssh/id_rsa.pub (there should only be one line)
4. place this line on c2, in ~/.ssh/authorized_keys

May 112009

This is from MacOSX Hints (here)

python ‘/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py’ -o …

May 112009

To sign a file:

$ gpg --detach-sign -u <email address> --armor <file>

To verify a file:

$ gpg --verify <file>.asc
© 2010 Math, Computing and Research Please leave a comment or contact me craig@mri.brechmos.org if you have any questions. Suffusion WordPress theme by Sayontan Sinha