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 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)
import sys import teem import pullDemo import ctypes npos = teem.nrrdNew() vol = pullDemo.volLoad(['../../data/fmob-c4h.nrrd:scalar:V'], './', 'ds:1,5', 1) infoList = ['h-c:V:val:0:-1', 'hgvec:V:gvec', 'hhess:V:hess', 'tan1:V:hevec2', 'strn:V:heval2:0:-1', 'lthr:V:heval2:-70:-1', 'sthr:V:heval2:-70:-1'] pullDemo.run(npos, init=[teem.pullInitMethodRandom, 300], efs=False, vol=vol, info=infoList, energy={'type':teem.pullInterTypeJustR, 'r':'cwell:0.6,-0.002'}, iterMax=200, radSpace=0.05) pullDemo.volFree(vol) if (teem.nrrdSave("npos.nrrd", npos, None)): estr = teem.biffGetDone('nrrd') print "problem running system:\n%s" % estr sys.exit(1)
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
import sys import teem import pullDemo import ctypes npos = teem.nrrdNew() vol = pullDemo.volLoad(['../../data/fmob-c4h.nrrd:scalar:V'], './', 'ds:1,5', 1) infoList = [ 'h-c:V:val:0:-1', 'hgvec:V:gvec', 'hhess:V:hess', 'tan1:V:hevec2', 'strn:V:heval2:0:-1', 'lthr:V:heval2:-70:-1', 'sthr:V:heval2:-70:-1' ] pullDemo.run(npos, init=[teem.pullInitMethodRandom, 300], efs=False, vol=vol, info=infoList, energy={ 'type': teem.pullInterTypeJustR, 'r': 'cwell:0.6,-0.002' }, iterMax=200, radSpace=0.05) pullDemo.volFree(vol) if (teem.nrrdSave("npos.nrrd", npos, None)): estr = teem.biffGetDone('nrrd') print "problem running system:\n%s" % estr sys.exit(1)