Ejemplo n.º 1
0
 def _poincare(self, nn, peaks):
     res = poincare(nni=nn, rpeaks=peaks, show=False)
     nn_mean = np.mean(nn)
     sd1, sd2 = res['sd1'], res['sd2']
     ellipse = mpl.patches.Ellipse((nn_mean, nn_mean),
                                   sd1 * 2,
                                   sd2 * 2,
                                   angle=-45,
                                   fc='k',
                                   zorder=1)
     ellipse = ellipse.get_verts()
     ellipse = {'x': ellipse[:, 0], 'y': ellipse[:, 1]}
     obj = {'sd1': res['sd1'], 'sd2': res['sd2'], 'ellipse': ellipse}
     return obj
Ejemplo n.º 2
0
    def extractNonLinearDomain(self, x):
        try:
            nni = self.extractRR(x)
            sampEntro = nn.sample_entropy(nni=nni, dim=2) #change dim from 1 to 2
            lyapEx = self.lyapunov_exponent(nni=nni, emb_dim=3, matrix_dim=2)
            pointCare = nn.poincare(nni=nni, show=False)
            # hrust = nolds.hurst_rs(nni)
            corrDim = nolds.corr_dim(nni, emb_dim=3)
            # csi = get_csi_cvi_features(nni) #caused worse results
            # geo = get_geometrical_features(nni) #caused worse results
            # dfa = nolds.dfa(nni)
            # return np.array([sampEntro["sampen"], lyapEx["lyapex"]])
            return np.array([sampEntro["sampen"], lyapEx["lyapex"], pointCare["sd1"], pointCare["sd2"], pointCare["sd_ratio"], pointCare["ellipse_area"],  corrDim])

        except:
            return np.array([])
Ejemplo n.º 3
0
def cal_hrv(data_list):

    if (np.nan in data_list) or (len(data_list) <= 1):
        sdsd = np.nan
        rmssd = np.nan
        sd1 = np.nan
        sd2 = np.nan
        sd_ratio = np.nan
        a1 = np.nan
        a2 = np.nan
        a_ratio = np.nan
        sampen = np.nan

    else:
        # sdsd
        sdsd = round(td.sdsd(nni=data_list)['sdsd'], 5)
        # rmssd
        rmssd = round(td.rmssd(nni=data_list)['rmssd'], 5)
        # sd1, sd2
        nl_results = nl.poincare(nni=data_list)
        sd1 = round(nl_results['sd1'], 5)
        sd2 = round(nl_results['sd2'], 5)
        sd_ratio = round(sd2 / sd1, 5)
        # dfa a1, a2
        dfa_results = nl.dfa(data_list)
        try:
            a1 = round(dfa_results['dfa_alpha1'], 5)
            a2 = round(dfa_results['dfa_alpha2'], 5)
            a_ratio = round(a2 / a1, 5)
        except:
            a1 = np.nan
            print(a1, type(a1))
            a2 = np.nan
            print(a2, type(a2))
            a_ratio = np.nan
        # Sampen
        t = np.std(data_list)
        sampen = round(
            nl.sample_entropy(nni=data_list, tolerance=t)['sampen'], 6)

    return sdsd, rmssd, sd1, sd2, sd_ratio, a1, a2, a_ratio, sampen
Ejemplo n.º 4
0
def resp_features(resp_peaks):
    bvp = tools.nn_intervals(resp_peaks.tolist())
    resp_features = {}

    #------時系列解析------#
    L = len(resp_peaks)
    resp_features['bvp_mean'] = np.mean(bvp)
    resp_features['bvp_max'] = np.max(bvp)
    resp_features['bvp_min'] = np.min(bvp)
    resp_features['bvp_sdnn'] = np.std(bvp)
    resp_features['bvp_sdsd'] = np.std(np.diff(bvp))
    resp_features['bvp_rmssd'] = np.sqrt((1 / L) * sum(np.diff(bvp)**2))
    resp_features['bvp_median'] = np.median(bvp)

    #-----ポアンカレプロット-----#
    _, resp_features['bvp_sd1'], resp_features['bvp_sd2'], resp_features[
        'bvp_sd_ratio'], resp_features['bvp_ellipse_area'] = nl.poincare(
            rpeaks=resp_peaks.astype(int).tolist(), show=False)

    #------MultiScaleEntropy-----#
    # 後で追加すること

    return resp_features
Ejemplo n.º 5
0
def Poincare_Windowing(rr_signal, plotter=False):
    """
    rr_signal :: RR vector of time in seconds
    plotter   :: Boolean to plot the Poincare output or not.
    """

    poin_r = list()

    for idx in range(0, len(rr_signal) - RR_WLEN, RR_STEP):
        window_slice = slice(idx, idx + RR_WLEN)
        rr_window = rr_signal[window_slice]
        if plotter == True:
            poincare_results = nl.poincare(rr_window,
                                           show=True,
                                           figsize=None,
                                           ellipse=True,
                                           vectors=True,
                                           legend=True)
            poin_r.append(poincare_results["sd_ratio"])
        elif plotter == False:
            poincare_results = poincarePlot(rr_window, show=False)
            poin_r.append(poincare_results["sd_ratio"])

    return poin_r
