Commit 6dc42bf885717de6f176ffcbe092060f51c06948

Authored by tfmoraes
1 parent 69aa6af4

Handling orientations in analyze files and storing it in memmap

invesalius/control.py
@@ -19,8 +19,8 @@ @@ -19,8 +19,8 @@
19 import math 19 import math
20 import os 20 import os
21 import plistlib 21 import plistlib
22 -import tempfile  
23 22
  23 +import numpy
24 import wx.lib.pubsub as ps 24 import wx.lib.pubsub as ps
25 25
26 import constants as const 26 import constants as const
@@ -386,12 +386,16 @@ class Controller(): @@ -386,12 +386,16 @@ class Controller():
386 proj.SetAcquisitionModality("MRI") 386 proj.SetAcquisitionModality("MRI")
387 #TODO: Verify if all Analyse are in AXIAL orientation 387 #TODO: Verify if all Analyse are in AXIAL orientation
388 388
389 - if not header['orient']: 389 + # To get Z, X, Y (used by InVesaliu), not X, Y, Z
  390 + matrix, matrix_filename = utils.analyze2mmap(imagedata)
  391 + if header['orient'] == 0:
390 proj.original_orientation = const.AXIAL 392 proj.original_orientation = const.AXIAL
391 elif header['orient'] == 1: 393 elif header['orient'] == 1:
392 proj.original_orientation = const.CORONAL 394 proj.original_orientation = const.CORONAL
393 elif header['orient'] == 2: 395 elif header['orient'] == 2:
394 proj.original_orientation = const.SAGITAL 396 proj.original_orientation = const.SAGITAL
  397 + else:
  398 + proj.original_orientation = const.SAGITAL
395 399
396 proj.threshold_range = (header['glmin'], 400 proj.threshold_range = (header['glmin'],
397 header['glmax']) 401 header['glmax'])
@@ -399,7 +403,8 @@ class Controller(): @@ -399,7 +403,8 @@ class Controller():
399 proj.level = (0.5 * (proj.threshold_range[1] + proj.threshold_range[0])) 403 proj.level = (0.5 * (proj.threshold_range[1] + proj.threshold_range[0]))
400 404
401 self.Slice = sl.Slice() 405 self.Slice = sl.Slice()
402 - self.Slice.matrix = imagedata.get_data().swapaxes(0, 2) 406 + self.Slice.matrix = matrix
  407 + self.Slice.matrix_filename = matrix_filename
403 408
404 self.Slice.window_level = proj.level 409 self.Slice.window_level = proj.level
405 self.Slice.window_width = proj.window 410 self.Slice.window_width = proj.window
invesalius/data/imagedata_utils.py
@@ -507,6 +507,45 @@ def dcm2memmap(files, slice_size, orientation): @@ -507,6 +507,45 @@ def dcm2memmap(files, slice_size, orientation):
507 507
508 return matrix, scalar_range, temp_file 508 return matrix, scalar_range, temp_file
509 509
  510 +def analyze2mmap(analyze):
  511 + data = analyze.get_data()
  512 + header = analyze.get_header()
  513 + temp_file = tempfile.mktemp()
  514 +
  515 + # Sagital
  516 + if header['orient'] == 2:
  517 + print "Orientation Sagital"
  518 + shape = tuple([data.shape[i] for i in (1, 2, 0)])
  519 + matrix = numpy.memmap(temp_file, mode='w+', dtype=data.dtype, shape=shape)
  520 + for n, slice in enumerate(data):
  521 + matrix[:,:, n] = slice
  522 +
  523 + # Coronal
  524 + elif header['orient'] == 1:
  525 + print "Orientation coronal"
  526 + shape = tuple([data.shape[i] for i in (1, 0, 2)])
  527 + matrix = numpy.memmap(temp_file, mode='w+', dtype=data.dtype, shape=shape)
  528 + for n, slice in enumerate(data):
  529 + matrix[:,n,:] = slice
  530 +
  531 + # AXIAL
  532 + elif header['orient'] == 0:
  533 + print "no orientation"
  534 + shape = tuple([data.shape[i] for i in (0, 1, 2)])
  535 + matrix = numpy.memmap(temp_file, mode='w+', dtype=data.dtype, shape=shape)
  536 + for n, slice in enumerate(data):
  537 + matrix[n] = slice
  538 +
  539 + else:
  540 + print "Orientation Sagital"
  541 + shape = tuple([data.shape[i] for i in (1, 2, 0)])
  542 + matrix = numpy.memmap(temp_file, mode='w+', dtype=data.dtype, shape=shape)
  543 + for n, slice in enumerate(data):
  544 + matrix[:,:, n] = slice
  545 +
  546 + matrix.flush()
  547 + return matrix, temp_file
  548 +
510 def to_vtk(n_array, spacing, slice_number, orientation): 549 def to_vtk(n_array, spacing, slice_number, orientation):
511 try: 550 try:
512 dz, dy, dx = n_array.shape 551 dz, dy, dx = n_array.shape