Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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}
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))
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)
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)
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)
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'):