|
Slicer 4.2
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
|
00001 00002 # 00003 # General 00004 # 00005 00006 EXIT_SUCCESS = 0 00007 EXIT_FAILURE = 1 00008 00009 def quit(): 00010 exit(EXIT_SUCCESS) 00011 00012 def exit(status=EXIT_SUCCESS): 00013 from slicer import app 00014 app.exit(status) 00015 00016 def restart(): 00017 from slicer import app 00018 app.restart() 00019 00020 # 00021 # Custom Import 00022 # 00023 00024 def importVTKClassesFromDirectory(directory, dest_module_name, filematch = '*'): 00025 import glob, os 00026 for fname in glob.glob(directory + '/' + filematch): 00027 try: 00028 vtk_module_name = os.path.splitext(os.path.basename(fname))[0] 00029 importVTKClasses(vtk_module_name, dest_module_name) 00030 except ImportError as detail: 00031 # TODO: this message should go in the application error log (how?) 00032 print detail 00033 00034 def importVTKClasses(vtk_module_name, dest_module_name): 00035 """Import VTK classes from module identified by vtk_module_name into 00036 the module identified by 'dest_module_name'.""" 00037 00038 # Obtain a reference to the module idenitifed by 'dest_module_name' 00039 import sys 00040 dest_module = sys.modules[dest_module_name] 00041 00042 exec "import %s" % (vtk_module_name) 00043 00044 # Obtain a reference to the associated VTK module 00045 module = eval(vtk_module_name) 00046 00047 # Loop over content of the python module associated with the given VTK python library 00048 for item_name in dir(module): 00049 00050 # Obtain a reference associated with the current object 00051 item = eval("%s.%s" % (vtk_module_name, item_name)) 00052 00053 # Add the vtk class to dest_module_globals_dict if any 00054 if type(item).__name__ == 'vtkclass': 00055 exec("from %s import %s" % (vtk_module_name, item_name)) 00056 exec("dest_module.%s = %s"%(item_name, item_name)) 00057 00058 # 00059 # UI 00060 # 00061 00062 def lookupTopLevelWidget(objectName, verbose = True): 00063 """Loop over all top level widget associated with 'slicer.app' and 00064 return the one matching 'objectName'""" 00065 from slicer import app 00066 for w in app.topLevelWidgets(): 00067 if w.objectName == objectName: return w 00068 if verbose: 00069 print "Failed to obtain reference to '%s'" % objectName 00070 return None 00071 00072 def mainWindow(verbose = True): 00073 return lookupTopLevelWidget('qSlicerMainWindow', verbose) 00074 00075 def pythonShell(verbose = True): 00076 return lookupTopLevelWidget('pythonConsole', verbose) 00077 00078 def showStatusMessage(message, duration = 0): 00079 mw = mainWindow(verbose=False) 00080 if mw: 00081 mw.statusBar().showMessage(message, duration) 00082 00083 def findChildren(widget=None,name="",text=""): 00084 """ return a list of child widgets that match the passed name """ 00085 # TODO: figure out why the native QWidget.findChildren method 00086 # does not seem to work from PythonQt 00087 if not widget: 00088 widget = mainWindow() 00089 children = [] 00090 parents = [widget] 00091 while parents != []: 00092 p = parents.pop() 00093 parents += p.children() 00094 if name and p.name == name: 00095 children.append(p) 00096 elif text: 00097 try: 00098 p.text 00099 if p.text == text: 00100 children.append(p) 00101 except AttributeError: 00102 pass 00103 return children 00104 00105 # 00106 # IO 00107 # 00108 00109 def loadNodeFromFile(filename, filetype, returnNode=False): 00110 from slicer import app 00111 from vtk import vtkCollection 00112 properties = {'fileName':filename} 00113 00114 if returnNode: 00115 loadedNodes = vtkCollection() 00116 success = app.coreIOManager().loadNodes(filetype, properties, loadedNodes) 00117 return success, loadedNodes.GetItemAsObject(0) 00118 else: 00119 success = app.coreIOManager().loadNodes(filetype, properties) 00120 return success 00121 00122 def loadColorTable(filename, returnNode=False): 00123 from slicer import qSlicerIO 00124 filetype = qSlicerIO.ColorTableFile 00125 return loadNodeFromFile(filename, filetype, returnNode) 00126 00127 def loadFiberBundle(filename, returnNode=False): 00128 from slicer import qSlicerIO 00129 filetype = qSlicerIO.FiberBundleFile 00130 return loadNodeFromFile(filename, filetype, returnNode) 00131 00132 def loadFiducialList(filename, returnNode=False): 00133 from slicer import qSlicerIO 00134 filetype = qSlicerIO.FiducialListFile 00135 return loadNodeFromFile(filename, filetype, returnNode) 00136 00137 def loadModel(filename, returnNode=False): 00138 from slicer import qSlicerIO 00139 filetype = qSlicerIO.ModelFile 00140 return loadNodeFromFile(filename, filetype, returnNode) 00141 00142 def loadScalarOverlay(filename, returnNode=False): 00143 from slicer import qSlicerIO 00144 filetype = qSlicerIO.ScalarOverlayFile 00145 return loadNodeFromFile(filename, filetype, returnNode) 00146 00147 def loadTransform(filename, returnNode=False): 00148 from slicer import qSlicerIO 00149 filetype = qSlicerIO.TransformFile 00150 return loadNodeFromFile(filename, filetype, returnNode) 00151 00152 def loadVolume(filename, returnNode=False): 00153 from slicer import qSlicerIO 00154 filetype = qSlicerIO.VolumeFile 00155 return loadNodeFromFile(filename, filetype, returnNode) 00156 00157 00158 def loadScene(filename, clear = True): 00159 from slicer import app, qSlicerIO 00160 filetype = qSlicerIO.SceneFile 00161 properties = {'fileName':filename} 00162 return app.coreIOManager().loadNodes(filetype, properties) 00163 00164 def openAddDataDialog(): 00165 from slicer import app 00166 return app.coreIOManager().openAddDataDialog() 00167 00168 def openAddVolumeDialog(): 00169 from slicer import app 00170 return app.coreIOManager().openAddVolumeDialog() 00171 00172 def openAddModelDialog(): 00173 from slicer import app 00174 return app.coreIOManager().openAddModelDialog() 00175 00176 def openAddScalarOverlayDialog(): 00177 from slicer import app 00178 return app.coreIOManager().openAddScalarOverlayDialog() 00179 00180 def openAddTransformDialog(): 00181 from slicer import app 00182 return app.coreIOManager().openAddTransformDialog() 00183 00184 def openAddColorTableDialog(): 00185 from slicer import app 00186 return app.coreIOManager().openAddColorTableDialog() 00187 00188 def openAddFiducialDialog(): 00189 from slicer import app 00190 return app.coreIOManager().openAddFiducialDialog() 00191 00192 def openAddFiberBundleDialog(): 00193 from slicer import app 00194 return app.coreIOManager().openAddFiberBundleDialog() 00195 00196 def openSaveDataDialog(): 00197 from slicer import app 00198 return app.coreIOManager().openSaveDataDialog() 00199 00200 # 00201 # Module 00202 # 00203 00204 def selectModule(module): 00205 moduleName = module 00206 if not isinstance(module, basestring): 00207 moduleName = module.name 00208 w = mainWindow() 00209 if not w: return 00210 w.moduleSelector().selectModule(moduleName) 00211 00212 def moduleNames(): 00213 from slicer import app 00214 return app.moduleManager().factoryManager().moduleNames() 00215 00216 def getModule(moduleName): 00217 from slicer import app 00218 module = app.moduleManager().module(moduleName); 00219 if not module: 00220 print "Could not find module with name '%s" % moduleName 00221 return None 00222 return module 00223 00224 def getModuleGui(module): 00225 if isinstance(module, basestring): 00226 module = getModule(module) 00227 if not module: 00228 return None 00229 widgetRepr = module.widgetRepresentation() 00230 if not widgetRepr: 00231 print "Could not find module widget representation with name '%s" % moduleName 00232 return None 00233 return widgetRepr 00234 00235 # 00236 # MRML 00237 # 00238 00239 def getNodes(pattern = ""): 00240 """Return a dictionary of nodes where the name or id matches the 'pattern'. 00241 Providing an empty 'pattern' string will return all nodes. 00242 """ 00243 import slicer, fnmatch 00244 nodes = {} 00245 scene = slicer.mrmlScene 00246 count = scene.GetNumberOfNodes() 00247 for idx in range(count): 00248 node = scene.GetNthNode(idx) 00249 name = node.GetName() 00250 id = node.GetID() 00251 if fnmatch.fnmatch(name, pattern) or fnmatch.fnmatch(id, pattern): 00252 nodes[node.GetName()] = node 00253 return nodes 00254 00255 def getNode(pattern = "", index = 0): 00256 """Return the indexth node where name or id matches 'pattern'. 00257 Providing an empty 'pattern' string will return all nodes. 00258 """ 00259 nodes = getNodes(pattern) 00260 try: 00261 if nodes.keys(): 00262 return nodes.values()[index] 00263 except IndexError: 00264 return None 00265 00266 # 00267 # MRML-numpy 00268 # 00269 00270 def array(pattern = "", index = 0): 00271 """Return the array you are "most likely to want" from the indexth 00272 MRML node that matches the pattern. Meant to be used in the python 00273 console for quick debugging/testing. More specific API should be 00274 used in scripts to be sure you get exactly what you want. 00275 """ 00276 import vtk.util.numpy_support 00277 n = getNode(pattern=pattern, index=index) 00278 if n.GetClassName() == 'vtkMRMLScalarVolumeNode': 00279 i = n.GetImageData() 00280 shape = list(n.GetImageData().GetDimensions()) 00281 shape.reverse() 00282 a = vtk.util.numpy_support.vtk_to_numpy(i.GetPointData().GetScalars()).reshape(shape) 00283 return a 00284 # TODO: accessors for other node types: tensors, polydata (verts, polys...), colors 00285 00286
1.7.4