|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | + |
| 4 | +import time |
| 5 | +import logging |
| 6 | +from biosppy.storage import load_txt |
| 7 | +from biosppy.signals import ecg |
| 8 | +import matplotlib.pyplot as plt |
| 9 | + |
| 10 | +logging.basicConfig(level=logging.DEBUG, |
| 11 | + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +signal_path = "./data/ecg_records_117.txt" |
| 15 | +ann_path = "./data/ecg_ann_117.txt" |
| 16 | + |
| 17 | +logging.info("--------------------------------------------------") |
| 18 | +signal, _ = load_txt(signal_path) |
| 19 | +logging.info("载入信号-%s, 长度 = %d. " % (signal_path, len(signal))) |
| 20 | +ann, _ = load_txt(ann_path) |
| 21 | +logging.info("载入R峰位置人工标记, 共 %d 个R峰." % (len(ann))) |
| 22 | + |
| 23 | +fs = 360 # 信号采样率 360 Hz |
| 24 | +logging.info("调用 hamilton_segmenter 进行R波检测 ...") |
| 25 | +tic = time.time() |
| 26 | +rpeaks = ecg.hamilton_segmenter(signal, sampling_rate=fs) |
| 27 | +toc = time.time() |
| 28 | +logging.info("完成. 用时: %f 秒. " % (toc - tic)) |
| 29 | +rpeaks = rpeaks[0] |
| 30 | + |
| 31 | +logging.info("使用compare_segmentation对比算法结果与人工标记 ...") |
| 32 | +tic = time.time() |
| 33 | +eval_results = ecg.compare_segmentation(ann, rpeaks, fs, tol=0.02) |
| 34 | +toc = time.time() |
| 35 | +logging.info("完成. 用时: %f 秒. 返回结果类型为 %s ." % (toc - tic, str(type(eval_results)))) |
| 36 | + |
| 37 | +dict_results = eval_results.as_dict() |
| 38 | +logging.info("********** 结果报告 *************") |
| 39 | +logging.info("* 准确率(acc): %.3f *" % dict_results["acc"]) |
| 40 | +logging.info("* 总体表现(performance): %.3f *" % dict_results["performance"]) |
| 41 | +logging.info("*********************************") |
| 42 | + |
| 43 | +correct_tol = 0.05 |
| 44 | +logging.info("使用correct_rpeaks校正R波位置, 最大校正范围 %.3f 秒 ..." % correct_tol) |
| 45 | +tic = time.time() |
| 46 | +rpeaks_correct = ecg.correct_rpeaks(signal, rpeaks, fs, tol=correct_tol) |
| 47 | +toc = time.time() |
| 48 | +logging.info("完成. 用时: %f 秒. 返回结果类型为 %s ." % (toc - tic, |
| 49 | + str(type(rpeaks_correct)))) |
| 50 | +rpeaks_correct = rpeaks_correct.as_dict()["rpeaks"] |
| 51 | + |
| 52 | +logging.info("绘制部分R峰校正前后的位置 ...") |
| 53 | +num_plot_samples = 3600 |
| 54 | +sig_plot = signal[:num_plot_samples] |
| 55 | +rpeaks_plot = rpeaks[rpeaks <= num_plot_samples] |
| 56 | +rpeaks_correct_plot = rpeaks_correct[rpeaks_correct <= num_plot_samples] |
| 57 | +plt.figure() |
| 58 | +plt.grid(True) |
| 59 | +plt.plot(sig_plot, label="ECG") |
| 60 | +plt.plot(rpeaks_plot, sig_plot[rpeaks_plot], "ro", label="rpeaks") |
| 61 | +plt.plot(rpeaks_correct_plot, sig_plot[rpeaks_correct_plot], "b*", label="corrected rpeaks") |
| 62 | +plt.legend() |
| 63 | +plt.show() |
| 64 | +logging.info("绘图完成.") |
| 65 | + |
| 66 | +logging.info("使用biosppy.signals.ecg.ecg 综合处理 ...") |
| 67 | +tic = time.time() |
| 68 | +summary_result = ecg.ecg(sig_plot, fs, True) |
| 69 | +toc = time.time() |
| 70 | +logging.info("完成. 用时: %f 秒. 返回结果类型为 %s ." % (toc - tic, |
| 71 | + str(type(summary_result)))) |
| 72 | + |
| 73 | +summary_result = summary_result.as_dict() |
0 commit comments