def test_ott_backend():
    n_samples, n_features = 100, 20
    epsilon = .1
    X = np.random.randn(n_samples, n_features)
    Y = np.random.randn(n_samples, n_features)
    algo = OptimalTransportAlignment(reg=epsilon,
                                     metric="euclidean",
                                     tol=1e-5,
                                     max_iter=10000)
    old_implem = POTAlignment(reg=epsilon,
                              metric="euclidean",
                              tol=1e-5,
                              max_iter=10000)
    algo.fit(X, Y)
    old_implem.fit(X, Y)
    assert_array_almost_equal(algo.R, old_implem.R, decimal=3)
Пример #2
0
def test_all_classes_R_and_pred_shape_and_better_than_identity():
    from scipy.sparse.csc import csc_matrix
    '''Test all classes on random case'''

    for n_samples, n_features in [(100, 20), (20, 100)]:
        X = np.random.randn(n_samples, n_features)
        Y = np.random.randn(n_samples, n_features)
        id = Identity()
        id.fit(X, Y)
        identity_baseline_score = zero_mean_coefficient_determination(Y, X)
        assert_array_almost_equal(X, id.transform(X))
        for algo in [RidgeAlignment(), ScaledOrthogonalAlignment(),
                     ScaledOrthogonalAlignment(scaling=False),
                     OptimalTransportAlignment(),
                     Hungarian(), DiagonalAlignment()]:
            print(algo)
            algo.fit(X, Y)
            # test that permutation matrix shape is (20, 20) except for Ridge
            if type(algo.R) == csc_matrix:
                R = algo.R.toarray()
                assert(R.shape == (n_features, n_features))
            elif type(algo) != RidgeAlignment:
                R = algo.R
                assert(R.shape == (n_features, n_features))
            # test pred shape and loss improvement compared to identity
            X_pred = algo.transform(X)
            assert(X_pred.shape == X.shape)
            algo_score = zero_mean_coefficient_determination(
                Y, X_pred)
            assert_greater(algo_score, identity_baseline_score)
def fit_one_piece_intra(X_i, Y_i, alignment_method):
    """ Align source and target data in one piece i, X_i and Y_i, using
    alignment method and learn transformation to map X to Y.

    Parameters
    ----------
    X_i: ndarray
        Source data for piece i (shape : n_samples, n_features)
    Y_i: ndarray
        Target data for piece i (shape : n_samples, n_features)
    alignment_method: string
        Algorithm used to perform alignment between X_i and Y_i :
        - either 'identity', 'scaled_orthogonal', 'optimal_transport',
        'ridge_cv', 'permutation', 'diagonal'
        - or an instance of one of alignment classes
            (imported from functional_alignment.alignment_methods)
    Returns
    -------
    alignment_algo
        Instance of alignment estimator class fitted for X_i, Y_i
    """

    if alignment_method == 'identity':
        alignment_algo = Identity()
    elif alignment_method == 'scaled_orthogonal':
        alignment_algo = ScaledOrthogonalAlignment()
    elif alignment_method == 'ridge_cv':
        alignment_algo = RidgeAlignment()
    elif alignment_method == 'permutation':
        alignment_algo = Hungarian()
    elif alignment_method == 'optimal_transport':
        alignment_algo = OptimalTransportAlignment()
    elif alignment_method == 'diagonal':
        alignment_algo = DiagonalAlignment()
    elif isinstance(alignment_method, (Identity, ScaledOrthogonalAlignment,
                                       RidgeAlignment, Hungarian,
                                       OptimalTransportAlignment,
                                       DiagonalAlignment)):
        alignment_algo = clone(alignment_method)

    if isinstance(alignment_algo, RidgeAlignment):
        if isinstance(alignment_algo.cv, int):
            if alignment_algo.cv > X_i.shape[0]:
                warnings.warn(
                    "Too few samples for RidgeCV, Ridge(alpha=1) fitted instead")
                alignment_algo = RidgeAl()
    try:
        alignment_algo.fit(X_i, Y_i)
    except UnboundLocalError:
        warn_msg = ("{} is an unrecognized ".format(alignment_method) +
                    "alignment method. Please provide a recognized " +
                    "alignment method.")
        raise NotImplementedError(warn_msg)
    return alignment_algo
