Exemplo n.º 1
0
 def writeImage(self, fileName=None):
     if fileName is None:
         self.fileDialog = FileDialog()
         if PlotItem.lastFileDir is not None:
             self.fileDialog.setDirectory(PlotItem.lastFileDir)
         self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
         self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
         self.fileDialog.show()
         self.fileDialog.fileSelected.connect(self.writeImage)
         return
     #if fileName is None:
     #fileName = QtGui.QFileDialog.getSaveFileName()
     if isinstance(fileName, tuple):
         raise Exception("Not implemented yet..")
     fileName = str(fileName)
     PlotItem.lastFileDir = os.path.dirname(fileName)
     self.png = QtGui.QImage(int(self.size().width()),
                             int(self.size().height()),
                             QtGui.QImage.Format_ARGB32)
     painter = QtGui.QPainter(self.png)
     painter.setRenderHints(painter.Antialiasing | painter.TextAntialiasing)
     self.scene().render(painter, QtCore.QRectF(),
                         self.mapRectToScene(self.boundingRect()))
     painter.end()
     self.png.save(fileName)
Exemplo n.º 2
0
 def writeCsv(self, fileName=None):
     if fileName is None:
         self.fileDialog = FileDialog()
         if PlotItem.lastFileDir is not None:
             self.fileDialog.setDirectory(PlotItem.lastFileDir)
         self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
         self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
         self.fileDialog.show()
         self.fileDialog.fileSelected.connect(self.writeCsv)
         return
     #if fileName is None:
         #fileName = QtGui.QFileDialog.getSaveFileName()
     fileName = str(fileName)
     PlotItem.lastFileDir = os.path.dirname(fileName)
     
     fd = open(fileName, 'w')
     data = [c.getData() for c in self.curves]
     i = 0
     while True:
         done = True
         for d in data:
             if i < len(d[0]):
                 fd.write('%g,%g,'%(d[0][i], d[1][i]))
                 done = False
             else:
                 fd.write(' , ,')
         fd.write('\n')
         if done:
             break
         i += 1
     fd.close()
Exemplo n.º 3
0
 def writeCsv(self, fileName=None):
     if fileName is None:
         self.fileDialog = FileDialog()
         if PlotItem.lastFileDir is not None:
             self.fileDialog.setDirectory(PlotItem.lastFileDir)
         self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
         self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
         self.fileDialog.show()
         self.fileDialog.fileSelected.connect(self.writeCsv)
         return
     #if fileName is None:
         #fileName = QtGui.QFileDialog.getSaveFileName()
     fileName = str(fileName)
     PlotItem.lastFileDir = os.path.dirname(fileName)
     
     fd = open(fileName, 'w')
     data = [c.getData() for c in self.curves]
     i = 0
     while True:
         done = True
         for d in data:
             if i < len(d[0]):
                 fd.write('%g,%g,'%(d[0][i], d[1][i]))
                 done = False
             else:
                 fd.write(' , ,')
         fd.write('\n')
         if done:
             break
         i += 1
     fd.close()
Exemplo n.º 4
0
 def fileSaveDialog(self, filter=None, opts=None):
     ## Show a file dialog, call self.export(fileName) when finished.
     if opts is None:
         opts = {}
     self.fileDialog = FileDialog()
     self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
     self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
     if filter is not None:
         if isinstance(filter, basestring):
             self.fileDialog.setNameFilter(filter)
         elif isinstance(filter, list):
             self.fileDialog.setNameFilters(filter)
     global LastExportDirectory
     exportDir = LastExportDirectory
     if exportDir is not None:
         self.fileDialog.setDirectory(exportDir)
     self.fileDialog.show()
     self.fileDialog.opts = opts
     self.fileDialog.fileSelected.connect(self.fileSaveFinished)
     return
Exemplo n.º 5
0
 def loadSolver(self, fileName=None, startDir=None):
     if fileName is None:
         if startDir is None:
             startDir = self.directory
         if startDir is None:
             startDir = "."
         self.fileDialog = FileDialog(None, "Load Flowchart..", startDir, "Protobuf (*.prototxt)")
         self.fileDialog.show()
         self.fileDialog.fileSelected.connect(self.loadSolver)
         return
     fileName = unicode(fileName)
     self.solverNode.setProto(fileName)
Exemplo n.º 6
0
    def saveSolver(self, fileName=None, startDir=None, suggestedFileName="solver.prototxt"):
        if fileName is None:
            if startDir is None:
                startDir = self.directory
            if startDir is None:
                startDir = "."
            self.fileDialog = FileDialog(None, "Save Solver..", startDir, "Prototxt (*.prototxt)")
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.saveSolver)
            return

        fileName = unicode(fileName)
        self.solverNode.writeProto(fileName)
Exemplo n.º 7
0
 def saveFile(self, fileName=None, startDir=None, suggestedFileName='flowchart.fc'):
     """save the current prototxt file"""
     if fileName is None:
         if startDir is None:
             startDir = self.filePath
         if startDir is None:
             startDir = '.'
         self.fileDialog = FileDialog(None, "Save Flowchart..", startDir, "Flowchart (*.fc)")
         self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
         self.fileDialog.show()
         self.fileDialog.fileSelected.connect(self.saveFile)
         return
     fileName = unicode(fileName)
     configfile.writeConfigFile(self.saveState(), fileName)
     self.sigFileSaved.emit(fileName)
Exemplo n.º 8
0
    def loadFile(self, fileName=None, startDir=None):
        """load a new prototxt file"""
        if fileName is None:
            if startDir is None:
                startDir = self.filePath
            if startDir is None:
                startDir = '.'
            self.fileDialog = FileDialog(None, "Load Flowchart..", startDir, "Protobuf (*.prototxt)")
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.loadFile)
            return
        fileName = unicode(fileName)
        self.clear()
        self.proto = parsePrototxt(fileName, 'net')
        self.layerList = self.proto.layer

        self.setupNodes()
        self.sigFileLoaded.emit(fileName)
Exemplo n.º 9
0
 def fileSaveDialog(self, filter=None, opts=None):
     ## Show a file dialog, call self.export(fileName) when finished.
     if opts is None:
         opts = {}
     self.fileDialog = FileDialog()
     self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
     self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
     if filter is not None:
         if isinstance(filter, basestring):
             self.fileDialog.setNameFilter(filter)
         elif isinstance(filter, list):
             self.fileDialog.setNameFilters(filter)
     global LastExportDirectory
     exportDir = LastExportDirectory
     if exportDir is not None:
         self.fileDialog.setDirectory(exportDir)
     self.fileDialog.show()
     self.fileDialog.opts = opts
     self.fileDialog.fileSelected.connect(self.fileSaveFinished)
     return
Exemplo n.º 10
0
 def writeImage(self, fileName=None):
     if fileName is None:
         self.fileDialog = FileDialog()
         if PlotItem.lastFileDir is not None:
             self.fileDialog.setDirectory(PlotItem.lastFileDir)
         self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
         self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
         self.fileDialog.show()
         self.fileDialog.fileSelected.connect(self.writeImage)
         return
     #if fileName is None:
         #fileName = QtGui.QFileDialog.getSaveFileName()
     if isinstance(fileName, tuple):
         raise Exception("Not implemented yet..")
     fileName = str(fileName)
     PlotItem.lastFileDir = os.path.dirname(fileName)
     self.png = QtGui.QImage(int(self.size().width()), int(self.size().height()), QtGui.QImage.Format_ARGB32)
     painter = QtGui.QPainter(self.png)
     painter.setRenderHints(painter.Antialiasing | painter.TextAntialiasing)
     self.scene().render(painter, QtCore.QRectF(), self.mapRectToScene(self.boundingRect()))
     painter.end()
     self.png.save(fileName)
Exemplo n.º 11
0
class Exporter(object):
    """
    Abstract class used for exporting graphics to file / printer / whatever.
    """    
    allowCopy = False  # subclasses set this to True if they can use the copy buffer
    Exporters = []
    
    @classmethod
    def register(cls):
        """
        Used to register Exporter classes to appear in the export dialog.
        """
        Exporter.Exporters.append(cls)
    
    def __init__(self, item):
        """
        Initialize with the item to be exported.
        Can be an individual graphics item or a scene.
        """
        object.__init__(self)
        self.item = item
        
    def parameters(self):
        """Return the parameters used to configure this exporter."""
        raise Exception("Abstract method must be overridden in subclass.")
        
    def export(self, fileName=None, toBytes=False, copy=False):
        """
        If *fileName* is None, pop-up a file dialog.
        If *toBytes* is True, return a bytes object rather than writing to file.
        If *copy* is True, export to the copy buffer rather than writing to file.
        """
        raise Exception("Abstract method must be overridden in subclass.")

    def fileSaveDialog(self, filter=None, opts=None):
        ## Show a file dialog, call self.export(fileName) when finished.
        if opts is None:
            opts = {}
        self.fileDialog = FileDialog()
        self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
        self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        if filter is not None:
            if isinstance(filter, basestring):
                self.fileDialog.setNameFilter(filter)
            elif isinstance(filter, list):
                self.fileDialog.setNameFilters(filter)
        global LastExportDirectory
        exportDir = LastExportDirectory
        if exportDir is not None:
            self.fileDialog.setDirectory(exportDir)
        self.fileDialog.show()
        self.fileDialog.opts = opts
        self.fileDialog.fileSelected.connect(self.fileSaveFinished)
        return
        
    def fileSaveFinished(self, fileName):
        fileName = asUnicode(fileName)
        global LastExportDirectory
        LastExportDirectory = os.path.split(fileName)[0]
        
        ## If file name does not match selected extension, append it now
        ext = os.path.splitext(fileName)[1].lower().lstrip('.')
        selectedExt = re.search(r'\*\.(\w+)\b', asUnicode(self.fileDialog.selectedNameFilter()))
        if selectedExt is not None:
            selectedExt = selectedExt.groups()[0].lower()
            if ext != selectedExt:
                fileName = fileName + '.' + selectedExt.lstrip('.')
        
        self.export(fileName=fileName, **self.fileDialog.opts)
        
    def getScene(self):
        if isinstance(self.item, GraphicsScene):
            return self.item
        else:
            return self.item.scene()
        
    def getSourceRect(self):
        if isinstance(self.item, GraphicsScene):
            w = self.item.getViewWidget()
            return w.viewportTransform().inverted()[0].mapRect(w.rect())
        else:
            return self.item.sceneBoundingRect()
        
    def getTargetRect(self):        
        if isinstance(self.item, GraphicsScene):
            return self.item.getViewWidget().rect()
        else:
            return self.item.mapRectToDevice(self.item.boundingRect())
        
    def setExportMode(self, export, opts=None):
        """
        Call setExportMode(export, opts) on all items that will 
        be painted during the export. This informs the item
        that it is about to be painted for export, allowing it to 
        alter its appearance temporarily
        
        
        *export*  - bool; must be True before exporting and False afterward
        *opts*    - dict; common parameters are 'antialias' and 'background'
        """
        if opts is None:
            opts = {}
        for item in self.getPaintItems():
            if hasattr(item, 'setExportMode'):
                item.setExportMode(export, opts)
    
    def getPaintItems(self, root=None):
        """Return a list of all items that should be painted in the correct order."""
        if root is None:
            root = self.item
        preItems = []
        postItems = []
        if isinstance(root, QtGui.QGraphicsScene):
            childs = [i for i in root.items() if i.parentItem() is None]
            rootItem = []
        else:
            childs = root.childItems()
            rootItem = [root]
        childs.sort(key=lambda a: a.zValue())
        while len(childs) > 0:
            ch = childs.pop(0)
            tree = self.getPaintItems(ch)
            if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or (ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0):
                preItems.extend(tree)
            else:
                postItems.extend(tree)
                
        return preItems + rootItem + postItems

    def render(self, painter, targetRect, sourceRect, item=None):
        self.getScene().render(painter, QtCore.QRectF(targetRect), QtCore.QRectF(sourceRect))
Exemplo n.º 12
0
                         "Horse Riding (Video)", "TaiChi (Video)"],
                  default="None")
exp_wg.setFixedSize(200, 30)
exp_wg_prev = exp_wg.currentText()

# Exit button
exit_wg = QtGui.QPushButton("Exit")
exit_wg.setFixedSize(200, 30)
exit_wg.setCheckable(True)
exit_wg.clicked.connect(lambda: gui.exit_wg_state(exit_wg, viewer_app))

# File chooser
file_button = QtGui.QPushButton("Open Image/Video")
file_button.setFixedSize(200, 30)
file_button.setCheckable(True)
file_chooser = FileDialog()
file_chooser.setFileMode(QtGui.QFileDialog.ExistingFile)
file_chooser.selectNameFilter("Image files (*.jpg *.jpeg *.png *.tiff)")
file_name = ""
file_name_prev = ""


def select_file():
    """select file."""
    global file_name
    file_name = file_chooser.getOpenFileName()
file_button.clicked.connect(select_file)

# Utitlity Layout
UTIL_layout.addRow(dis_label, dis_wg)
UTIL_layout.addRow(exp_label, exp_wg)
Exemplo n.º 13
0
class Exporter(object):
    """
    Abstract class used for exporting graphics to file / printer / whatever.
    """    

    def __init__(self, item):
        """
        Initialize with the item to be exported.
        Can be an individual graphics item or a scene.
        """
        object.__init__(self)
        self.item = item
        
    def item(self):
        return self.item
    
    def parameters(self):
        """Return the parameters used to configure this exporter."""
        raise Exception("Abstract method must be overridden in subclass.")
        
    def export(self):
        """"""
        raise Exception("Abstract method must be overridden in subclass.")

    def fileSaveDialog(self, filter=None, opts=None):
        ## Show a file dialog, call self.export(fileName) when finished.
        if opts is None:
            opts = {}
        self.fileDialog = FileDialog()
        self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
        self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        if filter is not None:
            if isinstance(filter, basestring):
                self.fileDialog.setNameFilter(filter)
            elif isinstance(filter, list):
                self.fileDialog.setNameFilters(filter)
        global LastExportDirectory
        exportDir = LastExportDirectory
        if exportDir is not None:
            self.fileDialog.setDirectory(exportDir)
        self.fileDialog.show()
        self.fileDialog.opts = opts
        self.fileDialog.fileSelected.connect(self.fileSaveFinished)
        return
        
    def fileSaveFinished(self, fileName):
        fileName = str(fileName)
        global LastExportDirectory
        LastExportDirectory = os.path.split(fileName)[0]
        self.export(fileName=fileName, **self.fileDialog.opts)
        
        
    def getScene(self):
        if isinstance(self.item, pg.GraphicsScene):
            return self.item
        else:
            return self.item.scene()
        
    def getSourceRect(self):
        if isinstance(self.item, pg.GraphicsScene):
            return self.item.getViewWidget().viewRect()
        else:
            return self.item.sceneBoundingRect()
        
    def getTargetRect(self):        
        if isinstance(self.item, pg.GraphicsScene):
            return self.item.getViewWidget().rect()
        else:
            return self.item.mapRectToDevice(self.item.boundingRect())
        
    def setExportMode(self, export, opts=None):
        """
        Call setExportMode(export, opts) on all items that will 
        be painted during the export. This informs the item
        that it is about to be painted for export, allowing it to 
        alter its appearance temporarily
        
        
        *export*  - bool; must be True before exporting and False afterward
        *opts*    - dict; common parameters are 'antialias' and 'background'
        """
        if opts is None:
            opts = {}
        for item in self.getPaintItems():
            if hasattr(item, 'setExportMode'):
                item.setExportMode(export, opts)
    
    def getPaintItems(self, root=None):
        """Return a list of all items that should be painted in the correct order."""
        if root is None:
            root = self.item
        preItems = []
        postItems = []
        if isinstance(root, QtGui.QGraphicsScene):
            childs = [i for i in root.items() if i.parentItem() is None]
            rootItem = []
        else:
            childs = root.childItems()
            rootItem = [root]
        childs.sort(lambda a,b: cmp(a.zValue(), b.zValue()))
        while len(childs) > 0:
            ch = childs.pop(0)
            tree = self.getPaintItems(ch)
            if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or (ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0):
                preItems.extend(tree)
            else:
                postItems.extend(tree)
                
        return preItems + rootItem + postItems

    def render(self, painter, targetRect, sourceRect, item=None):
    
        #if item is None:
            #item = self.item
        #preItems = []
        #postItems = []
        #if isinstance(item, QtGui.QGraphicsScene):
            #childs = [i for i in item.items() if i.parentItem() is None]
            #rootItem = []
        #else:
            #childs = item.childItems()
            #rootItem = [item]
        #childs.sort(lambda a,b: cmp(a.zValue(), b.zValue()))
        #while len(childs) > 0:
            #ch = childs.pop(0)
            #if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or (ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0):
                #preItems.extend(tree)
            #else:
                #postItems.extend(tree)
                
        #for ch in preItems:
            #self.render(painter, sourceRect, targetRect, item=ch)
        ### paint root here
        #for ch in postItems:
            #self.render(painter, sourceRect, targetRect, item=ch)
        
    
        self.getScene().render(painter, QtCore.QRectF(targetRect), QtCore.QRectF(sourceRect))
