February Updates

I’ve recently added a number of version updates after a delay of a month.  I was working on changing the data structures I use within the CGI programs, so that I could easily pass around more information about each program.  I want to include with the internal object I pass around, such peripheral information as the name of the screen capture and thumbnail image files, and their sizes.  That’s done now, so I have caught up with some new versions, some of which are listed here.

 Vinci is a remarkable program from Stefan Vollmar and group at the Max Planck Institute for Neurological Research, in Cologne.  It is an extremely advanced analysis tool for neurological images, particularly PET.  I use this program daily and still am discovering functions and features I didn’t know were in the program.  That said, it would also be possible to use it simply for image viewing; it handles a wide range of input formats, and also conversions.  In the area of functional analysis, it has few peers.

Loni Debabler is another huge program from the extremely prolific LONI group at UCLA.  It takes an ambitious and interesting approach to image file conversion, providing a specialized programming environment for reading, manipulating and writing image files.  This is all controlled through a graphical programming environment, and processing schemas are stored as XML files.  Sample conversion programs are provided for the most usual image formats, or you can develop your own.  This is a program I’ve not yet used as much as I want to, its internals are complex but it’s tackling a difficult job.  I have to admit I’ve only used and modified the pre-programmed sample files.

PixelMed Java DICOM Toolkit is another advanced project, providing an extremely comprehensive implementation of DICOM tools.  It comes from David Clunie, a name familiar to everyone who has done any work at all in the field of DICOM programming.  This toolkit will provide more functionality than most people can utilize and comes from the foremost authority.  It is updated extremely frequently, such that version numbers are not used.  It’s difficult to convey on my site how frequently it is released, as almost all other projects release versions on a monthly to annual basis.  This software is revised almost weekly.  Perhaps I should make it a ‘sticky’ at the top of the ‘New Releases’ list.

Another very comprehensive library, in C++ this time, is Imebra from Paolo Brandoli, of PuntoExe software.  Here is another true open source program, where the free version is identical to the commercial, and source code is provided in both cases.  This project has recently gained its own website (imebra.com), and related projects based on the library are at the Puntoexe website.

DP Tools, from Denis Ducreux, is another active program.  It specializes in the field of functional MRI and MR Diffusion.  I know nothing about this field but have seen an increasing amount of excellent software emerging.  It’s written in Delphi, in common with some of Chris Rorden’s very widely used imaging software.  I am also profoundly ignorant of Delphi, I seem to be exposing my weaknesses here.

ITK-SNAP is another specialized neuro program, this time for segmenting brain images.  It incorporates elements of the NLM Insight Segmentation and Registration Toolkit, hence the name.  Formerly developed at UNC-Chapel Hill and now at Penn, this project provides automatic and manual brain segmentation methods.  It’s cross-platform.

TomoVision is a little program that displays DICOM images and doesn’t do much else.  And, the free version is limited to 5 images.

MedImaView has a new version out.  It’s another small DICOM viewer, when I tried it just now I could only open multiple files via drag and drop, and when I dropped a 192-frame MRI sequence I got: 192 windows.  Hmmm.

Sed Cleverness

I got sed to do something clever today, though sadly the cleverness was not mine.  I tried to solve the problem myself and although in the process I learned a great deal about sed, I had to resort to copying the answer.

I want to add Google Analytics code to my sister’s website, which she’s writing using iWeb.  Analytics is enabled by including in your HTML a javascript snippet that Google gives you.  iWeb does a really nice job, but you have to do things their way, and that means no javascript.  Fair enough, I guess, Apple want to be ensure that websites produced using their software will always work, and introducing a programming language pretty much ensures that things frequently won’t work.


The website is hosted on a Linux server, not an Apple account, so to publish the site we export its contents to a directory, then FTP the directory contents to the server.  Initially we used Filezilla but it does not have incremental directory synchronization, so we switched to the awesome Cyberduck.
So my first thought was, well perhaps we can modify the HTML files before they leave her Mac.  There are a couple of approaches available, one is an iWeb add-on which looked more complex than needed, and you have to buy it.  Another was an Automator action you can download that will insert the Google javascript into the HTML files.  That sounded good but was one more action to perform, and I’ve never used Automator.
So I thought, I’ll knock up a little script that the web server can run as an hourly cron job, and edit any HTML files that don’t contain the Google code.  Ha!  Little script though it is, it took a while.  I got a lot of help from Bruce Barnett’s sed guide, as I don’t have a good shell book with me right now.  I should buy O’Reilly’s ‘Sed & Awk’ book, a classic if ever there was one, and I believe may even have been their first ever book published. I remember it in print in the early 90’s, and they even had a T shirt of the cover, which I dearly wish I’d bought.

The tricky part was, the Google code is supposed to be included immediately before the </body> tag in each HTML page.  Two problems: I couldn’t be sure that the </body> tag would be on a line by itself, and sed file inclusion acts after the matched pattern, not before.
Problem 1 was addressed using a simple substitution with newlines:

sed -e ‘





I used pipe-character delimiters.  The substitution is of the string </body>, and the newlines are inserted literally.  So the line-continuation backslashes continue the substitution pattern.  The ampersand is the matched string, so this substitution puts a newline before and after the </body> tag, to ensure it’s on its own line.
Problem 2 was harder.  I didn’t know about the file-insertion operator till today, though I figured that sed would have one.  It does, but it inserts after the matched pattern.  My initial approach to insert the file Google Analytics Javascript code, ga.js was:

sed -e ‘                                                                                       
/</body>/ {                                                                                       

r ga.js’


But this inserted the file after the </body> tag, which wasn’t allowed.  
Next thought was to take a two-way approach.  I’d print every line not matching the </body> pattern, and in a separate rule matching the </body> pattern, delete the pattern, insert the file, then print the pattern.

sed -e ‘

/</body>/ !{
/</body>/ {
r ga.js


Not to be.  The pattern matching quits at the delete as explained by Barnett, so the print command is never executed.
At this point after some hours of learning sed, I looked for an answer to inserting a file before a sed pattern, and found one.  At least by this point I knew enough to understand it (sort of).  This post by Tapani Tarvainen gave me a very succinct answer for the second pattern action:

/</body>/ {

r ga.js

‘ -e N -e ‘}’

OK I wouldn’t have thought of that one.  As he explains,

The ‘r’ command actually outputs the file just before reading a new line to the pattern buffer (or at EOF). That can be forced in mid-script by ‘n’ or ‘N’, and while ‘n’ will also print the
buffer before ‘r’ does its thing.

He goes on to cover more general cases.  There is also another approach described in the same forum which employs use of the hold command, h, and the swap operator, x.
Having tested the code that does the file insertion, I put it into a shell script that finds all *.html files, checks to see if they have the GA code in them already (you’re only allowed to put it in once), and if not, performs the insertion.
Next problem…the ISP’s server doesn’t offer cron!  I need this to run the sed script on the HTML files without having to ssh in to run it.  Aargh.  Neither can I execute an arbitrary command on the server using my FTP client.  I might try making a passwordless ssh script (using id_rsa.pub keys) and then see if I can get Cyberduck (FTP client) to run that after it performs the transfer.
That’s enough for one day, though.  I learned a lot about sed, and a lot about how much I don’t know.