Beispiel #1
0
class TestScalarFieldView(TestCaseQt, ParametricTestCase):
    """Tests of ScalarFieldView widget."""

    def setUp(self):
        super(TestScalarFieldView, self).setUp()
        self.widget = ScalarFieldView()
        self.widget.show()

        # Commented as it slows down the tests
        # self.qWaitForWindowExposed(self.widget)

    def tearDown(self):
        self.qapp.processEvents()
        self.widget.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.widget.close()
        del self.widget
        super(TestScalarFieldView, self).tearDown()

    @staticmethod
    def _buildData(size):
        """Make a 3D dataset"""
        coords = numpy.linspace(-10, 10, size)
        z = coords.reshape(-1, 1, 1)
        y = coords.reshape(1, -1, 1)
        x = coords.reshape(1, 1, -1)
        return numpy.sin(x * y * z) / (x * y * z)

    def testSimple(self):
        """Set the data and an isosurface"""
        data = self._buildData(size=32)

        self.widget.setData(data)
        self.widget.addIsosurface(0.5, (1., 0., 0., 0.5))
        self.widget.addIsosurface(0.7, qt.QColor('green'))
        self.qapp.processEvents()

    def testNotFinite(self):
        """Test with NaN and inf in data set"""

        # Some NaNs and inf
        data = self._buildData(size=32)
        data[8, :, :] = numpy.nan
        data[16, :, :] = numpy.inf
        data[24, :, :] = - numpy.inf

        self.widget.addIsosurface(0.5, 'red')
        self.widget.setData(data, copy=True)
        self.qapp.processEvents()
        self.widget.setData(None)

        # All NaNs or inf
        data = numpy.empty((4, 4, 4), dtype=numpy.float32)
        for value in (numpy.nan, numpy.inf):
            with self.subTest(value=str(value)):
                data[:] = value
                self.widget.setData(data, copy=True)
                self.qapp.processEvents()
class TestScalarFieldView(TestCaseQt, ParametricTestCase):
    """Tests of ScalarFieldView widget."""
    def setUp(self):
        super(TestScalarFieldView, self).setUp()
        self.widget = ScalarFieldView()
        self.widget.show()

        # Commented as it slows down the tests
        # self.qWaitForWindowExposed(self.widget)

    def tearDown(self):
        self.qapp.processEvents()
        self.widget.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.widget.close()
        del self.widget
        super(TestScalarFieldView, self).tearDown()

    @staticmethod
    def _buildData(size):
        """Make a 3D dataset"""
        coords = numpy.linspace(-10, 10, size)
        z = coords.reshape(-1, 1, 1)
        y = coords.reshape(1, -1, 1)
        x = coords.reshape(1, 1, -1)
        return numpy.sin(x * y * z) / (x * y * z)

    def testSimple(self):
        """Set the data and an isosurface"""
        data = self._buildData(size=32)

        self.widget.setData(data)
        self.widget.addIsosurface(0.5, (1., 0., 0., 0.5))
        self.widget.addIsosurface(0.7, qt.QColor('green'))
        self.qapp.processEvents()

    def testNotFinite(self):
        """Test with NaN and inf in data set"""

        # Some NaNs and inf
        data = self._buildData(size=32)
        data[8, :, :] = numpy.nan
        data[16, :, :] = numpy.inf
        data[24, :, :] = -numpy.inf

        self.widget.addIsosurface(0.5, 'red')
        self.widget.setData(data, copy=True)
        self.qapp.processEvents()
        self.widget.setData(None)

        # All NaNs or inf
        data = numpy.empty((4, 4, 4), dtype=numpy.float32)
        for value in (numpy.nan, numpy.inf):
            with self.subTest(value=str(value)):
                data[:] = value
                self.widget.setData(data, copy=True)
                self.qapp.processEvents()