Пример #4
0
def fit_one_piece(X_i, Y_i, alignment_method):
    """ Align source and target data in one piece i, X_i and Y_i, using
    alignment method and learn transformation to map X to Y.

    Parameters
    ----------
    X_i: ndarray
        Source data for piece i (shape : n_samples, n_features)
    Y_i: ndarray
        Target data for piece i (shape : n_samples, n_features)
    alignment_method: string
        Algorithm used to perform alignment between X_i and Y_i :
        - either 'identity', 'scaled_orthogonal', 'optimal_transport',
        'ridge_cv', 'permutation', 'diagonal'
        - or an instance of one of alignment classes
            (imported from functional_alignment.alignment_methods)
    Returns
    -------
    alignment_algo
        Instance of alignment estimator class fitted for X_i, Y_i
    """

    if alignment_method == 'identity':
        alignment_algo = Identity()
    elif alignment_method == 'scaled_orthogonal':
        alignment_algo = ScaledOrthogonalAlignment()
    elif alignment_method == 'ridge_cv':
        alignment_algo = RidgeAlignment()
    elif alignment_method == 'permutation':
        alignment_algo = Hungarian()
    elif alignment_method == 'optimal_transport':
        alignment_algo = OptimalTransportAlignment()
    elif alignment_method == 'diagonal':
        alignment_algo = DiagonalAlignment()
    elif isinstance(alignment_method,
                    (Identity, ScaledOrthogonalAlignment, RidgeAlignment,
                     Hungarian, OptimalTransportAlignment, DiagonalAlignment)):
        alignment_algo = clone(alignment_method)

    if not np.count_nonzero(X_i) or not np.count_nonzero(Y_i):
        warn_msg = ("Empty parcel found. Please check overlap between " +
                    "provided mask and functional image. Returning " +
                    "Identity alignment for empty parcel")
        warnings.warn(warn_msg)
        alignment_algo = Identity()
    try:
        alignment_algo.fit(X_i, Y_i)
    except UnboundLocalError:
        warn_msg = ("{} is an unrecognized ".format(alignment_method) +
                    "alignment method. Please provide a recognized " +
                    "alignment method.")
        raise NotImplementedError(warn_msg)
    return alignment_algo
Пример #5
0
def check_input_method(input_method):
    if input_method == "pairwise_scaled_orthogonal":
        method = "pairwise"
        pairwise_method = "scaled_orthogonal"
        local_align_method = "scaled_orthogonal"
    elif input_method == "pairwise_ot_e-1":
        method = "pairwise"
        pairwise_method = OptimalTransportAlignment(reg=.1)
        local_align_method = "ot_e-1"
    else:
        method = input_method
        pairwise_method = ""
        local_align_method = ""
    return method, pairwise_method, local_align_method
Пример #6
0
def fit_one_piece(X_i, Y_i, alignment_method):
    """ Align source and target data in one piece i, X_i and Y_i, using
    alignment method and learn transformation to map X to Y.

    Parameters
    ----------
    X_i: ndarray
        Source data for piece i (shape : n_features_i, n_samples)
    Y_i: ndarray
        Target data for piece i (shape : n_features_i, n_samples)
    alignment_method: string
        Algorithm used to perform alignment between X_i and Y_i :
        - either 'identity', 'scaled_orthogonal', 'ridge_cv',
            'permutation', 'diagonal'
        - or an instance of one of alignment classes
            (imported from functional_alignment.alignment_methods)

    Returns
    -------
    alignment_algo
        Instance of alignment estimator class fitted for X_i, Y_i
    """

    if alignment_method == 'identity':
        alignment_algo = Identity()
    elif alignment_method == 'scaled_orthogonal':
        alignment_algo = ScaledOrthogonalAlignment()
    elif alignment_method == 'ridge_cv':
        alignment_algo = RidgeAlignment()
    elif alignment_method == 'permutation':
        alignment_algo = Hungarian()
    elif alignment_method == 'optimal_transport':
        alignment_algo = OptimalTransportAlignment()
    elif alignment_method == 'diagonal':
        alignment_algo = DiagonalAlignment()
    elif isinstance(alignment_method,
                    (Identity, ScaledOrthogonalAlignment, RidgeAlignment,
                     Hungarian, OptimalTransportAlignment, DiagonalAlignment)):
        alignment_algo = copy.deepcopy(alignment_method)
    alignment_algo.fit(X_i.T, Y_i.T)

    return alignment_algo
Пример #7
0
ridge_alignment = RidgeAlignment(alphas=[0.01, 0.1], cv=2).fit(X.T, Y.T)

_plot_distributions_and_alignment(X,
                                  Y,
                                  R=ridge_alignment.R.coef_,
                                  title='Ridge between distributions',
                                  thr=.1)
_plot_mixing_matrix(R=ridge_alignment.R.coef_, title='Ridge coefficients')
###############################################################################
# Optimal Transport alignment
# ---------------------------
# Finally this package comes with a new method that build on the Wasserstein
# distance which is well-suited for this problem. This is the framework of
# Optimal Transport that search to transport all signal from `X` to `Y`
# while minimizing the overall cost of this transport. `R` is here the
# optimal coupling between `X` and `Y` with entropic regularization.
# This way of finding a transform uses more geometrical information from the
# distributions.
#

ot_alignment = OptimalTransportAlignment(reg=.1)
ot_alignment.fit(X.T, Y.T)
_plot_distributions_and_alignment(X,
                                  Y,
                                  R=ot_alignment.R,
                                  title='Optimal Transport',
                                  thr=.1)
_plot_mixing_matrix(R=ot_alignment.R, title='Optimal Transport coupling')

# sphinx_gallery_thumbnail_number = 7