Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"cb_append_data_point" leads to runtime error if no existing points are present #14

Closed
nhansendev opened this issue Nov 28, 2022 · 7 comments

Comments

@nhansendev
Copy link

nhansendev commented Nov 28, 2022

I initialize a LiveLinePlot, which is passed to a DataConnector for later updating. On the first iteration where I try to add a datapoint through the connecter via cb_append_data_point I receive the following error:

/home/user/.local/lib/python3.10/site-packages/pyqtgraph/debug.py:128: RuntimeWarning: Ignored exception:
Traceback (most recent call last):
  File "/home/user/path/pysideGUI.py", line 1101, in <module>
    app.exec()
  File "/home/user/.local/lib/python3.10/site-packages/pglive/sources/live_plot_widget.py", line 165, in paintEvent
    return super().paintEvent(ev)
  File "/home/user/.local/lib/python3.10/site-packages/pyqtgraph/widgets/GraphicsView.py", line 137, in paintEvent
    return super().paintEvent(ev)
  File "/home/user/.local/lib/python3.10/site-packages/pyqtgraph/debug.py", line 128, in w
    printExc('Ignored exception:')
  --- exception caught here ---
  File "/home/user/.local/lib/python3.10/site-packages/pyqtgraph/debug.py", line 126, in w
    func(*args, **kwds)
  File "/home/user/.local/lib/python3.10/site-packages/pyqtgraph/graphicsItems/PlotCurveItem.py", line 905, in paint
    p.drawLines(*self._getLineSegments())
TypeError: QPainter.drawLines() takes exactly one argument (0 given)
  printExc('Ignored exception:')

I've found that checking for the presence of data in the x or y variable in the DataConnector and adding the first datapoint twice if there is none prevents the error from appearing. E.g.:

if len(connector.x) < 1:
  *add extra datapoint*
*add datapoint like normal*

It seems like there just needs to be an additional check somewhere(?)

Using:
Python 3.10
pglive 0.5.5

@domarm-comat
Copy link
Owner

Error is thrown by underlying pyqtgraph, complaining, that there is no data to be plot I believe.

Are the pglive examples running OK from your environment?
Could you confirm, that you are actually passing some data when calling cb_append_data_point from your data generator?

I think either providing some input data causing error or posting some minimal reproducible code would help me to figure out the issue. Without it, I can't reproduce the exact issue and will be just guessing what might be wrong.

Try maybe just collecting the data before are passed to callback and see if there is no None values or empty List or something like that. Pyqtgraph might ignore for example None value or empty list or string if that is found in the X or Y values.

Thanks for using pglive, I hope we can solve your problem.

@nhansendev
Copy link
Author

nhansendev commented Nov 30, 2022

Forgot to mention that I'm using PySide6.

I just tried using the cb_set_data function and received a different error:

File "/home/user/path/pysideGUI.py", line 192, in add_points
   self.lines[name][1].cb_set_data(ys, xs)
 File "/home/user/.local/lib/python3.10/site-packages/pglive/sources/data_connector.py", line 132, in cb_set_data
   self.sig_data_toggle.emit(self)
TypeError: sig_data_toggle(PyObject,bool) needs 2 argument(s), 1 given!

Here is some example code to reproduce the errors:

from PySide6.QtWidgets import *
from pyqtgraph import mkPen
from pglive.sources.data_connector import DataConnector
from pglive.sources.live_plot import LiveLinePlot
from pglive.sources.live_plot_widget import LivePlotWidget
import random


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.layout = QVBoxLayout()

        self.testplot = TestPlot()
        self.layout.addWidget(self.testplot.plot_widget)

        testbutton = QPushButton(text="Add Point")
        testbutton.pressed.connect(self.add_point)
        self.layout.addWidget(testbutton)

        testbutton = QPushButton(text="Set Data")
        testbutton.pressed.connect(self.set_data)
        self.layout.addWidget(testbutton)

        self.main_widget = QWidget()
        self.main_widget.setLayout(self.layout)
        self.setCentralWidget(self.main_widget)

    def add_point(self):
        self.testplot.add_point(random.random(), random.random())

    def set_data(self):
        self.testplot.set_data(
            [random.random(), random.random()], [random.random(), random.random()]
        )


class TestPlot:
    def __init__(self):
        self.plot_widget = LivePlotWidget()

        tmp = LiveLinePlot(pen=mkPen((0, 8), width=2))
        self.conn = DataConnector(tmp)
        self.plot_widget.addItem(tmp)

    def clear(self):
        self.conn.cb_set_data(None, None)

    def set_data(self, xs, ys):
        self.conn.cb_set_data(ys, xs)

    def add_point(self, x, y):
        self.conn.cb_append_data_point(y, x)


if __name__ == "__main__":
    app = QApplication()

    window = MainWindow()
    window.show()

    app.exec()

@nhansendev
Copy link
Author

Not sure why, but removing the pen from the LiveLinePlot seems to get rid of the original error.
tmp = LiveLinePlot()

@nhansendev
Copy link
Author

nhansendev commented Nov 30, 2022

FYI, you are correct that the original error is just a pyqtgraph issue (happens with the normal PlotWidget). I've created an issue on their github insead.

The error from cb_set_data is still relevant, though.

@domarm-comat
Copy link
Owner

I can confirm error with cb_set_data. I found out the reason as well, but I need to do some more testing before releasing new version.

@domarm-comat
Copy link
Owner

domarm-comat commented Nov 30, 2022

V0.5.6 is out, fixing cb_set_data error. That should work just fine now.

For cb_append_data_point error, it seems it's really something internal with pyqtgraph and pen as you discovered.
I tried just pure pyqtgraph code:


from PyQt6.QtWidgets import QApplication
import pyqtgraph as pg

app = QApplication(sys.argv)

plotWidget = pg.plot(title="Three plot curves")
di = pg.PlotDataItem(pen=pg.mkPen("red", width=2))
di.setData([0], [1])
plotWidget.addItem(di)

app.exec()

Which gave me the same error. It kind of make sense, since it's trying to plot line using only one point. You should have two :).
When I removed pen parameter error was gone...

@nhansendev
Copy link
Author

FYI, the original error has been fixed on pyqtgraph's side, but not yet released (pyqtgraph/pyqtgraph#2481).
Confirmed that the V0.5.6 fix worked for me.

Thanks for the fast response on this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants