Skip to content

Commit

Permalink
Pyqtgraph automatically store ref to the last 100 plots (configurable) (
Browse files Browse the repository at this point in the history
microsoft#662)

* Protect against crashed qt remote processes

* WIP trying to store refs to plots

* Store last 100 qt plots in a deque

* make max num plots a configurable
  • Loading branch information
jenshnielsen authored and Dominik-Vogel committed Aug 7, 2017
1 parent 687a629 commit f3fa085
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
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", "null"],
"enum": ["QT", "matplotlib", null],
"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

0 comments on commit f3fa085

Please sign in to comment.