<s> import os <EOL> import unittest <EOL> from __main__ import vtk , qt , ctk , slicer <EOL> class LungRegistration : <EOL> def __init__ ( self , parent ) : <EOL> parent . title = "<STR_LIT>" <EOL> parent . categories = [ "<STR_LIT>" ] <EOL> parent . dependencies = [ ] <EOL> parent . contributors = [ "<STR_LIT>" ] <EOL> parent . helpText = """<STR_LIT>""" <EOL> parent . acknowledgementText = """<STR_LIT>""" <EOL> self . parent = parent <EOL> try : <EOL> slicer . selfTests <EOL> except AttributeError : <EOL> slicer . selfTests = { } <EOL> slicer . selfTests [ '<STR_LIT>' ] = self . runTest <EOL> def runTest ( self ) : <EOL> tester = LungRegistrationTest ( ) <EOL> tester . runTest ( ) <EOL> class LungRegistrationWidget : <EOL> def __init__ ( self , parent = None ) : <EOL> if not parent : <EOL> self . parent = slicer . qMRMLWidget ( ) <EOL> self . parent . setLayout ( qt . QVBoxLayout ( ) ) <EOL> self . parent . setMRMLScene ( slicer . mrmlScene ) <EOL> else : <EOL> self . parent = parent <EOL> self . layout = self . parent . layout ( ) <EOL> if not parent : <EOL> self . setup ( ) <EOL> self . parent . show ( ) <EOL> def setup ( self ) : <EOL> reloadCollapsibleButton = ctk . ctkCollapsibleButton ( ) <EOL> reloadCollapsibleButton . text = "<STR_LIT>" <EOL> self . layout . addWidget ( reloadCollapsibleButton ) <EOL> reloadFormLayout = qt . QFormLayout ( reloadCollapsibleButton ) <EOL> self . reloadButton = qt . QPushButton ( "<STR_LIT>" ) <EOL> self . reloadButton . toolTip = "<STR_LIT>" <EOL> self . reloadButton . name = "<STR_LIT>" <EOL> reloadFormLayout . addWidget ( self . reloadButton ) <EOL> self . reloadButton . connect ( '<STR_LIT>' , self . onReload ) <EOL> self . reloadAndTestButton = qt . QPushButton ( "<STR_LIT>" ) <EOL> self . reloadAndTestButton . toolTip = "<STR_LIT>" <EOL> reloadFormLayout . addWidget ( self . reloadAndTestButton ) <EOL> self . reloadAndTestButton . connect ( '<STR_LIT>' , self . onReloadAndTest ) <EOL> parametersCollapsibleButton = ctk . ctkCollapsibleButton ( ) <EOL> parametersCollapsibleButton . text = "<STR_LIT>" <EOL> self . layout . addWidget ( parametersCollapsibleButton ) <EOL> parametersFormLayout = qt . QFormLayout ( parametersCollapsibleButton ) <EOL> self . inputVTKSelector = slicer . qMRMLNodeComboBox ( ) <EOL> self . inputVTKSelector . nodeTypes = ( ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . inputVTKSelector . selectNodeUponCreation = True <EOL> self . inputVTKSelector . addEnabled = False <EOL> self . inputVTKSelector . removeEnabled = False <EOL> self . inputVTKSelector . noneEnabled = False <EOL> self . inputVTKSelector . showHidden = False <EOL> self . inputVTKSelector . showChildNodeTypes = False <EOL> self . inputVTKSelector . setMRMLScene ( slicer . mrmlScene ) <EOL> self . inputVTKSelector . setToolTip ( "<STR_LIT>" ) <EOL> parametersFormLayout . addRow ( "<STR_LIT>" , self . inputVTKSelector ) <EOL> self . inputSelector = slicer . qMRMLNodeComboBox ( ) <EOL> self . inputSelector . nodeTypes = ( ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . inputSelector . selectNodeUponCreation = True <EOL> self . inputSelector . addEnabled = False <EOL> self . inputSelector . removeEnabled = False <EOL> self . inputSelector . noneEnabled = False <EOL> self . inputSelector . showHidden = False <EOL> self . inputSelector . showChildNodeTypes = False <EOL> self . inputSelector . setMRMLScene ( slicer . mrmlScene ) <EOL> self . inputSelector . setToolTip ( "<STR_LIT>" ) <EOL> parametersFormLayout . addRow ( "<STR_LIT>" , self . inputSelector ) <EOL> self . atlasSelector = slicer . qMRMLNodeComboBox ( ) <EOL> self . atlasSelector . nodeTypes = ( ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . atlasSelector . selectNodeUponCreation = True <EOL> self . atlasSelector . addEnabled = False <EOL> self . atlasSelector . removeEnabled = False <EOL> self . atlasSelector . noneEnabled = False <EOL> self . atlasSelector . showHidden = False <EOL> self . atlasSelector . showChildNodeTypes = False <EOL> self . atlasSelector . setMRMLScene ( slicer . mrmlScene ) <EOL> self . atlasSelector . setToolTip ( "<STR_LIT>" ) <EOL> parametersFormLayout . addRow ( "<STR_LIT>" , self . atlasSelector ) <EOL> self . outputSelector = slicer . qMRMLNodeComboBox ( ) <EOL> self . outputSelector . nodeTypes = ( ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . outputSelector . addAttribute ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . outputSelector . selectNodeUponCreation = False <EOL> self . outputSelector . addEnabled = True <EOL> self . outputSelector . removeEnabled = True <EOL> self . outputSelector . noneEnabled = False <EOL> self . outputSelector . showHidden = False <EOL> self . outputSelector . showChildNodeTypes = False <EOL> self . outputSelector . setMRMLScene ( slicer . mrmlScene ) <EOL> self . outputSelector . setToolTip ( "<STR_LIT>" ) <EOL> parametersFormLayout . addRow ( "<STR_LIT>" , self . outputSelector ) <EOL> self . numberOfIterations = qt . QSpinBox ( ) <EOL> self . numberOfIterations . setRange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> self . numberOfIterations . setValue ( <NUM_LIT:200> ) <EOL> self . numberOfIterations . setToolTip ( "<STR_LIT>" ) <EOL> parametersFormLayout . addRow ( "<STR_LIT>" , self . numberOfIterations ) <EOL> self . boneThreshold = qt . QSpinBox ( ) <EOL> self . boneThreshold . setRange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> self . boneThreshold . setValue ( <NUM_LIT> ) <EOL> self . boneThreshold . setToolTip ( "<STR_LIT>" ) <EOL> parametersFormLayout . addRow ( "<STR_LIT>" , self . boneThreshold ) <EOL> self . applyButton = qt . QPushButton ( "<STR_LIT>" ) <EOL> self . applyButton . toolTip = "<STR_LIT>" <EOL> self . applyButton . enabled = False <EOL> parametersFormLayout . addRow ( self . applyButton ) <EOL> self . applyButton . connect ( '<STR_LIT>' , self . onApplyButton ) <EOL> self . inputSelector . connect ( "<STR_LIT>" , self . onSelect ) <EOL> self . outputSelector . connect ( "<STR_LIT>" , self . onSelect ) <EOL> self . inputVTKSelector . connect ( "<STR_LIT>" , self . onSelect ) <EOL> self . atlasSelector . connect ( "<STR_LIT>" , self . onSelect ) <EOL> self . boneThreshold . connect ( "<STR_LIT>" , self . onSelect ) <EOL> self . layout . addStretch ( <NUM_LIT:1> ) <EOL> def cleanup ( self ) : <EOL> pass <EOL> def onSelect ( self ) : <EOL> self . applyButton . enabled = self . outputSelector . currentNode ( ) <EOL> def onApplyButton ( self ) : <EOL> logic = LungRegistrationLogic ( ) <EOL> print ( "<STR_LIT>" ) <EOL> logic . run ( self . inputSelector . currentNode ( ) , self . atlasSelector . currentNode ( ) , self . inputVTKSelector . currentNode ( ) , self . numberOfIterations , self . boneThreshold , self . outputSelector . currentNode ( ) ) <EOL> def onReload ( self , moduleName = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> import imp , sys , os , slicer <EOL> widgetName = moduleName + "<STR_LIT>" <EOL> filePath = eval ( '<STR_LIT>' % moduleName . lower ( ) ) <EOL> p = os . path . dirname ( filePath ) <EOL> if not sys . path . __contains__ ( p ) : <EOL> sys . path . insert ( <NUM_LIT:0> , p ) <EOL> fp = open ( filePath , "<STR_LIT:r>" ) <EOL> globals ( ) [ moduleName ] = imp . load_module ( <EOL> moduleName , fp , filePath , ( '<STR_LIT>' , '<STR_LIT:r>' , imp . PY_SOURCE ) ) <EOL> fp . close ( ) <EOL> parent = slicer . util . findChildren ( name = '<STR_LIT>' % moduleName ) [ <NUM_LIT:0> ] . parent ( ) . parent ( ) <EOL> for child in parent . children ( ) : <EOL> try : <EOL> child . hide ( ) <EOL> except AttributeError : <EOL> pass <EOL> item = parent . layout ( ) . itemAt ( <NUM_LIT:0> ) <EOL> while item : <EOL> parent . layout ( ) . removeItem ( item ) <EOL> item = parent . layout ( ) . itemAt ( <NUM_LIT:0> ) <EOL> if hasattr ( globals ( ) [ '<STR_LIT>' ] . modules , widgetName ) : <EOL> getattr ( globals ( ) [ '<STR_LIT>' ] . modules , widgetName ) . cleanup ( ) <EOL> globals ( ) [ widgetName . lower ( ) ] = eval ( <EOL> '<STR_LIT>' % ( moduleName , widgetName ) ) <EOL> globals ( ) [ widgetName . lower ( ) ] . setup ( ) <EOL> setattr ( globals ( ) [ '<STR_LIT>' ] . modules , widgetName , globals ( ) [ widgetName . lower ( ) ] ) <EOL> def onReloadAndTest ( self , moduleName = "<STR_LIT>" ) : <EOL> try : <EOL> self . onReload ( ) <EOL> evalString = '<STR_LIT>' % ( moduleName , moduleName ) <EOL> tester = eval ( evalString ) <EOL> tester . runTest ( ) <EOL> except Exception , e : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> qt . QMessageBox . warning ( slicer . util . mainWindow ( ) , <EOL> "<STR_LIT>" , '<STR_LIT>' + str ( e ) + "<STR_LIT>" ) <EOL> class LungRegistrationLogic : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pass <EOL> def hasImageData ( self , volumeNode ) : <EOL> """<STR_LIT>""" <EOL> if not volumeNode : <EOL> print ( '<STR_LIT>' ) <EOL> return False <EOL> if volumeNode . GetImageData ( ) == None : <EOL> print ( '<STR_LIT>' ) <EOL> return False <EOL> return True <EOL> def run ( self , inputVolume , atlasVolume , convexHullVolume , numIterations , boneThreshold , outVolume ) : <EOL> """<STR_LIT>""" <EOL> print ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> f = qt . QTemporaryFile ( slicer . app . temporaryPath + "<STR_LIT>" ) <EOL> f . open ( ) <EOL> modelNode = slicer . mrmlScene . GetNodeByID ( convexHullVolume . GetID ( ) ) <EOL> polyData = modelNode . GetPolyData ( ) <EOL> cliparameters = { } <EOL> cliparameters [ '<STR_LIT>' ] = convexHullVolume . GetID ( ) <EOL> cliparameters [ '<STR_LIT>' ] = numIterations . value <EOL> cliparameters [ '<STR_LIT>' ] = boneThreshold . value <EOL> cliparameters [ '<STR_LIT>' ] = f . fileName ( ) <EOL> cliparameters [ '<STR_LIT>' ] = inputVolume . GetID ( ) <EOL> RegisterLungAtlas = slicer . modules . registerlungatlas <EOL> cliNode = slicer . cli . run ( RegisterLungAtlas , None , cliparameters , wait_for_completion = True ) <EOL> cliparameters = { } <EOL> cliparameters [ '<STR_LIT>' ] = atlasVolume . GetID ( ) <EOL> cliparameters [ '<STR_LIT>' ] = f . fileName ( ) <EOL> cliparameters [ '<STR_LIT>' ] = outVolume . GetID ( ) <EOL> cliparameters [ '<STR_LIT>' ] = inputVolume . GetID ( ) <EOL> cliparameters [ '<STR_LIT>' ] = True <EOL> ResampleLabelMap = slicer . modules . resamplelabelmap <EOL> cliNode = slicer . cli . run ( ResampleLabelMap , None , cliparameters , wait_for_completion = True ) , <EOL> return True <EOL> class LungRegistrationTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def delayDisplay ( self , message , msec = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> print ( message ) <EOL> self . info = qt . QDialog ( ) <EOL> self . infoLayout = qt . QVBoxLayout ( ) <EOL> self . info . setLayout ( self . infoLayout ) <EOL> self . label = qt . QLabel ( message , self . info ) <EOL> self . infoLayout . addWidget ( self . label ) <EOL> qt . QTimer . singleShot ( msec , self . info . close ) <EOL> self . info . exec_ ( ) <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> slicer . mrmlScene . Clear ( <NUM_LIT:0> ) <EOL> def runTest ( self ) : <EOL> """<STR_LIT>""" <EOL> self . setUp ( ) <EOL> self . test_LungRegistration1 ( ) <EOL> def test_LungRegistration1 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . delayDisplay ( "<STR_LIT>" ) <EOL> import urllib <EOL> downloads = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , slicer . util . loadVolume ) , <EOL> ) <EOL> for url , name , loader in downloads : <EOL> filePath = slicer . app . temporaryPath + '<STR_LIT:/>' + name <EOL> if not os . path . exists ( filePath ) or os . stat ( filePath ) . st_size == <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' % ( name , url ) ) <EOL> urllib . urlretrieve ( url , filePath ) <EOL> if loader : <EOL> print ( '<STR_LIT>' % ( name , ) ) <EOL> loader ( filePath ) <EOL> self . delayDisplay ( '<STR_LIT>' ) <EOL> volumeNode = slicer . util . getNode ( pattern = "<STR_LIT>" ) <EOL> logic = LungRegistrationLogic ( ) <EOL> self . assertTrue ( logic . hasImageData ( volumeNode ) ) <EOL> self . delayDisplay ( '<STR_LIT>' ) </s>
