Ejemplo n.º 1
0
class DataReaderTest(unittest.TestCase):
    def setUp(self):
        self.reader = DataReader()

    def tearDown(self):
        del self.reader

    def testDataReader(self):
        path = os.path.dirname(os.path.abspath(__file__))

        fileName = path + "/data/hi-3.mhd"
        imageData = self.reader.GetImageData(fileName)
        self.assertIsNotNone(imageData)
        dimensions = imageData.GetDimensions()
        self.assertEquals(dimensions, (21, 15, 9))

    def testUnsupportedDataTypes(self):
        self.assertRaises(Exception, self.reader.GetImageData, "data/hi-3.mrb")

    def testSupportedDataTypes(self):
        self.assertTrue(self.reader.IsExtensionSupported("mhd"))
        self.assertTrue(self.reader.IsExtensionSupported("vti"))
        self.assertTrue(self.reader.IsExtensionSupported("dcm"))

        self.assertFalse(self.reader.IsExtensionSupported("mrb"))
        self.assertFalse(self.reader.IsExtensionSupported("vtk"))
        self.assertFalse(self.reader.IsExtensionSupported("raw"))
        self.assertFalse(self.reader.IsExtensionSupported("dat"))

    # def testDatFileFormat(self):
    # 	path = os.path.dirname(os.path.abspath(__file__))
    # 	fileName = path + "/data/present492x492x442.dat"

    # 	imageData = self.reader.GetImageData(fileName)
    # 	dimensions = imageData.GetDimensions()
    # 	self.assertEquals(dimensions, (492, 492, 442))

    def testVTIFileFormat(self):
        path = os.path.dirname(os.path.abspath(__file__))
        fileName = path + "/data/modelSegmentation.vti"
        imageData = self.reader.GetImageData(fileName)
        dimensions = imageData.GetDimensions()
        self.assertEquals(dimensions, (376, 245, 206))

    def testEmptyDirectory(self):
        path = os.path.dirname(os.path.abspath(__file__))
        fileName = path + "/data"
        imageData = self.reader.GetImageData(fileName)
        self.assertIsNone(imageData)

    def testDICOMFileFormat(self):
        path = os.path.dirname(os.path.abspath(__file__))
        fileName = path + "/data/DICOM"
        imageData = self.reader.GetImageData(fileName)
        self.assertIsNotNone(imageData)
        dimensions = imageData.GetDimensions()
        self.assertEquals(dimensions, (320, 384, 11))
Ejemplo n.º 2
0
    def setFile(self, fileName):
        """
		:type fileName: str
		"""
        if fileName is None:
            self.imageData = None
            self.visualization = None
            self.renderWidget.setData(self.imageData)
            self.renderWidget.setVolumeVisualization(self.visualization)
            self.dataChanged.emit(self.imageData)
            self.visualizationChanged.emit(self.visualization)
            return

        # Read image data
        dataReader = DataReader()
        imageData = dataReader.GetImageData(fileName)
        resizer = DataResizer()
        self.imageData = resizer.ResizeData(imageData, maximum=25000000)

        # Give the image data to the widget
        self.renderWidget.setData(self.imageData)
        self.dataChanged.emit(self.imageData)

        # Set the render type
        self.setVisualizationType(self.visualizationType)
Ejemplo n.º 3
0
    def setFixedFile(self, fileName):
        """
		:type fileName: str
		"""
        if fileName is None:
            self.fixedImageData = None
            self.fixedVisualization = None
            self.visualization = None
            self.visualizations = dict()
            self.visualizationType = None
            self.multiRenderWidget.setFixedData(self.fixedImageData)
            self.multiRenderWidget.setVolumeVisualization(
                self.fixedVisualization)
            self.fixedDataChanged.emit(self.fixedImageData)
            self.visualizationChanged.emit(self.visualization)
            return

        # Read image data
        dataReader = DataReader()
        imageData = dataReader.GetImageData(fileName)
        # TODO: there should be a setting for this, either in project, per loaded
        # data file or a general setting
        resizer = DataResizer()
        self.fixedImageData = resizer.ResizeData(imageData, maximum=25000000)

        # Give the image data to the widget
        self.multiRenderWidget.setFixedData(self.fixedImageData)
        self.fixedDataChanged.emit(self.fixedImageData)

        # Set the visualization type
        self.setVisualizationType(self.visualizationType)
    def exportDataAs(self):
        """
		Opens a file dialog so that the user can provide a filename
		for saving the transformed dataset to.
		"""
        fileType = FileTypeDialog.getFileType(self,
                                              "Choose file type for export")
        if len(fileType) == 0:
            return

        extension = "(*." + fileType + ")"
        fileName, other = QFileDialog.getSaveFileName(
            self, "Save registration result to...", "", extension)
        if len(fileName) == 0:
            return

        self.showProgressBar("Exporting data...")

        transform = self.multiDataWidget.transformations.completeTransform()
        dataReader = DataReader()
        imageData = dataReader.GetImageData(
            ProjectController.Instance().currentProject.movingData)
        transformer = DataTransformer()
        outputData = transformer.TransformImageData(imageData, transform)
        writer = DataWriter()
        writer.WriteToFile(outputData, fileName, fileType)

        self.hideProgressBar()
