0915CXH_DL_SA/utils/ResultSummary.py

67 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:andrew
@file:ResultSummary.py
@email:admin@marques22.com
@email:2021022362@m.scnu.edu.cn
@time:2023/09/15
"""
import numpy as np
import pandas as pd
def AnalyseSegment(df_segment, thresh=0.5, thresh_event_interval=2, thresh_event_length=8,
save_path=None, true_sleep_time=0):
df_segment["thresh_Pred"] = df_segment["pred"].apply(lambda x: 1 if x > thresh else 0)
thresh_Pred = df_segment["thresh_Pred"].values
thresh_Pred2 = thresh_Pred.copy()
# 扩充
indices = np.where(thresh_Pred == 1)[0]
for i in range(len(indices) - 1):
if indices[i + 1] - indices[i] <= thresh_event_interval:
thresh_Pred2[indices[i]:indices[i + 1]] = 1
# 事件判断
# 如果连续的1的长度大于阈值则判断为事件记录起止位置
diffs = np.diff(thresh_Pred2, prepend=0, append=0)
start_indices = np.where(diffs == 1)[0]
end_indices = np.where(diffs == -1)[0]
event_indices = np.where(end_indices - start_indices >= thresh_event_length)[0]
result = [(start_indices[i], end_indices[i]) for i in event_indices]
df_event = pd.DataFrame(result, columns=["start", "end"])
df_event["length"] = df_event["end"] - df_event["start"]
df_event["start"] = df_event["start"] - 29
df_event["end"] = df_event["end"] - 29
if save_path is not None:
df_event.to_csv(save_path, index=False)
# 根据AHI评估严重程度
if true_sleep_time is 0:
record_length = len(df_segment) / 60 / 60
AHI = len(result) / record_length
else:
record_length = true_sleep_time / 60
AHI = len(result) / record_length
if AHI < 5:
severity = "Healthy"
elif AHI < 15:
severity = "Mild"
elif AHI < 30:
severity = "Moderate"
else:
severity = "Severe"
SA_HYP_count = len(result)
print("patient: ", save_path.stem)
print("Event number: ", SA_HYP_count)
print("Record length (hours): ", round(record_length, 2))
print("AHI: ", AHI)
print("Severity: ", severity)
print("=====================================")
return SA_HYP_count, AHI, severity