Skip to content

Commit 7267678

Browse files
authored
Merge pull request #21 from vtrokhymenko/master
improved code to python3+
2 parents e1ad66d + d94a06e commit 7267678

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

corextopic/vis_topic.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def vis_hierarchy(corexes, column_label=None, max_edges=100, prefix='topics', n_
5757
# inds = np.where(alpha[j] * mis[j] > 0)[0]
5858
inds = np.where(alpha[j] >= 1.)[0]
5959
inds = inds[np.argsort(-alpha[j, inds] * mis[j, inds])]
60-
group_number = u"red_" + unicode(j) if j < n_anchors else unicode(j)
61-
label = group_number + u':' + u' '.join([annotate(column_label[ind], corexes[0].sign[j,ind]) for ind in inds[:6]])
60+
group_number = str('red_') + str(j) if j < n_anchors else str(j)
61+
label = group_number + ':' + ' '.join([annotate(column_label[ind], corexes[0].sign[j,ind]) for ind in inds[:6]])
6262
label = textwrap.fill(label, width=25)
6363
l1_labels.append(label)
6464

@@ -79,7 +79,8 @@ def vis_hierarchy(corexes, column_label=None, max_edges=100, prefix='topics', n_
7979
# Output JSON files
8080
try:
8181
import os
82-
copyfile(os.path.dirname(os.path.realpath(__file__)) + '/tests/d3_files/force.html', prefix + '/graphs/force.html')
82+
#copyfile(os.path.dirname(os.path.realpath(__file__)) + '/tests/d3_files/force.html', prefix + '/graphs/force.html')
83+
copyfile(os.path.dirname(os.path.realpath('tests')) + '/tests/d3_files/force.html', prefix + '/graphs/force.html')
8384
except:
8485
print("Couldn't find 'force.html' file for visualizing d3 output")
8586
import json
@@ -156,7 +157,8 @@ def trim(g, max_parents=False, max_children=False):
156157
for node in g:
157158
if max_parents:
158159
parents = list(g.successors(node))
159-
weights = [g.edge[node][parent]['weight'] for parent in parents]
160+
#weights = [g.edge[node][parent]['weight'] for parent in parents]
161+
weights = [g.adj[node][parent]['weight'] for parent in parents]
160162
for weak_parent in np.argsort(weights)[:-max_parents]:
161163
g.remove_edge(node, parents[weak_parent])
162164
if max_children:
@@ -290,42 +292,46 @@ def edge2pdf(g, filename, threshold=0, position=None, labels=None, connected=Tru
290292
def cnn(node):
291293
#change node names for dot format
292294
if type(node) is tuple or type(node) is list:
293-
return u'n' + u'_'.join(list(map(unicode, node)))
295+
#return u'n' + u'_'.join(list(map(unicode, node)))
296+
return u'n' + u'_'.join(list(map(str, node)))
294297
else:
295-
return unicode(node)
298+
return node
296299

297300
if connected:
298301
touching = list(set(sum([[a, b] for a, b in g.edges()], [])))
299302
g = nx.subgraph(g, touching)
300303
print('non-isolated nodes,edges', len(list(g.nodes())), len(list(g.edges())))
301304
f = safe_open(filename + '.dot', 'w+')
305+
#print('f1->',f)
306+
#print('directed->',directed)
302307
if directed:
303-
f.write("strict digraph {\n".encode('utf-8'))
308+
f.write("strict digraph {\n")#.decode("utf-8")
309+
#f.write('strict digraph {')
304310
else:
305-
f.write("strict graph {\n".encode('utf-8'))
311+
f.write("strict graph {")
306312
#f.write("\tgraph [overlap=scale];\n".encode('utf-8'))
307-
f.write("\tnode [shape=point];\n".encode('utf-8'))
313+
f.write("\tnode [shape=point];\n")
308314
for a, b, d in g.edges(data=True):
309-
if d.has_key('weight'):
315+
if 'weight' in d:
310316
if directed:
311317
f.write(("\t" + cnn(a) + ' -> ' + cnn(b) + ' [penwidth=%.2f' % float(
312-
np.clip(d['weight'], 0, 9)) + '];\n').encode('utf-8'))
318+
np.clip(d['weight'], 0, 9)) + '];\n'))
313319
else:
314320
if d['weight'] > threshold:
315-
f.write(("\t" + cnn(a) + ' -- ' + cnn(b) + ' [penwidth=' + str(3 * d['weight']) + '];\n').encode(
316-
'utf-8'))
321+
f.write(("\t" + cnn(a) + ' -- ' + cnn(b) + ' [penwidth=' + str(3 * d['weight']) + '];\n'))
317322
else:
318323
if directed:
319-
f.write(("\t" + cnn(a) + ' -> ' + cnn(b) + ';\n').encode('utf-8'))
324+
f.write(("\t" + cnn(a) + ' -> ' + cnn(b) + ';\n'))
320325
else:
321-
f.write(("\t" + cnn(a) + ' -- ' + cnn(b) + ';\n').encode('utf-8'))
326+
f.write(("\t" + cnn(a) + ' -- ' + cnn(b) + ';\n'))
322327
for n in g.nodes():
323328
if labels is not None:
324329
if type(labels) == dict or type(labels) == list:
325330
thislabel = labels[n].replace(u'"', u'\\"')
326331
lstring = u'label="' + thislabel + u'",shape=none'
327332
elif type(labels) == str:
328-
if g.node[n].has_key('label'):
333+
#if g.node[n].has_key('label'):
334+
if 'label' in g.node[n]:
329335
thislabel = g.node[n][labels].replace(u'"', u'\\"')
330336
# combine dupes
331337
#llist = thislabel.split(',')
@@ -341,7 +347,7 @@ def cnn(node):
341347
lstring = u'shape=point,height=%0.2f' % weight
342348
else:
343349
lstring = 'label="' + str(n) + '",shape=none'
344-
lstring = unicode(lstring)
350+
lstring = str(lstring)
345351
else:
346352
lstring = False
347353
if position is not None:
@@ -353,7 +359,7 @@ def cnn(node):
353359
finalstring = u' [' + u','.join([ts for ts in [posstring, lstring] if ts]) + u']\n'
354360
#finalstring = u' ['+lstring+u']\n'
355361
f.write(u'\t' + cnn(n) + finalstring)
356-
f.write("}".encode('utf-8'))
362+
f.write("}")
357363
f.close()
358364
if makepdf:
359365
neato(filename, position=position, directed=directed)

0 commit comments

Comments
 (0)