Esempio n. 1
0
def test_sym_mat_to_vector():
    """Test converting between vectors and symmetric matrices."""
    mat = np.array([[0, 1, 2, 3],
                    [1, 4, 5, 6],
                    [2, 5, 7, 8],
                    [3, 6, 8, 9]])
    assert_array_equal(_sym_mat_to_vector(mat),
                       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    vec = np.arange(10)
    assert_array_equal(_vector_to_sym_mat(vec),
                       [[0, 1, 2, 3],
                        [1, 4, 5, 6],
                        [2, 5, 7, 8],
                        [3, 6, 8, 9]])

    # Test complex values: diagonals should be complex conjugates
    comp_vec = np.arange(3) + 1j
    assert_array_equal(_vector_to_sym_mat(comp_vec),
                       [[0. + 0.j, 1. + 1.j],
                        [1. - 1.j, 2. + 0.j]])

    # Test preservation of data type
    assert _sym_mat_to_vector(mat.astype(np.int8)).dtype == np.int8
    assert _vector_to_sym_mat(vec.astype(np.int8)).dtype == np.int8
    assert _sym_mat_to_vector(mat.astype(np.float16)).dtype == np.float16
    assert _vector_to_sym_mat(vec.astype(np.float16)).dtype == np.float16
Esempio n. 2
0
def test_sym_mat_to_vector():
    """Test converting between vectors and symmetric matrices."""
    mat = np.array([[0, 1, 2, 3],
                    [1, 4, 5, 6],
                    [2, 5, 7, 8],
                    [3, 6, 8, 9]])
    assert_array_equal(_sym_mat_to_vector(mat),
                       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    vec = np.arange(10)
    assert_array_equal(_vector_to_sym_mat(vec),
                       [[0, 1, 2, 3],
                        [1, 4, 5, 6],
                        [2, 5, 7, 8],
                        [3, 6, 8, 9]])

    # Test complex values: diagonals should be complex conjugates
    comp_vec = np.arange(3) + 1j
    assert_array_equal(_vector_to_sym_mat(comp_vec),
                       [[0. + 0.j,  1. + 1.j],
                        [1. - 1.j,  2. + 0.j]])

    # Test preservation of data type
    assert _sym_mat_to_vector(mat.astype(np.int8)).dtype == np.int8
    assert _vector_to_sym_mat(vec.astype(np.int8)).dtype == np.int8
    assert _sym_mat_to_vector(mat.astype(np.float16)).dtype == np.float16
    assert _vector_to_sym_mat(vec.astype(np.float16)).dtype == np.float16
Esempio n. 3
0
 def __getitem__(self, sel):  # noqa: D105
     """Subselect frequencies.
     Parameters
     ----------
     sel : ndarray
         Array of frequency indices to subselect.
     Returns
     -------
     csd : instance of CrossSpectralDensity
         A new CSD instance with the subset of frequencies.
     """
     return ResultConnectivity(
         data=_vector_to_sym_mat(self._data[..., sel]),
         info=self.info,
         metadata=self.metadata,
         fmin=self.fmin,
         fmax=self.fmax,
     )
Esempio n. 4
0
def pick_channels_connectivity(csd,
                               include=[],
                               exclude=[],
                               ordered=False,
                               copy=True):
    """Pick channels from connectivity matrix.
    Parameters
    ----------
    csd : instance of ResultConnectivity
        The Result object to select the channels from.
    include : list of str
        List of channels to include (if empty, include all available).
    exclude : list of str
        Channels to exclude (if empty, do not exclude any).
    ordered : bool
        If True (default False), ensure that the order of the channels in the
        modified instance matches the order of ``include``.
        .. versionadded:: 0.20.0
    copy : bool
        If True (the default), return a copy of the CSD matrix with the
        modified channels. If False, channels are modified in-place.
        .. versionadded:: 0.20.0
    Returns
    -------
    res : instance of CrossSpectralDensity
        Cross-spectral density restricted to selected channels.
    """
    if copy:
        csd = csd.copy()

    sel = pick_channels(csd.ch_names,
                        include=include,
                        exclude=exclude,
                        ordered=ordered)
    data = []
    for vec in csd._data.T:
        mat = _vector_to_sym_mat(vec)
        mat = mat[sel, :][:, sel]
        data.append(_sym_mat_to_vector(mat))
    ch_names = [csd.ch_names[i] for i in sel]

    csd._data = np.array(data).T
    csd.ch_names = ch_names
    return csd
Esempio n. 5
0
 def get_data(self, start=0, stop=None):
     """Get the CSD matrix for a given frequency as NumPy array.
     If there is only one matrix defined in the CSD object, calling this
     method without any parameters will return it. If multiple matrices are
     defined, use either the ``frequency`` or ``index`` parameter to select
     one.
     Parameters
     ----------
     start : int | 0
         Return the CSD matrix for the time point
     stop : int | 0
         Return the CSD matrix for the time point
     Returns
     -------
     csd : ndarray, shape (n_channels, n_channels, n_times)
         The CSD matrix corresponding to the requested frequency.
     """
     if stop is None:
         stop = self.n_times
     return _vector_to_sym_mat(self._data[..., start:stop])