def __loadSave(self, file_format): """Plot with an image: test MaskToolsWidget operations""" self.plot.addImage(numpy.arange(1024**2).reshape(1024, 1024), legend='test') self.qapp.processEvents() # Draw a polygon mask toolButton = getQToolButtonFromAction(self.maskWidget.polygonAction) self.assertIsNot(toolButton, None) self.mouseClick(toolButton, qt.Qt.LeftButton) self._drawPolygon() ref_mask = self.maskWidget.getSelectionMask() self.assertFalse(numpy.all(numpy.equal(ref_mask, 0))) with temp_dir() as tmp: mask_filename = os.path.join(tmp, 'mask.' + file_format) self.maskWidget.save(mask_filename, file_format) self.maskWidget.resetSelectionMask() self.assertTrue( numpy.all(numpy.equal(self.maskWidget.getSelectionMask(), 0))) self.maskWidget.load(mask_filename) self.assertTrue( numpy.all( numpy.equal(self.maskWidget.getSelectionMask(), ref_mask)))
def __loadSave(self, file_format): self.plot.addScatter(x=numpy.arange(256), y=25 * (numpy.arange(256) % 10), value=numpy.random.random(256), legend='test') self.plot._setActiveItem(kind="scatter", legend="test") self.plot.resetZoom() self.qapp.processEvents() # Draw a polygon mask toolButton = getQToolButtonFromAction(self.maskWidget.polygonAction) self.assertIsNot(toolButton, None) self.mouseClick(toolButton, qt.Qt.LeftButton) self._drawPolygon() ref_mask = self.maskWidget.getSelectionMask() self.assertFalse(numpy.all(numpy.equal(ref_mask, 0))) with temp_dir() as tmp: mask_filename = os.path.join(tmp, 'mask.' + file_format) self.maskWidget.save(mask_filename, file_format) self.maskWidget.resetSelectionMask() self.assertTrue( numpy.all(numpy.equal(self.maskWidget.getSelectionMask(), 0))) self.maskWidget.load(mask_filename) self.assertTrue( numpy.all( numpy.equal(self.maskWidget.getSelectionMask(), ref_mask)))
def __loadSave(self, file_format): """Plot with an image: test MaskToolsWidget operations""" self.plot.addImage(numpy.arange(1024**2).reshape(1024, 1024), legend='test') self.qapp.processEvents() # Draw a polygon mask toolButton = getQToolButtonFromAction(self.maskWidget.polygonAction) self.assertIsNot(toolButton, None) self.mouseClick(toolButton, qt.Qt.LeftButton) self._drawPolygon() ref_mask = self.maskWidget.getSelectionMask() self.assertFalse(numpy.all(numpy.equal(ref_mask, 0))) with temp_dir() as tmp: mask_filename = os.path.join(tmp, 'mask.' + file_format) self.maskWidget.save(mask_filename, file_format) self.maskWidget.resetSelectionMask() self.assertTrue( numpy.all(numpy.equal(self.maskWidget.getSelectionMask(), 0))) self.maskWidget.load(mask_filename) self.assertTrue(numpy.all(numpy.equal( self.maskWidget.getSelectionMask(), ref_mask)))
def __loadSave(self, file_format): self.plot.addScatter( x=numpy.arange(256), y=25 * (numpy.arange(256) % 10), value=numpy.random.random(256), legend='test') self.plot._setActiveItem(kind="scatter", legend="test") self.plot.resetZoom() self.qapp.processEvents() # Draw a polygon mask toolButton = getQToolButtonFromAction(self.maskWidget.polygonAction) self.assertIsNot(toolButton, None) self.mouseClick(toolButton, qt.Qt.LeftButton) self._drawPolygon() ref_mask = self.maskWidget.getSelectionMask() self.assertFalse(numpy.all(numpy.equal(ref_mask, 0))) with temp_dir() as tmp: mask_filename = os.path.join(tmp, 'mask.' + file_format) self.maskWidget.save(mask_filename, file_format) self.maskWidget.resetSelectionMask() self.assertTrue( numpy.all(numpy.equal(self.maskWidget.getSelectionMask(), 0))) self.maskWidget.load(mask_filename) self.assertTrue(numpy.all(numpy.equal( self.maskWidget.getSelectionMask(), ref_mask)))
def testWithCurves(self): """Plot with curves: test all ROI widget buttons""" for offset in range(2): self.plot.addCurve(numpy.arange(1000), offset + numpy.random.random(1000), legend=str(offset)) # Add two ROI self.mouseClick(self.widget.roiWidget.addButton, qt.Qt.LeftButton) self.mouseClick(self.widget.roiWidget.addButton, qt.Qt.LeftButton) # Change active curve self.plot.setActiveCurve(str(1)) # Delete a ROI self.mouseClick(self.widget.roiWidget.delButton, qt.Qt.LeftButton) with temp_dir() as tmpDir: self.tmpFile = os.path.join(tmpDir, 'test.ini') # Save ROIs self.widget.roiWidget.save(self.tmpFile) self.assertTrue(os.path.isfile(self.tmpFile)) # Reset ROIs self.mouseClick(self.widget.roiWidget.resetButton, qt.Qt.LeftButton) # Load ROIs self.widget.roiWidget.load(self.tmpFile) del self.tmpFile
def testLoadCustomFitFunction(self): """Test FitManager using a custom fit function defined in an external file and imported with FitManager.loadtheories""" # Create synthetic data with a sum of gaussian functions x = numpy.arange(100).astype(numpy.float) # a, b, c are the fit parameters # d is a known scaling parameter that is set using configure() a, b, c, d = 1.5, 2.5, 3.5, 4.5 y = (a * x**2 + b * x + c) / d # Fitting fit = fitmanager.FitManager() fit.setdata(x=x, y=y) # Create a temporary function definition file, and import it with temp_dir() as tmpDir: tmpfile = os.path.join(tmpDir, 'customfun.py') # custom_function_definition fd = open(tmpfile, "w") fd.write(custom_function_definition) fd.close() fit.loadtheories(tmpfile) tmpfile_pyc = os.path.join(tmpDir, 'customfun.pyc') if os.path.exists(tmpfile_pyc): os.unlink(tmpfile_pyc) os.unlink(tmpfile) fit.settheory('my fit theory') # Test configure fit.configure(d=4.5) fit.estimate() fit.runfit() self.assertEqual(fit.fit_results[0]["name"], "A1") self.assertAlmostEqual(fit.fit_results[0]["fitresult"], 1.5) self.assertEqual(fit.fit_results[1]["name"], "B1") self.assertAlmostEqual(fit.fit_results[1]["fitresult"], 2.5) self.assertEqual(fit.fit_results[2]["name"], "C1") self.assertAlmostEqual(fit.fit_results[2]["fitresult"], 3.5)
def testLoadCustomFitFunction(self): """Test FitManager using a custom fit function defined in an external file and imported with FitManager.loadtheories""" # Create synthetic data with a sum of gaussian functions x = numpy.arange(100).astype(numpy.float64) # a, b, c are the fit parameters # d is a known scaling parameter that is set using configure() a, b, c, d = 1.5, 2.5, 3.5, 4.5 y = (a * x**2 + b * x + c) / d # Fitting fit = fitmanager.FitManager() fit.setdata(x=x, y=y) # Create a temporary function definition file, and import it with temp_dir() as tmpDir: tmpfile = os.path.join(tmpDir, 'customfun.py') # custom_function_definition fd = open(tmpfile, "w") fd.write(custom_function_definition) fd.close() fit.loadtheories(tmpfile) tmpfile_pyc = os.path.join(tmpDir, 'customfun.pyc') if os.path.exists(tmpfile_pyc): os.unlink(tmpfile_pyc) os.unlink(tmpfile) fit.settheory('my fit theory') # Test configure fit.configure(d=4.5) fit.estimate() fit.runfit() self.assertEqual(fit.fit_results[0]["name"], "A1") self.assertAlmostEqual(fit.fit_results[0]["fitresult"], 1.5) self.assertEqual(fit.fit_results[1]["name"], "B1") self.assertAlmostEqual(fit.fit_results[1]["fitresult"], 2.5) self.assertEqual(fit.fit_results[2]["name"], "C1") self.assertAlmostEqual(fit.fit_results[2]["fitresult"], 3.5)
def testWithCurves(self): """Plot with curves: test all ROI widget buttons""" for offset in range(2): self.plot.addCurve(numpy.arange(1000), offset + numpy.random.random(1000), legend=str(offset)) # Add two ROI self.mouseClick(self.widget.roiWidget.addButton, qt.Qt.LeftButton) self.qWait(200) self.mouseClick(self.widget.roiWidget.addButton, qt.Qt.LeftButton) self.qWait(200) # Change active curve self.plot.setActiveCurve(str(1)) # Delete a ROI self.mouseClick(self.widget.roiWidget.delButton, qt.Qt.LeftButton) self.qWait(200) with temp_dir() as tmpDir: self.tmpFile = os.path.join(tmpDir, 'test.ini') # Save ROIs self.widget.roiWidget.save(self.tmpFile) self.assertTrue(os.path.isfile(self.tmpFile)) self.assertTrue(len(self.widget.getRois()) is 2) # Reset ROIs self.mouseClick(self.widget.roiWidget.resetButton, qt.Qt.LeftButton) self.qWait(200) rois = self.widget.getRois() self.assertTrue(len(rois) is 1) print(rois) roiID = list(rois.keys())[0] self.assertTrue(rois[roiID].getName() == 'ICR') # Load ROIs self.widget.roiWidget.load(self.tmpFile) self.assertTrue(len(self.widget.getRois()) is 2) del self.tmpFile
def testLoadUi(self): """Create a QMainWindow from an ui file""" with temp_dir() as tmp: uifile = os.path.join(tmp, "test.ui") # write file with open(uifile, mode='w') as f: f.write(self.TEST_UI) class TestMainWindow(qt.QMainWindow): def __init__(self, parent=None): super(TestMainWindow, self).__init__(parent) qt.loadUi(uifile, self) testMainWindow = TestMainWindow() testMainWindow.show() self.qWaitForWindowExposed(testMainWindow) testMainWindow.setAttribute(qt.Qt.WA_DeleteOnClose) testMainWindow.close()