# Path: hub_toolbox/distances.py
# def cosine_distance(X):
#     """Calculate the cosine distance between all pairs of vectors in `X`."""
#     xn = np.sqrt(np.sum(X**2, 1))
#     Y = X / xn[:, np.newaxis]
#     del xn
#     D = 1. - Y.dot(Y.T)
#     del Y
#     D[D < 0] = 0
#     D = np.triu(D, 1) + np.triu(D, 1).T
#     return D
# 
# Path: hub_toolbox/io.py
# def load_dexter():
# def check_is_nD_array(arr:np.ndarray, n:int, arr_type=''):
# def check_distance_matrix_shape(D:np.ndarray):
# def check_distance_matrix_shape_fits_vectors(D:np.ndarray, vectors:np.ndarray):
# def check_distance_matrix_shape_fits_labels(D:np.ndarray, classes:np.ndarray):
# def check_vector_matrix_shape_fits_labels(X:np.ndarray, classes:np.ndarray):
# def check_sample_shape_fits(D:np.ndarray, idx:np.ndarray):
# def check_valid_metric_parameter(metric:str):
# def matrix_split(rows, cols, elem_size=8, nr_matrices=4): # pragma: no cover
# def random_sparse_matrix(size, density=0.05):
# def save_csr_matrix(file, matrix):
# def load_csr_matrix(file):
#     def __init__(self, unit='kB'):
#     def _factor(self):
#     def total(self):
#     def used(self):
#     def used_real(self):
#     def shared(self):
#     def buffers(self):
#     def cached(self):
#     def user_free(self):
#     def swap(self):
#     def swap_free(self):
#     def swap_used(self):
#     D = cosine_distance(vectors)
#     S = sparse.rand(size, size, density, 'csr')
# class FreeMemLinux(object): # pragma: no cover


import ctypes
import numpy as np
from multiprocessing import cpu_count, Pool, RawArray
from sklearn.metrics.pairwise import euclidean_distances
from hub_toolbox.distances import cosine_distance as cos
from hub_toolbox import io
from functools import partial

    test_set_mask : ndarray, optional (default: None)
        Hold back data as a test set and perform centering on the remaining 
        data (training set).

    Returns
    ------- 
    X_cent : ndarray

        Centered vectors with shape (n, m), if given vector data.

    K_cent : ndarray

        Centered inner product similarities with shape (n, n), if given Gram matrix.
        
    References
    ----------
    .. [1] Suzuki, I., Hara, K., Shimbo, M., Saerens, M., & Fukumizu, K. (2013). 
           Centering similarity measures to reduce hubs. In Proceedings of the 
           2013 Conference on Empirical Methods in Natural Language Processing 
           (pp 613–623). 
           Retrieved from https://www.aclweb.org/anthology/D/D13/D13-1058.pdf
    """
    # Kernel based centering requires inner product similarities, NOT distances.
    # Since the parameter was previously erroneously called 'distance',
    # this is kept for compatibility reasons.
    if metric in ('similarity', 'distance', 'inner', 'inner_product'):
        if test_set_mask is not None:
            raise NotImplementedError("Kernel based centering does not "
                                      "support train/test splits so far.")

# Path: hub_toolbox/distances.py
# def cosine_distance(X):
#     """Calculate the cosine distance between all pairs of vectors in `X`."""
#     xn = np.sqrt(np.sum(X**2, 1))
#     Y = X / xn[:, np.newaxis]
#     del xn
#     D = 1. - Y.dot(Y.T)
#     del Y
#     D[D < 0] = 0
#     D = np.triu(D, 1) + np.triu(D, 1).T
#     return D
# 
# Path: hub_toolbox/io.py
# def load_dexter():
# def check_is_nD_array(arr:np.ndarray, n:int, arr_type=''):
# def check_distance_matrix_shape(D:np.ndarray):
# def check_distance_matrix_shape_fits_vectors(D:np.ndarray, vectors:np.ndarray):
# def check_distance_matrix_shape_fits_labels(D:np.ndarray, classes:np.ndarray):
# def check_vector_matrix_shape_fits_labels(X:np.ndarray, classes:np.ndarray):
# def check_sample_shape_fits(D:np.ndarray, idx:np.ndarray):
# def check_valid_metric_parameter(metric:str):
# def matrix_split(rows, cols, elem_size=8, nr_matrices=4): # pragma: no cover
# def random_sparse_matrix(size, density=0.05):
# def save_csr_matrix(file, matrix):
# def load_csr_matrix(file):
#     def __init__(self, unit='kB'):
#     def _factor(self):
#     def total(self):
#     def used(self):
#     def used_real(self):
#     def shared(self):
#     def buffers(self):
#     def cached(self):
#     def user_free(self):
#     def swap(self):
#     def swap_free(self):
#     def swap_used(self):
#     D = cosine_distance(vectors)
#     S = sparse.rand(size, size, density, 'csr')
# class FreeMemLinux(object): # pragma: no cover


# Path: hub_toolbox/centering.py
import ctypes
import numpy as np
from multiprocessing import cpu_count, Pool, RawArray
from sklearn.metrics.pairwise import euclidean_distances
from hub_toolbox.distances import cosine_distance as cos
from hub_toolbox import io
from functools import partial


    test_set_mask : ndarray, optional (default: None)
        Hold back data as a test set and perform centering on the remaining 
        data (training set).

    Returns
    ------- 
    X_cent : ndarray

        Centered vectors with shape (n, m), if given vector data.

    K_cent : ndarray

        Centered inner product similarities with shape (n, n), if given Gram matrix.
        
    References
    ----------
    .. [1] Suzuki, I., Hara, K., Shimbo, M., Saerens, M., & Fukumizu, K. (2013). 
           Centering similarity measures to reduce hubs. In Proceedings of the 
           2013 Conference on Empirical Methods in Natural Language Processing 
           (pp 613–623). 
           Retrieved from https://www.aclweb.org/anthology/D/D13/D13-1058.pdf
    """
    # Kernel based centering requires inner product similarities, NOT distances.
    # Since the parameter was previously erroneously called 'distance',
    # this is kept for compatibility reasons.
    if metric in ('similarity', 'distance', 'inner', 'inner_product'):
        if test_set_mask is not None:
            raise NotImplementedError("Kernel based centering does not "
                                      "support train/test splits so far.")

        io.check_distance_matrix_shape(X)