Beispiel #3
0
class TestScalarFieldView(TestCaseQt):
    """Tests StatsWidget combined with ScalarFieldView"""
    def setUp(self):
        super(TestScalarFieldView, self).setUp()
        self.scalarFieldView = ScalarFieldView()
        self.scalarFieldView.resize(300, 300)
        self.scalarFieldView.show()
        self.statsWidget = BasicStatsWidget()
        self.statsWidget.setPlot(self.scalarFieldView)
        # self.qWaitForWindowExposed(self.sceneWidget)

    def tearDown(self):
        Stats._getContext.cache_clear()
        self.qapp.processEvents()
        self.scalarFieldView.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.scalarFieldView.close()
        del self.scalarFieldView
        self.statsWidget.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.statsWidget.close()
        del self.statsWidget
        super(TestScalarFieldView, self).tearDown()

    def _getTextFor(self, row, name):
        """Returns text in table at given row for column name

        :param int row: Row number in the table
        :param str name: Column id
        :rtype: Union[str,None]
        """
        statsTable = self.statsWidget._getStatsTable()

        for column in range(statsTable.columnCount()):
            headerItem = statsTable.horizontalHeaderItem(column)
            if headerItem.data(qt.Qt.UserRole) == name:
                tableItem = statsTable.item(row, column)
                return tableItem.text()

        return None

    def test(self):
        """Test StatsWidget with ScalarFieldView"""
        data = numpy.arange(64**3, dtype=numpy.float64).reshape(64, 64, 64)
        self.scalarFieldView.setData(data)

        statsTable = self.statsWidget._getStatsTable()

        # Test selection only
        self.statsWidget.setDisplayOnlyActiveItem(True)
        self.assertEqual(statsTable.rowCount(), 1)

        # Test all data
        self.statsWidget.setDisplayOnlyActiveItem(False)
        self.assertEqual(statsTable.rowCount(), 1)

        for column in range(statsTable.columnCount()):
            self.assertEqual(float(self._getTextFor(0, 'min')),
                             numpy.min(data))
            self.assertEqual(float(self._getTextFor(0, 'max')),
                             numpy.max(data))
            sum_ = numpy.sum(data)
            comz = numpy.sum(
                numpy.arange(data.shape[0]) *
                numpy.sum(data, axis=(1, 2))) / sum_
            comy = numpy.sum(
                numpy.arange(data.shape[1]) *
                numpy.sum(data, axis=(0, 2))) / sum_
            comx = numpy.sum(
                numpy.arange(data.shape[2]) *
                numpy.sum(data, axis=(0, 1))) / sum_
            self.assertEqual(self._getTextFor(0, 'COM'), str(
                (comx, comy, comz)))
Beispiel #4
0
class TestScalarFieldView(TestCaseQt):
    """Tests StatsWidget combined with ScalarFieldView"""

    def setUp(self):
        super(TestScalarFieldView, self).setUp()
        self.scalarFieldView = ScalarFieldView()
        self.scalarFieldView.resize(300, 300)
        self.scalarFieldView.show()
        self.statsWidget = BasicStatsWidget()
        self.statsWidget.setPlot(self.scalarFieldView)
        # self.qWaitForWindowExposed(self.sceneWidget)

    def tearDown(self):
        self.qapp.processEvents()
        self.scalarFieldView.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.scalarFieldView.close()
        del self.scalarFieldView
        self.statsWidget.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.statsWidget.close()
        del self.statsWidget
        super(TestScalarFieldView, self).tearDown()

    def _getTextFor(self, row, name):
        """Returns text in table at given row for column name

        :param int row: Row number in the table
        :param str name: Column id
        :rtype: Union[str,None]
        """
        statsTable = self.statsWidget._getStatsTable()

        for column in range(statsTable.columnCount()):
            headerItem = statsTable.horizontalHeaderItem(column)
            if headerItem.data(qt.Qt.UserRole) == name:
                tableItem = statsTable.item(row, column)
                return tableItem.text()

        return None

    def test(self):
        """Test StatsWidget with ScalarFieldView"""
        data = numpy.arange(64**3, dtype=numpy.float64).reshape(64, 64, 64)
        self.scalarFieldView.setData(data)

        statsTable = self.statsWidget._getStatsTable()

        # Test selection only
        self.statsWidget.setDisplayOnlyActiveItem(True)
        self.assertEqual(statsTable.rowCount(), 1)

        # Test all data
        self.statsWidget.setDisplayOnlyActiveItem(False)
        self.assertEqual(statsTable.rowCount(), 1)

        for column in range(statsTable.columnCount()):
            self.assertEqual(float(self._getTextFor(0, 'min')), numpy.min(data))
            self.assertEqual(float(self._getTextFor(0, 'max')), numpy.max(data))
            sum_ = numpy.sum(data)
            comz = numpy.sum(numpy.arange(data.shape[0]) * numpy.sum(data, axis=(1, 2))) / sum_
            comy = numpy.sum(numpy.arange(data.shape[1]) * numpy.sum(data, axis=(0, 2))) / sum_
            comx = numpy.sum(numpy.arange(data.shape[2]) * numpy.sum(data, axis=(0, 1))) / sum_
            self.assertEqual(self._getTextFor(0, 'COM'), str((comx, comy, comz)))