Commit 58534ceb1e1f48178a6c81de4d6a3021ed9122c6

Authored by tfmoraes
1 parent f7a5655a

ENH: Ported analyze reader to new way to handle slices (using memmap).

* Nibabel already works with memmap, it was only necessary to reorient the
* slices because we works with number of slices as the first dimension from
* memmap matrix, nibabel uses the first dimension the image width.
invesalius/control.py
@@ -383,16 +383,31 @@ class Controller(): @@ -383,16 +383,31 @@ class Controller():
383 ps.Publisher().sendMessage('End busy cursor') 383 ps.Publisher().sendMessage('End busy cursor')
384 384
385 def CreateAnalyzeProject(self, imagedata): 385 def CreateAnalyzeProject(self, imagedata):
  386 + header = imagedata.get_header()
386 proj = prj.Project() 387 proj = prj.Project()
387 proj.name = _("Untitled") 388 proj.name = _("Untitled")
388 proj.SetAcquisitionModality("MRI") 389 proj.SetAcquisitionModality("MRI")
389 - proj.imagedata = imagedata  
390 #TODO: Verify if all Analyse are in AXIAL orientation 390 #TODO: Verify if all Analyse are in AXIAL orientation
391 - proj.original_orientation = const.AXIAL  
392 - proj.threshold_range = imagedata.GetScalarRange() 391 +
  392 + if not header['orient']:
  393 + proj.original_orientation = const.AXIAL
  394 + elif header['orient'] == 1:
  395 + proj.original_orientation = const.CORONAL
  396 + elif header['orient'] == 2:
  397 + proj.original_orientation = const.SAGITAL
  398 +
  399 + proj.threshold_range = (header['glmin'],
  400 + header['glmax'])
393 proj.window = proj.threshold_range[1] - proj.threshold_range[0] 401 proj.window = proj.threshold_range[1] - proj.threshold_range[0]
394 proj.level = (0.5 * (proj.threshold_range[1] + proj.threshold_range[0])) 402 proj.level = (0.5 * (proj.threshold_range[1] + proj.threshold_range[0]))
395 403
  404 + self.Slice = sl.Slice()
  405 + self.Slice.matrix = imagedata.get_data().swapaxes(0, 2)
  406 +
  407 + self.Slice.window_level = proj.level
  408 + self.Slice.window_width = proj.window
  409 + self.Slice.spacing = header.get_zooms()[:3]
  410 +
396 411
397 def CreateDicomProject(self, imagedata, dicom): 412 def CreateDicomProject(self, imagedata, dicom):
398 name_to_const = {"AXIAL":const.AXIAL, 413 name_to_const = {"AXIAL":const.AXIAL,
invesalius/data/imagedata_utils.py
@@ -492,9 +492,8 @@ def dcm2memmap(files, slice_size, orientation): @@ -492,9 +492,8 @@ def dcm2memmap(files, slice_size, orientation):
492 492
493 def to_vtk(n_array, spacing, slice_number, orientation): 493 def to_vtk(n_array, spacing, slice_number, orientation):
494 dy, dx = n_array.shape 494 dy, dx = n_array.shape
495 - n_array.shape = dx * dy  
496 495
497 - v_image = numpy_support.numpy_to_vtk(n_array) 496 + v_image = numpy_support.numpy_to_vtk(n_array.flat)
498 497
499 print orientation 498 print orientation
500 if orientation == 'AXIAL': 499 if orientation == 'AXIAL':
invesalius/reader/analyze_reader.py
@@ -23,43 +23,12 @@ import tempfile @@ -23,43 +23,12 @@ import tempfile
23 23
24 import vtk 24 import vtk
25 25
26 -from nibabel import AnalyzeHeader 26 +from nibabel import AnalyzeImage, squeeze_image
27 27
28 def ReadAnalyze(filename): 28 def ReadAnalyze(filename):
29 - print "Reading analyze file:", filename 29 + anlz = squeeze_image(AnalyzeImage.from_filename(filename))
  30 + return anlz
30 31
31 - # Reading info from analyze header  
32 - header_file = open(filename)  
33 - header = AnalyzeHeader.from_fileobj(header_file)  
34 - xf, yf, zf = header.get_data_shape()[:3]  
35 - data_type = header.get_data_dtype().name  
36 - pixel_spacing = header.get_zooms()[:3]  
37 -  
38 - # Mapping from numpy type to vtk type.  
39 - anlz_2_vtk_type = {  
40 - 'int16': 'SetDataScalarTypeToShort',  
41 - 'uint16': 'SetDataScalarTypeToUnsignedShort',  
42 - 'float32': 'SetDataScalarTypeToFloat'  
43 - }  
44 -  
45 - print header  
46 -  
47 - reader = vtk.vtkImageReader()  
48 - reader.SetFileName(filename[:-3] + 'img')  
49 -  
50 - # Setting the endiannes based on the analyze header.  
51 - if header.endianness == '<':  
52 - reader.SetDataByteOrderToLittleEndian()  
53 - elif header.endianness == '>':  
54 - reader.SetDataByteOrderToBigEndian()  
55 -  
56 - reader.SetFileDimensionality(3)  
57 - reader.SetDataExtent(0, xf-1, 0, yf-1, 0, zf-1)  
58 - reader.SetDataSpacing(pixel_spacing)  
59 - reader.SetHeaderSize(0)  
60 - # reader.SetTransform(transform)  
61 - getattr(reader, anlz_2_vtk_type[data_type])()  
62 - reader.Update()  
63 32
64 return reader.GetOutput() 33 return reader.GetOutput()
65 34