Beispiel #1
0
def estimate_spindle_band_power(lfps,
                                sampling_frequency,
                                spindle_band=(10, 16),
                                start_time=0.00,
                                multitaper_params=_DEFAULT_MULTITAPER_PARAMS):
    """Estimates the spindle power of each LFP.

    Parameters
    ----------
    lfps : ndarray, shape (n_time, n_signals)
    sampling_frequency : float
    spindle_band : (start_freq, end_freq)
    start_time : float
    multitaper_params : dict

    Returns
    -------
    time : ndarray, shape (n_time_windows,)
    spindle_band_power : ndarray (n_time_windows, n_signals)

    """
    m = Multitaper(lfps,
                   sampling_frequency=sampling_frequency,
                   **multitaper_params)
    c = Connectivity.from_multitaper(m)
    freq_ind = ((c.frequencies > spindle_band[0]) &
                (c.frequencies < spindle_band[1]))
    power = c.power()[..., freq_ind, :]

    return c.time, power
Beispiel #2
0
def get_replay_triggered_power(lfps,
                               replay_info,
                               tetrode_info,
                               multitaper_params,
                               window_offset=(-0.250, 0.250),
                               sampling_frequency=1500):
    ripple_locked_lfps = reshape_to_segments(
        lfps,
        replay_info.loc[:, ['start_time', 'end_time']],
        window_offset=window_offset,
        sampling_frequency=sampling_frequency)
    ripple_locked_lfps = (ripple_locked_lfps.to_xarray().to_array().rename({
        'variable':
        'tetrodes'
    }).transpose('time', 'replay_id', 'tetrodes').dropna('replay_id'))
    ripple_locked_lfps = (ripple_locked_lfps -
                          ripple_locked_lfps.mean(['replay_id']))
    start_time = ripple_locked_lfps.time.min().values / np.timedelta64(1, 's')
    m = Multitaper(ripple_locked_lfps.values,
                   **multitaper_params,
                   start_time=start_time)
    c = Connectivity.from_multitaper(m)
    dimension_names = ['time', 'frequency', 'tetrode']
    data_vars = {'power': (dimension_names, c.power())}
    coordinates = {
        'time': _center_time(c.time),
        'frequency': c.frequencies + np.diff(c.frequencies)[0] / 2,
        'tetrode': lfps.columns,
    }
    return (xr.Dataset(data_vars,
                       coords=coordinates).sel(frequency=slice(0, 300)))
Beispiel #3
0
def estimate_theta_power(time, tetrode_info, multitaper_params=None):
    if multitaper_params is None:
        multitaper_params = MULTITAPER_PARAMETERS['4Hz']

    is_brain_areas = (
        tetrode_info.area.astype(str).str.upper().isin(BRAIN_AREAS))
    tetrode_keys = tetrode_info.loc[is_brain_areas].index

    lfps = get_LFPs(tetrode_keys, ANIMALS)

    m = Multitaper(lfps.values,
                   **multitaper_params,
                   start_time=lfps.index[0].total_seconds())
    c = Connectivity.from_multitaper(m)
    coordinates = {
        'time': pd.to_timedelta(c.time, unit='s'),
        'frequency': c.frequencies,
        'tetrode': lfps.columns,
    }

    dimension_names = ['time', 'frequency', 'tetrode']
    data_vars = {'power': (dimension_names, c.power())}
    theta_power = (xr.Dataset(data_vars, coords=coordinates).sel(
        frequency=slice(4, 12)).mean('frequency').dropna('tetrode').interp(
            time=time).to_dataframe().unstack(level=0))

    theta_power_change = theta_power.transform(lambda df: df / df.mean())
    theta_power_zscore = theta_power.transform(lambda df:
                                               (df - df.mean()) / df.std())

    return dict(
        theta_power=theta_power,
        theta_power_change=theta_power_change,
        theta_power_zscore=theta_power_zscore,
    )
