Exemple #1
0
class DangleGraphPopup(BasePopup):
  
  def __init__(self, parent):

    BasePopup.__init__(self, parent=parent, title=" Dangle Prediction Graph",
                       transient=False, borderwidth=6)

    parent.protocol("WM_DELETE_WINDOW", self.close)

    self.geometry('700x700+50+50')

  def body(self, guiFrame):
 
    guiFrame.expandGrid(0,0)
    guiFrame.expandGrid(1,0)
 
    self.anglesGraph = ScrolledGraph(guiFrame, xLabel='Residue',yLabel='Angle',
                                     relief='flat', symbolSize=1, grid=(0,0),
                                     zoom=1.0,width=500,height=200,
                                     dataColors=['#D00000','#0000D0'],
                                     dataNames=['Phi','Psi'])
    self.anglesGraph.draw()

    self.islandsGraph = ScrolledGraph(guiFrame, xLabel='Residue',yLabel='No. Islands',
                                     relief='flat', graphType='histogram',
                                     zoom=1.0,width=500,height=200 ,grid=(1,0),
                                     dataColors=['#8080FF','#FFFF00','#E00000'],
                                     dataNames=['1','2-3','4+'],)
    self.islandsGraph.draw()

  def update(self, phiData, psiData, islandData):
    
    self.anglesGraph.update(dataSets=[phiData,psiData])   
    self.islandsGraph.update(dataSets=islandData)
Exemple #2
0
  def draw(self):
  
    self.drawContours()
  
    ScrolledGraph.draw(self)
  
    plotRegion = self.getPlotRegion()

    minX = self.minT1
    maxX = self.maxT1
    minY = self.minT2
    maxY = self.maxT2

    dataRegion = [minX, minY, maxX, maxY]
    
    deltaXplot = plotRegion[2] - plotRegion[0]
    deltaYplot = plotRegion[3] - plotRegion[1] 
    deltaXdata = dataRegion[2] - dataRegion[0]
    deltaYdata = dataRegion[3] - dataRegion[1]
  
    ppvX = deltaXplot/float(deltaXdata)
    ppvY = deltaYplot/float(deltaYdata)
    createText = self.canvas.create_text

    for label, x, y in self.outlierLabels:
      x0 = (x - dataRegion[0])*ppvX
      y0 = (y - dataRegion[1])*ppvY
      x0 += plotRegion[0]
      y0 += plotRegion[1]
      
      item = createText(x0+8,y0, text=label,
                        fill='#000000',anchor='w')
      self.contourItems.add(item)
