BERTopic icon indicating copy to clipboard operation
BERTopic copied to clipboard

datamap visulisation does not work.

Open Hansyvea opened this issue 1 year ago • 8 comments

other visualisation works except for this. It suggests BERTopic does not have this method.

Hansyvea avatar Apr 29 '24 05:04 Hansyvea

@Hansyvea Could you provide your error and sample code? Your #1955 does not provide details either.

semmyk-research avatar Apr 29 '24 13:04 semmyk-research

PS: I had a similar error on Colab last week. I presume you might be referring to this error or similar # "Data map plotting is unavailable unless datamapplot is installed." # datamapplot not installed

What works for me: Ensure you install and load datamapplot

## Install DataMapPlots: Below for Colab or if working inline within Notebook
!pip install datamapplot

## load library
    import datamapplot

You should be able to execute topic_model.visualize_document_datamap(docs, reduced_embeddings=reduced_embeddings) ## where reduced_embedding is already derived from your umap fit_transformed.

Should BERTopic datamap not work, you can attempt working with datamap directly. In my case, I called create_plot directly: PS: I also had a go at datamapplot.create_interactive_plot() with success.

datamapplot.create_plot(
    reduced_embeddings05,
    docs_info.Name,
...
...)

## where docs_info.Name or ocs_info['Name'] comes from topic_model_.get_document_info

semmyk-research avatar Apr 29 '24 13:04 semmyk-research

Also, make sure that you install BERTopic v0.16.1 (or higher when it releases) since it was supported first in v0.16.1. If BERTopic suggests that it does not have this function, then it is either what was previously stated or simply install the latest version.

MaartenGr avatar Apr 30 '24 07:04 MaartenGr

I also have issues getting datamapplot to work. Here is my code:

import datamapplot
print(bertopic.__version__)
0.16.1

topic_model.visualize_document_datamap(docs, embeddings=embeddings, title='', width=1200, height=1200,)

Error output:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/work/infomedia/sim_analysis.ipynb Cell 410 line 1
----> <a href='vscode-notebook-cell://app-5046147-0.cloud.sdu.dk/work/infomedia/sim_analysis.ipynb#Z1405sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a> topic_model.visualize_document_datamap(docs, embeddings=embeddings, title='', width=1200, height=1200,)

File [~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2431](https://vscode-remote+app-002d5046147-002d0-002ecloud-002esdu-002edk.vscode-resource.vscode-cdn.net/work/infomedia/~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2431), in BERTopic.visualize_document_datamap(self, docs, topics, embeddings, reduced_embeddings, custom_labels, title, sub_title, width, height, **datamap_kwds)
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2429'>2429</a> check_is_fitted(self)
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2430'>2430</a> check_documents_type(docs)
-> <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2431'>2431</a> return plotting.visualize_document_datamap(self,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2432'>2432</a>                                            docs,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2433'>2433</a>                                            topics,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2434'>2434</a>                                            embeddings,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2435'>2435</a>                                            reduced_embeddings,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2436'>2436</a>                                            custom_labels,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2437'>2437</a>                                            title,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2438'>2438</a>                                            sub_title,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2439'>2439</a>                                            width,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2440'>2440</a>                                            height,
   <a href='~/.local/lib/python3.10/site-packages/bertopic/_bertopic.py:2441'>2441</a>                                            **datamap_kwds)

File [~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:142](https://vscode-remote+app-002d5046147-002d0-002ecloud-002esdu-002edk.vscode-resource.vscode-cdn.net/work/infomedia/~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:142), in visualize_document_datamap(topic_model, docs, topics, embeddings, reduced_embeddings, custom_labels, title, sub_title, width, height, **datamap_kwds)
    <a href='~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:139'>139</a> # Map in topic names and plot
    <a href='~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:140'>140</a> named_topic_per_doc = pd.Series(topic_per_doc).map(topic_name_mapping).values
--> <a href='~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:142'>142</a> figure, axes = datamapplot.create_plot(
    <a href='~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:143'>143</a>     embeddings_2d,
...
    <a href='~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:149'>149</a>     **datamap_kwds,
    <a href='~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:150'>150</a> )
    <a href='~/.local/lib/python3.10/site-packages/bertopic/plotting/_datamap.py:152'>152</a> return figure

NameError: name 'datamapplot' is not defined

emilbakke avatar May 10 '24 12:05 emilbakke

@emilbakke Assuming datamapplot was correctly installed, it might be that you need to restart the notebook and try again. Not sure, but it might be that the try/except clause of the datamapplot import was already finished before installing datamapplot but that's a guess since I don't know the exact process.

MaartenGr avatar May 12 '24 18:05 MaartenGr

@emilbakke Assuming datamapplot was correctly installed, it might be that you need to restart the notebook and try again. Not sure, but it might be that the try/except clause of the datamapplot import was already finished before installing datamapplot but that's a guess since I don't know the exact process.

You are absolutely correct, solved it. The good old reboot. Thanks!

emilbakke avatar May 14 '24 14:05 emilbakke