def New(self, *args, **kargs): """Use the parameters to infer the types of the template parameters. """ # extract the types from the arguments to instantiate the class import itk types = tuple(itk.class_(o) for o in args) return self[types].New(*args, **kargs)
def wrongClassName(cl, name): o = cl.New() # be sure that the type of the instantiated object is the same # than the one of the class. It can be different if the class # is an "abstract" one and don't provide any New() method. # In that case, the one of the superclass is used. return o.GetNameOfClass() != name and itk.class_(o) == cl
def __init__(self, InputImage, sigmaArray, ammount): """ Simple workflow implementing unsharp masking. """ im = itk.image(InputImage) InType = itk.class_(im) self.gaussianSmooth = itk.SmoothingRecursiveGaussianImageFilter[InType,InType].New(\ InputImage, SigmaArray = sigmaArray) self.substract = itk.SubtractImageFilter[InType,InType,InType].New(\ Input1 = InputImage, Input2 = self.gaussianSmooth.GetOutput()) self.shiftScale = itk.ShiftScaleImageFilter[InType,InType].New(\ Input = self.substract.GetOutput(), Scale = ammount, Shift = 0) self.addFilter = itk.AddImageFilter[InType,InType,InType].New(\ Input1 = self.shiftScale.GetOutput(), Input2 = InputImage)
PixelType = itk.UC dim = 2 ImageType = itk.Image[PixelType, dim] ReaderType = itk.ImageFileReader[ImageType] reader = ReaderType.New(FileName=filename) # test snake_case keyword arguments reader = ReaderType.New(file_name=filename) # test echo itk.echo(reader) itk.echo(reader, sys.stdout) # test class_ assert itk.class_(reader) == ReaderType assert itk.class_("dummy") == str # test template assert itk.template(ReaderType) == (itk.ImageFileReader, (ImageType, )) assert itk.template(reader) == (itk.ImageFileReader, (ImageType, )) try: itk.template(str) raise Exception("unknown class should send an exception") except KeyError: pass # test ctype assert itk.ctype("unsigned short") == itk.US assert itk.ctype(" unsigned \n short \t ") == itk.US assert itk.ctype("signed short") == itk.SS
PixelType = itk.UC dim = 2 ImageType = itk.Image[PixelType, dim] ReaderType = itk.ImageFileReader[ImageType] reader = ReaderType.New(FileName=fileName) # test snake_case keyword arguments reader = ReaderType.New(file_name=fileName) # test echo itk.echo(reader) itk.echo(reader, sys.stdout) # test class_ assert itk.class_(reader) == ReaderType assert itk.class_("dummy") == str # test template assert itk.template(ReaderType) == (itk.ImageFileReader, (ImageType,)) assert itk.template(reader) == (itk.ImageFileReader, (ImageType,)) try: itk.template(str) raise Exception("unknown class should send an exception") except KeyError: pass # test ctype assert itk.ctype("unsigned short") == itk.US assert itk.ctype(" unsigned \n short \t ") == itk.US assert itk.ctype("signed short") == itk.SS
fileName = sys.argv[1] PType = itk.US dim = 2 IType = itk.Image[PType, dim] ReaderType = itk.ImageFileReader[IType] reader = ReaderType.New(FileName=fileName) # test echo itk.echo(reader) itk.echo(reader, sys.stdout) # test class_ assert itk.class_(reader) == ReaderType assert itk.class_(reader.GetPointer()) == ReaderType assert itk.class_("dummy") == str # test template assert itk.template(ReaderType) == (itk.ImageFileReader, (IType,)) assert itk.template(reader) == (itk.ImageFileReader, (IType,)) assert itk.template(reader.GetPointer()) == (itk.ImageFileReader, (IType,)) try: itk.template(str) raise Exception("unknown class should send an exception") except KeyError: pass # test ctype assert itk.ctype("unsigned short") == itk.US
# wwith a wrong attribute type try: reader = readerType.New(FileName=1) raise Exception('no exception sent for wrong attribute type') except: pass # pass filter as argument for input # to a filter with SetInput method median = itk.MedianImageFilter[ImageType, ImageType].New(reader) assert reader.GetOutput() == median.GetInput() # filter type determined by the input passed as an arg median_args = itk.MedianImageFilter.New(reader.GetOutput()) assert itk.class_(median) == itk.class_(median_args) # filter type determined by the input passed as a primary method input median_kwarg = itk.MedianImageFilter.New(Input=reader.GetOutput()) assert itk.class_(median) == itk.class_(median_kwarg) # to a filter with a SetImage method calculator = itk.MinimumMaximumImageCalculator[ImageType].New(reader) # not GetImage() method here to verify it's the right image # to a filter with several inputs sub = itk.SubtractImageFilter[ImageType, ImageType, ImageType].New(reader, reader2) assert reader.GetOutput() == sub.GetInput(0) assert reader2.GetOutput() == sub.GetInput(1)
try: reader = itk.ImageFileReader.New(FileName="wrong filename") raise Exception('no exception sent for wrong file name') except RuntimeError as e: if not "The file doesn't exist." in str(e): raise e pass # pass filter as argument for input # to a filter with SetInput method median = itk.MedianImageFilter[ImageType, ImageType].New(reader) assert reader.GetOutput() == median.GetInput() # filter type determined by the input passed as an arg median_args = itk.MedianImageFilter.New(reader.GetOutput()) assert itk.class_(median) == itk.class_(median_args) # filter type determined by the input passed as a primary method input median_kwarg = itk.MedianImageFilter.New(Input=reader.GetOutput()) assert itk.class_(median) == itk.class_(median_kwarg) # to a filter with a SetImage method calculator = itk.MinimumMaximumImageCalculator[ImageType].New(reader) # not GetImage() method here to verify it's the right image # to a filter with several inputs sub = itk.SubtractImageFilter[ImageType, ImageType, ImageType].New(reader, reader2) assert reader.GetOutput() == sub.GetInput(0) assert reader2.GetOutput() == sub.GetInput(1)
# first case - that's a templated class if isinstance(T, itk.Vector.__class__) and len(T)>0: # use only the first specialization - all of them return the same name i = T.values()[0] # GetNameOfClass() is a virtual method of the LightObject class, so we must # instantiate an object with the New() method if 'New' in dir(i): I = i.New() # be sure that the type of the instantiated object is the same than the # one of the class. It can be different if the class is an "abstract" one # and don't provide any New() method. In that case, the one of the superclass # is used. if 'GetNameOfClass' in dir(I): # print "Checking", t n = I.GetNameOfClass() if n != t and itk.class_(I) == i: print >> sys.stderr, t, "doesn't provide the right name." wrongName = True else: if 'New' in dir(T): I = T.New() if 'GetNameOfClass' in dir(I): # print "Checking", t n = I.GetNameOfClass() if n != t and itk.class_(I) == T: print >> sys.stderr, t, "doesn't provide the right name." wrongName = True if wrongName: print >> sys.stderr, "Some classes are not providing the correct name."
T = itk.__dict__[t] # first case - that's a templated class if isinstance(T, itk.Vector.__class__) and len(T) > 0: # use only the first specialization - all of them return the same # name i = T.values()[0] # GetNameOfClass() is a virtual method of the LightObject class, # so we must instantiate an object with the New() method if "New" in dir(i) and "GetNameOfClass" in dir(i): totalName += 1 if wrongClassName(i, t): msg = f"{T}: wrong class name: {t}" print(msg, file=sys.stderr) wrongName += 1 else: if "New" in dir(T) and "GetNameOfClass" in dir(T): totalName += 1 if wrongClassName(T, t): msg = f"{T}: wrong class name: {t}" print(msg, file=sys.stderr) o = T.New() print(itk.class_(o), file=sys.stderr) print(o.GetNameOfClass(), file=sys.stderr) wrongName += 1 print(f"{totalName} classes checked.") if wrongName: print(f"{wrongName} classes are not providing the correct name.", file=sys.stderr) sys.exit(1)
# use only the first specialization - all of them return the same # name i = T.values()[0] # GetNameOfClass() is a virtual method of the LightObject class, # so we must instantiate an object with the New() method if 'New' in dir(i): I = i.New() # be sure that the type of the instantiated object is the same # than the one of the class. It can be different if the class # is an "abstract" one and don't provide any New() method. # In that case, the one of the superclass is used. if 'GetNameOfClass' in dir(I): # print("Checking", t) totalName += 1 n = I.GetNameOfClass() if n != t and itk.class_(I) == i: msg = "%s: wrong class name: %s" % (t, n) print(msg, file=sys.stderr) wrongName += 1 else: if 'New' in dir(T): I = T.New() if 'GetNameOfClass' in dir(I): # print("Checking", t) totalName += 1 n = I.GetNameOfClass() if n != t and itk.class_(I) == T: msg = "%s: wrong class name: %s" % (t, n) print(msg, file=sys.stderr) wrongName += 1