Beispiel #1
0
def plot_raw(path,
             emotion_section={
                 "Stress": [300, 600],
                 "Amusement": [900, 1200]
             }):
    arc = OpenSignalsReader(path)

    # 心拍データからピークを取り出す
    ecg_result = signals.ecg.ecg(signal=arc.signal(['ECG']),
                                 sampling_rate=1000.0,
                                 show=False)
    # 皮膚コンダクタンス からSCRを取り出す
    eda_filtered = eda_analysis.eda_preprocess(arc.signal(['EDA']), 1000.)

    # 呼吸周波数を取り出す
    resp_result = resp_analysis.resp(arc.signal('RESP'), show=False)

    # 描画設定
    fig, axes = plt.subplots(3,
                             1,
                             sharex=True,
                             figsize=(16, 9),
                             subplot_kw=({
                                 "xticks": np.arange(0, 1200, 100)
                             }))
    axes[0].set_title(path)
    # 心拍変動の描画
    axes[0].plot(ecg_result['heart_rate_ts'], ecg_result['heart_rate'], 'b')
    axes[0].set_xlim(0, 1200)
    axes[0].set_ylabel("HR[bpm]")

    # 皮膚コンダクタンスの描画
    axes[1].plot(arc.t, arc.signal(['EDA']))
    axes[1].set_ylabel('[us]')
    axes[1].set_ylim(0, 25)
    # 呼吸の描画
    axes[2].plot(arc.t, arc.signal(['RESP']))
    axes[2].set_ylabel('RESP[Hz]')
    axes[2].set_ylim(-50, 50)

    for i in range(3):
        for key in emotion_section.keys():
            if key == "Stress":
                axes[i].axvspan(
                    emotion_section[key][0],  #start
                    emotion_section[key][1],  #end
                    alpha=0.3,
                    color="r",
                    label="Stress")
            elif key == "Amusement":
                axes[i].axvspan(
                    emotion_section[key][0],  #start
                    emotion_section[key][1],  #end
                    alpha=0.3,
                    color="b",
                    label="Amusement")
    plt.legend()
    plt.tight_layout()
    plt.xlabel("Time[s]")
    return plt
def biosignal_multi_summary(path_list, emotion=None):
    i = 0
    df_summary = None
    for path in path_list:
        print('{} ....start'.format(path))
        for n in range(3):
            arc = OpenSignalsReader(path, multidevice=n)
            print("device= {}".format(n))

            # 心拍変動
            rri_peaks = signals.ecg.ecg(arc.signal('ECG'),
                                        show=False)['rpeaks']
            # 呼吸変動
            resp_peaks = resp_analysis.resp(arc.signal('RESP'),
                                            show=False)['peaks']
            # 皮膚コンダクタンス
            scr_data = eda_analysis.scr(arc.signal('EDA'), downsamp=4)
            # キーワードを設定
            keyword = {'id': i, 'path_name': path}

            df = pd.DataFrame([])
            df = biosignal_features(rri_peaks, resp_peaks, scr_data, emotion,
                                    keyword)

            if i == 0:
                df_summary = pd.DataFrame([], columns=df.columns)
            # ファイルを結合
            df_summary = pd.concat([df_summary, df], ignore_index=True)
            i += 1

    return df_summary
def biosignal_time_summary(path,
                           duration=300,
                           overlap=150,
                           skip_time=None,
                           outpath=None):
    # 生体データを取得
    arc = OpenSignalsReader(path)

    # 時間変数を作成
    time_ = np.arange(duration, arc.t.max(), overlap)
    label_ = time_
    if skip_time is not None:
        time_ = time_ + skip_time
    section_ = zip((time_ - duration), time_)
    emotion = dict(zip(label_.tolist(), section_))

    # HRV Features
    rri_peaks = signals.ecg.ecg(arc.signal('ECG'),
                                sampling_rate=1000.0,
                                show=False)['rpeaks']
    # RESP Features
    resp_peaks = resp_analysis.resp(arc.signal('RESP'),
                                    sampling_rate=1000.0,
                                    show=False)['peaks']
    # EDA Features
    scr_data = eda_analysis.scr(arc.signal('EDA'),
                                sampling_rate=1000.0,
                                downsamp=4)

    df = pd.DataFrame([])
    # 各生体データを時間区間りで算出
    df = biosignal_features(rri_peaks, resp_peaks, scr_data, emotion)
    return df
