From 3ca032d616596a3312d67ed9bf9b4035e9faf094 Mon Sep 17 00:00:00 2001 From: Christian Herz Date: Mon, 16 Oct 2017 12:07:49 -0400 Subject: [PATCH] ENH: generate button for every single test automatically --- QuantitativeReporting.py | 2 +- Testing/QuantitativeReportingTests.py | 96 ++++++++++++++++----------- 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/QuantitativeReporting.py b/QuantitativeReporting.py index 4ed5fc9..a88434a 100644 --- a/QuantitativeReporting.py +++ b/QuantitativeReporting.py @@ -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) diff --git a/Testing/QuantitativeReportingTests.py b/Testing/QuantitativeReportingTests.py index 9bc942c..385b022 100644 --- a/Testing/QuantitativeReportingTests.py +++ b/Testing/QuantitativeReportingTests.py @@ -4,6 +4,7 @@ import vtk import slicer import logging +import inspect from slicer.ScriptedLoadableModule import ScriptedLoadableModuleTest, ScriptedLoadableModuleWidget, \ ScriptedLoadableModuleLogic @@ -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): @@ -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" @@ -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 @@ -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!') @@ -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): @@ -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: