Arrays, plotting, fitting gaussian distribution for multiples on graph which represents a power spectrum

StackOverflow https://stackoverflow.com/questions/20317157

Question

This is the code I have done which reads in a data text file which has some metadata at the start. And then 2 columns of data which represent Angle 2 theta on the left column and Radiation count in the right column. My code looks for the value of wavelenght in metadata and stores it in a variable for later use, it then resets to the start of the file, skips the metadata and reads in the dataset needed into an array. The plot of log of Radiation count vs Angle of Diffraction is plot. There are several peaks on this plot, I want to be able to access each peak individually, calculate and store parameters such as the Full Width Half Maximum and The Area under Curve for later access. Is searching the main array called data , scanning through it creating smaller arrays to store the additional data I need to calculate the parameters of each individual peak a good method to use? Here is my code below which is followed by the data I am using in the text file.

import matplotlib.pyplot as plt
import numpy as np
import math
figure = plt.figure()
count = 0
with open("chan.txt", "r") as metapart:
    for line in metapart:
        if "&END" in line:
            break
        count = count + 1
    print count
    print metapart.next()
    metapart.seek(0)
    data = np.genfromtxt(metapart, skiprows=11)
    print data
metapart.close()
x_data = data[:,0]
y_init = data[:,1]
y_data = []
for i in y_init:
    if i > 0:
        y_data.append(math.log10(i))
    else:
        y_data.append(i)
print y_data
plt.plot(x_data,y_data, 'r')
plt.show()

Here is the data from text file:

 &SRS