Exemplo n.º 14
0
class Exporter(object):
    """
    Abstract class used for exporting graphics to file / printer / whatever.
    """
    def __init__(self, item):
        """
        Initialize with the item to be exported.
        Can be an individual graphics item or a scene.
        """
        object.__init__(self)
        self.item = item

    def item(self):
        return self.item

    def parameters(self):
        """Return the parameters used to configure this exporter."""
        raise Exception("Abstract method must be overridden in subclass.")

    def export(self):
        """"""
        raise Exception("Abstract method must be overridden in subclass.")

    def fileSaveDialog(self, filter=None, opts=None):
        ## Show a file dialog, call self.export(fileName) when finished.
        if opts is None:
            opts = {}
        self.fileDialog = FileDialog()
        self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
        self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        if filter is not None:
            if isinstance(filter, basestring):
                self.fileDialog.setNameFilter(filter)
            elif isinstance(filter, list):
                self.fileDialog.setNameFilters(filter)
        global LastExportDirectory
        exportDir = LastExportDirectory
        if exportDir is not None:
            self.fileDialog.setDirectory(exportDir)
        self.fileDialog.show()
        self.fileDialog.opts = opts
        self.fileDialog.fileSelected.connect(self.fileSaveFinished)
        return

    def fileSaveFinished(self, fileName):
        fileName = str(fileName)
        global LastExportDirectory
        LastExportDirectory = os.path.split(fileName)[0]
        self.export(fileName=fileName, **self.fileDialog.opts)

    def getScene(self):
        if isinstance(self.item, pg.GraphicsScene):
            return self.item
        else:
            return self.item.scene()

    def getSourceRect(self):
        if isinstance(self.item, pg.GraphicsScene):
            return self.item.getViewWidget().viewRect()
        else:
            return self.item.sceneBoundingRect()

    def getTargetRect(self):
        if isinstance(self.item, pg.GraphicsScene):
            return self.item.getViewWidget().rect()
        else:
            return self.item.mapRectToDevice(self.item.boundingRect())

    def setExportMode(self, export, opts=None):
        """
        Call setExportMode(export, opts) on all items that will 
        be painted during the export. This informs the item
        that it is about to be painted for export, allowing it to 
        alter its appearance temporarily
        
        
        *export*  - bool; must be True before exporting and False afterward
        *opts*    - dict; common parameters are 'antialias' and 'background'
        """
        if opts is None:
            opts = {}
        for item in self.getPaintItems():
            if hasattr(item, 'setExportMode'):
                item.setExportMode(export, opts)

    def getPaintItems(self, root=None):
        """Return a list of all items that should be painted in the correct order."""
        if root is None:
            root = self.item
        preItems = []
        postItems = []
        if isinstance(root, QtGui.QGraphicsScene):
            childs = [i for i in root.items() if i.parentItem() is None]
            rootItem = []
        else:
            childs = root.childItems()
            rootItem = [root]
        childs.sort(lambda a, b: cmp(a.zValue(), b.zValue()))
        while len(childs) > 0:
            ch = childs.pop(0)
            tree = self.getPaintItems(ch)
            if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or (
                    ch.zValue() < 0 and
                    int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0):
                preItems.extend(tree)
            else:
                postItems.extend(tree)

        return preItems + rootItem + postItems

    def render(self, painter, targetRect, sourceRect, item=None):

        #if item is None:
        #item = self.item
        #preItems = []
        #postItems = []
        #if isinstance(item, QtGui.QGraphicsScene):
        #childs = [i for i in item.items() if i.parentItem() is None]
        #rootItem = []
        #else:
        #childs = item.childItems()
        #rootItem = [item]
        #childs.sort(lambda a,b: cmp(a.zValue(), b.zValue()))
        #while len(childs) > 0:
        #ch = childs.pop(0)
        #if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or (ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0):
        #preItems.extend(tree)
        #else:
        #postItems.extend(tree)

        #for ch in preItems:
        #self.render(painter, sourceRect, targetRect, item=ch)
        ### paint root here
        #for ch in postItems:
        #self.render(painter, sourceRect, targetRect, item=ch)

        self.getScene().render(painter, QtCore.QRectF(targetRect),
                               QtCore.QRectF(sourceRect))
