<commit_before># -*- coding: utf-8 -*-
from os.path import join as path_join
from typing import Union

import matplotlib.pyplot as plt
import numpy as np
from sklearn import metrics
from sklearn.cluster import KMeans as KMeans
from sklearn.decomposition import PCA

from lexos.helpers.constants import KMEANS_GRAPH_FILENAME, \
    PCA_SMALL_GRAPH_FILENAME, PCA_BIG_GRAPH_FILENAME, ROUND_DIGIT


def translate_points_to_positive(xs, ys, trans_x, trans_y):
    coord_list = []
    for i in range(0, len(xs)):
        xs[i] += trans_x
        ys[i] += trans_y
        coord_list.append([xs[i], ys[i]])

    return coord_list


def translate_coords_to_positive(xs, ys, trans_x, trans_y):
    for i in range(0, len(xs)):
        xs[i] += trans_x
        ys[i] += trans_y
    return xs, ys


def translate_centroids_to_positive(coords, trans_x, trans_y):
    coord_list = []
    for i in range(0, len(coords)):
        coords[i][0] += trans_x
        coords[i][1] += trans_y
        coord_list.append([coords[i][0], coords[i][1]])

    return coord_list


def text_attrs_dictionary(title, x, y):
    attr_dict = {"x": x, "y": y, "title": title}
    return attr_dict


def _get_silhouette_score_(k: int, matrix: np.ndarray, k_means: KMeans,
                           metric_dist: str) -> Union[str, float]:
    """Generates silhouette score based on the KMeans algorithm.

    This function returns a proper message if it is under the condition where
    it cannot perform the calculation on finding silhouette score
    :param k: k-value for k-means analysis
    :param matrix: a 2D numpy matrix that contains word counts
    :param k_means: a KMeans class object
    :param metric_dist: method of the distance metric
    :return: the calculated silhouette score or a proper message if the
             conditions for calculation were not met
    """
    if k <= 2:
        return "N/A [Not available for K ≤ 2]"
    elif k > (matrix.shape[0] - 1):
        return "N/A [Not available if (K value) > (number of active files -1)]"
    else:
        labels = k_means.fit(matrix).labels_
        silhouette_score = metrics.silhouette_score(X=matrix,
                                                    labels=labels,
                                                    metric=metric_dist)
        return round(silhouette_score, ROUND_DIGIT)


def _get_voronoi_plot_data_(data: np.ndarray,
                            group_index: np.ndarray) -> np.ndarray:
    """Generate the data needed to be plotted in voronoi analysis method.<commit_msg>Delete unused functions<commit_after># -*- coding: utf-8 -*-
from os.path import join as path_join
from typing import Union

import matplotlib.pyplot as plt
import numpy as np
from sklearn import metrics
from sklearn.cluster import KMeans as KMeans
from sklearn.decomposition import PCA

from lexos.helpers.constants import KMEANS_GRAPH_FILENAME, \
    PCA_SMALL_GRAPH_FILENAME, PCA_BIG_GRAPH_FILENAME, ROUND_DIGIT


def _get_silhouette_score_(k: int, matrix: np.ndarray, k_means: KMeans,
                           metric_dist: str) -> Union[str, float]:
    """Generates silhouette score based on the KMeans algorithm.

    This function returns a proper message if it is under the condition where
    it cannot perform the calculation on finding silhouette score
    :param k: k-value for k-means analysis
    :param matrix: a 2D numpy matrix that contains word counts
    :param k_means: a KMeans class object
    :param metric_dist: method of the distance metric
    :return: the calculated silhouette score or a proper message if the
             conditions for calculation were not met
    """
    if k <= 2:
        return "N/A [Not available for K ≤ 2]"
    elif k > (matrix.shape[0] - 1):
        return "N/A [Not available if (K value) > (number of active files -1)]"
    else:
        labels = k_means.fit(matrix).labels_
        silhouette_score = metrics.silhouette_score(X=matrix,
                                                    labels=labels,
                                                    metric=metric_dist)
        return round(silhouette_score, ROUND_DIGIT)


def _get_voronoi_plot_data_(data: np.ndarray,
                            group_index: np.ndarray) -> np.ndarray:
    """Generate the data needed to be plotted in voronoi analysis method.
{'fraction_non_alphanumeric': 0.08099326990020886, 'fraction_numerical': 0.004873520538407983, 'mean_word_length': 2.6066945606694563, 'pattern_counts': {'":': 3, '<': 5, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '345705', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1274, 'n_words': 436}