Skip to content

Commit

Permalink
Merge pull request #25 from vnmabus/feature/remove_impl_functions
Browse files Browse the repository at this point in the history
Remove remaining code for Python 2 compatibility.
  • Loading branch information
vnmabus committed Feb 24, 2021
2 parents 161a6f5 + 192a9e1 commit a5f12a7
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 255 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ ENV/
/docs/modules

# conda build
/conda_build
/conda_build
/.pytest_cache/
12 changes: 5 additions & 7 deletions dcor/_dcor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
"""

from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

import collections
from dcor._dcor_internals import _af_inv_scaled
from enum import Enum

import numpy as np

from ._dcor_internals import _distance_matrix, _u_distance_matrix
from ._dcor_internals import mean_product, u_product
from dcor._dcor_internals import _af_inv_scaled

from ._dcor_internals import (_distance_matrix, _u_distance_matrix,
mean_product, u_product)
from ._fast_dcov_avl import _distance_covariance_sqr_avl_generic
from ._fast_dcov_mergesort import _distance_covariance_sqr_mergesort_generic
from ._utils import _sqrt, CompileMode
from ._utils import CompileMode, _sqrt


class _DcovAlgorithmInternals():
Expand Down
83 changes: 27 additions & 56 deletions dcor/_dcor_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
The functions in this module are used for performing computations related with
distance covariance and correlation.
"""
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

import warnings

import numpy as np
Expand Down Expand Up @@ -53,36 +50,8 @@ def _float_copy_to_out(out, origin):
return out


def _double_centered_imp(a, out=None):
"""
Real implementation of :func:`double_centered`.
This function is used to make parameter ``out`` keyword-only in
Python 2.
"""
out = _float_copy_to_out(out, a)

dim = np.size(a, 0)

mu = np.sum(a) / (dim * dim)
sum_cols = np.sum(a, 0, keepdims=True)
sum_rows = np.sum(a, 1, keepdims=True)
mu_cols = sum_cols / dim
mu_rows = sum_rows / dim

# Do one operation at a time, to improve broadcasting memory usage.
out -= mu_rows
out -= mu_cols
out += mu

return out


def double_centered(a, **kwargs):
def double_centered(a, *, out=None):
r"""
double_centered(a, *, out=None)
Return a copy of the matrix :math:`a` which is double centered.
A matrix is double centered if both the sum of its columns and the sum of
Expand Down Expand Up @@ -139,43 +108,27 @@ def double_centered(a, **kwargs):
[-0.22222222, 0.11111111, 0.11111111],
[-0.22222222, 0.11111111, 0.11111111]])
"""
return _double_centered_imp(a, **kwargs)


def _u_centered_imp(a, out=None):
"""
Real implementation of :func:`u_centered`.
This function is used to make parameter ``out`` keyword-only in
Python 2.
"""
out = _float_copy_to_out(out, a)

dim = np.size(a, 0)

u_mu = np.sum(a) / ((dim - 1) * (dim - 2))
mu = np.sum(a) / (dim * dim)
sum_cols = np.sum(a, 0, keepdims=True)
sum_rows = np.sum(a, 1, keepdims=True)
u_mu_cols = np.ones((dim, 1)).dot(sum_cols / (dim - 2))
u_mu_rows = (sum_rows / (dim - 2)).dot(np.ones((1, dim)))
mu_cols = sum_cols / dim
mu_rows = sum_rows / dim

# Do one operation at a time, to improve broadcasting memory usage.
out -= u_mu_rows
out -= u_mu_cols
out += u_mu

# The diagonal is zero
out[np.eye(dim, dtype=bool)] = 0
out -= mu_rows
out -= mu_cols
out += mu

return out


def u_centered(a, **kwargs):
def u_centered(a, *, out=None):
r"""
u_centered(a, *, out=None)
Return a copy of the matrix :math:`a` which is :math:`U`-centered.
If the element of the i-th row and j-th column of the original
Expand Down Expand Up @@ -241,7 +194,25 @@ def u_centered(a, **kwargs):
[nan, 0.]])
"""
return _u_centered_imp(a, **kwargs)
out = _float_copy_to_out(out, a)

dim = np.size(a, 0)

