<commit_before># Author: <NAME>
# Contributors: <NAME>, <NAME>
# Copyright (c) 2014, Stanford University
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#   Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
#   Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from __future__ import print_function, division, absolute_import
import numpy as np
import scipy.linalg
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils import array2d

__all__ = ['tICA']

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------


class tICA(BaseEstimator, TransformerMixin):

    """Time-structure Independent Component Analysis (tICA)

    Linear dimensionality reduction using an eigendecomposition of the
    time-lag correlation matrix and covariance matrix of the data and keeping
    only the vectors which decorrelate slowest to project the data into a lower
    dimensional space.

    Parameters
    ----------
    n_components : int, None
        Number of components to keep.
    offset : int
        Delay time forward or backward in the input data. The time-lagged
        correlations is computed between datas X[t] and X[t+offset].

    Attributes
    ----------
    `components_` : array-like, shape (n_components, n_features)
        Components with maximum autocorrelation.
    `offset_correlation_` : array-like, shape (n_features, n_features)
    `eigenvalues_` : array-like, shape (n_features,)
    `eigenvectors_` : array-like, shape (n_components, n_features)
    `means_` : array, shape (n_features,)
    `n_observations` : int
    `n_sequences` : int

    References
    ----------
    .. [1] Schwantes, <NAME>., and <NAME>. J. Chem Theory Comput.
    9.4 (2013): 2000-2009.
    .. [2] Perez-Hernandez, Guillermo, et al. J Chem. Phys (2013): 015102.
    """

    def __init__(self, n_components=None, offset=1):
        self.n_components = n_components
        self.offset = offset

        self.n_features = None
        self.n_observations_ = None
        self.n_sequences_ = None

        self._initialized = False

        # X[:-self.offset].T dot X[self.offset:]
        self._outer_0_to_T_lagged = None
        # X[:-self.offset].sum(axis=0)
        self._sum_0_to_TminusOffset = None
        # X[self.offset:].sum(axis=0)
        self._sum_tau_to_T = None
        # X[self.offset:].sum(axis=0)
        self._sum_0_to_T = None

        # X[:-self.offset].T dot X[:-self.offset])
        self._outer_0_to_TminusOffset = None
        # X[self.offset:].T dot X[self.offset:]
        self._outer_offset_to_T = None

        # the tICs themselves
        self._components_ = None
        # Cached results of the eigendecompsition
        self._eigenvectors_ = None
        self._eigenvalues = None

        # are our current tICs dirty? this indicates that we've updated
        # the model with more data since the last time we computed components_,
        # eigenvalues, eigenvectors, and is set by _fit
        self._is_dirty = True

    def _initialize(self, n_features):
        if self._initialized:
            return

        if self.n_components is None:
            self.n_components = n_features
        self.n_features = n_features
        self.n_observations_ = 0
        self.n_sequences_ = 0
        self._outer_0_to_T_lagged = np.zeros((n_features, n_features))
        self._sum_0_to_TminusOffset = np.zeros(n_features)
        self._sum_tau_to_T = np.zeros(n_features)
        self._sum_0_to_T = np.zeros(n_features)
        self._outer_0_to_TminusOffset = np.zeros((n_features, n_features))
        self._outer_offset_to_T = np.zeros((n_features, n_features))

    def _solve_eigenproblem(self):
        if not self._is_dirty:
            return

        vals, vecs = scipy.linalg.eig(self.offset_correlation_, b=self.covariance_)

        # sort in order of decreasing value
        ind = np.argsort(np.real(vals))[::-1]
        vals = vals[ind]
        vecs = vecs[:, ind]

        self._eigenvalues_ = vals
        self._eigenvectors_ = vecs
<commit_msg>cast eigenvalues to real<commit_after># Author: <NAME>
# Contributors: <NAME>, <NAME>
# Copyright (c) 2014, Stanford University
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#   Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
#   Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from __future__ import print_function, division, absolute_import
import numpy as np
import scipy.linalg
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils import array2d

__all__ = ['tICA']

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------


class tICA(BaseEstimator, TransformerMixin):

    """Time-structure Independent Component Analysis (tICA)

    Linear dimensionality reduction using an eigendecomposition of the
    time-lag correlation matrix and covariance matrix of the data and keeping
    only the vectors which decorrelate slowest to project the data into a lower
    dimensional space.

    Parameters
    ----------
    n_components : int, None
        Number of components to keep.
    offset : int
        Delay time forward or backward in the input data. The time-lagged
        correlations is computed between datas X[t] and X[t+offset].

    Attributes
    ----------
    `components_` : array-like, shape (n_components, n_features)
        Components with maximum autocorrelation.
    `offset_correlation_` : array-like, shape (n_features, n_features)
    `eigenvalues_` : array-like, shape (n_features,)
    `eigenvectors_` : array-like, shape (n_components, n_features)
    `means_` : array, shape (n_features,)
    `n_observations` : int
    `n_sequences` : int

    References
    ----------
    .. [1] Schwantes, <NAME>., and <NAME>. Chem Theory Comput.
    9.4 (2013): 2000-2009.
    .. [2] Perez-Hernandez, Guillermo, et al. J Chem. Phys (2013): 015102.
    """

    def __init__(self, n_components=None, offset=1):
        self.n_components = n_components
        self.offset = offset

        self.n_features = None
        self.n_observations_ = None
        self.n_sequences_ = None

        self._initialized = False

        # X[:-self.offset].T dot X[self.offset:]
        self._outer_0_to_T_lagged = None
        # X[:-self.offset].sum(axis=0)
        self._sum_0_to_TminusOffset = None
        # X[self.offset:].sum(axis=0)
        self._sum_tau_to_T = None
        # X[self.offset:].sum(axis=0)
        self._sum_0_to_T = None

        # X[:-self.offset].T dot X[:-self.offset])
        self._outer_0_to_TminusOffset = None
        # X[self.offset:].T dot X[self.offset:]
        self._outer_offset_to_T = None

        # the tICs themselves
        self._components_ = None
        # Cached results of the eigendecompsition
        self._eigenvectors_ = None
        self._eigenvalues = None

        # are our current tICs dirty? this indicates that we've updated
        # the model with more data since the last time we computed components_,
        # eigenvalues, eigenvectors, and is set by _fit
        self._is_dirty = True

    def _initialize(self, n_features):
        if self._initialized:
            return

        if self.n_components is None:
            self.n_components = n_features
        self.n_features = n_features
        self.n_observations_ = 0
        self.n_sequences_ = 0
        self._outer_0_to_T_lagged = np.zeros((n_features, n_features))
        self._sum_0_to_TminusOffset = np.zeros(n_features)
        self._sum_tau_to_T = np.zeros(n_features)
        self._sum_0_to_T = np.zeros(n_features)
        self._outer_0_to_TminusOffset = np.zeros((n_features, n_features))
        self._outer_offset_to_T = np.zeros((n_features, n_features))

    def _solve_eigenproblem(self):
        if not self._is_dirty:
            return

        vals, vecs = scipy.linalg.eig(self.offset_correlation_, b=self.covariance_)

        # sort in order of decreasing value
        ind = np.argsort(np.real(vals))[::-1]
        vals = vals[ind]
        vecs = vecs[:, ind]

        self._eigenvalues_ = np.real(vals)
        self._eigenvectors_ = vecs

{'fraction_non_alphanumeric': 0.13665166681972632, 'fraction_numerical': 0.008448893378638994, 'mean_word_length': 2.3978159126365055, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7008248', 'n_tokens_mistral': 3219, 'n_tokens_neox': 2969, 'n_words': 990}