def sort_sentences(sentences, words,model, pagerank_config = {'alpha': 0.85,}):
"""???????????????
Keyword arguments:
sentences -- ????????
words -- ?????????sentences???????????????
sim_func -- ????????????????????????
pagerank_config -- pagerank???
"""
sorted_sentences = []
_source = words
sentences_num = len(_source)
graph = np.zeros((sentences_num, sentences_num))
for x in xrange(sentences_num):
for y in xrange(x, sentences_num):
similarity = get_similarity( _source[x], _source[y], model)
graph[x, y] = similarity
graph[y, x] = similarity
nx_graph = nx.from_numpy_matrix(graph)
scores = nx.pagerank(nx_graph, **pagerank_config) # this is a dict
sorted_scores = sorted(scores.items(), key = lambda item: item[1], reverse=True)
for index, score in sorted_scores:
item = AttrDict(index=index, sentence=sentences[index], weight=score)
sorted_sentences.append(item)
return sorted_sentences
python类from_numpy_matrix()的实例源码
def sort_sentences(sentences, words, sim_func = get_similarity, pagerank_config = {'alpha': 0.85,}):
"""???????????????
Keyword arguments:
sentences -- ????????
words -- ?????????sentences???????????????
sim_func -- ????????????????????????
pagerank_config -- pagerank???
"""
sorted_sentences = []
_source = words
sentences_num = len(_source)
graph = np.zeros((sentences_num, sentences_num))
for x in xrange(sentences_num):
for y in xrange(x, sentences_num):
similarity = sim_func( _source[x], _source[y] )
graph[x, y] = similarity
graph[y, x] = similarity
nx_graph = nx.from_numpy_matrix(graph)
scores = nx.pagerank(nx_graph, **pagerank_config) # this is a dict
sorted_scores = sorted(scores.items(), key = lambda item: item[1], reverse=True)
for index, score in sorted_scores:
item = AttrDict(index=index, sentence=sentences[index], weight=score)
sorted_sentences.append(item)
return sorted_sentences
def create_graph(connectome, labels):
""" Create a graph structure from the connectome matrix.
Parameters
----------
connectome: array (N, N)
a matrix representing the structural connections.
labels: list of str (N,)
the labels used to create the connectome matrix.
Returns
-------
graph: Graph
a graph structure.
"""
graph = nx.from_numpy_matrix(connectome)
for index, name in enumerate(labels):
name = name.rstrip("\n")
graph.node[index] = {"label": name}
return graph
def test_similar_output_to_naive_mat_3(self):
mat = scipy.io.loadmat('bcspwr01.mat')
# I love the UFSMC (https://www.cise.ufl.edu/research/sparse/matrices/)
# but wow they really buried the matrix in this .mat
A = mat['Problem'][0][0][1].todense()
G = nx.from_numpy_matrix(A)
G3 = graphpca.reduce_graph_efficiently(G, 3)
G3n = graphpca.reduce_graph_naively(G, 3)
self.assertTrue(np.allclose(G3, G3n, rtol=1e-04, atol=1e-06),
'Regular result:\n{}\nNaive result:\n{}\n'.format(G3, G3n))
def test_add_supernode_similar_output_to_naive_mat_3(self):
mat = scipy.io.loadmat('bcspwr01.mat')
A = mat['Problem'][0][0][1].todense()
G = nx.from_numpy_matrix(A)
G3 = graphpca.reduce_graph_efficiently(G, 3, add_supernode=True)
G3n = graphpca.reduce_graph_naively(G, 3)
self.assertTrue(np.allclose(G3, G3n, rtol=1e-02, atol=1e-06),
'Regular result:\n{}\nNaive result:\n{}\n'.format(G3, G3n))
def astar_len(graph, width, height, startx, starty, targetx, targety):
adj = adjecent_2DGridWorld(graph, width, height)
G = nx.from_numpy_matrix(adj)
return nx.astar_path_length(G, starty*width+startx, targety*width+targetx)
def nx_plot(adj, pos, value):
# input: adjacent matrix, position, value map
label = np.arange(len(pos))
G=nx.from_numpy_matrix(adj)
nx.draw_networkx_nodes(G, pos, node_color = value)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos)
plt.ion()
plt.show()
def astar_len(adj, start, target):
G = nx.from_numpy_matrix(adj)
return nx.astar_path_length(G, start, target)
# Data
def nx_plot(adj, pos, value):
# input: adjacent matrix, position, value map
label = np.arange(len(pos))
G=nx.from_numpy_matrix(adj)
nx.draw_networkx_nodes(G, pos, node_color = value)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, width=1.0)
plt.ion()
plt.show()
def nx_plot(adj, pos, value):
# input: adjacent matrix, position, value map
label = np.arange(len(pos))
G=nx.from_numpy_matrix(adj)
nodes = nx.draw_networkx_nodes(G, pos, node_color=value, node_size=200)
nodes.set_edgecolor('black')
nx.draw_networkx_labels(G, pos, font_size=10)
nx.draw_networkx_edges(G, pos, width=1.0)
plt.ion()
plt.show()
def sort_sentences(sentences, words, sim_func = get_similarity, pagerank_config = {'alpha': 0.85,}):
"""???????????????
Keyword arguments:
sentences -- ????????
words -- ?????????sentences???????????????
sim_func -- ????????????????????????
pagerank_config -- pagerank???
"""
sorted_sentences = []
_source = words
sentences_num = len(_source)
graph = np.zeros((sentences_num, sentences_num))
for x in xrange(sentences_num):
for y in xrange(x, sentences_num):
similarity = sim_func( _source[x], _source[y] )
graph[x, y] = similarity
graph[y, x] = similarity
nx_graph = nx.from_numpy_matrix(graph)
scores = nx.pagerank(nx_graph, **pagerank_config) # this is a dict
sorted_scores = sorted(scores.items(), key = lambda item: item[1], reverse=True)
for index, score in sorted_scores:
item = AttrDict(index=index, sentence=sentences[index], weight=score)
sorted_sentences.append(item)
return sorted_sentences
def _load_data(self):
# adjacency matrix
adj = np.loadtxt(self.data, delimiter = ",")
self._ref = nx.DiGraph()
self._ref = nx.from_numpy_matrix(adj, create_using = self._ref)
# vertex attributes
with open(self.details, mode = "r") as handle:
read = csv.reader(handle)
for row in read:
u = self._ref.node[int(row[0])]
u["ndd"] = row[1] == "1"
u["bp"] = row[2]
u["bd"] = row[3]
def _load_data(self):
# adjacency matrix
adj = np.loadtxt(self.data, delimiter = ",")
self._ref = nx.DiGraph()
self._ref = nx.from_numpy_matrix(adj, create_using = self._ref)
# vertex attributes
with open(self.details, mode = "r") as handle:
read = csv.reader(handle)
for row in read:
u = self._ref.node[int(row[0])]
u["ndd"] = row[1] == "1"
u["bp"] = row[2]
u["bd"] = row[3]
def _load_data(self):
# adjacency matrix
adj = np.loadtxt(self.data, delimiter = ",")
self._ref = nx.DiGraph()
self._ref = nx.from_numpy_matrix(adj, create_using = self._ref)
# vertex attributes
with open(self.details, mode = "r") as handle:
read = csv.reader(handle)
for row in read:
u = self._ref.node[int(row[0])]
u["ndd"] = row[1] == "1"
u["bp"] = row[2]
u["bd"] = row[3]
def __init__(self,data):
super(GraphVisualization, self).__init__()
self.data = data
self.G = nx.from_numpy_matrix(self.data)
self.DrawHighlightedGraph()
def Find_HighlightedEdges(self,weight = -0.54):
self.ThresholdData = np.copy(self.data)
low_values_indices = self.ThresholdData < weight # Where values are low
self.ThresholdData[low_values_indices] = 0
self.g = nx.from_numpy_matrix(self.ThresholdData)
def __init__(self,data):
super(GraphVisualization, self).__init__()
self.data = data
self.G = nx.from_numpy_matrix(self.data)
self.DrawHighlightedGraph()
def setG(self):
self.G = nx.from_numpy_matrix(self.data)
def Find_HighlightedEdges(self,weight = 0):
self.ThresholdData = np.copy(self.data)
# low_values_indices = self.ThresholdData < weight # Where values are low
# self.ThresholdData[low_values_indices] = 0
# graterindices = [ (i,j) for i,j in np.ndenumerate(self.ThresholdData) if any(i > j) ]
# self.ThresholdData[graterindices[:1]] = 0
# self.ThresholdData = np.tril(self.ThresholdData)
# print self.ThresholdData, "is the data same??"
"""
test 2 highlighted edges there
"""
# np.savetxt('test2.txt', self.ThresholdData, delimiter=',', fmt='%1.4e')
self.g = nx.from_numpy_matrix(self.ThresholdData)
def returnThresholdedDataValues(data, weight = 0):
ThresholdData = np.copy(data)
low_values_indices = ThresholdData < weight # Where values are low
ThresholdData[low_values_indices] = 0
return nx.from_numpy_matrix(ThresholdData)
def sort_sentences(sentences, words, sim_func = get_similarity, pagerank_config = {'alpha': 0.85,}):
"""???????????????
Keyword arguments:
sentences -- ????????
words -- ?????????sentences???????????????
sim_func -- ????????????????????????
pagerank_config -- pagerank???
"""
sorted_sentences = []
_source = words
sentences_num = len(_source)
graph = np.zeros((sentences_num, sentences_num))
for x in xrange(sentences_num):
for y in xrange(x, sentences_num):
similarity = sim_func( _source[x], _source[y] )
graph[x, y] = similarity
graph[y, x] = similarity
nx_graph = nx.from_numpy_matrix(graph)
scores = nx.pagerank(nx_graph, **pagerank_config) # this is a dict
sorted_scores = sorted(scores.items(), key = lambda item: item[1], reverse=True)
for index, score in sorted_scores:
item = AttrDict(index=index, sentence=sentences[index], weight=score)
sorted_sentences.append(item)
return sorted_sentences
def nxG(y):
if type(y) is np.ndarray:
if (y == y.T).all():
# Undirected Graph
typeG = nx.Graph()
else:
# Directed Graph
typeG = nx.DiGraph()
G = nx.from_numpy_matrix(y, create_using=typeG)
else:
G = y
return G
# Global settings
def getG(self):
if not hasattr(self, 'G'):
if self.is_symmetric():
# Undirected Graph
typeG = nx.Graph()
else:
# Directed Graph
typeG = nx.DiGraph()
self.G = nx.from_numpy_matrix(self.data, create_using=typeG)
#self.G = nx.from_scipy_sparse_matrix(self.data, typeG)
return self.G
def to_directed(self):
''' Return self verion of graph wehre all links are flatened '''
if self.is_symmetric():
return self.getG()
else:
# nx to_undirected nedd a linkks in both side.
return nx.from_numpy_matrix(self.data, create_using=nx.Graph())
#
# Get Statistics
#
def plot_graph(self, am, position=None, cls=None, fig_name='graph.png'):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
g = nx.from_numpy_matrix(am)
if position is None:
position=nx.drawing.circular_layout(g)
fig = plt.figure()
if cls is None:
cls='r'
else:
# Make a user-defined colormap.
cm1 = mcol.LinearSegmentedColormap.from_list("MyCmapName", ["r", "b"])
# Make a normalizer that will map the time values from
# [start_time,end_time+1] -> [0,1].
cnorm = mcol.Normalize(vmin=0, vmax=1)
# Turn these into an object that can be used to map time values to colors and
# can be passed to plt.colorbar().
cpick = cm.ScalarMappable(norm=cnorm, cmap=cm1)
cpick.set_array([])
cls = cpick.to_rgba(cls)
plt.colorbar(cpick, ax=fig.add_subplot(111))
nx.draw(g, pos=position, node_color=cls, ax=fig.add_subplot(111))
fig.savefig(os.path.join(self.plotdir, fig_name))
def sort_words(vertex_source, edge_source, model, window = 2, pagerank_config = {'alpha': 0.85,}):
"""??????????????
Keyword arguments:
vertex_source -- ???????????????????????????????pagerank????
edge_source -- ?????????????????????????????????pagerank???
window -- ????????window????????????
pagerank_config -- pagerank???
"""
#??????????
sorted_words = []
word_index = {}
index_word = {}
_vertex_source = vertex_source
_edge_source = edge_source
words_number = 0
for word_list in _vertex_source:
for word in word_list:
if not word in word_index:
word_index[word] = words_number
index_word[words_number] = word
words_number += 1
graph = np.zeros((words_number, words_number))
#???
for word_list in _edge_source:
for w1, w2 in combine(word_list, window):
if w1 in word_index and w2 in word_index:
index1 = word_index[w1]
index2 = word_index[w2]
try:
similarity = model.similarity(w1,w2)
if similarity<0:
similarity = 0
#print similarity
except:
similarity = 0
graph[index1][index2] = similarity
graph[index2][index1] = similarity
# graph[index1][index2] = 1.0
# graph[index2][index1] = 1.0
nx_graph = nx.from_numpy_matrix(graph)
scores = nx.pagerank(nx_graph, max_iter=100,**pagerank_config) # this is a dict
sorted_scores = sorted(scores.items(), key = lambda item: item[1], reverse=True)
for index, score in sorted_scores:
item = AttrDict(word=index_word[index], weight=score)
sorted_words.append(item)
return sorted_words
def mapper_graph(df, lens_data=None, lens='pca', resolution=10, gain=0.5, equalize=True, clust='kmeans', stat='db',
max_K=5):
"""
input: N x n_dim image of of raw data under lens function, as a dataframe
output: (undirected graph, list of node contents, dictionary of patches)
"""
if lens_data is None:
lens_data = apply_lens(df, lens=lens)
patch_clusterings = {}
counter = 0
patches = covering_patches(lens_data, resolution=resolution, gain=gain, equalize=equalize)
for key, patch in patches.items():
if len(patch) > 0:
patch_clusterings[key] = optimal_clustering(df, patch, method=clust, statistic=stat, max_K=max_K)
counter += 1
print 'total of {} patches required clustering'.format(counter)
all_clusters = []
for key in patch_clusterings:
all_clusters += patch_clusterings[key]
num_nodes = len(all_clusters)
print 'this implies {} nodes in the mapper graph'.format(num_nodes)
A = np.zeros((num_nodes, num_nodes))
for i in range(num_nodes):
for j in range(i):
overlap = set(all_clusters[i]).intersection(set(all_clusters[j]))
if len(overlap) > 0:
A[i, j] = 1
A[j, i] = 1
G = nx.from_numpy_matrix(A)
total = []
all_clusters_new = []
mapping = {}
cont = 0
for m in all_clusters:
total += m
for n, m in enumerate(all_clusters):
if len(m) == 1 and total.count(m) > 1:
G.remove_node(n)
else:
all_clusters_new.append(m)
mapping[n] = cont
cont += 1
H = nx.relabel_nodes(G, mapping)
return H, all_clusters_new, patches
def sort_words(vertex_source, edge_source, window = 2, pagerank_config = {'alpha': 0.85,}):
"""??????????????
Keyword arguments:
vertex_source -- ???????????????????????????????pagerank????
edge_source -- ?????????????????????????????????pagerank???
window -- ????????window????????????
pagerank_config -- pagerank???
"""
sorted_words = []
word_index = {}
index_word = {}
_vertex_source = vertex_source
_edge_source = edge_source
words_number = 0
for word_list in _vertex_source:
for word in word_list:
if not word in word_index:
word_index[word] = words_number
index_word[words_number] = word
words_number += 1
graph = np.zeros((words_number, words_number))
for word_list in _edge_source:
for w1, w2 in combine(word_list, window):
if w1 in word_index and w2 in word_index:
index1 = word_index[w1]
index2 = word_index[w2]
graph[index1][index2] = 1.0
graph[index2][index1] = 1.0
debug('graph:\n', graph)
nx_graph = nx.from_numpy_matrix(graph)
scores = nx.pagerank(nx_graph, **pagerank_config) # this is a dict
sorted_scores = sorted(scores.items(), key = lambda item: item[1], reverse=True)
for index, score in sorted_scores:
item = AttrDict(word=index_word[index], weight=score)
sorted_words.append(item)
return sorted_words
def Find_InterModular_Edge_correlativity(self):
# Induced graph is the data structure responsible for the adjacency matrix of the community
self.Matrix = nx.to_numpy_matrix(self.induced_graph)
# Matrix Before calculating the correlation strength
# finding out the lower half values of the matrix, can discard other values as computationally intensive
self.Matrix = np.tril(self.Matrix,-1)
i=0
Sum = 0
j=0
SumTemp = 0
Edges = 0
nodes1 = [item for item in self.Graphwidget.scene().items() if isinstance(item, Node)]
# ite1rateing over the indices
for community in set(self.partition.values()):
i= i + 1
j=0
for community2 in set(self.partition.values()):
j= j + 1
# Not Calculating the communities which are communities to itself
if community == community2:
continue
# Calculating the correlation strength only with the lower half of the adjacency matrix
if i <= j:
continue
# list_nodes1 and list_nodes2 indicate which nodes are actually present in these communties
list_nodes1 = [nodes for nodes in self.partition.keys() if self.partition[nodes] == community]
list_nodes2 = [nodes for nodes in self.partition.keys() if self.partition[nodes] == community2]
# Re-initializing the
SumTemp = 0
Edges = 0
for node1 in nodes1:
if node1.counter-1 in list_nodes1:
for node2 in nodes1:
if node2.counter-1 in list_nodes2:
if node1.counter-1 == node2.counter-1:
continue
if self.Graphwidget.Graph_data().ThresholdData[node1.counter-1][node2.counter-1] > 0:
Edges = Edges + 1
if Edges != 0:
Sum=float("{0:.2f}".format(self.Matrix[i-1,j-1]/Edges))
self.Matrix[i-1,j-1] = Sum
self.induced_graph = nx.from_numpy_matrix(self.Matrix)
def threshold_shortest_paths(mtx, treatment=False):
""" Threshold a graph via via shortest path identification using Dijkstra's algorithm.
.. [Dimitriadis2010] Dimitriadis, S. I., Laskaris, N. A., Tsirka, V., Vourkas, M., Micheloyannis, S., & Fotopoulos, S. (2010). Tracking brain dynamics via time-dependent network analysis. Journal of neuroscience methods, 193(1), 145-155.
Parameters
----------
mtx : array-like, shape(N, N)
Symmetric, weighted and undirected connectivity matrix.
treatment : boolean
Convert the weights to distances by inversing the matrix. Also,
fill the diagonal with zeroes. Default `false`.
Returns
-------
binary_mtx : array-like, shape(N, N)
A binary mask matrix.
"""
imtx = mtx
if treatment:
imtx = 1.0 / mtx
np.fill_diagonal(imtx, 0.0)
binary_mtx = np.zeros_like(imtx, dtype=np.int32)
graph = nx.from_numpy_matrix(imtx)
paths = dict(nx.all_pairs_dijkstra_path(graph))
N, _ = np.shape(mtx)
for x in range(N):
for y in range(N):
r_path = paths[x][y]
num_nodes = len(r_path)
ind1 = -1
ind2 = -1
for m in range(0, num_nodes - 1):
ind1 = ind1 + 1
ind2 = ind1 + 1
binary_mtx[r_path[ind1], r_path[ind2]] = 1
binary_mtx[r_path[ind2], r_path[ind1]] = 1
return binary_mtx