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

Pyqtgraph automatically store ref to the last 100 plots (configurable) #662

Merged
merged 4 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion qcodes/config/qcodesrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
},
"gui" :{
"notebook": true,
"plotlib": "matplotlib"
"plotlib": "matplotlib",
"pyqtmaxplots": 100
},
"user": {}
}
7 changes: 6 additions & 1 deletion qcodes/config/qcodesrc_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@
"type": "string",
"enum": ["QT", "matplotlib" ],
"default": "matplotlib"
},
"pyqtmaxplots": {
"description": "Maximum number of PyQtPlots to automatically keep in memory",
"type": "integer",
"default": 100
}
},
"required":[ "notebook", "plotlib" ]
"required":[ "notebook", "plotlib", "pyqtmaxplots" ]
},
"user":{
"type" : "object",
Expand Down
34 changes: 28 additions & 6 deletions qcodes/plots/pyqtgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import numpy as np
import pyqtgraph as pg
import pyqtgraph.multiprocess as pgmp
from pyqtgraph.multiprocess.remoteproxy import ClosedError
import warnings
from collections import namedtuple
from collections import namedtuple, deque

from .base import BasePlot
from .colors import color_cycle, colorscales

import qcodes.config

TransformState = namedtuple('TransformState', 'translate scale revisit')

Expand Down Expand Up @@ -37,6 +38,15 @@ class QtPlot(BasePlot):
"""
proc = None
rpg = None
# we store references to plots to keep the garbage collections from
# destroying the windows. To keep memory consumption within bounds we
# limit this to an arbitrary number of plots here using a deque
# The issue is that even when closing a window it's difficult to
# remove it from the list. This could potentially be done with a
# close event on win but this is difficult with remote proxy process
# as the list of plots lives in the main process and the plot locally
# in a remote process
plots = deque(maxlen=qcodes.config['gui']['pyqtmaxplots'])

def __init__(self, *args, figsize=(1000, 600), interval=0.25,
window_title='', theme=((60, 60, 60), 'w'), show_window=True, remote=True, **kwargs):
Expand All @@ -55,7 +65,16 @@ def __init__(self, *args, figsize=(1000, 600), interval=0.25,
else:
# overrule the remote pyqtgraph class
self.rpg = pg
self.win = self.rpg.GraphicsWindow(title=window_title)
try:
self.win = self.rpg.GraphicsWindow(title=window_title)
except ClosedError as err:
# the remote process may have crashed. In that case try restarting
# it
if remote:
self._init_qt()
self.win = self.rpg.GraphicsWindow(title=window_title)
else:
raise err
self.win.setBackground(theme[1])
self.win.resize(*figsize)
self.subplots = [self.add_subplot()]
Expand All @@ -66,13 +85,16 @@ def __init__(self, *args, figsize=(1000, 600), interval=0.25,
if not show_window:
self.win.hide()

def _init_qt(self):
self.plots.append(self)

@classmethod
def _init_qt(cls):
# starting the process for the pyqtgraph plotting
# You do not want a new process to be created every time you start a
# run, so this only starts once and stores the process in the class
pg.mkQApp()
self.__class__.proc = pgmp.QtProcess() # pyqtgraph multiprocessing
self.__class__.rpg = self.proc._import('pyqtgraph')
cls.proc = pgmp.QtProcess() # pyqtgraph multiprocessing
cls.rpg = cls.proc._import('pyqtgraph')

def clear(self):
"""
Expand Down