Exemplo n.º 15
0
class PlotItem(GraphicsWidget):
    
    """
    **Bases:** :class:`GraphicsWidget <pyqtgraph.GraphicsWidget>`
    
    Plot graphics item that can be added to any graphics scene. Implements axes, titles, and interactive viewbox. 
    PlotItem also provides some basic analysis functionality that may be accessed from the context menu.
    Use :func:`plot() <pyqtgraph.PlotItem.plot>` to create a new PlotDataItem and add it to the view.
    Use :func:`addItem() <pyqtgraph.PlotItem.addItem>` to add any QGraphicsItem to the view.
    
    This class wraps several methods from its internal ViewBox:
    :func:`setXRange <pyqtgraph.ViewBox.setXRange>`,
    :func:`setYRange <pyqtgraph.ViewBox.setYRange>`,
    :func:`setRange <pyqtgraph.ViewBox.setRange>`,
    :func:`autoRange <pyqtgraph.ViewBox.autoRange>`,
    :func:`setXLink <pyqtgraph.ViewBox.setXLink>`,
    :func:`setYLink <pyqtgraph.ViewBox.setYLink>`,
    :func:`setAutoPan <pyqtgraph.ViewBox.setAutoPan>`,
    :func:`setAutoVisible <pyqtgraph.ViewBox.setAutoVisible>`,
    :func:`viewRect <pyqtgraph.ViewBox.viewRect>`,
    :func:`viewRange <pyqtgraph.ViewBox.viewRange>`,
    :func:`setMouseEnabled <pyqtgraph.ViewBox.setMouseEnabled>`,
    :func:`enableAutoRange <pyqtgraph.ViewBox.enableAutoRange>`,
    :func:`disableAutoRange <pyqtgraph.ViewBox.disableAutoRange>`,
    :func:`setAspectLocked <pyqtgraph.ViewBox.setAspectLocked>`,
    :func:`invertY <pyqtgraph.ViewBox.invertY>`,
    :func:`register <pyqtgraph.ViewBox.register>`,
    :func:`unregister <pyqtgraph.ViewBox.unregister>`
    
    The ViewBox itself can be accessed by calling :func:`getViewBox() <pyqtgraph.PlotItem.getViewBox>` 
    
    ==================== =======================================================================
    **Signals**
    sigYRangeChanged     wrapped from :class:`ViewBox <pyqtgraph.ViewBox>`
    sigXRangeChanged     wrapped from :class:`ViewBox <pyqtgraph.ViewBox>`
    sigRangeChanged      wrapped from :class:`ViewBox <pyqtgraph.ViewBox>`
    ==================== =======================================================================
    """
    
    sigRangeChanged = QtCore.Signal(object, object)    ## Emitted when the ViewBox range has changed
    sigYRangeChanged = QtCore.Signal(object, object)   ## Emitted when the ViewBox Y range has changed
    sigXRangeChanged = QtCore.Signal(object, object)   ## Emitted when the ViewBox X range has changed
    
    
    lastFileDir = None
    managers = {}
    
    def __init__(self, parent=None, name=None, labels=None, title=None, viewBox=None, axisItems=None, enableMenu=True, **kargs):
        """
        Create a new PlotItem. All arguments are optional.
        Any extra keyword arguments are passed to PlotItem.plot().
        
        ==============  ==========================================================================================
        **Arguments**
        *title*         Title to display at the top of the item. Html is allowed.
        *labels*        A dictionary specifying the axis labels to display::
                   
                            {'left': (args), 'bottom': (args), ...}
                     
                        The name of each axis and the corresponding arguments are passed to 
                        :func:`PlotItem.setLabel() <pyqtgraph.PlotItem.setLabel>`
                        Optionally, PlotItem my also be initialized with the keyword arguments left,
                        right, top, or bottom to achieve the same effect.
        *name*          Registers a name for this view so that others may link to it
        *viewBox*       If specified, the PlotItem will be constructed with this as its ViewBox.
        *axisItems*     Optional dictionary instructing the PlotItem to use pre-constructed items
                        for its axes. The dict keys must be axis names ('left', 'bottom', 'right', 'top')
                        and the values must be instances of AxisItem (or at least compatible with AxisItem).
        ==============  ==========================================================================================
        """
        
        GraphicsWidget.__init__(self, parent)
        
        self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        
        ## Set up control buttons
        path = os.path.dirname(__file__)
        #self.autoImageFile = os.path.join(path, 'auto.png')
        #self.lockImageFile = os.path.join(path, 'lock.png')
        self.autoBtn = ButtonItem(pyqtgraph.pixmaps.getPixmap('auto'), 14, self)
        self.autoBtn.mode = 'auto'
        self.autoBtn.clicked.connect(self.autoBtnClicked)
        #self.autoBtn.hide()
        self.buttonsHidden = False ## whether the user has requested buttons to be hidden
        self.mouseHovering = False
        
        self.layout = QtGui.QGraphicsGridLayout()
        self.layout.setContentsMargins(1,1,1,1)
        self.setLayout(self.layout)
        self.layout.setHorizontalSpacing(0)
        self.layout.setVerticalSpacing(0)
        
        if viewBox is None:
            viewBox = ViewBox()
        self.vb = viewBox
        self.vb.sigStateChanged.connect(self.viewStateChanged)
        self.setMenuEnabled(enableMenu, enableMenu) ## en/disable plotitem and viewbox menus
        
        if name is not None:
            self.vb.register(name)
        self.vb.sigRangeChanged.connect(self.sigRangeChanged)
        self.vb.sigXRangeChanged.connect(self.sigXRangeChanged)
        self.vb.sigYRangeChanged.connect(self.sigYRangeChanged)
        
        self.layout.addItem(self.vb, 2, 1)
        self.alpha = 1.0
        self.autoAlpha = True
        self.spectrumMode = False
        
        self.legend = None
        
        ## Create and place axis items
        if axisItems is None:
            axisItems = {}
        self.axes = {}
        for k, pos in (('top', (1,1)), ('bottom', (3,1)), ('left', (2,0)), ('right', (2,2))):
            axis = axisItems.get(k, AxisItem(orientation=k))
            axis.linkToView(self.vb)
            self.axes[k] = {'item': axis, 'pos': pos}
            self.layout.addItem(axis, *pos)
            axis.setZValue(-1000)
            axis.setFlag(axis.ItemNegativeZStacksBehindParent)
        
        self.titleLabel = LabelItem('', size='11pt')
        self.layout.addItem(self.titleLabel, 0, 1)
        self.setTitle(None)  ## hide
        
        
        for i in range(4):
            self.layout.setRowPreferredHeight(i, 0)
            self.layout.setRowMinimumHeight(i, 0)
            self.layout.setRowSpacing(i, 0)
            self.layout.setRowStretchFactor(i, 1)
            
        for i in range(3):
            self.layout.setColumnPreferredWidth(i, 0)
            self.layout.setColumnMinimumWidth(i, 0)
            self.layout.setColumnSpacing(i, 0)
            self.layout.setColumnStretchFactor(i, 1)
        self.layout.setRowStretchFactor(2, 100)
        self.layout.setColumnStretchFactor(1, 100)
        

        ## Wrap a few methods from viewBox
        for m in [
            'setXRange', 'setYRange', 'setXLink', 'setYLink', 'setAutoPan', 'setAutoVisible',
            'setRange', 'autoRange', 'viewRect', 'viewRange', 'setMouseEnabled',
            'enableAutoRange', 'disableAutoRange', 'setAspectLocked', 'invertY',
            'register', 'unregister']:  ## NOTE: If you update this list, please update the class docstring as well.
            setattr(self, m, getattr(self.vb, m))
            
        self.items = []
        self.curves = []
        self.itemMeta = weakref.WeakKeyDictionary()
        self.dataItems = []
        self.paramList = {}
        self.avgCurves = {}
        
        ### Set up context menu
        
        w = QtGui.QWidget()
        self.ctrl = c = Ui_Form()
        c.setupUi(w)
        dv = QtGui.QDoubleValidator(self)
        
        menuItems = [
            ('Transforms', c.transformGroup),
            ('Downsample', c.decimateGroup),
            ('Average', c.averageGroup),
            ('Alpha', c.alphaGroup),
            ('Grid', c.gridGroup),
            ('Points', c.pointsGroup),
        ]
        
        
        self.ctrlMenu = QtGui.QMenu()
        
        self.ctrlMenu.setTitle('Plot Options')
        self.subMenus = []
        for name, grp in menuItems:
            sm = QtGui.QMenu(name)
            act = QtGui.QWidgetAction(self)
            act.setDefaultWidget(grp)
            sm.addAction(act)
            self.subMenus.append(sm)
            self.ctrlMenu.addMenu(sm)
        
        self.stateGroup = WidgetGroup()
        for name, w in menuItems:
            self.stateGroup.autoAdd(w)
        
        self.fileDialog = None
        
        c.alphaGroup.toggled.connect(self.updateAlpha)
        c.alphaSlider.valueChanged.connect(self.updateAlpha)
        c.autoAlphaCheck.toggled.connect(self.updateAlpha)

        c.xGridCheck.toggled.connect(self.updateGrid)
        c.yGridCheck.toggled.connect(self.updateGrid)
        c.gridAlphaSlider.valueChanged.connect(self.updateGrid)

        c.fftCheck.toggled.connect(self.updateSpectrumMode)
        c.logXCheck.toggled.connect(self.updateLogMode)
        c.logYCheck.toggled.connect(self.updateLogMode)

        c.downsampleSpin.valueChanged.connect(self.updateDownsampling)

        self.ctrl.avgParamList.itemClicked.connect(self.avgParamListClicked)
        self.ctrl.averageGroup.toggled.connect(self.avgToggled)
        
        self.ctrl.maxTracesCheck.toggled.connect(self.updateDecimation)
        self.ctrl.maxTracesSpin.valueChanged.connect(self.updateDecimation)
        
        self.hideAxis('right')
        self.hideAxis('top')
        self.showAxis('left')
        self.showAxis('bottom')
        
        if labels is None:
            labels = {}
        for label in list(self.axes.keys()):
            if label in kargs:
                labels[label] = kargs[label]
                del kargs[label]
        for k in labels:
            if isinstance(labels[k], basestring):
                labels[k] = (labels[k],)
            self.setLabel(k, *labels[k])
                
        if title is not None:
            self.setTitle(title)
        
        if len(kargs) > 0:
            self.plot(**kargs)
        
        
    def implements(self, interface=None):
        return interface in ['ViewBoxWrapper']

    def getViewBox(self):
        """Return the :class:`ViewBox <pyqtgraph.ViewBox>` contained within."""
        return self.vb
    
    
    
    def setLogMode(self, x, y):
        """
        Set log scaling for x and y axes.
        This informs PlotDataItems to transform logarithmically and switches
        the axes to use log ticking. 
        
        Note that *no other items* in the scene will be affected by
        this; there is no generic way to redisplay a GraphicsItem
        with log coordinates.
        
        """
        self.ctrl.logXCheck.setChecked(x)
        self.ctrl.logYCheck.setChecked(y)
        
    def showGrid(self, x=None, y=None, alpha=None):
        """
        Show or hide the grid for either axis.
        
        ==============  =====================================
        **Arguments:**
        x               (bool) Whether to show the X grid
        y               (bool) Whether to show the Y grid
        alpha           (0.0-1.0) Opacity of the grid
        ==============  =====================================
        """
        if x is None and y is None and alpha is None:
            raise Exception("Must specify at least one of x, y, or alpha.")  ## prevent people getting confused if they just call showGrid()
        
        if x is not None:
            self.ctrl.xGridCheck.setChecked(x)
        if y is not None:
            self.ctrl.yGridCheck.setChecked(y)
        if alpha is not None:
            v = np.clip(alpha, 0, 1)*self.ctrl.gridAlphaSlider.maximum()
            self.ctrl.gridAlphaSlider.setValue(v)
        
    #def paint(self, *args):
        #prof = debug.Profiler('PlotItem.paint', disabled=True)
        #QtGui.QGraphicsWidget.paint(self, *args)
        #prof.finish()
        
    ## bad idea. 
    #def __getattr__(self, attr):  ## wrap ms
        #return getattr(self.vb, attr)
        
    def close(self):
        #print "delete", self
        ## Most of this crap is needed to avoid PySide trouble. 
        ## The problem seems to be whenever scene.clear() leads to deletion of widgets (either through proxies or qgraphicswidgets)
        ## the solution is to manually remove all widgets before scene.clear() is called
        if self.ctrlMenu is None: ## already shut down
            return
        self.ctrlMenu.setParent(None)
        self.ctrlMenu = None
        
        #self.ctrlBtn.setParent(None)
        #self.ctrlBtn = None
        #self.autoBtn.setParent(None)
        #self.autoBtn = None
        
        for k in self.axes:
            i = self.axes[k]['item']
            i.close()
            
        self.axes = None
        self.scene().removeItem(self.vb)
        self.vb = None
        
        ## causes invalid index errors:
        #for i in range(self.layout.count()):
            #self.layout.removeAt(i)
            
        #for p in self.proxies:
            #try:
                #p.setWidget(None)
            #except RuntimeError:
                #break
            #self.scene().removeItem(p)
        #self.proxies = []
        
        #self.menuAction.releaseWidget(self.menuAction.defaultWidget())
        #self.menuAction.setParent(None)
        #self.menuAction = None
        
        #if self.manager is not None:
            #self.manager.sigWidgetListChanged.disconnect(self.updatePlotList)
            #self.manager.removeWidget(self.name)
        #else:
            #print "no manager"

    def registerPlot(self, name):   ## for backward compatibility
        self.vb.register(name)
        
    def updateGrid(self, *args):
        alpha = self.ctrl.gridAlphaSlider.value()
        x = alpha if self.ctrl.xGridCheck.isChecked() else False
        y = alpha if self.ctrl.yGridCheck.isChecked() else False
        self.getAxis('top').setGrid(x)
        self.getAxis('bottom').setGrid(x)
        self.getAxis('left').setGrid(y)
        self.getAxis('right').setGrid(y)

    def viewGeometry(self):
        """Return the screen geometry of the viewbox"""
        v = self.scene().views()[0]
        b = self.vb.mapRectToScene(self.vb.boundingRect())
        wr = v.mapFromScene(b).boundingRect()
        pos = v.mapToGlobal(v.pos())
        wr.adjust(pos.x(), pos.y(), pos.x(), pos.y())
        return wr


    def avgToggled(self, b):
        if b:
            self.recomputeAverages()
        for k in self.avgCurves:
            self.avgCurves[k][1].setVisible(b)
        
    def avgParamListClicked(self, item):
        name = str(item.text())
        self.paramList[name] = (item.checkState() == QtCore.Qt.Checked)
        self.recomputeAverages()
        
    def recomputeAverages(self):
        if not self.ctrl.averageGroup.isChecked():
            return
        for k in self.avgCurves:
            self.removeItem(self.avgCurves[k][1])
        self.avgCurves = {}
        for c in self.curves:
            self.addAvgCurve(c)
        self.replot()
        
    def addAvgCurve(self, curve):
        ## Add a single curve into the pool of curves averaged together
        
        ## If there are plot parameters, then we need to determine which to average together.
        remKeys = []
        addKeys = []
        if self.ctrl.avgParamList.count() > 0:
        
            ### First determine the key of the curve to which this new data should be averaged
            for i in range(self.ctrl.avgParamList.count()):
                item = self.ctrl.avgParamList.item(i)
                if item.checkState() == QtCore.Qt.Checked:
                    remKeys.append(str(item.text()))
                else:
                    addKeys.append(str(item.text()))
                    
            if len(remKeys) < 1:  ## In this case, there would be 1 average plot for each data plot; not useful.
                return
                
        p = self.itemMeta.get(curve,{}).copy()
        for k in p:
            if type(k) is tuple:
                p['.'.join(k)] = p[k]
                del p[k]
        for rk in remKeys:
            if rk in p:
                del p[rk]
        for ak in addKeys:
            if ak not in p:
                p[ak] = None
        key = tuple(p.items())
        
        ### Create a new curve if needed
        if key not in self.avgCurves:
            plot = PlotDataItem()
            plot.setPen(fn.mkPen([0, 200, 0]))
            plot.setShadowPen(fn.mkPen([0, 0, 0, 100], width=3))
            plot.setAlpha(1.0, False)
            plot.setZValue(100)
            self.addItem(plot, skipAverage=True)
            self.avgCurves[key] = [0, plot]
        self.avgCurves[key][0] += 1
        (n, plot) = self.avgCurves[key]
        
        ### Average data together
        (x, y) = curve.getData()
        if plot.yData is not None:
            newData = plot.yData * (n-1) / float(n) + y * 1.0 / float(n)
            plot.setData(plot.xData, newData)
        else:
            plot.setData(x, y)
        
    def autoBtnClicked(self):
        if self.autoBtn.mode == 'auto':
            self.enableAutoRange()
            self.autoBtn.hide()
        else:
            self.disableAutoRange()
            
    def viewStateChanged(self):
        self.updateButtons()
            
    def enableAutoScale(self):
        """
        Enable auto-scaling. The plot will continuously scale to fit the boundaries of its data.
        """
        print("Warning: enableAutoScale is deprecated. Use enableAutoRange(axis, enable) instead.")
        self.vb.enableAutoRange(self.vb.XYAxes)

    def addItem(self, item, *args, **kargs):
        """
        Add a graphics item to the view box. 
        If the item has plot data (PlotDataItem, PlotCurveItem, ScatterPlotItem), it may
        be included in analysis performed by the PlotItem.
        """
        self.items.append(item)
        vbargs = {}
        if 'ignoreBounds' in kargs:
            vbargs['ignoreBounds'] = kargs['ignoreBounds']
        self.vb.addItem(item, *args, **vbargs)
        if hasattr(item, 'implements') and item.implements('plotData'):
            self.dataItems.append(item)
            #self.plotChanged()
            
            params = kargs.get('params', {})
            self.itemMeta[item] = params
            #item.setMeta(params)
            self.curves.append(item)
            #self.addItem(c)
            
        if hasattr(item, 'setLogMode'):
            item.setLogMode(self.ctrl.logXCheck.isChecked(), self.ctrl.logYCheck.isChecked())
            
        if isinstance(item, PlotDataItem):
            ## configure curve for this plot
            (alpha, auto) = self.alphaState()
            item.setAlpha(alpha, auto)
            item.setFftMode(self.ctrl.fftCheck.isChecked())
            item.setDownsampling(self.downsampleMode())
            item.setPointMode(self.pointMode())
            
            ## Hide older plots if needed
            self.updateDecimation()
            
            ## Add to average if needed
            self.updateParamList()
            if self.ctrl.averageGroup.isChecked() and 'skipAverage' not in kargs:
                self.addAvgCurve(item)
                
            #c.connect(c, QtCore.SIGNAL('plotChanged'), self.plotChanged)
            #item.sigPlotChanged.connect(self.plotChanged)
            #self.plotChanged()
        name = kargs.get('name', getattr(item, 'opts', {}).get('name', None))
        if name is not None and hasattr(self, 'legend') and self.legend is not None:
            self.legend.addItem(item, name=name)
            

    def addDataItem(self, item, *args):
        print("PlotItem.addDataItem is deprecated. Use addItem instead.")
        self.addItem(item, *args)
        
    def addCurve(self, c, params=None):
        print("PlotItem.addCurve is deprecated. Use addItem instead.")
        self.addItem(c, params)

    def removeItem(self, item):
        """
        Remove an item from the internal ViewBox.
        """
        if not item in self.items:
            return
        self.items.remove(item)
        if item in self.dataItems:
            self.dataItems.remove(item)
            
        if item.scene() is not None:
            self.vb.removeItem(item)
        if item in self.curves:
            self.curves.remove(item)
            self.updateDecimation()
            self.updateParamList()
            #item.connect(item, QtCore.SIGNAL('plotChanged'), self.plotChanged)
            #item.sigPlotChanged.connect(self.plotChanged)

    def clear(self):
        """
        Remove all items from the ViewBox.
        """
        for i in self.items[:]:
            self.removeItem(i)
        self.avgCurves = {}
    
    def clearPlots(self):
        for i in self.curves[:]:
            self.removeItem(i)
        self.avgCurves = {}
        
    
    def plot(self, *args, **kargs):
        """
        Add and return a new plot.
        See :func:`PlotDataItem.__init__ <pyqtgraph.PlotDataItem.__init__>` for data arguments
        
        Extra allowed arguments are:
            clear    - clear all plots before displaying new data
            params   - meta-parameters to associate with this data
        """
        
        
        clear = kargs.get('clear', False)
        params = kargs.get('params', None)
          
        if clear:
            self.clear()
            
        item = PlotDataItem(*args, **kargs)
            
        if params is None:
            params = {}
        self.addItem(item, params=params)
        
        return item

    def addLegend(self, size=None, offset=(30, 30)):
        """
        Create a new LegendItem and anchor it over the internal ViewBox.
        Plots will be automatically displayed in the legend if they
        are created with the 'name' argument.
        """
        self.legend = LegendItem(size, offset)
        self.legend.setParentItem(self.vb)
        return self.legend
        
    def scatterPlot(self, *args, **kargs):
        if 'pen' in kargs:
            kargs['symbolPen'] = kargs['pen']
        kargs['pen'] = None
            
        if 'brush' in kargs:
            kargs['symbolBrush'] = kargs['brush']
            del kargs['brush']
            
        if 'size' in kargs:
            kargs['symbolSize'] = kargs['size']
            del kargs['size']

        return self.plot(*args, **kargs)
                
    def replot(self):
        self.update()

    def updateParamList(self):
        self.ctrl.avgParamList.clear()
        ## Check to see that each parameter for each curve is present in the list
        for c in self.curves:
            for p in list(self.itemMeta.get(c, {}).keys()):
                if type(p) is tuple:
                    p = '.'.join(p)
                    
                ## If the parameter is not in the list, add it.
                matches = self.ctrl.avgParamList.findItems(p, QtCore.Qt.MatchExactly)
                if len(matches) == 0:
                    i = QtGui.QListWidgetItem(p)
                    if p in self.paramList and self.paramList[p] is True:
                        i.setCheckState(QtCore.Qt.Checked)
                    else:
                        i.setCheckState(QtCore.Qt.Unchecked)
                    self.ctrl.avgParamList.addItem(i)
                else:
                    i = matches[0]
                    
                self.paramList[p] = (i.checkState() == QtCore.Qt.Checked)


    ## Qt's SVG-writing capabilities are pretty terrible. 
    def writeSvgCurves(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeSvg)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        if isinstance(fileName, tuple):
            raise Exception("Not implemented yet..")
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        rect = self.vb.viewRect()
        xRange = rect.left(), rect.right() 
        
        svg = ""
        fh = open(fileName, 'w')

        dx = max(rect.right(),0) - min(rect.left(),0)
        ymn = min(rect.top(), rect.bottom())
        ymx = max(rect.top(), rect.bottom())
        dy = max(ymx,0) - min(ymn,0)
        sx = 1.
        sy = 1.
        while dx*sx < 10:
            sx *= 1000
        while dy*sy < 10:
            sy *= 1000
        sy *= -1

        #fh.write('<svg viewBox="%f %f %f %f">\n' % (rect.left()*sx, rect.top()*sx, rect.width()*sy, rect.height()*sy))
        fh.write('<svg>\n')
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M%f,0 L%f,0"/>\n' % (rect.left()*sx, rect.right()*sx))
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M0,%f L0,%f"/>\n' % (rect.top()*sy, rect.bottom()*sy))


        for item in self.curves:
            if isinstance(item, PlotCurveItem):
                color = fn.colorStr(item.pen.color())
                opacity = item.pen.color().alpha() / 255.
                color = color[:6]
                x, y = item.getData()
                mask = (x > xRange[0]) * (x < xRange[1])
                mask[:-1] += mask[1:]
                m2 = mask.copy()
                mask[1:] += m2[:-1]
                x = x[mask]
                y = y[mask]
                
                x *= sx
                y *= sy
                
                #fh.write('<g fill="none" stroke="#%s" stroke-opacity="1" stroke-width="1">\n' % color)
                fh.write('<path fill="none" stroke="#%s" stroke-opacity="%f" stroke-width="1" d="M%f,%f ' % (color, opacity, x[0], y[0]))
                for i in range(1, len(x)):
                    fh.write('L%f,%f ' % (x[i], y[i]))
                
                fh.write('"/>')
                #fh.write("</g>")
        for item in self.dataItems:
            if isinstance(item, ScatterPlotItem):
                
                pRect = item.boundingRect()
                vRect = pRect.intersected(rect)
                
                for point in item.points():
                    pos = point.pos()
                    if not rect.contains(pos):
                        continue
                    color = fn.colorStr(point.brush.color())
                    opacity = point.brush.color().alpha() / 255.
                    color = color[:6]
                    x = pos.x() * sx
                    y = pos.y() * sy
                    
                    fh.write('<circle cx="%f" cy="%f" r="1" fill="#%s" stroke="none" fill-opacity="%f"/>\n' % (x, y, color, opacity))
                    #fh.write('<path fill="none" stroke="#%s" stroke-opacity="%f" stroke-width="1" d="M%f,%f ' % (color, opacity, x[0], y[0]))
                    #for i in xrange(1, len(x)):
                        #fh.write('L%f,%f ' % (x[i], y[i]))
                    
                    #fh.write('"/>')
            
        ## get list of curves, scatter plots
        
        
        fh.write("</svg>\n")
        
        
    
    def writeSvg(self, fileName=None):
        if fileName is None:
            fileName = QtGui.QFileDialog.getSaveFileName()
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        self.svg = QtSvg.QSvgGenerator()
        self.svg.setFileName(fileName)
        res = 120.
        view = self.scene().views()[0]
        bounds = view.viewport().rect()
        bounds = QtCore.QRectF(0, 0, bounds.width(), bounds.height())
        
        self.svg.setResolution(res)
        self.svg.setViewBox(bounds)
        
        self.svg.setSize(QtCore.QSize(bounds.width(), bounds.height()))
        
        painter = QtGui.QPainter(self.svg)
        view.render(painter, bounds)
        
        painter.end()
        
        ## Workaround to set pen widths correctly
        import re
        data = open(fileName).readlines()
        for i in range(len(data)):
            line = data[i]
            m = re.match(r'(<g .*)stroke-width="1"(.*transform="matrix\(([^\)]+)\)".*)', line)
            if m is not None:
                #print "Matched group:", line
                g = m.groups()
                matrix = list(map(float, g[2].split(',')))
                #print "matrix:", matrix
                scale = max(abs(matrix[0]), abs(matrix[3]))
                if scale == 0 or scale == 1.0:
                    continue
                data[i] = g[0] + ' stroke-width="%0.2g" ' % (1.0/scale) + g[1] + '\n'
                #print "old line:", line
                #print "new line:", data[i]
        open(fileName, 'w').write(''.join(data))
        
        
    def writeImage(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeImage)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        if isinstance(fileName, tuple):
            raise Exception("Not implemented yet..")
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        self.png = QtGui.QImage(int(self.size().width()), int(self.size().height()), QtGui.QImage.Format_ARGB32)
        painter = QtGui.QPainter(self.png)
        painter.setRenderHints(painter.Antialiasing | painter.TextAntialiasing)
        self.scene().render(painter, QtCore.QRectF(), self.mapRectToScene(self.boundingRect()))
        painter.end()
        self.png.save(fileName)
        
    def writeCsv(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeCsv)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        fd = open(fileName, 'w')
        data = [c.getData() for c in self.curves]
        i = 0
        while True:
            done = True
            for d in data:
                if i < len(d[0]):
                    fd.write('%g,%g,'%(d[0][i], d[1][i]))
                    done = False
                else:
                    fd.write(' , ,')
            fd.write('\n')
            if done:
                break
            i += 1
        fd.close()


    def saveState(self):
        state = self.stateGroup.state()
        state['paramList'] = self.paramList.copy()
        state['view'] = self.vb.getState()
        return state
        
    def restoreState(self, state):
        if 'paramList' in state:
            self.paramList = state['paramList'].copy()
            
        self.stateGroup.setState(state)
        self.updateSpectrumMode()
        self.updateDownsampling()
        self.updateAlpha()
        self.updateDecimation()
        
        if 'powerSpectrumGroup' in state:
            state['fftCheck'] = state['powerSpectrumGroup']
        if 'gridGroup' in state:
            state['xGridCheck'] = state['gridGroup']
            state['yGridCheck'] = state['gridGroup']
            
        self.stateGroup.setState(state)
        self.updateParamList()
        
        if 'view' not in state:
            r = [[float(state['xMinText']), float(state['xMaxText'])], [float(state['yMinText']), float(state['yMaxText'])]]
            state['view'] = {
                'autoRange': [state['xAutoRadio'], state['yAutoRadio']],
                'linkedViews': [state['xLinkCombo'], state['yLinkCombo']],
                'targetRange': r,
                'viewRange': r,
            }
        self.vb.setState(state['view'])
        

    def widgetGroupInterface(self):
        return (None, PlotItem.saveState, PlotItem.restoreState)
      
    def updateSpectrumMode(self, b=None):
        if b is None:
            b = self.ctrl.fftCheck.isChecked()
        for c in self.curves:
            c.setFftMode(b)
        self.enableAutoRange()
        self.recomputeAverages()
            
    def updateLogMode(self):
        x = self.ctrl.logXCheck.isChecked()
        y = self.ctrl.logYCheck.isChecked()
        for i in self.items:
            if hasattr(i, 'setLogMode'):
                i.setLogMode(x,y)
        self.getAxis('bottom').setLogMode(x)
        self.getAxis('top').setLogMode(x)
        self.getAxis('left').setLogMode(y)
        self.getAxis('right').setLogMode(y)
        self.enableAutoRange()
        self.recomputeAverages()
        
        
    def updateDownsampling(self):
        ds = self.downsampleMode()
        for c in self.curves:
            c.setDownsampling(ds)
        self.recomputeAverages()
        
        
    def downsampleMode(self):
        if self.ctrl.decimateGroup.isChecked():
            if self.ctrl.manualDecimateRadio.isChecked():
                ds = self.ctrl.downsampleSpin.value()
            else:
                ds = True
        else:
            ds = False
        return ds
        
    def updateDecimation(self):
        if self.ctrl.maxTracesCheck.isChecked():
            numCurves = self.ctrl.maxTracesSpin.value()
        else:
            numCurves = -1
            
        curves = self.curves[:]
        split = len(curves) - numCurves
        for i in range(len(curves)):
            if numCurves == -1 or i >= split:
                curves[i].show()
            else:
                if self.ctrl.forgetTracesCheck.isChecked():
                    curves[i].clear()
                    self.removeItem(curves[i])
                else:
                    curves[i].hide()
        
      
    def updateAlpha(self, *args):
        (alpha, auto) = self.alphaState()
        for c in self.curves:
            c.setAlpha(alpha**2, auto)
     
    def alphaState(self):
        enabled = self.ctrl.alphaGroup.isChecked()
        auto = self.ctrl.autoAlphaCheck.isChecked()
        alpha = float(self.ctrl.alphaSlider.value()) / self.ctrl.alphaSlider.maximum()
        if auto:
            alpha = 1.0  ## should be 1/number of overlapping plots
        if not enabled:
            auto = False
            alpha = 1.0
        return (alpha, auto)

    def pointMode(self):
        if self.ctrl.pointsGroup.isChecked():
            if self.ctrl.autoPointsCheck.isChecked():
                mode = None
            else:
                mode = True
        else:
            mode = False
        return mode
        

    def resizeEvent(self, ev):
        if self.autoBtn is None:  ## already closed down
            return
        btnRect = self.mapRectFromItem(self.autoBtn, self.autoBtn.boundingRect())
        y = self.size().height() - btnRect.height()
        self.autoBtn.setPos(0, y)
    
    
    def getMenu(self):
        return self.ctrlMenu
    
    def getContextMenus(self, event):
        ## called when another item is displaying its context menu; we get to add extras to the end of the menu.
        if self.menuEnabled():
            return self.ctrlMenu
        else:
            return None
    
    def setMenuEnabled(self, enableMenu=True, enableViewBoxMenu='same'):
        """
        Enable or disable the context menu for this PlotItem.
        By default, the ViewBox's context menu will also be affected.
        (use enableViewBoxMenu=None to leave the ViewBox unchanged)
        """
        self._menuEnabled = enableMenu
        if enableViewBoxMenu is None:
            return
        if enableViewBoxMenu is 'same':
            enableViewBoxMenu = enableMenu 
        self.vb.setMenuEnabled(enableViewBoxMenu)
    
    def menuEnabled(self):
        return self._menuEnabled
    
    def hoverEvent(self, ev):
        if ev.enter:
            self.mouseHovering = True
        if ev.exit:
            self.mouseHovering = False
            
        self.updateButtons()
    

    def getLabel(self, key):
        pass
        
    def _checkScaleKey(self, key):
        if key not in self.axes:
            raise Exception("Scale '%s' not found. Scales are: %s" % (key, str(list(self.axes.keys()))))
        
    def getScale(self, key):
        return self.getAxis(key)
        
    def getAxis(self, name):
        """Return the specified AxisItem. 
        *name* should be 'left', 'bottom', 'top', or 'right'."""
        self._checkScaleKey(name)
        return self.axes[name]['item']
        
    def setLabel(self, axis, text=None, units=None, unitPrefix=None, **args):
        """
        Set the label for an axis. Basic HTML formatting is allowed.
        
        ============= =================================================================
        **Arguments**
        axis          must be one of 'left', 'bottom', 'right', or 'top'
        text          text to display along the axis. HTML allowed.
        units         units to display after the title. If units are given, 
                      then an SI prefix will be automatically appended
                      and the axis values will be scaled accordingly.
                      (ie, use 'V' instead of 'mV'; 'm' will be added automatically)
        ============= =================================================================
        """
        self.getAxis(axis).setLabel(text=text, units=units, **args)
        
    def showLabel(self, axis, show=True):
        """
        Show or hide one of the plot's axis labels (the axis itself will be unaffected).
        axis must be one of 'left', 'bottom', 'right', or 'top'
        """
        self.getScale(axis).showLabel(show)

    def setTitle(self, title=None, **args):
        """
        Set the title of the plot. Basic HTML formatting is allowed.
        If title is None, then the title will be hidden.
        """
        if title is None:
            self.titleLabel.setVisible(False)
            self.layout.setRowFixedHeight(0, 0)
            self.titleLabel.setMaximumHeight(0)
        else:
            self.titleLabel.setMaximumHeight(30)
            self.layout.setRowFixedHeight(0, 30)
            self.titleLabel.setVisible(True)
            self.titleLabel.setText(title, **args)

    def showAxis(self, axis, show=True):
        """
        Show or hide one of the plot's axes.
        axis must be one of 'left', 'bottom', 'right', or 'top'
        """
        s = self.getScale(axis)
        p = self.axes[axis]['pos']
        if show:
            s.show()
        else:
            s.hide()
            
    def hideAxis(self, axis):
        """Hide one of the PlotItem's axes. ('left', 'bottom', 'right', or 'top')"""
        self.showAxis(axis, False)
            
    def showScale(self, *args, **kargs):
        print("Deprecated. use showAxis() instead")
        return self.showAxis(*args, **kargs)
            
    def hideButtons(self):
        """Causes auto-scale button ('A' in lower-left corner) to be hidden for this PlotItem"""
        #self.ctrlBtn.hide()
        self.buttonsHidden = True
        self.updateButtons()
        
    def showButtons(self):
        """Causes auto-scale button ('A' in lower-left corner) to be visible for this PlotItem"""
        #self.ctrlBtn.hide()
        self.buttonsHidden = False
        self.updateButtons()
        
    def updateButtons(self):
        if self._exportOpts is False and self.mouseHovering and not self.buttonsHidden and not all(self.vb.autoRangeEnabled()):
            self.autoBtn.show()
        else:
            self.autoBtn.hide()
            
    def _plotArray(self, arr, x=None, **kargs):
        if arr.ndim != 1:
            raise Exception("Array must be 1D to plot (shape is %s)" % arr.shape)
        if x is None:
            x = np.arange(arr.shape[0])
        if x.ndim != 1:
            raise Exception("X array must be 1D to plot (shape is %s)" % x.shape)
        c = PlotCurveItem(arr, x=x, **kargs)
        return c
            
        
        
    def _plotMetaArray(self, arr, x=None, autoLabel=True, **kargs):
        inf = arr.infoCopy()
        if arr.ndim != 1:
            raise Exception('can only automatically plot 1 dimensional arrays.')
        ## create curve
        try:
            xv = arr.xvals(0)
        except:
            if x is None:
                xv = np.arange(arr.shape[0])
            else:
                xv = x
        c = PlotCurveItem(**kargs)
        c.setData(x=xv, y=arr.view(np.ndarray))
        
        if autoLabel:
            name = arr._info[0].get('name', None)
            units = arr._info[0].get('units', None)
            self.setLabel('bottom', text=name, units=units)
            
            name = arr._info[1].get('name', None)
            units = arr._info[1].get('units', None)
            self.setLabel('left', text=name, units=units)
            
        return c

      
    def setExportMode(self, export, opts=None):
        GraphicsWidget.setExportMode(self, export, opts)
        self.updateButtons()
