from pathlib import Path import numpy as np import pandas as pd from matplotlib import pyplot as plt import os os.environ["DISPLAY"] = "localhost:10.0" from func import calculate_hr_spo2_zhihu, ppg2spo2_pipeline, func_1 from spo2_pipeline import spo2_pipeline def read_data(file_path): """ Read data from a file and return it as a list of floats. Args: file_path (str or Path): The path to the file to read. Returns: list of float: The data read from the file. """ df = pd.read_csv(file_path) # red = df['red'].values[300:] # ir = df['ied'].values[300:] red = df['red'].values[50:] ir = df['ied'].values[50:] return red, ir def read_labels(file_path): """ Read labels from a file and return them as a list of integers. Args: file_path (str or Path): The path to the file to read. Returns: list of int: The labels read from the file. """ df = pd.read_csv(file_path) timestamp = df['timestamp'].values hr_label = df['心率_值'].values spo2_label = df['血氧组_值'].values.astype(float) r_value = df['R组_均方根'].values np.place(spo2_label, spo2_label == 0, np.nan) return timestamp, hr_label, spo2_label, r_value def draw_compare(red_signal, ir_signal, spo2_values, hr_values, r_values, fs=25): """ Draw comparison plots for red and IR signals along with SpO2 values. Args: red_signal (array-like): The red light signal data. ir_signal (array-like): The infrared light signal data. spo2_values (array-like): The computed SpO2 values. fs (int): Sampling frequency in Hz. Default is 25. """ time_axis = [i / fs for i in range(len(red_signal))] plt.figure(figsize=(15, 10)) plt.subplot(3, 1, 1) plt.plot(time_axis, red_signal, label='Red Signal', color='red') plt.plot(time_axis, ir_signal, label='IR Signal', color='blue') plt.title('Red and IR Signals') plt.xlabel('Time (s)') plt.ylabel('Amplitude') plt.legend() plt.subplot(3, 1, 2) plt.plot(spo2_values, label='Computed SpO2', color='green') plt.title('Computed SpO2 Values') plt.xlabel('Beats') plt.ylabel('SpO2 (%)') plt.legend() plt.subplot(3, 1, 3) plt.plot(hr_values, label='Computed Heart Rate', color='orange') plt.title('Computed Heart Rate Values') plt.xlabel('Beats') plt.ylabel('Heart Rate (bpm)') plt.legend() plt.tight_layout() plt.show() if __name__ == '__main__': file_path = Path("./data/spo2hr[42-A3-C5-2F-F7-32]2025.11.17_17.12.15.csv") # file_path = "./data/spo2hr[42-A3-C5-2F-F7-32]2025.11.17_17.01.13.csv" red_signal, ir_signal = read_data(file_path) label_path = file_path.parent / "spo2_alg_info[42_A3_C5_2F_F7_32]2025.11.17_17.12.16.csv" timestamp, hr_label, spo2_label, r_value = read_labels(label_path) # draw_compare(red_signal, ir_signal, spo2_label, hr_label, r_value, fs=25) # df, summary = spo2_pipeline( # red_signal, # ir_signal, # fs=25, # calibrate_with_ref=spo2_label, # A=104.0, # B=17.0 # ) # print(summary) # print(df) # HR_result, SpO2_result = ppg2spo2_pipeline(red_signal, ir_signal, fs=25) func_1(red_signal, ir_signal, fs=25)