# Path: modl/decomposition/stability.py
# def mean_amari_discrepency(dictionaries: List[np.ndarray], n_jobs=1):
#     discrepencies = Parallel(n_jobs=n_jobs)(
#         delayed(amari_discrepency)(D1, D2)
#         for i, D1 in enumerate(dictionaries[:-1])
#         for D2 in dictionaries[i + 1:])
#     return np.mean(np.array(discrepencies)), np.std(np.array(discrepencies))
# 
# def amari_discrepency(D1: np.ndarray, D2: np.ndarray) -> float:
#     """
# 
#     Parameters
#     ----------
#     D1: np.ndarray, shape (n_components, n_features)
# 
#     D2: np.ndarray, shape (n_components, n_features)
# 
#     Returns
#     -------
#     discrepency: Amari discrepency
#     """
#     C = (D1.dot(D2.T) / np.sqrt(np.sum(D1 ** 2, axis=1))[:, None]
#          / np.sqrt(np.sum(D2 ** 2, axis=1))[None, :])
#     return .5 * (np.mean(1 - C.max(axis=0)) + np.mean(1 - C.max(axis=1)))


from sklearn.utils import check_random_state
from modl.decomposition.stability import mean_amari_discrepency, amari_discrepency
from numpy.testing import assert_almost_equal




def test_amari_discrepency():
    n_components, n_features = 50, 100
    rng = check_random_state(23)
    dictionaries = [rng.randn(n_components, n_features) for _ in range(2)]
    assert amari_discrepency(dictionaries[0], dictionaries[1]) >= 0

# Path: modl/decomposition/stability.py
# def mean_amari_discrepency(dictionaries: List[np.ndarray], n_jobs=1):
#     discrepencies = Parallel(n_jobs=n_jobs)(
#         delayed(amari_discrepency)(D1, D2)
#         for i, D1 in enumerate(dictionaries[:-1])
#         for D2 in dictionaries[i + 1:])
#     return np.mean(np.array(discrepencies)), np.std(np.array(discrepencies))
# 
# def amari_discrepency(D1: np.ndarray, D2: np.ndarray) -> float:
#     """
# 
#     Parameters
#     ----------
#     D1: np.ndarray, shape (n_components, n_features)
# 
#     D2: np.ndarray, shape (n_components, n_features)
# 
#     Returns
#     -------
#     discrepency: Amari discrepency
#     """
#     C = (D1.dot(D2.T) / np.sqrt(np.sum(D1 ** 2, axis=1))[:, None]
#          / np.sqrt(np.sum(D2 ** 2, axis=1))[None, :])
#     return .5 * (np.mean(1 - C.max(axis=0)) + np.mean(1 - C.max(axis=1)))


# Path: modl/decomposition/tests/test_stability.py
from sklearn.utils import check_random_state
from modl.decomposition.stability import mean_amari_discrepency, amari_discrepency
from numpy.testing import assert_almost_equal





def test_amari_discrepency():
    n_components, n_features = 50, 100
    rng = check_random_state(23)
    dictionaries = [rng.randn(n_components, n_features) for _ in range(2)]
    assert amari_discrepency(dictionaries[0], dictionaries[1]) >= 0

    assert_almost_equal(amari_discrepency(dictionaries[0], dictionaries[0]), 0)