Monday, 16 December 2013

Thumbnails, default viewer and property tab in nautilus for medical images

This first post is on customising Ubuntu to your needs to make your work easier (and more enjoyable). It is accompanied by this github repository.

Here are quick changes to a brand new installation of Ubuntu:
  1. First of all, restart it with the Gnome classic (no effects) mode to get rid of blinking effects, and set it as default mode.
  2. Set a proper prompt in .bashrc:
    PS1="\[\033[1;30m\][\$?]<\[\033[1;35m\]\u\[\033[1;30m\]>{\$(date +%d-%m\" \"%H:%M)}[\w]\[\033[0m\] "
    and in .tcshrc:
    set prompt="%{\033[1;30m%}[%?]<%{\033n@[1;35m%}m%{\033[1;30m%}>{%D-%W %T}{\033\[0m[%~]} "
    The prompt is here to give you useful information such as current folder, current time or exit code of the last command.
  3. Set the location bar to text mode in nautilus, I find it much more easier to type in text with auto-completion than scroll-down and click on folders, and it enables you to copy and paste folder paths:
    gsettings set org.gnome.nautilus.preferences always-use-location-entry true
  4. Install nautilus-open-terminal to open a terminal in the current directory from nautilus:
    sudo apt-get install nautilus-open-terminal

As you can guess from its title, the main focus of this post is to give medical images what they lack compared with more common image formats: MIME types, thumbnails, open a chosen viewer when you double-click on them, and a property tab in nautilus.


 After making changes to nautilus, you might need to restart it for these changes to take effect. You can do so with the following command:
nautilus -q && nautilus

MIME types for *.nii and *.nii.gz:
MIME types are a way of identifying file formats using a type and a subtype, such as image/png. Most MIME types on your machine are defined in:
/usr/share/mime/packages/freedesktop.org.xml

If you look at this file, you will see that the MIME type for the DICOM format is already defined, with the key lines:
    <glob pattern="*.dcm"/>
    <magic priority="50">
      <match value="DICM" type="string" offset="128"/>
    </magic>

What these lines say is that if a file name ends with ".dcm", then the file is a DICOM file. If the content of a file begins with "DICM", then it is a DICOM file but depending on the other rules with a priority 50 (the beginning of a file corresponds to the "magic number" identifier).

So the DICOM format is already recognised, we only need to add MIME types for NIFTI (*.nii) and compressed NIFTI (*.nii.gz).
For this purpose, create a file ~/.local/share/mime/packages/nii.xml with the following content:
  <?xml version="1.0" encoding="UTF-8"?>
  <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="image/nii">
    <comment>NIFTI</comment>
    <generic-icon name="image-x-generic"/>
    <glob pattern="*.nii"/>
  </mime-type>
  <mime-type type="image/gznii">
    <comment>gzip-compressed NIFTI</comment>
    <sub-class-of type="application/x-gzip"/>
    <generic-icon name="image-x-generic"/>
    <glob pattern="*.nii.gz"/>
  </mime-type>
  </mime-info>

This defines the types image/nii for files ending in ".nii" and image/gznii for files ending in ".nii.gz". Note that image/gznii is defined as a subtype of gzip compressed files.

Finally, run:
update-mime-database .local/share/mime


Thumbnails for *.dcm, *.nii and *.nii.gz:

The first step to setup thumbnails is to write a thumbnailer script. I placed one on github, written in Python with SimpleITK, as ITK brings support to both DICOM and NIFTI files. For a given file, the script takes the middle slice and stretches the contrast with some saturation. It currently works for 2D and 3D images. You can copy the file nii_thumbnailer.py from github and place it into a folder in your home directory such as ~/Scripts .

In previous versions of Ubuntu, adding a new thumbnailer could be done through gconf-editor (GUI), or in command-line:
gconftool --type=string --set "/desktop/gnome/thumbnailers/image@nii/command"
"/home/USERNAME/Scripts/Nifti_thumbnailer.py %s %i %o"
&&
gconftool --type=bool --set "/desktop/gnome/thumbnailers/image@nii/enable" "true"

Now it is a bit less obvious and I do not know how to do it without root rights.

Using your sudo rights, create a file nii.thumbnailer in /usr/share/thumbnailers/ with the following text:

[Thumbnailer Entry]
TryExec=/home/USERNAME/Scripts/nii_thumbnailer.py
Exec=/home/USERNAME/Scripts/nii_thumbnailer.py %s %i %o
MimeType=image/nii;image/gznii;application/dicom;

et voilĂ !

In this screenshot of nautilus, you can see the text location bar and thumbnails for NIFTI images.


Default viewer for medical images

When I double-click on a DICOM file, it is automatically opened with ImageJ. What I would like is that when I double-click on a NIFTI file, it gets opened in rview, the main viewer that accompanies the IRTK library.

Here is how to proceed:
sudo gedit /usr/share/applications/defaults.list
add the line:
image/nii=rview.desktop
then create a file referencing rview:
sudo gedit /usr/share/applications/rview.desktop

[Desktop Entry]
Encoding=UTF-8
GenericName=IRTK Viewer
Name=rview
Exec=/home/kevin/Imperial/PhD/irtk/build/bin/rview %F
TryExec=/home/kevin/Imperial/PhD/irtk/build/bin/rview
Type=Application
Terminal=false
Categories=Education;Graphics;Science;DataVisualization;MedicalSoftware;Viewer;
Icon=/home/kevin/Imperial/PhD/irtk/icons/IRTK.ico


Nautilus property tab for medical images

The remaining missing feature is now a property tab in nautilus. Indeed, wouldn't it be nice to right-click on a NIFTI file and automatically have the output of IRTK's info tool without going through the hassle of launching a terminal?

Following the examples given by Saravanan Thirumuruganathan on his blog, I wrote nautilus-irtk-property-page.py . It brings all the information from the NIFTI headers, and a bit more, directly in nautilus. It is based on the cython IRTK wrapper I develop for my PhD.

All you need to do in order to install this script is copy it into ~/.local/share/nautilus-python/extensions/ and restart nautilus. Make sure you have nautilus-python installed on your machine.

Property tab in nautilus for NIFTI images.



I hope you found this blog post useful and do not hesitate to leave comments!

No comments:

Post a Comment