Python numpy 模块,triu_indices_from() 实例源码
我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用numpy.triu_indices_from()。
def get_adjacency_matrix(out_dir, sid, expt_id):
"Returns the adjacency matrix"
vec_path = pjoin(out_dir, sid, '{}_graynet.csv'.format(expt_id))
edge_vec = np.genfromtxt(vec_path)
matrix_size = np.int64( (1.0 + np.sqrt(1.0+8.0*len(edge_vec)))/2.0 )
edge_mat = np.zeros([matrix_size, matrix_size])
# making this symmetric as required by nilearn's plot_connectome (stupid)
# upper tri; diag +1; # lower tri; diag -1
upper_tri = np.triu_indices_from(edge_mat, +1)
lower_tri = np.tril_indices_from(edge_mat, -1)
edge_mat[upper_tri] = edge_vec
edge_mat[lower_tri] = edge_mat.T[lower_tri]
return edge_mat
def from_sym_2_tri(symm):
"""convert a 2D symmetric matrix to an upper
triangular matrix in 1D format
Parameters
----------
symm : 2D array
Symmetric matrix
Returns
-------
tri: 1D array
Contains elements of upper triangular matrix
"""
inds = np.triu_indices_from(symm)
tri = symm[inds]
return tri
def tangent_space(covmats, Cref):
"""Project a set of covariance matrices in the tangent space according to the given reference point Cref
:param covmats: Covariance matrices set, Ntrials X Nchannels X Nchannels
:param Cref: The reference covariance matrix
:returns: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
"""
Nt, Ne, Ne = covmats.shape
Cm12 = invsqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
T = numpy.empty((Nt, Ne * (Ne + 1) / 2))
coeffs = (
numpy.sqrt(2) *
numpy.triu(
numpy.ones(
(Ne,
Ne)),
1) +
numpy.eye(Ne))[idx]
for index in range(Nt):
tmp = numpy.dot(numpy.dot(Cm12, covmats[index, :, :]), Cm12)
tmp = logm(tmp)
T[index, :] = numpy.multiply(coeffs, tmp[idx])
return T
def untangent_space(T, Cref):
"""Project a set of Tangent space vectors in the manifold according to the given reference point Cref
:param T: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
:param Cref: The reference covariance matrix
:returns: A set of Covariance matrix, Ntrials X Nchannels X Nchannels
"""
Nt, Nd = T.shape
Ne = int((numpy.sqrt(1 + 8 * Nd) - 1) / 2)
C12 = sqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
covmats = numpy.empty((Nt, Ne, Ne))
covmats[:, idx[0], idx[1]] = T
for i in range(Nt):
covmats[i] = numpy.diag(numpy.diag(covmats[i])) + numpy.triu(
covmats[i], 1) / numpy.sqrt(2) + numpy.triu(covmats[i], 1).T / numpy.sqrt(2)
covmats[i] = expm(covmats[i])
covmats[i] = numpy.dot(numpy.dot(C12, covmats[i]), C12)
return covmats
def correlation(data,title=''):
corr = data.corr(method='spearman')
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
sns.set(style="white")
sns.set_context("notebook", font_scale=2, rc={"lines.linewidth": 0.3})
rcParams['figure.figsize'] = 25, 12
rcParams['font.family'] = 'Verdana'
rcParams['figure.dpi'] = 300
g = sns.heatmap(corr, mask=mask, linewidths=1, cmap="RdYlGn", annot=False)
g.set_xticklabels(data,rotation=25,ha="right");
plt.tick_params(axis='both', which='major', pad=15);
def get_net_vectors(subject_list, kind, atlas_name="aal"):
"""
subject_list : the subject short IDs list
kind : the kind of connectivity to be used, e.g. lasso, partial correlation, correlation
atlas_name : name of the atlas used
returns:
matrix : matrix of connectivity vectors (num_subjects x num_connections)
"""
# This is an alternative implementation
networks = load_all_networks(subject_list, kind, atlas_name=atlas_name)
# Get Fisher transformed matrices
norm_networks = [np.arctanh(mat) for mat in networks]
# Get upper diagonal indices
idx = np.triu_indices_from(norm_networks[0], 1)
# Get vectorised matrices
vec_networks = [mat[idx] for mat in norm_networks]
# Each subject should be a row of the matrix
matrix = np.vstack(vec_networks)
return matrix
def tangent_space(covmats, Cref):
"""Project a set of covariance matrices in the tangent space according to the given reference point Cref
:param covmats: Covariance matrices set, Ntrials X Nchannels X Nchannels
:param Cref: The reference covariance matrix
:returns: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
"""
Nt, Ne, Ne = covmats.shape
Cm12 = invsqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
T = numpy.empty((Nt, Ne * (Ne + 1) / 2))
coeffs = (
numpy.sqrt(2) *
numpy.triu(
numpy.ones(
(Ne,
Ne)),
1) +
numpy.eye(Ne))[idx]
for index in range(Nt):
tmp = numpy.dot(numpy.dot(Cm12, covmats[index, :, :]), Cm12)
tmp = logm(tmp)
T[index, :] = numpy.multiply(coeffs, tmp[idx])
return T
def untangent_space(T, Cref):
"""Project a set of Tangent space vectors in the manifold according to the given reference point Cref
:param T: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
:param Cref: The reference covariance matrix
:returns: A set of Covariance matrix, Ntrials X Nchannels X Nchannels
"""
Nt, Nd = T.shape
Ne = int((numpy.sqrt(1 + 8 * Nd) - 1) / 2)
C12 = sqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
covmats = numpy.empty((Nt, Ne, Ne))
covmats[:, idx[0], idx[1]] = T
for i in range(Nt):
covmats[i] = numpy.diag(numpy.diag(covmats[i])) + numpy.triu(
covmats[i], 1) / numpy.sqrt(2) + numpy.triu(covmats[i], 1).T / numpy.sqrt(2)
covmats[i] = expm(covmats[i])
covmats[i] = numpy.dot(numpy.dot(C12, covmats[i]), C12)
return covmats
def sns_triangle(matrix, plt_title, only_class=None):
sns.set(style="white")
# Generate a mask for the upper triangle
mask = np.zeros_like(matrix, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Set up the matplotlib figure
f, ax = subplots(figsize=(11, 9))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(matrix.as_matrix(), mask=mask, cmap=cmap, vmax=.3,
square=True, xticklabels=5, yticklabels=5,
linewidths=.5, cbar_kws={"shrink": .5}, ax=ax)
title(plt_title)
xlabel('Preprocessed Features')
ylabel('Preprocessed Features')
if only_class is None:
only_class = ''
savefig('images/triangle'+only_class+'.png')
def _compute_indices_of_one_pair_of_mergeable_groups(distance_matrix, min_dist_between_items_in_different_groups):
"""
This function semantically operates on a collection of grouped items.
It returns a pair of indices corresponding to a pair of groups which are close enough to merge.
Args:
distance_matrix (np.ndarray): the matrix containing distances between all groups
min_dist_between_items_in_different_groups (float): the algorithm will only suggest a pair of groups to be
merged if they each contain an item which is within this
value of the other.
Returns:
a pair of indices corresponding to a mergeable pair of groups
"""
# Get all indices (i,j) with i<j, in a 2-element tuple (all rows indices, all column indices).
inds = np.triu_indices_from(distance_matrix, 1) # type: tuple
# Get a boolean vector which indexes both elements of inds and tells whether the pair of groups should be merged
groups_could_be_merged = distance_matrix[inds] < min_dist_between_items_in_different_groups
# Get the subset of indices for groups we can merge
inds_of_mergeable_groups = [inds[i][groups_could_be_merged] for i in range(len(inds))]
# Return the first pair of indices, if any were found
indices_matrix = np.transpose(inds_of_mergeable_groups)
return indices_matrix[0] if len(indices_matrix) > 0 else None
def plot_corr_heatmap(corr, labels, heading):
sns.set(style="white")
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(8, 8))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3,
square=True, xticklabels=labels, yticklabels=labels,
linewidths=.5, ax=ax, cbar_kws={"shrink": .5}, annot=True)
ax.set_title(heading)
plt.show()
def lowertosymmetric(a, copy=False):
a = np.copy(a) if copy else a
idxs = np.triu_indices_from(a)
a[idxs] = a[(idxs[1], idxs[0])]
def uppertosymmetric(a, copy=False):
a = np.copy(a) if copy else a
idxs = np.triu_indices_from(a)
a[(idxs[1], idxs[0])] = a[idxs]
def __init__(self, parent, data, labels, width=6, height=6, dpi=100):
figure = Figure(figsize=(width, height), dpi=dpi, tight_layout=True)
axes = figure.add_subplot(111)
super(CorrelationPlot, self).__init__(figure)
self.setParent(parent)
sns.set(style="darkgrid")
corr = data
# cmap = sns.diverging_palette(220, 10, as_cmap=True)
# corrplot(data, names=labels, annot=True, sig_stars=False,
# diag_names=True, cmap=cmap, ax=axes, cbar=True)
df = pd.DataFrame(data=data, columns=labels)
corr = df.corr()
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Draw the heatmap with the mask and correct aspect ratio
vmax = np.abs(corr.values[~mask]).max()
# vmax = np.abs(corr).max()
sns.heatmap(corr, mask=mask, cmap=plt.cm.PuOr, vmin=-vmax, vmax=vmax,
square=True, linecolor="lightgray", linewidths=1, ax=axes)
for i in range(len(corr)):
axes.text(i + 0.5, i + 0.5, corr.columns[i],
ha="center", va="center", rotation=0)
for j in range(i + 1, len(corr)):
s = "{:.3f}".format(corr.values[i, j])
axes.text(j + 0.5, i + 0.5, s,
ha="center", va="center")
axes.axis("off")
# If uncommented, fills widget
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.updateGeometry()
self.setMinimumSize(self.size())
def get_triu_handle_inf_nan(weights_matrix):
"Issue a warning when NaNs or Inf are found."
if weights_matrix is None:
raise ValueError('Computation failed.')
upper_tri_vec = weights_matrix[np.triu_indices_from(weights_matrix, 1)]
warn_nan(upper_tri_vec)
return upper_tri_vec
def plot_corrmat(in_csv, out_file=None):
import seaborn as sn
sn.set(style="whitegrid")
dataframe = pd.read_csv(in_csv, index_col=False, na_values='n/a', na_filter=False)
colnames = dataframe.columns.ravel().tolist()
for col in ['subject_id', 'site', 'modality']:
try:
colnames.remove(col)
except ValueError:
pass
# Correlation matrix
corr = dataframe[colnames].corr()
corr = corr.dropna((0,1), 'all')
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Generate a custom diverging colormap
cmap = sn.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
corrplot = sn.clustermap(corr, cmap=cmap, center=0., method='average', square=True, linewidths=.5)
plt.setp(corrplot.ax_heatmap.yaxis.get_ticklabels(), rotation='horizontal')
# , mask=mask, square=True, linewidths=.5, cbar_kws={"shrink": .5})
if out_file is None:
out_file = 'corr_matrix.svg'
fname, ext = op.splitext(out_file)
if ext[1:] not in ['pdf', 'svg', 'png']:
ext = '.svg'
out_file = fname + '.svg'
corrplot.savefig(out_file, format=ext[1:], bbox_inches='tight', pad_inches=0, dpi=100)
return corrplot
def heatmap(data, half=True, scale=1, vmin=-0.8, vmax=0.8, cmap='RdBu_r', **kwargs):
"""
:param dataframe:
:param half:
:param scale:
:param vmin:
:param vmax:
:param cmap:
:param kwargs:
:return:
"""
figsize = (6 * scale, 4 * scale)
for arg in kwargs.keys():
if arg is 'figsize':
figsize = kwargs[arg]
if half:
mask = np.zeros_like(data)
mask[np.triu_indices_from(mask)] = True
else:
mask = None
fig = plt.figure(figsize=figsize, dpi=300)
fig.set_facecolor('white')
axes = fig.add_subplot(111)
with sns.plotting_context("notebook", font_scale=1):
ax = sns.heatmap(data, mask=mask, vmin=vmin, vmax=vmax,
cmap=cmap, square=True, ax=axes)
# ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45)
ax.tick_params(labelsize=3.5, length=0)
# ax.set_yticklabels(ax.yaxis.get_majorticklabels(), rotation=45)
cbar = ax.collections[0].colorbar
cbar.set_ticks([vmin, 0, vmax])
# cbar.set_ticklabels(['low', '20%', '75%', '100%'])
def scatterplot_matrix(data, attNames, **kwargs):
rows, atts = data.shape
fig, axes = plt.subplots(nrows = atts, ncols =atts, figsize=(30,30))
fig.subplots_adjust(hspace = 0.05 , wspace = 0.05)
for ax in axes.flat:
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
if ax.is_first_col():
ax.yaxis.set_ticks_position('left')
if ax.is_last_col():
ax.yaxis.set_ticks_position('right')
if ax.is_first_row():
ax.xaxis.set_ticks_position('top')
if ax.is_last_row():
ax.xaxis.set_ticks_position('bottom')
for i, j in zip(*np.triu_indices_from(axes, k=1)):
for x, y in [(i,j), (j,i)]:
axes[x,y].plot(data[y], data[x], **kwargs)
# Label the diagonal subplots...
for i, label in enumerate(attNames):
axes[i,i].annotate(label, (0.5, 0.5), xycoords='axes fraction',
ha='center', va='center')
for i, j in zip(range(atts), itertools.cycle((-1, 0))):
axes[j,i].xaxis.set_visible(True)
axes[i,j].yaxis.set_visible(True)
return fig
def draw(self, **kwargs):
"""
Draws the heatmap of the ranking matrix of variables.
"""
# Set the axes aspect to be equal
self.ax.set_aspect("equal")
# Generate a mask for the upper triangle
mask = np.zeros_like(self.ranks_, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Draw the heatmap
# TODO: Move mesh to a property so the colorbar can be finalized
data = np.ma.masked_where(mask, self.ranks_)
mesh = self.ax.pcolormesh(data, cmap=self.colormap, vmin=-1, vmax=1)
# Set the Axis limits
self.ax.set(
xlim=(0, data.shape[1]), ylim=(0, data.shape[0])
)
# Add the colorbar
cb = self.ax.figure.colorbar(mesh, None, self.ax)
cb.outline.set_linewidth(0)
# Reverse the rows to get the lower left triangle
self.ax.invert_yaxis()
# Add ticks and tick labels
self.ax.set_xticks(np.arange(len(self.ranks_)) + 0.5)
self.ax.set_yticks(np.arange(len(self.ranks_)) + 0.5)
if self.show_feature_names_:
self.ax.set_xticklabels(self.features_, rotation=90)
self.ax.set_yticklabels(self.features_)
else:
self.ax.set_xticklabels([])
self.ax.set_yticklabels([])
def mds_variance_explained(corrmat, mds_coords):
"""Determine how much variance is explained by projection onto MDS coords."""
orig_dist = (1 - corrmat)[np.triu_indices_from(corrmat, 1)]
mds_dist = distance.pdist(mds_coords)
r, _ = stats.pearsonr(orig_dist, mds_dist)
return r ** 2
def plot_scatters(subjects, axes):
ftemp = "correlation_analysis/{}_{}_ifs.pkz"
for subj, ax in zip(subjects, axes):
sticks = moss.load_pkl(ftemp.format(subj, "sticks")).corrmat
rest = moss.load_pkl(ftemp.format(subj, "rest")).corrmat
triu = np.triu_indices_from(rest, 1)
ax.scatter(sticks[triu], rest[triu], s=3, linewidth=.2,
color=".6", edgecolor="w",
rasterized=True)
ax.plot([-.2, .8], [-.2, .8], lw=1, dashes=(5, 2), color=".3")
plt.setp(axes,
xlim=(-.25, .8), ylim=(-.25, .8),
xticks=np.linspace(-.2, .8, 6),
yticks=np.linspace(-.2, .8, 6),
aspect="equal")
plt.setp(axes[1:], yticklabels=[])
for ax in axes:
sns.despine(ax=ax, trim=True)
plt.setp(ax.get_xticklabels(), size=6)
plt.setp(ax.get_yticklabels(), size=6)
def plot_kdes(subjects, axes):
ftemp = "correlation_analysis/{}_{}_ifs.pkz"
for subj, ax in zip(subjects, axes):
sticks = moss.load_pkl(ftemp.format(subj, "sticks")).corrmat
rest = moss.load_pkl(ftemp.format(subj, "rest")).corrmat
triu = np.triu_indices_from(rest, 1)
sns.kdeplot(sticks[triu], color=".15",
label="residual", ax=ax)
sns.kdeplot(rest[triu], color=".45", dashes=[4, 1],
label="resting", ax=ax)
plt.setp(axes,
xlim=(-.25, .8), ylim=(0, 17),
xticks=np.linspace(-.2, .8, 6),
yticks=[])
for ax in axes:
sns.despine(ax=ax, left=True, trim=True)
plt.setp(ax.get_xticklabels(), size=6)
plt.setp(ax.get_yticklabels(), size=6)
axes[0].legend(bbox_to_anchor=(1.2, .8))
for ax in axes[1:]:
ax.legend_ = None
def upper_tri_vec(matrix):
"Returns the vectorized values of upper triangular part of a matrix"
triu_idx = np.triu_indices_from(matrix, 1)
return matrix[triu_idx]
def triang2mtx(xs: Vector, dim: int) -> Matrix:
"""
Transform a symmetric matrix represented as a flatten upper triangular
matrix to the correspoding 2-dimensional array.
"""
# New array
mtx = np.zeros((dim, dim))
# indexes of the upper triangular
inds = np.triu_indices_from(mtx)
# Fill the upper triangular of the new array
mtx[inds] = xs
# Fill the lower triangular
mtx[(inds[1], inds[0])] = xs
return mtx
def mask_upper_triangle(data):
mask = np.zeros_like(data)
mask[np.triu_indices_from(mask)] = True
data = np.ma.array(data, mask=mask)
return data
def read_triangular(filepath):
"""Open Pi matrix output from SLICE.
All matrix opening functions return first the
genomic windows corresponding to the axes of the
proximity matrix, then the proximity matrix itself.
Since SLICE output matrices do not embed the genomic
locations of the windows, the first return value is
None.
:param str filepath: Path to the SLICE output file
:returns: (None, SLICE Pi matrix)
"""
with open(filepath) as in_data:
arr = [[float(i) for i in line.split()] for line in in_data]
size = len(arr[-1])
proximity_matrix = np.zeros((size, size))
lower_i = np.tril_indices_from(proximity_matrix)
upper_i = np.triu_indices_from(proximity_matrix)
proximity_matrix[:] = np.NAN
proximity_matrix[lower_i] = list(itertools.chain(*arr))
proximity_matrix[upper_i] = proximity_matrix.T[upper_i]
proximity_matrix[proximity_matrix > 1.] = np.NAN
return None, proximity_matrix
def sample_invwishart(S,nu):
# TODO make a version that returns the cholesky
# TODO allow passing in chol/cholinv of matrix parameter lmbda
# TODO lowmem! memoize! dchud (eigen?)
n = S.shape[0]
chol = np.linalg.cholesky(S)
if (nu <= 81+n) and (nu == np.round(nu)):
x = np.random.randn(nu,n)
else:
x = np.diag(np.sqrt(np.atleast_1d(stats.chi2.rvs(nu-np.arange(n)))))
x[np.triu_indices_from(x,1)] = np.random.randn(n*(n-1)//2)
R = np.linalg.qr(x,'r')
T = scipy.linalg.solve_triangular(R.T,chol.T,lower=True).T
return np.dot(T,T.T)
def sample_invwishart(S,nu):
# TODO make a version that returns the cholesky
# TODO allow passing in chol/cholinv of matrix parameter lmbda
# TODO lowmem! memoize! dchud (eigen?)
n = S.shape[0]
chol = np.linalg.cholesky(S)
if (nu <= 81+n) and (nu == np.round(nu)):
x = np.random.randn(nu,n)
else:
x = np.diag(np.sqrt(np.atleast_1d(stats.chi2.rvs(nu-np.arange(n)))))
x[np.triu_indices_from(x,1)] = np.random.randn(n*(n-1)/2)
R = np.linalg.qr(x,'r')
T = scipy.linalg.solve_triangular(R.T,chol.T,lower=True).T
return np.dot(T,T.T)
def plot_2_corr_heatmaps(corr1, corr2, labels, title1, title2):
fig=plt.figure(figsize=(9, 8))
gs = gridspec.GridSpec(1, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
sns.set(style="white")
# Generate a mask for the upper triangle
mask = np.zeros_like(corr1, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr1, mask=mask, cmap=cmap, vmax=.3,
square=True, xticklabels=labels, yticklabels=labels,
linewidths=.5, ax=ax1, cbar_kws={"shrink": .3}, annot=True)
ax1.set_title(title1)
sns.heatmap(corr2, mask=mask, cmap=cmap, vmax=.3,
square=True, xticklabels=labels, yticklabels=labels,
linewidths=.5, ax=ax2, cbar_kws={"shrink": .3}, annot=True)
ax2.set_title(title2)
fig.tight_layout()
plt.show()
###############################################################################
# Attribution
###############################################################################