How to use the nltools.data.adjacency.Adjacency function in nltools

To help you get started, we’ve selected a few nltools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cosanlab / nltools / nltools / data / adjacency.py View on Github external
out: (dict) contains Adjacency instances of t values (or mean if
                 running permutation) and Adjacency instance of p values.

        '''
        if self.is_single_matrix:
            raise ValueError('t-test cannot be run on single matrices.')

        if permutation:
            t = []
            p = []
            for i in range(self.data.shape[1]):
                stats = one_sample_permutation(self.data[:, i], **kwargs)
                t.append(stats['mean'])
                p.append(stats['p'])
            t = Adjacency(np.array(t))
            p = Adjacency(np.array(p))
        else:
            t = self.mean().copy()
            p = deepcopy(t)
            t.data, p.data = ttest_1samp(self.data, 0, 0)

        return {'t': t, 'p': p}
github cosanlab / nltools / nltools / data / adjacency.py View on Github external
def fix_missing(data):
                X = data.squareform().copy()
                x,y = np.where(np.isnan(X))
                for i,j in zip(x,y):
                    if i != j:
                        X[i,j] = (np.nanmean(X[i,:]) + np.nanmean(X[:,j]))/2
                X = Adjacency(X, matrix_type=data.matrix_type)
                return (X, (x,y))
github cosanlab / nltools / nltools / data / adjacency.py View on Github external
Args:
            axis:  (int) calculate mean over features (0) or data (1).
                    For data it will be on upper triangle.

        Returns:
            mean:  float if single, adjacency if axis=0, np.array if axis=1
                    and multiple

        '''

        if self.is_single_matrix:
            return np.nanmean(self.data)
        else:
            if axis == 0:
                return Adjacency(data=np.nanmean(self.data, axis=axis),
                                 matrix_type=self.matrix_type + '_flat')
            elif axis == 1:
                return np.nanmean(self.data, axis=axis)
github cosanlab / nltools / nltools / data / adjacency.py View on Github external
Args:
            axis:  (int) calculate median over features (0) or data (1).
                    For data it will be on upper triangle.

        Returns:
            mean:  float if single, adjacency if axis=0, np.array if axis=1
                    and multiple

        '''

        if self.is_single_matrix:
            return np.nanmedian(self.data)
        else:
            if axis == 0:
                return Adjacency(data=np.nanmedian(self.data, axis=axis),
                                 matrix_type=self.matrix_type + '_flat')
            elif axis == 1:
                return np.nanmedian(self.data, axis=axis)
github cosanlab / nltools / nltools / data / adjacency.py View on Github external
Args:
            axis:  (int) calculate std over features (0) or data (1).
                    For data it will be on upper triangle.

        Returns:
            std:  float if single, adjacency if axis=0, np.array if axis=1 and
                    multiple

        '''

        if self.is_single_matrix:
            return np.nanstd(self.data)
        else:
            if axis == 0:
                return Adjacency(data=np.nanstd(self.data, axis=axis),
                                 matrix_type=self.matrix_type + '_flat')
            elif axis == 1:
                return np.nanstd(self.data, axis=axis)
github cosanlab / nltools / nltools / data / adjacency.py View on Github external
def similarity(self, data, plot=False, perm_type='2d', n_permute=5000,
                   metric='spearman', ignore_diagonal=False, **kwargs):
        ''' Calculate similarity between two Adjacency matrices.
        Default is to use spearman correlation and permutation test.
        Args:
            data: Adjacency data, or 1-d array same size as self.data
            perm_type: (str) '1d','2d', or None
            metric: (str) 'spearman','pearson','kendall'
            ignore_diagonal: (bool) only applies to 'directed' Adjacency types using perm_type=None or perm_type='1d'
        '''
        data1 = self.copy()
        if not isinstance(data, Adjacency):
            data2 = Adjacency(data)
        else:
            data2 = data.copy()

        if perm_type is None:
            n_permute = 0
            similarity_func = correlation_permutation
        elif perm_type == '1d':
            similarity_func = correlation_permutation
        elif perm_type == '2d':
            similarity_func = matrix_permutation
        else:
            raise ValueError("perm_type must be ['1d','2d', or None']")

        def _convert_data_similarity(data, perm_type=None, ignore_diagonal=ignore_diagonal):
            '''Helper function to convert data correctly'''
            if (perm_type is None) or (perm_type == '1d'):