
    Wi                         d dl Zd dlmZmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ 	 	 	 	 	 	 	 	 ddee   dz  dedz  dedz  d	ed
eeef   dedededej*                  fdZy)    N)ListUnion)fclusterlinkage)cosine_similarity)select_topic_representationtopicstop_n_topics
n_clusters
use_ctfidfcustom_labelstitlewidthheightreturnc	                    t        | j                  | j                  |      d   | j                  d }	| j	                         }
|
j
                  |
j                  dk7  ddf   }
|t        |      }nL|'t        |
j                  j                         d|       }n#t        |
j                  j                               }|}|r|t        t        |            k\  rt        d      t        |	|         }t        |d      }t        ||d      }|D ci c]  }|g  }}t!        ||      D ]  \  }}||   j#                  |        |j%                         D cg c]  }| }}|D cg c]  }|D ]  }|  }}}t'        j(                  |D cg c]  }|j+                  |       c}      }|	|   }	t        |	      }t-        |t.              r|D cg c]#  }t/        |      dgg| j0                  |   |   z   % }}|D cg c]'  }dj3                  |dd	 D cg c]  }|d   	 c}      ) }}}|D cg c]  }t        |      d
k  r|n|dd dz    }}n| j4                  ,|r*|D cg c]  }| j4                  || j                  z        }}n|D cg c]!  }t/        |      dgg| j7                  |      # }}|D cg c]'  }dj3                  |dd	 D cg c]  }|d   	 c}      ) }}}|D cg c]  }t        |      d
k  r|n|dd dz    }}t9        j:                  |t=        d      ||d      }|j?                  | ddddt=        dd      d||t=        ddd             |j?                  d       |j?                  d !       |S c c}w c c}w c c}}w c c}w c c}w c c}w c c}}w c c}w c c}w c c}w c c}w c c}}w c c}w )"a   Visualize a heatmap of the topic's similarity matrix.

    Based on the cosine similarity matrix between topic embeddings (either c-TF-IDF or the embeddings from the embedding
    model), a heatmap is created showing the similarity between topics.

    Arguments:
        topic_model: A fitted BERTopic instance.
        topics: A selection of topics to visualize.
        top_n_topics: Only select the top n most frequent topics.
        n_clusters: Create n clusters and order the similarity
                    matrix by those clusters.
        use_ctfidf: Whether to calculate distances between topics based on c-TF-IDF embeddings. If False, the embeddings
                    from the embedding model are used.
        custom_labels: If bool, whether to use custom topic labels that were defined using
                       `topic_model.set_topic_labels`.
                       If `str`, it uses labels from other aspects, e.g., "Aspect1".
        title: Title of the plot.
        width: The width of the figure.
        height: The height of the figure.

    Returns:
        fig: A plotly figure

    Examples:
    To visualize the similarity matrix of
    topics simply run:

    ```python
    topic_model.visualize_heatmap()
    ```

    Or if you want to save the resulting figure:

    ```python
    fig = topic_model.visualize_heatmap()
    fig.write_html("path/to/file.html")
    ```
    <iframe src="../../getting_started/visualization/heatmap.html"
    style="width:1000px; height: 720px; border: 0px;""></iframe>
    r   NzKMake sure to set `n_clusters` lower than the total number of unique topics.wardmaxclust)t	criterion_         z...zSimilarity Score)colorGnBu)labelsxycolor_continuous_scalegffffff?g?centertop   Black)sizer   )textr    r   xanchoryanchorfontwhite   Rockwell)bgcolor	font_sizefont_family)r   r   r   
hoverlabelT)
showlegendTrend)legend_title_text) r   	c_tf_idf_topic_embeddings_	_outliersget_topic_freqlocTopiclistsortedto_listlenset
ValueErrorr   r   r   zipappendvaluesnparrayindex
isinstancestrtopic_aspects_joincustom_labels_	get_topicpximshowdictupdate_layout)topic_modelr	   r
   r   r   r   r   r   r   
embeddingsfreq_dfsorted_topicsdistance_matrixZclustersclustermappingtopicindices
new_labelsr   labelfigs                          f/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/plotting/_heatmap.pyvisualize_heatmapr`      s   f -[-B-BKDaDacmnopqJ
 ((*Gkk'--2-q01Gf		!--/>?--/0 MS[))jkk+Jv,>?OV,AzB /7777B;77!&(3 	+NE7G##E*	+*1..*:;w7;;.5K77K%KKK hhGU+GHGG$J'
3O -%an
X]c%j$ ;#=#=m#LU#SS

 
 R\\vchhfRajAUaAB\
\T^_5s5zBeE#2J4FF_
_		#	#	/M]jkTYk009N9N1NOk
kWdeeE
D)IK,A,A%,HIe
eQ[\vchhfRajAUaAB\
\T^_5s5zBeE#2J4FF_
_
)),-

%C gb0
 2:N   &0Jc 8 <K H
  B\_keA\_sf   
N	N$N
N(N!<N+N&	N+/N1##N6&N;:OO 	O-O&N+ O)NNNFFz<b>Similarity Matrix</b>   ra   )numpyrD   typingr   r   scipy.cluster.hierarchyr   r   sklearn.metrics.pairwiser   bertopic._utilsr   plotly.expressexpressrM   plotly.graph_objectsgraph_objectsgointboolrH   Figurer`        r_   <module>rq      s      5 6 7  !
  $#!&++}I} *} d
	}
 } s#} } } } YY}rp   