fitting

141 views
Skip to first unread message

Johann Eicher

unread,
Jul 7, 2011, 7:51:22 AM7/7/11
to nmrglue-discuss
Hi there,

Your module is amazing so far but I'm struggling to fit a simple
lorentzian to the peaks in a 1D spectrum. I'm not sure what the
problem is but I keep on getting this error:

section of script...

#FITTING INDIVIDUAL PEAKS
bounds = []
for i in peak_lnsh[0]:
bounds.append([(None,None)])

ampbounds = []
for i in peak_lnsh[1]:
ampbounds.append(None)

spectrum = data_ft[fid]
params = peak_lnsh[0]
amps = peak_lnsh[1]
centers = peaks[0]
r_id = np.arange(peaks[0].shape[0]).tolist()
box_width = (10)

ng.analysis.linesh.fit_spectrum(spectrum,'l',params,amps,bounds,ampbounds,centers,r_id,box_width,error_flag=True,verb=True)


error...


TypeError Traceback (most recent call
last)

/home/jeicher/Desktop/nmr/import_simple.py in <module>()
----> 1
2
3
4
5

/usr/local/lib/python2.7/dist-packages/nmrglue/analysis/linesh.pyc in
fit_spectrum(spectrum, lineshapes, params, amps, bounds, ampbounds,
centers, rIDs, box_width, error_flag, verb, **kw)
236 ecbounds = [ [ zip(*[ls.add_edge(b,(mn,mx)) for b in
zip(*db)])
237 for ls,mn,mx,db in
zip(ls_classes,rmin,rmax,pb) ]
--> 238 for pb in cbounds ]
239
240 # fit the region


TypeError: zip argument #1 must support iteration


Any idea what to do about it?
J

Jon Helmus

unread,
Jul 20, 2011, 5:32:32 PM7/20/11
to nmrglue...@googlegroups.com
Johanna,

    Sorry for the delay, I just finished up my time at Ohio State and over the last two weeks I made a move to the UConn Health Center and haven't had the time nor the internet access to properly answer you question. 

The analysis modules are still under development so I don't have any examples to share which would probably be most helpful but the documentation on the fit_spectrum function is available (http://nmrglue.googlecode.com/svn/trunk/doc/_build/html/reference/linesh.html#nmrglue.analysis.linesh.fit_spectrum)

Although I can't tell for sure from the code you provided I think you need to check that all the passed parameters have the correct format.  Specifically for a 1D spectrum:

* spectrum should be a 1D numpy array
* lineshape (the 2nd parameter) should be a list of strings, so ['l'].
* params should be a list of length equal to the number of peaks in the spectrum, with each element of the lists itself a length 1 list (since 1D) of 2-tuples containing estimated lineshape parameters, something like [ [(20.2,0.6)] ] would be appropriate for a single peak with center at ~20.2 and linewidth ~0.6.
* amps should also be a list of
length equal to the number of peaks in the spectrum of estimated amplitudes, so [1000.0]for a single peak in a 1D.
* bounds should be a list similar to params with None in place of lineshape parameters. [ [(None,None)] ] would work.
* ampbounds should be a list like amps, so [None] for a single peak.
* centers is a list of tuples indicated region centers for examples [(20,)]
* rID is a list, yours looks alright as long as peaks[0].shape[0] is the number of peaks in the spectrum.
* box_width NEEDS to be a tuple so (10,).


If you could provide some details on what the peak_lnsh and peaks variables are I should be able to give some more specifics. 

Cheers,

    -Jonathan Helmus
Message has been deleted

Kresten Bertelsen

unread,
Dec 5, 2014, 2:39:32 PM12/5/14
to nmrglue...@googlegroups.com
Hi,

I'm reworking this tread since I have come about the same problem. This is my first experience with python so forgive me if I ask trivial questions.

Below is some code which reads in a processed spectrum, peak picks it and the tries to deconvolute. The deconvolution fails and
fiddling around with the debugger it seems it is the command zip(*db) which fails since db is (0,100) and cannot be unzipped.

This must relate to format of the passed parameters but I cannot find the error. Please Help


import nmrglue as ng
import numpy as np

dic, data = ng.bruker.read_pdata('./Data Files/P31_1D/10/pdata/1')
SF=dic['procs']['SF']
SW_p=dic['procs']['SW_p']
SI=dic['procs']['SI']
OFFSET=dic['procs']['OFFSET']
AX=np.linspace(OFFSET,OFFSET-SW_p/SF,SI)

peaktab = ng.analysis.peakpick.pick(data,np.max(data)*0.02)
# Prepare the inputs for deconvolution
lsh= list('g' for x in range(np.size(peaktab['X_AXIS'])))
params=[]
for idx,peak  in enumerate(peaktab['X_AXIS']):
    params.append([tuple((peak,10))])
