File: Nrrd.py

package info (click to toggle)
teem 1.12.0~20160122-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,444 kB
  • sloc: ansic: 152,549; python: 10,748; perl: 281; sh: 58; makefile: 41; cpp: 26
file content (183 lines) | stat: -rwxr-xr-x 7,594 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
##
##  Nrrd.py: bridge between Nrrd and Numpy arrays
##  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
##  created by Sam Quinan - samquinan@cs.uchicago.edu
##
##  Permission is hereby granted, free of charge, to any person obtaining
##  a copy of this software and associated documentation files (the
##  "Software"), to deal in the Software without restriction, including
##  without limitation the rights to use, copy, modify, merge, publish,
##  distribute, sublicense, and/or sell copies of the Software, and to
##  permit persons to whom the Software is furnished to do so, subject to
##  the following conditions:
##
##  The above copyright notice and this permission notice shall be
##  included in all copies or substantial portions of the Software.
##
##  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
##  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
##  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
##  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
##  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
##  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
##  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##

import numpy, teem, ctypes

# translates from numpy type to teem type
def ndarrayGetTypes( array ):
    typeTable = {
        numpy.int8      :   (ctypes.c_byte, teem.nrrdTypeChar),
        numpy.uint8     :   (ctypes.c_ubyte, teem.nrrdTypeUChar),
        numpy.int16     :   (ctypes.c_short, teem.nrrdTypeShort),
        numpy.uint16    :   (ctypes.c_ushort, teem.nrrdTypeUShort),
        numpy.int       :   (ctypes.c_int, teem.nrrdTypeInt),
        numpy.uint      :   (ctypes.c_uint, teem.nrrdTypeUInt),
        numpy.int32     :   (ctypes.c_int32, teem.nrrdTypeInt),
        numpy.uint32    :   (ctypes.c_uint32, teem.nrrdTypeUInt),
        numpy.int64     :   (ctypes.c_int64, teem.nrrdTypeLLong),
        numpy.uint64    :   (ctypes.c_uint64, teem.nrrdTypeULLong),
        numpy.float     :   (ctypes.c_float, teem.nrrdTypeFloat),
        numpy.double    :   (ctypes.c_double, teem.nrrdTypeDouble)     }

    dt = array.dtype.type

    if dt not in typeTable:
        raise Exception("array type %s not supported" % dt)
    # sanity checking
    ct, tt = typeTable[dt]
    x = numpy.dtype(dt).itemsize
    y = ctypes.sizeof(ct)
    z = teem.nrrdTypeSize[tt]
    if not (x == y) or not (x == z) or not (y == z):
        raise Exception("corresponding numpy, teem, and ctypes types are not of the same size")
    return typeTable[dt]

def teemTypeToNumpyType(teem_type):
    numpy_endianess_glyph = '<' if teem.airMyEndian() == 1234 else '>'
    numpy_type_map = {
        teem.nrrdTypeChar: numpy_endianess_glyph + 'i1',
        teem.nrrdTypeUChar: numpy_endianess_glyph + 'u1',
        teem.nrrdTypeShort: numpy_endianess_glyph + 'i2',
        teem.nrrdTypeUShort: numpy_endianess_glyph + 'u2',
        teem.nrrdTypeInt: numpy_endianess_glyph + 'i4',
        teem.nrrdTypeUInt: numpy_endianess_glyph + 'u4',
        teem.nrrdTypeLLong: numpy_endianess_glyph + 'i8',
        teem.nrrdTypeULLong: numpy_endianess_glyph + 'u8',
        teem.nrrdTypeFloat: numpy_endianess_glyph + 'f4',
        teem.nrrdTypeDouble: numpy_endianess_glyph + 'f8'
    }
    return numpy_type_map[teem_type]


# design based on a combination of work done by Carlos Scheidegger
#   [ http://code.google.com/p/python-teem/source/browse/trunk/teem/capi/numpy/__init__.py ,
#     http://code.google.com/p/python-teem/source/browse/trunk/teem/nrrd.py ]
#   and ideas presented by Travis Oliphant [ http://blog.enthought.com/?p=62 ]

