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.
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…
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.
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.
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); |
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 |
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 |
There isn’t much magic here, just a simple idea that I use almost daily.
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.




Recent Comments