def get_tf_connectivity(self, measure_name, winlen, winstep, plot=False, baseline=None, crange='default'):
        """ Calculate estimate of time-varying connectivity.

        Connectivity is estimated in a sliding window approach on the current data set. The window is stepped
        `n_steps` = (`n_samples` - `winlen`) // `winstep` times.

        Parameters
        ----------
        measure_name : str
            Name of the connectivity measure to calculate. See :class:`Connectivity` for supported measures.
        winlen : int
            Length of the sliding window (in samples).
        winstep : int
            Step size for sliding window (in samples).
        plot : {False, None, Figure object}, optional
            Whether and where to plot the connectivity. If set to **False**, nothing is plotted. Otherwise set to the
            Figure object. If set to **None**, a new figure is created.
        baseline : [int, int] or None
            Start and end of the baseline period in samples. The baseline is subtracted from the connectivity. It is
            computed as the average of all windows that contain start or end, or fall between start and end.
            If set to None no baseline is subtracted.

        Returns
        -------
        result : array, shape = [n_channels, n_channels, nfft, n_steps]
            Values of the connectivity measure.
        fig : Figure object, optional
            Instance of the figure in which was plotted. This is only returned if `plot` is not **False**.

        Raises
        ------
        RuntimeError
            If the :class:`Workspace` instance does not contain a fitted VAR model.
        """
        if self.activations_ is None:
            raise RuntimeError("Time/Frequency Connectivity requires activations (call set_data after do_mvarica)")
        _, m, n = self.activations_.shape

        steps = list(range(0, n - winlen, winstep))
        nstep = len(steps)

        result = np.zeros((m, m, self.nfft_, nstep), np.complex64)
        for i, j in enumerate(steps):
            win = np.arange(winlen) + j
            data = self.activations_[:, :, win]
            data = data[self.trial_mask_, :, :]
            self.var_.fit(data)
            con = Connectivity(self.var_.coef, self.var_.rescov, self.nfft_)
            result[:, :, :, i] = getattr(con, measure_name)()
            
        if baseline:
            inref = np.zeros(nstep, bool)
            for i, j in enumerate(steps):
                a, b = j, j + winlen - 1
                inref[i] = b >= baseline[0] and a <= baseline[1]
            if np.any(inref):
                ref = np.mean(result[:, :, :, inref], axis=3, keepdims=True)
                result -= ref

        if plot is None or plot:
            fig = plot
            t0 = 0.5 * winlen / self.fs_ + self.time_offset_
            t1 = self.data_.shape[2] / self.fs_ - 0.5 * winlen / self.fs_ + self.time_offset_
            if self.plot_diagonal == 'fill':
                diagonal = 0
            elif self.plot_diagonal == 'S':
                diagonal = -1
                s = np.abs(self.get_tf_connectivity('S', winlen, winstep))
                if crange == 'default':
                    crange = [np.min(s), np.max(s)]
                fig = self.plotting.plot_connectivity_timespectrum(s, fs=self.fs_, crange=[np.min(s), np.max(s)],
                                                              freq_range=self.plot_f_range, time_range=[t0, t1],
                                                              diagonal=1, border=self.plot_outside_topo, fig=fig)
            else:
                diagonal = -1

            tfc = self._clean_measure(measure_name, result)
            if crange == 'default':
                if diagonal == -1:
                    for m in range(tfc.shape[0]):
                        tfc[m, m, :, :] = 0
                crange = [np.min(tfc), np.max(tfc)]
            fig = self.plotting.plot_connectivity_timespectrum(tfc, fs=self.fs_, crange=crange,
                                                          freq_range=self.plot_f_range, time_range=[t0, t1],
                                                          diagonal=diagonal, border=self.plot_outside_topo, fig=fig)

            return result, fig

        return result
def get_tf_connectivity ( self , measure_name , winlen , winstep , plot = False , baseline = None , crange = 'default' ) : if self . activations_ is None : raise RuntimeError ( "Time/Frequency Connectivity requires activations (call set_data after do_mvarica)" ) _ , m , n = self . activations_ . shape steps = list ( range ( 0 , n - winlen , winstep ) ) nstep = len ( steps ) result = np . zeros ( ( m , m , self . nfft_ , nstep ) , np . complex64 ) for i , j in enumerate ( steps ) : win = np . arange ( winlen ) + j data = self . activations_ [ : , : , win ] data = data [ self . trial_mask_ , : , : ] self . var_ . fit ( data ) con = Connectivity ( self . var_ . coef , self . var_ . rescov , self . nfft_ ) result [ : , : , : , i ] = getattr ( con , measure_name ) ( ) if baseline : inref = np . zeros ( nstep , bool ) for i , j in enumerate ( steps ) : a , b = j , j + winlen - 1 inref [ i ] = b >= baseline [ 0 ] and a <= baseline [ 1 ] if np . any ( inref ) : ref = np . mean ( result [ : , : , : , inref ] , axis = 3 , keepdims = True ) result -= ref if plot is None or plot : fig = plot t0 = 0.5 * winlen / self . fs_ + self . time_offset_ t1 = self . data_ . shape [ 2 ] / self . fs_ - 0.5 * winlen / self . fs_ + self . time_offset_ if self . plot_diagonal == 'fill' : diagonal = 0 elif self . plot_diagonal == 'S' : diagonal = - 1 s = np . abs ( self . get_tf_connectivity ( 'S' , winlen , winstep ) ) if crange == 'default' : crange = [ np . min ( s ) , np . max ( s ) ] fig = self . plotting . plot_connectivity_timespectrum ( s , fs = self . fs_ , crange = [ np . min ( s ) , np . max ( s ) ] , freq_range = self . plot_f_range , time_range = [ t0 , t1 ] , diagonal = 1 , border = self . plot_outside_topo , fig = fig ) else : diagonal = - 1 tfc = self . _clean_measure ( measure_name , result ) if crange == 'default' : if diagonal == - 1 : for m in range ( tfc . shape [ 0 ] ) : tfc [ m , m , : , : ] = 0 crange = [ np . min ( tfc ) , np . max ( tfc ) ] fig = self . plotting . plot_connectivity_timespectrum ( tfc , fs = self . fs_ , crange = crange , freq_range = self . plot_f_range , time_range = [ t0 , t1 ] , diagonal = diagonal , border = self . plot_outside_topo , fig = fig ) return result , fig return result
Calculate estimate of time-varying connectivity.

        Connectivity is estimated in a sliding window approach on the current data set. The window is stepped
        `n_steps` = (`n_samples` - `winlen`) // `winstep` times.

        Parameters
        ----------
        measure_name : str
            Name of the connectivity measure to calculate. See :class:`Connectivity` for supported measures.
        winlen : int
            Length of the sliding window (in samples).
        winstep : int
            Step size for sliding window (in samples).
        plot : {False, None, Figure object}, optional
            Whether and where to plot the connectivity. If set to **False**, nothing is plotted. Otherwise set to the
            Figure object. If set to **None**, a new figure is created.
        baseline : [int, int] or None
            Start and end of the baseline period in samples. The baseline is subtracted from the connectivity. It is
            computed as the average of all windows that contain start or end, or fall between start and end.
            If set to None no baseline is subtracted.

        Returns
        -------
        result : array, shape = [n_channels, n_channels, nfft, n_steps]
            Values of the connectivity measure.
        fig : Figure object, optional
            Instance of the figure in which was plotted. This is only returned if `plot` is not **False**.

        Raises
        ------
        RuntimeError
            If the :class:`Workspace` instance does not contain a fitted VAR model.