class Nrrd:
    def __init__(self):
        self._ctypesobj = teem.nrrdNew()
        # self._init = False
        self.base_ref = None
        self.teem = teem # world's ugliest hack -- but it appears to work

    def __del__(self):
        if self.base_ref == None:
            self.teem.nrrdNuke(self._ctypesobj)
        else:
            self.teem.nrrdNix(self._ctypesobj)
        self.base_ref = None
        self.teem = None

    def __get_array_interface(self):
        nrrd = self._ctypesobj.contents
        s = []
        for i in reversed(range(nrrd.dim)):
            s.append(nrrd.axis[i].size)
        r = {'shape': tuple(s),
            'typestr': teemTypeToNumpyType(nrrd.type),
            'data': (nrrd.data, False),
            'version': 3}
        return r

    __array_interface__ = property(__get_array_interface)

    def fromNDArray(self, array):

        if self.base_ref != None:
            self.base_ref = None
            teem.nrrdNix(self._ctypesobj)
            self._ctypesobj = teem.nrrdNew()

        try:
            cTy, teemTy = ndarrayGetTypes(array)
        except Exception as err:
            print err
            return

        c_type_p = ctypes.POINTER( cTy )
        if (array.flags.f_contiguous): # memory shared with array
            array_p = array.ctypes.data_as(c_type_p)
            dims = list(array.shape)
            sizes = (ctypes.c_size_t * teem.NRRD_DIM_MAX)(*dims)
            teem.nrrdWrap_nva(self._ctypesobj, array_p, teemTy, ctypes.c_uint(array.ndim), sizes)
            self.base_ref = array
        elif (array.flags.c_contiguous): # memory shared with array
            array_p = array.ctypes.data_as(c_type_p)
            dims = list(reversed(array.shape))
            sizes = (ctypes.c_size_t * teem.NRRD_DIM_MAX)(*dims)
            teem.nrrdWrap_nva(self._ctypesobj, array_p, teemTy, ctypes.c_uint(array.ndim), sizes)
            self.base_ref = array
        else: # must make c_contiguous -- memory not shared with array, transfer full control to teem
            arr = numpy.ascontiguousarray(array, dtype=array.dtype.type)
            array_p = arr.ctypes.data_as(c_type_p)
            dims = list(reversed(arr.shape))
            nrrd_tmp = teem.nrrdNew()
            sizes = (ctypes.c_size_t * teem.NRRD_DIM_MAX)(*dims)
            teem.nrrdWrap_nva(nrrd_tmp, array_p, teemTy, ctypes.c_uint(array.ndim), sizes)
            teem.nrrdCopy(self._ctypesobj, nrrd_tmp) # this call hangs with fmob-ch4.nrrd
            teem.nrrdNix(nrrd_tmp)
            self.base_ref = None
        self._init = True

        if isinstance(array, ExtendedArray):
            if not array.base_ref is None: # means there is some Nrrd file sharing it's memory with the array
                teem.nrrdAxisInfoCopy(self._ctypesobj, array.base_ref._ctypesobj, None, teem.NRRD_AXIS_INFO_SIZE_BIT)

    def load(self, file, args=None):

        if self.base_ref != None:
            self.base_ref = None
            teem.nrrdNix(self._ctypesobj)
            self._ctypesobj = teem.nrrdNew()

        teem.nrrdLoad(self._ctypesobj, file, args)
        self.base_ref = None
        self._init = True

    def save(self, file, args=None):
        teem.nrrdSave(file, self._ctypesobj, args)




class ExtendedArray(numpy.ndarray):
    def __new__(cls, input_array, base_ref=None):
        if isinstance(input_array, Nrrd):
            if input_array.base_ref == None:
                obj = numpy.asarray(input_array).view(cls)
                obj.base_ref = input_array
            else:
                obj = numpy.asarray(input_array.base_ref).view(cls)
                obj.base_ref = None
        else:
            obj = numpy.asarray(input_array).view(cls)
            obj.base_ref = base_ref
        return obj

    def __array_finalize__(self, obj):
        if obj is None: return
        self.base_ref = getattr(obj, 'base_ref', None)