Exemplo n.º 16
0
    def writeSvgCurves(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeSvg)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        if isinstance(fileName, tuple):
            raise Exception("Not implemented yet..")
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        rect = self.vb.viewRect()
        xRange = rect.left(), rect.right() 
        
        svg = ""
        fh = open(fileName, 'w')

        dx = max(rect.right(),0) - min(rect.left(),0)
        ymn = min(rect.top(), rect.bottom())
        ymx = max(rect.top(), rect.bottom())
        dy = max(ymx,0) - min(ymn,0)
        sx = 1.
        sy = 1.
        while dx*sx < 10:
            sx *= 1000
        while dy*sy < 10:
            sy *= 1000
        sy *= -1

        #fh.write('<svg viewBox="%f %f %f %f">\n' % (rect.left()*sx, rect.top()*sx, rect.width()*sy, rect.height()*sy))
        fh.write('<svg>\n')
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M%f,0 L%f,0"/>\n' % (rect.left()*sx, rect.right()*sx))
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M0,%f L0,%f"/>\n' % (rect.top()*sy, rect.bottom()*sy))


        for item in self.curves:
            if isinstance(item, PlotCurveItem):
                color = fn.colorStr(item.pen.color())
                opacity = item.pen.color().alpha() / 255.
                color = color[:6]
                x, y = item.getData()
                mask = (x > xRange[0]) * (x < xRange[1])
                mask[:-1] += mask[1:]
                m2 = mask.copy()
                mask[1:] += m2[:-1]
                x = x[mask]
                y = y[mask]
                
                x *= sx
                y *= sy
                
                #fh.write('<g fill="none" stroke="#%s" stroke-opacity="1" stroke-width="1">\n' % color)
                fh.write('<path fill="none" stroke="#%s" stroke-opacity="%f" stroke-width="1" d="M%f,%f ' % (color, opacity, x[0], y[0]))
                for i in range(1, len(x)):
                    fh.write('L%f,%f ' % (x[i], y[i]))
                
                fh.write('"/>')
                #fh.write("</g>")
        for item in self.dataItems:
            if isinstance(item, ScatterPlotItem):
                
                pRect = item.boundingRect()
                vRect = pRect.intersected(rect)
                
                for point in item.points():
                    pos = point.pos()
                    if not rect.contains(pos):
                        continue
                    color = fn.colorStr(point.brush.color())
                    opacity = point.brush.color().alpha() / 255.
                    color = color[:6]
                    x = pos.x() * sx
                    y = pos.y() * sy
                    
                    fh.write('<circle cx="%f" cy="%f" r="1" fill="#%s" stroke="none" fill-opacity="%f"/>\n' % (x, y, color, opacity))
                    #fh.write('<path fill="none" stroke="#%s" stroke-opacity="%f" stroke-width="1" d="M%f,%f ' % (color, opacity, x[0], y[0]))
                    #for i in xrange(1, len(x)):
                        #fh.write('L%f,%f ' % (x[i], y[i]))
                    
                    #fh.write('"/>')
            
        ## get list of curves, scatter plots
        
        
        fh.write("</svg>\n")
],
                  default="None")
