Wednesday, 12 November 2014

Viewing medical images in an IPython notebook

I was looking for a simple way to browse medical images within an IPython notebook, and came across the interact function which makes it really easy to work with sliders. The function irtk.imshow returns three orthogonal slices for a given (z,y,x) concatenated in a single image. PIL is used to avoid writing temporary images to disk.


In [1]:
import irtk

patient_id = '2159'
img = irtk.imread( patient_id + "_img.nii.gz" )
seg = irtk.imread( patient_id + "_seg.nii.gz" )
In [2]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
from IPython.display import display

def f(z,y,x):
    display( irtk.imshow(img,seg,index=(z,y,x)) )
 
interact( f,
         z=widgets.IntSliderWidget(min=0,max=img.shape[0]-1,step=1,value=img.shape[0]/2),
         y=widgets.IntSliderWidget(min=0,max=img.shape[1]-1,step=1,value=img.shape[1]/2),
         x=widgets.IntSliderWidget(min=0,max=img.shape[2]-1,step=1,value=img.shape[2]/2) )


The sliders disappear when the notebook is saved as HTML, here are some hints for preserving the interactivity through JavaScript (though this is probably not the best approach for an image viewer).

Ideally, a medical image viewer integrated within the IPython notebook would use XTK, but there is still works to be done before it provides a working solution, see github/fperez and github/richstoner for proofs of concept.


4 comments:

  1. The example notebooks of SimpleITK use the same interact function to browse the Z dimension of 3d volumes and render the image using matplotlib:
    https://github.com/SimpleITK/SimpleITK-Notebooks/blob/master/myshow.py

    ReplyDelete
  2. Thanks for this post. I get "TypeError: imshow() got an unexpected keyword argument 'index'" and when referring to the irk documentation it seems that there no argument as index. Could you please clarify?

    ReplyDelete
  3. Hi Arman,
    Sorry for the delay in replying to your post.
    As explained in the following email:
    http://mailman.ic.ac.uk/pipermail/doc-biomedic-irtk/2015-February/000008.html
    you can now install python-irtk on Linux through conda.
    Alternatively, you can browse the latest version of the code on github. The answer to your question is thus:
    https://github.com/BioMedIA/python-irtk/blob/master/irtk/image.py#L1122
    Please note that at present, you cannot compile the code of the python-irtk module using the IRTK code which is on github. Hopefully, this will be fixed in the future.
    Kind regards,
    Kevin

    ReplyDelete
  4. Your positivity shines through thank you. This profile has more great insights Emotional Intelligence Test, including a section on the Emotional Intelligence Test for self-awareness.

    ReplyDelete