<commit_before>'''
Created on Jan 5, 2014

@author: noe
'''

import emma2.util.pystallone as stallone
import util
import general_transform
# shortcuts
CoordinateTransform = general_transform.CoordinateTransform

class PCA(CoordinateTransform):
    """
    Wrapper class to stallone PCA
    """
       
    def __init__(self, jpca):
        CoordinateTransform.__init__(self,jpca)
        self.self_evec = None
        self._ndim = None
    
    def mean(self):
        """
        Returns the mean vector estimated from the data
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getMeanVector())

    def covariance_matrix(self):
        """
        Returns the covariance matrix estimated from the data
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getCovarianceMatrix())
    
    def eigenvalues(self):
        """
        Returns the eigenvalues of the covariance matrix
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getEigenvalues())
    
    def eigenvector(self, i):
        """
        Returns the i'th largest eigenvector of the covariance matrix
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getEigenvector(i))
    
    def eigenvectors(self):
        """
        Returns the eigenvector matrix of the covariance matrix
        with eigenvectors as column vectors
        """
        if (self._evec is None):
            self._evec = stallone.stallone_array_to_ndarray(self.jtransform().getEigenvectorMatrix())
        return self._evec
    
    def set_dimension(self, d):
        """
        Sets the dimension for the projection. If not set, no dimension
        reduction will be made, i.e. the output data is transformed but
        has the same dimension as the input data.
        """
        self._ndim = d
        self.jtransform().setDimension(d)
    
    def transform(self, x):
        """
        Performs a linear transformation (and possibly dimension reduction
        if set_dimension has been used) of the input vector x to the 
        coordinate system defined by the covariance matrix eigenvectors.
        """
        return util.project(x.flatten(), self.eigenvectors(), self._ndim)
    
    def __has_efficient_transform(self):
        """
        Returns True
        """
        return True

class PCA_AMUSE(PCA):
    
    def __init__(self, amuse):
        self.amuse = amuse
        
    def mean(self):
        return self.amuse.mean
    
    def covariance_matrix(self):
        return self.amuse.cov
    
    def eigenvalues(self):
        return self.amuse.pca_values
    
    def eigenvector(self, i):
        return self.amuse.pca_weights[i]
    
    def eigenvectors(self):
        return self.amuse.pca_weights
    
    def set_dimension(self, d):
        self.d = d
        
    def transform(self, x):
        return util.project(x.flatten(), self.eigenvectors(), self.d)

class TICA(PCA):
    """
    Wrapper class to stallone TICA
    """
    
    def __init__(self, jtica):
        PCA.__init__(self,jtica)
    
    def covariance_matrix_lagged(self):
        """<commit_msg>[coordinates/transform] * added jtransform() override in PCA_AMUSE and use linear operator from stallone.                         * fix bug due to misspelled variable.<commit_after>'''
Created on Jan 5, 2014

@author: noe
'''

import emma2.util.pystallone as stallone
import util
import general_transform
# shortcuts
CoordinateTransform = general_transform.CoordinateTransform

class PCA(CoordinateTransform):
    """
    Wrapper class to stallone PCA
    """
       
    def __init__(self, jpca):
        CoordinateTransform.__init__(self,jpca)
        self._evec = None
        self.ndim = None
    
    def mean(self):
        """
        Returns the mean vector estimated from the data
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getMeanVector())

    def covariance_matrix(self):
        """
        Returns the covariance matrix estimated from the data
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getCovarianceMatrix())
    
    def eigenvalues(self):
        """
        Returns the eigenvalues of the covariance matrix
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getEigenvalues())
    
    def eigenvector(self, i):
        """
        Returns the i'th largest eigenvector of the covariance matrix
        """
        return stallone.stallone_array_to_ndarray(self.jtransform().getEigenvector(i))
    
    def eigenvectors(self):
        """
        Returns the eigenvector matrix of the covariance matrix
        with eigenvectors as column vectors
        """
        if (self._evec is None):
            self._evec = stallone.stallone_array_to_ndarray(self.jtransform().getEigenvectorMatrix())
        return self._evec
    
    def set_dimension(self, d):
        """
        Sets the dimension for the projection. If not set, no dimension
        reduction will be made, i.e. the output data is transformed but
        has the same dimension as the input data.
        """
        self.ndim = d
        self.jtransform().setDimension(d)
    
    def transform(self, x):
        """
        Performs a linear transformation (and possibly dimension reduction
        if set_dimension has been used) of the input vector x to the 
        coordinate system defined by the covariance matrix eigenvectors.
        """
        return util.project(x.flatten(), self.eigenvectors(), self.ndim)
    
    def __has_efficient_transform(self):
        """
        Returns True
        """
        return True

class PCA_AMUSE(PCA):
    
    def __init__(self, amuse):
        self.amuse = amuse
        self.ndim = None
        
    def mean(self):
        return self.amuse.mean
    
    def covariance_matrix(self):
        return self.amuse.cov
    
    def eigenvalues(self):
        return self.amuse.pca_values
    
    def eigenvector(self, i):
        return self.amuse.pca_weights[i]
    
    def eigenvectors(self):
        return self.amuse.pca_weights
    
    def set_dimension(self, d):
        self.ndim = d
        
    def transform(self, x):
        return util.project(x.flatten(), self.eigenvectors(), self.ndim)
    
    def jtransform(self):
        if self.ndim is not None:
            A = self.eigenvectors()[:, 0 : self.ndim]
        else:
            A = self.eigenvectors()
        A = stallone.ndarray_to_stallone_array(A.T)
        return stallone.API.coorNew.linear_operator(A)


class TICA(PCA):
    """
    Wrapper class to stallone TICA
    """
    
    def __init__(self, jtica):
        PCA.__init__(self,jtica)
    
    def covariance_matrix_lagged(self):
        """
{'fraction_non_alphanumeric': 0.08790072388831438, 'fraction_numerical': 0.001920520017727877, 'mean_word_length': 2.2800387596899223, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1207992', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1901, 'n_words': 509}