exp_wg.setFixedSize(200, 30)
exp_wg_prev = exp_wg.currentText()

# Exit button
exit_wg = QtGui.QPushButton("Exit")
exit_wg.setFixedSize(200, 30)
exit_wg.setCheckable(True)
exit_wg.clicked.connect(lambda: gui.exit_wg_state(exit_wg, viewer_app))

# File chooser
file_button = QtGui.QPushButton("Open Image/Video")
file_button.setFixedSize(200, 30)
file_button.setCheckable(True)
file_chooser = FileDialog()
file_chooser.setFileMode(QtGui.QFileDialog.ExistingFile)
file_chooser.selectNameFilter("Image files (*.jpg *.jpeg *.png *.tiff)")
file_name = ""
file_name_prev = ""


def select_file():
    """select file."""
    global file_name
    file_name = file_chooser.getOpenFileName()


file_button.clicked.connect(select_file)

# Utitlity Layout
Exemplo n.º 18
0
class PlotItem(GraphicsWidget):
    
    """
    **Bases:** :class:`GraphicsWidget <pyqtgraph.GraphicsWidget>`
    
    Plot graphics item that can be added to any graphics scene. Implements axes, titles, and interactive viewbox. 
    PlotItem also provides some basic analysis functionality that may be accessed from the context menu.
    Use :func:`plot() <pyqtgraph.PlotItem.plot>` to create a new PlotDataItem and add it to the view.
    Use :func:`addItem() <pyqtgraph.PlotItem.addItem>` to add any QGraphicsItem to the view.
    
    This class wraps several methods from its internal ViewBox:
    :func:`setXRange <pyqtgraph.ViewBox.setXRange>`,
    :func:`setYRange <pyqtgraph.ViewBox.setYRange>`,
    :func:`setRange <pyqtgraph.ViewBox.setRange>`,
    :func:`autoRange <pyqtgraph.ViewBox.autoRange>`,
    :func:`setXLink <pyqtgraph.ViewBox.setXLink>`,
    :func:`setYLink <pyqtgraph.ViewBox.setYLink>`,
    :func:`setAutoPan <pyqtgraph.ViewBox.setAutoPan>`,
    :func:`setAutoVisible <pyqtgraph.ViewBox.setAutoVisible>`,
    :func:`viewRect <pyqtgraph.ViewBox.viewRect>`,
    :func:`viewRange <pyqtgraph.ViewBox.viewRange>`,
    :func:`setMouseEnabled <pyqtgraph.ViewBox.setMouseEnabled>`,
    :func:`enableAutoRange <pyqtgraph.ViewBox.enableAutoRange>`,
    :func:`disableAutoRange <pyqtgraph.ViewBox.disableAutoRange>`,
    :func:`setAspectLocked <pyqtgraph.ViewBox.setAspectLocked>`,
    :func:`invertY <pyqtgraph.ViewBox.invertY>`,
    :func:`register <pyqtgraph.ViewBox.register>`,
    :func:`unregister <pyqtgraph.ViewBox.unregister>`
    
    The ViewBox itself can be accessed by calling :func:`getViewBox() <pyqtgraph.PlotItem.getViewBox>` 
    
    ==================== =======================================================================
    **Signals**
    sigYRangeChanged     wrapped from :class:`ViewBox <pyqtgraph.ViewBox>`
    sigXRangeChanged     wrapped from :class:`ViewBox <pyqtgraph.ViewBox>`
    sigRangeChanged      wrapped from :class:`ViewBox <pyqtgraph.ViewBox>`
    ==================== =======================================================================
    """
    
    sigRangeChanged = QtCore.Signal(object, object)    ## Emitted when the ViewBox range has changed
    sigYRangeChanged = QtCore.Signal(object, object)   ## Emitted when the ViewBox Y range has changed
    sigXRangeChanged = QtCore.Signal(object, object)   ## Emitted when the ViewBox X range has changed
    
    
    lastFileDir = None
    managers = {}
    
    def __init__(self, parent=None, name=None, labels=None, title=None, viewBox=None, axisItems=None, enableMenu=True, **kargs):
        """
        Create a new PlotItem. All arguments are optional.
        Any extra keyword arguments are passed to PlotItem.plot().
        
        ==============  ==========================================================================================
        **Arguments**
        *title*         Title to display at the top of the item. Html is allowed.
        *labels*        A dictionary specifying the axis labels to display::
                   
                            {'left': (args), 'bottom': (args), ...}
                     
                        The name of each axis and the corresponding arguments are passed to 
                        :func:`PlotItem.setLabel() <pyqtgraph.PlotItem.setLabel>`
                        Optionally, PlotItem my also be initialized with the keyword arguments left,
                        right, top, or bottom to achieve the same effect.
        *name*          Registers a name for this view so that others may link to it
        *viewBox*       If specified, the PlotItem will be constructed with this as its ViewBox.
        *axisItems*     Optional dictionary instructing the PlotItem to use pre-constructed items
                        for its axes. The dict keys must be axis names ('left', 'bottom', 'right', 'top')
                        and the values must be instances of AxisItem (or at least compatible with AxisItem).
        ==============  ==========================================================================================
        """
        
        GraphicsWidget.__init__(self, parent)
        
        self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        
        ## Set up control buttons
        path = os.path.dirname(__file__)
        #self.autoImageFile = os.path.join(path, 'auto.png')
        #self.lockImageFile = os.path.join(path, 'lock.png')
        self.autoBtn = ButtonItem(pyqtgraph.pixmaps.getPixmap('auto'), 14, self)
        self.autoBtn.mode = 'auto'
        self.autoBtn.clicked.connect(self.autoBtnClicked)
        #self.autoBtn.hide()
        self.buttonsHidden = False ## whether the user has requested buttons to be hidden
        self.mouseHovering = False
        
        self.layout = QtGui.QGraphicsGridLayout()
        self.layout.setContentsMargins(1,1,1,1)
        self.setLayout(self.layout)
        self.layout.setHorizontalSpacing(0)
        self.layout.setVerticalSpacing(0)
        
        if viewBox is None:
            viewBox = ViewBox()
        self.vb = viewBox
        self.vb.sigStateChanged.connect(self.viewStateChanged)
        self.setMenuEnabled(enableMenu, enableMenu) ## en/disable plotitem and viewbox menus
        
        if name is not None:
            self.vb.register(name)
        self.vb.sigRangeChanged.connect(self.sigRangeChanged)
        self.vb.sigXRangeChanged.connect(self.sigXRangeChanged)
        self.vb.sigYRangeChanged.connect(self.sigYRangeChanged)
        
        self.layout.addItem(self.vb, 2, 1)
        self.alpha = 1.0
        self.autoAlpha = True
        self.spectrumMode = False
        
        self.legend = None
        
        ## Create and place axis items
        if axisItems is None:
            axisItems = {}
        self.axes = {}
        for k, pos in (('top', (1,1)), ('bottom', (3,1)), ('left', (2,0)), ('right', (2,2))):
            axis = axisItems.get(k, AxisItem(orientation=k))
            axis.linkToView(self.vb)
            self.axes[k] = {'item': axis, 'pos': pos}
            self.layout.addItem(axis, *pos)
            axis.setZValue(-1000)
            axis.setFlag(axis.ItemNegativeZStacksBehindParent)
        
        self.titleLabel = LabelItem('', size='11pt')
        self.layout.addItem(self.titleLabel, 0, 1)
        self.setTitle(None)  ## hide
        
        
        for i in range(4):
            self.layout.setRowPreferredHeight(i, 0)
            self.layout.setRowMinimumHeight(i, 0)
            self.layout.setRowSpacing(i, 0)
            self.layout.setRowStretchFactor(i, 1)
            
        for i in range(3):
            self.layout.setColumnPreferredWidth(i, 0)
            self.layout.setColumnMinimumWidth(i, 0)
            self.layout.setColumnSpacing(i, 0)
            self.layout.setColumnStretchFactor(i, 1)
        self.layout.setRowStretchFactor(2, 100)
        self.layout.setColumnStretchFactor(1, 100)
        

        ## Wrap a few methods from viewBox
        for m in [
            'setXRange', 'setYRange', 'setXLink', 'setYLink', 'setAutoPan', 'setAutoVisible',
            'setRange', 'autoRange', 'viewRect', 'viewRange', 'setMouseEnabled',
            'enableAutoRange', 'disableAutoRange', 'setAspectLocked', 'invertY',
            'register', 'unregister']:  ## NOTE: If you update this list, please update the class docstring as well.
            setattr(self, m, getattr(self.vb, m))
            
        self.items = []
        self.curves = []
        self.itemMeta = weakref.WeakKeyDictionary()
        self.dataItems = []
        self.paramList = {}
        self.avgCurves = {}
        
        ### Set up context menu
        
        w = QtGui.QWidget()
        self.ctrl = c = Ui_Form()
        c.setupUi(w)
        dv = QtGui.QDoubleValidator(self)
        
        menuItems = [
            ('Transforms', c.transformGroup),
            ('Downsample', c.decimateGroup),
            ('Average', c.averageGroup),
            ('Alpha', c.alphaGroup),
            ('Grid', c.gridGroup),
            ('Points', c.pointsGroup),
        ]
        
        
        self.ctrlMenu = QtGui.QMenu()
        
        self.ctrlMenu.setTitle('Plot Options')
        self.subMenus = []
        for name, grp in menuItems:
            sm = QtGui.QMenu(name)
            act = QtGui.QWidgetAction(self)
            act.setDefaultWidget(grp)
            sm.addAction(act)
            self.subMenus.append(sm)
            self.ctrlMenu.addMenu(sm)
        
        self.stateGroup = WidgetGroup()
        for name, w in menuItems:
            self.stateGroup.autoAdd(w)
        
        self.fileDialog = None
        
        c.alphaGroup.toggled.connect(self.updateAlpha)
        c.alphaSlider.valueChanged.connect(self.updateAlpha)
        c.autoAlphaCheck.toggled.connect(self.updateAlpha)

        c.xGridCheck.toggled.connect(self.updateGrid)
        c.yGridCheck.toggled.connect(self.updateGrid)
        c.gridAlphaSlider.valueChanged.connect(self.updateGrid)

        c.fftCheck.toggled.connect(self.updateSpectrumMode)
        c.logXCheck.toggled.connect(self.updateLogMode)
        c.logYCheck.toggled.connect(self.updateLogMode)

        c.downsampleSpin.valueChanged.connect(self.updateDownsampling)
        c.downsampleCheck.toggled.connect(self.updateDownsampling)
        c.autoDownsampleCheck.toggled.connect(self.updateDownsampling)
        c.subsampleRadio.toggled.connect(self.updateDownsampling)
        c.meanRadio.toggled.connect(self.updateDownsampling)
        c.clipToViewCheck.toggled.connect(self.updateDownsampling)

        self.ctrl.avgParamList.itemClicked.connect(self.avgParamListClicked)
        self.ctrl.averageGroup.toggled.connect(self.avgToggled)
        
        self.ctrl.maxTracesCheck.toggled.connect(self.updateDecimation)
        self.ctrl.maxTracesSpin.valueChanged.connect(self.updateDecimation)
        
        self.hideAxis('right')
        self.hideAxis('top')
        self.showAxis('left')
        self.showAxis('bottom')
        
        if labels is None:
            labels = {}
        for label in list(self.axes.keys()):
            if label in kargs:
                labels[label] = kargs[label]
                del kargs[label]
        for k in labels:
            if isinstance(labels[k], basestring):
                labels[k] = (labels[k],)
            self.setLabel(k, *labels[k])
                
        if title is not None:
            self.setTitle(title)
        
        if len(kargs) > 0:
            self.plot(**kargs)
        
        
    def implements(self, interface=None):
        return interface in ['ViewBoxWrapper']

    def getViewBox(self):
        """Return the :class:`ViewBox <pyqtgraph.ViewBox>` contained within."""
        return self.vb
    
    
    
    def setLogMode(self, x=None, y=None):
        """
        Set log scaling for x and/or y axes.
        This informs PlotDataItems to transform logarithmically and switches
        the axes to use log ticking. 
        
        Note that *no other items* in the scene will be affected by
        this; there is (currently) no generic way to redisplay a GraphicsItem
        with log coordinates.
        
        """
        if x is not None:
            self.ctrl.logXCheck.setChecked(x)
        if y is not None:
            self.ctrl.logYCheck.setChecked(y)
        
    def showGrid(self, x=None, y=None, alpha=None):
        """
        Show or hide the grid for either axis.
        
        ==============  =====================================
        **Arguments:**
        x               (bool) Whether to show the X grid
        y               (bool) Whether to show the Y grid
        alpha           (0.0-1.0) Opacity of the grid
        ==============  =====================================
        """
        if x is None and y is None and alpha is None:
            raise Exception("Must specify at least one of x, y, or alpha.")  ## prevent people getting confused if they just call showGrid()
        
        if x is not None:
            self.ctrl.xGridCheck.setChecked(x)
        if y is not None:
            self.ctrl.yGridCheck.setChecked(y)
        if alpha is not None:
            v = np.clip(alpha, 0, 1)*self.ctrl.gridAlphaSlider.maximum()
            self.ctrl.gridAlphaSlider.setValue(v)
        
    #def paint(self, *args):
        #prof = debug.Profiler('PlotItem.paint', disabled=True)
        #QtGui.QGraphicsWidget.paint(self, *args)
        #prof.finish()
        
    ## bad idea. 
    #def __getattr__(self, attr):  ## wrap ms
        #return getattr(self.vb, attr)
        
    def close(self):
        ## Most of this crap is needed to avoid PySide trouble. 
        ## The problem seems to be whenever scene.clear() leads to deletion of widgets (either through proxies or qgraphicswidgets)
        ## the solution is to manually remove all widgets before scene.clear() is called
        if self.ctrlMenu is None: ## already shut down
            return
        self.ctrlMenu.setParent(None)
        self.ctrlMenu = None
        
        for k in self.axes:
            i = self.axes[k]['item']
            i.close()
            
        self.axes = None
        self.scene().removeItem(self.vb)
        self.vb = None
        
    def registerPlot(self, name):   ## for backward compatibility
        self.vb.register(name)
        
    def updateGrid(self, *args):
        alpha = self.ctrl.gridAlphaSlider.value()
        x = alpha if self.ctrl.xGridCheck.isChecked() else False
        y = alpha if self.ctrl.yGridCheck.isChecked() else False
        self.getAxis('top').setGrid(x)
        self.getAxis('bottom').setGrid(x)
        self.getAxis('left').setGrid(y)
        self.getAxis('right').setGrid(y)

    def viewGeometry(self):
        """Return the screen geometry of the viewbox"""
        v = self.scene().views()[0]
        b = self.vb.mapRectToScene(self.vb.boundingRect())
        wr = v.mapFromScene(b).boundingRect()
        pos = v.mapToGlobal(v.pos())
        wr.adjust(pos.x(), pos.y(), pos.x(), pos.y())
        return wr


    def avgToggled(self, b):
        if b:
            self.recomputeAverages()
        for k in self.avgCurves:
            self.avgCurves[k][1].setVisible(b)
        
    def avgParamListClicked(self, item):
        name = str(item.text())
        self.paramList[name] = (item.checkState() == QtCore.Qt.Checked)
        self.recomputeAverages()
        
    def recomputeAverages(self):
        if not self.ctrl.averageGroup.isChecked():
            return
        for k in self.avgCurves:
            self.removeItem(self.avgCurves[k][1])
        self.avgCurves = {}
        for c in self.curves:
            self.addAvgCurve(c)
        self.replot()
        
    def addAvgCurve(self, curve):
        ## Add a single curve into the pool of curves averaged together
        
        ## If there are plot parameters, then we need to determine which to average together.
        remKeys = []
        addKeys = []
        if self.ctrl.avgParamList.count() > 0:
        
            ### First determine the key of the curve to which this new data should be averaged
            for i in range(self.ctrl.avgParamList.count()):
                item = self.ctrl.avgParamList.item(i)
                if item.checkState() == QtCore.Qt.Checked:
                    remKeys.append(str(item.text()))
                else:
                    addKeys.append(str(item.text()))
                    
            if len(remKeys) < 1:  ## In this case, there would be 1 average plot for each data plot; not useful.
                return
                
        p = self.itemMeta.get(curve,{}).copy()
        for k in p:
            if type(k) is tuple:
                p['.'.join(k)] = p[k]
                del p[k]
        for rk in remKeys:
            if rk in p:
                del p[rk]
        for ak in addKeys:
            if ak not in p:
                p[ak] = None
        key = tuple(p.items())
        
        ### Create a new curve if needed
        if key not in self.avgCurves:
            plot = PlotDataItem()
            plot.setPen(fn.mkPen([0, 200, 0]))
            plot.setShadowPen(fn.mkPen([0, 0, 0, 100], width=3))
            plot.setAlpha(1.0, False)
            plot.setZValue(100)
            self.addItem(plot, skipAverage=True)
            self.avgCurves[key] = [0, plot]
        self.avgCurves[key][0] += 1
        (n, plot) = self.avgCurves[key]
        
        ### Average data together
        (x, y) = curve.getData()
        if plot.yData is not None:
            newData = plot.yData * (n-1) / float(n) + y * 1.0 / float(n)
            plot.setData(plot.xData, newData)
        else:
            plot.setData(x, y)
        
    def autoBtnClicked(self):
        if self.autoBtn.mode == 'auto':
            self.enableAutoRange()
            self.autoBtn.hide()
        else:
            self.disableAutoRange()
            
    def viewStateChanged(self):
        self.updateButtons()
            
    def enableAutoScale(self):
        """
        Enable auto-scaling. The plot will continuously scale to fit the boundaries of its data.
        """
        print("Warning: enableAutoScale is deprecated. Use enableAutoRange(axis, enable) instead.")
        self.vb.enableAutoRange(self.vb.XYAxes)

    def addItem(self, item, *args, **kargs):
        """
        Add a graphics item to the view box. 
        If the item has plot data (PlotDataItem, PlotCurveItem, ScatterPlotItem), it may
        be included in analysis performed by the PlotItem.
        """
        self.items.append(item)
        vbargs = {}
        if 'ignoreBounds' in kargs:
            vbargs['ignoreBounds'] = kargs['ignoreBounds']
        self.vb.addItem(item, *args, **vbargs)
        if hasattr(item, 'implements') and item.implements('plotData'):
            self.dataItems.append(item)
            #self.plotChanged()
            
            params = kargs.get('params', {})
            self.itemMeta[item] = params
            #item.setMeta(params)
            self.curves.append(item)
            #self.addItem(c)
            
        if hasattr(item, 'setLogMode'):
            item.setLogMode(self.ctrl.logXCheck.isChecked(), self.ctrl.logYCheck.isChecked())
            
        if isinstance(item, PlotDataItem):
            ## configure curve for this plot
            (alpha, auto) = self.alphaState()
            item.setAlpha(alpha, auto)
            item.setFftMode(self.ctrl.fftCheck.isChecked())
            item.setDownsampling(*self.downsampleMode())
            item.setClipToView(self.clipToViewMode())
            item.setPointMode(self.pointMode())
            
            ## Hide older plots if needed
            self.updateDecimation()
            
            ## Add to average if needed
            self.updateParamList()
            if self.ctrl.averageGroup.isChecked() and 'skipAverage' not in kargs:
                self.addAvgCurve(item)
                
            #c.connect(c, QtCore.SIGNAL('plotChanged'), self.plotChanged)
            #item.sigPlotChanged.connect(self.plotChanged)
            #self.plotChanged()
        name = kargs.get('name', getattr(item, 'opts', {}).get('name', None))
        if name is not None and hasattr(self, 'legend') and self.legend is not None:
            self.legend.addItem(item, name=name)
            

    def addDataItem(self, item, *args):
        print("PlotItem.addDataItem is deprecated. Use addItem instead.")
        self.addItem(item, *args)
        
    def listDataItems(self):
        """Return a list of all data items (PlotDataItem, PlotCurveItem, ScatterPlotItem, etc)
        contained in this PlotItem."""
        return self.dataItems[:]
        
    def addCurve(self, c, params=None):
        print("PlotItem.addCurve is deprecated. Use addItem instead.")
        self.addItem(c, params)

    def addLine(self, x=None, y=None, z=None, **kwds):
        """
        Create an InfiniteLine and add to the plot. 
        
        If *x* is specified,
        the line will be vertical. If *y* is specified, the line will be
        horizontal. All extra keyword arguments are passed to
        :func:`InfiniteLine.__init__() <pyqtgraph.InfiniteLine.__init__>`.
        Returns the item created.
        """
        pos = kwds.get('pos', x if x is not None else y)
        angle = kwds.get('angle', 0 if x is None else 90)
        line = InfiniteLine(pos, angle, **kwds)
        self.addItem(line)
        if z is not None:
            line.setZValue(z)
        return line
        
        

    def removeItem(self, item):
        """
        Remove an item from the internal ViewBox.
        """
        if not item in self.items:
            return
        self.items.remove(item)
        if item in self.dataItems:
            self.dataItems.remove(item)
            
        if item.scene() is not None:
            self.vb.removeItem(item)
        if item in self.curves:
            self.curves.remove(item)
            self.updateDecimation()
            self.updateParamList()
            #item.connect(item, QtCore.SIGNAL('plotChanged'), self.plotChanged)
            #item.sigPlotChanged.connect(self.plotChanged)

    def clear(self):
        """
        Remove all items from the ViewBox.
        """
        for i in self.items[:]:
            self.removeItem(i)
        self.avgCurves = {}
    
    def clearPlots(self):
        for i in self.curves[:]:
            self.removeItem(i)
        self.avgCurves = {}
        
    
    def plot(self, *args, **kargs):
        """
        Add and return a new plot.
        See :func:`PlotDataItem.__init__ <pyqtgraph.PlotDataItem.__init__>` for data arguments
        
        Extra allowed arguments are:
            clear    - clear all plots before displaying new data
            params   - meta-parameters to associate with this data
        """
        
        
        clear = kargs.get('clear', False)
        params = kargs.get('params', None)
          
        if clear:
            self.clear()
            
        item = PlotDataItem(*args, **kargs)
            
        if params is None:
            params = {}
        self.addItem(item, params=params)
        
        return item

    def addLegend(self, size=None, offset=(30, 30)):
        """
        Create a new LegendItem and anchor it over the internal ViewBox.
        Plots will be automatically displayed in the legend if they
        are created with the 'name' argument.
        """
        self.legend = LegendItem(size, offset)
        self.legend.setParentItem(self.vb)
        return self.legend
        
    def scatterPlot(self, *args, **kargs):
        if 'pen' in kargs:
            kargs['symbolPen'] = kargs['pen']
        kargs['pen'] = None
            
        if 'brush' in kargs:
            kargs['symbolBrush'] = kargs['brush']
            del kargs['brush']
            
        if 'size' in kargs:
            kargs['symbolSize'] = kargs['size']
            del kargs['size']

        return self.plot(*args, **kargs)
                
    def replot(self):
        self.update()

    def updateParamList(self):
        self.ctrl.avgParamList.clear()
        ## Check to see that each parameter for each curve is present in the list
        for c in self.curves:
            for p in list(self.itemMeta.get(c, {}).keys()):
                if type(p) is tuple:
                    p = '.'.join(p)
                    
                ## If the parameter is not in the list, add it.
                matches = self.ctrl.avgParamList.findItems(p, QtCore.Qt.MatchExactly)
                if len(matches) == 0:
                    i = QtGui.QListWidgetItem(p)
                    if p in self.paramList and self.paramList[p] is True:
                        i.setCheckState(QtCore.Qt.Checked)
                    else:
                        i.setCheckState(QtCore.Qt.Unchecked)
                    self.ctrl.avgParamList.addItem(i)
                else:
                    i = matches[0]
                    
                self.paramList[p] = (i.checkState() == QtCore.Qt.Checked)


    ## Qt's SVG-writing capabilities are pretty terrible. 
    def writeSvgCurves(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeSvg)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        if isinstance(fileName, tuple):
            raise Exception("Not implemented yet..")
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        rect = self.vb.viewRect()
        xRange = rect.left(), rect.right() 
        
        svg = ""
        fh = open(fileName, 'w')

        dx = max(rect.right(),0) - min(rect.left(),0)
        ymn = min(rect.top(), rect.bottom())
        ymx = max(rect.top(), rect.bottom())
        dy = max(ymx,0) - min(ymn,0)
        sx = 1.
        sy = 1.
        while dx*sx < 10:
            sx *= 1000
        while dy*sy < 10:
            sy *= 1000
        sy *= -1

        #fh.write('<svg viewBox="%f %f %f %f">\n' % (rect.left()*sx, rect.top()*sx, rect.width()*sy, rect.height()*sy))
        fh.write('<svg>\n')
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M%f,0 L%f,0"/>\n' % (rect.left()*sx, rect.right()*sx))
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M0,%f L0,%f"/>\n' % (rect.top()*sy, rect.bottom()*sy))


        for item in self.curves:
            if isinstance(item, PlotCurveItem):
                color = fn.colorStr(item.pen.color())
                opacity = item.pen.color().alpha() / 255.
                color = color[:6]
                x, y = item.getData()
                mask = (x > xRange[0]) * (x < xRange[1])
                mask[:-1] += mask[1:]
                m2 = mask.copy()
                mask[1:] += m2[:-1]
                x = x[mask]
                y = y[mask]
                
                x *= sx
                y *= sy
                
                #fh.write('<g fill="none" stroke="#%s" stroke-opacity="1" stroke-width="1">\n' % color)
                fh.write('<path fill="none" stroke="#%s" stroke-opacity="%f" stroke-width="1" d="M%f,%f ' % (color, opacity, x[0], y[0]))
                for i in range(1, len(x)):
                    fh.write('L%f,%f ' % (x[i], y[i]))
                
                fh.write('"/>')
                #fh.write("</g>")
        for item in self.dataItems:
            if isinstance(item, ScatterPlotItem):
                
                pRect = item.boundingRect()
                vRect = pRect.intersected(rect)
                
                for point in item.points():
                    pos = point.pos()
                    if not rect.contains(pos):
                        continue
                    color = fn.colorStr(point.brush.color())
                    opacity = point.brush.color().alpha() / 255.
                    color = color[:6]
                    x = pos.x() * sx
                    y = pos.y() * sy
                    
                    fh.write('<circle cx="%f" cy="%f" r="1" fill="#%s" stroke="none" fill-opacity="%f"/>\n' % (x, y, color, opacity))
        
        fh.write("</svg>\n")
        
        
    
    def writeSvg(self, fileName=None):
        if fileName is None:
            fileName = QtGui.QFileDialog.getSaveFileName()
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        self.svg = QtSvg.QSvgGenerator()
        self.svg.setFileName(fileName)
        res = 120.
        view = self.scene().views()[0]
        bounds = view.viewport().rect()
        bounds = QtCore.QRectF(0, 0, bounds.width(), bounds.height())
        
        self.svg.setResolution(res)
        self.svg.setViewBox(bounds)
        
        self.svg.setSize(QtCore.QSize(bounds.width(), bounds.height()))
        
        painter = QtGui.QPainter(self.svg)
        view.render(painter, bounds)
        
        painter.end()
        
        ## Workaround to set pen widths correctly
        import re
        data = open(fileName).readlines()
        for i in range(len(data)):
            line = data[i]
            m = re.match(r'(<g .*)stroke-width="1"(.*transform="matrix\(([^\)]+)\)".*)', line)
            if m is not None:
                #print "Matched group:", line
                g = m.groups()
                matrix = list(map(float, g[2].split(',')))
                #print "matrix:", matrix
                scale = max(abs(matrix[0]), abs(matrix[3]))
                if scale == 0 or scale == 1.0:
                    continue
                data[i] = g[0] + ' stroke-width="%0.2g" ' % (1.0/scale) + g[1] + '\n'
                #print "old line:", line
                #print "new line:", data[i]
        open(fileName, 'w').write(''.join(data))
        
        
    def writeImage(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeImage)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        if isinstance(fileName, tuple):
            raise Exception("Not implemented yet..")
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        self.png = QtGui.QImage(int(self.size().width()), int(self.size().height()), QtGui.QImage.Format_ARGB32)
        painter = QtGui.QPainter(self.png)
        painter.setRenderHints(painter.Antialiasing | painter.TextAntialiasing)
        self.scene().render(painter, QtCore.QRectF(), self.mapRectToScene(self.boundingRect()))
        painter.end()
        self.png.save(fileName)
        
    def writeCsv(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeCsv)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        fd = open(fileName, 'w')
        data = [c.getData() for c in self.curves]
        i = 0
        while True:
            done = True
            for d in data:
                if i < len(d[0]):
                    fd.write('%g,%g,'%(d[0][i], d[1][i]))
                    done = False
                else:
                    fd.write(' , ,')
            fd.write('\n')
            if done:
                break
            i += 1
        fd.close()


    def saveState(self):
        state = self.stateGroup.state()
        state['paramList'] = self.paramList.copy()
        state['view'] = self.vb.getState()
        return state
        
    def restoreState(self, state):
        if 'paramList' in state:
            self.paramList = state['paramList'].copy()
            
        self.stateGroup.setState(state)
        self.updateSpectrumMode()
        self.updateDownsampling()
        self.updateAlpha()
        self.updateDecimation()
        
        if 'powerSpectrumGroup' in state:
            state['fftCheck'] = state['powerSpectrumGroup']
        if 'gridGroup' in state:
            state['xGridCheck'] = state['gridGroup']
            state['yGridCheck'] = state['gridGroup']
            
        self.stateGroup.setState(state)
        self.updateParamList()
        
        if 'view' not in state:
            r = [[float(state['xMinText']), float(state['xMaxText'])], [float(state['yMinText']), float(state['yMaxText'])]]
            state['view'] = {
                'autoRange': [state['xAutoRadio'], state['yAutoRadio']],
                'linkedViews': [state['xLinkCombo'], state['yLinkCombo']],
                'targetRange': r,
                'viewRange': r,
            }
        self.vb.setState(state['view'])
        

    def widgetGroupInterface(self):
        return (None, PlotItem.saveState, PlotItem.restoreState)
      
    def updateSpectrumMode(self, b=None):
        if b is None:
            b = self.ctrl.fftCheck.isChecked()
        for c in self.curves:
            c.setFftMode(b)
        self.enableAutoRange()
        self.recomputeAverages()
            
    def updateLogMode(self):
        x = self.ctrl.logXCheck.isChecked()
        y = self.ctrl.logYCheck.isChecked()
        for i in self.items:
            if hasattr(i, 'setLogMode'):
                i.setLogMode(x,y)
        self.getAxis('bottom').setLogMode(x)
        self.getAxis('top').setLogMode(x)
        self.getAxis('left').setLogMode(y)
        self.getAxis('right').setLogMode(y)
        self.enableAutoRange()
        self.recomputeAverages()
        
    def setDownsampling(self, ds=None, auto=None, mode=None):
        """Change the default downsampling mode for all PlotDataItems managed by this plot.
        
        ===========  =================================================================
        Arguments
        ds           (int) Reduce visible plot samples by this factor, or
                     (bool) To enable/disable downsampling without changing the value.
        auto         (bool) If True, automatically pick *ds* based on visible range
        mode         'subsample': Downsample by taking the first of N samples. 
                         This method is fastest and least accurate.
                     'mean': Downsample by taking the mean of N samples.
                     'peak': Downsample by drawing a saw wave that follows the min 
                         and max of the original data. This method produces the best 
                         visual representation of the data but is slower.
        ===========  =================================================================
        """
        if ds is not None:
            if ds is False:
                self.ctrl.downsampleCheck.setChecked(False)
            elif ds is True:
                self.ctrl.downsampleCheck.setChecked(True)
            else:
                self.ctrl.downsampleCheck.setChecked(True)
                self.ctrl.downsampleSpin.setValue(ds)
                
        if auto is not None:
            if auto and ds is not False:
                self.ctrl.downsampleCheck.setChecked(True)
            self.ctrl.autoDownsampleCheck.setChecked(auto)
            
        if mode is not None:
            if mode == 'subsample':
                self.ctrl.subsampleRadio.setChecked(True)
            elif mode == 'mean':
                self.ctrl.meanRadio.setChecked(True)
            elif mode == 'peak':
                self.ctrl.peakRadio.setChecked(True)
            else:
                raise ValueError("mode argument must be 'subsample', 'mean', or 'peak'.")
            
    def updateDownsampling(self):
        ds, auto, method = self.downsampleMode()
        clip = self.ctrl.clipToViewCheck.isChecked()
        for c in self.curves:
            c.setDownsampling(ds, auto, method)
            c.setClipToView(clip)
        self.recomputeAverages()
        
    def downsampleMode(self):
        if self.ctrl.downsampleCheck.isChecked():
            ds = self.ctrl.downsampleSpin.value()
        else:
            ds = 1
            
        auto = self.ctrl.downsampleCheck.isChecked() and self.ctrl.autoDownsampleCheck.isChecked()
            
        if self.ctrl.subsampleRadio.isChecked():
            method = 'subsample' 
        elif self.ctrl.meanRadio.isChecked():
            method = 'mean'
        elif self.ctrl.peakRadio.isChecked():
            method = 'peak'
        
        return ds, auto, method
        
    def setClipToView(self, clip):
        """Set the default clip-to-view mode for all PlotDataItems managed by this plot.
        If *clip* is True, then PlotDataItems will attempt to draw only points within the visible
        range of the ViewBox."""
        self.ctrl.clipToViewCheck.setChecked(clip)
        
    def clipToViewMode(self):
        return self.ctrl.clipToViewCheck.isChecked()
        
        
        
    def updateDecimation(self):
        if self.ctrl.maxTracesCheck.isChecked():
            numCurves = self.ctrl.maxTracesSpin.value()
        else:
            numCurves = -1
            
        curves = self.curves[:]
        split = len(curves) - numCurves
        for i in range(len(curves)):
            if numCurves == -1 or i >= split:
                curves[i].show()
            else:
                if self.ctrl.forgetTracesCheck.isChecked():
                    curves[i].clear()
                    self.removeItem(curves[i])
                else:
                    curves[i].hide()
        
      
    def updateAlpha(self, *args):
        (alpha, auto) = self.alphaState()
        for c in self.curves:
            c.setAlpha(alpha**2, auto)
     
    def alphaState(self):
        enabled = self.ctrl.alphaGroup.isChecked()
        auto = self.ctrl.autoAlphaCheck.isChecked()
        alpha = float(self.ctrl.alphaSlider.value()) / self.ctrl.alphaSlider.maximum()
        if auto:
            alpha = 1.0  ## should be 1/number of overlapping plots
        if not enabled:
            auto = False
            alpha = 1.0
        return (alpha, auto)

    def pointMode(self):
        if self.ctrl.pointsGroup.isChecked():
            if self.ctrl.autoPointsCheck.isChecked():
                mode = None
            else:
                mode = True
        else:
            mode = False
        return mode
        

    def resizeEvent(self, ev):
        if self.autoBtn is None:  ## already closed down
            return
        btnRect = self.mapRectFromItem(self.autoBtn, self.autoBtn.boundingRect())
        y = self.size().height() - btnRect.height()
        self.autoBtn.setPos(0, y)
    
    
    def getMenu(self):
        return self.ctrlMenu
    
    def getContextMenus(self, event):
        ## called when another item is displaying its context menu; we get to add extras to the end of the menu.
        if self.menuEnabled():
            return self.ctrlMenu
        else:
            return None
    
    def setMenuEnabled(self, enableMenu=True, enableViewBoxMenu='same'):
        """
        Enable or disable the context menu for this PlotItem.
        By default, the ViewBox's context menu will also be affected.
        (use enableViewBoxMenu=None to leave the ViewBox unchanged)
        """
        self._menuEnabled = enableMenu
        if enableViewBoxMenu is None:
            return
        if enableViewBoxMenu is 'same':
            enableViewBoxMenu = enableMenu 
        self.vb.setMenuEnabled(enableViewBoxMenu)
    
    def menuEnabled(self):
        return self._menuEnabled
    
    def hoverEvent(self, ev):
        if ev.enter:
            self.mouseHovering = True
        if ev.exit:
            self.mouseHovering = False
            
        self.updateButtons()
    

    def getLabel(self, key):
        pass
        
    def _checkScaleKey(self, key):
        if key not in self.axes:
            raise Exception("Scale '%s' not found. Scales are: %s" % (key, str(list(self.axes.keys()))))
        
    def getScale(self, key):
        return self.getAxis(key)
        
    def getAxis(self, name):
        """Return the specified AxisItem. 
        *name* should be 'left', 'bottom', 'top', or 'right'."""
        self._checkScaleKey(name)
        return self.axes[name]['item']
        
    def setLabel(self, axis, text=None, units=None, unitPrefix=None, **args):
        """
        Set the label for an axis. Basic HTML formatting is allowed.
        
        ============= =================================================================
        **Arguments**
        axis          must be one of 'left', 'bottom', 'right', or 'top'
        text          text to display along the axis. HTML allowed.
        units         units to display after the title. If units are given, 
                      then an SI prefix will be automatically appended
                      and the axis values will be scaled accordingly.
                      (ie, use 'V' instead of 'mV'; 'm' will be added automatically)
        ============= =================================================================
        """
        self.getAxis(axis).setLabel(text=text, units=units, **args)
        self.showAxis(axis)
        
    def setLabels(self, **kwds):
        """
        Convenience function allowing multiple labels and/or title to be set in one call.
        Keyword arguments can be 'title', 'left', 'bottom', 'right', or 'top'.
        Values may be strings or a tuple of arguments to pass to setLabel.
        """
        for k,v in kwds.items():
            if k == 'title':
                self.setTitle(v)
            else:
                if isinstance(v, basestring):
                    v = (v,)
                self.setLabel(k, *v)
        
        
    def showLabel(self, axis, show=True):
        """
        Show or hide one of the plot's axis labels (the axis itself will be unaffected).
        axis must be one of 'left', 'bottom', 'right', or 'top'
        """
        self.getScale(axis).showLabel(show)

    def setTitle(self, title=None, **args):
        """
        Set the title of the plot. Basic HTML formatting is allowed.
        If title is None, then the title will be hidden.
        """
        if title is None:
            self.titleLabel.setVisible(False)
            self.layout.setRowFixedHeight(0, 0)
            self.titleLabel.setMaximumHeight(0)
        else:
            self.titleLabel.setMaximumHeight(30)
            self.layout.setRowFixedHeight(0, 30)
            self.titleLabel.setVisible(True)
            self.titleLabel.setText(title, **args)

    def showAxis(self, axis, show=True):
        """
        Show or hide one of the plot's axes.
        axis must be one of 'left', 'bottom', 'right', or 'top'
        """
        s = self.getScale(axis)
        p = self.axes[axis]['pos']
        if show:
            s.show()
        else:
            s.hide()
            
    def hideAxis(self, axis):
        """Hide one of the PlotItem's axes. ('left', 'bottom', 'right', or 'top')"""
        self.showAxis(axis, False)
            
    def showScale(self, *args, **kargs):
        print("Deprecated. use showAxis() instead")
        return self.showAxis(*args, **kargs)
            
    def hideButtons(self):
        """Causes auto-scale button ('A' in lower-left corner) to be hidden for this PlotItem"""
        #self.ctrlBtn.hide()
        self.buttonsHidden = True
        self.updateButtons()
        
    def showButtons(self):
        """Causes auto-scale button ('A' in lower-left corner) to be visible for this PlotItem"""
        #self.ctrlBtn.hide()
        self.buttonsHidden = False
        self.updateButtons()
        
    def updateButtons(self):
        if self._exportOpts is False and self.mouseHovering and not self.buttonsHidden and not all(self.vb.autoRangeEnabled()):
            self.autoBtn.show()
        else:
            self.autoBtn.hide()
            
    def _plotArray(self, arr, x=None, **kargs):
        if arr.ndim != 1:
            raise Exception("Array must be 1D to plot (shape is %s)" % arr.shape)
        if x is None:
            x = np.arange(arr.shape[0])
        if x.ndim != 1:
            raise Exception("X array must be 1D to plot (shape is %s)" % x.shape)
        c = PlotCurveItem(arr, x=x, **kargs)
        return c
            
        
        
    def _plotMetaArray(self, arr, x=None, autoLabel=True, **kargs):
        inf = arr.infoCopy()
        if arr.ndim != 1:
            raise Exception('can only automatically plot 1 dimensional arrays.')
        ## create curve
        try:
            xv = arr.xvals(0)
        except:
            if x is None:
                xv = np.arange(arr.shape[0])
            else:
                xv = x
        c = PlotCurveItem(**kargs)
        c.setData(x=xv, y=arr.view(np.ndarray))
        
        if autoLabel:
            name = arr._info[0].get('name', None)
            units = arr._info[0].get('units', None)
            self.setLabel('bottom', text=name, units=units)
            
            name = arr._info[1].get('name', None)
            units = arr._info[1].get('units', None)
            self.setLabel('left', text=name, units=units)
            
        return c

      
    def setExportMode(self, export, opts=None):
        GraphicsWidget.setExportMode(self, export, opts)
        self.updateButtons()
