<commit_before>"""
The main class for Nexa will be here.
"""
import numpy as np
from sklearn import manifold, cluster

from inputs.sensors import PerceptualSpace


class Nexa():
    """
    This is the class for Nexa.
    """

    n_init = 10
    n_jobs = -1  # -1 To use all CPUs, 1 for only one

    def __init__(self, sensors, Nspatial_clusters,
                 Ntime_clusters, Nembedding, SLM=None, lags_first=True):
        """
        Describe the parameters
        """

        self.Nspatial_clusters = Nspatial_clusters
        self.Ntime_clusters = Ntime_clusters
        self.Nembedding = Nembedding

        # Check that sensors are a PerceptualSpace instance
        # To do: Make this a try statmeent
        if isinstance(sensors, PerceptualSpace):
            self.sensors = sensors
        else:
            raise TypeError("Sensors should be perceptual space")
            self.sensors = PerceptualSpace(sensors, self.NLags, lags_first)

        self.lags_first = self.sensors.lag_first

        if SLM is None:
            self.SLM = self.sensors.calculate_SLM()

        # Initiate values for the methods
        self.STDM = None
        self.embedding = None
        self.index_to_cluster = None
        self.cluster_to_index = None
        self.cluster_to_time_centers = None
        self.dimensions_of_embedding_space = None

    def calculate_distance_matrix(self):
        self.STDM = self.sensors.calculate_STDM()

    def calculate_embedding(self):
        """
        This calculates the euclidian embedding of our
        distance matrix using MDS.

        Calculates the embedding, that is, it embedds every
        sensor on an Euclidian space with dimensions equal to
        self.Nembedding. Threfore it should return an array
        with a shape = (self.Nsensors, self.Nembedding).

        To Do: assertion for STDM not previously calculated
        """
        disimi = 'precomputed'
        n_init = Nexa.n_init
        n_jobs = Nexa.n_jobs
        n_comp = self.Nembedding

        classifier = manifold.MDS(n_components=n_comp, n_init=n_init,
                                  n_jobs=n_jobs, dissimilarity=disimi)

        self.embedding = classifier.fit_transform(1 - self.STDM)

        return classifier.stress_

    def calculate_spatial_clustering(self, centers=False):
        """
        This class calculates the spatial clustering. Once there is
        an embedding this function performs a clustering in the
        embedded space with as many clusters as self.Nspatial_clusters

        It returns an index to cluster which maps every sensor to the
        cluster (via a number) that it belongs to.
        """

        n_jobs = Nexa.n_jobs
        n_clusters = self.Nspatial_clusters

        classifier = cluster.KMeans(n_clusters=n_clusters, n_jobs=n_jobs)
        self.index_to_cluster = classifier.fit_predict(self.embedding)

        if centers:
            self.spatial_cluster_centers = classifier.cluster_centers_

    def calculate_cluster_to_indexes(self):
        """<commit_msg>Changed the way the the MDS calculated the distance, adding the absolute value<commit_after>"""
The main class for Nexa will be here.
"""
import numpy as np
from sklearn import manifold, cluster

from inputs.sensors import PerceptualSpace


class Nexa():
    """
    This is the class for Nexa.
    """

    n_init = 10
    n_jobs = -1  # -1 To use all CPUs, 1 for only one

    def __init__(self, sensors, Nspatial_clusters,
                 Ntime_clusters, Nembedding, SLM=None, lags_first=True):
        """
        Describe the parameters
        """

        self.Nspatial_clusters = Nspatial_clusters
        self.Ntime_clusters = Ntime_clusters
        self.Nembedding = Nembedding

        # Check that sensors are a PerceptualSpace instance
        # To do: Make this a try statmeent
        if isinstance(sensors, PerceptualSpace):
            self.sensors = sensors
        else:
            raise TypeError("Sensors should be perceptual space")
            self.sensors = PerceptualSpace(sensors, self.NLags, lags_first)

        self.lags_first = self.sensors.lag_first

        if SLM is None:
            self.SLM = self.sensors.calculate_SLM()

        # Initiate values for the methods
        self.STDM = None
        self.embedding = None
        self.index_to_cluster = None
        self.cluster_to_index = None
        self.cluster_to_time_centers = None
        self.dimensions_of_embedding_space = None

    def calculate_distance_matrix(self):
        self.STDM = self.sensors.calculate_STDM()

    def calculate_embedding(self):
        """
        This calculates the euclidian embedding of our
        distance matrix using MDS.

        Calculates the embedding, that is, it embedds every
        sensor on an Euclidian space with dimensions equal to
        self.Nembedding. Threfore it should return an array
        with a shape = (self.Nsensors, self.Nembedding).

        To Do: assertion for STDM not previously calculated
        """
        disimi = 'precomputed'
        n_init = Nexa.n_init
        n_jobs = Nexa.n_jobs
        n_comp = self.Nembedding

        classifier = manifold.MDS(n_components=n_comp, n_init=n_init,
                                  n_jobs=n_jobs, dissimilarity=disimi)

        self.embedding = classifier.fit_transform(1 - np.abs(self.STDM))

        return classifier.stress_

    def calculate_spatial_clustering(self, centers=False):
        """
        This class calculates the spatial clustering. Once there is
        an embedding this function performs a clustering in the
        embedded space with as many clusters as self.Nspatial_clusters

        It returns an index to cluster which maps every sensor to the
        cluster (via a number) that it belongs to.
        """

        n_jobs = Nexa.n_jobs
        n_clusters = self.Nspatial_clusters

        classifier = cluster.KMeans(n_clusters=n_clusters, n_jobs=n_jobs)
        self.index_to_cluster = classifier.fit_predict(self.embedding)

        if centers:
            self.spatial_cluster_centers = classifier.cluster_centers_

    def calculate_cluster_to_indexes(self):
        """
{'fraction_non_alphanumeric': 0.064882400648824, 'fraction_numerical': 0.0019464720194647203, 'mean_word_length': 2.354733405875952, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4348697', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1718, 'n_words': 514}