File: filter_generator.py

package info (click to toggle)
vistrails 2.1.1-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,208 kB
  • ctags: 46,250
  • sloc: python: 316,267; xml: 52,512; sql: 3,627; php: 731; sh: 260; makefile: 108
file content (170 lines) | stat: -rw-r--r-- 5,980 bytes parent folder | download
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
# This is a rough code generation tool that works to generate code used to wrap itk functions for Vistrails
# In order to use this one must:
#   -set generation variables
#   -run script
#   -paste output into desired *Filters.py file
#   -add new filter name to __init__.py file
#
#NOTE: some filters require special calls, params, etc. that are not generated with this code
#      for example, many filters only accept decimal PixelTypes meaning that the
#      filter initialization should be wrapped in a try/except raising a ModuleError
#
# For example filter implementation, check out:
#       http://math.nist.gov/mcsd/savg/software/filters/
#       http://voxel.jouy.inra.fr/darcs/contrib-itk/WrapITK/Python/Tests/

##########################################################
#String Manipulation Methods
##########################################################
def AddSpaces(word):
    output = ""
    for char in word:
        if char.isupper() and word.index(char)!=0:
            output += " %s"%char
        else:
            output += char
    return output

def SplitCap(word):
    temp = word.split("_")
    output = ""
    for elem in temp:
        output += "%s "%elem.capitalize()
    return output.strip()

##########################################################
#SETUP generation variables
##########################################################
#filter_name should be equal to itk.filter_name
filter_name = "BinaryErodeImageFilter"

#designate namespace
namespace = "Filter|Neighborhood"

#some filters require kernel inputs, usually they don't though
has_kernel = True

#usage: {variable name:[variable type, default value, itk filter set method]}
#NOTE: you must look up the itk filter set method (dir(itk.filter_name))
filter_values = {"level":["basic.Float",None, "SetSquaredDistance"],
                 "lower_threshold":["basic.Float",None, "SetUseImageSpacing"]}

##########################################################
#code generation
##########################################################
out_class = """class %s(Module):
    my_namespace = '%s'"""%(filter_name, namespace)

compute = """
    def compute(self):
        im = self.getInputFromPort("Input Image")

        #check for input PixelType
        if self.hasInputFromPort("Input PixelType"):
            inPixelType = self.getInputFromPort("Input PixelType")
        else:
            inPixelType = im.getPixelType()

        #check for output PixelType
        if self.hasInputFromPort("Output PixelType"):
            outPixelType = self.getInputFromPort("Output PixelType")
        else:
            outPixelType = inPixelType

        #check for dimension
        if self.hasInputFromPort("Dimension"):
            dim = self.getInputFromPort("Dimension")
        else:
            dim = im.getDim()
            """
if has_kernel:
    compute += """
        kernel = self.getInputFromPort("Kernel")
        """

compute += """
        #set up filter
        inImgType = itk.Image[inPixelType._type, dim]
        outImgType = itk.Image[outPixelType._type, dim]
"""
for param in filter_values:
    #check if default value is set
    if type(filter_values[param][1]).__name__ != "NoneType":
        compute += """
        if self.hasInputFromPort("%s"):
            %s = self.getInputFromPort("%s")
        else:
            %s = %d\n"""%(SplitCap(param), param, SplitCap(param), param, filter_values[param][1])
    else:
        compute += """
        %s = self.getInputFromPort("%s")\n"""%(param, SplitCap(param))

if has_kernel:
    compute += """
        self.filter_ = itk.%s[inImgType, outImgType, kernel].New(im.getImg())
        self.filter_.SetKernel(kernel)"""%filter_name

else:
    compute += """
        self.filter_ = itk.%s[inImgType, outImgType].New(im.getImg())"""%filter_name

#set filter values
for param in filter_values:
    compute += """
        self.filter_.%s(%s)"""%(filter_values[param][2], param)

compute += """
        self.filter_.Update()

        #setup output image
        outIm = Image()
        outIm.setImg(self.filter_.GetOutput())
        outIm.setPixelType(outPixelType)
        outIm.setDim(dim)

        #set results
        self.setResult("Output Image", outIm)
        self.setResult("Filter", self)
        self.setResult("Output PixelType", outPixelType)
"""
register = """
    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, name="%s", namespace=cls.my_namespace)

        reg.add_input_port(cls, "Input Image", (Image, 'Input Image'))
        reg.add_input_port(cls, "Input PixelType", (PixelType, 'Input PixelType'),True)
        reg.add_input_port(cls, "Output PixelType", (PixelType, 'Output PixelType'),True)
        reg.add_input_port(cls, "Dimension", (basic.Integer, 'Dimension'),True)
"""%AddSpaces(filter_name)
if has_kernel:
    register += """
        reg.add_input_port(cls, "Kernel", (Kernel, 'Kernel'))
        """

for param in filter_values:
    #check if default value is set
    if type(filter_values[param][1]).__name__ != "NoneType":
        register += """
        reg.add_input_port(cls, "%s", (%s, '%s'),True)"""%(SplitCap(param),\
                                                          filter_values[param][0],\
                                                          SplitCap(param))
    else:
        register += """
        reg.add_input_port(cls, "%s", (%s, '%s'))"""%(SplitCap(param),\
                                                     filter_values[param][0],\
                                                     SplitCap(param))

register += """

        reg.add_output_port(cls, "Output Image", (Image, 'Output Image'))
        reg.add_output_port(cls, "Filter", (Filter, 'Filter'), True)
        reg.add_output_port(cls, "Output PixelType", (PixelType, 'Output PixelType'),True)
"""

##########################################################
#Output
##########################################################
print out_class
print compute
print register