-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
297 lines (264 loc) · 13 KB
/
main.py
File metadata and controls
297 lines (264 loc) · 13 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
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
# -----------------------------------------------------------#
# Name: Chunli Tang #
# School: Auburn University #
# -----------------------------------------------------------#
import os
from tkinter import *
from matplotlib import pyplot
import matplotlib.patches as patches
import numpy as np
import pandas as pd
from tkinter import filedialog
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.shapes.autoshape import Shape as shape
from lmfit import Model
import lmfit
from skimage import filters, measure, morphology, restoration
class SHG_Processing():
def __init__(self):
super().__init__()
self.run()
def run(self):
root = Tk()
folder_selected = filedialog.askdirectory(initialdir="/Users/labaccess/Library/CloudStorage/Box-Box/Jin Lab Shared Folder/SHG-RA data")
# dir_list = os.listdir(folder_selected)
root.withdraw()
for i in range(len(folder_selected) - 1, 0, -1):
if folder_selected[i] == '/':
folder_name_pptx = folder_selected[i+1:]
break
folder_selected = folder_selected + "/"
Parameter = pd.read_csv(folder_selected + "Experimental_Parameters.txt", header=None, sep=':', engine='c')
print(Parameter)
Date = Parameter.iat[0, 1]
step_size = Parameter.iat[7, 1]
step_size = int(step_size)
file_name = str(Parameter.iat[1, 1]).replace(" ", "")
print(file_name)
avg_x = 0
avg_y = 0
iteration = 0
deg_file_org = []
sig_file_org = []
# for degree in range(0, 10, step_size):
degree = 130
temp_temp = 80
SHG_Raw = np.loadtxt(folder_selected + file_name + "_{}deg".format(degree) + ".txt", dtype=int, delimiter=',')
auto = True
if auto == True:
region_size = 80
# Variables to store the maximum intensity and its corresponding position
max_intensity = -np.inf
max_intensity_position = None
# Iterate over all possible square regions
for i in range(SHG_Raw.shape[0] - region_size + 1):
for j in range(SHG_Raw.shape[1] - region_size + 1):
# Extract the current square region
region = SHG_Raw[i:i + region_size, j:j + region_size]
# Calculate the intensity of the region
intensity = np.sum(region)
# Check if the current intensity is the maximum
if intensity > max_intensity:
max_intensity = intensity
max_intensity_position = (i, j)
# Get the coordinates of the square region with the maximum intensity
top_left_row, top_left_col = max_intensity_position
bottom_right_row = top_left_row + region_size - 1
bottom_right_col = top_left_col + region_size - 1
# Plot the heatmap with the square region of maximum intensity highlighted
pyplot.imshow(SHG_Raw, aspect='auto', vmin=0,vmax=5000)
polarization = Parameter.iat[8, 1]
pyplot.colorbar(label='{} Polarization'.format(polarization))
pyplot.scatter(top_left_col + region_size/2, top_left_row + region_size/2, s=30, color='tomato', marker='x')
exposure_time = str(float(Parameter.iat[9, 1]))
title = str(Parameter.iat[1, 1]) + '' + str(Parameter.iat[2, 1]) + '' + str(Parameter.iat[3, 1]) \
+ '\n' + str(Parameter.iat[4, 1]) + 'mW Exposure Time ' + exposure_time + 's Averaging ' \
+ str(int(Parameter.iat[11, 1]))
pyplot.title(title + ' at {} Degree'.format(degree), pad=10, wrap=True)
pyplot.gca().add_patch(pyplot.Rectangle((top_left_col, top_left_row), region_size, region_size,
edgecolor='white', facecolor='none', linewidth=2))
pyplot.savefig(folder_selected + "Figure_1.png")
pyplot.show()
else:
fig, ax = pyplot.subplots()
im = pyplot.imshow(SHG_Raw, vmin=0, vmax=5000)
polarization = Parameter.iat[8, 1]
pyplot.colorbar(label='{} Polarization'.format(polarization))
exposure_time = str(float(Parameter.iat[9, 1]))
title = str(Parameter.iat[1, 1]) + '' + str(Parameter.iat[2, 1]) + '' + str(Parameter.iat[3, 1]) \
+ '\n' + str(Parameter.iat[4, 1]) + 'mW Exposure Time ' + exposure_time + 's Averaging ' \
+ str(int(Parameter.iat[11, 1]))
pyplot.title(title + ' at {} Degree'.format(degree), pad=10, wrap=True)
def onclick(event):
if event.dblclick:
global cur_x, cur_y, click
cur_x = event.xdata
cur_y = event.ydata
click = event.button
if click == 1:
print('Closing')
pyplot.close()
connection_id = fig.canvas.mpl_connect('button_press_event', onclick)
pyplot.savefig(folder_selected+"Figure_1.png")
pyplot.show()
deg_file = []
sig_file = []
if not auto:
center_x = int(cur_x)
center_y = int(cur_y)
region_size = int(input('Enter the box size: '))
half_region_size = (np.ceil(region_size / 2)).astype(int)
SHG_Raw = np.loadtxt(folder_selected + file_name + "_{}deg".format(degree) + ".txt", dtype=int, delimiter=',')
fig, ax = pyplot.subplots()
region = SHG_Raw[center_x - half_region_size: center_x + half_region_size,
center_y - half_region_size: center_y + half_region_size]
im = ax.imshow(SHG_Raw, vmin=1000, vmax=5000)
fig.colorbar(im, ax=ax, label='Interactive colorbar')
ax.scatter(center_x, center_y, s=30, color='tomato', marker='x')
rect = patches.Rectangle((center_x - half_region_size, center_y - half_region_size),
region_size, region_size, linewidth=1, edgecolor='r', facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
pyplot.show()
for degree in range(0, 365, step_size):
deg_file = np.append(deg_file, degree)
SHG_Raw = np.loadtxt(folder_selected + file_name + "_{}deg".format(degree) + ".txt", dtype=int, delimiter=',')
if not auto:
region = SHG_Raw[center_x - half_region_size: center_x + half_region_size,
center_y - half_region_size: center_y + half_region_size]
else:
region = SHG_Raw[top_left_col: top_left_col + region_size, top_left_row: top_left_row + region_size]
small_sum = sum(map(sum, region))
large_sum = sum(map(sum, SHG_Raw))
bkg_pixel = (large_sum - small_sum) / (512 ** 2 - region_size ** 2)
sig = small_sum - bkg_pixel * region_size ** 2
sig_file = np.append(sig_file, sig)
sig_file = sig_file.astype(np.float64)
# sig_file = sig_file.tolist()
max_lim = max(sig_file)
min_lim = min(sig_file)
deg_file = deg_file * np.pi / 180
deg_file = deg_file.astype(np.float64)
# deg_file = deg_file.tolist()
fig, ax = pyplot.subplots(subplot_kw={'projection': 'polar'})
ax.plot(deg_file, sig_file, color='red')
ax.set_ylim(bottom=min_lim, top=max_lim)
# pyplot.autoscale()
pyplot.show()
pyplot.plot(deg_file, sig_file, linewidth=5, color='blue')
pyplot.show()
slope = (sig_file[-1] - sig_file[0]) / (deg_file[-1] - deg_file[0])
const = sig_file[-1] - slope * deg_file[-1]
# const = sig_file[-1] + slope * deg_file[-1]
sig_file = sig_file - (slope * deg_file + const)
sig_file = (sig_file/30)/380000
csv_file_name = 'Processed_Data.csv'
comb = pd.DataFrame(list(zip(deg_file, sig_file)))
rec_data = pd.DataFrame()
rec_data = pd.concat([rec_data, comb], ignore_index=True, axis=1)
rec_data.to_csv(folder_selected + csv_file_name, mode='a', index=False, encoding='utf-8-sig', header=None)
min_sig = min(sig_file)
sig_file = sig_file - min_sig
csv_file_name = 'Processed_Data_Min_Shift.csv'
comb = pd.DataFrame(list(zip(deg_file, sig_file)))
rec_data = pd.DataFrame()
rec_data = pd.concat([rec_data, comb], ignore_index=True, axis=1)
rec_data.to_csv(folder_selected + csv_file_name, mode='w', index=False, encoding='utf-8-sig', header=None)
pyplot.plot(deg_file, sig_file, linewidth=5, color='blue')
pyplot.show()
max_lim = max(sig_file)
min_lim = min(sig_file)
fig, ax = pyplot.subplots(subplot_kw={'projection': 'polar'})
ax.plot(deg_file, sig_file, color='red')
ax.set_ylim(bottom=min_lim, top=max_lim)
pyplot.title(title + '{} Polarization'.format(polarization), pad=10, wrap=True)
pyplot.tight_layout()
pyplot.savefig(folder_selected+"Figure_2.png")
pyplot.show()
fit = True
if fit == True:
def shg_sin(params, x, data=None):
A = params['A']
a2 = params['a2']
B = params['B']
x0 = params['x0']
model = (A*np.sin(a2-3*(x-x0))+B*np.sin(a2+(x-x0)))**2
if data is None:
return model
return model - data
def shg_cos(params, x, data=None):
A = params['A']
a2 = params['a2']
B = params['B']
model = A*np.cos(a2-3*x)+B*np.cos(a2+3*x)*2
if data is None:
return model
return model - data
# Create a Parameters object
params = lmfit.Parameters()
params.add('A', value=-0.1)
params.add('a2', value=-0.1)
params.add('B', value=11)
params.add('x0', value=0.1)
result_sin = lmfit.minimize(shg_sin, params, args=(deg_file,), kws={'data': sig_file})
sin_A = result_sin.params['A'].value
sin_a2 = result_sin.params['a2'].value
sin_B = result_sin.params['B'].value
sin_x0 = result_sin.params['x0'].value
fig, ax = pyplot.subplots(subplot_kw={'projection': 'polar'})
ax.scatter(deg_file, sig_file, color='black', s=1)
ax.plot(deg_file, result_sin.residual + sig_file, color='red',linewidth=2)
ax.set_ylim(bottom=min_lim*1.05, top=max_lim*1.05)
pyplot.title(title + '{} Polarization'.format(polarization), pad=10, wrap=True)
pyplot.tight_layout()
pyplot.savefig(folder_selected + "Fitted_Data.png")
pyplot.show()
df = pd.DataFrame()
df_comb = pd.DataFrame(list(zip([sin_A], [sin_a2], [sin_B], [sin_x0])))
df = pd.concat([df, df_comb], ignore_index=True, axis=1)
df.to_csv(folder_selected + 'Fitted_Data.csv', index=False)
if os.path.exists(folder_selected + 'Results.pptx'):
prs = Presentation(folder_selected + 'Results.pptx')
prs.slide_width = Inches(13.33)
prs.slide_height = Inches(7.5)
else:
prs = Presentation()
prs.slide_width = Inches(13.33)
prs.slide_height = Inches(7.5)
prs.save(folder_selected + 'Results.pptx')
prs = Presentation(folder_selected + 'Results.pptx')
prs.slide_width = Inches(13.33)
prs.slide_height = Inches(7.5)
SHG_Image = folder_selected + 'Figure_1.png'
SHG_Signal = folder_selected + 'Figure_2.png'
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
SHG_Image_img = slide.shapes.add_picture(SHG_Image, Inches(0.32), Inches(1.42), Inches(6.39))
SHG_Signal_img = slide.shapes.add_picture(SHG_Signal, Inches(6.49), Inches(1.42), Inches(6.39))
text_frame = slide.shapes.add_textbox(Inches(0.18), Inches(0.2), Inches(6.67), Inches(0.4))
Data_frame = slide.shapes.add_textbox(Inches(11.19), Inches(0.2), Inches(2.04), Inches(0.4))
text_frame = text_frame.text_frame
Data_frame = Data_frame.text_frame
p = text_frame.paragraphs[0]
d = Data_frame.paragraphs[0]
run = p.add_run()
run.text = str(folder_name_pptx)
font = run.font
font.name = 'Calibri'
font.size = Pt(18)
run_d = d.add_run()
run_d.text = str(Date)
font_d = run_d.font
font_d.name = 'Calibri'
font_d.size = Pt(18)
prs.save(folder_selected + 'Results.pptx')
if __name__ == '__main__':
running = True
while running:
# construct the main wi
try:
window1 = SHG_Processing()
except KeyboardInterrupt:
break
# endless loop unless quit