<commit_before>""" demixed Principal Component Analysis
"""

# Author: <NAME> <<EMAIL>>
#
# License: BSD 3 clause

from __future__ import print_function

import numpy as np
from collections import OrderedDict
from itertools import combinations, chain
from scipy.sparse.linalg import svds
from scipy.linalg import pinv

from sklearn.base import BaseEstimator
from sklearn.utils.extmath import randomized_svd
import numexpr as ne
#from ..utils import check_random_state, as_float_array
#from ..utils import check_array
#from ..utils.extmath import fast_dot, fast_logdet, randomized_svd

import pyximport
pyximport.install(setup_args={'include_dirs': np.get_include()})
from . import nan_shuffle

class dPCA(BaseEstimator):
    """ demixed Principal component analysis (dPCA)

    dPCA is a linear dimensionality reduction technique that automatically discovers
    and highlights the essential features of complex population activities. The
    population activity is decomposed into a few demixed components that capture most
    of the variance in the data and that highlight the dynamic tuning of the population
    to various task parameters, such as stimuli, decisions, rewards, etc.

    Parameters
    ----------
    labels : int or string
        Labels of feature axis.

        If int the corresponding number of labels are selected from the alphabet 'abcde...'

    join : None or dict
        Parameter combinations to join

        If a data set has parametrized by time t and stimulus s, then dPCA will split
        the data into marginalizations corresponding to 't', 's' and 'ts'. At times,
        we want to join different marginalizations (like 's' and 'ts'), e.g. if
        we are only interested in the time-modulated stimulus components. In this case,
        we would pass {'ts' : ['s','ts']}.

    regularizer : None, float, 'auto'
        Regularization parameter. If None or 0, then no regularization is applied.
        For float, the regularization weight is regularizer*var(data). If 'auto', the
        optimal regularization parameter is found during fitting (might take some time).

    n_components : None, int or dict
        Number of components to keep.

        If n_components is int, then the same number of components are kept in every
        marginalization. Otherwise, the dict allows to set the number of components
        in each marginalization (e.g. {'t' : 10, 'ts' : 5}). Defaults to 10.

    copy : bool
        If False, data passed to fit are overwritten and running
        fit(X).transform(X) will not yield the expected results,
        use fit_transform(X) instead.

    n_iter : int (default: 0)
        Number of iterations for randomized SVD solver (sklearn).

    Attributes
    ----------
    explained_variance_ratio_ : dict with arrays, [n_components]
        Dictionary in which each key refers to one marginalization and the \
        value is a vector with the percentage of variance explained by each of \
        the marginal components.

    Notes
    -----
    Implements the dPCA model from:
    <NAME>*, <NAME>*, <NAME>, <NAME>, <NAME>, <NAME>, \
    <NAME>, <NAME>, <NAME>, <NAME>
    Demixed principal component analysis of population activity in higher \
    cortical areas reveals independent representation of task parameters,


    Examples
    --------

    >>> import numpy as np
    >>> from sklearn.decomposition import dPCA
    >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
    >>> dpca = dPCA(n_components=2)
    >>> dpca.fit(X)
    PCA(copy=True, n_components=2, whiten=False)
    >>> print(pca.explained_variance_ratio_)
    [ 0.99244...  0.00755...]
    """
    def __init__(self, labels=None, join=None, n_components=10, regularizer=None, copy=True, n_iter=0):
        # create labels from alphabet if not provided
        if isinstance(labels,str):
            self.labels = labels
        elif isinstance(labels,int):
            alphabet = 'abcdefghijklmnopqrstuvwxyz'
            labels = alphabet[:Y.ndim-1]
        else:
            raise TypeError('Wrong type for labels. Please either set labels to the number of variables or provide the axis labels as a single string of characters (like "ts" for time and stimulus)')

        self.join = join
        self.regularizer = 0 if regularizer == None else regularizer
        self.opt_regularizer_flag = regularizer == 'auto'
        self.n_components = n_components
        self.copy = copy
        self.marginalizations = self._get_parameter_combinations()
        self.n_iter = n_iter

        # set debug mode, 0 = no reports, 1 = warnings, 2 = warnings & progress, >2 = everything
        self.debug = 2

        if regularizer == 'auto':
            print("""You chose to determine the regularization parameter automatically. This can
                    take substantial time and grows linearly with the number of crossvalidation
                    folds. The latter can be set by changing self.n_trials (default = 3). Similarly,
                    use self.protect to set the list of axes that are not supposed to get to get shuffled
                    (e.g. upon splitting the data into test- and training, time-points should always
                    be drawn from the same trial, i.e. self.protect = ['t']). This can significantly<commit_msg>Fixed Y undefined bug<commit_after>""" demixed Principal Component Analysis
"""

# Author: <NAME> <<EMAIL>>
#
# License: BSD 3 clause

from __future__ import print_function

import numpy as np
from collections import OrderedDict
from itertools import combinations, chain
from scipy.sparse.linalg import svds
from scipy.linalg import pinv

from sklearn.base import BaseEstimator
from sklearn.utils.extmath import randomized_svd
import numexpr as ne
#from ..utils import check_random_state, as_float_array
#from ..utils import check_array
#from ..utils.extmath import fast_dot, fast_logdet, randomized_svd

import pyximport
pyximport.install(setup_args={'include_dirs': np.get_include()})
from . import nan_shuffle

class dPCA(BaseEstimator):
    """ demixed Principal component analysis (dPCA)

    dPCA is a linear dimensionality reduction technique that automatically discovers
    and highlights the essential features of complex population activities. The
    population activity is decomposed into a few demixed components that capture most
    of the variance in the data and that highlight the dynamic tuning of the population
    to various task parameters, such as stimuli, decisions, rewards, etc.

    Parameters
    ----------
    labels : int or string
        Labels of feature axis.

        If int the corresponding number of labels are selected from the alphabet 'abcde...'

    join : None or dict
        Parameter combinations to join

        If a data set has parametrized by time t and stimulus s, then dPCA will split
        the data into marginalizations corresponding to 't', 's' and 'ts'. At times,
        we want to join different marginalizations (like 's' and 'ts'), e.g. if
        we are only interested in the time-modulated stimulus components. In this case,
        we would pass {'ts' : ['s','ts']}.

    regularizer : None, float, 'auto'
        Regularization parameter. If None or 0, then no regularization is applied.
        For float, the regularization weight is regularizer*var(data). If 'auto', the
        optimal regularization parameter is found during fitting (might take some time).

    n_components : None, int or dict
        Number of components to keep.

        If n_components is int, then the same number of components are kept in every
        marginalization. Otherwise, the dict allows to set the number of components
        in each marginalization (e.g. {'t' : 10, 'ts' : 5}). Defaults to 10.

    copy : bool
        If False, data passed to fit are overwritten and running
        fit(X).transform(X) will not yield the expected results,
        use fit_transform(X) instead.

    n_iter : int (default: 0)
        Number of iterations for randomized SVD solver (sklearn).

    Attributes
    ----------
    explained_variance_ratio_ : dict with arrays, [n_components]
        Dictionary in which each key refers to one marginalization and the \
        value is a vector with the percentage of variance explained by each of \
        the marginal components.

    Notes
    -----
    Implements the dPCA model from:
    <NAME>*, <NAME>*, <NAME>, <NAME>, <NAME>, <NAME>, \
    <NAME>, <NAME>, <NAME>, <NAME>
    Demixed principal component analysis of population activity in higher \
    cortical areas reveals independent representation of task parameters,


    Examples
    --------

    >>> import numpy as np
    >>> from sklearn.decomposition import dPCA
    >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
    >>> dpca = dPCA(n_components=2)
    >>> dpca.fit(X)
    PCA(copy=True, n_components=2, whiten=False)
    >>> print(pca.explained_variance_ratio_)
    [ 0.99244...  0.00755...]
    """
    def __init__(self, labels=None, join=None, n_components=10, regularizer=None, copy=True, n_iter=0):
        # create labels from alphabet if not provided
        if isinstance(labels,str):
            self.labels = labels
        elif isinstance(labels,int):
            alphabet = 'abcdefghijklmnopqrstuvwxyz'
            labels = alphabet[:labels]
        else:
            raise TypeError('Wrong type for labels. Please either set labels to the number of variables or provide the axis labels as a single string of characters (like "ts" for time and stimulus)')

        self.join = join
        self.regularizer = 0 if regularizer == None else regularizer
        self.opt_regularizer_flag = regularizer == 'auto'
        self.n_components = n_components
        self.copy = copy
        self.marginalizations = self._get_parameter_combinations()
        self.n_iter = n_iter

        # set debug mode, 0 = no reports, 1 = warnings, 2 = warnings & progress, >2 = everything
        self.debug = 2

        if regularizer == 'auto':
            print("""You chose to determine the regularization parameter automatically. This can
                    take substantial time and grows linearly with the number of crossvalidation
                    folds. The latter can be set by changing self.n_trials (default = 3). Similarly,
                    use self.protect to set the list of axes that are not supposed to get to get shuffled
                    (e.g. upon splitting the data into test- and training, time-points should always
                    be drawn from the same trial, i.e. self.protect = ['t']). This can significantly
{'fraction_non_alphanumeric': 0.08294239069243761, 'fraction_numerical': 0.008350534809532746, 'mean_word_length': 2.80406852248394, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 67, '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': '615270', 'n_tokens_mistral': 2919, 'n_tokens_neox': 2764, 'n_words': 1210}