File: background.py

package info (click to toggle)
pycorrfit 0.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,632 kB
  • ctags: 733
  • sloc: python: 9,403; sh: 29; makefile: 23
file content (679 lines) | stat: -rw-r--r-- 27,476 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# -*- coding: utf-8 -*-
""" PyCorrFit

    Module tools - background
    We make some background corection here.

    Dimensionless representation:
    unit of time        : 1 ms
    unit of inverse time: 10³ /s
    unit of distance    : 100 nm
    unit of Diff.coeff  : 10 µm²/s
    unit of inverse area: 100 /µm²
    unit of inv. volume : 1000 /µm³

    Copyright (C) 2011-2012  Paul Müller

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License 
    along with this program. If not, see <http://www.gnu.org/licenses/>.
"""


import numpy as np
import os
import sys
import traceback                        # for Error handling
import wx
from wx.lib.agw import floatspin        # Float numbers in spin fields
import wx.lib.plot as plot    

import doc
import misc
import openfile as opf                  # How to treat an opened file
import readfiles

# Menu entry name
MENUINFO = ["&Background correction", "Open a file for background correction."]

class BackgroundCorrection(wx.Frame):
    def __init__(self, parent):
        self.MyName="BACKGROUND"
        # Parent is main frame
        self.parent = parent
        # Get the window positioning correctly
        pos = self.parent.GetPosition()
        pos = (pos[0]+100, pos[1]+100)
        wx.Frame.__init__(self, parent=parent, title="Background correction",
                 pos=pos, style=wx.DEFAULT_FRAME_STYLE|wx.FRAME_FLOAT_ON_PARENT)
        ## MYID
        # This ID is given by the parent for an instance of this class
        self.MyID = None
        # Current trace we are looking at
        self.activetrace = None 
        # Importet trace
        self.trace = None
        # Importet trace after user decides to cange radio buttons
        self.oldtrace = None
        self.oldfilename = None
        self.average = None
        ## Start drawing
        # Splitter Window
        self.sp = wx.SplitterWindow(self, style=wx.SP_NOBORDER)
        ## Controls
        panel = wx.Panel(self.sp)
        # text1
        backgroundinit = (
            "Correct the amplitude for non-correlated background.\n"+
            "The background intensity <B> can be either imported\n"+
            "from a blank measurement or set manually.")
        textinit = wx.StaticText(panel, label=backgroundinit)
        # Radio buttons
        self.rbtnfile = wx.RadioButton(panel, -1, 'Blank measurement: ', 
                                        style = wx.RB_GROUP)
        self.rbtnfile.SetValue(True)
        self.btnbrowse = wx.Button(panel, wx.ID_ANY, 'Browse ...')
        self.rbtnhand = wx.RadioButton (panel, -1, 'Manual, <B> [kHz]: ')
        # Spincontrol
        self.spinctrl = floatspin.FloatSpin(panel, digits=4, min_val=0,
                                            increment=.01)
        self.spinctrl.Enable(False)
        # Verbose text
        self.textfile = wx.StaticText(panel,
                                    label="No blank measurement file selected.")
        textmeanavg = wx.StaticText(panel,
                                    label="Average background signal [kHz]: ")
        self.textmean = wx.StaticText(panel, label="")
        # name
        textname = wx.StaticText(panel, label="User defined background name: ")
        sizeTextn = textname.GetSize()[0]
        self.bgname = wx.TextCtrl(panel, value="", size=(sizeTextn,-1))
        self.bgname.Enable(False)
        self.btnimport = wx.Button(panel, wx.ID_ANY, 'Import into session')
        self.btnimport.Enable(False)
        # Dropdown
        self.BGlist = ["File/User"] # updated by self.UpdateDropdown()
        textdropdown = wx.StaticText(panel, label="Show background: ")
        self.dropdown = wx.ComboBox(panel, -1, "File/User", (15, -1),
                     wx.DefaultSize, self.BGlist, wx.CB_DROPDOWN|wx.CB_READONLY)
        self.UpdateDropdown()
        # Radio buttons Channel1 and 2
        self.rbtnCh1 = wx.RadioButton (panel, -1, 'Ch1 ', 
                                        style = wx.RB_GROUP)
        self.rbtnCh1.SetValue(True)
        self.rbtnCh2 = wx.RadioButton (panel, -1, 'Ch2')
        # Apply buttons
        self.btnapply = wx.Button(panel, wx.ID_ANY, 'Apply')
        textor = wx.StaticText(panel, label=" or ")
        self.btnrem = wx.Button(panel, wx.ID_ANY, 'Dismiss')
        textpages   = wx.StaticText(panel, label=" correction for pages: ")
        self.WXTextPages = wx.TextCtrl(panel, value="")
        # Initial value for WXTextPages
        pagenumlist = list()
        for i in np.arange(self.parent.notebook.GetPageCount()):
            Page = self.parent.notebook.GetPage(i)
            pagenumlist.append(int(filter(lambda x: x.isdigit(), Page.counter)))
        valstring=misc.parsePagenum2String(pagenumlist)
        self.WXTextPages.SetValue(valstring)
        textyma   = wx.StaticText(panel, label="Shortcut - ")
        self.btnapplyall = wx.Button(panel, wx.ID_ANY, 'Apply to all pages')
        textor2 = wx.StaticText(panel, label=" or ")
        self.btnremyall = wx.Button(panel, wx.ID_ANY, 'Dismiss from all pages')
        # Bindings
        self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.btnbrowse)
        self.Bind(wx.EVT_RADIOBUTTON, self.OnRadioFile, self.rbtnfile)
        self.Bind(wx.EVT_RADIOBUTTON, self.OnRadioHand, self.rbtnhand)
        self.Bind(wx.EVT_SPINCTRL, self.SpinCtrlChange, self.spinctrl)
        self.Bind(wx.EVT_BUTTON, self.OnImport, self.btnimport)
        self.Bind(wx.EVT_COMBOBOX, self.OnDraw, self.dropdown)
        self.Bind(wx.EVT_BUTTON, self.OnApply, self.btnapply)
        self.Bind(wx.EVT_BUTTON, self.OnApplyAll, self.btnapplyall)
        self.Bind(wx.EVT_BUTTON, self.OnRemove, self.btnrem)
        self.Bind(wx.EVT_BUTTON, self.OnRemoveAll, self.btnremyall)
        # Sizers
        topSizer = wx.BoxSizer(wx.VERTICAL)
        text1sizer = wx.BoxSizer(wx.HORIZONTAL)
        text1sizer.Add(self.rbtnfile)
        text1sizer.Add(self.btnbrowse)
        text2sizer = wx.BoxSizer(wx.HORIZONTAL)
        text2sizer.Add(self.rbtnhand)
        text2sizer.Add(self.spinctrl)
        textmeansizer = wx.BoxSizer(wx.HORIZONTAL)
        textmeansizer.Add(textmeanavg)
        textmeansizer.Add(self.textmean)
        dropsizer = wx.BoxSizer(wx.HORIZONTAL)
        dropsizer.Add(textdropdown)
        droprightsizer = wx.BoxSizer(wx.VERTICAL)
        dropsizer.Add(droprightsizer)
        droprightsizer.Add(self.dropdown)
        #droprightsizer.Add(self.textafterdropdown)
        applysizer = wx.BoxSizer(wx.HORIZONTAL)
        applysizer.Add(self.btnapply)
        applysizer.Add(textor)
        applysizer.Add(self.btnrem)
        applysizer.Add(textpages)
        applysizer.Add(self.WXTextPages)
        applysizer.Add(self.rbtnCh1)
        applysizer.Add(self.rbtnCh2)
        allsizer = wx.BoxSizer(wx.HORIZONTAL)
        allsizer.Add(textyma)
        allsizer.Add(self.btnapplyall)
        allsizer.Add(textor2)
        allsizer.Add(self.btnremyall)
        
        topSizer.Add(textinit)
        topSizer.Add(text1sizer)
        topSizer.Add(text2sizer)
        topSizer.Add(self.textfile)
        topSizer.Add(textmeansizer)
        topSizer.Add(textname)
        topSizer.Add(self.bgname)
        topSizer.Add(self.btnimport)
        topSizer.Add(dropsizer)
        topSizer.Add(applysizer)
        topSizer.Add(allsizer)
        panel.SetSizer(topSizer)
        topSizer.Fit(self)
        self.SetMinSize(topSizer.GetMinSizeTuple())
        self.Show(True)
        ## Canvas
        self.canvas = plot.PlotCanvas(self.sp)
        # Sizes
        psize = panel.GetBestSize()
        initial_size = (psize[0],psize[1]+200)
        self.SetSize(initial_size)
        sashsize = psize[1]+3
        # This is also necessary to prevent unsplitting
        self.sp.SetMinimumPaneSize(sashsize)
        self.sp.SplitHorizontally(panel, self.canvas, sashsize)
        # If there is no page, disable ourselves:
        self.OnPageChanged(self.parent.notebook.GetCurrentPage())
        #Icon
        if parent.MainIcon is not None:
            wx.Frame.SetIcon(self, parent.MainIcon)


    def OnApply(self, event):
        strFull = self.WXTextPages.GetValue()
        PageNumbers = misc.parseString2Pagenum(self, strFull)
        if PageNumbers is None:
            # Something went wrong and parseString2Pagenum already displayed
            # an error message.
            return
        # BG number
        item = self.dropdown.GetSelection()
        # Apply to corresponding pages
        for i in np.arange(self.parent.notebook.GetPageCount()):
            Page = self.parent.notebook.GetPage(i)
            j = filter(lambda x: x.isdigit(), Page.counter)
            if int(j) in PageNumbers:
                if self.rbtnCh1.GetValue() == True:
                    Page.bgselected = item
                else:
                    Page.bg2selected = item
                if Page.IsCrossCorrelation is False:
                    # Autocorrelation only has one background!
                    Page.bg2selected = None
                Page.OnAmplitudeCheck("init")
                Page.PlotAll()
        # Clean up unused backgrounds
        CleanupAutomaticBackground(self.parent)


    def OnApplyAll(self, event):
        self.btnrem.Enable(True)
        self.btnremyall.Enable(True)
        N = self.parent.notebook.GetPageCount()
        item = self.dropdown.GetSelection()
        for i in np.arange(N):
            # Set Page 
            Page = self.parent.notebook.GetPage(i)
            Page.bgselected = item
            if Page.IsCrossCorrelation:
                Page.bg2selected = item
            else:
                Page.bg2selected = None
            try:
                Page.OnAmplitudeCheck("init")
                Page.PlotAll()
            except OverflowError:
                errstr = "Could not apply background to Page "+Page.counter+\
                 ". \n Check the value of the trace average and the background."
                dlg = wx.MessageDialog(self, errstr, "Error", 
                    style=wx.ICON_ERROR|wx.OK|wx.STAY_ON_TOP)
                dlg.ShowModal()
                Page.bgselected = None
                Page.bg2selected = None
        # Clean up unused backgrounds
        CleanupAutomaticBackground(self.parent)


    def OnClose(self, event=None):
        self.parent.toolmenu.Check(self.MyID, False)
        self.parent.ToolsOpen.__delitem__(self.MyID)
        self.Destroy()
            

    def OnBrowse(self, event):
        # opf.BGFiletypes is a dictionary with filetypes that have some
        # trace signal information.
        SupFiletypes = opf.BGFiletypes.keys()
        SupFiletypes.sort()
        filters = ""
        for i in np.arange(len(SupFiletypes)):
            # Add to the filetype filter
            filters = filters+SupFiletypes[i]
            if i+1 != len(SupFiletypes):
                # Add a separator
                filters = filters+"|"
        dlg = wx.FileDialog(self, "Choose a data file", 
            self.parent.dirname, "", filters, wx.FD_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            # Workaround since 0.7.5
            (dirname, filename) = os.path.split(dlg.GetPath())
            #filename = dlg.GetFilename()
            #dirname = dlg.GetDirectory()
            # Set parent dirname for user comfort
            self.parent.dirname = dirname
            try:
                # [data, trace, curvelist]
                stuff = readfiles.openAnyBG(dirname, filename)
            except:
                # The file does not seem to be what it seems to be.
                info = sys.exc_info()
                errstr = "Unknown file format:\n"
                errstr += str(filename)+"\n\n"
                errstr += str(info[0])+"\n"
                errstr += str(info[1])+"\n"
                for tb_item in traceback.format_tb(info[2]):
                    errstr += tb_item
                wx.MessageDialog(self, errstr, "Error", 
                    style=wx.ICON_ERROR|wx.OK|wx.STAY_ON_TOP)
                return
            # Usually we will get a bunch of traces. Let the user select which
            # one to take.
            if len(stuff["Filename"]) > 1:
                choices = list()
                for i2 in np.arange(len(stuff["Filename"])):
                    choices.append(str(i2)+". " + stuff["Filename"][i2] + " " +
                                   stuff["Type"][i2])
                dlg = wx.SingleChoiceDialog(self, "Choose a curve",
                                            "Curve selection", choices=choices)
                if dlg.ShowModal() == wx.ID_OK:
                    selindex = dlg.GetSelection()
                else:
                    return
            else:
                selindex = 0
            # If we accidentally recorded a cross correlation curve
            # as the background, let the user choose which trace he wants:
            channelindex = None
            if ( len(stuff["Type"][selindex]) >= 2 and 
                 stuff["Type"][selindex][0:2] == "CC"       ):
                choices = ["Channel 1", "Channel 2"]
                label = "From which channel do you want to use the trace?"
                dlg = wx.SingleChoiceDialog(self, label,
                                "Curve selection", choices=choices)
                if dlg.ShowModal() == wx.ID_OK:
                    channelindex = dlg.GetSelection()
                    trace = stuff["Trace"][selindex][channelindex]
                else:
                    return
            else:
                trace = stuff["Trace"][selindex]
            if trace is None:
                print "WARNING: I did not find any trace data."
                return
            # Display filename and some of the directory
            self.textfile.SetLabel("File: ..."+dirname[-10:]+"/"+filename)
            name = str(selindex)+". "+stuff["Filename"][selindex]+" "+\
                   stuff["Type"][selindex]
            if channelindex is not None:
                name += " "+str(channelindex+1)
            self.bgname.SetValue(name)
            
            self.trace = trace
            # Calculate average
            self.average = self.trace[:,1].mean()
            # Display average
            self.textmean.SetLabel(str(self.average)+" kHz")
            self.spinctrl.SetValue(self.average)
            # Let the user see the opened file
            self.dropdown.SetSelection(0)
            # show trace
            self.OnDraw()
            # Enable button and editable name
            self.bgname.Enable(True)
            self.btnimport.Enable(True)
        else:
            # User pressed "Abort" - do nothing.
            self.parent.dirname = dlg.GetDirectory()
            dlg.Destroy()
            return


    def OnDraw(self, event=None):
        item = self.dropdown.GetSelection()
        if item < 0:
            # Disable Apply Buttons
            self.btnapply.Enable(False)
            self.btnapplyall.Enable(False)
            # Draw the trace that was just imported
            if self.trace != None:
                # Calculate average
                self.average = self.trace[:,1].mean()
                self.activetrace = self.trace
                #self.textafterdropdown.SetLabel(" Avg:  "+str(self.average)+
                #                                " kHz")
                self.textmean.SetLabel(str(self.average))
                self.spinctrl.SetValue(self.average)
            else:
                # Clear the canvas. Looks better.
                self.canvas.Clear()
                # Don't show the average
                #self.textafterdropdown.SetLabel("")
                self.textmean.SetLabel("")
                return
        else:
            # Enable Apply Buttons
            self.btnapply.Enable(True)
            self.btnapplyall.Enable(True)
            # Draw a trace from the list
            self.activetrace = self.parent.Background[item-1][2]
            #self.textafterdropdown.SetLabel(" Avg:  "+
            #                    str(self.parent.Background[item-1][0]))
        # We want to have the trace in [s] here.
        trace = 1.*self.activetrace
        trace[:,0] = trace[:,0]/1000
        linesig = plot.PolyLine(trace, legend='', colour='blue', width=1)
        self.canvas.Draw(plot.PlotGraphics([linesig], 
                         xLabel='time [s]', 
                         yLabel='background signal [kHz]'))


    def OnImport(self, event):
        self.parent.Background.append([self.average, self.bgname.GetValue(), 
                                      self.trace])
        # Next two lines are taken care of by UpdateDropdown
        #name = "{} ({:.2f} kHz)".format(self.bgname.GetValue(), self.average)
        #self.BGlist.append(name)
        self.UpdateDropdown()
        self.btnremyall.Enable(True)
        self.btnrem.Enable(True)
        self.btnapplyall.Enable(True)
        self.btnapply.Enable(True)
        self.OnDraw()
        # Update BG dropdown of each page
        for i in np.arange(self.parent.notebook.GetPageCount()):
            self.parent.notebook.GetPage(i).OnAmplitudeCheck()


    def OnPageChanged(self, page=None, trigger=None):
        """
            This function is called, when something in the panel
            changes. The variable `trigger` is used to prevent this
            function from being executed to save stall time of the user.
            Forr a list of possible triggers, see the doc string of
            `tools`.
        """
        # We do not need the *Range* Commands here yet.
        # We open and close the SelectChannelsFrame every time we
        # import some data.
        if trigger in ["parm_batch", "fit_batch", "page_add_batch"]:
            return
        if len(self.parent.Background) == 0:
            self.BGlist = list()
            self.UpdateDropdown()
            self.dropdown.SetValue("File/User")
        if self.parent.notebook.GetPageCount() == 0:
            self.sp.Disable()
            return
        self.sp.Enable()
        if len(self.BGlist) <= 0:
            self.btnrem.Enable(False)
            self.btnremyall.Enable(False)
            self.btnapply.Enable(False)
            self.btnapplyall.Enable(False)
        else:
            self.btnrem.Enable(True)
            self.btnremyall.Enable(True)
            self.btnapply.Enable(True)
            self.btnapplyall.Enable(True)
        if (self.WXTextPages.GetValue() == ""
            and self.parent.notebook.GetPageCount() != 0):
            # Initial value for WXTextPages
            pagenumlist = list()
            for i in np.arange(self.parent.notebook.GetPageCount()):
                Page = self.parent.notebook.GetPage(i)
                pagenumlist.append(int(filter(lambda x: x.isdigit(), Page.counter)))
            valstring=misc.parsePagenum2String(pagenumlist)
            self.WXTextPages.SetValue(valstring)
        


    def OnRadioFile(self, event):
        # Do not let the user change the spinctrl
        # setting.
        self.spinctrl.Enable(False)
        self.btnbrowse.Enable(True)
        # Restor the old trace
        self.trace = self.oldtrace
        if self.oldfilename is not None:
            self.textfile.SetLabel(self.oldfilename)
        if self.trace is None:
            # Disable button and editable name
            self.bgname.Enable(False)
            self.btnimport.Enable(False)
        # Let us draw
        self.dropdown.SetSelection(0)
        self.OnDraw()


    def OnRadioHand(self, event):
        # Let user enter a signal.
        self.spinctrl.Enable(True)
        self.btnbrowse.Enable(False)
        # save the old trace. We might want to switch back to it.
        if self.trace is not None:
            self.oldtrace = 1.*self.trace
            self.oldfilename = self.textfile.GetLabel()
        self.SpinCtrlChange()
        # Do not show the filename
        self.textfile.SetLabel("No file selected.")
        # Enable button and editable name
        self.bgname.Enable(True)
        self.btnimport.Enable(True)
        if len(self.bgname.GetValue()) == 0:
            # Enter something as name
            self.bgname.SetValue("User")


    def OnRemove(self, event):
        strFull = self.WXTextPages.GetValue()
        PageNumbers = misc.parseString2Pagenum(self, strFull)
        if PageNumbers is None:
            # Something went wrong and parseString2Pagenum already displayed
            # an error message.
            return
        # BG number
        item = self.dropdown.GetSelection()
        # Apply to corresponding pages
        for i in np.arange(self.parent.notebook.GetPageCount()):
            Page = self.parent.notebook.GetPage(i)
            j = filter(lambda x: x.isdigit(), Page.counter)
            if int(j) in PageNumbers:
                if self.rbtnCh1.GetValue() == True:
                    Page.bgselected = None
                else:
                    Page.bg2selected = None
                Page.bgselected = None
                Page.OnAmplitudeCheck("init")
                Page.PlotAll()
        # Clean up unused backgrounds
        CleanupAutomaticBackground(self.parent)
        

    def OnRemoveAll(self, event):
        N = self.parent.notebook.GetPageCount()
        for i in np.arange(N):
            Page = self.parent.notebook.GetPage(i)
            Page.bgselected = None
            Page.bg2selected = None
            Page.OnAmplitudeCheck("init")
            Page.PlotAll()
        # Clean up unused backgrounds
        CleanupAutomaticBackground(self.parent)

    def SetPageNumbers(self, pagestring):
        self.WXTextPages.SetValue(pagestring)
    
    
    def SpinCtrlChange(self, event=None):
        # Let user see the continuous trace we will generate
        self.average = self.spinctrl.GetValue()
        self.trace = np.array([[0,self.average],[1,self.average]])
        self.textmean.SetLabel(str(self.average))
        self.OnDraw()


    def UpdateDropdown(self, e=None):
        self.BGlist = list()
        #self.BGlist.append("File/User")
        for item in self.parent.Background:
            bgname = "{} ({:.2f} kHz)".format(item[1],item[0])
            self.BGlist.append(bgname)
        if len(self.BGlist) == 0:
            ddlist = ["File/User"]
        else:
            ddlist = 1*self.BGlist
        self.dropdown.SetItems(self.BGlist)
        # Show the last item
        self.dropdown.SetSelection(len(self.BGlist)-1)


def ApplyAutomaticBackground(page, bg, parent):
    """
        Creates an "automatic" background with countrate in kHz *bg* and
        applies it to the given *page* object. If an automatic
        background with the same countrate exists, uses it.
        
        Input:
        *page*   - page to which the background should be applied
        *bg*     - background that should be applied to that page
                   float or list of 1 or two elements
                   -> if the page is cross-correlation, the second
                      background will be applied as well.
        *parent* - parent containing *Background* list
    """
    bglist = 1*np.atleast_1d(bg)
    # minus 1 to identify non-set background id
    bgid = np.zeros(bglist.shape, dtype=int) - 1
    for b in xrange(len(bglist)):
        # Check if exists:
        for i in xrange(len(parent.Background)):
            if parent.Background[i][0] == bglist[b]:
                bgid[b] = i
        if bgid[b] == -1:
            # Add new background
            bgname = "AUTO: {:e} kHz \t".format(bglist[b])
            trace = np.array([[0,bglist[b]],[1,bglist[b]]])
            parent.Background.append([bglist[b], bgname, trace])
            bgid[b] = len(parent.Background) - 1
    # Apply background to page
    # Last item is id of background
    page.bgselected = bgid[0]
    if page.IsCrossCorrelation:
        if len(bgid) != 2:
            raise NotImplementedError("Cross-correlation data needs"+
                "exactly two signals for background-correction!")
        # Apply second background
        page.bg2selected = bgid[1]
    else:
        page.bg2selected = None
    CleanupAutomaticBackground(parent)
    page.OnAmplitudeCheck("init")
    page.PlotAll()


def CleanupAutomaticBackground(parent):
    """
        Goes through the pagelist *parent.notebook.GetPageCount()*
        and checks *parent.Background* for unnused automatic
        backgrounds.
        Removes these and updates the references to all backgrounds
        within the pages.
    """
    # Create a dictionary with keys: indices of old background list -
    # and elements: list of pages having this background
    BGdict = dict()
    BG2dict = dict() # cross-correlation
    for i in xrange(len(parent.Background)):
        BGdict[i] = list()
        BG2dict[i] = list()
    # Append pages to the lists inside the dictionary
    for i in xrange(parent.notebook.GetPageCount()):
        Page = parent.notebook.GetPage(i)
        if Page.bgselected is not None:
            BGdict[Page.bgselected].append(Page)
        if Page.bg2selected is not None:
            BG2dict[Page.bg2selected].append(Page)
    # Sort the keys and create a new background list
    NewBGlist = list()
    keyID = 0
    keys = BGdict.keys()
    keys.sort()
    for key in keys:
        # Do not delete user-generated backgrounds
        if len(BGdict[key]) == 0 and parent.Background[key][1][-1]=="\t":
            # This discrads auto-generated backgrounds that have no
            # pages assigned to them
            pass
        else:
            for page in BGdict[key]:
                page.bgselected = keyID
            NewBGlist.append(parent.Background[key])
            keyID += 1
    # Same thing for cross-correlation (two bg signals)
    #keyID = 0
    keys = BG2dict.keys()
    keys.sort()
    for key in keys:
        # Do not delete user-generated backgrounds
        if len(BG2dict[key]) == 0 and parent.Background[key][1][-1]=="\t":
            # This discrads auto-generated backgrounds that have no
            # pages assigned to them
            pass
        elif parent.Background[key][1][-1]=="\t":
            # We already added the user-defined backgrounds
            # Therefore, we only check for aut-generated backgrounds
            # ("\t")
            for page in BG2dict[key]:
                page.bg2selected = keyID
            NewBGlist.append(parent.Background[key])
            keyID += 1
    # Finally, write back background list
    parent.Background = NewBGlist
    # If the background correction tool is open, update the list
    # of backgrounds.
    # (self.MyName="BACKGROUND")
    toolkeys = parent.ToolsOpen.keys()
    if len(toolkeys) == 0:
        pass
    else:
        for key in toolkeys:
            tool = parent.ToolsOpen[key]
            try:
                if tool.MyName == "BACKGROUND":
                    tool.UpdateDropdown()
                    tool.OnPageChanged()
            except:
                pass