Exemplo n.º 19
0
class MainView(QtGui.QMainWindow):
    """The main view of the application"""

    def __init__(self, modelFile=None, weightsFile=None, solverFile=None, directory=None):
        """
        The top-level GUI object, containing three main tabs:
        Configure: for graphically editing a network backed by a caffe prototxt file
        Train: for monitoring the training process and plotting the accuracies
        Test: for visualizing the activations at different points in a (hopefully) trained network
        :param modelFile: str
            The .prototxt file defining the network architecture
        :param weightsFile: str
            The .caffemodel file containing the learned network weights
        :param solverFile: str
            The .prototxt file defining the solver parameters
        :param directory: str
            The base directory containing the files, for relative file paths
        :return:
        """
        if directory is not None:
            modelFile = directory + modelFile
            weightsFile = directory + weightsFile
            solverFile = directory + solverFile

        self.directory = directory

        self.plots = {}
        self.plotNodes = {}
        self.plotDocks = {}
        self.trainLossPlots = {}

        QtGui.QMainWindow.__init__(self)

        self.resize(1200, 800)
        self.setWindowTitle("caffeViz")
        self.tabWidget = QtGui.QTabWidget()
        self.tabWidget.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding))

        self.ui = MView.Ui_tabWidget()
        self.ui.setupUi(self.tabWidget)

        cw = QtGui.QWidget()
        cw.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding))
        layout = QtGui.QGridLayout()
        cw.setLayout(layout)
        layout.addWidget(self.tabWidget)
        self.setCentralWidget(cw)

        self.fc = NetFlowchart(prototxt=modelFile, weights=weightsFile)
        self.fc.sigChartChanged.connect(self.fc.chartNodeEdited)
        w = self.fc.widget()

        self.ui.splitter.addWidget(self.ui.displayLayout)

        # self.plotLabel = QtGui.QLabel()
        # self.plotLabelDock = dockarea.Dock('label')
        # self.plotLabelDock.addWidget(self.plotLabel)
        # self.ui.plotWidget.addDock(self.plotLabelDock)
        # self.hoverDock.addWidget(self.hoverText)
        # self.addDock(self.hoverDock, 'bottom')
        # self.ui.plotWidget.addDock

        ctrlList = w.ui.ctrlList
        ctrlList.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        w.ui.ctrlList = ctrlList
        # w = w._nodes['LayerNode.0'].ctrlWidget()
        w.sizePolicy().setHorizontalPolicy(QtGui.QSizePolicy.MinimumExpanding)

        self.tabWidget.currentChanged.connect(self.tabChanged)
        self.tabWidget.setCurrentIndex(1)
        self.tabWidget.setCurrentIndex(2)
        self.fc.sigDisplayNodeAdded.connect(self.addPlot)

        self.displayControlDock = pg.dockarea.Dock("Control")
        formWidget = pg.QtGui.QWidget()
        displayCtrlUi = DispCtrl.Ui_displayControlWidget()
        displayCtrlUi.setupUi(formWidget)
        displayCtrlUi.previousBtn.clicked.connect(self.updateDisplay)
        displayCtrlUi.updateBtn.clicked.connect(self.updateDisplay)
        displayCtrlUi.nextBtn.clicked.connect(self.updateDisplay)
        self.displayCtrlUi = displayCtrlUi

        self.displayControlDock.addWidget(formWidget)
        self.ui.displayArea.addDock(self.displayControlDock, position="top")

        self.solverNode = SolverNode("solver", solverFile, weightsFile, directory)
        self.solverNode.sigOutputDataChanged.connect(self.updateTrainPlots)
        self.ui.trainButton.pressed.connect(self.solverNode.trainNet)
        self.ui.stopButton.pressed.connect(self.solverNode.stopTraining)
        self.ui.resumeButton.pressed.connect(self.solverNode.resumeTraining)
        self.ui.loadSolverButton.pressed.connect(self.loadSolver)
        self.ui.saveSolverButton.pressed.connect(self.saveSolver)
        self.ui.trainParamTree.addParameters(self.solverNode.param)
        self.trainPlotItem = self.ui.trainGraphicsLayout.plotItem

    def tabChanged(self, tabIndex):
        """handle moving the flowchart around"""
        if tabIndex == 0:
            self.setTab0()
        elif tabIndex == 2:
            self.setTab2()
        # self.tabWidget.update()

    def setTab0(self):
        self.ui.flowchartCtrlWidget.layout().addWidget(self.fc.widget())
        self.ui.flowchartWidget.layout().addWidget(self.fc.widget().chartWidget)
        self.tabWidget.update()

    def setTab2(self):
        self.ui.flowchartCtrlWidget_2.layout().addWidget(self.fc.widget())
        self.ui.flowchartWidget_2.layout().addWidget(self.fc.widget().chartWidget)
        self.tabWidget.update()

    def addPlot(self, plotNode):
        """
        add an connect a plot
        """
        name = plotNode.name()
        plot = pg.PlotWidget(name=name)
        self.plots[name] = plot
        self.plotNodes[name] = plotNode
        # get class of plot node to determine type of plot
        if isinstance(plotNode, displayNodes):
            plotNode.setPlot(plot)

        plotDock = pg.dockarea.Dock(name)
        self.plotDocks[name] = plotDock
        plotDock.addWidget(plot)
        self.ui.displayArea.addDock(plotDock)
        self.tabWidget.update()

        # for name, plotNode in self.plotNodes.items():
        #     plotNode.setPlotList(self.plots)

    def updateDisplay(self):
        sender = self.sender()
        if sender.objectName() == u"previousBtn":
            self.selectInput(-1)
        elif sender.objectName() == u"nextBtn":
            self.fc.process(forward=True)
        else:
            self.fc.process(forward=False)

    def loadSolver(self, fileName=None, startDir=None):
        if fileName is None:
            if startDir is None:
                startDir = self.directory
            if startDir is None:
                startDir = "."
            self.fileDialog = FileDialog(None, "Load Flowchart..", startDir, "Protobuf (*.prototxt)")
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.loadSolver)
            return
        fileName = unicode(fileName)
        self.solverNode.setProto(fileName)

    def saveSolver(self, fileName=None, startDir=None, suggestedFileName="solver.prototxt"):
        if fileName is None:
            if startDir is None:
                startDir = self.directory
            if startDir is None:
                startDir = "."
            self.fileDialog = FileDialog(None, "Save Solver..", startDir, "Prototxt (*.prototxt)")
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.saveSolver)
            return

        fileName = unicode(fileName)
        self.solverNode.writeProto(fileName)

    def updateTrainPlots(self, data):
        for lossName, color in dict(trainLoss="b", testLoss="g", testAcc="r").items():
            for trainLossName, trainLossArr in data[lossName].items():
                try:
                    self.trainLossPlots[trainLossName].setData(trainLossArr)
                except KeyError:
                    self.trainLossPlots[trainLossName] = self.trainPlotItem.plot(
                        trainLossArr, pen=color, name=trainLossName
                    )

    def stopTraining(self):
        pass

    def resumeTraining(self):
        pass
