<commit_before>"""Core functions used in compositional data analysis."""

# Part of Tetrahydra library
# Copyright (C) 2016-2017  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import division
import numpy as np
from scipy.linalg import helmert


def closure(data, k=1.0):
    """Apply closure to data, sample-wise.

    Parameters
    ----------
    data : 2d numpy array, shape [n_samples, n_measurements]
        Data to be closed to a certain constant. Do not forget to deal with
        zeros in the data before this operation.
    k : float, positive
        Sum of the measurements will be equal to this number.

    Returns
    -------
    data : 2d numpy array, shape [n_samples, n_measurements]
        Closed data.

    Reference
    ---------
    [1] <NAME>., <NAME>., & <NAME>.
        (2015). Modelling and Analysis of Compositional Data, pg. 9.
        Chichester, UK: John Wiley & Sons, Ltd.
        DOI: 10.1002/9781119003144

    """
    data_sum = np.sum(data, axis=1)
    for i in range(data.shape[1]):
        np.divide(data[:, i], data_sum, out=data[:, i])
    return data * k


def perturb(x, y):
    """Perturbation (analogous to addition in real space).

    Parameters
    ----------
    x, y: 2d numpy array, shape [n_samples, n_measurements]
        Input x will be perturbed by y. Use y**-1 as the second input for
        perturbation difference (analogous to subtraction in real space).

    Returns
    -------
    out : 2d numpy array, shape [n_samples, n_measurements]
        Perturbed x.

    Reference
    ---------
    [1] <NAME>., <NAME>., & <NAME>.
        (2015). Modelling and Analysis of Compositional Data, pg. 24.
        Chichester, UK: John Wiley & Sons, Ltd.
        DOI: 10.1002/9781119003144

    """
    out = closure(x * y)
    return out


def power(x, a):
    """Powering transformation (analogous to multiplication in real space).

    Parameters
    ----------
    x : 2d numpy array, shape [n_samples, n_measurements]
        Input x will be powered by a.
    a : float
        Constant, real number.

    Returns
    -------
    out : 2d numpy array, shape [n_samples, n_measurements]
        Power transformed x.

    Reference
    ---------
    [1] <NAME>., <NAME>., & <NAME>.
        (2015). Modelling and Analysis of Compositional Data, pg. 24.
        Chichester, UK: John Wiley & Sons, Ltd.
        DOI: 10.1002/9781119003144

    """
    out = closure(np.power(x, a))
    return out


def aitchison_inner_product(x, y):
    """Aitchison inner product of vectors in D dimensional simplex space [1].

    Parameters<commit_msg>Closure in power and perturb can now be disabled.<commit_after>"""Core functions used in compositional data analysis."""

# Part of Tetrahydra library
# Copyright (C) 2016-2017  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import division
import numpy as np
from scipy.linalg import helmert


def closure(data, k=1.0):
    """Apply closure to data, sample-wise.

    Parameters
    ----------
    data : 2d numpy array, shape [n_samples, n_measurements]
        Data to be closed to a certain constant. Do not forget to deal with
        zeros in the data before this operation.
    k : float, positive
        Sum of the measurements will be equal to this number.

    Returns
    -------
    data : 2d numpy array, shape [n_samples, n_measurements]
        Closed data.

    Reference
    ---------
    [1] <NAME>., <NAME>., & <NAME>.
        (2015). Modelling and Analysis of Compositional Data, pg. 9.
        Chichester, UK: John Wiley & Sons, Ltd.
        DOI: 10.1002/9781119003144

    """
    data_sum = np.sum(data, axis=1)
    for i in range(data.shape[1]):
        np.divide(data[:, i], data_sum, out=data[:, i])
    return data * k


def perturb(x, y, reclose=True):
    """Perturbation (analogous to addition in real space).

    Parameters
    ----------
    x, y: 2d numpy array, shape [n_samples, n_measurements]
        Input x will be perturbed by y. Use y**-1 as the second input for
        perturbation difference (analogous to subtraction in real space).
    reclose: bool
        Apply closure to the compositions after perturbation. True by default.

    Returns
    -------
    out : 2d numpy array, shape [n_samples, n_measurements]
        Perturbed x.

    Reference
    ---------
    [1] <NAME>., <NAME>., & <NAME>.
        (2015). Modelling and Analysis of Compositional Data, pg. 24.
        Chichester, UK: John Wiley & Sons, Ltd.
        DOI: 10.1002/9781119003144

    """
    out = x * y
    return closure(out) if reclose else out


def power(x, a, reclose=True):
    """Powering transformation (analogous to multiplication in real space).

    Parameters
    ----------
    x : 2d numpy array, shape [n_samples, n_measurements]
        Input x will be powered by a.
    a : float
        Constant, real number.
    reclose: bool
        Apply closure to the compositions after powering. True by default.

    Returns
    -------
    out : 2d numpy array, shape [n_samples, n_measurements]
        Power transformed x.

    Reference
    ---------
    [1] <NAME>., <NAME>., & <NAME>.
        (2015). Modelling and Analysis of Compositional Data, pg. 24.
        Chichester, UK: John Wiley & Sons, Ltd.
        DOI: 10.1002/9781119003144

    """
    out = np.power(x, a)
    return closure(out) if reclose else out


def aitchison_inner_product(x, y):
    """Aitchison inner product of vectors in D dimensional simplex space [1].

    Parameters
{'fraction_non_alphanumeric': 0.10761194029850746, 'fraction_numerical': 0.029253731343283584, 'mean_word_length': 2.4612603305785123, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5243246', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1915, 'n_words': 811}