Exemple #3
0
class EditFitGraphPopup(BasePopup):
    """
  **Analyse Function Curve Fitting to Peak Series Data**
  
  This popup is used to display the fit of a curve, of the displayed equation
  type, to data that has been extracted for a group of spectrum peaks from an
  NMR series. Precisely which kind of data is being fitted depends on the parent
  tool that this popup window was launched from. For example, for the `Follow
  Intensity Changes`_ tool the displayed graph is of a time or frequency value
  on the "X" axis (e.g. T1) verses peak intensity. For the `Follow Shift
  Changes`_ system the plot is for the parameters of the experiment titration on
  the "X" axis (e.g concentration) verses chemical shift distance.

  The upper graph shows the peak data that was used; the blue points, and the
  points of the fitted theoretical curve; red points. The lower table lists the
  data points for all of the peaks in the group, to which the data relates.
  There will be one peak for each row of the table, and hence point on the "X"
  axis. For each data point in the table the corresponding peak from which the
  data was extracted may be located with the "Follow in window" option, using
  the stated spectrum window and making marker lines as desired. Alternatively,
  the peak may be viewed in a table with [Show Peak]

  In general operation this system is used to check how well the selected
  function curve fits the peak data. The data that is displayed comes from the
  analysis tool that launched this popup. Any outliers may be removed using
  [Remove Point] (only with good reason) or the peaks themselves may be
  corrected if something has gone awry. Given that the display is launched from
  the selection of a specific group of peaks from a popup like `Follow Shift
  Changes`_  or `Follow Intensity Changes`_ and there are usually several peak
  groups that need to be analysed, the [Previous Set] and [Next Set] buttons can
  be used to quickly jump to the next peak group and see the next curve in the
  analysis results.

  .. _`Follow Intensity Changes`: CalcRatesPopup.html
  .. _`Follow Shift Changes`: FollowShiftChangesPopup.html

  """
    def __init__(self,
                 parent,
                 dataFitting,
                 getXYfunction,
                 updateFunction,
                 xLabel,
                 yLabel,
                 showObjectFunction=None,
                 nextSetFunction=None,
                 prevSetFunction=None,
                 graphTitle='',
                 **kw):

        self.guiParent = parent
        self.getXYfunction = getXYfunction
        self.updateFunction = updateFunction
        self.nextSetFunction = nextSetFunction
        self.prevSetFunction = prevSetFunction
        self.dataFitting = dataFitting
        self.object = None
        self.xLabel = xLabel
        self.yLabel = yLabel
        self.x = []
        self.y = []
        self.yFit = []
        self.params = ()
        self.chiSq = 0
        self.method = dataFitting.fitFunction
        self.waiting = False
        self.noiseLevel = dataFitting.noiseLevel
        self.graphTitle = graphTitle
        self.windowPane = None
        self.mark = None

        BasePopup.__init__(self, parent=parent, title='Fit Graph', **kw)
        parent.protocol("WM_DELETE_WINDOW", self.close)

    def body(self, guiFrame):

        guiFrame.grid_columnconfigure(0, weight=1)
        row = 0

        self.scrolledGraph = ScrolledGraph(guiFrame,
                                           width=400,
                                           height=300,
                                           symbolSize=5,
                                           symbols=['square', 'circle'],
                                           dataColors=['#000080', '#800000'],
                                           lineWidths=[0, 1],
                                           grid=(row, 0))

        #self.scrolledGraph.setZoom(0.7)

        row += 1
        frame = Frame(guiFrame, grid=(row, 0), sticky='ew')

        label = Label(frame, text='Fitting Function:', grid=(0, 0))

        tipText = 'Selects which form of function to fit to the experimental data'
        self.methodPulldown = PulldownList(frame,
                                           self.changeMethod,
                                           grid=(0, 1),
                                           tipText=tipText)

        row += 1
        frame = Frame(guiFrame, grid=(row, 0), sticky='ew')

        tipText = 'The effective equation of the final fitted graph, incorporating all parameters'
        self.equationLabel = Label(frame,
                                   text='Equation:',
                                   grid=(0, 0),
                                   tipText=tipText)

        tipText = 'The error in the fit of the selected parameterised function to the experimental data'
        self.errorLabel = Label(frame,
                                text='Fit Error:',
                                grid=(0, 1),
                                tipText=tipText)

        row += 1
        frame = Frame(guiFrame, grid=(row, 0), sticky='ew')

        label = Label(frame, text='Include x origin?:', grid=(0, 0))

        tipText = 'Whether to include the x=0 point in the drawing'
        self.xOriginSelect = CheckButton(frame,
                                         callback=self.draw,
                                         grid=(0, 1),
                                         selected=False,
                                         tipText=tipText)

        label = Label(frame, text='Include y origin?:', grid=(0, 2))

        tipText = 'Whether to include the y=0 point in the drawing'
        self.yOriginSelect = CheckButton(frame,
                                         callback=self.draw,
                                         grid=(0, 3),
                                         selected=False,
                                         tipText=tipText)

        label = Label(frame, text='Include y error?:', grid=(0, 4))

        tipText = 'Whether to include the y error bars in the drawing (if these exist)'
        self.yErrorSelect = CheckButton(frame,
                                        callback=self.draw,
                                        grid=(0, 5),
                                        selected=False,
                                        tipText=tipText)

        row += 1
        frame = Frame(guiFrame, grid=(row, 0), sticky='ew')

        label = Label(frame, text='Navigation Window:', grid=(0, 0))

        tipText = 'Selects which spectrum window will be used for navigating to peak positions'
        self.windowPanePulldown = PulldownList(frame,
                                               self.changeWindow,
                                               grid=(0, 1),
                                               tipText=tipText)

        label = Label(frame, text='Follow in window?:', grid=(0, 2))

        tipText = 'Whether to navigate to the position of the reference peak (for the group), in the selected window'
        self.followSelect = CheckButton(frame,
                                        callback=self.windowPaneNavigate,
                                        grid=(0, 3),
                                        selected=False,
                                        tipText=tipText)

        label = Label(frame, text='Mark Ref Peak?:', grid=(0, 4))

        tipText = 'Whether to put a multi-dimensional cross-mark through the reference peak position, so it can be identified in spectra'
        self.markSelect = CheckButton(frame,
                                      callback=None,
                                      tipText=tipText,
                                      grid=(0, 5),
                                      selected=False)

        row += 1
        guiFrame.grid_rowconfigure(row, weight=1)
        tipTexts = [
            'The number of the data point, in order of increasing X-axis value',
            'For each point, the value of the parameter which is varied in the NMR series, e.g. T1, temperature, concentration etc.',
            'For each point, the experimental value being fitted, e.g. peak intensity of chemical shift distance',
            'The value of the best-fit function at the X-axis location',
            'The difference between the experimental (Y-axis) value and the fitted value',
            'The error in the experimental (Y-axis) value'
        ]
        headingList = ['Point', 'x', 'y', 'Fitted y', u'\u0394', 'y error']
        self.scrolledMatrix = ScrolledMatrix(guiFrame,
                                             headingList=headingList,
                                             callback=self.selectObject,
                                             tipTexts=tipTexts,
                                             grid=(row, 0))

        row += 1
        tipTexts = [
            'Remove the selected data point, optionally removing the underlying peak',
            'Show a table of spectrum peaks that correspond to the selected data point'
        ]
        texts = ['Remove Point', 'Show Peak']
        commands = [self.removePoint, self.showObject]

        if self.prevSetFunction:
            texts.append('Previous Set')
            tipTexts.append(
                'Move to the previous set of fitted values; the next group of peaks, often corresponding to a different residue or resonance'
            )
            commands.append(self.prevSet)

        if self.nextSetFunction:
            tipTexts.append(
                'Move to the next set of fitted values; the next group of peaks, often corresponding to a different residue or resonance'
            )
            texts.append('Next Set')
            commands.append(self.nextSet)

        bottomButtons = UtilityButtonList(guiFrame,
                                          texts=texts,
                                          commands=commands,
                                          helpUrl=self.help_url,
                                          tipTexts=tipTexts,
                                          grid=(row, 0),
                                          doClone=False)
        self.removeButton = bottomButtons.buttons[0]

        for func in ('__init__', 'delete', 'setName'):
            self.registerNotify(self.updateWindows,
                                'ccpnmr.Analysis.SpectrumWindow', func)

        self.update()

    def destroy(self):

        for func in ('__init__', 'delete', 'setName'):
            self.unregisterNotify(self.updateWindows,
                                  'ccpnmr.Analysis.SpectrumWindow', func)

        BasePopup.destroy(self)

    def nextSet(self):

        if self.nextSetFunction:
            self.nextSetFunction()
            self.windowPaneNavigate()

    def prevSet(self):

        if self.prevSetFunction:
            self.prevSetFunction()
            self.windowPaneNavigate()

    def windowPaneNavigate(self, event=None):

        if self.followSelect.get() and self.dataFitting:
            peaks = self.dataFitting.objects
            if self.windowPane and peaks:
                peak = peaks[int(len(peaks) / 2)]

                windowFrame = self.windowPane.getWindowFrame()
                windowFrame.gotoPeak(peak)

                if self.markSelect.get():
                    if self.mark and not self.mark.isDeleted:
                        self.mark.delete()

                    self.mark = createPeakMark(self.dataFitting.refObject)

    def getWindows(self):

        peaks = self.scrolledMatrix.objectList

        windowPanes = []
        if peaks:
            peak = peaks[0]
            project = peak.root
            spectrum = peak.peakList.dataSource
            tryWindows = getActiveWindows(project)
            for window in tryWindows:
                for windowPane in window.sortedSpectrumWindowPanes():
                    if isSpectrumInWindowPane(windowPane, spectrum):
                        windowPanes.append(windowPane)

        return windowPanes

    def changeWindow(self, windowPane):

        if windowPane is not self.windowPane:
            self.windowPane = windowPane
            self.windowPaneNavigate()

    def updateWindows(self, window=None):

        windowPanes = self.getWindows()
        index = -1
        names = [getWindowPaneName(wp) for wp in windowPanes]
        windowPane = self.windowPane

        if windowPanes:
            if windowPane not in windowPanes:
                windowPane = windowPanes[0]

            index = windowPanes.index(windowPane)

        if windowPane is not self.windowPane:
            self.windowPane = windowPane
            self.windowPaneNavigate()

        self.windowPanePulldown.setup(names, windowPanes, index)

    def showObject(self):

        if self.object and (self.object.className == 'Peak'):
            peaks = [self.object]
            self.guiParent.guiParent.viewPeaks(peaks)

    def close(self):

        BasePopup.close(self)

    def changeMethod(self, index):

        if self.dataFitting:
            self.method = index
            self.dataFitting.fitFunction = self.method

        self.updateAfter()

    def updateMethods(self, *opt):

        n = len(METHOD_NAMES)
        if self.method >= n:
            self.method = 1

        self.methodPulldown.setup(METHOD_NAMES, range(n), self.method)

    def selectObject(self, object, row, col):

        if object:
            self.object = object
            self.updateButtons()

    def removePoint(self):

        if self.object and (len(self.dataFitting.objects) > 2):
            self.dataFitting.objects.remove(self.object)
            if showYesNo('Query',
                         'Delete the corresponding %s?' %
                         (self.object.className),
                         parent=self):
                self.object.delete()
            self.updateAfter()

    def draw(self, *junk):

        title = '%s Function fit %s' % (self.graphTitle,
                                        METHOD_NAMES[self.method])
        dataSet1 = []
        dataSet2 = []
        useErr = self.yErrorSelect.isSelected()
        if not useErr:
            useErr = None
        for i in range(len(self.scrolledMatrix.textMatrix)):
            row = self.scrolledMatrix.textMatrix[i]
            x = row[1]
            y = row[2]
            y2 = row[3]
            err = useErr and row[5]
            dataSet1.append([x, y, err])
            dataSet2.append([x, y2])

        dataSet1.sort()
        dataSet2.sort()

        if dataSet1 and dataSet2:
            drawOriginX = self.xOriginSelect.isSelected()
            drawOriginY = self.yOriginSelect.isSelected()
            self.scrolledGraph.update(dataSets=[dataSet1, dataSet2],
                                      xLabel=self.xLabel,
                                      yLabel=self.yLabel,
                                      title=title,
                                      drawOriginX=drawOriginX,
                                      drawOriginY=drawOriginY)
            self.scrolledGraph.draw()

    def updateAfter(self, *object):

        if self.waiting:
            return
        else:
            self.waiting = True
            self.after_idle(self.update)

    def update(self,
               dataFitting=None,
               xLabel=None,
               yLabel=None,
               graphTitle=None,
               force=False):

        if (not force) and dataFitting and (self.dataFitting is dataFitting):
            if (self.method, self.xLabel, self.yLabel, self.noiseLevel) == \
               (self.dataFitting.fitFunction, xLabel, yLabel, self.dataFitting.noiseLevel):
                self.waiting = False
                return

        if dataFitting:
            self.dataFitting = dataFitting

        self.xLabel = xLabel or self.xLabel
        self.yLabel = yLabel or self.yLabel
        self.graphTitle = graphTitle or self.graphTitle

        if not self.dataFitting:
            self.waiting = False
            return
        else:
            dataFitting = self.dataFitting
            dataFitting = self.getXYfunction(dataFitting)
            if dataFitting.fitFunction is not None:
                self.method = dataFitting.fitFunction
            self.noiseLevel = dataFitting.noiseLevel or self.noiseLevel
        self.updateMethods()

        isFitted = dataFitting.fit()

        textMatrix = []
        objectList = []

        if isFitted and self.method:
            methodInfo = getFitMethodInfo()[self.method - 1]
            textFormat, indices = methodInfo[2]
            textParams = [dataFitting.parameters[i] or 0 for i in indices]
            equationText = textFormat % tuple(textParams)
            errorText = '%4f' % dataFitting.fitError

            x = dataFitting.dataX
            y = dataFitting.dataY
            yFit = dataFitting.fittedY
            N = len(x)
            err = hasattr(dataFitting, 'dataErr') and dataFitting.dataErr
            if not err:
                err = None

            # wb104: 10 Mar 2014: not sure why the below was here, it isn't needed
            #if self.method == 10:
            #  x = [sqrt(v) for v in x]

            data = [(x[i], y[i], yFit[i], y[i] - yFit[i],
                     dataFitting.objects[i], err and err[i]) for i in range(N)]
            data.sort()
            for i in range(N):
                xi, yi, yFiti, deltai, object, erri = data[i]
                textMatrix.append([i + 1, xi, yi, yFiti, deltai, erri])
                objectList.append(object)

        else:
            equationText = 'Equation: <No Fit>'
            errorText = '<None>'
            x = dataFitting.dataX
            y = dataFitting.dataY

            for i, x in enumerate(x):
                textMatrix.append([i + 1, x, y[i], None, None, None])
                objectList.append(dataFitting.objects[i])

        self.equationLabel.set('Equation: %s' % equationText)
        self.errorLabel.set('Fit Error: %s' % errorText)
        self.scrolledMatrix.update(textMatrix=textMatrix,
                                   objectList=objectList)

        self.updateWindows()
        self.updateButtons()

        self.draw()

        if self.updateFunction:
            self.updateFunction(dataFitting)

        self.waiting = False

    def updateButtons(self):

        if self.object:
            self.removeButton.enable()
        else:
            self.removeButton.disable()