<MetaDataAtStart>
multiple=True
Wavelength (Angstrom)=0.97587
mode=assessment
background=False
issid=py11n2g
noisy=False
</MetaDataAtStart>
&END
Two Theta(deg)  Counts(sec^-1)
10.0    0.0
10.1    0.0
10.2    0.0
10.3    0.0
10.4    0.0
10.5    0.0
10.6    0.0
10.7    0.0
10.8    0.0
10.9    0.0
11.0    0.0
11.1    0.0
11.2    0.0
11.3    0.0
11.4    0.0
11.5    0.0
11.6    0.0
11.7    0.0
11.8    0.0
11.9    0.0
12.0    0.0
12.1    0.0
12.2    0.0
12.3    0.0
12.4    0.0
12.5    0.0
12.6    0.0
12.7    0.0
12.8    0.0
12.9    0.0
13.0    0.0
13.1    0.0
13.2    0.0
13.3    0.0
13.4    0.0
13.5    0.0
13.6    0.0
13.7    0.0
13.8    0.0
13.9    0.0
14.0    0.0
14.1    0.0
14.2    0.0
14.3    0.0
14.4    0.0
14.5    0.0
14.6    0.0
14.7    0.0
14.8    0.0
14.9    0.0
15.0    0.0
15.1    0.0
15.2    0.0
15.3    0.0
15.4    0.0
15.5    0.0
15.6    0.0
15.7    0.0
15.8    0.0
15.9    0.0
16.0    0.0
16.1    0.0
16.2    0.0
16.3    0.0
16.4    0.0
16.5    0.0
16.6    0.0
16.7    0.0
16.8    0.0
16.9    0.0
17.0    0.0
17.1    0.0
17.2    0.0
17.3    0.0
17.4    0.0
17.5    0.0
17.6    0.0
17.7    0.0
17.8    0.0
17.9    0.0
18.0    0.0
18.1    0.0
18.2    0.0
18.3    0.0
18.4    0.0
18.5    0.0
18.6    0.0
18.7    0.0
18.8    0.0
18.9    0.0
19.0    0.0
19.1    0.0
19.2    0.0
19.3    0.0
19.4    0.0
19.5    0.0
19.6    0.0
19.7    0.0
19.8    0.0
19.9    0.0
20.0    0.0
20.1    0.0
20.2    0.0
20.3    0.0
20.4    0.0
20.5    0.0
20.6    0.0
20.7    0.0
20.8    0.0
20.9    0.0
21.0    0.0
21.1    0.0
21.2    0.0
21.3    0.0
21.4    0.0
21.5    0.0
21.6    0.0
21.7    0.0
21.8    0.0
21.9    0.0
22.0    0.0
22.1    0.0
22.2    0.0
22.3    0.0
22.4    0.0
22.5    0.0
22.6    0.0
22.7    0.0
22.8    0.0
22.9    0.0
23.0    0.0
23.1    0.0
23.2    0.0
23.3    0.0
23.4    0.0
23.5    0.0
23.6    0.0
23.7    2.0
23.8    8.0
23.9    30.0
24.0    86.0
24.1    205.0
24.2    400.0
24.3    642.0
24.4    848.0
24.5    922.0
24.6    823.0
24.7    605.0
24.8    365.0
24.9    181.0
25.0    74.0
25.1    25.0
25.2    7.0
25.3    2.0
25.4    0.0
25.5    0.0
25.6    0.0
25.7    0.0
25.8    0.0
25.9    0.0
26.0    0.0
26.1    0.0
26.2    0.0
26.3    0.0
26.4    0.0
26.5    0.0
26.6    0.0
26.7    0.0
26.8    0.0
26.9    0.0
27.0    0.0
27.1    0.0
27.2    0.0
27.3    0.0
27.4    0.0
27.5    1.0
27.6    3.0
27.7    10.0
27.8    33.0
27.9    88.0
28.0    193.0
28.1    347.0
28.2    515.0
28.3    630.0
28.4    636.0
28.5    529.0
28.6    363.0
28.7    206.0
28.8    96.0
28.9    37.0
29.0    12.0
29.1    3.0
29.2    1.0
29.3    0.0
29.4    0.0
29.5    0.0
29.6    0.0
29.7    0.0
29.8    0.0
29.9    0.0
30.0    0.0
30.1    0.0
30.2    0.0
30.3    0.0
30.4    0.0
30.5    0.0
30.6    0.0
30.7    0.0
30.8    0.0
30.9    0.0
31.0    0.0
31.1    0.0
31.2    0.0
31.3    0.0
31.4    0.0
31.5    0.0
31.6    0.0
31.7    0.0
31.8    0.0
31.9    0.0
32.0    0.0
32.1    0.0
32.2    0.0
32.3    0.0
32.4    0.0
32.5    0.0
32.6    0.0
32.7    0.0
32.8    0.0
32.9    0.0
33.0    0.0
33.1    0.0
33.2    0.0
33.3    0.0
33.4    0.0
33.5    0.0
33.6    0.0
33.7    0.0
33.8    0.0
33.9    0.0
34.0    0.0
34.1    0.0
34.2    0.0
34.3    0.0
34.4    0.0
34.5    0.0
34.6    0.0
34.7    0.0
34.8    0.0
34.9    0.0
35.0    0.0
35.1    0.0
35.2    0.0
35.3    0.0
35.4    0.0
35.5    0.0
35.6    0.0
35.7    0.0
35.8    0.0
35.9    0.0
36.0    0.0
36.1    0.0
36.2    0.0
36.3    0.0
36.4    0.0
36.5    0.0
36.6    0.0
36.7    0.0
36.8    0.0
36.9    0.0
37.0    0.0
37.1    0.0
37.2    0.0
37.3    0.0
37.4    0.0
37.5    0.0
37.6    0.0
37.7    0.0
37.8    0.0
37.9    0.0
38.0    0.0
38.1    0.0
38.2    0.0
38.3    0.0
38.4    0.0
38.5    0.0
38.6    0.0
38.7    0.0
38.8    0.0
38.9    0.0
39.0    0.0
39.1    0.0
39.2    0.0
39.3    0.0
39.4    0.0
39.5    0.0
39.6    0.0
39.7    1.0
39.8    5.0
39.9    18.0
40.0    53.0
40.1    125.0
40.2    249.0
40.3    415.0
40.4    575.0
40.5    667.0
40.6    645.0
40.7    521.0
40.8    352.0
40.9    198.0
41.0    93.0
41.1    37.0
41.2    12.0
41.3    3.0
41.4    1.0
41.5    0.0
41.6    0.0
41.7    0.0
41.8    0.0
41.9    0.0
42.0    0.0
42.1    0.0
42.2    0.0
42.3    0.0
42.4    0.0
42.5    0.0
42.6    0.0
42.7    0.0
42.8    0.0
42.9    0.0
43.0    0.0
43.1    0.0
43.2    0.0
43.3    0.0
43.4    0.0
43.5    0.0
43.6    0.0
43.7    0.0
43.8    0.0
43.9    0.0
44.0    0.0
44.1    0.0
44.2    0.0
44.3    0.0
44.4    0.0
44.5    0.0
44.6    0.0
44.7    0.0
44.8    0.0
44.9    0.0
45.0    0.0
45.1    0.0
45.2    0.0
45.3    0.0
45.4    0.0
45.5    0.0
45.6    0.0
45.7    0.0
45.8    0.0
45.9    0.0
46.0    0.0
46.1    0.0
46.2    0.0
46.3    0.0
46.4    0.0
46.5    0.0
46.6    0.0
46.7    0.0
46.8    0.0
46.9    0.0
47.0    1.0
47.1    5.0
47.2    18.0
47.3    58.0
47.4    155.0
47.5    351.0
47.6    670.0
47.7    1079.0
47.8    1463.0
47.9    1672.0
48.0    1610.0
48.1    1307.0
48.2    894.0
48.3    515.0
48.4    250.0
48.5    103.0
48.6    35.0
48.7    10.0
48.8    3.0
48.9    1.0
49.0    0.0
49.1    0.0
49.2    0.0
49.3    1.0
49.4    2.0
49.5    8.0
49.6    25.0
49.7    63.0
49.8    135.0
49.9    244.0
50.0    374.0
50.1    483.0
50.2    528.0
50.3    488.0
50.4    382.0
50.5    252.0
50.6    141.0
50.7    66.0
50.8    26.0
50.9    9.0
51.0    3.0
51.1    1.0
51.2    0.0
51.3    0.0
51.4    0.0
51.5    0.0
51.6    0.0
51.7    0.0
51.8    0.0
51.9    0.0
52.0    0.0
52.1    0.0
52.2    0.0
52.3    0.0
52.4    0.0
52.5    0.0
52.6    0.0
52.7    0.0
52.8    0.0
52.9    0.0
53.0    0.0
53.1    0.0
53.2    0.0
53.3    0.0
53.4    0.0
53.5    0.0
53.6    0.0
53.7    0.0
53.8    0.0
53.9    0.0
54.0    0.0
54.1    0.0
54.2    0.0
54.3    0.0
54.4    0.0
54.5    0.0
54.6    0.0
54.7    0.0
54.8    0.0
54.9    0.0
55.0    0.0
55.1    0.0
55.2    0.0
55.3    0.0
55.4    0.0
55.5    0.0
55.6    0.0
55.7    0.0
55.8    0.0
55.9    0.0
56.0    0.0
56.1    0.0
56.2    0.0
56.3    0.0
56.4    0.0
56.5    0.0
56.6    0.0
56.7    0.0
56.8    0.0
56.9    0.0
57.0    0.0
57.1    0.0
57.2    0.0
57.3    0.0
57.4    0.0
57.5    0.0
57.6    0.0
57.7    0.0
57.8    1.0
57.9    3.0
58.0    10.0
58.1    27.0
58.2    60.0
58.3    113.0
58.4    184.0
58.5    256.0
58.6    305.0
58.7    310.0
58.8    270.0
58.9    202.0
59.0    129.0
59.1    70.0
59.2    33.0
59.3    13.0
59.4    4.0
59.5    1.0
59.6    0.0
59.7    0.0
59.8    0.0
59.9    0.0
60.0    0.0
60.1    0.0
60.2    0.0
60.3    0.0
60.4    0.0
60.5    0.0
60.6    0.0
60.7    0.0
60.8    0.0
60.9    0.0
61.0    0.0
61.1    0.0
61.2    0.0
61.3    0.0
61.4    0.0
61.5    0.0
61.6    0.0
61.7    0.0
61.8    0.0
61.9    0.0
62.0    0.0
62.1    0.0
62.2    0.0
62.3    0.0
62.4    0.0
62.5    0.0
62.6    0.0
62.7    0.0
62.8    0.0
62.9    0.0
63.0    0.0
63.1    0.0
63.2    0.0
63.3    0.0
63.4    0.0
63.5    0.0
63.6    0.0
63.7    0.0
63.8    0.0
63.9    0.0
64.0    0.0
64.1    0.0
64.2    0.0
64.3    0.0
64.4    0.0
64.5    0.0
64.6    0.0
64.7    0.0
64.8    0.0
64.9    0.0
65.0    0.0
65.1    0.0
65.2    0.0
65.3    0.0
65.4    0.0
65.5    0.0
65.6    0.0
65.7    0.0
65.8    0.0
65.9    0.0
66.0    0.0
66.1    0.0
66.2    0.0
66.3    0.0
66.4    0.0
66.5    0.0
66.6    0.0
66.7    0.0
66.8    0.0
66.9    0.0
67.0    0.0
67.1    0.0
67.2    0.0
67.3    0.0
67.4    0.0
67.5    0.0
67.6    0.0
67.7    0.0
67.8    0.0
67.9    0.0
68.0    0.0
68.1    0.0
68.2    0.0
68.3    0.0
68.4    0.0
68.5    0.0
68.6    0.0
68.7    0.0
68.8    0.0
68.9    0.0
69.0    0.0
69.1    0.0
69.2    0.0
69.3    0.0
69.4    0.0
69.5    0.0
69.6    0.0
69.7    0.0
69.8    0.0
69.9    0.0
70.0    0.0
70.1    0.0
70.2    0.0
70.3    0.0
70.4    0.0
70.5    0.0
70.6    0.0
70.7    0.0
70.8    0.0
70.9    0.0
71.0    0.0
71.1    0.0
71.2    0.0
71.3    0.0
71.4    0.0
71.5    0.0
71.6    0.0
71.7    0.0
71.8    0.0
71.9    0.0
72.0    0.0
72.1    0.0
72.2    0.0
72.3    0.0
72.4    0.0
72.5    0.0
72.6    0.0
72.7    0.0
72.8    0.0
72.9    0.0
73.0    0.0
73.1    0.0
73.2    0.0
73.3    0.0
73.4    0.0
73.5    0.0
73.6    0.0
73.7    0.0
73.8    0.0
73.9    0.0
74.0    0.0
74.1    0.0
74.2    0.0
74.3    0.0
74.4    0.0
74.5    0.0
74.6    0.0
74.7    0.0
74.8    0.0
74.9    0.0
75.0    0.0
75.1    0.0
75.2    0.0
75.3    0.0
75.4    0.0
75.5    0.0
75.6    0.0
75.7    0.0
75.8    0.0
75.9    0.0
76.0    0.0
76.1    0.0
76.2    0.0
76.3    0.0
76.4    0.0
76.5    0.0
76.6    0.0
76.7    0.0
76.8    0.0
76.9    0.0
77.0    0.0
77.1    0.0
77.2    0.0
77.3    0.0
77.4    0.0
77.5    0.0
77.6    0.0
77.7    0.0
77.8    0.0
77.9    0.0
78.0    0.0
78.1    0.0
78.2    0.0
78.3    0.0
78.4    0.0
78.5    0.0
78.6    0.0
78.7    0.0
78.8    0.0
78.9    0.0
79.0    0.0
79.1    0.0
79.2    0.0
79.3    0.0
79.4    0.0
79.5    0.0
79.6    0.0
79.7    0.0
79.8    0.0
79.9    0.0
80.0    0.0
80.1    0.0
80.2    0.0
80.3    0.0
80.4    0.0
80.5    0.0
80.6    0.0
80.7    0.0
80.8    0.0
80.9    0.0
81.0    0.0
81.1    0.0
81.2    0.0
81.3    0.0
81.4    0.0
81.5    0.0
81.6    0.0
81.7    0.0
81.8    0.0
81.9    0.0
82.0    0.0
82.1    0.0
82.2    0.0
82.3    0.0
82.4    0.0
82.5    0.0
82.6    0.0
82.7    0.0
82.8    0.0
82.9    0.0
83.0    0.0
83.1    0.0
83.2    0.0
83.3    0.0
83.4    0.0
83.5    0.0
83.6    0.0
83.7    0.0
83.8    0.0
83.9    0.0
84.0    0.0
84.1    0.0
84.2    0.0
84.3    0.0
84.4    0.0
84.5    0.0
84.6    0.0
84.7    0.0
84.8    0.0
84.9    0.0
85.0    0.0
85.1    0.0
85.2    0.0
85.3    0.0
85.4    0.0
85.5    0.0
85.6    0.0
85.7    0.0
85.8    0.0
85.9    0.0
86.0    0.0
86.1    0.0
86.2    0.0
86.3    0.0
86.4    0.0
86.5    0.0
86.6    0.0
86.7    0.0
86.8    0.0
86.9    0.0
87.0    0.0
87.1    0.0
87.2    0.0
87.3    0.0
87.4    0.0
87.5    0.0
87.6    0.0
87.7    0.0
87.8    0.0
87.9    0.0
88.0    0.0
88.1    0.0
88.2    0.0
88.3    0.0
88.4    0.0
88.5    0.0
88.6    0.0
88.7    0.0
88.8    0.0
88.9    0.0
89.0    0.0
89.1    0.0
89.2    0.0
89.3    0.0
89.4    0.0
89.5    0.0
89.6    0.0
89.7    0.0
89.8    0.0
89.9    0.0
90.0    0.0
90.1    0.0
90.2    0.0
90.3    0.0
90.4    0.0
90.5    0.0
90.6    0.0
90.7    0.0
90.8    0.0
90.9    0.0
91.0    0.0
91.1    0.0
91.2    0.0
91.3    0.0
91.4    0.0
91.5    0.0
91.6    0.0
91.7    0.0
91.8    0.0
91.9    0.0
92.0    0.0
92.1    0.0
92.2    0.0
92.3    0.0
92.4    0.0
92.5    0.0
92.6    0.0
92.7    0.0
92.8    0.0
92.9    0.0
93.0    0.0
93.1    0.0
93.2    0.0
93.3    0.0
93.4    0.0
93.5    0.0
93.6    0.0
93.7    0.0
93.8    0.0
93.9    0.0
94.0    0.0
94.1    0.0
94.2    0.0
94.3    0.0
94.4    0.0
94.5    0.0
94.6    0.0
94.7    0.0
94.8    0.0
94.9    0.0
95.0    0.0
95.1    0.0
95.2    0.0
95.3    0.0
95.4    0.0
95.5    0.0
95.6    0.0
95.7    0.0
95.8    0.0
95.9    0.0
96.0    0.0
96.1    0.0
96.2    0.0
96.3    0.0
96.4    0.0
96.5    0.0
96.6    0.0
96.7    0.0
96.8    0.0
96.9    0.0
97.0    0.0
97.1    0.0
97.2    0.0
97.3    0.0
97.4    0.0
97.5    0.0
97.6    0.0
97.7    0.0
97.8    0.0
97.9    0.0
98.0    0.0
98.1    0.0
98.2    0.0
98.3    0.0
98.4    0.0
98.5    0.0
98.6    0.0
98.7    0.0
98.8    0.0
98.9    0.0
99.0    0.0
99.1    0.0
99.2    0.0
99.3    0.0
99.4    0.0
99.5    0.0
99.6    0.0
99.7    0.0
99.8    0.0
99.9    0.0
100.0   0.0
100.1   0.0
100.2   0.0
100.3   0.0
100.4   0.0
100.5   0.0
100.6   0.0
100.7   0.0
100.8   0.0
100.9   0.0
101.0   0.0
101.1   0.0
101.2   0.0
101.3   0.0
101.4   0.0
101.5   0.0
101.6   0.0
101.7   0.0
101.8   0.0
101.9   0.0
102.0   0.0
102.1   0.0
102.2   0.0
102.3   0.0
102.4   0.0
102.5   0.0
102.6   0.0
102.7   0.0
102.8   0.0
102.9   0.0
103.0   0.0
103.1   0.0
103.2   0.0
103.3   0.0
103.4   0.0
103.5   0.0
103.6   0.0
103.7   0.0
103.8   0.0
103.9   0.0
104.0   0.0
104.1   0.0
104.2   0.0
104.3   0.0
104.4   0.0
104.5   0.0
104.6   0.0
104.7   0.0
104.8   0.0
104.9   0.0
105.0   0.0
105.1   0.0
105.2   0.0
105.3   0.0
105.4   0.0
105.5   0.0
105.6   0.0
105.7   0.0
105.8   0.0
105.9   0.0
106.0   0.0
106.1   0.0
106.2   0.0
106.3   0.0
106.4   0.0
106.5   0.0
106.6   0.0
106.7   0.0
106.8   0.0
106.9   0.0
107.0   0.0
107.1   0.0
107.2   0.0
107.3   0.0
107.4   0.0
107.5   0.0
107.6   0.0
107.7   0.0
107.8   0.0
107.9   0.0
108.0   0.0
108.1   0.0
108.2   0.0
108.3   0.0
108.4   0.0
108.5   0.0
108.6   0.0
108.7   0.0
108.8   0.0
108.9   0.0
109.0   0.0
109.1   0.0
109.2   0.0
109.3   0.0
109.4   0.0
109.5   0.0
109.6   0.0
109.7   0.0
109.8   0.0
109.9   0.0
110.0   0.0
Was it helpful?