Ejemplo n.º 6
0
    def _prepare_figures(self):
        """Re-creates plot figures based on the results for the PDF report"""
        plots = {}

        # Tachogram
        try:
            fig = tools.tachogram(nni=self.nni,
                                  show=False,
                                  figsize=self.figsizes,
                                  interval='complete')['tachogram_plot']
            plots['tachogram'] = fig
            self._set_section('tachogram')
        except Exception as e:
            self._set_section('tachogram', False)
            warnings.warn(
                "\nAn error occurred while trying to create the Tachogram figure for "
                "the PDF report: \n%s'" % str(e),
                stacklevel=2)

        # ECG signal plot
        try:
            if self.signal is not None:
                plots['ecg_plot'] = \
                tools.plot_ecg(signal=self.signal, show=False, interval='complete', figsize=self.figsizes)['ecg_plot']
                self._set_section('ecg_plot')
        except Exception as e:
            self._set_section('ecg_plot', False)
            warnings.warn(
                "\nAn error occurred while trying to create the ECG plot figure for "
                "the PDF report: \n%s'" % str(e),
                stacklevel=2)

        # Histogram
        try:
            plots['histogram'] = td.triangular_index(
                nni=self.nni, show=False, legend=False,
                figsize=self.figsizes)['tri_histogram']
            self._set_section('histogram')
        except Exception as e:
            self._set_section('histogram', False)
            warnings.warn(
                "\nAn error occurred while trying to create the NNI histogram figure for "
                "the PDF report: \n%s'" % str(e),
                stacklevel=2)

        # HR Heat Plot
        if isinstance(self._general_info['age'], int) and isinstance(
                self._general_info['gender'], str):
            try:
                plots['hr_heatplot'] = \
                pyhrv.utils.heart_rate_heatplot(nni=self.nni, show=False, age=self._general_info['age'],
                        gender=self._general_info['gender'], figsize=self.figsizes)['hr_heatplot']
                self._set_section('hrheatplot')
            except Exception as e:
                self._set_section('hrheatplot', False)
                warnings.warn(
                    "\nAn error occurred while trying to create the HR heatplotfor "
                    "the PDF report: \n%s'" % str(e),
                    stacklevel=2)

        # Welch's Plot
        if 'fft_plot' in self.results.keys():
            try:
                plots['fft_plot'] = \
                fd.welch_psd(nni=self.nni, fbands=self.results['fft_bands'], window=self.results['fft_window'],
                    show=False, show_param=False, figsize=self.figsizes)['fft_plot']
                self._set_section('fft_plot')
            except Exception as e:
                self._set_section('fft_plot', False)
                warnings.warn(
                    "\nAn error occurred while trying to create the FFT/Welch figure for "
                    "the PDF report: \n%s'" % str(e),
                    stacklevel=2)

        # Welch's Plot
        if 'ar_plot' in self.results.keys():
            try:
                plots['ar_plot'] = \
                fd.ar_psd(nni=self.nni, fbands=self.results['ar_bands'], show=False, show_param=False,
                    figsize=self.figsizes)['ar_plot']
                self._set_section('ar_plot')
            except Exception as e:
                self._set_section('ar_plot', False)
                warnings.warn(
                    "\nAn error occurred while trying to create the AR PSD figure for "
                    "the PDF report: \n%s'" % str(e),
                    stacklevel=2)

        # Welch's Plot
        if 'lomb_plot' in self.results.keys():
            try:
                plots['lomb_plot'] = \
                fd.lomb_psd(nni=self.nni, fbands=self.results['lomb_bands'], ma_size=self.results['lomb_ma'],
                   show=False, show_param=False, figsize=self.figsizes)['lomb_plot']
                self._set_section('lomb_plot')
            except Exception as e:
                self._set_section('lomb_plot', False)
                warnings.warn(
                    "\nAn error occurred while trying to create the AR PSD figure for "
                    "the PDF report: \n%s'" % str(e),
                    stacklevel=2)

        # Poincare
        if 'poincare_plot' in self.results.keys():
            try:
                plots['poincare_plot'] = nl.poincare(
                    nni=self.nni, show=False, legend=False)['poincare_plot']
                self._set_section('poincare_plot')
            except Exception as e:
                self._set_section('poincare_plot', False)
                warnings.warn(
                    "\nAn error occurred while trying to create the Poincare plot figure for "
                    "the PDF report: \n%s'" % str(e),
                    stacklevel=2)

        # DFA
        if 'dfa_plot' in self.results.keys():
            try:
                fig = nl.dfa(nn=self.nni, show=False, legend=False)['dfa_plot']
                plots['dfa_plot'] = fig
                self._set_section('dfa')
            except Exception as e:
                self._set_section('dfa', False)
                warnings.warn(
                    "\nAn error occurred while trying to create the DFA plot figure for "
                    "the PDF report: %s'" % str(e),
                    stacklevel=2)

        # Save all plot figures
        for f in plots.keys():
            plots[f].savefig("%s%s.png" %
                             (self._figure_path, f.replace('_', '')),
                             dpi=300,
                             bbox_inches='tight')
Ejemplo n.º 7
0
def poincare_plot(nni, title=''):
    nl.poincare(nni, title=title)