-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPeakFinding.py
More file actions
150 lines (100 loc) · 3.75 KB
/
PeakFinding.py
File metadata and controls
150 lines (100 loc) · 3.75 KB
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
from __future__ import print_function
from __future__ import division
import numpy as np
from matplotlib import pyplot as plt
def vote_peaks(signal, filter_size=1,passes=2,threshold=.8):
"""
Input:
signal : dictionary, contains two_theta, d_spacings, and input_vector arrays
peaks locations found in the profile
filter_size : int, starting bin width of each pass
passes : int, number of voting rounds
thresh : 0 < float < 1, percentage votes to be considered a peak
Outputs:
payload : dictionary, contains classification statistics and predictions
"""
# determine size of filter
size = len(signal)
# Pad profile so it can be indexed properly
signal = np.pad(signal,(filter_size+passes,filter_size+passes),'constant', constant_values=(0))
# create vote holder array
votes = np.zeros_like(signal)
# iterate over the array accruing votes for the tallest peaks
for j in range(passes):
# step through the array and vote for peak locations
for i in range(0,size+j):
scalar = 1
votes[np.argmax(signal[i:i+filter_size+j])+i] += scalar
# Pare down the votes to remove extraneous peaks but preserve candidates
votes[votes<np.amax(votes)*threshold] = 0
# trim off the padding from the votes array
inner = (filter_size+passes)
peak_locs_pixel = votes[inner:-inner]
return peak_locs_pixel
def pixel2theta(x,SIZE=1e-9,DIST=300.0,WAVE=1.54046):
"""
Inputs:
x : array, vector of bins
SIZE: float, pixel_size in microns/pixel
DIST: float, distance of camera from sample in millimeters
WAVE: float, wave length in nanometers
Outputs:
theta: array, vector of two theta values
d: array, vector of d space values
"""
r = x*SIZE*1e-3 #in millimeters
d = DIST * WAVE / r
theta = np.arcsin((1/(d)))*360/np.pi #radians->degrees*2
return theta, d
def profile2theta(pixel_profile,SIZE=1e9,WAVE=.15406):
"""
Inputs:
pixel_profile : array, vector of bins in inverse D
SIZE: float, pixel_size in microns/pixel
WAVE: float, wave length in nanometers
Outputs:
t_scale: array, vector of two theta values
rd_scale: array, vector of d space values
"""
id_scale = np.linspace(0.00001,len(pixel_profile),len(pixel_profile))*SIZE
rd_scale = 1/id_scale
t_scale = d2theta(rd_scale,WAVE)
return t_scale, rd_scale
def d2theta(d,wavelength=.15406):
"""
Inputs:
pixel_profile : array, vector of bins in D
WAVE: float, wave length in nanometers
Outputs:
theta_vec: array, vector of two theta values
"""
theta_vec = 2 * np.arcsin(wavelength/(d*2)) * 180/np.pi
return theta_vec
def plot_peaks(signal,scale,votes,display_type):
"""
Inputs:
signal : array, vector of bins in inverse D
scale: array, pixel_size in microns/pixel
votes: array, wave length in nanometers
display_type: string, d or theta
Outputs:
None
"""
indicies = np.where(votes>0)
plt.figure(figsize=(6,2))
plt.plot(scale,signal,linewidth=3)
sig_min = np.amin(signal)
counter = 1
for index in indicies[0]:
x = np.array([scale[index],scale[index]])
y = np.array([signal[index],sig_min])
plt.plot(x,y,linewidth=2,label="peak {}".format(counter))
counter += 1
if display_type == "d":
plt.xlim(.5,6)
plt.title("d spacing")
elif display_type == "theta":
plt.xlim(0,110)
plt.title("two theta")
plt.legend()
plt.show(block=False)