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 282010

I have been working on some offline processing of data and creating graphs on the fly which automatically get updated on a website. What has been problematic is to do this without a display (for example run from a cron job). I found a solution which seems to work with the EPD package I am using on a linux box.

1
2
3
4
5
6
7
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg

fig = Figure(figsize=(4,4))
fig.gca().plot(range(1,10))
canvas=FigureCanvasAgg(fig)
canvas.print_figure('bob.png', dpi=150)

There are likely some other ways to do it, but this works for me.

Feb 282010

Go Canada!!

Feb 182010

I am always looking for ways of emulating many Linux things on the Mac. I must admit I use the Terminal window almost exclusively for doing my work but there are times that using the Finder is just faster.

Then, often when I find the directory I have been looking for I want to get a Terminal window that is in that same directory. Typically I have just opened a Terminal window and cd’ed to the location – which can be quite laborious. So, finally, I looked around for a solution. I found a nice little Finder button called cdto. This will put a button on the Finder window and open a Terminal in the same directory as where the Finder is currently pointing.

Nice…

Feb 152010

I enjoy watching the TED talks. One that was really good was Keith Barry. The first part is a little slow (I found), but it is quite entertaining.


Feb 132010

Another interesting Podcast, this time by Melvyn Bragg in his In Our Time show. It is on Unintended Concsequences in Mathematics and talks about cubic equations, statistics and non-Euclidean geometry.

Feb 082010

I am always look for different MRI file readers and writers for the myriad of formats that we use in MRI research. One of the relatively simple and common ones is the Analyze fileformat. Some of the large packages have writers (e.g., SPM) but I am typically wanting to do my own small processing and then write out the data. So, I wrote up my own writeanalyze.m function. It will do the basic formatting though the offsets etc don’t work. Try it out but I can’t guarantee anything.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function [] = writeanalyze(fname, data, ftype)

if( nargin == 2 )
    ftype = 'int16';
end

if( strcmp( ftype, 'int16' ) == 1 )
    file_type = 4; bpp = 16;
elseif( strcmp( ftype, 'uint16' ) == 1 )
    file_type = 4; bpp = 16;
elseif( strcmp( ftype, 'int32' ) == 1 )
    file_type = 8; bpp = 32;
elseif( strcmp( ftype, 'float' ) == 1 )
    file_type = 16; bpp = 32;
elseif( strcmp( ftype, 'double' ) == 1 )
    file_type = 64; bpp = 64;
else
    error(sprintf('Unknown data type %s', ftype));
end

fp = fopen([fname '.hdr'], 'wb', 'b');

%%
%%  Write the header_key part
%%
fwrite(fp, 348, 'int32');
fwrite(fp, repmat(' ', 1, 10), 'char');
fwrite(fp, repmat(' ', 1, 18), 'char');
fwrite(fp, 16384, 'int32');
fwrite(fp, 0, 'int16');
fwrite(fp, 'r ', 'char');


%%
%% Write the image_dimension part.
%%
fwrite(fp, length( size(data) ), 'int16');
for ii=1:length( size(data) )
    fwrite(fp, size(data,ii), 'int16');
end

for ii=length( size(data) )+1:7
    fwrite(fp, 1, 'int16');
end

fwrite(fp, 0, 'int16');  % unused 8
fwrite(fp, 0, 'int16');  % unused 9
fwrite(fp, 0, 'int16');  % unused 10
fwrite(fp, 0, 'int16');  % unused 11
fwrite(fp, 0, 'int16');  % unused 12
fwrite(fp, 0, 'int16');  % unused 13
fwrite(fp, 0, 'int16');  % unused 14

% data type
fwrite(fp, file_type, 'int16');   % 4 = signed short
fwrite(fp, bpp, 'int16');   % bpp
fwrite(fp, 0, 'int16');
for ii=1:8
    fwrite(fp, 1.0, 'float32');
end
fwrite(fp, 0, 'float32');
fwrite(fp, 0, 'float32'); % funused 1
fwrite(fp, 0, 'float32'); % funused 2
fwrite(fp, 0, 'float32'); % funused 3

fwrite(fp, max(data(:)), 'float32');
fwrite(fp, min(data(:)), 'float32');
fwrite(fp, 0, 'float32');
fwrite(fp, 0, 'float32');

fwrite(fp, round(max(data(:))), 'int32');   % glmax
fwrite(fp, round(min(data(:))), 'int32');   % glmin

%%
%%  Data history
%%
fwrite(fp, repmat(' ', 1, 80), 'char'); % descrip
fwrite(fp, repmat(' ', 1, 24), 'char'); % aux_file
fwrite(fp, '3', 'char'); % aux_file
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 3), 'char'); % originator

fwrite(fp, 0, 'int32'); % views
fwrite(fp, 0, 'int32'); % vols_added
fwrite(fp, 0, 'int32'); % start_fiedl
fwrite(fp, 0, 'int32'); % field_skip
fwrite(fp, 0, 'int32'); % omax
fwrite(fp, 0, 'int32'); % omin
fwrite(fp, 0, 'int32'); % small_max
fwrite(fp, 0, 'int32'); % small_min

fclose(fp);

%%
%%  Write the data
%%
fp = fopen([fname '.img'], 'wb', 'b');
fwrite(fp, data, ftype);
fclose(fp);
Feb 032010

From xkcd:

Purity

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.

© 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