Exemple #1
0
 def insert_or_append(self, insert):
    new_peak = Mca.McaPeak()
    if (self.callback_command != None):
       cursor = self.callback_command(get_cursor=1)
       energy = self.mca.channel_to_energy(cursor)
    else:
       energy=0.
    new_peak.initial_energy = energy
    # Make a reasonble estimate of initial FWHM
    new_peak.initial_fwhm = .15
    new_peak.energy_flag=1
    new_peak.fwhm_flag=1
    if (insert):
       self.fit.peaks.insert(self.index, new_peak)
    else:
       self.fit.peaks.insert(self.index+1, new_peak)
       self.index = self.index + 1
    self.update_peaks()
Exemple #2
0
 def clear_peaks(self):
    self.fit.peaks=[Mca.McaPeak()]
    self.index=0
    self.update_peaks()
Exemple #3
0
   def __init__(self, mca, fit=None, command=None):
      class widgets:
         pass
      self.results_file = 'fit_results.txt'
      self.spreadsheet_file = 'fit_spreadsheet.txt'
      self.mca = mca
      self.callback_command = command
      self.widgets = widgets()
      self.background_mca = copy.deepcopy(mca)
      self.fit_mca = copy.deepcopy(mca)
      self.mark_peaks_mca = copy.deepcopy(mca)
      data = self.background_mca.get_data() * 0
      self.background_mca.set_data(data)
      self.fit_mca.set_data(data)
      self.mark_peaks_mca.set_data(data)
      self.index=0
      if (fit == None):
         self.fit = Mca.McaFit(mca)
      else:
         self.fit = fit
      # Update the fit object to be consistent with the Mca
      self.fit.update(self.fit_mca)
      
      if (len(self.fit.peaks) == 0): self.fit.peaks=[Mca.McaPeak()]
      xsize=10
      top = myTkTop.myTkTop()
      Pmw.initialise(top)
      top.title('Peak Fit')
      self.widgets.top = top
      frame = Frame(top, borderwidth=1, relief='raised')
      frame.pack(fill=X)
      mb = Pmw.MenuBar(frame)
      mb.pack(fill=X)
      mb.addmenu('File', '', side='left')
      self.widgets.file = mb.component('File-menu')
      mb.addmenuitem('File', 'command', 
                      label='Read peaks...', 
                      command=self.read_peak_file)
      mb.addmenuitem('File', 'command', 
                      label='Write peaks...',
                      command=self.write_peak_file)
      mb.addmenuitem('File', 'command', 
                      label='Results file...', command=self.set_results_file)
      mb.addmenuitem('File', 'command', 
                      label='Spreadsheet file...', 
                      command=self.set_spreadsheet_file)
      mb.addmenuitem('File', 'command', 'Exit', label='Exit',
                      command=self.menu_exit)
      t = Pmw.Group(top, tag_text='Background Parameters'); t.pack(fill=X)
      bcol = t.interior()
      row = Frame(bcol); row.pack(side=TOP)
      background = self.fit.background
      self.exponent_options = ('2','4','6')
      self.widgets.exponent = t = Pmw.OptionMenu(row, labelpos=N,
                                    label_text='Exponent',
                                    items=self.exponent_options,
                                    initialitem = str(background.exponent))
      t.pack(side=LEFT)
      self.widgets.top_width = t = Pmw.EntryField(row,
                                    value=background.top_width,
                                    entry_width=8, entry_justify=CENTER,
                                    labelpos=N, label_text='Top width',
                                    validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.bottom_width = t = Pmw.EntryField(row,
                                       value=background.bottom_width,
                                       entry_width=8, entry_justify=CENTER,
                                       labelpos=N, label_text='Bottom width',
                                       validate={'validator':'real'})
      t.pack(side=LEFT)
      self.tangent_options = ('No', 'Yes')
      self.widgets.tangent = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='Tangent?',
                                        items=self.tangent_options,
                                        menubutton_width=3,
                                        initialitem = background.tangent)
      t.pack(side=LEFT)
      self.compress_options = ('1', '2', '4', '8', '16')
      self.widgets.compress = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='Compression',
                                        items=self.compress_options,
                                        menubutton_width=2,
                                        initialitem = str(background.compress))
      t.pack(side=LEFT)
      row = Frame(bcol); row.pack(side=TOP)
      self.widgets.fit_background = t = Button(row, text='Fit background',
                                               command=self.fit_background)
      t.pack(side=LEFT)
      self.widgets.plot_background = t = Button(row, text='Re-plot background',
                                                command=self.plot_background)
      t.pack(side=LEFT)

      t = Pmw.Group(top, tag_text='Peak Fit Parameters'); t.pack(fill=X)
      fcol = t.interior()
      t = Pmw.Group(fcol, tag_text='Initial energy calibration'); 
      t.pack(side=TOP)
      row = t.interior()
      self.widgets.energy_cal_offset = t = Pmw.EntryField(row,
                            value=self.fit.initial_energy_offset,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Offset:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.energy_cal_slope = t = Pmw.EntryField(row,
                            value=self.fit.initial_energy_slope,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Slope:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.optimize_options = ('Fix', 'Optimize')
      self.widgets.energy_cal_flag = t = Pmw.OptionMenu(row, labelpos=W,
                                        label_text='Flag:',
                                        items=self.optimize_options,
                                        menubutton_width=8,
                                        initialitem = self.fit.energy_flag)
      t.pack(side=LEFT)
      t = Pmw.Group(fcol, tag_text='Initial FWHM calibration');
      t.pack(side=TOP)
      row = t.interior()
      self.widgets.fwhm_cal_offset = t = Pmw.EntryField(row, 
                            value=self.fit.initial_fwhm_offset,
                            command = self.update_peaks,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Offset:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.fwhm_cal_slope = t = Pmw.EntryField(row,
                            value=self.fit.initial_fwhm_slope,
                            command = self.update_peaks,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Slope:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.fwhm_cal_flag = t = Pmw.OptionMenu(row, labelpos=W,
                                        label_text='Flag:',
                                        items=self.optimize_options,
                                        menubutton_width=8,
                                        initialitem = self.fit.fwhm_flag)
      t.pack(side=LEFT)

      width=10
      t = Pmw.Group(fcol, tag_text='Peak parameters'); t.pack(fill=X)
      pcol = t.interior()
      row = Frame(pcol); row.pack()
      self.widgets.label = t = Pmw.EntryField(row, value=' ',
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='Label',
                            command=self.peak_label)
      t.pack(side=LEFT)
      self.widgets.energy = t = Pmw.EntryField(row, value=0.,
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='Energy',
                            validate={'validator':'real'},
                            command=self.peak_params)
      t.pack(side=LEFT)

      self.widgets.energy_flag = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='Energy flag',
                                        items=self.optimize_options,
                                        initialitem = 1,
                                        menubutton_width=8,
                                        command=self.peak_params)
      t.pack(side=LEFT)
      self.widgets.fwhm = t = Pmw.EntryField(row, value=0.,
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='FWHM',
                            validate={'validator':'real'},
                            command=self.peak_params)
      t.pack(side=LEFT)
      self.global_optimize_options = ('Global', 'Optimize', 'Fix')
      self.widgets.fwhm_flag = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='FWHM flag',
                                        items=self.global_optimize_options,
                                        initialitem = 0,
                                        menubutton_width=8,
                                        command=self.peak_params)
      t.pack(side=LEFT)
      self.widgets.ampl_factor = t = Pmw.EntryField(row, value=0.,
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='Ampl. ratio',
                            validate={'validator':'real'},
                            command=self.peak_params)
      t.pack(side=LEFT)
      row = Frame(pcol); row.pack(side=TOP, anchor=W)
      col = Frame(row); col.pack(side=LEFT)
      width=7
      self.widgets.insert = t = Button(col, text='Insert', width=width,
                           command=lambda s=self: s.insert_or_append(insert=1)); 
      t.pack()
      self.widgets.append = t = Button(col, text='Append', width=width,
                           command=lambda s=self: s.insert_or_append(insert=0)); 
      t.pack()
      self.widgets.delete = t = Button(col, text='Delete', width=width,
                                       command=self.delete_peak); 
      t.pack()
      self.widgets.clear_peaks = t = Button(col, text='Clear', width=width,
                                            command=self.clear_peaks) 
      t.pack()
      self.widgets.sort = t = Button(col, text='Sort', width=width,
                                     command=self.sort_peaks); 
      t.pack()
      self.widgets.mark_peaks = t = Button(col, text='Mark all', width=width,
                                           command=self.mark_peaks) 
      t.pack()

      self.widgets.peak_list = t = Pmw.ScrolledListBox(row,
                                            listbox_font=('Courier','8'), 
                                            listbox_width=65, listbox_height=10,
                                            vscrollmode='static',
                                            hscrollmode='none',
                                            selectioncommand=self.new_index)
      t.pack()

      width=40
      row = Frame(fcol); row.pack(anchor=W)
      t = Label(row, text='Fit results file:'); t.pack(side=LEFT)
      self.widgets.results_file_name = t = Label(row, 
                            text=self.results_file,
                            foreground='blue', background='white')
      t.pack(side=LEFT)
      row = Frame(fcol); row.pack(anchor=W)
      t = Label(row, text='Spreadsheet file:'); t.pack(side=LEFT)
      self.widgets.spreadsheet_file_name = t = Label(row, 
                            text=self.spreadsheet_file,
                            foreground='blue', background='white')
      t.pack(side=LEFT)
      self.widgets.append_mode = t = Pmw.OptionMenu(fcol, labelpos=W,
            label_text='Overwrite or append results and spreadsheet files:',
                                        items=('Overwrite', 'Append'),
                                        menubutton_width=9,
                                        initialitem = 1)
      t.pack(anchor=W)
      row = Frame(fcol); row.pack(side=TOP)
      self.widgets.fit_peaks = t = Button(row, text='Fit peaks',
                                          command=self.fit_peaks) 
      t.pack(side=LEFT)
      self.widgets.plot_fit = t = Button(row, text='Re-plot fit',
                                         command=self.plot_fit)
      t.pack(side=LEFT)
      self.update_peaks()