Files
Feature-Extraction/hand_coordinates.py
2025-10-20 22:01:18 +08:00

184 lines
8.5 KiB
Python

import matplotlib.pyplot as plt
import os
from global_var import global_var_init
cycle, fs, record_name, data_path = global_var_init()
from processing import processing
(signal_dwt , waves_dwt , rpeaks , ecg_signal,
signal, signal.v, signal.fs, rows_to_extract, t_ecg, t_ppg,
on_column_list, on_column_list_toArea, on_values, on_values_toArea,
sp_column_list, sp_values, dn_column_list, dn_values, dp_column_list,
dp_values, u_column_list, u_column_list_toArea, u_values, u_values_toArea,
v_column_list, v_values, w_column_list, w_values, a_column_list, a_column_list_toArea,
a_values, a_values_toArea, b_column_list, b_values, c_column_list, c_values,
e_column_list, e_values, f_column_list, f_values,
signal.ppg, signal.vpg, signal.apg, signal.jpg) = processing()
from ecg_peaks_val import ecg_peaks_val
(P_peaks, Q_peaks, R_peaks, S_peaks, T_peaks,
P_onsets, P_offsets, T_onsets, T_offsets,
P_peaks_values, Q_peaks_values, R_peaks_values, S_peaks_values, T_peaks_values,
P_onsets_values, P_offsets_values, T_onsets_values, T_offsets_values,
PQ_baseline) = ecg_peaks_val()
from ppg_peaks_val import ppg_peaks_val
(on, on_toArea, sp, dn, dp,
on_values, on_toArea_values, sp_values, dn_values, dp_values,
u, u_toArea, v, w,
u_values, u_toArea_values, v_values, w_values,
a, a_toArea, b, c, e, f,
a_values, a_toArea_values, b_values, c_values, e_values, f_values) = ppg_peaks_val()
# 创建一个字典来存储所有的特征点(用于测试)
features = {
"P_peaks_values": P_peaks_values,
"Q_peaks_values": Q_peaks_values,
"R_peaks_values": R_peaks_values,
"S_peaks_values": S_peaks_values,
"T_peaks_values": T_peaks_values,
"P_onsets_values": P_onsets_values,
"P_offsets_values": P_offsets_values,
"T_onsets_values": T_onsets_values,
"T_offsets_values": T_offsets_values,
"on_values": on_values,
"on_toArea_values": on_toArea_values,
"sp_values": sp_values,
"dn_values": dn_values,
"dp_values": dp_values,
"u_values": u_values,
"u_toArea_values": u_toArea_values,
"v_values": v_values,
"w_values": w_values,
"a_values": a_values,
"a_toArea_values": a_toArea_values,
"b_values": b_values,
"c_values": c_values,
"e_values": e_values,
"f_values": f_values,
}
# 遍历 features 字典并打印每个键对应的时间序列长度(用于测试)
for label, times in features.items():
print(f"{label}: {len(times)}")
# 设置保存目录
save_directory = "D://python_study//big_boss//doc//output"
# 文件名
file_name = "coordinates.txt"
# 文件名及路径
output_file = os.path.join(save_directory, file_name)
# 清空文件(如果存在)
open(output_file, "w").close()
# 状态变量:是否按下空格键
space_pressed = False
def on_key_press(event):
"""处理键盘按键事件,检查是否按下空格键。"""
global space_pressed
if event.key == ' ': # 检查是否按下空格键
space_pressed = True
def on_key_release(event):
"""处理键盘松开事件,松开空格键时取消按下状态。"""
global space_pressed
if event.key == ' ': # 检查是否松开空格键
space_pressed = False
def onclick(event):
"""处理鼠标点击事件,记录横坐标到文件,只有在按下空格键时才触发。"""
global space_pressed
if event.xdata is not None and space_pressed: # 判断是否按下空格键
x_coord = int(event.xdata*fs)
print(f"{x_coord},")
with open(output_file, "a") as f:
f.write(f"{x_coord},") # 将横坐标写入文件
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#----------------------------------- 创建绘图函数 ----------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
def create_plot_with_draggable_points(save_path = None):
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True)
# 绘制归一化后的ECG信号
ax1.plot(t_ecg, ecg_signal)
ax1.set(xlabel='', ylabel='ECG')
# 绘制归一化后的PPG信号
ax2.plot(t_ppg, signal.ppg)
ax2.set(xlabel='', ylabel='PPG')
# 绘制归一化后的一阶导数
ax3.plot(t_ppg, signal.vpg)
ax3.set(xlabel='', ylabel='vpg')
# 绘制归一化后的二阶导数
ax4.plot(t_ppg, signal.apg)
ax4.set(xlabel='Time (s)', ylabel='apg')
# 存储并绘制这些点对象
points_1 = [ax1.plot(x, y, 'o')[0] for x, y in zip(t_ecg[P_peaks], P_peaks_values)]
points_1 += [ax1.plot(x, y, 'o', color='cyan')[0] for x, y in zip(t_ecg[Q_peaks], Q_peaks_values)]
points_1 += [ax1.plot(x, y, 'o', color='purple')[0] for x, y in zip(t_ecg[R_peaks], R_peaks_values)]
points_1 += [ax1.plot(x, y, 'o', color='blue')[0] for x, y in zip(t_ecg[S_peaks], S_peaks_values)]
points_1 += [ax1.plot(x, y, 'o', color='black')[0] for x, y in zip(t_ecg[T_peaks], T_peaks_values)]
points_1 += [ax1.plot(x, y, 'x', color='red')[0] for x, y in zip(t_ecg[P_onsets], P_onsets_values)]
points_1 += [ax1.plot(x, y, 'x', color='magenta')[0] for x, y in zip(t_ecg[P_offsets], P_offsets_values)]
points_1 += [ax1.plot(x, y, 'x', color='black')[0] for x, y in zip(t_ecg[T_onsets], T_onsets_values)]
points_1 += [ax1.plot(x, y, 'o', color='red')[0] for x, y in zip(t_ecg[T_offsets], T_offsets_values)]
points_2 = [ax2.plot(x, y, 'o', color='red')[0] for x, y in zip(t_ppg[on], on_values)]
points_2 += [ax2.plot(x, y, 'o', color='blue')[0] for x, y in zip(t_ppg[sp], sp_values)]
points_2 += [ax2.plot(x, y, 'x', color='orange')[0] for x, y in zip(t_ppg[dn], dn_values)]
points_2 += [ax2.plot(x, y, 'o', color='purple')[0] for x, y in zip(t_ppg[dp], dp_values)]
points_3 = [ax3.plot(x, y, 'o', color='cyan')[0] for x, y in zip(t_ppg[u], u_values)]
points_3 += [ax3.plot(x, y, 'o', color='magenta')[0] for x, y in zip(t_ppg[v], v_values)]
points_3 += [ax3.plot(x, y, 'o', color='black')[0] for x, y in zip(t_ppg[w], w_values)]
points_4 = [ax4.plot(x, y, 'o', color='black')[0] for x, y in zip(t_ppg[a], a_values)]
points_4 += [ax4.plot(x, y, 'o', color='blue')[0] for x, y in zip(t_ppg[b], b_values)]
points_4 += [ax4.plot(x, y, 'o', color='green')[0] for x, y in zip(t_ppg[c], c_values)]
points_4 += [ax4.plot(x, y, 'o', color='orange')[0] for x, y in zip(t_ppg[e], e_values)]
points_4 += [ax4.plot(x, y, 'o', color='purple')[0] for x, y in zip(t_ppg[f], f_values)]
# 绑定事件
fig.canvas.mpl_connect('button_press_event', onclick)
fig.canvas.mpl_connect('key_press_event', on_key_press)
fig.canvas.mpl_connect('key_release_event', on_key_release)
# 如果指定了保存路径,则保存图像
if save_path:
plt.savefig(save_path)
plt.tight_layout()
plt.show()
# 将图形嵌入到 Qt 的 canvas 中
# canvas.figure = fig
# canvas.draw()
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#----------------------------------- 调用绘图函数 ----------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
create_plot_with_draggable_points()