Beispiel #4
0
def estimate_ripple_band_power(lfps, sampling_frequency):
    """Estimates the 200 Hz power of each LFP.

    Parameters
    ----------
    lfps : ndarray, shape (n_time, n_signals)
    sampling_frequency : float

    Returns
    -------
    ripple_band_power : ndarray (n_time, n_signals)

    """
    n_time = lfps.shape[0]
    m = Multitaper(lfps,
                   sampling_frequency=sampling_frequency,
                   time_halfbandwidth_product=1,
                   time_window_duration=0.020,
                   time_window_step=1 / sampling_frequency)
    c = Connectivity.from_multitaper(m)
    closest_200Hz_freq_ind = np.argmin(np.abs(c.frequencies - 200))
    power = c.power()[..., closest_200Hz_freq_ind, :].squeeze()
    n_power_time = power.shape[0]
    unobserved = np.full((n_time - n_power_time, *power.shape[1:]), np.nan)
    return np.concatenate((power, unobserved))
Beispiel #5
0
def estimate_ripple_band_power(lfps, sampling_frequency):
    m = Multitaper(lfps.values, sampling_frequency=sampling_frequency,
                   time_halfbandwidth_product=1,
                   time_window_duration=0.020,
                   time_window_step=0.020,
                   start_time=lfps.index[0].total_seconds())
    c = Connectivity.from_multitaper(m)
    closest_200Hz_freq_ind = np.argmin(np.abs(c.frequencies - 200))
    power = c.power()[..., closest_200Hz_freq_ind, :].squeeze() + np.spacing(1)
    n_samples = int(0.020 * sampling_frequency)
    index = lfps.index[np.arange(1, power.shape[0] * n_samples + 1, n_samples)]
    power = pd.DataFrame(power, index=index)
    return power.reindex(lfps.index)
