# Python numpy.triu_indices_from() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

```def get_adjacency_matrix(out_dir, sid, expt_id):

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 ```

Example 2

```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 ```

Example 3

```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 ```

Example 4

```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 ```

Example 5

```def correlation(data,title=''):

corr = data.corr(method='spearman')

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.set_xticklabels(data,rotation=25,ha="right");

Example 6

```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
# 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 ```

Example 7

```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 ```

Example 8

```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 ```

Example 9

```def sns_triangle(matrix, plt_title, only_class=None):

sns.set(style="white")
# Generate a mask for the upper triangle

# 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
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') ```

Example 10

```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 ```

Example 11

```def plot_corr_heatmap(corr, labels, heading):

sns.set(style="white")

# Generate a mask for the upper triangle

# 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
square=True, xticklabels=labels, yticklabels=labels,
linewidths=.5, ax=ax, cbar_kws={"shrink": .5}, annot=True)
plt.show() ```

Example 12

```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])] ```

Example 13

```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] ```

Example 14

```def __init__(self, parent, data, labels, width=6, height=6, dpi=100):
figure = Figure(figsize=(width, height), dpi=dpi, tight_layout=True)

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
# Draw the heatmap with the mask and correct aspect ratio
# vmax = np.abs(corr).max()
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()) ```

Example 15

```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 ```

Example 16

```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:
else:
fig = plt.figure(figsize=figsize, dpi=300)
fig.set_facecolor('white')

with sns.plotting_context("notebook", font_scale=1):
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%']) ```

Example 17

```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 ```

Example 18

```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

# Draw the heatmap
# TODO: Move mesh to a property so the colorbar can be finalized
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])
)

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([]) ```

Example 19

```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 ```

Example 20

```def plot_scatters(subjects, axes):

ftemp = "correlation_analysis/{}_{}_ifs.pkz"
for subj, ax in zip(subjects, axes):

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) ```

Example 21

```def plot_kdes(subjects, axes):

ftemp = "correlation_analysis/{}_{}_ifs.pkz"
for subj, ax in zip(subjects, axes):

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 ```

Example 22

```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] ```

Example 23

```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 ```

Example 24

```def mask_upper_triangle(data):
return data ```

Example 25

```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 ```

Example 26

```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) ```

Example 27

```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) ```

Example 28

```def plot_2_corr_heatmaps(corr1, corr2, labels, title1, title2):
fig=plt.figure(figsize=(9, 8))
gs = gridspec.GridSpec(1, 2)

sns.set(style="white")

# Generate a mask for the upper triangle

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
square=True, xticklabels=labels, yticklabels=labels,
linewidths=.5, ax=ax1, cbar_kws={"shrink": .3}, annot=True)
ax1.set_title(title1)
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()

###############################################################################
############################################################################### ```

Example 29

```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

# 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')