Exemple #4
0
class PopupTemplate(BasePopup):

  def __init__(self, parent, project=None, *args, **kw):

    self.project = project
    self.parent  = parent
    self.objects = self.getObjects()
    self.object  = None
    
    BasePopup.__init__(self, parent=parent, title='Popup Template', **kw)
                       
    self.updateObjects()

  def body(self, mainFrame):

    mainFrame.grid_columnconfigure(1, weight=1, minsize=100)
    mainFrame.config(borderwidth=5, relief='solid')

    row = 0
    label = Label(mainFrame, text="Frame (with sub-widgets):")
    label.grid(row=row, column=0, sticky=Tkinter.E)

    frame = Frame(mainFrame, relief='raised', border=2, background='#8080D0')
    # Frame expands East-West
    frame.grid(row=row, column=1, sticky=Tkinter.EW)
    # Last column expands => Widgets pusted to the West
    frame.grid_columnconfigure(3, weight=1)
    
    # Label is within the sub frame
    label = Label(frame, text='label ')
    label.grid(row=0, column=0, sticky=Tkinter.W)
    
    entry = Entry(frame, text='Entry', returnCallback=self.showWarning)
    entry.grid(row=0, column=1, sticky=Tkinter.W)
    
    self.check = CheckButton(frame, text='Checkbutton', selected=True, callback=self.updateObjects)
    self.check.grid(row=0, column=2, sticky=Tkinter.W)
    
    # stick a button to the East wall
    button = Button(frame, text='Button', command=self.pressButton)
    button.grid(row=0, column=3, sticky=Tkinter.E)
  
    row += 1
    label = Label(mainFrame, text="Text:")
    label.grid(row=row, column=0, sticky=Tkinter.E)
    self.textWindow = Text(mainFrame, text='Initial Text\n', width=60, height=5)
    self.textWindow.grid(row=row, column=1, sticky=Tkinter.NSEW)
    
    row += 1
    label = Label(mainFrame, text="CheckButtons:")
    label.grid(row=row, column=0, sticky=Tkinter.E)

    entries = ['Alpha','Beta','Gamma','Delta']
    selected = entries[2:]
    self.checkButtons = CheckButtons(mainFrame, entries, selected=selected,select_callback=self.changedCheckButtons)
    self.checkButtons.grid(row=row, column=1, sticky=Tkinter.W)
  
    row += 1
    label = Label(mainFrame, text="PartitionedSelector:")
    label.grid(row=row, column=0, sticky=Tkinter.E)

    labels   = ['Bool','Int','Float','String']
    objects  = [type(0),type(1),type(1.0),type('a')]
    selected = [type('a')]
    self.partitionedSelector= PartitionedSelector(mainFrame, labels=labels,
                                                  objects=objects,
                                                  colors = ['red','yellow','green','#000080'],
                                                  callback=self.toggleSelector,selected=selected)
    self.partitionedSelector.grid(row=row, column=1, sticky=Tkinter.EW)

    row += 1
    label = Label(mainFrame, text="PulldownMenu")
    label.grid(row=row, column=0, sticky=Tkinter.E)
    
    entries = ['Frodo','Pipin','Merry','Sam','Bill','Gandalf','Strider','Gimli','Legolas']
    self.pulldownMenu = PulldownMenu(mainFrame, callback=self.selectPulldown,
                                     entries=entries, selected_index=2,
                                     do_initial_callback=False)
    self.pulldownMenu.grid(row=row, column=1, sticky=Tkinter.W)

    row += 1
    label = Label(mainFrame, text="RadioButtons in a\nScrolledFrame.frame:")
    label.grid(row=row, column=0, sticky=Tkinter.EW)
    
    frame = ScrolledFrame(mainFrame, yscroll = False, doExtraConfig = True, width=100)
    frame.grid(row=row, column=1, sticky=Tkinter.EW)
    frame.grid_columnconfigure(0, weight=1)

    self.radioButtons = RadioButtons(frame.frame, entries=entries,
                                     select_callback=self.checkRadioButtons,
                                     selected_index=1, relief='groove')
    self.radioButtons.grid(row=0, column=0, sticky=Tkinter.W)
    
    row += 1
    label = Label(mainFrame, text="LabelFrame with\nToggleLabels inside:")
    label.grid(row=row, column=0, sticky=Tkinter.E)

    labelFrame = LabelFrame(mainFrame, text='Frame Title')
    labelFrame.grid(row=row, column=1, sticky=Tkinter.NSEW)
    labelFrame.grid_rowconfigure(0, weight=1)
    labelFrame.grid_columnconfigure(3, weight=1)
    
        
    self.toggleLabel1 = ToggleLabel(labelFrame, text='ScrolledMatrix', callback=self.toggleFrame1)
    self.toggleLabel1.grid(row=0, column=0, sticky=Tkinter.W)
    self.toggleLabel1.arrowOn()

    self.toggleLabel2 = ToggleLabel(labelFrame, text='ScrolledGraph', callback=self.toggleFrame2)
    self.toggleLabel2.grid(row=0, column=1, sticky=Tkinter.W)

    self.toggleLabel3 = ToggleLabel(labelFrame, text='ScrolledCanvas', callback=self.toggleFrame3)
    self.toggleLabel3.grid(row=0, column=2, sticky=Tkinter.W)
    
    row += 1
    mainFrame.grid_rowconfigure(row, weight=1)

    label = Label(mainFrame, text="changing/shrinking frames:")
    label.grid(row=row, column=0, sticky=Tkinter.E)
    
    self.toggleRow = row
    self.toggleFrame = Frame(mainFrame)
    self.toggleFrame.grid(row=row, column=1, sticky=Tkinter.NSEW)
    self.toggleFrame.grid_rowconfigure(0, weight=1)
    self.toggleFrame.grid_columnconfigure(0, weight=1)
    
    # option 1
    
    self.intEntry = IntEntry(self, returnCallback = self.setNumber, width=8)
    
    self.multiWidget = MultiWidget(self, Entry, options=None, 
                                  values=None, callback=self.setKeywords,
                                  minRows=3, maxRows=5)

    editWidgets      = [None, None, self.intEntry,  self.multiWidget]
    editGetCallbacks = [None, None, self.getNumber, self.getKeywords]
    editSetCallbacks = [None, None, self.setNumber, self.setKeywords]
    
    headingList = ['Name','Color','Number','Keywords']
    self.scrolledMatrix = ScrolledMatrix(self.toggleFrame, headingList=headingList,
                                         editSetCallbacks=editSetCallbacks,
                                         editGetCallbacks=editGetCallbacks,
                                         editWidgets=editWidgets,
                                         callback=self.selectObject,
                                         multiSelect=False) 
                                         
    self.scrolledMatrix.grid(row=0, column=0, sticky=Tkinter.NSEW)

    # option 2
    self.scrolledGraph = ScrolledGraph(self.toggleFrame, width=400,
                                       height=300, symbolSize=5,
                                       symbols=['square','circle'],
                                       dataColors=['#000080','#800000'],
                                       lineWidths=[0,1] )

    self.scrolledGraph.setZoom(1.3)

    dataSet1 = [[0,0],[1,1],[2,4],[3,9],[4,16],[5,25]]
    dataSet2 = [[0,0],[1,3],[2,6],[3,9],[4,12],[5,15]]
    self.scrolledGraph.update(dataSets=[dataSet1,dataSet2],
                              xLabel = 'X axis label',
                              yLabel = 'Y axis label',
                              title  = 'Main Title')
    self.scrolledGraph.draw()

    # option 3
    self.scrolledCanvas = ScrolledCanvas(self.toggleFrame,relief = 'groove', borderwidth = 2, resizeCallback=None)
    canvas = self.scrolledCanvas.canvas
    font   = 'Helvetica 10'
    box    = canvas.create_rectangle(10,10,150,200, outline='grey', fill='grey90')
    line   = canvas.create_line(0,0,200,200,fill='#800000', width=2)
    text   = canvas.create_text(120,50, text='Text', font=font, fill='black')
    circle = canvas.create_oval(30,30,50,50,outline='#008000',fill='#404040',width=3)
     
    row += 1
    label = Label(mainFrame, text="FloatEntry:")
    label.grid(row=row, column=0, sticky=Tkinter.E)
    self.floatEntry = FloatEntry(mainFrame, text=3.14159265, returnCallback=self.floatEntryReturn)
    self.floatEntry.grid(row=row, column=1, sticky=Tkinter.W)
    
     
    row += 1
    label = Label(mainFrame, text="Scale:")
    label.grid(row=row, column=0, sticky=Tkinter.E)
    self.scale = Scale(mainFrame, from_=10, to=90, value=50, orient=Tkinter.HORIZONTAL)
    self.scale.grid(row=row, column=1, sticky=Tkinter.W)

    row += 1
    label = Label(mainFrame, text="Value Ramp:")
    label.grid(row=row, column=0, sticky=Tkinter.E)
    self.valueRamp = ValueRamp(mainFrame, self.valueRampCallback, speed = 1.5, delay = 50)
    self.valueRamp.grid(row=row, column=1, sticky=Tkinter.W)
  

    row += 1
    label = Label(mainFrame, text="ButtonList:")
    label.grid(row=row, column=0, sticky=Tkinter.E)
    
    texts    = ['Select File','Close','Quit']
    commands = [self.selectFile, self.close, self.quit]
    bottomButtons = ButtonList(mainFrame, texts=texts, commands=commands, expands=True) 
    bottomButtons.grid(row=row, column=1, sticky=Tkinter.EW)
  
    self.protocol('WM_DELETE_WINDOW', self.quit)

  def floatEntryReturn(self, event):
  
    value = self.floatEntry.get()
    self.textWindow.setText('%s\n' % value)

  def selectObject(self, object, row, col):
  
    self.object = object

  def getKeywords(self, object):
  
    if object :
      values = object.keywords
      self.multiWidget.set(values)
  
  def setKeywords(self, event):
  
    values = self.multiWidget.get()
    self.object.keywords = values
    self.updateObjects()

  def getNumber(self, object):

    if object :
      self.intEntry.set(object.quantity)

  def setNumber(self, event):

    value = self.intEntry.get()
    self.object.quantity = value
    self.updateObjects()

  def toggleFrame1(self, isHidden):
 
    if isHidden:
      self.scrolledMatrix.grid_forget()
      self.toggleFrame.grid_forget()
    else:
      self.scrolledGraph.grid_forget()
      self.scrolledCanvas.grid_forget()
      self.scrolledMatrix.grid(row=0, column=0, sticky=Tkinter.NSEW)
      self.toggleFrame.grid(row=self.toggleRow, column=1,sticky=Tkinter.NSEW)
      self.toggleLabel2.arrowOff()
      self.toggleLabel3.arrowOff()


  def toggleFrame2(self, isHidden):
 
    if isHidden:
      self.scrolledGraph.grid_forget()
      self.toggleFrame.grid_forget()
    else:
      self.scrolledMatrix.grid_forget()
      self.scrolledCanvas.grid_forget()
      self.scrolledGraph.grid(row=0, column=0, sticky=Tkinter.NSEW)
      self.toggleFrame.grid(row=self.toggleRow, column=1,sticky=Tkinter.NSEW)
      self.toggleLabel1.arrowOff()
      self.toggleLabel3.arrowOff()

  def toggleFrame3(self, isHidden):
 
    if isHidden:
      self.scrolledCanvas.grid_forget()
      self.toggleFrame.grid_forget()
    else:
      self.scrolledMatrix.grid_forget()
      self.scrolledGraph.grid_forget()
      self.scrolledCanvas.grid(row=0, column=0, sticky=Tkinter.NSEW)
      self.toggleFrame.grid(row=self.toggleRow, column=1,sticky=Tkinter.NSEW)
      self.toggleLabel1.arrowOff()
      self.toggleLabel2.arrowOff()

 
  def valueRampCallback(self, value):
  
    self.textWindow.setText('%s\n' % value)

  def checkRadioButtons(self, value):
  
    self.textWindow.setText('%s\n' % value)

  def selectPulldown(self, index, name):
  
    self.textWindow.setText('%d, %s\n' % (index, name))

  def toggleSelector(self, value):
  
    self.textWindow.setText('%s\n' % value)

  def changedCheckButtons(self, values):
  
    self.textWindow.setText(','.join(values) + '\n')

  def getObjects(self):
  
    objects = []
    
    objects.append( Fruit('Lemon',   '#FFFF00',1,keywords=['Bitter','Tangy'] ) )
    objects.append( Fruit('Orange',  '#FF8000',4 ) )
    objects.append( Fruit('Banana',  '#FFF000',5 ) )
    objects.append( Fruit('Pinapple','#FFD000',9 ) )
    objects.append( Fruit('Kiwi',    '#008000',12) )
    objects.append( Fruit('Lime',    '#00FF00',2 ) )
    objects.append( Fruit('Apple',   '#800000',5,keywords=['Crunchy'] ) )
    objects.append( Fruit('Pear',    '#408000',6 ) )
    objects.append( Fruit('Peach',   '#FFE0C0',2,keywords=['Sweet','Furry'] ) )
    objects.append( Fruit('Plumb',   '#800080',7 ) )
    
    return objects

  def updateObjects(self, event=None):

    textMatrix = []
    objectList = []
    colorMatrix = []
    
    for object in self.objects:
      datum = []
      datum.append( object.name )
      datum.append( None )
      datum.append( object.quantity )
      datum.append( ','.join(object.keywords) )
    
      colors = [None, object.color, None, None]
    
      textMatrix.append(datum)
      objectList.append(object)
      colorMatrix.append(colors)

    if self.check.get():
      self.scrolledMatrix.update(textMatrix=textMatrix, objectList=objectList)
    else:
      self.scrolledMatrix.update(textMatrix=textMatrix, objectList=objectList, colorMatrix=colorMatrix)

  def selectFile(self):

    fileSelectPopup = FileSelectPopup(self, title = 'Choose file', dismiss_text = 'Cancel',
                                      selected_file_must_exist = True)

    fileName = fileSelectPopup.getFile()

    self.textWindow.setText('File Selected: %s\n' % fileName)

  def showWarning(self, eventObject):
  
    self.textWindow.setText('Text Entry Return Pressed\n')
    showWarning('Warning Title','Warning Message')
    return

  def pressButton(self):
  
    self.textWindow.setText('Button Pressed\n')
    if showYesNo('Title','Prompt: Clear text window?'):
       self.textWindow.clear()
       
    return

  def quit(self):
  
    BasePopup.destroy(self)  