u_mu = np.sum(a) / ((dim - 1) * (dim - 2))
sum_cols = np.sum(a, 0, keepdims=True)
sum_rows = np.sum(a, 1, keepdims=True)
u_mu_cols = np.ones((dim, 1)).dot(sum_cols / (dim - 2))
u_mu_rows = (sum_rows / (dim - 2)).dot(np.ones((1, dim)))

# Do one operation at a time, to improve broadcasting memory usage.
out -= u_mu_rows
out -= u_mu_cols
out += u_mu

# The diagonal is zero
out[np.eye(dim, dtype=bool)] = 0

return out


def mean_product(a, b):
Expand Down
46 changes: 17 additions & 29 deletions dcor/_energy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""Energy distance functions"""

from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

import warnings

import numpy as np
Expand Down Expand Up @@ -43,32 +40,9 @@ def _energy_distance_from_distance_matrices(
)


def _energy_distance_imp(x, y, average=None, exponent=1):
"""
Real implementation of :func:`energy_distance`.
This function is used to make parameter ``exponent`` keyword-only in
Python 2.
"""
x = _transform_to_2d(x)
y = _transform_to_2d(y)

_check_valid_energy_exponent(exponent)

distance_xx = distances.pairwise_distances(x, exponent=exponent)
distance_yy = distances.pairwise_distances(y, exponent=exponent)
distance_xy = distances.pairwise_distances(x, y, exponent=exponent)

return _energy_distance_from_distance_matrices(distance_xx=distance_xx,
distance_yy=distance_yy,
distance_xy=distance_xy,
average=average)


def energy_distance(x, y, **kwargs):
def energy_distance(x, y, *, average=None, exponent=1):
"""
energy_distance(x, y, *, exponent=1, average=None)
Estimator for energy distance.
Computes the estimator for the energy distance of the
random vectors corresponding to :math:`x` and :math:`y`.
Expand Down Expand Up @@ -123,4 +97,18 @@ def energy_distance(x, y, **kwargs):
0.0
"""
return _energy_distance_imp(x, y, **kwargs)
x = _transform_to_2d(x)
y = _transform_to_2d(y)

_check_valid_energy_exponent(exponent)

distance_xx = distances.pairwise_distances(x, exponent=exponent)
distance_yy = distances.pairwise_distances(y, exponent=exponent)
distance_xy = distances.pairwise_distances(x, y, exponent=exponent)

return _energy_distance_from_distance_matrices(
distance_xx=distance_xx,
distance_yy=distance_yy,
distance_xy=distance_xy,
average=average,
)
7 changes: 2 additions & 5 deletions dcor/_partial_dcor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"""Functions for computing partial distance covariance and correlation"""

from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

import numpy as np

from ._dcor_internals import _u_distance_matrix, u_complementary_projection
from ._dcor_internals import u_product
from ._dcor_internals import (_u_distance_matrix, u_complementary_projection,
u_product)
from ._utils import _sqrt


Expand Down
3 changes: 0 additions & 3 deletions dcor/_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""Utility functions"""
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

import enum

import numba

import numpy as np


Expand Down
13 changes: 5 additions & 8 deletions dcor/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
a double precision floating point number will not cause loss of precision.
"""

from __future__ import absolute_import, division, print_function

from dcor._utils import _transform_to_2d
import numpy as _np
import scipy.spatial as _spatial

from dcor._utils import _transform_to_2d

from ._utils import _can_be_double


Expand Down Expand Up @@ -89,10 +88,8 @@ def _cdist(x, y, exponent=1):
return _cdist_naive(x, y, exponent)


def pairwise_distances(x, y=None, **kwargs):
def pairwise_distances(x, y=None, *, exponent=1):
r"""
pairwise_distances(x, y=None, *, exponent=1)
Pairwise distance between points.
Return the pairwise distance between points in two sets, or
Expand Down Expand Up @@ -143,7 +140,7 @@ def pairwise_distances(x, y=None, **kwargs):
x = _transform_to_2d(x)

if y is None or y is x:
return _pdist(x, **kwargs)
return _pdist(x, exponent=exponent)
else:
y = _transform_to_2d(y)
return _cdist(x, y, **kwargs)
return _cdist(x, y, exponent=exponent)

0 comments on commit a5f12a7

Please sign in to comment.