<commit_before>from typing import Optional

import pandas as pd
import plotly.figure_factory as ff
from plotly.graph_objs.graph_objs import Figure
from plotly.offline import plot
from scipy.cluster.hierarchy import linkage
from scipy.spatial.distance import pdist

from lexos.models.base_model import BaseModel
from lexos.models.matrix_model import MatrixModel
from lexos.receivers.dendro_receiver import DendroOption, DendroReceiver


class DendrogramModel(BaseModel):
    def __init__(self, test_dtm: Optional[pd.DataFrame] = None,
                 test_option: Optional[DendroOption] = None):
        """This is the class to generate dendrogram.

        :param test_dtm: (fake parameter)
                    the doc term matrix used of testing
        :param test_option: (fake parameter)
                    the dendrogram used for testing
        """
        super().__init__()
        self._test_dtm = test_dtm
        self._test_option = test_option

    @property
    def _doc_term_matrix(self) -> pd.DataFrame:

        return self._test_dtm if self._test_dtm is not None \
            else MatrixModel().get_matrix()

    @property
    def _dendro_option(self) -> DendroOption:

        return self._test_option if self._test_option is not None \
            else DendroReceiver().options_from_front_end()

    def _get_dendrogram_fig(self) -> Figure:
        """Generate a dendrogram figure object in plotly.

        :return: A plotly figure object
        """

        return ff.create_dendrogram(
            self._doc_term_matrix,
            orientation=self._dendro_option.orientation,

            distfun=lambda matrix: pdist(
                matrix, metric=self._dendro_option.dist_metric),

            linkagefun=lambda dist: linkage(
                dist, method=self._dendro_option.linkage_method),

            labels=self._doc_term_matrix.index.values
        )

    def get_dendrogram_div(self) -> str:
        """Generate the dendrogram div to send to the front end

        :return: a div
        """
        figure = self._get_dendrogram_fig()
        figure['layout'].update({'width': 800, 'height': 1000})

        div = plot(figure, show_link=False, output_type="div",
                   include_plotlyjs=False)

        return div
<commit_msg>only show the height of the branch on hover<commit_after>from typing import Optional

import pandas as pd
import plotly.figure_factory as ff
from plotly.graph_objs.graph_objs import Figure
from plotly.offline import plot
from scipy.cluster.hierarchy import linkage
from scipy.spatial.distance import pdist

from lexos.models.base_model import BaseModel
from lexos.models.matrix_model import MatrixModel
from lexos.receivers.dendro_receiver import DendroOption, DendroReceiver


class DendrogramModel(BaseModel):
    def __init__(self, test_dtm: Optional[pd.DataFrame] = None,
                 test_option: Optional[DendroOption] = None):
        """This is the class to generate dendrogram.

        :param test_dtm: (fake parameter)
                    the doc term matrix used of testing
        :param test_option: (fake parameter)
                    the dendrogram used for testing
        """
        super().__init__()
        self._test_dtm = test_dtm
        self._test_option = test_option

    @property
    def _doc_term_matrix(self) -> pd.DataFrame:

        return self._test_dtm if self._test_dtm is not None \
            else MatrixModel().get_matrix()

    @property
    def _dendro_option(self) -> DendroOption:

        return self._test_option if self._test_option is not None \
            else DendroReceiver().options_from_front_end()

    def _get_dendrogram_fig(self) -> Figure:
        """Generate a dendrogram figure object in plotly.

        :return: A plotly figure object
        """

        return ff.create_dendrogram(
            self._doc_term_matrix,
            orientation=self._dendro_option.orientation,

            distfun=lambda matrix: pdist(
                matrix, metric=self._dendro_option.dist_metric),

            linkagefun=lambda dist: linkage(
                dist, method=self._dendro_option.linkage_method),

            labels=self._doc_term_matrix.index.values
        )

    def get_dendrogram_div(self) -> str:
        """Generate the dendrogram div to send to the front end

        :return: a div
        """
        figure = self._get_dendrogram_fig()

        # update the style of the image
        figure['layout'].update({'width': 800, 'height': 1000,
                                 'hovermode': 'x'})

        div = plot(figure, show_link=False, output_type="div",
                   include_plotlyjs=False)

        return div

{'fraction_non_alphanumeric': 0.07716973065412569, 'fraction_numerical': 0.002992731936725096, 'mean_word_length': 2.507496251874063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1656278', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1326, 'n_words': 352}