Reading Raw Data in Python

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

[cc lang="python"]
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
[/cc]

Reading raw data in Matlab

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 [cci lang="matlab"]fp=fopen(‘filename.dat’…[/cci].  After typing the fopen, fread, reshape and fclose too many times, I finally made it into an all-in-one Matlab function called [cci lang="matlab"]readraw()[/cci] which will do all the reading and reformatting in one function call.

[cc lang="matlab"]
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);
[/cc]

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

Huge Java Resource

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.

Show SQL table output using PHP

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.

[cc lang="php"]
function display($table)
{
$db_host = ‘localhost’;
$db_user = ‘‘;
$db_pwd = ‘ ‘;
 
$database = ‘‘;
 
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 “

;Table: {$table}

“;
echo “

“;
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "

“;
}
echo “

\n”;
// printing table rows
while($row = mysql_fetch_row($result))
{
echo “

“;
 
// $row is array… foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo “

“;
 
echo “

\n”;
}
echo “

{$field->name}
$cell

“;
mysql_free_result($result);
}
[/cc]

Backups and Revision Control

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.

Python, Scipy and Numpy

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.

Passwordless Login Using SSH

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