Exemplo n.º 20
0
class NetFlowchart(Flowchart):
    """An extension of pyqtgraph's flowchart for Caffe-based Neural Nets"""

    sigDisplayNodeAdded = QtCore.Signal(object)  # node

    def __init__(self, prototxt=None, weights=None):
        """
        A flowchart that enables visualizing, editing, and running a caffe neural network model

        :param prototxt: string
            Optional path to a caffe prototxt file, which will be used as the initial model for the network
        :param weights: string
            Optional path to a caffe caffemodel file, which will be used as the initial weights for the network
        """

        self.nodeList = []
        self.displayNodes = {}
        self.netNeedsUpdate = True
        self.netNeedsEval = True
        self.holdUpdateConnects = True
        self.nextData = None
        Flowchart.__init__(self, terminals={
            'dataIn': {'io': 'in'},
            'dataOut': {'io': 'out'}
        })

        if prototxt is None:
            self.proto = NetProto()
            self.layerList = self.proto.layer
            self.plotList = {}
        else:
            self.proto = parsePrototxt(prototxt, 'net')
            self.layerList = self.proto.layer
            # for old proto format
            if len(self.layerList) == 0:
                self.layerList = self.proto.layers

        self.weights = weights

        for proto in self.layerList:
            print proto

        self.setupNodes()

        fcw = self.widget().chartWidget
        fcw.viewDock.sizePolicy().setHorizontalPolicy(QtGui.QSizePolicy.Maximum)

        fcw.moveDock(fcw.hoverDock, 'bottom', fcw.viewDock)
        fcw.moveDock(fcw.selDock, 'right', fcw.hoverDock)

    def setupNodes(self):
        """
        perform the necessary setup
        """
        self.createAndLayoutNodes()
        self.holdUpdateConnects = True
        self.configureNodes()
        self.holdUpdateConnects = False
        self.connectNodes()
        self.viewBox.autoRange()

    def clear(self):
        self.nodeList = []
        Flowchart.clear(self)

    def createAndLayoutNodes(self):
        """
        Create all the nodes and lay them out in the flowchart
        """

        nodeSize = 200
        spacingSize = int(nodeSize * 1.1)

        # layout the nodes in a somewhat sensible fashion based upon the numbers used in the names
        # could be a lot more
        if self.layerList:
            ypos, xpos = 0, -spacingSize
            lastNum = 1
            for i, layerParam in enumerate(self.layerList):
                name = str(layerParam.name).lower()

                digits = [int(s) for s in name if s.isdigit()]
                if len(digits) == 0:
                    primaryDigit = 0
                else:
                    primaryDigit = digits[0]
                # numeric suffix of layer
                if lastNum != primaryDigit:
                    ypos += spacingSize
                    xpos = 0
                    # xpos += node.graphicsItem().bounds.width() + 20
                else:
                    xpos += spacingSize
                lastNum = primaryDigit
                node = self.createNode("Layer", name=name, pos=(xpos, ypos))
                self.nodeList.append(node)
            ypos += 150
            self.outputNode.graphicsItem().setPos(xpos, 0)
        else:
            # default node config
            node = self.createNode("Layer")

    def createNode(self, nodeType, name=None, pos=None):
        """
        overrides Flowchart.createNode to pick sensible names that caffe will accept
        """
        if name is None:
            name = nodeType.lower()
        if name in self._nodes:
            name2 = name
            n = 1
            while name2 in self._nodes:
                name2 = "%s.%d" % (name, n)
                n += 1
            name = name2
        return Flowchart.createNode(self, nodeType, name=name, pos=pos)

    def configureNodes(self):
        """run through self.layerList attribute and configure all the nodes from their caffe layerSpec"""
        for i, spec in enumerate(self.layerList):
            node = self.nodeList[i]
            node.configFromLayerSpec(spec)

    def connectNodes(self):
        """run through the nodeList attribute, connecting all the terminals of the nodes based on their names"""
        if self.holdUpdateConnects:
            return

        # connect node to first preceding node with same name
        for i, node in enumerate(self.nodeList[1:]):
            for inTerm in node.inputs().values():
                inName = inTerm.name()
                # work backward to find the first node with the correct output terminal (there can be more than one)
                for previousNode in self.nodeList[i::-1]:
                    for outTerm in previousNode.outputs().values():
                        outName = outTerm.name()
                        if inName == outName:
                            color = self.getConnectionColor(inTerm, previousNode)
                            if color is not None:
                                conItem = NetConnectionItem(inTerm.graphicsItem(), outTerm.graphicsItem())
                                conItem.setStyle(color=color, shape='cubic')
                                try:
                                    assert isinstance(inTerm, NetTerminal)
                                    inTerm.connectTo(outTerm, conItem, rename=False)
                                except TypeError:
                                    inTerm.connectTo(outTerm, conItem)
        try:
            # try and link up the flowchart's input and output to the node network, but this can fail depending on the
            # type of input nodes
            inTerm = self.nodeList[0].inputs().values()[0]
            self.connectTerminals(inTerm, self['dataIn'])
            outTerm = self.nodeList[-1].outputs().values()[0]
            self.connectTerminals(outTerm, self['dataOut'])
        except IndexError:
            self.hideInputs()

        self.updateProto()

    def getConnectionColor(self, inTerm, outNode):
        """
        get the color for the node connection based on the phases of the inputs
        :param inTerm: Terminal
            The input terminal
        :param outNode: Node
            the output Node
        """
        outPhase = outNode.phase()
        # if input node is already connected to an output node of the same phase, do not connect
        for connectedTerm in inTerm.connections().keys():
            if connectedTerm.node().phase() == outPhase:
                return None
        inPhase = inTerm.node().phase()
        return self.getColorMapping(inPhase, outPhase)

    @staticmethod
    def getColorMapping(inPhase, outPhase):
        """
        get the color mapping based on the phases of the input and output
        :param inPhase: int
            the input phase
        :param outPhase: int
            the output phase
        """
        colorVal = None
        if inPhase == outPhase:
            colorVal = inPhase
        elif inPhase == -1:
            colorVal = outPhase
        elif outPhase == -1:
            colorVal = inPhase
        return colorDict[colorVal]

    def hideInputs(self):
        for node in [self.inputNode, self.outputNode]:
            node._allowRemove = True
            self.removeNode(node)

    def chartNodeEdited(self, sender, action, node):
        """
        configure a node
        :param sender: QObject, not used
        :param action: str
            name of the action
        :param node: Node
            node object that the action was performed on
        """
        if action == 'add':
            if isinstance(node, LayerNode):
                node.connectParamTree()
            elif isinstance(node, displayNodes):
                self.displayNodes[node.name()] = node
                self.sigDisplayNodeAdded.emit(node)
        if isinstance(node, LayerNode):
            self.netNeedsUpdate = True

    def updateProto(self):
        """
        update the Prototxt file for the entire net based upon the Flowchart's LayerNodes
        """
        deps = {}
        for name, node in self._nodes.items():
            if isinstance(node, LayerNode):
                deps[node] = node.dependentNodes()
        try:
            sortedNodes = toposort.toposort_flatten(deps)
        except StandardError as e:
            print 'no valid net'
            print e
            return

        # oldProto = NetProto()
        # oldProto.CopyFrom(self.proto)

        self.proto.ClearField('layer')
        self.proto.layer.extend([node.proto for node in sortedNodes])
        return self.proto

    def loadFile(self, fileName=None, startDir=None):
        """load a new prototxt file"""
        if fileName is None:
            if startDir is None:
                startDir = self.filePath
            if startDir is None:
                startDir = '.'
            self.fileDialog = FileDialog(None, "Load Flowchart..", startDir, "Protobuf (*.prototxt)")
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.loadFile)
            return
        fileName = unicode(fileName)
        self.clear()
        self.proto = parsePrototxt(fileName, 'net')
        self.layerList = self.proto.layer

        self.setupNodes()
        self.sigFileLoaded.emit(fileName)

    def saveFile(self, fileName=None, startDir=None, suggestedFileName='flowchart.fc'):
        """save the current prototxt file"""
        if fileName is None:
            if startDir is None:
                startDir = self.filePath
            if startDir is None:
                startDir = '.'
            self.fileDialog = FileDialog(None, "Save Flowchart..", startDir, "Flowchart (*.fc)")
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.saveFile)
            return
        fileName = unicode(fileName)
        configfile.writeConfigFile(self.saveState(), fileName)
        self.sigFileSaved.emit(fileName)

    def process(self, forward=False, **args):
        """
        Respond to the flowchart's request to process
        :param forward: whether to process the next data or reprocess current data
        """
        # set needs update whenever net changes params
        # but don't let user change things like net dimensions
        if self.netNeedsUpdate:
            self.getCaffeNet()

        if self.netNeedsEval or forward:
            kwargs = dict()
            if self.nextData is not None:
                kwargs['data'] = self.nextData
            self.net.forward(**kwargs)
            self.netNeedsEval = False

        self.updatePlots()

    def getCaffeNet(self):
        """
        load a caffe Net object based on the updated protocol buffer
        :return:
        """
        proto = self.updateProto()
        # tempFile = NamedTemporaryFile()

        # for now make a nonTemp file so I can read it!
        tempFile = file('aprototxt.prototxt', 'w')

        # print str(self.proto)
        tempFile.write(str(proto))
        tempFile.close()
        netArgs = (tempFile.name,)
        # store the weights somewhere!
        if self.weights:
            netArgs += (self.weights,)
        # depends on tab? or is process only for test tab anyway?
        netArgs += (caffe.TEST,)
        self.net = caffe.Net(*netArgs)
        self.netNeedsUpdate = False
        self.netNeedsEval = True

    def updatePlots(self):
        """run through the display nodes and update the immediately preceding LayerNodes from the caffe net data"""
        # we want the terminals that provide the input for our displayNodes
        for nodeName, node in self.displayNodes.items():
            for inputName, inputTerm in node.inputs().items():
                for outputTerm in inputTerm.inputTerminals():
                    self.setNetTerminalData(outputTerm)

    def setNetTerminalData(self, term):
        """
        pulls the data from the caffe net's blob with the corresponding name and sets the terminal's data to this value,
        which will cause any dependent Display nodes to update their displays
        :param term: NetTerminal
        """
        blobName = term.name()
        netOutput = self.net.blobs[blobName].data
        term.setValue(netOutput)
        node = term.node()
        node.sigOutputChanged.emit(node)