Ejemplo n.º 5
0
    def imageData(self):
        if not self.filename:
            return None

        if not self.__imageData:
            dataReader = DataReader()
            self.__imageData = dataReader.GetImageData(self._filename)

        return self.__imageData
Ejemplo n.º 6
0
    def transformation(self):
        transformation = ParameterList()
        transformation.append(Parameter("Transform", "AffineTransform"))

        transformMatrix = self.transform.GetMatrix()
        matrix = vtkMatrix4x4()
        matrix.DeepCopy(transformMatrix)
        matrix.Invert()
        if isIdentity(matrix):
            return None

        elemList = listFromMatrix(matrix)
        imageReader = DataReader()
        imageData = imageReader.GetImageData(self.dataset)
        if not imageData:
            raise Exception("Could not create image data")

        scalarType = imageData.GetScalarTypeAsString()
        dimensions = list(imageData.GetDimensions())
        bounds = list(imageData.GetBounds())
        spacing = list(imageData.GetSpacing())

        if not scalarType or not bounds or not spacing:
            raise Exception("Could not get the needed parameters")

        center = [(bounds[0] + bounds[1]) / 2.0, (bounds[2] + bounds[3]) / 2.0,
                  (bounds[4] + bounds[5]) / 2.0]

        transformation.append(Parameter("NumberOfParameters", len(elemList)))
        transformation.append(Parameter("TransformParameters", elemList))
        transformation.append(
            Parameter("InitialTransformParametersFileName",
                      "NoInitialTransform"))
        transformation.append(Parameter("HowToCombineTransforms", "Compose"))
        transformation.append(Parameter("MovingImageDimension", 3))
        transformation.append(Parameter("FixedImageDimension", 3))

        # The following options are from the dataset
        transformation.append(
            Parameter("FixedInternalImagePixelType", scalarType))
        transformation.append(
            Parameter("MovingInternalImagePixelType", scalarType))
        transformation.append(Parameter("Size", dimensions))
        transformation.append(Parameter("Spacing", spacing))
        transformation.append(Parameter("CenterOfRotation", center))
        return transformation
    def testDataWriter(self):
        path = os.path.dirname(os.path.abspath(__file__))
        fileName = path + "/data/hi-3.mhd"
        outputFolder = path + "/data/DataWriter"
        exportFileName = outputFolder + "/output.mhd"
        dataReader = DataReader()
        imageData = dataReader.GetImageData(fileName)
        fileType = DataReader.TypeMHD

        self.writer.WriteToFile(imageData, exportFileName, fileType)

        # Cleanup test directory
        try:
            if os.path.exists(outputFolder):
                import shutil
                shutil.rmtree(outputFolder)
        except Exception, e:
            raise e
