Calculate average coordinate of several peaks (sparky script)

From NMR Wiki

Jump to: navigation, search



Select several peaks and calculate average coordinate in Hertz


  • with plain text editor create file in python/sparky directory which is located in the root of sparky installation
  • paste code below into that file, save and close file
  • open file
  • find peak_menu array in the file and add line highligthed in red into that array as shown below
peak_menu = (
    ('hc', 'HC peaks',                  ('hcpeaks','show_dialog')),
    ('af', 'Calculate average frequenies',        ('calc_ave_freq','show_dialog')),
  • when you open some file in sparky, select some peaks and type 'af' average frequencies should be shown in a pop-up window


Info window won't go away when "ok" button is clicked.


Please feel free to improve the code.

import Tkinter
import noesy
import pyutil
import sputil
import tkutil
def show_dialog(session):
    tk =
    window = Tkinter.Toplevel(tk) 
    view = session.selected_view()
    spectrum = view.spectrum
    peaks = spectrum.selected_peaks()
    num_peaks = len(peaks)
    if num_peaks <= 1 :
        message = 'Select two or more peaks!'
        dim = len(peaks[0].position)
        ave = [0,0,0]
        for i in range(dim):
            ave[i] = 0
        for p in peaks:
            for i in range(dim):
                ave[i] = ave[i] + p.position[i]
        npeaks = len(peaks)
        for i in range(dim):
            ave[i] = ave[i]/npeaks
        message = ''
        lbl = ('F1:','F2:','F3:')
        for i in range(dim):
        #x y z must correspond to actual xyz (axis order!)
        #print hertz instead of ppms
            ppm = spectrum.hz_per_ppm[i]
            ave[i] = ave[i]*ppm
            output = '%.2f' % ave[i]
            message = message + lbl[i] + str(output) + '\n'
    lbl = Tkinter.Label(window,text=message)
    btn = Tkinter.Button(window,text='ok')
Personal tools