예제 #1
0
def test_directed_transfer_function():
    c = Connectivity(fourier_coefficients=np.empty((1, )))
    type(c)._transfer_function = PropertyMock(
        return_value=np.arange(1, 5).reshape((2, 2)))
    dtf = c.directed_transfer_function()
    assert np.allclose(dtf.sum(axis=-1), 1.0)
    assert np.all((dtf >= 0.0) & (dtf <= 1.0))
예제 #2
0
def test_partial_directed_coherence():
    c = Connectivity(fourier_coefficients=np.empty((1, )))
    type(c)._MVAR_Fourier_coefficients = PropertyMock(
        return_value=np.arange(1, 5).reshape((2, 2)))
    pdc = c.partial_directed_coherence()
    assert np.allclose(pdc.sum(axis=-2), 1.0)
    assert np.all((pdc >= 0.0) & (pdc <= 1.0))
예제 #3
0
def test_pairwise_phase_consistency():
    '''Test that incoherent signals are set to zero or below
    and that differences in power are ignored.'''
    np.random.seed(0)
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 200, 5,
                                                                    1, 2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    magnitude1 = np.random.uniform(
        0.5, 3, (n_time_samples, n_trials, n_tapers, n_fft_samples))
    angles1 = np.random.uniform(
        0, 2 * np.pi, (n_time_samples, n_trials, n_tapers, n_fft_samples))
    magnitude2 = np.random.uniform(
        0.5, 3, (n_time_samples, n_trials, n_tapers, n_fft_samples))
    angles2 = np.random.uniform(
        0, 2 * np.pi, (n_time_samples, n_trials, n_tapers, n_fft_samples))

    fourier_coefficients[..., 0] = magnitude1 * np.exp(1j * angles1)
    fourier_coefficients[..., 1] = magnitude2 * np.exp(1j * angles2)

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)
    ppc = this_Conn.pairwise_phase_consistency()

    # set diagonal to zero because its always 1
    diagonal_ind = np.arange(0, n_signals)
    ppc[..., diagonal_ind, diagonal_ind] = 0

    assert np.all(ppc < np.finfo(float).eps)
예제 #4
0
def test_weighted_phase_lag_index_sets_zero_phase_signals_to_zero():
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 30, 1,
                                                                    1, 2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    fourier_coefficients[..., :] = [2 * np.exp(1j * 0), 3 * np.exp(1j * 0)]
    expected_phase_lag_index = np.zeros((2, 2))

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)
    assert np.allclose(this_Conn.weighted_phase_lag_index().squeeze(),
                       expected_phase_lag_index)
예제 #5
0
def test_weighted_phase_lag_index_is_same_as_phase_lag_index():
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 30, 1,
                                                                    1, 2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    fourier_coefficients[..., :] = [
        1 * np.exp(1j * 3 * np.pi / 4), 1 * np.exp(1j * 5 * np.pi / 4)
    ]

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)
    assert np.allclose(this_Conn.phase_lag_index(),
                       this_Conn.weighted_phase_lag_index())
예제 #6
0
def test_imaginary_coherence():
    '''Test that imaginary coherence sets signals with the same phase
    to zero.'''
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 30, 1,
                                                                    1, 2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    fourier_coefficients[..., :] = [2 * np.exp(1j * 0), 3 * np.exp(1j * 0)]
    expected_imaginary_coherence = np.zeros((2, 2))

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)
    assert np.allclose(this_Conn.imaginary_coherence().squeeze(),
                       expected_imaginary_coherence)
예제 #7
0
def test_phase_locking_value():
    '''Make sure phase locking value ignores magnitudes.'''
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 30, 1,
                                                                    1, 2)
    fourier_coefficients = (np.random.uniform(
        0, 2, (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals)) *
                            np.exp(1j * np.pi / 2))
    expected_phase_locking_value_magnitude = np.ones(
        fourier_coefficients.shape)
    expected_phase_locking_value_angle = np.zeros(fourier_coefficients.shape)
    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)

    assert np.allclose(np.abs(this_Conn.phase_locking_value()),
                       expected_phase_locking_value_magnitude)
    assert np.allclose(np.angle(this_Conn.phase_locking_value()),
                       expected_phase_locking_value_angle)
예제 #8
0
def test_cross_spectrum(axis):
    '''Test that the cross spectrum is correct for each dimension.'''
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (2, 2, 2, 2,
                                                                    2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    signal_fourier_coefficient = [
        2 * np.exp(1j * np.pi / 2), 3 * np.exp(1j * -np.pi / 2)
    ]
    fourier_ind = [slice(0, 4)] * 5
    fourier_ind[-1] = slice(None)
    fourier_ind[axis] = slice(1, 2)
    fourier_coefficients[fourier_ind] = signal_fourier_coefficient

    expected_cross_spectral_matrix = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals,
         n_signals),
        dtype=np.complex)

    expected_slice = np.array([[4, -6], [-6, 9]], dtype=np.complex)
    expected_ind = [slice(0, 5)] * 6
    expected_ind[-1] = slice(None)
    expected_ind[-2] = slice(None)
    expected_ind[axis] = slice(1, 2)
    expected_cross_spectral_matrix[expected_ind] = expected_slice

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)
    assert np.allclose(expected_cross_spectral_matrix,
                       this_Conn._cross_spectral_matrix)
