Beispiel #1
0
sinx_xCurve.attach(graph)
sinx_xCurve.setPen(qt.QPen(qt.Qt.red))
sinx_xCurve.setData(xData,yData_sinx_x)

#add a curve follow marked
sinx_xFollowMarked = sinx_xCurve.getCurvePointFollowMarked()
format = _myFormat()
sinx_xFollowMarked.setTextLabelCallback(format.CBK)

#add a  marked point on the middle of the curve
markedPoint = sinx_xCurve.getPointMarked(len(xData) / 2)

#Second curve sin(x)
sinxCurve = QubGraphCurve(title='sin(x)')
sinxCurve.attach(graph)
sinxCurve.setPen(qt.QPen(qt.Qt.green))
sinxCurve.setData(xData,yData_sinx)


#add a controled marked point on all y == 0
controledPoints = []
for i,y in enumerate(yData_sinx) :
    if 0. < y < 1e-3:
        controledPoints.append(sinxCurve.getPointControl(i))
    
graph.replot()
graph.show()
app.setMainWidget(graph)

app.exec_loop()
Beispiel #2
0
class QubColormapDialog(qt.QWidget):
    """
    Creates a dialog box to change a colormap.
    It allows to change colormaps (Greyscale, Reverse Grey, Temperature,
    Red, Green, Blue, Many) and the mapping on the selected colormap of
    the min and max value of the data. it also allows to set the colormap in
    autoscale mode or not.
    When any of these parameters change, the signal "ColormapChanged" is
    sent with the parameters colormap, autoscale, minValue, maxValue
    """
    def __init__(self, parent=None, name="Colormap"):
        """
        Constructor method
        Create the layout of the colormap dialog widget using default value
        """
        qt.QWidget.__init__(self, parent,name)
        self.setIcon(loadIcon('colormap.png'))
        self.__dataMin = 0
        self.__dataMax = 255
        self.__data = None
        self.__histoTimer = qt.QTimer(self)
        qt.QObject.connect(self.__histoTimer,qt.SIGNAL('timeout()'),self.__calcHistogram)
        
        self.__colormapSps = [("Greyscale",LUT.Palette.GREYSCALE),
                              ("Reverse Grey",LUT.Palette.REVERSEGREY),
                              ("Temperature",LUT.Palette.TEMP),
                              ("Red",LUT.Palette.RED),
                              ("Green",LUT.Palette.GREEN),
                              ("Blue",LUT.Palette.BLUE),
                              ("Many",LUT.Palette.MANY),
                              ("Geographical",LUT.Palette.FIT2D)]

        self.__lutType = [('Linear',LUT.LINEAR),('Logarithm',LUT.LOG),('Shift Logarithm',LUT.SHIFT_LOG)]
                         
        
        """
        main layout (VERTICAL)
        """
        vlayout = qt.QVBoxLayout(self, 0, -1, "Main ColormapDialog Layout")
        vlayout.setMargin(10)
        vlayout.setSpacing(10)

        """
        combo for colormap
        """
        colormapData = numpy.resize(numpy.arange(60), (10,60))
        self.__colormapCombo = qt.QComboBox(self)
        palette = LUT.Palette()
        for colormapName,colormapType in self.__colormapSps:
            palette.fillPalette(colormapType)
            image,(minVal,maxVal) = LUT.map_on_min_max_val(colormapData,palette,LUT.LINEAR)
            self.__colormapCombo.insertItem(qt.QPixmap(image),colormapName)
        self.connect(self.__colormapCombo, qt.SIGNAL("activated(int)"),
                     self.__colormapTypeChanged)
        vlayout.addWidget(self.__colormapCombo)

        self.__lutTypeCombo = qt.QComboBox(self)
        for lutTypeName,lutType in self.__lutType:
            self.__lutTypeCombo.insertItem(lutTypeName)
        vlayout.addWidget(self.__lutTypeCombo)
        self.connect(self.__lutTypeCombo, qt.SIGNAL("activated(int)"),
                     self.__lutTypeChanged)
        """
        layout 2 (HORIZONTAL)
            - min value label
            - min value text
        """
        hlayout2 = qt.QHBoxLayout(vlayout, -1, "layout 2")
        hlayout2.setSpacing(10)

        """
        min value label
        """
        self.__minLabel  = qt.QLabel("Minimum", self)
        hlayout2.addWidget(self.__minLabel)
        
        """
        min label text
        """
        hlayout2.addSpacing(5)
        hlayout2.addStretch(1)
        self.__minText  = qt.QLineEdit(self)
        self.__minText.setValidator(qt.QDoubleValidator(self.__minText))
        self.__minText.setFixedWidth(150)
        self.__minText.setAlignment(qt.Qt.AlignRight)
        self.connect(self.__minText, qt.SIGNAL("returnPressed()"),
                     self.__minMaxTextChanged)
        hlayout2.addWidget(self.__minText)
        
        """
        layout 3 (HORIZONTAL)
            - max value label
            - max value text
        """
        hlayout3 = qt.QHBoxLayout(vlayout, -1, "layout 3")
        hlayout3.setSpacing(10)

        """
        max value label
        """
        self.__maxLabel  = qt.QLabel("Maximum", self)
        hlayout3.addWidget(self.__maxLabel)
        
        """
        max label text
        """
        hlayout3.addSpacing(5)
        hlayout3.addStretch(1)
        self.__maxText  = qt.QLineEdit(self)
        self.__maxText.setValidator(qt.QDoubleValidator(self.__maxText))
        self.__maxText.setFixedWidth(150)
        self.__maxText.setAlignment(qt.Qt.AlignRight)
        
        self.connect(self.__maxText, qt.SIGNAL("returnPressed()"),
                     self.__minMaxTextChanged)
        hlayout3.addWidget(self.__maxText)
        
        """
        hlayout 4 (HORIZONTAL)
            - graph
        """
        hlayout4 = qt.QHBoxLayout(vlayout, -1, "layout 4")
        hlayout4.setSpacing(10)
        
        """
        graph
        """
        self.__colormapGraph  = QubGraph(self)
        
        """
        remove Y axis label
        set X axis label
        remove curves legends
        """
        self.__colormapGraph.enableAxis(self.__colormapGraph.yLeft,False)
        self.__colormapGraph.setXLabel("Data Values")

        """
        set the curve _/-
        """
        self.colormapCurve = QubGraphCurve("ColormapCurve")
        self.colormapCurve.setData(numpy.array([0, 10, 20, 30]),numpy.array([-10, -10, 10, 10 ]))
        self.colormapCurve.attach(self.__colormapGraph)
        self.__minControl = self.colormapCurve.getPointControl(1)
        self.__minControl.setValueChangeCallback(self.__minMoved)
        
        self.__maxControl = self.colormapCurve.getPointControl(2)
        self.__maxControl.setValueChangeCallback(self.__maxMoved)
        
        self.__lowerBound = self.colormapCurve.getPointMarked(0)
        self.__upperBound = self.colormapCurve.getPointMarked(3)


        self.__colormapGraph.setMinimumSize(qt.QSize(250,200))

        hlayout4.addWidget(self.__colormapGraph)

        #Histogram curve
        self.__histoCurve = QubGraphCurve("Histo")
        self.__histoCurve.setAxis(QubGraph.xBottom,QubGraph.yRight)
        self.__histoCurve.setStyle(self.__histoCurve.Sticks)
        self.__histoCurve.setPen(qt.QPen(qt.Qt.blue,2))
        self.__histoCurve.attach(self.__colormapGraph)
        #Histogram Data Curve
        self.__histoDataCurve = QubGraphCurve("Histo Data")
        self.__histoDataCurve.setAxis(QubGraph.xBottom,QubGraph.yRight)
        self.__histoDataCurve.setPen(qt.QPen(qt.Qt.red,1))
        self.__histoDataCurve.attach(self.__colormapGraph)

        self.__colormapGraph.setAxisAutoScale(QubGraph.yRight)
        """
        hlayout 5 (HORIZONTAL)
            - autoscale
            - scale 90%
            - full scale
        """
        hlayout5 = qt.QHBoxLayout(vlayout, -1, "layout 5")
        hlayout5.setSpacing(10)
        
        """
        autoscale
        """
        self.__autoscaleToggle = qt.QPushButton("Autoscale", self)
        self.__autoscaleToggle.setToggleButton(True)
        self.connect(self.__autoscaleToggle, qt.SIGNAL("toggled(bool)"),
                     self.__autoscaleChanged)
        hlayout5.addWidget(self.__autoscaleToggle)

        """
        scale 90%
        """
        self.__scale90Button = qt.QPushButton("90%", self)
        self.connect(self.__scale90Button, qt.SIGNAL("clicked()"),
                     self.__scale90Changed)
        hlayout5.addWidget(self.__scale90Button)

        """
        full scale
        """
        self.__fullscaleButton = qt.QPushButton("Full", self)
        self.connect(self.__fullscaleButton, qt.SIGNAL("clicked()"),
                     self.__fullscaleChanged)
        hlayout5.addWidget(self.__fullscaleButton)

        """
        sigma
        """
        self.__sigmaScaleMode = [('\xb1 std dev / 2',0,0.5),
                                 ('\xb1 std dev',1,1.),
                                 ('\xb1 std dev * 2',2,2.),
                                 ('\xb1 std dev * 3',3,3.)]
        
        self.__sigmaScale = qt.QToolButton(self,"sigma")
        qt.QObject.connect(self.__sigmaScale, qt.SIGNAL("clicked()"),
                           self.__sigmaScaleChanged)
        popupMenu = qt.QPopupMenu(self.__sigmaScale)
        qt.QObject.connect(popupMenu, qt.SIGNAL("activated(int )"),
                           self.__sigmaModeSelected)

        self.__sigmaScale.setPopup(popupMenu)
        self.__sigmaScale.setPopupDelay(0)
        for itemText,itemVal,_ in self.__sigmaScaleMode :
            popupMenu.insertItem(itemText,itemVal)
        self.__sigmaModeSelected(3)
        hlayout5.addWidget(self.__sigmaScale)
        """
        colormap window can not be resized
        """
        self.__refreshCallback = None
        self.__colormap = None
        
    def __del__(self) :
        self.__refreshCallback = None
                
    ##@brief setCaptionPrefix
    def setCaptionPrefix(self,prefix) :
        self.setCaption(prefix)
    
    ##@brief this methode set the callback fro refresh when colormap has changed
    def setColormapNRefreshCallBack(self,colormap,cbk) :
        self.__colormap = colormap
        self.__refreshCallback = createWeakrefMethod(cbk)
    #############################################
    ### COLORMAP
    #############################################
    def __colormapTypeChanged(self, colormapID):
        """
        type of colormap has changed
            - update the colormap dialog
            - send the "ColormapChanged" signal
        """
        if self.__colormap:
            self.__colormap.setColorMapType(self.__colormapSps[colormapID][1])
            self.__refreshImage()
    #############################################
    ### LUT
    #############################################
    def __lutTypeChanged(self,lutID) :
        if self.__colormap:
            self.__colormap.setLutType(self.__lutType[lutID][1])
            self.__refreshImage()
    #############################################
    ### MIN/MAX VALUES
    #############################################
    def __minMaxTextChanged(self):
        """
        min value changed
            - update the colormap dialog
            - send the "ColormapChanged" signal
        """
        if self.__colormap :
            minMax,_ = self.__minText.text().toFloat()
            maxVal,_ = self.__maxText.text().toFloat()

            self.__colormap.setMinMax(minMax,maxVal)
            self.__refreshImage()

    #############################################
    ### SCALES
    #############################################
    def __autoscaleChanged(self, val):
        """
        autoscale value changed
            - update the colormap dialog
            - send the "ColormaChanged" signal
        """
        for widget in [self.__colormapGraph,self.__scale90Button,self.__fullscaleButton,
                       self.__minText,self.__maxText,self.__sigmaScale] :
            widget.setEnabled(not val)
        if self.__colormap:
            self.__colormap.setAutoscale(val)
            self.__refreshImage()
            
    def __scale90Changed(self):
        """
        set min value to be the min value of the data and max value to be
        the value corresponding to 90% of the different value of the data
        """
        if self.__colormap:
            datatmp = self.__data.copy()
            datatmp = datatmp.ravel()
            datatmp.sort()
            cumsum = datatmp.cumsum()
            limit = cumsum[-1] * 0.95
            maskResult = cumsum > limit
            maxVal = datatmp[maskResult][0]
            self.__colormap.setMinMax(self.__data.min(),maxVal)
            self.__refreshImage()

    def __fullscaleChanged(self):
        """
        set min/max value of the colormap to the min/max value of the data
        """
        if self.__colormap:
            self.__colormap.setMinMax(self.__data.min(),self.__data.max())
            self.__refreshImage()

    def __sigmaScaleChanged(self) :
        try:
            minMax = self.__get_min_max_with_sigma()
        except AttributeError: return
        self.__colormap.setMinMax(*minMax)
        self.__refreshImage()
        
    def __sigmaModeSelected(self,index) :
        self.__sigmaScaleSelectedMode = index
        self.__sigmaScale.setText(self.__sigmaScaleMode[index][0])
        self.__sigmaScaleChanged()

    def __get_min_max_with_sigma(self) :
        nbValue = 1
        for val in self.__data.shape:
            nbValue *= val
        integralVal = self.__data.sum() 
        average = integralVal / nbValue
        stdDeviation = self.__data.std()
        stdDeviation *= self.__sigmaScaleMode[self.__sigmaScaleSelectedMode][2]
        return (average - stdDeviation,average + stdDeviation)
 
    #############################################
    ### GRAPH
    #############################################
    def __maxMoved(self,x,y) :
        if self.__colormap:
            minVal,_ = self.__colormap.minMax()
            self.__colormap.setMinMax(minVal,x)
        self.__refreshImage()
        
    def __minMoved(self,x,y) :
        if self.__colormap:
            _,maxVal = self.__colormap.minMax()
            self.__colormap.setMinMax(x,maxVal)
        self.__refreshImage()
        
    def setData(self,data):
        colormap = self.__colormap
        if colormap and data is not None:
            self.__data = data
            if colormap.autoscale() :
                if colormap.lutType() == LUT.LOG :
                    self.__dataMin,self.__dataMax = colormap.minMaxMappingMethode()
                else:
                    self.__dataMin,self.__dataMax = self.__data.min(),self.__data.max()
            else:
                self.__dataMin,self.__dataMax = colormap.minMax()
                self.__dataMin = min(self.__data.min(),self.__dataMin)
                self.__dataMax = max(self.__dataMax,self.__data.max())
                
                     ####### GRAPH UPDATE #######
            """
            calculate visible part of the graph outside data values (margin)
            """
            marge = (abs(self.__dataMax) + abs(self.__dataMin)) / 6.0
            minmd = self.__dataMin - marge
            maxpd = self.__dataMax + marge
            self.__colormapGraph.setAxisScale(self.__colormapGraph.xBottom,minmd-marge/2, maxpd)
            self.__colormapGraph.setAxisScale(self.__colormapGraph.yLeft,-11.5, 11.5)
            """
            tells where points can move:
                first and last : do not move
                second and third: cannot move in Y dir.,can move
                                  in X dir. between datamin and datamax
            """
            self.__minControl.setConstraints(None,None, -10, -10)
            self.__maxControl.setConstraints(None,None,  10,  10)

            """
            move points to their values
            """
            if colormap.autoscale() :
                minValue,maxValue = self.__dataMin,self.__dataMax
            else:
                minValue,maxValue = colormap.minMax()

            self.__lowerBound.setValue(minmd, -10)
            self.__minControl.setValue(minValue, -10)
            self.__maxControl.setValue(maxValue, 10)
            self.__upperBound.setValue(maxpd, 10)
            self.__colormapGraph.replot()
                         ####### TEXT UPDATE #######
            if isinstance(minValue,int):
                self.__minText.setText('%d' % minValue)
                self.__maxText.setText('%d' % maxValue)
            else:
                self.__minText.setText('%lf' % minValue)
                self.__maxText.setText('%lf' % maxValue)
 
            colormapType = self.__colormapSps[self.__colormapCombo.currentItem()]
            if colormapType[1] != colormap.colorMapType():
                for i,(colorTypeName,colorType) in enumerate(self.__colormapSps):
                    if colorType == colormap.colorType() :
                        self.__colormapCombo.setCurrentItem(i)
                        break

            lutType = self.__lutType[self.__lutTypeCombo.currentItem()]
            if lutType[1] != colormap.lutType() :
                for i,(lutTypeName,lutType) in enumerate(self.__lutType):
                    if lutType == colormap.lutType() :
                        self.__lutTypeCombo.setCurrentItem(i)
                        break

            self.__autoscaleToggle.setOn(colormap.autoscale())
        #HISTO
        if self.isShown() and not self.__histoTimer.isActive():
            self.__histoTimer.start(5000)
        
    def __refreshImage(self) :
        if self.__refreshCallback :
            self.__refreshCallback()

    def __calcHistogram(self) :
        self.__histoTimer.stop()
        if self.__data is not None:
            minVal,maxVal = self.__colormap.minMax()
            
            if Stat:
                YDataHisto,XDataHisto = Stat.histo(self.__data,32)
            else:
                YDataHisto,XDataHisto = numpy.histogram(self.__data,bins = 32)
                
            self.__histoDataCurve.setData(XDataHisto,YDataHisto)

            if Stat:
                YHisto,XHisto = Stat.histo(self.__data,32,minVal,maxVal)
            else:
                YHisto,XHisto = numpy.histogram(self.__data,bins = 32,range=[minVal,maxVal])
                YHisto = YHisto[:-1]
                XHisto = XHisto[:-1]
            lastValue = YHisto[-1]
            maxVal = YDataHisto.max()
            if lastValue > maxVal : YHisto[-1] = maxVal
            self.__histoCurve.setData(XHisto,YHisto)

            self.__colormapGraph.replot()
            
    def show(self) :
        qt.QWidget.show(self)
        self.__histoTimer.start(200)