Skip to content

Instantly share code, notes, and snippets.

@ChrisBeaumont
Created May 29, 2014 15:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBeaumont/e97d4efdc9c472836214 to your computer and use it in GitHub Desktop.
Save ChrisBeaumont/e97d4efdc9c472836214 to your computer and use it in GitHub Desktop.
MRI Glue demo
# -*- coding: utf-8 -*-
"""
This script loads a Brain Tumor DICOM dataset into Glue.
The data comes from http://www.osirix-viewer.com/datasets/DATA/BRAINIX.zip
To run this locally, download and unzip that file, and run this script
from the directory that you dowloaded the ZIP file to
This requires the dicom library, which you can install via
pip install pydicom
"""
from glob import glob
from glue import qglue
from dicom import read_file
import numpy as np
def load(pth):
ds = read_file(pth)
shp = ds.Rows, ds.Columns
return np.fromstring(ds.PixelData, dtype=np.short).reshape(shp)
datasets = {}
base = 'BRAINIX/BRAINIX/IRM cérébrale, neuro-crâne/'
subdirs = ['SOUS - 702',
'T1-SE-extrp - 601',
'T2W-FE-EPI - 501',
'sT2W-FLAIR - 401',
'T1-3D-FFE-C - 801',
'T1-SE-extrp - 701',
'sT2-TSE-T - 301'
]
for dir in subdirs:
slices = glob(base + dir + '/*dcm')
data = [load(s) for s in slices] # read each slice
data = np.dstack(data).transpose((2, 0, 1)) # assemble into cube
datasets[dir] = {'value': data}
qglue(**datasets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment