From: jh777916 Date: Wed, 20 Sep 2023 12:40:28 +0000 (+0200) Subject: Adding barplot and stemplot to the cpp library; removing the old isDirac bool from... X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=ddbc09e9c0e320184fd9495ddb3f3ed6aed08e75;p=modules%2Fgui.git Adding barplot and stemplot to the cpp library; removing the old isDirac bool from the barplots (now taken care of in stem plots); considering the width as the different between x and x+1 ticks --- diff --git a/tools/CurvePlot/src/cpp/CurvePlot.cxx b/tools/CurvePlot/src/cpp/CurvePlot.cxx index ebae22604..e94eddff5 100644 --- a/tools/CurvePlot/src/cpp/CurvePlot.cxx +++ b/tools/CurvePlot/src/cpp/CurvePlot.cxx @@ -205,8 +205,6 @@ namespace CURVEPLOT code = std::string("import curveplot;")+ std::string("__cont=curveplot.PlotController.GetInstance()"); - PyRun_SimpleString("import site\n"); - PyRun_SimpleString("print(site.getsitepackages())\n"); int ret = PyRun_SimpleString(const_cast(code.c_str())); if (ret == -1) @@ -284,6 +282,70 @@ namespace CURVEPLOT return curveId; } + PlotID CurvePlot::AddBarPlot(const ColumnVector & x, const ColumnVector & y, + PlotID & plot_set_id, + std::string curve_label/*=""*/, std::string x_label/*=""*/, std::string y_label/*=""*/, + bool append/*=true*/) + { + PyLockWrapper lock; + PyObject * cont = GetInstance()->_impl->_controller; + + PyObject * xx = (PyObject *)x._impl->_npArray; + PyObject * yy = (PyObject *)y._impl->_npArray; + + PyObjWrapper ret( + PyObject_CallMethod(cont, (char *)"AddBarPlot", (char *)"OOOOOi", xx, yy, + strToPyUnicode(curve_label), strToPyUnicode(x_label), strToPyUnicode(y_label), + append ? 1 : 0) + ); + HandleAndPrintPyError("CurvePlot::AddBarPlot(): unexpected error!"); + // Now extract curve_id and plot_set_id from the returned tuple: + if(!PyTuple_Check(ret)) + throw Exception("CurvePlot::AddBarPlot(): Unexpected returned type!"); + PyObject * o1 = PyTuple_GetItem(ret, 0); + if (!PyLong_Check(o1)) + throw Exception("CurvePlot::AddBarPlot(): Unexpected returned type!"); + PlotID curveId = PyLong_AsLong(o1); + PyObject * o2 = PyTuple_GetItem(ret, 1); + if (!PyLong_Check(o2)) + throw Exception("CurvePlot::AddBarPlot(): Unexpected returned type!"); + plot_set_id = PyLong_AsLong(o2); + return curveId; + } + + PlotID CurvePlot::AddStemPlot(const ColumnVector & x, const ColumnVector & y, + PlotID & plot_set_id, + std::string curve_label/*=""*/, std::string x_label/*=""*/, std::string y_label/*=""*/, + bool append/*=true*/) + { + PyLockWrapper lock; + PyObject * cont = GetInstance()->_impl->_controller; + + PyObject * xx = (PyObject *)x._impl->_npArray; + PyObject * yy = (PyObject *)y._impl->_npArray; + + PyObjWrapper ret( + PyObject_CallMethod(cont, (char *)"AddStemPlot", (char *)"OOOOOi", xx, yy, + strToPyUnicode(curve_label), strToPyUnicode(x_label), strToPyUnicode(y_label), + append ? 1 : 0) + ); + HandleAndPrintPyError("CurvePlot::AddStemPlot(): unexpected error!"); + // Now extract curve_id and plot_set_id from the returned tuple: + if(!PyTuple_Check(ret)) + throw Exception("CurvePlot::AddStemPlot(): Unexpected returned type!"); + PyObject * o1 = PyTuple_GetItem(ret, 0); + if (!PyLong_Check(o1)) + throw Exception("CurvePlot::AddStemPlot(): Unexpected returned type!"); + PlotID curveId = PyLong_AsLong(o1); + PyObject * o2 = PyTuple_GetItem(ret, 1); + if (!PyLong_Check(o2)) + throw Exception("CurvePlot::AddStemPlot(): Unexpected returned type!"); + plot_set_id = PyLong_AsLong(o2); + return curveId; + } + + + PlotID CurvePlot::AddPlotSet(std::string title/*=""*/) { PyLockWrapper lock; diff --git a/tools/CurvePlot/src/cpp/CurvePlot.hxx b/tools/CurvePlot/src/cpp/CurvePlot.hxx index b0743235b..380ea469d 100644 --- a/tools/CurvePlot/src/cpp/CurvePlot.hxx +++ b/tools/CurvePlot/src/cpp/CurvePlot.hxx @@ -94,6 +94,16 @@ namespace CURVEPLOT std::string curve_label="", std::string x_label="", std::string y_label="", bool append=true); + static PlotID AddBarPlot(const ColumnVector & x, const ColumnVector & y, + PlotID & plot_set_id, + std::string curve_label="", std::string x_label="", std::string y_label="", + bool append=true); + + static PlotID AddStemPlot(const ColumnVector & x, const ColumnVector & y, + PlotID & plot_set_id, + std::string curve_label="", std::string x_label="", std::string y_label="", + bool append=true); + static PlotID AddPlotSet(std::string title=""); static PlotID DeleteCurve(PlotID curve_id=-1); diff --git a/tools/CurvePlot/src/cpp/test/test_curveplot.cxx b/tools/CurvePlot/src/cpp/test/test_curveplot.cxx index a3e2339ed..a80654270 100644 --- a/tools/CurvePlot/src/cpp/test/test_curveplot.cxx +++ b/tools/CurvePlot/src/cpp/test/test_curveplot.cxx @@ -90,7 +90,6 @@ void initPython() } std::cout << " > Inside initPython()" << std::endl; - } /* Little hack to gather widgets created on the Python side */ diff --git a/tools/CurvePlot/src/python/controller/PlotController.py b/tools/CurvePlot/src/python/controller/PlotController.py index 8f3ff17cb..aa3bd4684 100644 --- a/tools/CurvePlot/src/python/controller/PlotController.py +++ b/tools/CurvePlot/src/python/controller/PlotController.py @@ -192,20 +192,22 @@ class PlotController(object): @classmethod - def AddBarPlot(cls, x, height, width, isDirac, curve_label="", x_label="", y_label="", append=True): + def AddBarPlot(cls, x_ticks, height, curve_label="", x_label="", y_label="", append=True): """ Testing some stuff with BarPlot""" from .XYView import XYView control = cls.GetInstance() pm = control._plotManager t = TableModel(control) - data = np.transpose(np.vstack([x, height, width])) + x_inf = x_ticks[:-1] # TAG CHECK, verif nb ticks + width = x_ticks[1:] - x_ticks[:-1] + data = np.transpose(np.vstack([x_inf, height, width])) t.setData(data) # ensure a single Matplotlib repaint for all operations to come in AddBarPlot prevLock = pm.isRepaintLocked() if not prevLock: pm.lockRepaint() - curveID, plotSetID = control.plotBarPlotFromTable(t, isDirac, + curveID, plotSetID = control.plotBarPlotFromTable(t, x_col_index=0, height_col_index=1, width_col_index=2, curve_label=curve_label, append=append) @@ -784,7 +786,7 @@ class PlotController(object): return cm.getID(),cps.getID() - def plotBarPlotFromTable(self, table, isDirac=True, + def plotBarPlotFromTable(self, table, x_col_index=0, height_col_index=1, width_col_index=2, curve_label="", append=True): """ @@ -802,7 +804,7 @@ class PlotController(object): cps = self._plotManager.getCurrentPlotSet() - bm = BarModel(self, table, isDirac, height_col_index, width_col_index) + bm = BarModel(self, table, height_col_index, width_col_index) bm.setXAxisIndex(x_col_index) # X axis label diff --git a/tools/CurvePlot/src/python/model/BarModel.py b/tools/CurvePlot/src/python/model/BarModel.py index 148f658da..26b5c739b 100644 --- a/tools/CurvePlot/src/python/model/BarModel.py +++ b/tools/CurvePlot/src/python/model/BarModel.py @@ -22,14 +22,13 @@ from .utils import toUnicodeWithWarning class BarModel(Model): - def __init__(self, controller, table=None, isDirac=True, heightIndex=-1, widthIndex=-1): + def __init__(self, controller, table=None, heightIndex=-1, widthIndex=-1): Model.__init__(self, controller) # self.ID_PLOT += 1 self._name = "BarModel" self._title = "Barplot %d" % self.getID() self._table = table - self._isDirac = isDirac self._xaxisIndex = 0 # By default the first column of the table is used for the X-s self._heightIndex = heightIndex # column index in the table, corresponding to the bar height @@ -43,15 +42,8 @@ class BarModel(Model): ret._xaxisIndex = self._xaxisIndex ret._heightIndex = self._heightIndex ret._widthIndex = self._widthIndex - ret._isDirac = self._isDirac return ret - def getIsDirac(self): - return self._isDirac - - def setIsDirac(self, isD, silent=False): - self._isDirac = isD - def setTable(self, t, silent=False): self._table = t if not silent: diff --git a/tools/CurvePlot/src/python/test/TestDesktop.py b/tools/CurvePlot/src/python/test/TestDesktop.py index 5356cc3ea..ba66bf44e 100644 --- a/tools/CurvePlot/src/python/test/TestDesktop.py +++ b/tools/CurvePlot/src/python/test/TestDesktop.py @@ -78,7 +78,6 @@ class TestDesktop(QMainWindow): self.perfTestAction.triggered.connect(self.perfTest) self.addStemPlotAction.triggered.connect(self.addStemplot) self.addBarPlotNonDiracAction.triggered.connect(self.addBarplotNonDirac) - self.addBarPlotDiracAction.triggered.connect(self.addBarplotDirac) def generateID(self): @@ -98,7 +97,6 @@ class TestDesktop(QMainWindow): self.perfActionID = self.generateID() self.addStemPlotActionID = self.generateID() self.addBarPlotNonDiracActionID = self.generateID() - self.addBarPlotDiracActionID = self.generateID() # Menus self.etudeMenuID = self.generateID() @@ -117,7 +115,6 @@ class TestDesktop(QMainWindow): self.perfTestAction = ca(self.perfActionID, "Perf test", "Perf test", "", "") self.addStemPlotAction = ca(self.addStemPlotActionID, "Add StemPlot - Dirac", "Add StemPlot - Dirac", "", "") self.addBarPlotNonDiracAction = ca(self.addBarPlotNonDiracActionID, "Add BarPlot - Non Dirac", "Add BarPlot- Non Dirac", "", "") - self.addBarPlotDiracAction = ca(self.addBarPlotDiracActionID, "Add BarPlot - Dirac", "Add BarPlot - Dirac", "", "") def createToolbars(self): pass @@ -141,7 +138,6 @@ class TestDesktop(QMainWindow): self._sgPyQt.createMenu(self.perfTestAction, curveMenu) self._sgPyQt.createMenu(self.addStemPlotAction, curveMenu) self._sgPyQt.createMenu(self.addBarPlotNonDiracAction, curveMenu) - self._sgPyQt.createMenu(self.addBarPlotDiracAction, curveMenu) dummyMenu = self._sgPyQt.createMenu( "Dummy", -1, self.dummyMenuID, self._sgPyQt.defaultMenuGroup() ) self._sgPyQt.createMenu(self.addTabAction, dummyMenu) @@ -196,18 +192,6 @@ class TestDesktop(QMainWindow): curveplot.UnlockRepaint() print("Elapsed: %.2f" % ( time() - t0)) - @pyqtSlot() - def addBarplotDirac(self): - energy_val = np.array([0., 1.15, 5.2, 8.01, 8.33, 8.67, 8.99, 9.01, 9.33, 9.99, 15.1, 17.8, 19.7]) - - intensity = np.random.rand(energy_val.size) * 10 - isDirac = True - width_dirac = np.full(energy_val.size, 0.05) - - _, ps_id = curveplot.AddBarPlot(energy_val, intensity, width_dirac, isDirac, - x_label="Energy", y_label="Intensity", append=True) - - curveplot.SetLegendVisible(ps_id, True) @pyqtSlot() def addStemplot(self): @@ -226,28 +210,14 @@ class TestDesktop(QMainWindow): from curveplot import TableModel from random import random - energy_inf = np.array([0., 1.15, 5.2, 8.01, 8.33, 8.67, 8.99, 9.01, 9.33, 9.99, 15.1, 17.8, 19.7]) - energy_sup = np.array([1.15, 5.2, 8.01, 8.33, 8.67, 8.99, 9.01, 9.33, 9.99, 15.1, 17.8, 19.7, 20.]) - deltaE = energy_sup - energy_inf - intensity = np.random.rand(energy_inf.size) * 10 - isDirac = False + energy_ticks = np.array([0., 1.15, 5.2, 8.01, 8.33, 8.67, 8.99, 9.01, 9.33, 9.99, 15.1, 17.8, 19.7, 20.]) - t = TableModel(None) - t.setTitle("Non Dirac data") - t.addColumn(energy_inf) # x : energy inf - t.addColumn(intensity) # height : intensity - t.addColumn(deltaE) # width : delta energy - t.setColumnTitle(0, "Energy") - t.setColumnTitle(1, "Intensity") - t.setColumnTitle(2, "DeltaE") + # intensity.size = energy_ticks.size - 1 + intensity = np.random.rand(energy_ticks.size - 1) * 10 + + _, ps_id = curveplot.AddBarPlot(energy_ticks, intensity, + x_label="Energy", y_label="Intensity", append=True) - bar_label = "Some spectrum" - cont = curveplot.PlotController.GetInstance() - crv_id, ps_id = cont.plotBarPlotFromTable(t, isDirac, - x_col_index=0, height_col_index=1, width_col_index=2, - curve_label=bar_label, - append=True) - curveplot.SetLegendVisible(ps_id, True) diff --git a/tools/CurvePlot/src/python/views/BarView.py b/tools/CurvePlot/src/python/views/BarView.py index 8e65c4293..2384f716e 100644 --- a/tools/CurvePlot/src/python/views/BarView.py +++ b/tools/CurvePlot/src/python/views/BarView.py @@ -45,7 +45,7 @@ class BarView(View): def draw(self): m = self._model x_idx, height_idx, width_idx = m.getXAxisIndex(), m.getHeightIndex(), m.getWidthIndex() - x_align = "center" if m.getIsDirac() else "edge" + x_align = "edge" # inf borns d = self._model.getTable().getData() # Bar returns a BarContainer (list of Artists) diff --git a/tools/CurvePlot/src/python/views/XYView.py b/tools/CurvePlot/src/python/views/XYView.py index 01dbf539d..d9d15c630 100644 --- a/tools/CurvePlot/src/python/views/XYView.py +++ b/tools/CurvePlot/src/python/views/XYView.py @@ -46,7 +46,7 @@ class EventHandler(QObject): return QObject.eventFilter(self, obj, event) class XYView(View): - AUTOFIT_MARGIN = 0.03 # 3% + AUTOFIT_MARGIN = 0.1 # 3% # See http://matplotlib.org/api/markers_api.html: CURVE_MARKERS = [ "o" ,# circle