Skip to content

Commit

Permalink
ENH: generate button for every single test automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
che85 committed Oct 16, 2017
1 parent 6ffabe1 commit 3ca032d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 41 deletions.
2 changes: 1 addition & 1 deletion QuantitativeReporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def setupButtonConnections():
getattr(self.completeReportButton.clicked, funcName)(self.onCompleteReportButtonClicked)
getattr(self.calculateMeasurementsButton.clicked, funcName)(lambda: self.updateMeasurementsTable(triggered=True))
getattr(self.segmentStatisticsConfigButton.clicked, funcName)(self.onEditParameters)
getattr(self.retrieveTestDataButton.clicked, funcName)(self.loadTestData)
getattr(self.retrieveTestDataButton.clicked, funcName)(lambda clicked: self.loadTestData())

def setupOtherConnections():
getattr(self.layoutManager.layoutChanged, funcName)(self.onLayoutChanged)
Expand Down
96 changes: 56 additions & 40 deletions Testing/QuantitativeReportingTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import vtk
import slicer
import logging
import inspect

from slicer.ScriptedLoadableModule import ScriptedLoadableModuleTest, ScriptedLoadableModuleWidget, \
ScriptedLoadableModuleLogic
Expand Down Expand Up @@ -47,43 +48,28 @@ def __init__(self, parent=None):
def setup(self):
ScriptedLoadableModuleWidget.setup(self)

testsCollapsibleButton = ctk.ctkCollapsibleButton()
testsCollapsibleButton.setLayout(qt.QFormLayout())
testsCollapsibleButton.text = "Quantitative Reporting Tests"
self.layout.addWidget(testsCollapsibleButton)
self.testsCollapsibleButton = ctk.ctkCollapsibleButton()
self.testsCollapsibleButton.setLayout(qt.QFormLayout())
self.testsCollapsibleButton.text = "Quantitative Reporting Tests"
self.layout.addWidget(self.testsCollapsibleButton)
self.generateButtons()

self.runReportCreationTest = qt.QPushButton("Run Report Creation Test")
self.runReportCreationTest.toolTip = "Creation of a DICOM TID1500 structured report."
testsCollapsibleButton.layout().addWidget(self.runReportCreationTest)
def generateButtons(self):

self.runReportReadingTest = qt.QPushButton("Run Report Read Test")
self.runReportReadingTest.toolTip = "Reading of a DICOM TID1500 structured report."
testsCollapsibleButton.layout().addWidget(self.runReportReadingTest)
def onButtonPressed(button):
print "pressed button %s" % button.name
tester = QuantitativeReportingTest()
tester.setUp()
getattr(tester, button.name)()

self.setupConnections()
buttons = []
for testName in [f for f in QuantitativeReportingTest.__dict__.keys() if f.startswith('test_')]:
b = qt.QPushButton(testName)
b.name = testName
self.testsCollapsibleButton.layout().addWidget(b)
buttons.append(b)

def setupConnections(self):
self.runReportCreationTest.connect('clicked(bool)', self.onReportCreationButtonClicked)
self.runReportReadingTest.connect('clicked(bool)', self.onReportReadingButtonClicked)

self.layout.addStretch(1)

def onReportCreationButtonClicked(self):
tester = QuantitativeReportingTest()
tester.setUp()
tester.test_create_report()

def onReportReadingButtonClicked(self):
tester = QuantitativeReportingTest()
tester.setUp()
tester.test_read_report()


# def onReloadAndTest(self,moduleName="AtlasTests"):
# self.onReload()
# evalString = 'globals()["%s"].%sTest()' % (moduleName, moduleName)
# tester = eval(evalString)
# tester.runTest()
map(lambda b: b.clicked.connect(lambda clicked: onButtonPressed(b)), buttons)


class QuantitativeReportingTest(ScriptedLoadableModuleTest):
Expand All @@ -102,6 +88,8 @@ def setUp(self):
slicer.mrmlScene.Clear(0)

def loadTestData(self):
self.delayDisplay("Loading testdata")

liverCTSeriesUID = "1.2.392.200103.20080913.113635.1.2009.6.22.21.43.10.23430.1"
collection = "CTLiver"

Expand All @@ -120,8 +108,7 @@ def runTest(self):

def test_create_report(self):

self.delayDisplay("Starting test for creating a report")

self.delayDisplay('Starting %s' % inspect.stack()[0][3])

self.layoutManager.selectModule("QuantitativeReporting")
qrWidget = slicer.modules.QuantitativeReportingWidget
Expand Down Expand Up @@ -167,12 +154,41 @@ def test_create_report(self):
self.delayDisplay('Test passed!')

def test_import_labelmap(self):

self.delayDisplay('Starting %s' % inspect.stack()[0][3])

self.layoutManager.selectModule("QuantitativeReporting")

qrWidget = slicer.modules.QuantitativeReportingWidget
self.loadTestData()

sampleData = TestDataLogic.downloadAndUnzipSampleData("CTLiver")
segmentationsDir = sampleData['seg_nrrd']

labels = []
for f in [os.path.join(segmentationsDir, f) for f in os.listdir(segmentationsDir) if f.endswith(".nrrd")]:
_, label = slicer.util.loadVolume(f, {'labelmap': True}, returnNode=True)
if label:
labels.append(label)

labelImportWidget = qrWidget.labelMapImportWidget
for label in labels:
labelImportWidget.labelMapSelector.setCurrentNode(label)
labelImportWidget.importButton.click()

segmentation = qrWidget.segmentEditorWidget.segmentationNode.GetSegmentation()
self.assertEquals(segmentation.GetNumberOfSegments(), len(labels))

self.delayDisplay('Test passed!')

def test_import_segmentation(self):
self.delayDisplay('Starting %s' % inspect.stack()[0][3])

self.delayDisplay('Test passed!')

def test_read_report(self):
self.delayDisplay('Starting %s' % inspect.stack()[0][3])

self.delayDisplay('Test passed!')


Expand Down Expand Up @@ -202,12 +218,12 @@ def importIntoDICOMDatabase(dicomFilesDirectory):

@staticmethod
def unzipSampleData(filePath, collection, kind):
dicomFilesDirectory = TestDataLogic.getUnzippedDirectoryPath(collection, kind)
qt.QDir().mkpath(dicomFilesDirectory)
success = slicer.app.applicationLogic().Unzip(filePath, dicomFilesDirectory)
destinationDirectory = TestDataLogic.getUnzippedDirectoryPath(collection, kind)
qt.QDir().mkpath(destinationDirectory)
success = slicer.app.applicationLogic().Unzip(filePath, destinationDirectory)
if not success:
logging.error("Archive %s was NOT unzipped successfully." % filePath)
return dicomFilesDirectory
return destinationDirectory

@staticmethod
def getUnzippedDirectoryPath(collection, kind):
Expand All @@ -228,7 +244,7 @@ def downloadAndUnzipSampleData(collection='MRHead'):
if not os.path.exists(filePath) or os.stat(filePath).st_size == 0:
slicer.util.delayDisplay('Requesting download %s from %s...\n' % (filename, url), 1000)
urllib.urlretrieve(url, filePath)
if not os.path.exists(expectedOutput):
if not os.path.exists(expectedOutput) or not len(os.listdir(expectedOutput)):
logging.debug('Unzipping data into %s' % expectedOutput)
downloaded[kind] = TestDataLogic.unzipSampleData(filePath, collection, kind)
else:
Expand Down

0 comments on commit 3ca032d

Please sign in to comment.