amps=list(data[[int(x) for x in peaktab['X_AXIS']]])
bounds=[[(0,100)] for x in range(np.size(peaktab['X_AXIS']))]
ampbounds =[None for x in range(np.size(peaktab['X_AXIS']))]
centers = list([(peaktab['X_AXIS'][x],) for x in range(np.size(peaktab['X_AXIS']))])
rIDs = list(peaktab['cID'])
box_width = (200,)
print(lsh)
print(params)
print(amps)
print(bounds)
print(ampbounds)
print(centers)
print(rIDs)
print(box_width)
a1,a2,a3,a4,a5 = ng.analysis.linesh.fit_spectrum(data,lsh,params,amps,bounds,ampbounds,centers,rIDs,box_width,0)

outputs__________________

['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g']
[[(58584.0, 10)], [(61144.0, 10)], [(63213.0, 10)], [(65301.0, 10)], [(65564.0, 10)], [(66386.0, 10)], [(67026.0, 10)], [(67191.0, 10)], [(67400.0, 10)], [(67976.0, 10)], [(69279.0, 10)]]
[7030234, 37110063, 6675731, 8611088, 12539195, 64660137, 8064182, 5709396, 56927064, 142038459, 273571071]
[[(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)], [(0, 100)]]
[None, None, None, None, None, None, None, None, None, None, None]
[(58584.0,), (61144.0,), (63213.0,), (65301.0,), (65564.0,), (66386.0,), (67026.0,), (67191.0,), (67400.0,), (67976.0,), (69279.0,)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
(200,)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-11c5e854b34a> in <module>()
     19 print(rIDs)
     20 print(box_width)
---> 21 a1,a2,a3,a4,a5 = ng.analysis.linesh.fit_spectrum(data,lsh,params,amps,bounds,ampbounds,centers,rIDs,box_width,0)

/Users/kbr/anaconda/lib/python2.7/site-packages/nmrglue/analysis/linesh.pyc in fit_spectrum(spectrum, lineshapes, params, amps, bounds, ampbounds, centers, rIDs, box_width, error_flag, verb, **kw)
    271         ecbounds = [[zip(*[ls.add_edge(b, (mn, mx)) for b in zip(*db)])
    272                     for ls, mn, mx, db in zip(ls_classes, rmin, rmax, pb)]
--> 273                     for pb in cbounds]
    274 
    275         # fit the region

Jonathan Helmus

unread,
Dec 8, 2014, 12:37:32 PM12/8/14
to nmrglue...@googlegroups.com
Kresten,

   Suggestions are inline


On 12/05/2014 01:39 PM, Kresten Bertelsen wrote:
Hi,

I'm reworking this tread since I have come about the same problem. This is my first experience with python so forgive me if I ask trivial questions.

Below is some code which reads in a processed spectrum, peak picks it and the tries to deconvolute. The deconvolution fails and
fiddling around with the debugger it seems it is the command zip(*db) which fails since db is (0,100) and cannot be unzipped.

This must relate to format of the passed parameters but I cannot find the error. Please Help


import nmrglue as ng
import numpy as np

dic, data = ng.bruker.read_pdata('./Data Files/P31_1D/10/pdata/1')
SF=dic['procs']['SF']
SW_p=dic['procs']['SW_p']
SI=dic['procs']['SI']
OFFSET=dic['procs']['OFFSET']
AX=np.linspace(OFFSET,OFFSET-SW_p/SF,SI)

peaktab = ng.analysis.peakpick.pick(data,np.max(data)*0.02)
# Prepare the inputs for deconvolution
lsh= list('g' for x in range(np.size(peaktab['X_AXIS'])))
I think this should just be a length 1 list (for the one dimension), so ['g']. 

params=[]
for idx,peak  in enumerate(peaktab['X_AXIS']):
    params.append([tuple((peak,10))])
amps=list(data[[int(x) for x in peaktab['X_AXIS']]])
bounds=[[(0,100)] for x in range(np.size(peaktab['X_AXIS']))]
This in only specifying a bound for the first parameter of the lineshape (the peak location) you also need to provide one for the second parameter, the linewidth.  The following should work which add only a lower bound to the lineshape.

bounds=[[(0,100), (0, None)] for x in range(np.size(peaktab['X_AXIS']))]



ampbounds =[None for x in range(np.size(peaktab['X_AXIS']))]
centers = list([(peaktab['X_AXIS'][x],) for x in range(np.size(peaktab['X_AXIS']))])
The center of the region may need to be an interger, not sure if a float will work but casting these to ints will not hurt:

centers = list([(int(peaktab['X_AXIS'][x]),) for x in range(np.size(peaktab['X_AXIS']))])
Hope this helps,

    - Jonathan Helmus

Jonathan Helmus

unread,
Dec 8, 2014, 3:29:53 PM12/8/14
to nmrglue...@googlegroups.com
Kresten,

    You may found the following example which finds and fits peaks in a 1D spectrum using nmrglue helpful:

https://github.com/jjhelmus/nmrglue/wiki/Find-and-fit-peaks-in-a-spectrum

Cheers,

    - Jonathan Helmus



On 12/05/2014 01:39 PM, Kresten Bertelsen wrote:
--
You received this message because you are subscribed to the Google Groups "nmrglue-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nmrglue-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages