Пример #1
0
def test_peak_finder():
    """Test the peak detection method."""
    # check for random data
    rng = np.random.RandomState(42)
    peak_inds, peak_mags = peak_finder(rng.randn(20))

    assert_equal(peak_inds.dtype, np.dtype('int64'))
    assert_equal(peak_mags.dtype, np.dtype('float64'))

    # check for empty array as created in the #5025
    with pytest.raises(ValueError):
        peak_finder(np.arange(2, 1, 0.05))

    # check for empty array
    with pytest.raises(ValueError):
        peak_finder([])

    # check for monotonic function
    peak_inds, peak_mags = peak_finder(np.arange(1, 2, 0.05))

    assert_equal(peak_inds.dtype, np.dtype('int64'))
    assert_equal(peak_mags.dtype, np.dtype('float64'))

    # check for no peaks
    peak_inds, peak_mags = peak_finder(np.zeros(20))

    assert_equal(peak_inds.dtype, np.dtype('int64'))
    assert_equal(peak_mags.dtype, np.dtype('float64'))

    # check values
    peak_inds, peak_mags = peak_finder([0, 2, 5, 0, 6, -1])
    assert_array_equal(peak_inds, [2, 4])
Пример #2
0
def select_rpeaks(signals):
    r_peak = []
    for k in range(12):
        lead = signals[:,k]
        peak,_ = peak_finder(lead,verbose=0)
        r_peak.extend(peak)
    
    R_peak_arr = np.zeros(len(signals))
    R_peak_arr[r_peak]=100
    return R_peak_arr
Пример #3
0
def phase_locked_amplitude(inst,
                           freqs_phase,
                           freqs_amp,
                           ix_ph,
                           ix_amp,
                           tmin=-.5,
                           tmax=.5,
                           mask_times=None):
    """Calculate the average amplitude of a signal at a phase of another.

    Parameters
    ----------
    inst : instance of mne.Epochs | mne.io.Raw
        The data to be used in phase locking computation.
    freqs_phase : np.array
        The frequencies to use in phase calculation. The phase of each
        frequency will be averaged together.
    freqs_amp : np.array
        The frequencies to use in amplitude calculation.
    ix_ph : int
        The index of the signal to be used for phase calculation.
    ix_amp : int
        The index of the signal to be used for amplitude calculation.
    tmin : float
        The time to include before each phase peak.
    tmax : float
        The time to include after each phase peak.
    mask_times : np.array, dtype bool, shape (inst.n_times,)
        If `inst` is an instance of Raw, this will only include times contained
        in `mask_times`. Defaults to using all times.

    Returns
    -------
    data_am : np.array
        The mean amplitude values for the frequencies specified in `freqs_amp`,
        time-locked to peaks of the low-frequency phase.
    data_ph : np.array
        The mean low-frequency signal, phase-locked to low-frequency phase
        peaks.
    times : np.array
        The times before / after each phase peak.
    """
    sfreq = inst.info['sfreq']
    # Pull the amplitudes/phases using Morlet
    data_ph, data_am = _pull_data(inst, ix_ph, ix_amp)
    angle_ph, band_ph, amp = _extract_phase_and_amp(data_ph, data_am, sfreq,
                                                    freqs_phase, freqs_amp)

    angle_ph = angle_ph.mean(0)  # Mean across freq bands
    band_ph = band_ph.mean(0)

    # Find peaks in the phase for time-locking
    phase_peaks, vals = peak_finder(angle_ph)
    ixmin, ixmax = [t * sfreq for t in [tmin, tmax]]
    # Remove peaks w/o buffer
    phase_peaks = phase_peaks[(phase_peaks > np.abs(ixmin)) *
                              (phase_peaks < len(angle_ph) - ixmax)]

    if mask_times is not None:
        # Set datapoints outside out times to nan so we can drop later
        if len(mask_times) != angle_ph.shape[-1]:
            raise ValueError('mask_times must be == in length to data')
        band_ph[..., ~mask_times] = np.nan

    data_ph, times, msk_window = _raw_to_epochs_array(band_ph[np.newaxis, :],
                                                      sfreq, phase_peaks, tmin,
                                                      tmax)
    data_am, times, msk_window = _raw_to_epochs_array(amp, sfreq, phase_peaks,
                                                      tmin, tmax)
    data_ph = data_ph.squeeze()
    data_am = data_am.squeeze()

    # Drop any peak events where there was a nan
    keep_rows = np.where(~np.isnan(data_ph).any(-1))[0]
    data_ph = data_ph[keep_rows, ...]
    data_am = data_am[keep_rows, ...]

    # Average across phase peak events
    data_am = data_am.mean(0)
    data_ph = data_ph.mean(0)
    return data_am, data_ph, times