def __find_param__(self, paramSetString): """find the parameters of the template paramSetString is the c++ string which define the parameters set __find_param__ returns a list of itk classes, itkCType, and/or numbers which correspond to the parameters described in paramSetString. The parameters MUST have been registered before calling this method, or __find_param__ will return a string and not the wanted object, and will display a warning. Registration order is important. This method is not static only to be able to display the template name in the warning """ # split the string in a list of parameters paramStrings = [] inner = 0 part = paramSetString.split(",") for elt in part: if inner == 0: paramStrings.append(elt) else: paramStrings[-1] += "," + elt inner += elt.count("<") - elt.count(">") # convert all string parameters into classes (if possible) parameters = [] for param in paramStrings: # the parameter need to be normalized several time below # do it once here param = param.strip() paramNorm = normalizeName(param) if itkTemplate.__templates__.has_key(paramNorm): # the parameter is registered. # just get the really class form the dictionary param = itkTemplate.__templates__[paramNorm] elif itkCType.GetCType(param): # the parameter is a c type # just get the itkCtype instance param = itkCType.GetCType(param) elif paramNorm.isdigit(): # the parameter is a number # convert the string to a number ! param = int(param) else: # unable to convert the parameter # use it without changes, but display a warning message, to incite # developer to fix the problem print >> sys.stderr, "Warning: Unknown parameter '%s' in template '%s'" % ( param, self.__name__) parameters.append(param) return parameters
def ctype(s): """Return the c type corresponding to the string passed in parameter The string can contain some extra spaces. see also itkCType """ from itkTypes import itkCType ret = itkCType.GetCType(" ".join(s.split())) if ret is None: raise KeyError("Unrecognized C type '%s'" % s) return ret
def loadItkImage(self, filename): base = itk.ImageIOFactory.CreateImageIO(filename, itk.ImageIOFactory.ReadMode) base.SetFileName(filename) base.ReadImageInformation() componentType = base.GetComponentType() itkctype = itkCType.GetCType("float") imageType = itk.Image[itkctype, base.GetNumberOfDimensions()] reader = itk.ImageFileReader[imageType].New() reader.SetFileName(filename) reader.Update() self.itkImage = reader.GetOutput() self.itkPixelType = itkctype self.dimensions = base.GetNumberOfDimensions()