예제 #9
0
def test_power():
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 1, 1, 1,
                                                                    2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    fourier_coefficients[..., :] = [
        2 * np.exp(1j * np.pi / 2), 3 * np.exp(1j * -np.pi / 2)
    ]

    expected_power = np.zeros((n_time_samples, n_fft_samples, n_signals))

    expected_power[..., :] = [4, 9]

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)
    assert np.allclose(expected_power, this_Conn.power())
예제 #10
0
def test_n_observations(expectation_type, expected_n_observations):
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 2, 3, 4,
                                                                    5)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    this_Conn = Connectivity(
        fourier_coefficients=fourier_coefficients,
        expectation_type=expectation_type,
    )
    assert this_Conn.n_observations == expected_n_observations
예제 #11
0
def test_debiased_squared_phase_lag_index():
    '''Test that incoherent signals are set to zero or below.'''
    np.random.seed(0)
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 200, 5,
                                                                    1, 2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    angles1 = np.random.uniform(
        0, 2 * np.pi, (n_time_samples, n_trials, n_tapers, n_fft_samples))
    angles2 = np.random.uniform(
        0, 2 * np.pi, (n_time_samples, n_trials, n_tapers, n_fft_samples))

    fourier_coefficients[..., 0] = np.exp(1j * angles1)
    fourier_coefficients[..., 1] = np.exp(1j * angles2)

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)

    assert np.all(
        this_Conn.debiased_squared_phase_lag_index() < np.finfo(float).eps)
예제 #12
0
def test_phase_lag_index_sets_angles_up_to_pi_to_same_value():
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 30, 1,
                                                                    1, 2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)
    fourier_coefficients[..., 0] = (np.random.uniform(
        0.1, 2, (n_time_samples, n_trials, n_tapers, n_fft_samples)) *
                                    np.exp(1j * np.pi / 2))
    fourier_coefficients[..., 1] = (np.random.uniform(
        0.1, 2, (n_time_samples, n_trials, n_tapers, n_fft_samples)) *
                                    np.exp(1j * np.pi / 4))

    expected_phase_lag_index = np.zeros((2, 2))
    expected_phase_lag_index[0, 1] = 1
    expected_phase_lag_index[1, 0] = -1

    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)

    assert np.allclose(this_Conn.phase_lag_index().squeeze(),
                       expected_phase_lag_index)
예제 #13
0
def test_coherency():
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 30, 1,
                                                                    1, 2)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    fourier_coefficients[..., :] = [
        2 * np.exp(1j * np.pi / 2), 3 * np.exp(1j * -np.pi / 2)
    ]
    this_Conn = Connectivity(fourier_coefficients=fourier_coefficients)
    expected_coherence_magnitude = np.array([[np.nan, 1], [1, np.nan]])
    expected_phase = np.zeros((2, 2)) * np.nan
    expected_phase[0, 1] = np.pi
    expected_phase[1, 0] = -np.pi

    assert np.allclose(np.abs(this_Conn.coherency().squeeze()),
                       expected_coherence_magnitude,
                       equal_nan=True)
    assert np.allclose(np.angle(this_Conn.coherency().squeeze()),
                       expected_phase,
                       equal_nan=True)
예제 #14
0
def test_expectation(expectation_type, expected_shape):
    n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals = (1, 2, 3, 4,
                                                                    5)
    fourier_coefficients = np.zeros(
        (n_time_samples, n_trials, n_tapers, n_fft_samples, n_signals),
        dtype=np.complex)

    this_Conn = Connectivity(
        fourier_coefficients=fourier_coefficients,
        expectation_type=expectation_type,
    )
    expectation_function = this_Conn._expectation
    assert np.allclose(expected_shape,
                       expectation_function(fourier_coefficients).shape)
time_extent = (0, 1205.02733)
n_time_samples = ((time_extent[1] - time_extent[0]) * sampling_frequency) + 1
time = np.linspace(time_extent[0],
                   time_extent[1],
                   num=n_time_samples,
                   endpoint=True)
signals = np.array(eeg['electric_potential'])[:len(time)]

m = Multitaper(signals,
               sampling_frequency=sampling_frequency,
               time_halfbandwidth_product=8,
               time_window_duration=0.6,
               time_window_step=0.500,
               start_time=time[0])
c = Connectivity(fourier_coefficients=m.fft(),
                 frequencies=m.frequencies,
                 time=m.time)

mesh = plt.pcolormesh(c.time,
                      c.frequencies,
                      c.power().squeeze().T,
                      vmin=0.0,
                      vmax=0.03,
                      cmap='viridis')
#axes[1, 0].set_ylim((0, 300))
plt.axvline(time[int(np.fix(n_time_samples / 2))], color='black')

# plt.tight_layout()
cb = plt.colorbar(mesh,
                  orientation='horizontal',
                  shrink=.5,