-
Notifications
You must be signed in to change notification settings - Fork 0
/
ORCASpectrumPlot.py
303 lines (276 loc) · 9.97 KB
/
ORCASpectrumPlot.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# Import necessary libraries
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
from scipy.signal import convolve, gaussian
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
# Create an empty DataFrame to store spectrum data
spectrum_data = pd.DataFrame()
# Define a function to convolve a spectrum with a Gaussian function
def convolve_with_gaussian(energy, intensity, fwhm):
# Calculate the standard deviation (sigma) from FWHM
sigma = fwhm / np.sqrt(8 * np.log(2))
# Determine the number of points for the Gaussian kernel
n_points = int(fwhm * 10)
# Generate a Gaussian kernel
gaussian_kernel = gaussian(n_points, sigma)
gaussian_kernel /= gaussian_kernel.sum()
# Convolve the intensity with the Gaussian kernel
convolved_intensity = convolve(intensity, gaussian_kernel, mode="same")
return convolved_intensity
# Define a function to update and plot the spectrum
def update_plot(fwhm, max_energy, shift=0, show_fc=False, show_ht=False):
global spectrum_data
if not spectrum_data.empty:
# Shift the energy values by the specified amount
shifted_energy = spectrum_data["Energy"] + shift
# Sort the data by energy values
sorted_indices = shifted_energy.argsort()
shifted_energy = shifted_energy.iloc[sorted_indices]
sorted_intensity = spectrum_data["TotalSpectrum"].iloc[sorted_indices]
# Convolve the sorted intensity with a Gaussian
convolved = convolve_with_gaussian(shifted_energy, sorted_intensity, fwhm)
# Normalize the convolved spectrum
convolved *= sorted_intensity.max() / convolved.max()
# Apply a mask to limit the plotted energy range
mask = shifted_energy <= max_energy
filtered_spectrum = shifted_energy[mask]
filtered_convolved = convolved[mask]
# Clear the existing plot and create a new one
ax.clear()
ax.plot(
filtered_spectrum,
sorted_intensity[mask],
label="Original Spectrum",
color="blue",
)
ax.plot(
filtered_spectrum,
filtered_convolved,
label="Convolved Spectrum",
color="orange",
linestyle="--",
)
if show_fc:
sorted_intensity_fc = spectrum_data["IntensityFC"].iloc[sorted_indices]
ax.plot(
filtered_spectrum,
sorted_intensity_fc[mask],
label="IntensityFC",
color="green",
linestyle="-.",
)
if show_ht:
sorted_intensity_ht = spectrum_data["IntensityHT"].iloc[sorted_indices]
ax.plot(
filtered_spectrum,
sorted_intensity_ht[mask],
label="IntensityHT",
color="red",
linestyle="-.",
)
ax.set_title("Spectrum Convolution with Gaussian Function")
ax.set_xlabel("Energy (nm)")
ax.set_ylabel("Intensity")
ax.set_xlim(filtered_spectrum.min(), max_energy)
ax.legend()
ax.grid(True)
canvas.draw()
# Define a function to load spectrum data from a file
def load_data():
global spectrum_data
# Open a file dialog to select a data file
file_path = filedialog.askopenfilename()
if file_path:
try:
# Read the data from the selected file
spectrum_data = pd.read_csv(
file_path,
delim_whitespace=True,
usecols=[0, 1, 2, 3],
names=["Energy", "TotalSpectrum", "IntensityFC", "IntensityHT"],
)
# Convert columns to numeric values and handle NaN values
spectrum_data["Energy"] = pd.to_numeric(
spectrum_data["Energy"], errors="coerce"
)
spectrum_data["TotalSpectrum"] = pd.to_numeric(
spectrum_data["TotalSpectrum"], errors="coerce"
)
spectrum_data["IntensityFC"] = pd.to_numeric(
spectrum_data["IntensityFC"], errors="coerce"
)
spectrum_data["IntensityHT"] = pd.to_numeric(
spectrum_data["IntensityHT"], errors="coerce"
)
spectrum_data.dropna(inplace=True)
# Sort the data by energy values
spectrum_data.sort_values("Energy", inplace=True)
# Update scale ranges and enable UI elements
max_energy_scale.config(
from_=spectrum_data["Energy"].min(), to=spectrum_data["Energy"].max()
)
max_energy_scale.set(spectrum_data["Energy"].max())
shift_scale.config(from_=-max_energy_scale.get(), to=max_energy_scale.get())
shift_scale.set(0)
fwhm_scale.config(state="normal")
max_energy_scale.config(state="normal")
shift_scale.config(state="normal")
show_fc_button.config(state="normal")
show_ht_button.config(state="normal")
# Update the plot with initial settings
update_plot(fwhm_scale.get(), max_energy_scale.get(), shift_scale.get())
except Exception as e:
# Show an error message if loading data fails
messagebox.showerror("Error", f"Failed to load data: {e}")
else:
# Show an information message if no file is selected
messagebox.showinfo("Load Data", "No file selected.")
# Define a function to save the convolved spectrum data to a file
def save_data():
fwhm = fwhm_scale.get()
max_energy = max_energy_scale.get()
shift = shift_scale.get()
show_fc = show_fc_var.get()
show_ht = show_ht_var.get()
shifted_energy = spectrum_data["Energy"] + shift
sorted_indices = shifted_energy.argsort()
shifted_energy = shifted_energy.iloc[sorted_indices]
sorted_intensity = spectrum_data["TotalSpectrum"].iloc[sorted_indices]
convolved = convolve_with_gaussian(shifted_energy, sorted_intensity, fwhm)
convolved *= sorted_intensity.max() / convolved.max()
mask = shifted_energy <= max_energy
filtered_spectrum = shifted_energy[mask]
filtered_convolved = convolved[mask]
save_df = pd.DataFrame(
{
"ShiftedEnergy": filtered_spectrum,
"TotalSpectrum": sorted_intensity[mask],
"ConvolvedSpectrum": filtered_convolved,
}
)
if show_fc:
sorted_intensity_fc = spectrum_data["IntensityFC"].iloc[sorted_indices]
save_df["IntensityFC"] = sorted_intensity_fc[mask]
if show_ht:
sorted_intensity_ht = spectrum_data["IntensityHT"].iloc[sorted_indices]
save_df["IntensityHT"] = sorted_intensity_ht[mask]
# Open a file dialog to specify the save location and filename
file_path = filedialog.asksaveasfilename(
defaultextension=".csv",
filetypes=[("CSV files", "*.csv")],
)
if file_path:
# Save the convolved spectrum data to the selected file
save_df.to_csv(file_path, index=False)
# Show a success message
messagebox.showinfo(
"Save Data", "The convolved spectrum data has been saved successfully."
)
# Create the main application window
root = tk.Tk()
root.title("Spectrum Analyzer")
# Create a frame for plotting
plot_frame = tk.Frame(root)
plot_frame.pack(fill=tk.BOTH, expand=1)
# Create a Matplotlib figure and canvas for displaying the plot
fig, ax = plt.subplots(figsize=(10, 5))
canvas = FigureCanvasTkAgg(fig, master=plot_frame)
canvas.get_tk_widget().pack(fill=tk.BOTH, expand=1)
# Create a frame for toolbar buttons
toolbar_frame = tk.Frame(root)
toolbar_frame.pack(fill=tk.X)
# Create buttons for loading and saving data
load_button = tk.Button(toolbar_frame, text="Load Data", command=load_data)
load_button.pack(side=tk.LEFT, padx=2)
save_button = tk.Button(toolbar_frame, text="Save Data", command=save_data)
save_button.pack(side=tk.LEFT, padx=2)
# Create scales and checkboxes for controlling the plot
fwhm_scale = tk.Scale(
toolbar_frame,
from_=0.1,
to=200,
resolution=0.1,
orient=tk.HORIZONTAL,
label="FWHM",
command=lambda v: update_plot(
float(v),
max_energy_scale.get(),
shift_scale.get(),
show_fc_var.get(),
show_ht_var.get(),
),
)
fwhm_scale.set(1.0)
fwhm_scale.pack(side=tk.LEFT, fill=tk.X, expand=1)
fwhm_scale.config(state="disabled")
max_energy_scale = tk.Scale(
toolbar_frame,
from_=0,
to=2000,
resolution=1,
orient=tk.HORIZONTAL,
label="Max Energy",
command=lambda v: update_plot(
fwhm_scale.get(),
float(v),
shift_scale.get(),
show_fc_var.get(),
show_ht_var.get(),
),
)
max_energy_scale.set(1000)
max_energy_scale.pack(side=tk.LEFT, fill=tk.X, expand=1)
max_energy_scale.config(state="disabled")
shift_scale = tk.Scale(
toolbar_frame,
from_=-1000,
to=1000,
resolution=1,
orient=tk.HORIZONTAL,
label="Energy Shift (nm)",
command=lambda v: update_plot(
fwhm_scale.get(),
max_energy_scale.get(),
float(v),
show_fc_var.get(),
show_ht_var.get(),
),
)
shift_scale.set(0)
shift_scale.pack(side=tk.LEFT, fill=tk.X, expand=1)
shift_scale.config(state="disabled")
show_fc_var = tk.BooleanVar()
show_fc_button = tk.Checkbutton(
toolbar_frame,
text="Show FC",
variable=show_fc_var,
state="disabled",
command=lambda: update_plot(
fwhm_scale.get(),
max_energy_scale.get(),
shift_scale.get(),
show_fc_var.get(),
show_ht_var.get(),
),
)
show_fc_button.pack(side=tk.LEFT, padx=2)
show_ht_var = tk.BooleanVar()
show_ht_button = tk.Checkbutton(
toolbar_frame,
text="Show HT",
variable=show_ht_var,
state="disabled",
command=lambda: update_plot(
fwhm_scale.get(),
max_energy_scale.get(),
shift_scale.get(),
show_fc_var.get(),
show_ht_var.get(),
),
)
show_ht_button.pack(side=tk.LEFT, padx=2)
# Start the Tkinter main event loop
root.mainloop()