gregversteeg / corex_topic

Hierarchical unsupervised and semi-supervised topic models for sparse count data with CorEx
Apache License 2.0
627 stars 120 forks source link

Can't visualize use vis_hierarchy() #26

Open AlexanderZhujiageng opened 5 years ago

AlexanderZhujiageng commented 5 years ago

I try to run the example code and vis_hierarchy() I have download the force.html according** to issue 19 But When I open the force.html, the page is whole blank and I inspect the element. In console it says: ncaught TypeError: Cannot read property 'push' of undefined at t (d3.v2.min.js?2.9.3:3) at e (d3.v2.min.js?2.9.3:3) at Object.n.start (d3.v2.min.js?2.9.3:3) at force.html:34 at d3.v2.min.js?2.9.3:2 at r (d3.v2.min.js?2.9.3:2) at XMLHttpRequest.r.onreadystatechange (d3.v2.min.js?2.9.3:2)

Seems that the src used in force.html has some problem How can I solve this Thanks a lot

gregversteeg commented 5 years ago

These stopped working for me too. I'm not sure why. It uses libraries from https://d3js.org. I'm not sure if browser standard have changed or if the source code (which resides on d3js.org and is called from force.html) has changed somehow. If anyone has an idea please let me know

gwpicard commented 4 years ago

I have been playing around with this library and may have figured out the issue... It seems that D3 requires that the source/target attributes for the links generated by the networkx .json need to match the ID attributes of the nodes in the .json graph. However, in this line: mapping = dict([(n, tree_nodes[n].get('label', str(n))) for n in tree.nodes()]) The IDs generated are a mix of strings and "edge tuples", whereas the links in the .json refer to int values as source/target values. To fix this, I had to replace the above line with: mapping = dict([(n, id_) for id_, n in enumerate(tree.nodes()]) in order to give every node a unique int ID that matches the format of the source/target references in the .json links. You lose the strings labels as a result but that can be fixed manually. Hope that helps!

ryanjgallagher commented 4 years ago

@gwpicard Thanks for taking a look at this! Would you be able to put together a quick pull request for that change if it seems like it's working?

I haven't used the visualization code as much as @gregversteeg, so I don't want to accidentally change the wrong line here.

mawic commented 3 years ago

I have the same problem. But @gwpicard 's fix doesn't help. Is there any fix yet? @gwpicard : Do you mind to provide your source code of vis_topic.py

gregversteeg commented 3 years ago

Ok, I think I understand the issue. The networkx interface changed so that g.node became g._node (and g.node still works but gives the wrong thing! which messes up the graphs.) I updated vis_corex. Could someone try it and see if it works?

gwpicard commented 3 years ago

Hey everyone, sorry only reading these now. I had a version of the code I was hoping to create a PR for but unfortunately, it's gone. However, given was @gregversteeg said, that should probably do the trick as my fix was a little hacky. @gregversteeg I haven't tested it but might do in the next couple of days when I have time. Haven't been using the library in a number of months.

mawic commented 3 years ago

Thanks for the quick response. Unfortunately, I get still the following error (in Chrome), if I open force.html (I copied the file from the repo you mentioned in the other comment):

force.html:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at d3.v2.min.js?2.9.3:2 at r (d3.v2.min.js?2.9.3:2) at XMLHttpRequest.r.onreadystatechange (d3.v2.min.js?2.9.3:2)

I'm using python==3.8.3 and networkx==2.4.

fancyqi commented 3 years ago

Hey, I cannot run both the visualization code in the example . the vis_rep() function throw error like this. image

the vis_hierarchy() function throw error like this. image how should I change the code and get it right. Thanks.

ydennisy commented 3 years ago

+1 on seeing various errors when using the viz part of the lib:

vt.vis_hierarchy([topic_model, tm_layer2, tm_layer3, tm_layer4], column_label=words, max_edges=300, prefix='topic-model-example')

Gives:

weight threshold is 0.000000 for graph with max of 300.000000 edges 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-5b099abc6ed3> in <module>
----> 1 vt.vis_hierarchy([topic_model, tm_layer2, tm_layer3, tm_layer4], column_label=words, max_edges=300, prefix='topic-model-example')

/opt/conda/lib/python3.7/site-packages/corextopic/vis_topic.py in vis_hierarchy(corexes, column_label, max_edges, prefix, n_anchors)
     66     weights = [corex.alpha.clip(0, 1) * corex.mis for corex in corexes[1:]]
     67     node_weights = [corex.tcs for corex in corexes[1:]]
---> 68     g = make_graph(weights, node_weights, l1_labels, max_edges=max_edges)
     69 
     70     # Display pruned version

/opt/conda/lib/python3.7/site-packages/corextopic/vis_topic.py in make_graph(weights, node_weights, column_label, max_edges)
    137         for j in range(m):
    138             g.add_node((layer + 1, j))
--> 139             g.node[(layer + 1, j)]['weight'] = 0.3 * node_weights[layer][j] / max_node_weight
    140             for i in range(n):
    141                 if weight[j, i] > w_thresh:

AttributeError: 'DiGraph' object has no attribute 'node'

Also

from corextopic import vis_topic as vt
vt.vis_rep(topic_model, column_label=words, prefix='topic-model-example')

Gives

Print topics in text file
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-4af8b3811710> in <module>
      1 from corextopic import vis_topic as vt
----> 2 vt.vis_rep(topic_model, column_label=words, prefix='topic-model-example')

/opt/conda/lib/python3.7/site-packages/corextopic/vis_topic.py in vis_rep(corex, data, row_label, column_label, prefix)
     34 
     35     print('Print topics in text file')
---> 36     output_groups(corex.tcs, alpha, corex.mis, column_label, corex.sign, prefix=prefix)
     37     output_labels(corex.labels, row_label, prefix=prefix)
     38     output_cont_labels(corex.p_y_given_x, row_label, prefix=prefix)

/opt/conda/lib/python3.7/site-packages/corextopic/vis_topic.py in output_groups(tcs, alpha, mis, column_label, direction, thresh, prefix)
    181         inds = inds[np.argsort(-alpha[j, inds] * mis[j, inds])]
    182         for ind in inds:
--> 183             f.write(column_label[ind] + u', %0.3f, %0.3f, %0.3f\n' % (
    184                 mis[j, ind], alpha[j, ind], mis[j, ind] * alpha[j, ind]))
    185         #h.write(unicode(j) + u':' + u','.join([annotate(column_label[ind], direction[j,ind]) for ind in inds[:10]]) + u'\n')

IndexError: tuple index out of range
kellyann88 commented 2 years ago

+2 As with above users - i have exactly the same errors -is there a fix available please?:


IndexError Traceback (most recent call last)

in ----> 1 vt.vis_hierarchy([OHT_topic_model, tm_layer2, tm_layer3], column_label=words, max_edges=300, prefix='topic-model-example') ~/anaconda3/lib/python3.7/site-packages/corextopic/vis_topic.py in vis_hierarchy(corexes, column_label, max_edges, prefix, n_anchors) 59 inds = inds[np.argsort(-alpha[j, inds] * mis[j, inds])] 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]]) 62 label = textwrap.fill(label, width=25) 63 l1_labels.append(label) Is there a fix for this available please? ~/anaconda3/lib/python3.7/site-packages/corextopic/vis_topic.py in (.0) 59 inds = inds[np.argsort(-alpha[j, inds] * mis[j, inds])] 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]]) 62 label = textwrap.fill(label, width=25) 63 l1_labels.append(label) IndexError: tuple index out of range
gregversteeg commented 2 years ago

I will have a closer look next week.