Exemplo n.º 21
0
    def writeSvgCurves(self, fileName=None):
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) 
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeSvg)
            return
        #if fileName is None:
            #fileName = QtGui.QFileDialog.getSaveFileName()
        if isinstance(fileName, tuple):
            raise Exception("Not implemented yet..")
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        
        rect = self.vb.viewRect()
        xRange = rect.left(), rect.right() 
        
        svg = ""
        fh = open(fileName, 'w')

        dx = max(rect.right(),0) - min(rect.left(),0)
        ymn = min(rect.top(), rect.bottom())
        ymx = max(rect.top(), rect.bottom())
        dy = max(ymx,0) - min(ymn,0)
        sx = 1.
        sy = 1.
        while dx*sx < 10:
            sx *= 1000
        while dy*sy < 10:
            sy *= 1000
        sy *= -1

        #fh.write('<svg viewBox="%f %f %f %f">\n' % (rect.left()*sx, rect.top()*sx, rect.width()*sy, rect.height()*sy))
        fh.write('<svg>\n')
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M%f,0 L%f,0"/>\n' % (rect.left()*sx, rect.right()*sx))
        fh.write('<path fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="1" d="M0,%f L0,%f"/>\n' % (rect.top()*sy, rect.bottom()*sy))


        for item in self.curves:
            if isinstance(item, PlotCurveItem):
                color = fn.colorStr(item.pen.color())
                opacity = item.pen.color().alpha() / 255.
                color = color[:6]
                x, y = item.getData()
                mask = (x > xRange[0]) * (x < xRange[1])
                mask[:-1] += mask[1:]
                m2 = mask.copy()
                mask[1:] += m2[:-1]
                x = x[mask]
                y = y[mask]
                
                x *= sx
                y *= sy
                
                #fh.write('<g fill="none" stroke="#%s" stroke-opacity="1" stroke-width="1">\n' % color)
                fh.write('<path fill="none" stroke="#%s" stroke-opacity="%f" stroke-width="1" d="M%f,%f ' % (color, opacity, x[0], y[0]))
                for i in range(1, len(x)):
                    fh.write('L%f,%f ' % (x[i], y[i]))
                
                fh.write('"/>')
                #fh.write("</g>")
        for item in self.dataItems:
            if isinstance(item, ScatterPlotItem):
                
                pRect = item.boundingRect()
                vRect = pRect.intersected(rect)
                
                for point in item.points():
                    pos = point.pos()
                    if not rect.contains(pos):
                        continue
                    color = fn.colorStr(point.brush.color())
                    opacity = point.brush.color().alpha() / 255.
                    color = color[:6]
                    x = pos.x() * sx
                    y = pos.y() * sy
                    
                    fh.write('<circle cx="%f" cy="%f" r="1" fill="#%s" stroke="none" fill-opacity="%f"/>\n' % (x, y, color, opacity))
        
        fh.write("</svg>\n")