Commit 4893bc0acd48b6a8f880c2c5a877a27829e5becc
1 parent
5ba88607
Exists in
master
and in
6 other branches
ENH: Rewritting the analyze reader using nipy
Showing
1 changed file
with
39 additions
and
63 deletions
Show diff stats
invesalius/reader/analyze_reader.py
@@ -18,37 +18,55 @@ | @@ -18,37 +18,55 @@ | ||
18 | #-------------------------------------------------------------------------- | 18 | #-------------------------------------------------------------------------- |
19 | 19 | ||
20 | import os | 20 | import os |
21 | - | ||
22 | -import itk | ||
23 | import multiprocessing | 21 | import multiprocessing |
24 | import tempfile | 22 | import tempfile |
23 | + | ||
25 | import vtk | 24 | import vtk |
26 | 25 | ||
26 | +from nipy.io.imageformats import AnalyzeHeader | ||
27 | 27 | ||
28 | def ReadAnalyze(filename): | 28 | def ReadAnalyze(filename): |
29 | - | ||
30 | - pipe_in, pipe_out = multiprocessing.Pipe() | ||
31 | - | ||
32 | - sp = ItktoVtk(pipe_in, filename) | ||
33 | - sp.start() | ||
34 | - | ||
35 | - while 1: | ||
36 | - msg = pipe_out.recv() | ||
37 | - if(msg is None): | ||
38 | - break | ||
39 | - | ||
40 | - filename = pipe_out.recv() | ||
41 | - | ||
42 | - reader = vtk.vtkXMLImageDataReader() | ||
43 | - reader.SetFileName(filename) | 29 | + print "Reading analyze file:", filename |
30 | + | ||
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_datatype() | ||
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])() | ||
44 | reader.Update() | 62 | reader.Update() |
45 | - | ||
46 | - os.remove(filename) | ||
47 | - | 63 | + |
48 | return reader.GetOutput() | 64 | return reader.GetOutput() |
49 | 65 | ||
50 | def ReadDirectory(dir_): | 66 | def ReadDirectory(dir_): |
51 | - file_list = [] | 67 | + """ |
68 | + Looking for analyze files in the given directory | ||
69 | + """ | ||
52 | imagedata = None | 70 | imagedata = None |
53 | for root, sub_folders, files in os.walk(dir_): | 71 | for root, sub_folders, files in os.walk(dir_): |
54 | for file in files: | 72 | for file in files: |
@@ -57,45 +75,3 @@ def ReadDirectory(dir_): | @@ -57,45 +75,3 @@ def ReadDirectory(dir_): | ||
57 | imagedata = ReadAnalyze(filename) | 75 | imagedata = ReadAnalyze(filename) |
58 | return imagedata | 76 | return imagedata |
59 | return imagedata | 77 | return imagedata |
60 | - | ||
61 | - | ||
62 | -class ItktoVtk(multiprocessing.Process): | ||
63 | - | ||
64 | - def __init__(self, pipe, filename): | ||
65 | - multiprocessing.Process.__init__(self) | ||
66 | - self.filename = filename | ||
67 | - self.pipe = pipe | ||
68 | - | ||
69 | - def run(self): | ||
70 | - self.Convert() | ||
71 | - | ||
72 | - def Convert(self): | ||
73 | - | ||
74 | - import ItkVtkGlue | ||
75 | - | ||
76 | - reader = itk.ImageFileReader.IUC3.New() | ||
77 | - reader.SetFileName(self.filename) | ||
78 | - reader.Update() | ||
79 | - | ||
80 | - x_spacing = reader.GetOutput().GetSpacing().GetElement(0) | ||
81 | - y_spacing = reader.GetOutput().GetSpacing().GetElement(1) | ||
82 | - z_spacing = reader.GetOutput().GetSpacing().GetElement(2) | ||
83 | - spacing = (x_spacing, y_spacing, z_spacing) | ||
84 | - | ||
85 | - glue = ItkVtkGlue.ImageToVTKImageFilter.IUC3.New() | ||
86 | - glue.SetInput(reader.GetOutput()) | ||
87 | - glue.Update() | ||
88 | - | ||
89 | - imagedata = vtk.vtkImageData() | ||
90 | - imagedata.DeepCopy(glue.GetOutput()) | ||
91 | - imagedata.SetSpacing(spacing) | ||
92 | - | ||
93 | - filename = tempfile.mktemp() | ||
94 | - writer = vtk.vtkXMLImageDataWriter() | ||
95 | - writer.SetInput(imagedata) | ||
96 | - writer.SetFileName(filename) | ||
97 | - writer.Write() | ||
98 | - | ||
99 | - self.pipe.send(None) | ||
100 | - self.pipe.send(filename) | ||
101 | - | ||
102 | \ No newline at end of file | 78 | \ No newline at end of file |