U
    f»{jÝ  ã                   @   s0   d dl ZddlmZmZ ddlmZ dd„ ZdS )é    Né   )Ú_validate_typeÚ_check_option)Úint_likec                     s  ddl m‰ t| ƒ} t| ƒD ]ô\}}d|› d�}t|dtjˆjf|ƒ t|t	ƒrxˆj
ddgdt|ƒt|ƒftd� ¡ }n^t|› d	�|jd
gƒ |jd |jd kr¶t|› d|j› �ƒ‚t|ˆjƒsÎˆ |¡}n| ¡ }d|j|j|jk< | ¡  |jdk ¡ �stdƒ‚|| |< qt‡fdd„| D ƒƒ�s.t‚t dd„ | D ƒt¡‰ t ‡ fdd„tt| ƒƒD ƒt¡}t dd„ | D ƒt¡| }| ¡ }t ˆ ¡}t |¡  ˆ ¡}t !d
|| ft¡}	t "|t#¡}
t !|| t¡}d}t| ƒD ]æ\}}t$dƒgtˆ ƒ }t$dƒgtˆ ƒ }|j||< |j||< |jj|jj  k�rB|jjk�sHn t‚t$||||  ƒ}|t%|ƒ  &¡ |t%|ƒ  &¡ g|	dd…|f< t '|j|| ¡||< ||| 7 }|
|  (¡ �rºt‚d|
|< �qÞ|
 ¡ �sÔt‚| &¡ |	dd…|d…f< d||d…< ˆ ||	f|j)|j)f¡}|S )ak  Create a sparse binary adjacency/neighbors matrix.

    Parameters
    ----------
    *structure : list
        The adjacency along each dimension. Each entry can be:

        - ndarray or scipy.sparse.spmatrix
            A square binary adjacency matrix for the given dimension.
            For example created by :func:`mne.channels.find_ch_adjacency`.
        - int
            The number of elements along the given dimension. A lattice
            adjacency will be generated, which is a binary matrix
            reflecting that element N of an array is adjacent to
            elements at indices N - 1 and N + 1.

    Returns
    -------
    adjacency : scipy.sparse.coo_matrix, shape (n_features, n_features)
        The square adjacency matrix, where the shape ``n_features``
        corresponds to the product of the length of all dimensions.
        For example ``len(times) * len(freqs) * len(chans)``.

    See Also
    --------
    mne.channels.find_ch_adjacency
    mne.channels.read_ch_adjacency

    Notes
    -----
    For 4-dimensional data with shape ``(n_obs, n_times, n_freqs, n_chans)``,
    you can specify **no** connections among elements in a particular
    dimension by passing a matrix of zeros. For example:

    >>> import numpy as np
    >>> from scipy.sparse import diags
    >>> from mne.stats import combine_adjacency
    >>> n_times, n_freqs, n_chans = (50, 7, 16)
    >>> chan_adj = diags([1., 1.], offsets=(-1, 1), shape=(n_chans, n_chans))
    >>> combine_adjacency(
    ...     n_times,  # regular lattice adjacency for times
    ...     np.zeros((n_freqs, n_freqs)),  # no adjacency between freq. bins
    ...     chan_adj,  # custom matrix, or use mne.channels.find_ch_adjacency
    ...     )  # doctest: +NORMALIZE_WHITESPACE
    <5600x5600 sparse matrix of type '<class 'numpy.float64'>'
            with 27076 stored elements in COOrdinate format>
    r   ©Úsparsez
structure[ú]zint-likeé   )éÿÿÿÿr	   )ÚoffsetsÚshapeZdtypez.ndimr   z must be square, got shape g        z#All adjacency values must be 0 or 1c                 3   s   | ]}t |ˆ jƒV  qd S )N)Ú
isinstanceÚ
coo_matrix©Ú.0Údimr   © úS/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/stats/_adjacency.pyÚ	<genexpr>X   s     z$combine_adjacency.<locals>.<genexpr>c                 S   s   g | ]}|j d  ‘qS )r   ©r   )r   Údr   r   r   Ú
<listcomp>Y   s     z%combine_adjacency.<locals>.<listcomp>c              
      s4   g | ],}t  t  ˆ d |… ˆ |d d … g¡¡‘qS )Nr	   )ÚnpÚprodZconcatenate)r   Údir   r   r   r   Z   s   ÿc                 S   s   g | ]}|j j‘qS r   )ÚdataÚsizer   r   r   r   r   \   s     NTg      ð?)*Zscipyr   ÚlistÚ	enumerater   r   ZndarrayZspmatrixr   r   ZdiagsÚintÚfloatZtocoor   Úndimr   Ú
ValueErrorr   Úcopyr   ÚrowÚcolZeliminate_zerosÚallÚAssertionErrorÚarrayÚrangeÚlenÚsumr   ZarangeZreshapeÚemptyZzerosÚboolÚsliceÚtupleZravelZtileÚanyr   )Z	structurer   r   ÚnameZn_othersZn_eachZn_offZn_diagZverticesÚedgesÚusedÚweightsÚoffsetZs_lZs_rÚslÚgraphr   )r   r   r   Úcombine_adjacency   sx    0

ýÿ

ÿÿ


*ÿ

ÿr8   )Únumpyr   Úutilsr   r   Zutils.checkr   r8   r   r   r   r   Ú<module>   s   