File: init.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 (185 lines) | stat: -rw-r--r-- 7,772 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import paraview
import paraview.simple
import paraview.servermanager as sm
import PVBase
import core.modules
import core.modules.module_registry
from pvconfig import QPVConfigWindow
from core.modules.vistrails_module import new_module, Module, ModuleError
from core.modules.module_registry import (registry, add_module,
                                          has_input_port,
                                          add_input_port, add_output_port)
from core.modules.source_configure import SourceConfigurationWidget
from core.modules.python_source_configure import PythonEditor, PythonSourceConfigurationWidget
from core.modules.basic_modules import PythonSource
from configuration import configuration
import urllib

forbidden = ['AlltoN',
             'Balance',
             'CTHSurface',
             'SelectionSourceBase']


class ProgrammableFilterConfigurationWidget(SourceConfigurationWidget):
    def __init__(self, module, controller, parent=None):
        SourceConfigurationWidget.__init__(self, module, controller, 
                                           PythonEditor, False, False, parent,
                                           portName='Script', encode=False)

class PVServerPythonSourceConfigurationWidget(SourceConfigurationWidget):
    def __init__(self, module, controller, parent=None):
        SourceConfigurationWidget.__init__(self, module, controller, 
                                           PythonEditor, True, False, parent,
                                           portName='Script', encode=False)

class PVServerPythonSource(PVBase.PVModule):

    def compute(self):
        if self.pvInstance:
            del self.pvInstance
        paraview.simple.SetActiveSource(None)
        inputDefs = ''
        for k in self.inputPorts:
            if k=='Script':
                continue
            value = self.getInputFromPort(k)
            if type(value)==str:
                inputDefs += '%s = "%s"\n' % (k, value.replace('"', '\\"'))
            elif type(value)==int:
                inputDefs += '%s = %s\n' % (k, value)
        prefix = """
import paraview.servermanager as sm
import paraview.vtk.dataset_adapter as DA
proc = sm.vtkProcessModule.GetProcessModule()
nPartitions = proc.GetNumberOfLocalPartitions()
partitionId = proc.GetPartitionId()
result = ''
"""
        source = self.forceGetInputFromPort('Script', '')
        suffix = """
if len(result)>0:
    import vtk
    dataImporter = vtk.vtkImageImport()
    dataImporter.CopyImportVoidPointer(result, len(result))
    dataImporter.SetDataScalarTypeToUnsignedChar()
    dataImporter.SetNumberOfScalarComponents(1)
    dataImporter.SetDataExtent(0, len(result)-1, 0, 0, 0, 0)
    dataImporter.SetWholeExtent(0, len(result)-1, 0, 0, 0, 0)
    dataImporter.Update()
    self.GetOutputDataObject(0).DeepCopy(dataImporter.GetOutput())
"""
        self.pvInstance = paraview.simple.ProgrammableFilter()
        self.pvInstance.OutputDataSetType = 'vtkImageData'
        self.pvInstance.Script = inputDefs + prefix + source + suffix
        self.pvInstance.UpdatePipeline()

        self.setResult('Output', self)


class PVClientFetch(PythonSource):

    def compute(self):
        prefix = """
import paraview.servermanager as sm
import paraview.vtk.dataset_adapter as DA
proc = sm.vtkProcessModule.GetProcessModule()
nPartitions = proc.GetNumberOfPartitions(sm.ActiveConnection.ID)
module = self.getInputFromPort('ServerModule')
results = []
for i in xrange(nPartitions):
    data = sm.Fetch(module.pvInstance, i)
    if data==None or data.GetPointData().GetNumberOfArrays()==0:
        continue
    narray = DA.numpy_support.vtk_to_numpy(data.GetPointData().GetArray(0))
    result = narray.tostring()
    results.append(result)
"""
        s = urllib.unquote(str(self.forceGetInputFromPort('source', '')))
        self.run_code(prefix + s, use_input=True, use_output=True)
        
        
