Python plots with no display

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.

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

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

Open Terminal from Finder

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…

writeanalyze in Matlab

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.

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

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.