]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
Adding barplot and stemplot to the cpp library; removing the old isDirac bool from...
authorjh777916 <juba.hamma@cea.fr>
Wed, 20 Sep 2023 12:40:28 +0000 (14:40 +0200)
committerjh777916 <juba.hamma@cea.fr>
Wed, 20 Sep 2023 12:40:28 +0000 (14:40 +0200)
tools/CurvePlot/src/cpp/CurvePlot.cxx
tools/CurvePlot/src/cpp/CurvePlot.hxx
tools/CurvePlot/src/cpp/test/test_curveplot.cxx
tools/CurvePlot/src/python/controller/PlotController.py
tools/CurvePlot/src/python/model/BarModel.py
tools/CurvePlot/src/python/test/TestDesktop.py
tools/CurvePlot/src/python/views/BarView.py
tools/CurvePlot/src/python/views/XYView.py

index ebae22604469db0874ffbc604fe86d984b65b5f4..e94eddff56f2ecd8400c8ae270228ddaf17918a9 100644 (file)
@@ -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<char*>(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;
index b0743235b7471784c65a523063fb96b337862d52..380ea469dbf50019548106e39d91c7e5c59a3da0 100644 (file)
@@ -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);
index a3e2339ed40d16e9274bbe38eef28e01217af7a8..a80654270f7d0875657d9f9d528cfd11f97cbc21 100644 (file)
@@ -90,7 +90,6 @@ void initPython()
 
   }
   std::cout << " > Inside initPython()" << std::endl;
-
 }
 
 /* Little hack to gather widgets created on the Python side  */
index 8f3ff17cbb9493af9c040231694091abcd205196..aa3bd46840b2ceec00b866b2231b0354417153c8 100644 (file)
@@ -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
index 148f658daf35ffa8364d034264ff04d11134c33d..26b5c739b0cce0ca3342e2fd31afbf8dc17ce736 100644 (file)
@@ -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:
index 5356cc3eaaa63bf392cb27f5426fcdc7deb4c030..ba66bf44ea50ee648c7312db7227359f587b3dc4 100644 (file)
@@ -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)
 
 
index 8e65c429381f101e565a7c4a7b2247cd1ef098ba..2384f716ef72c3272e077843466e4fac23212927 100644 (file)
@@ -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)
index 01dbf539d7c6c65b237a64d7b0bc4f0bbd5592d1..d9d15c63037218dfff59543b5fd104e78ba2c056 100644 (file)
@@ -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