def add_paraview_module(name, proxy, module_type, ns, hide=False, pvFunction=None):

    mod = new_module(module_type, name)
    mod.pvSpace = ns.lower()
    mod.pvClass = name
    if pvFunction != None:
        mod.pvFunction = pvFunction
    if name=='ProgrammableFilter':
        add_module(mod, name = name, namespace=ns, configureWidgetType=ProgrammableFilterConfigurationWidget)
    else:
        add_module(mod, name = name, namespace=ns)

    for prop in proxy.ListProperties():
        optional = False
        if hide and prop != "Input":
            optional = True
        p = proxy.GetProperty(prop)
        if isinstance(p, sm.ProxyProperty):
            add_input_port(mod, prop, PVBase.PVModule, optional)
            continue
        if isinstance(p, sm.EnumerationProperty):
            add_input_port(mod, prop, core.modules.basic_modules.String, optional)
            continue
        if isinstance(p, sm.VectorProperty):
            params = []
            typ = None
            if p.IsA("vtkSMDoubleVectorProperty"):
                typ = core.modules.basic_modules.Float
            elif p.IsA("vtkSMIntVectorProperty"):
                typ = core.modules.basic_modules.Integer
            elif p.IsA("vtkSMStringVectorProperty"):
                typ = core.modules.basic_modules.String
            elif p.IsA("vtkSMIdTypeVectorProperty"):
                typ = core.modules.basic_modules.Integer
            nel = len(p)
            if nel > 0:
                for i in range(nel):
                    params.append(typ)
            else:
                params.append(typ)
            add_input_port(mod, prop, params, optional)

    add_output_port(mod, "Output", module_type)
        
def initialize(*args, **keywords):
    reg = core.modules.module_registry
    basic = core.modules.basic_modules
    add_module(PVBase.PVModule, namespace='base')
    mod_dict = {}

    mlist = [("Sources", sm.sources), ("Filters", sm.filters), 
             ("Animation", sm.animation), ("Writers", sm.writers)]
    for ns, m in mlist:
        dt = m.__dict__
        for key in dt.keys():
            if forbidden.__contains__(key):
                continue
            if key.__contains__('Base'):
                continue
            cl = dt[key]
            if not isinstance(cl, str):
                if paraview.simple._func_name_valid(key):
                    add_paraview_module(key, m.__dict__[key](no_update=True), PVBase.PVModule, ns)


    add_paraview_module("GeometryRepresentation", sm.rendering.GeometryRepresentation(no_update=True), PVBase.PVModule, "Rendering", True, sm.rendering.GeometryRepresentation)
    add_paraview_module("PVLookupTable", sm.rendering.PVLookupTable(no_update=True), PVBase.PVModule, "Rendering", False, sm.rendering.PVLookupTable)
    add_paraview_module("ScalarBarWidgetRepresentation", sm.rendering.ScalarBarWidgetRepresentation(no_update=True), PVBase.PVModule, "Rendering", True, sm.rendering.ScalarBarWidgetRepresentation)
    add_module(PVServerPythonSource, name = "PVServerPythonSource", configureWidgetType=PVServerPythonSourceConfigurationWidget)
    add_input_port(PVServerPythonSource, "Script", (core.modules.basic_modules.String, ""), True)
    add_output_port(PVServerPythonSource, "self", PVServerPythonSource)
    add_module(PVClientFetch, name = "PVClientFetch", configureWidgetType=PythonSourceConfigurationWidget)
    add_input_port(PVClientFetch, "ServerModule", (PVServerPythonSource, ""))
    add_input_port(PVClientFetch, "source", (core.modules.basic_modules.String, ""))

    import pvcell
    pvcell.registerSelf()

    global pvConfigWindow
    pvConfigWindow = QPVConfigWindow(proc_num=configuration.num_proc,
                                     port=configuration.port)
    pvConfigWindow.show()
    if configuration.start_server == True:
        pvConfigWindow.togglePVServer()