Beispiel #4
0
def plot_signal(path):
    arc = OpenSignalsReader(path)

    # 心拍データからピークを取り出す
    ecg_result = signals.ecg.ecg(signal=arc.signal(['ECG']),
                                 sampling_rate=1000.0,
                                 show=False)
    # 皮膚コンダクタンス からSCRを取り出す
    #eda_result = eda_analysis.scr(arc.signal(['EDA']))
    eda_result = eda_analysis.eda_preprocess(arc.signal(['EDA']), 1000.)
    # 呼吸周波数を取り出す
    resp_result = resp_analysis.resp(arc.signal('RESP'), show=False)

    # output
    # ECG
    np.savetxt(r"C:\Users\akito\Desktop\2019年度発表スライド\ecg_sample.csv",
               ecg_result["rpeaks"],
               delimiter=",")
    # EDA
    np.savetxt(r"C:\Users\akito\Desktop\2019年度発表スライド\eda_sample.csv",
               eda_result[::25])
    # RESP
    np.savetxt(r"C:\Users\akito\Desktop\2019年度発表スライド\resp_sample.csv",
               np.c_[resp_result['resp_rate_ts'], resp_result['resp_rate']])

    # 描画設定
    fig, axes = plt.subplots(3,
                             1,
                             sharex=True,
                             figsize=(16, 9),
                             subplot_kw=({
                                 "xticks": np.arange(0, 1200, 100)
                             }))
    axes[0].set_title(path)
    # 心拍変動の描画
    axes[0].plot(ecg_result['heart_rate_ts'], ecg_result['heart_rate'], 'b')
    axes[0].set_xlim(0, 1200)
    axes[0].set_ylabel("HR[bpm]")

    # 皮膚コンダクタンスの描画
    axes[1].plot(eda_result['ts'], eda_result['filtered'])
    axes[1].set_ylabel('SCR[us]')

    # 呼吸の描画
    axes[2].plot(resp_result['resp_rate_ts'], resp_result['resp_rate'], 'b')
    axes[2].set_ylabel('RESP[Hz]')
    axes[2].set_ylim(0, 0.5)

    for i in range(3):
        axes[i].axvspan(300, 600, alpha=0.3, color="r", label="Stress")
        axes[i].axvspan(900, 1200, alpha=0.3, color="b", label="Amusement")
    plt.legend()
    plt.tight_layout()
    plt.xlabel("Time[s]")
    return plt
def biosignal_summary(path_list, emotion=None, output_path=None):
    for i, path in enumerate(path_list):
        print(path + ' ....start')

        # ファイル名およぶフォルダ名を取得
        dict_path = os.path.dirname(path)
        fname = os.path.splitext(os.path.basename(path))[0]

        # pathから名前と日付に変換する
        user = dict_path.split("\\")[-1]
        day, time = fname.split("_")[-2:]
        date = datetime.datetime.strptime(day + " " + time,
                                          '%Y-%m-%d %H-%M-%S')

        # キーワードを設定
        keyword = {'id': i, 'path_name': path, 'user': user, 'date': date}

        arc = OpenSignalsReader(path)

        # 心拍変動
        rri_peaks = signals.ecg.ecg(arc.signal('ECG'),
                                    sampling_rate=1000.0,
                                    show=False)['rpeaks']
        # 呼吸変動
        resp_peaks = resp_analysis.resp(arc.signal('RESP'),
                                        sampling_rate=1000.0,
                                        show=False)['peaks']
        # 皮膚コンダクタンス
        scr_data = eda_analysis.scr(arc.signal('EDA'),
                                    sampling_rate=1000.0,
                                    downsamp=4)

        df = pd.DataFrame([])
        df = biosignal_features(rri_peaks, resp_peaks, scr_data, emotion,
                                keyword)

        if i == 0:
            if output_path == None:
                df_summary = pd.DataFrame([], columns=df.columns)
            else:
                df_summary = pd.read_excel(output_path)

        # ファイルを結合
        df_summary = pd.concat([df_summary, df], ignore_index=True)
        df_summary.to_excel(output_path)
    return df_summary
Beispiel #6
0
import eda_analysis
import resp_analysis
import pyhrv.frequency_domain as fd
from opensignalsreader import OpenSignalsReader
import biosignal_plot
path = r"C:\Users\akito\Desktop\test.txt"

arc = OpenSignalsReader(path)

# 心拍データからピークを取り出す
ecg_result = signals.ecg.ecg(signal=arc.signal(['ECG']),
                             sampling_rate=1000.0,
                             show=False)

# 呼吸周波数を取り出す
resp_result = resp_analysis.resp(arc.signal('RESP'), show=False)

# 描画設定
fig, axes = plt.subplots(2, 1, sharex=True, figsize=(16, 9))
axes[0].set_title(path)

# Compute NNI series
nni = tools.nn_intervals(ecg_result['rpeaks'].tolist())
filtered = biosignal_plot.detrend(nni, 500)

# 心拍変動の描画
axes[0].plot(ecg_result['heart_rate_ts'].tolist(), filtered, 'b')
axes[0].set_ylabel("HR[bpm]")

# 呼吸の描画
axes[1].plot(resp_result['ts'], resp_result['filtered'])