<commit_before>"""Provides functions for performing a t-SNE reduction on audio fingerprint data"""
import os
from multiprocessing.pool import Pool

import numpy as np
from matplotlib import pyplot as plt
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA

from utils import *


def save_tsv(data: np.ndarray, file_name: str):
    """
    Save reduced data to a specified .tsv file.
    
    :param data: 2-dimensional numpy array containing reduced data
    :type data: numpy.ndarray
    :param file_name: File name of file to write
    :type file_name: str
    """
    np.savetxt(file_name, data, fmt="%.5f", delimiter="\t")


def t_sne(data: np.ndarray, no_dims: int = 3, args=None):
    if args:
        perplexity = args.perplexity
    else:
        perplexity = [30]
    if args and args.parallel:
        with Pool() as p:
            l_nd = list(p.map(t_sne_job, [(data, x, no_dims) for x in perplexity]))
    else:
        l_nd = []
        for p in perplexity:
            l_nd += t_sne_job((data, p, no_dims)), p
    if args and args.output_file:
        for x_nd in l_nd:
            save_tsv(x_nd[0], insert_suffix(args.output_file, x_nd[1]))
    return l_nd


def t_sne_job(params: tuple) -> tuple:
    """
    run r_sne fit transform on specified data in a specified way

    :param params: tuple of data, perplexity, output dimension
    :type params: tuple(numpy.ndarray, int, int)
    :return: typle of reduced data and perplexity
    :rtype: tuple(numpy.ndarray, str)
    """
    print("Running t-SNE with perplexity {}".format(params[1]))
    model = TSNE(n_components=params[2], perplexity=params[1], method='exact', verbose=2)
    return model.fit_transform(params[0]), str(params[1]), params[1]


def pca(output_dimensions: int, data: np.ndarray, args):
    model = PCA(n_components=output_dimensions, svd_solver='full')
    return model.fit_transform(data), "pca"<commit_msg>squashed bugs<commit_after>"""Provides functions for performing a t-SNE reduction on audio fingerprint data"""
import os
from multiprocessing.pool import Pool

import numpy as np
from matplotlib import pyplot as plt
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA

from utils import *


def save_tsv(data: np.ndarray, file_name: str):
    """
    Save reduced data to a specified .tsv file.
    
    :param data: 2-dimensional numpy array containing reduced data
    :type data: numpy.ndarray
    :param file_name: File name of file to write
    :type file_name: str
    """
    np.savetxt(file_name, data, fmt="%.5f", delimiter="\t")


def t_sne(data: np.ndarray, no_dims: int = 3, args=None):
    if args:
        perplexity = args.perplexity
    else:
        perplexity = [30]
    if args and args.parallel:
        with Pool() as p:
            l_nd = list(p.map(t_sne_job, [(data, x, no_dims) for x in perplexity]))
    else:
        l_nd = []
        for p in perplexity:
            l_nd.append(t_sne_job((data, p, no_dims)))
    return l_nd


def t_sne_job(params: tuple) -> tuple:
    """
    run r_sne fit transform on specified data in a specified way

    :param params: tuple of data, perplexity, output dimension
    :type params: tuple(numpy.ndarray, int, int)
    :return: typle of reduced data and perplexity
    :rtype: tuple(numpy.ndarray, str)
    """
    print("Running t-SNE with perplexity {}".format(params[1]))
    model = TSNE(n_components=params[2], perplexity=params[1], method='exact', verbose=2)
    return model.fit_transform(params[0]), str(params[1])


def pca(output_dimensions: int, data: np.ndarray, args):
    model = PCA(n_components=output_dimensions, svd_solver='full')
    return model.fit_transform(data), "pca"
{'fraction_non_alphanumeric': 0.10954828239112795, 'fraction_numerical': 0.0067622396537733295, 'mean_word_length': 3.3919239904988125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3941015', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1220, 'n_words': 378}