Ejemplo n.º 8
0
    def setMovingFile(self, fileName):
        if fileName is None:
            self.movingImageData = None
            self.movingVisualization = None
            self.multiRenderWidget.setMovingData(self.movingImageData)
            self.multiRenderWidget.setVolumeVisualization(self.visualization)
            self.movingDataChanged.emit(self.movingImageData)
            self.visualizationChanged.emit(self.movingVisualization)
            return

        # Read image data
        dataReader = DataReader()
        # self.movingImageData = dataReader.GetImageData(fileName)
        imageData = dataReader.GetImageData(fileName)
        resizer = DataResizer()
        self.movingImageData = resizer.ResizeData(imageData, maximum=25000000)

        # Give the image data to the widget
        self.multiRenderWidget.setMovingData(self.movingImageData)
        self.movingDataChanged.emit(self.movingImageData)

        # Set the visualization type
        self.setVisualizationType(self.visualizationType)
	def setFile(self, fileName):
		"""
		Slot that reads properties of the dataset and displays them in a few widgets.
		"""
		if fileName is None:
			return

		self.fileName = fileName

		# Read info from dataset
		# TODO: read out the real world dimensions in inch or cm
		# TODO: scalar type (int, float, short, etc.)
		imageReader = DataReader()
		imageData = imageReader.GetImageData(fileName)

		directory, name = os.path.split(fileName)
		dimensions = imageData.GetDimensions()
		minimum, maximum = imageData.GetScalarRange()
		scalarType = imageData.GetScalarTypeAsString()

		bins = DataAnalyzer.histogramForData(imageData, 256)

		self.histogram = Histogram()
		self.histogram.bins = bins
		self.histogram.enabled = True

		self.histogramWidget = HistogramWidget()
		self.histogramWidget.setMinimumHeight(100)
		self.histogramWidget.setHistogram(self.histogram)
		self.histogramWidget.setAxeMode(bottom=HistogramWidget.AxeClear,
			left=HistogramWidget.AxeLog)
		Style.styleWidgetForTab(self.histogramWidget)

		nameText = name
		dimsText = "(" + str(dimensions[0]) + ", " + str(dimensions[1]) + ", " + str(dimensions[2]) + ")"
		voxsText = str(dimensions[0] * dimensions[1] * dimensions[2])
		rangText = "[" + str(minimum) + " : " + str(maximum) + "]"
		typeText = scalarType

		layout = self.layout()
		if not layout:
			# Create a new layout
			layout = QGridLayout()
			layout.setAlignment(Qt.AlignTop)

			# Create string representations
			nameLabels = []
			nameLabels.append(QLabel("File name:"))
			nameLabels.append(QLabel("Dimensions:"))
			nameLabels.append(QLabel("Voxels:"))
			nameLabels.append(QLabel("Range:"))
			nameLabels.append(QLabel("Data type:"))

			for label in nameLabels:
				label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)

			# Create 'dynamic' labels
			self.labelTitle = QLabel(nameText)
			self.labelDimensions = QLabel(dimsText)
			self.labelVoxels = QLabel(voxsText)
			self.labelRange = QLabel(rangText)
			self.labelType = QLabel(typeText)

			index = 0
			for label in nameLabels:
				layout.addWidget(label, index, 0)
				index += 1

			layout.addWidget(self.labelTitle, 0, 1)
			layout.addWidget(self.labelDimensions, 1, 1)
			layout.addWidget(self.labelVoxels, 2, 1)
			layout.addWidget(self.labelRange, 3, 1)
			layout.addWidget(self.labelType, 4, 1)
			layout.addWidget(self.histogramWidget, 5, 0, 1, 2)

			widget = QWidget()
			widget.setLayout(layout)
			Style.styleWidgetForTab(widget)
			self.scrollArea.setWidget(widget)

			scrollLayout = QGridLayout()
			scrollLayout.setSpacing(0)
			scrollLayout.setContentsMargins(0, 0, 0, 0)
			scrollLayout.addWidget(self.scrollArea)
			self.setLayout(scrollLayout)
		else:
			# Just update the text for the 'dynamic' labels
			self.labelTitle.setText(nameText)
			self.labelDimensions.setText(dimsText)
			self.labelVoxels.setText(voxsText)
			self.labelRange.setText(rangText)
			self.labelType.setText(typeText)
	def setFile(self, fileName):
		"""
		Slot that reads properties of the dataset and displays them in a few widgets.
		"""
		if fileName is None:
			return

		# Read info from dataset
		# TODO: read out the real world dimensions in inch or cm
		# TODO: scalar type (int, float, short, etc.)
		imageReader = DataReader()
		imageData = imageReader.GetImageData(fileName)

		directory, name = os.path.split(fileName)
		dimensions = imageData.GetDimensions()
		minimum, maximum = imageData.GetScalarRange()

		bins = DataAnalyzer.histogramForData(imageData, 256)

		self.histogram = Histogram()
		self.histogram.bins = bins
		self.histogram.enabled = True

		self.histogramWidget = HistogramWidget()
		self.histogramWidget.setHistogram(self.histogram)
		self.histogramWidget.setAxeMode(bottom=HistogramWidget.AxeClear,
			left=HistogramWidget.AxeLog)
		Style.styleWidgetForTab(self.histogramWidget)

		nameText = name
		dimsText = "(" + str(dimensions[0]) + ", " + str(dimensions[1]) + ", " + str(dimensions[2]) + ")"
		voxsText = str(dimensions[0] * dimensions[1] * dimensions[2])
		rangText = "[" + str(minimum) + " : " + str(maximum) + "]"

		layout = self.layout()
		if not layout:
			# Create a new layout
			layout = QGridLayout()
			layout.setAlignment(Qt.AlignTop)

			# Create string representations
			nameField = QLabel("File name:")
			dimsField = QLabel("Dimensions:")
			voxsField = QLabel("Voxels:")
			rangField = QLabel("Range:")

			nameField.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
			dimsField.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
			voxsField.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
			rangField.setAlignment(Qt.AlignRight | Qt.AlignVCenter)

			# Create 'dynamic' labels
			self.labelTitle = QLabel(nameText)
			self.labelDimensions = QLabel(dimsText)
			self.labelVoxels = QLabel(voxsText)
			self.labelRange = QLabel(rangText)

			layout.addWidget(nameField, 0, 0)
			layout.addWidget(dimsField, 1, 0)
			layout.addWidget(voxsField, 2, 0)
			layout.addWidget(rangField, 3, 0)

			layout.addWidget(self.labelTitle, 0, 1)
			layout.addWidget(self.labelDimensions, 1, 1)
			layout.addWidget(self.labelVoxels, 2, 1)
			layout.addWidget(self.labelRange, 3, 1)
			layout.addWidget(self.histogramWidget, 4, 0, 1, 2)
			self.setLayout(layout)
		else:
			# Just update the text for the 'dynamic' labels
			self.labelTitle.setText(nameText)
			self.labelDimensions.setText(dimsText)
			self.labelVoxels.setText(voxsText)
			self.labelRange.setText(rangText)