class AnnealingSettingsTab(object):
    '''This class describes the tab in the GUI where the user
       can change setting that govern the monte carlo / annleaing
       procedure. This also includes which information from the ccpn
       analysis project is used and which information is
       ignored. This includes:
           * present sequential assignments
           * tentative assignments
           * amino acid type information
           * whether to include untyped spin systems
           * assignments to peak dimensions
       ALso the chain can be selected here.
       Furthermore the user can set the temperature
       regime of the annealing, the amount of times the procedure
       is repeated to obtain statistics. The fraction of peaks
       that is left out in each run to diversify the results,
       the treshhold score for amino acid typing and the treshhold
       collabelling for a peak to be expected.
    '''

    def __init__(self, parent, frame):
        '''Init. args: parent: the guiElement that this
                               tab is part of.
                       frame:  the frame this part of the
                               GUI lives in.
        '''

        self.guiParent = parent
        self.frame = frame
        self.project = parent.project
        self.nmrProject = parent.nmrProject

        self.minIsoFrac = 0.1
        self.leavePeaksOutFraction = 0.0
        self.minTypeScore = 1.0
        self.chain = None
        self.amountOfRepeats = 10
        self.amountOfSteps = 10000
        self.acceptanceConstantList = [0.0, 0.01, 0.015, 0.022,
                                       0.033, 0.050, 0.075, 0.113,
                                       0.170, 0.256, 0.384, 0.576,
                                       0.864, 1.297, 1.946, 2.919,
                                       4.378, 6.568, 9.852, 14.77,
                                       22.16, 33.25]
        self.energyDataSets = [[]]
        self.residues = []
        self.body()

    def body(self):
        '''describes the body of this tab. It bascically consists
           of some field to fill out for the user at the top and
           a ScrolledGraph that shows the progess of the annealing
           procedure a the bottom.
        '''

        frame = self.frame

        # frame.expandGrid(13,0)
        frame.expandGrid(15, 1)
        row = 0

        text = 'Calculate Assignment Suggestions'
        command = self.runCalculations
        self.startButton = Button(frame, command=command, text=text)
        self.startButton.grid(row=row, column=0, sticky='nsew', columnspan=2)

        row += 1

        Label(frame, text='Amount of runs: ', grid=(row, 0))
        tipText = 'The amount of times the whole optimization procedure is performed, each result is safed'
        self.repeatEntry = IntEntry(frame, grid=(row, 1), width=7, text=10,
                                    returnCallback=self.updateRepeatEntry,
                                    tipText=tipText, sticky='nsew')
        self.repeatEntry.bind('<Leave>', self.updateRepeatEntry, '+')

        row += 1

        Label(frame, text='Temperature regime: ', grid=(row, 0))
        tipText = 'This list of numbers govern the temperature steps during the annealing, every number represents 1/(kb*t), where kb is the Boltzmann constant and t the temperature of one step.'
        self.tempEntry = Entry(frame, text=map(str, self.acceptanceConstantList), width=64,
                               grid=(row, 1), isArray=True, returnCallback=self.updateAcceptanceConstantList,
                               tipText=tipText, sticky='nsew')

        row += 1

        Label(frame, text='Amount of attempts per temperature:', grid=(row, 0))
        tipText = 'The amount of attempts to switch the position of two spinsystems in the sequence are performed for each temperature point'
        self.NAStepEntry = IntEntry(frame, grid=(row, 1), width=7, text=10000,
                                    returnCallback=self.updateStepEntry,
                                    tipText=tipText, sticky='nsew')
        self.NAStepEntry.bind('<Leave>', self.updateStepEntry, '+')

        row += 1

        Label(frame, text='Fraction of peaks to leave out:', grid=(row, 0))
        tipText = 'In each run a fraction of the peaks can be left out of the optimization, thereby increasing the variability in the outcome and reducing false negatives. In each run this will be different randomly chosen sub-set of all peaks. 0.1 (10%) can be a good value.'
        self.leaveOutPeaksEntry = FloatEntry(frame, grid=(row, 1), width=7, text=0.0,
                                             returnCallback=self.updateLeavePeaksOutEntry,
                                             tipText=tipText, sticky='nsew')
        self.leaveOutPeaksEntry.bind(
            '<Leave>', self.updateLeavePeaksOutEntry, '+')

        row += 1

        Label(frame, text='Minmal amino acid typing score:', grid=(row, 0))
        tipText = 'If automatic amino acid typing is selected, a cut-off value has to set. Every amino acid type that scores higher than the cut-off is taken as a possible type. This is the same score as can be found under resonance --> spin systems --> predict type. Value should be between 0 and 100'
        self.minTypeScoreEntry = FloatEntry(frame, grid=(row, 1), width=7, text=1.0,
                                            returnCallback=self.updateMinTypeScoreEntry,
                                            tipText=tipText, sticky='nsew')
        self.minTypeScoreEntry.bind(
            '<Leave>', self.updateMinTypeScoreEntry, '+')

        row += 1

        Label(frame, text='Minimal colabelling fraction:', grid=(row, 0))
        tipText = 'The minimal amount of colabelling the different nuclei should have in order to still give rise to a peak.'
        self.minLabelEntry = FloatEntry(frame, grid=(row, 1), width=7, text=0.1,
                                        returnCallback=self.updateMinLabelEntry,
                                        tipText=tipText, sticky='nsew')
        self.minLabelEntry.bind('<Leave>', self.updateMinLabelEntry, '+')

        row += 1

        Label(frame, text='Use sequential assignments:', grid=(row, 0))
        tipText = 'When this option is select the present sequential assignments will be kept in place'
        self.useAssignmentsCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Use tentative assignments:', grid=(row, 0))
        tipText = 'If a spin system has tentative assignments this can be used to narrow down the amount of possible sequential assignments.'
        self.useTentativeCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Use amino acid types:', grid=(row, 0))
        tipText = 'Use amino acid types of the spin systems. If this option is not checked the spin systems are re-typed, only resonance names and frequencies are used'
        self.useTypeCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Include untyped spin systems:', grid=(row, 0))
        tipText = 'Also include spin system that have no type information. Amino acid typing will be done on the fly.'
        self.useAlsoUntypedSpinSystemsCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Use dimensional assignments:', grid=(row, 0))
        tipText = 'If one or more dimensions of a peak is already assigned, assume that this assignment is the only option. If not the check the program will consider all possibilities for the assignment of the dimension.'
        self.useDimensionalAssignmentsCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Chain:', grid=(row, 0))
        self.molPulldown = PulldownList(
            frame, callback=self.changeMolecule, grid=(row, 1))
        self.updateChains()

        row += 1

        Label(frame, text='Residue ranges: ', grid=(row, 0))
        tipText = 'Which residues should be included. Example: "10-35, 62-100, 130".'
        self.residueRangeEntry = Entry(frame, text=None, width=64,
                                       grid=(row, 1), isArray=True, returnCallback=self.updateResidueRanges,
                                       tipText=tipText, sticky='nsew')
        self.updateResidueRanges(fromChain=True)

        row += 1

        self.energyPlot = ScrolledGraph(frame, symbolSize=2, width=600,
                                        height=200, title='Annealing',
                                        xLabel='temperature step', yLabel='energy')
        self.energyPlot.grid(row=row, column=0, columnspan=2, sticky='nsew')

    def runCalculations(self):
        '''Run all calculations. Also triggers the disabling of
           some buttons and fields.
        '''

        self.startButton.disable()
        self.disableIllegalButtonsAfterPrecalculations()
        self.guiParent.connector.runAllCalculations()
        self.startButton.configure(text='More runs')
        self.startButton.enable()

    def disableIllegalButtonsAfterPrecalculations(self):
        '''Disable buttons and field the user can not alter
           any longer after the model is set up and the
           'pre-calculations' have finished.
           This is done because this part of the calculation
           should only be run once. All settings that would
           be changed after this point will not have any influence.
        '''

        illegalButtons = [self.minTypeScoreEntry, self.minLabelEntry,
                          self.useAlsoUntypedSpinSystemsCheck, self.useAssignmentsCheck,
                          self.useTypeCheck, self.useDimensionalAssignmentsCheck,
                          self.useTentativeCheck]

        for illegalButton in illegalButtons:
            illegalButton.configure(state='disabled')

        self.molPulldown.disable()

    def getChainName(self, chain):
        '''Get the name for a chain.
               args: chain: ccpn analysis
                            chain object
               returns: chain name
        '''

        return '%s:%s (%s)' % (chain.molSystem.code, chain.code, chain.molecule.molType)

    def getChains(self):
        '''Get all chains present in the project.
               returns: list of ccpn analysis chain objects
        '''
        chains = []
        if self.project:
            for molSystem in self.project.sortedMolSystems():
                for chain in molSystem.sortedChains():
                    if chain.residues:
                        chains.append(chain)

        return chains

    def updateChains(self, *opt):
        '''Updates the list of chains if a new one is added
           to or deleted from the project. Updates the
           pull down list where a chain can be selected.
        '''

        index = 0
        texts = []
        chains = self.getChains()
        chain = self.chain

        if chains:
            if chain not in chains:
                chain = chains[0]

            texts = [self.getChainName(c) for c in chains]
            index = chains.index(chain)

        else:
            chain = None

        self.molPulldown.setup(texts, chains, index)

        if chain is not self.chain:
            self.chain = chain

    def changeMolecule(self, chain):
        '''Select a molecular chain.'''

        if chain is not self.chain:
            self.chain = chain
            self.updateResidueRanges(fromChain=True)

    def updateStepEntry(self, event=None):
        '''Update the value and entry that sets the amount of
           steps per temperature point.
        '''

        value = self.NAStepEntry.get()
        if value == self.amountOfSteps:
            return
        if value < 1:
            self.NAStepEntry.set(1)
            self.amountOfSteps = 1
        else:
            self.amountOfSteps = value
            self.NAStepEntry.set(value)

    def updateRepeatEntry(self, event=None):
        '''Update the value and entry of that sets
           the amount of times the whole annealing
           procedure is repeated in order
           to obtain statistics.
        '''

        value = self.repeatEntry.get()

        if value == self.amountOfRepeats:
            return
        if value < 1:
            self.repeatEntry.set(1)
            self.amountOfRepeats = 1
        else:
            self.amountOfRepeats = value
            self.repeatEntry.set(value)

    def updateMinTypeScoreEntry(self, event=None):
        '''Updates the value and the entry for the
           treshhold value for amino acid typing.
        '''

        value = self.minTypeScoreEntry.get()

        if value == self.minTypeScore:
            return
        if value < 0:
            self.minTypeScoreEntry.set(0.0)
            self.minTypeScore = 0.0
        elif value > 100:
            self.minTypeScoreEntry.set(100.0)
            self.minTypeScore = 100.0
        else:
            self.minTypeScoreEntry.set(value)
            self.minTypeScore = value

    def updateMinLabelEntry(self, event=None):
        '''Updates the minimum colabelling fraction
           for which a peak is expected to be present
           in the spectra.
        '''

        value = self.minLabelEntry.get()

        if value == self.minIsoFrac:
            return
        if value < 0:
            self.minIsoFrac = 0.0
            self.minLabelEntry.set(0.0)
        elif value > 1:
            self.minIsoFrac = 1.0
            self.minLabelEntry.set(1.0)
        else:
            self.minIsoFrac = value
            self.minLabelEntry.set(value)

    def updateLeavePeaksOutEntry(self, event=None):
        '''Updates the value and entry of the fraction
           of peaks that should be left out in each
           run in order to diversify the results.
        '''

        value = self.leaveOutPeaksEntry.get()

        if value == self.leavePeaksOutFraction:
            return
        if value < 0:
            self.leavePeaksOutFraction = 0.0
            self.leaveOutPeaksEntry.set(0.0)
        elif value > 1:
            self.leavePeaksOutFraction = 1.0
            self.leaveOutPeaksEntry.set(1.0)
        else:
            self.leavePeaksOutFraction = value
            self.leaveOutPeaksEntry.set(value)

    def updateAcceptanceConstantList(self, event=None):
        '''Updates the list with constants that are used
           during the monte carlo procedure to decide whether
           a changed is accepted or not.
        '''

        acList = self.tempEntry.get()
        newList = []

        for constant in acList:

            try:

                number = float(constant)
                newList.append(number)

            except ValueError:

                string = constant + \
                    ' in temperature constants is not a number.'

                showWarning('Not A Number', string, parent=self.guiParent)

                return False

        self.acceptanceConstantList = newList

        return True

    def updateResidueRanges(self, event=None, fromChain=False):

        self.residues = set()

        subRanges = self.residueRangeEntry.get()
        if not subRanges or fromChain:
            self.residues = set(self.chain.residues)
            residues = self.chain.sortedResidues()
            text = '{}-{}'.format(residues[0].seqCode, residues[-1].seqCode)
            self.residueRangeEntry.set(text=text)
            return

        for subRange in subRanges:
            indeces = subRange.split('-')
            start = int(indeces[0])
            stop = int(indeces[-1]) + 1
            for seqCode in range(start, stop):
                residue = self.chain.findFirstResidue(seqCode=seqCode)
                if not residue:
                    showWarning('Residue out of range.',
                                'There is no residue at position {}'.format(seqCode),
                                parent=self.guiParent)
                    self.residues = set()
                    return
                self.residues.add(residue)

    def addEnergyPoint(self, energy, time):
        '''Adds a point to the graph that shows the progress
           of the annealling procedure.
               args: energy: the y-value
                     time:   the x-value
        '''

        point = (time, energy * -1)

        # This means one run has finished
        if len(self.energyDataSets[-1]) / (len(self.acceptanceConstantList) + 1):

            self.energyDataSets.append([point])

        else:

            self.energyDataSets[-1].append(point)

        colors = colorSeries
        Ncolors = len(colors)
        NdataSets = len(self.energyDataSets)
        colorList = (NdataSets / Ncolors) * colors + \
            colors[:NdataSets % Ncolors]
        self.energyPlot.update(dataSets=self.energyDataSets,
                               dataColors=colorList)

        # Forcing the graph to draw, eventhough calculations
        # are still running. Only do this with high numbers of
        # steps, otherwise drawing takes longer than annealling.
        if self.amountOfSteps >= 100000:

            self.energyPlot.draw()