Solution

Yes that is a suitable approach.

First some code to get you going (I tweaked your code):

import matplotlib.pyplot as plt
import numpy as np
import math
figure = plt.figure()

def Gaussian(x, Geoms):
    # Geoms is a n X 3 array:
    # Position, Amplitude,  FWHM

    #Geoms = [[2,.00001,3][1,17,.2]]

    Position = Geoms[:, 0]
    Amplitude = Geoms[:, 1]
    FWHM = Geoms[:, 2]

    y = np.zeros(x.shape)

    for i in np.arange(0, Amplitude.shape[0]):
        g = Position[i]; q = (x-g); v = FWHM[i]
        q = q*q; v = v*v
        y += (Amplitude[i] * np.exp(-q/(v*np.sqrt(2))))

    return y

count = 0
with open("chan.txt", "r") as metapart:
    for line in metapart:
        if "&END" in line:
            break
        count = count + 1
    print count
    print metapart.next()
    metapart.seek(0)
    data = np.genfromtxt(metapart, skiprows=11)
    print data
metapart.close()
x_data = data[:,0]
y_data = data[:,1]

# Build several gaussian peaks.
FWHM = 0.25
Geoms = np.array([[24.48, 922, FWHM], [28.35, 644, FWHM]])
y_fit = Gaussian(x_data, Geoms)

# I recommend keeping your data in its original form.  Only display it logarithmically if you want.
plt.semilogy(x_data,y_data, 'r', x_data, y_fit, 'b')
# And you may want to control the plot range.
plt.axis([0,100, 1,1000])
plt.show()

I added a function I wrote a while ago called Gaussian that, generates your peaks. Sorry it isn't better commented, but it should be pretty straightforward to use. It requires numpy inputs.

Second, I removed your for loop for truncating the data at 0. (You can look into numpy.clip() in the future if you want to do that). I recommend leaving your data in the original format, and only plot it logarithmically. This is especially true if you are doing fits, since otherwise you have to write a log_gaussian function, etc., and it gets confusing.

Here, I just dropped in the first two gaussians by hand. Of course, you can drop in the rest by hand and tweak the values until they are as accurate as the uncertainty imposed by the digitization of your data within a few minutes. If you're doing this once, that's what I'd do. But if you are doing this more than once, or you have more complicated shapes, I would spend the time to learn the SciPy optimize module. You probably want to start with the SciPy tutorial. Using ScyPy optimize has a longer runway, but it pays off with interest and dividends, and many stock splits.

Enjoy!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top