Beispiel #6
0
    # Granger causality from v1 to v4
    granger_v1_v4 = np.empty((1, 203, 0))
    for idx_v1 in range(len(stc_label_v1[1].vertices)):
        granger_v4 = np.empty((1, 203, 0))
        for idx_v4 in range(len(stc_label_v4[1].vertices)):
            signal = np.append(signal_v1[:, :, idx_v1, np.newaxis],
                               signal_v4[:, :, idx_v4, np.newaxis],
                               axis=2)
            # Compute granger causality
            m = Multitaper(signal,
                           sfreq,
                           time_halfbandwidth_product=2,
                           start_time=-0.8,
                           n_tapers=1)
            c = Connectivity(fourier_coefficients=m.fft(),
                             frequencies=m.frequencies)
            granger = c.pairwise_spectral_granger_prediction()
            granger_v4 = np.append(granger_v4,
                                   granger[..., 0, 1, np.newaxis],
                                   axis=2)
        granger_v1_v4 = np.append(granger_v1_v4,
                                  granger_v4.mean(2)[..., np.newaxis],
                                  axis=2)
    granger_all_v1_v4 = granger_v1_v4.mean(2)

    # Granger causality from v4 to v1
    granger_v1_v4 = np.empty((1, 203, 0))
    for idx_v1 in range(len(stc_label_v1[1].vertices)):
        granger_v4 = np.empty((1, 203, 0))
        for idx_v4 in range(len(stc_label_v4[1].vertices)):
            signal = np.append(signal_v4[:, :, idx_v4, np.newaxis],
    # Create signals input
    datarray = np.asarray(tcs_v1)
    signal_v1 = np.transpose(datarray.mean(2),
                             (2, 1, 0))  #(times,epochs,signals))
    datarray = np.asarray(tcs_v4)
    signal_v4 = np.transpose(datarray.mean(2), (2, 1, 0))

    signal = np.append(signal_v1, signal_v4, axis=2)

    # Compute granger causality
    m = Multitaper(signal,
                   sfreq,
                   time_halfbandwidth_product=2,
                   start_time=-0.8,
                   n_tapers=1)
    c = Connectivity(fourier_coefficients=m.fft()[:, :, :, 0:130, :],
                     frequencies=m.frequencies[0:130])

    cross_spectral_matrix = c._expectation(c._cross_spectral_matrix)
    n_signals = cross_spectral_matrix.shape[-1]
    total_power = c.power()
    n_frequencies = cross_spectral_matrix.shape[-3]
    non_neg_index = np.arange(0, (n_frequencies + 1) // 2)
    new_shape = list(cross_spectral_matrix.shape)
    new_shape[-3] = non_neg_index.size

    predictive_power = np.empty(new_shape)
    nvert_v1 = len(stc_label_v1[1].vertices)
    nvert_v4 = len(stc_label_v4[1].vertices)

    n_sign_v1 = np.arange(nvert_v1)
    n_sign_v4 = np.arange(nvert_v1, nvert_v1 + nvert_v4)
                                          signal_v1[:,0:idx_slow,idx_v1,np.newaxis], axis=2)
            signal_mt_v1_med = np.append(signal_mt[:,idx_slow:idx_slow+idx_med,idx_mt,np.newaxis], 
                                         signal_v1[:,idx_slow:idx_slow+idx_med,idx_v1,np.newaxis], axis=2)
            signal_mt_v1_fast = np.append(signal_mt[:,idx_slow+idx_med:idx_all,idx_mt,np.newaxis], 
                                          signal_v1[:,idx_slow+idx_med:idx_all,idx_v1,np.newaxis], axis=2)
            
            # Compute granger causality
            m_v1_mt_slow = Multitaper(signal_v1_mt_slow, sfreq, time_halfbandwidth_product=2, start_time=-0.8, n_tapers=1)
            m_v1_mt_med = Multitaper(signal_v1_mt_med, sfreq, time_halfbandwidth_product=2, start_time=-0.8, n_tapers=1)
            m_v1_mt_fast = Multitaper(signal_v1_mt_fast, sfreq, time_halfbandwidth_product=2, start_time=-0.8, n_tapers=1)
            
            m_mt_v1_slow = Multitaper(signal_mt_v1_slow, sfreq, time_halfbandwidth_product=2, start_time=-0.8, n_tapers=1)
            m_mt_v1_med = Multitaper(signal_mt_v1_med, sfreq, time_halfbandwidth_product=2, start_time=-0.8, n_tapers=1)
            m_mt_v1_fast = Multitaper(signal_mt_v1_fast, sfreq, time_halfbandwidth_product=2, start_time=-0.8, n_tapers=1)
            
            c_v1_mt_slow = Connectivity(fourier_coefficients=m_v1_mt_slow.fft(), frequencies=m_v1_mt_slow.frequencies)
            c_v1_mt_med = Connectivity(fourier_coefficients=m_v1_mt_med.fft(), frequencies=m_v1_mt_med.frequencies)
            c_v1_mt_fast = Connectivity(fourier_coefficients=m_v1_mt_fast.fft(), frequencies=m_v1_mt_fast.frequencies)
            
            c_mt_v1_slow = Connectivity(fourier_coefficients=m_mt_v1_slow.fft(), frequencies=m_mt_v1_slow.frequencies)
            c_mt_v1_med = Connectivity(fourier_coefficients=m_mt_v1_med.fft(), frequencies=m_mt_v1_med.frequencies)
            c_mt_v1_fast = Connectivity(fourier_coefficients=m_mt_v1_fast.fft(), frequencies=m_mt_v1_fast.frequencies)
            
            granger_v1_mt_slow[:,:,idx_mt] = c_v1_mt_slow.pairwise_spectral_granger_prediction()[...,0,1]
            granger_v1_mt_med[:,:,idx_mt] = c_v1_mt_med.pairwise_spectral_granger_prediction()[...,0,1]
            granger_v1_mt_fast[:,:,idx_mt] = c_v1_mt_fast.pairwise_spectral_granger_prediction()[...,0,1]

            granger_mt_v1_slow[:,:,idx_mt] = c_mt_v1_slow.pairwise_spectral_granger_prediction()[...,0,1]
            granger_mt_v1_med[:,:,idx_mt] = c_mt_v1_med.pairwise_spectral_granger_prediction()[...,0,1]
            granger_mt_v1_fast[:,:,idx_mt] = c_mt_v1_fast.pairwise_spectral_granger_prediction()[...,0,1]