Skip to content

Commit

Permalink
BUG: fix failing tests (issue QIICR#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
che85 committed Feb 7, 2018
1 parent 2514801 commit e15c8f0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 119 deletions.
12 changes: 3 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,15 @@ find_package(SlicerDevelopmentToolbox REQUIRED)
find_package(DCMQI REQUIRED)
find_package(PETDICOMExtension REQUIRED)

set(DEPENDENCY_BUILD_DIRS
set(DEPENDENCIES_ADDITIONAL_MODULE_PATHS
${SlicerDevelopmentToolbox_DIR}/${Slicer_QTSCRIPTEDMODULES_LIB_DIR}
${SlicerDevelopmentToolbox_DIR}/${Slicer_QTLOADABLEMODULES_LIB_DIR}
${SlicerDevelopmentToolbox_DIR}/${Slicer_CLIMODULES_LIB_DIR}
${PETDICOMExtension_DIR}/${Slicer_QTLOADABLEMODULES_LIB_DIR}
${PETDICOMExtension_DIR}/${Slicer_QTSCRIPTEDMODULES_LIB_DIR}
${PETDICOMExtension_DIR}/${Slicer_CLIMODULES_LIB_DIR}
${DCMQI_DIR}/dcmqi-build/bin
)

message("DEPENDENCY_BUILD_DIRS: ${DEPENDENCY_BUILD_DIRS}")

set(DEPENDENCIES_ADDITIONAL_MODULE_PATHS
${DEPENDENCY_BUILD_DIRS}
${SlicerDevelopmentToolbox_DIR}/${Slicer_QTSCRIPTEDMODULES_LIB_DIR}
)

add_subdirectory(DICOMPlugins)
add_subdirectory(QuantitativeReporting)

Expand Down
6 changes: 2 additions & 4 deletions QuantitativeReporting/QuantitativeReporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ def loadSeriesByFileName(self, filename):
self.loadSeries(seriesUID)

def loadSeries(self, seriesUID):
dicomWidget = slicer.modules.dicom.widgetRepresentation().self()
dicomWidget.detailsPopup.offerLoadables(seriesUID, 'Series')
dicomWidget.detailsPopup.examineForLoading()
dicomWidget.detailsPopup.loadCheckedLoadables()
from DICOMLib.DICOMUtils import loadSeriesByUID
loadSeriesByUID([seriesUID])

def setupSelectionArea(self):
self.segmentEditorWidget.editor.masterVolumeNodeSelectorAddAttribute("vtkMRMLScalarVolumeNode",
Expand Down
216 changes: 110 additions & 106 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 inspect
from DICOMLib import DICOMUtils

import vtkSegmentationCorePython as vtkSegmentationCore

Expand Down Expand Up @@ -102,6 +103,8 @@ def layoutManager(self):

def setUp(self):
self.delayDisplay("Closing the scene")
self.dicomDatabaseDir = os.path.join(slicer.app.temporaryPath, 'QuantitativeReporting_SelfTest', 'CtkDicomDatabase')
self.layoutManager.selectModule("QuantitativeReporting")
slicer.mrmlScene.Clear(0)
self.setupTimer()

Expand All @@ -121,7 +124,6 @@ def loadTestVolume(self):
def runTest(self):
"""Run as few or as many tests as needed here.
"""

for testName in [f for f in QuantitativeReportingTest.__dict__.keys() if f.startswith('test_')]:
self.setUp()
getattr(self, testName)()
Expand All @@ -136,172 +138,174 @@ def loadTestData():
sampleData = TestDataLogic.downloadAndUnzipSampleData(self.collection)
TestDataLogic.importIntoDICOMDatabase(sampleData[imageType])

def checkFocusAndClickButton():
focus = slicer.app.focusWidget()
focus.parent().parent().yesButton.click()
with DICOMUtils.TemporaryDICOMDatabase(self.dicomDatabaseDir) as db:
self.assertTrue(db.isOpen)
self.assertEqual(slicer.dicomDatabase, db)

loadTestData()
loadTestData()

dicomWidget = slicer.modules.dicom.widgetRepresentation().self()
checkbox = dicomWidget.detailsPopup.pluginSelector.checkBoxByPlugin["DICOMLongitudinalTID1500Plugin"]
crntState = checkbox.checked
checkbox.checked = False
# dicomWidget = slicer.modules.dicom.widgetRepresentation().self()
# checkbox = dicomWidget.detailsPopup.pluginSelector.checkBoxByPlugin["DICOMLongitudinalTID1500Plugin"]
# crntState = checkbox.checked
# checkbox.checked = False

timer = qt.QTimer()
timer.setInterval(3000)
timer.setSingleShot(True)
timer.timeout.connect(checkFocusAndClickButton)
timer.start()
qrWidget = slicer.modules.QuantitativeReportingWidget

qrWidget = slicer.modules.QuantitativeReportingWidget
qrWidget.loadSeries(self.data['sr']['uid'])
settings = 'DICOM/automaticallyLoadReferences'
cacheAutoLoadReferences = qt.QSettings().value('DICOM/automaticallyLoadReferences')
qt.QSettings().setValue(settings, qt.QMessageBox.Yes)

qrWidget.loadSeries(self.data['sr']['uid'])

checkbox.checked = crntState
qt.QSettings().setValue(settings, cacheAutoLoadReferences)

tableNodes = slicer.util.getNodesByClass("vtkMRMLTableNode")
# checkbox.checked = crntState

self.assertTrue(len(tableNodes),
"Loading SR into mrmlScene failed. No vtkMRMLTableNodes were found within the scene.")
tableNodes = slicer.util.getNodesByClass("vtkMRMLTableNode")

self.delayDisplay('Selecting measurements report')
self.assertTrue(len(tableNodes),
"Loading SR into mrmlScene failed. No vtkMRMLTableNodes were found within the scene.")

qrWidget.measurementReportSelector.setCurrentNode(tableNodes[0])
self.delayDisplay('Selecting measurements report')

self.delayDisplay('Checking number of segments')
self.assertTrue(len(qrWidget.segmentEditorWidget.segments) == 3,
"Number of segments does not match expected count of 3")
qrWidget.measurementReportSelector.setCurrentNode(tableNodes[0])

self.delayDisplay('Checking referenced master volume', 2000)
self.assertIsNotNone(qrWidget.segmentEditorWidget.masterVolumeNode,
"Master volume for the selected measurement report is None!")
self.delayDisplay('Checking number of segments')
self.assertTrue(len(qrWidget.segmentEditorWidget.segments) == 3,
"Number of segments does not match expected count of 3")

self.delayDisplay('Test passed!')
self.delayDisplay('Checking referenced master volume', 2000)
self.assertIsNotNone(qrWidget.segmentEditorWidget.masterVolumeNode,
"Master volume for the selected measurement report is None!")

self.delayDisplay('Test passed!')

def test_create_report(self):

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

qrWidget = slicer.modules.QuantitativeReportingWidget

self.loadTestVolume()
success, err = qrWidget.saveReport()
self.assertFalse(success)
with DICOMUtils.TemporaryDICOMDatabase(self.dicomDatabaseDir) as db:
self.assertTrue(db.isOpen)
self.assertEqual(slicer.dicomDatabase, db)

self.delayDisplay('Add segments')
self.loadTestVolume()
success, err = qrWidget.saveReport()
self.assertFalse(success)

qrWidget = slicer.modules.QuantitativeReportingWidget
segmentation = qrWidget.segmentEditorWidget.segmentationNode.GetSegmentation()
self.delayDisplay('Add segments')

segmentGeometries = {
'Tumor': [[2, 30, 30, -127.7], [2, 40, 40, -127.7], [2, 50, 50, -127.7], [2, 40, 80, -127.7]],
'Air': [[2, 60, 100, -127.7], [2, 80, 30, -127.7]]
}
qrWidget = slicer.modules.QuantitativeReportingWidget
segmentation = qrWidget.segmentEditorWidget.segmentationNode.GetSegmentation()

for segmentName, segmentGeometry in segmentGeometries.iteritems():
appender = vtk.vtkAppendPolyData()
segmentGeometries = {
'Tumor': [[2, 30, 30, -127.7], [2, 40, 40, -127.7], [2, 50, 50, -127.7], [2, 40, 80, -127.7]],
'Air': [[2, 60, 100, -127.7], [2, 80, 30, -127.7]]
}

for sphere in segmentGeometry:
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(sphere[0])
sphereSource.SetCenter(sphere[1], sphere[2], sphere[3])
appender.AddInputConnection(sphereSource.GetOutputPort())
for segmentName, segmentGeometry in segmentGeometries.iteritems():
appender = vtk.vtkAppendPolyData()

segment = vtkSegmentationCore.vtkSegment()
segment.SetName(segmentation.GenerateUniqueSegmentID(segmentName))
for sphere in segmentGeometry:
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(sphere[0])
sphereSource.SetCenter(sphere[1], sphere[2], sphere[3])
appender.AddInputConnection(sphereSource.GetOutputPort())

appender.Update()
representationName = vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationClosedSurfaceRepresentationName()
segment.AddRepresentation(representationName, appender.GetOutput())
segmentation.AddSegment(segment)
segment = vtkSegmentationCore.vtkSegment()
segment.SetName(segmentation.GenerateUniqueSegmentID(segmentName))

self.delayDisplay('Save report')
appender.Update()
representationName = vtkSegmentationCore.vtkSegmentationConverter.GetSegmentationClosedSurfaceRepresentationName()
segment.AddRepresentation(representationName, appender.GetOutput())
segmentation.AddSegment(segment)

success, err = qrWidget.saveReport()
self.assertTrue(success)
self.delayDisplay('Save report')

self.delayDisplay('Test passed!')
success, err = qrWidget.saveReport()
self.assertTrue(success)

self.delayDisplay('Test passed!')

def test_import_labelmap(self):

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

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

sampleData = TestDataLogic.downloadAndUnzipSampleData(self.collection)
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)
with DICOMUtils.TemporaryDICOMDatabase(self.dicomDatabaseDir) as db:
self.assertTrue(db.isOpen)
self.assertEqual(slicer.dicomDatabase, db)

labelImportWidget = qrWidget.labelMapImportWidget
self.loadTestVolume()

timer = qt.QTimer()
timer.setInterval(2000)
timer.timeout.connect(self._checkFocusAndClickButton)
timer.start()
sampleData = TestDataLogic.downloadAndUnzipSampleData(self.collection)
segmentationsDir = sampleData['seg_nrrd']

for label in labels:
labelImportWidget.labelMapSelector.setCurrentNode(label)
labelImportWidget.importButton.click()
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)

timer.stop()
labelImportLogic = qrWidget.labelMapImportWidget.logic

segmentation = qrWidget.segmentEditorWidget.segmentationNode.GetSegmentation()
self.assertEquals(segmentation.GetNumberOfSegments(), len(labels))
for label in labels:
labelImportLogic.labelmap = label
labelImportLogic.run(resampleIfNecessary=True)

self.delayDisplay('Test passed!')
segmentation = qrWidget.segmentEditorWidget.segmentationNode.GetSegmentation()
self.assertEquals(segmentation.GetNumberOfSegments(), len(labels))

def _checkFocusAndClickButton(self):
focus = slicer.app.focusWidget()
if type(focus) is qt.QPushButton:
focus.click()
self.delayDisplay('Test passed!')

def test_import_segmentation(self):

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

uid = self.data["seg_dcm"]["uid"]

self.loadTestVolume()
with DICOMUtils.TemporaryDICOMDatabase(self.dicomDatabaseDir) as db:
self.assertTrue(db.isOpen)
self.assertEqual(slicer.dicomDatabase, db)

if not len(slicer.dicomDatabase.filesForSeries(uid)):
sampleData = TestDataLogic.downloadAndUnzipSampleData(self.collection)
TestDataLogic.importIntoDICOMDatabase(sampleData["seg_dcm"])
self.loadTestVolume()

def checkFocusAndClickButton():
focus = slicer.app.focusWidget()
focus.parent().parent().noButton.click()
if not len(slicer.dicomDatabase.filesForSeries(uid)):
sampleData = TestDataLogic.downloadAndUnzipSampleData(self.collection)
TestDataLogic.importIntoDICOMDatabase(sampleData["seg_dcm"])

timer = qt.QTimer()
timer.setInterval(3000)
timer.setSingleShot(True)
timer.timeout.connect(checkFocusAndClickButton)
timer.start()
def checkFocusAndClickButton():
focus = slicer.app.focusWidget()
focus.parent().parent().noButton.click()

qrWidget = slicer.modules.QuantitativeReportingWidget
qrWidget.loadSeries(uid)
timer = qt.QTimer()
timer.setInterval(3000)
timer.setSingleShot(True)
timer.timeout.connect(checkFocusAndClickButton)
timer.start()

qrWidget = slicer.modules.QuantitativeReportingWidget
qrWidget.loadSeries(uid)

segmentationNode = slicer.util.getNodesByClass('vtkMRMLSegmentationNode')[-1]
segmentationNode = slicer.util.getNodesByClass('vtkMRMLSegmentationNode')[-1]

qrWidget.importSegmentationCollapsibleButton.collapsed = False
qrWidget.importSegmentationCollapsibleButton.collapsed = False

importWidget = qrWidget.segmentImportWidget
importWidget.otherSegmentationNodeSelector.setCurrentNode(segmentationNode)
importWidget = qrWidget.segmentImportWidget
importWidget.otherSegmentationNodeSelector.setCurrentNode(segmentationNode)

segmentIDs = qrWidget.segmentEditorWidget.logic.getSegmentIDs(segmentationNode, False)
importWidget.otherSegmentsTableView.setSelectedSegmentIDs(segmentIDs)
segmentIDs = qrWidget.segmentEditorWidget.logic.getSegmentIDs(segmentationNode, False)
importWidget.otherSegmentsTableView.setSelectedSegmentIDs(segmentIDs)

importWidget.copyOtherToCurrentButton.click()
importWidget.copyOtherToCurrentButton.click()

self.delayDisplay('Checking number of imported segments')
self.assertTrue(len(qrWidget.segmentEditorWidget.segments) == 3,
"Number of segments does not match expected count of 3")
self.delayDisplay('Checking number of imported segments')
self.assertTrue(len(qrWidget.segmentEditorWidget.segments) == 3,
"Number of segments does not match expected count of 3")

self.delayDisplay('Test passed!')
self.delayDisplay('Test passed!')

def _selectModule(self):
self.layoutManager.selectModule("QuantitativeReporting")

0 comments on commit e15c8f0

Please sign in to comment.