Ejemplo n.º 1
0
    def riemannian(covmats,
                   tol=10e-9,
                   max_iter=50,
                   init=None,
                   sample_weight=None):
        if init is None:
            output = CovMat(covmats.mean(0), False)
        else:
            output = init

        k = 0
        nu = 1.0
        tau = numpy.finfo(numpy.double).max
        crit = numpy.finfo(numpy.double).max

        while crit > tol and k < max_iter and nu > tol:
            k += 1
            tmp = CovMats([(output.invsqrtm * covmat * output.invsqrtm).logm
                           for covmat in covmats], False)
            average = CovMat(tmp.average(0, sample_weight), False)
            crit = average.norm(ord='fro')
            h = nu * crit
            output = output.sqrtm * (nu * average).expm * output.sqrtm

            if h < tau:
                nu *= 0.95
                tau = h
            else:
                nu *= 0.5

        # if k == max_iter:
        #    print("Max iter reach")

        return output
Ejemplo n.º 2
0
    def riemannian(covmats, tol=10e-9, max_iter=50, init=None, sample_weight=None):
        if init is None:
            output = CovMat(covmats.mean(0), False)
        else:
            output = init

        k = 0
        nu = 1.0
        tau = numpy.finfo(numpy.double).max
        crit = numpy.finfo(numpy.double).max

        while crit > tol and k < max_iter and nu > tol:
            k += 1
            tmp = CovMats([(output.invsqrtm * covmat * output.invsqrtm).logm for covmat in covmats], False)
            average = CovMat(tmp.average(0, sample_weight), False)
            crit = average.norm(ord='fro')
            h = nu * crit
            output = output.sqrtm * (nu * average).expm * output.sqrtm

            if h < tau:
                nu *= 0.95
                tau = h
            else:
                nu *= 0.5

        # if k == max_iter:
        #    print("Max iter reach")

        return output
Ejemplo n.º 3
0
    def wasserstein(covmats,
                    tol=10e-4,
                    max_iter=1,
                    init=None,
                    sample_weight=None):
        if init is None:
            output = CovMat(covmats.mean(0))
        else:
            output = init

        k = 0
        crit = numpy.finfo(numpy.double).max

        while (crit > tol) and (k < max_iter):
            k += 1
            tmp = CovMats([(output.sqrtm * covmat * output.sqrtm).sqrtm
                           for covmat in covmats], False)
            average = CovMat(tmp.average(0, sample_weight), False)

            new_output = average.sqrtm
            crit = (new_output - output.sqrtm).norm(ord='fro')
            output = new_output

        # if k == max_iter:
        #    print("Max iter reach")

        return output * output
Ejemplo n.º 4
0
    def log_determinant(covmats,
                        tol=10e-5,
                        max_iter=50,
                        init=None,
                        sample_weight=None):
        if init is None:
            output = CovMat(covmats.mean(0), False)
        else:
            output = init

        k = 0
        crit = numpy.finfo(numpy.double).max

        while crit > tol and k < max_iter:
            k += 1
            tmp = CovMats([(0.5 * (covmat + output)).inverse
                           for covmat in covmats], False)
            new_output = CovMat(tmp.average(0, sample_weight), False).inverse
            crit = (new_output - output).norm(ord='fro')
            output = new_output

        # if k == max_iter:
        #    print("Max iter reach")

        return output
Ejemplo n.º 5
0
def test_sqrtm():
    numpy_array = numpy.array([[2, 1, 0], [1, 2, 0], [0, 0, 3]])
    if (CovMat(numpy_array).sqrtm - CovMat(sqrtm(numpy_array))).norm() < 1e-10:
        print("sqrtm: PASS")
        return True
    else:
        print("sqrtm: FAIL")
        return False
Ejemplo n.º 6
0
def test_powm():
    numpy_array = numpy.array([[2, 1, 0], [1, 2, 0], [0, 0, 3]])
    if (CovMat(numpy_array).powm(5) -
            CovMat(powm(numpy_array, 5))).norm() < 1e-10:
        print("powm: PASS")
        return True
    else:
        print("powm: FAIL")
        return False
Ejemplo n.º 7
0
def function():
    covmat1 = CovMat.random(100)
    covmat2 = CovMat.random(100)
    a = Distance.euclidean(covmat1, covmat2)
    b = Distance.log_euclidean(covmat1, covmat2)
    c = Distance.log_determinant(covmat1, covmat2)
    d = Distance.riemannian(covmat1, covmat2)
    e = Distance.wasserstein(covmat1, covmat2)
    f = Distance.kullback(covmat1, covmat2)
    g = Distance.kullback_right(covmat1, covmat2)
    h = Distance.kullback_sym(covmat1, covmat2)
Ejemplo n.º 8
0
def test_euclidean():
    m1 = CovMat.random(10)
    m2 = CovMat.random(10)
    old_dist = distance_euclid(m1.numpy_array, m2.numpy_array)
    m1.reset_fields()
    m2.reset_fields()
    new_dist = Distance.euclidean(m1, m2)

    if abs( old_dist - new_dist ) < 1e-10:
        print("euclid: PASS")
        return True
    else:
        print("euclid: FAIL")
        return False
Ejemplo n.º 9
0
def test_euclidean():
    m1 = CovMat.random(10)
    m2 = CovMat.random(10)
    old_dist = distance_euclid(m1.numpy_array, m2.numpy_array)
    m1.reset_fields()
    m2.reset_fields()
    new_dist = Distance.euclidean(m1, m2)

    if abs(old_dist - new_dist) < 1e-10:
        print("euclid: PASS")
        return True
    else:
        print("euclid: FAIL")
        return False
Ejemplo n.º 10
0
 def __covmats_from_numpy_array(self, arg, copy, data_type=DataType.double):
     self.reset_fields()
     self._numpy_array = numpy.array(arg, dtype=data_type, copy=copy)
     self.__covmats = [
         CovMat(self._numpy_array[i, :, :], False)
         for i in range(self.length)
     ]
Ejemplo n.º 11
0
def function():
    covmat = CovMat.random(100)
    sqrtm = covmat.sqrtm
    invsqrtm = covmat.invsqrtm
    expm = covmat.expm
    logm = covmat.logm
    pown = covmat.powm(2)
Ejemplo n.º 12
0
def function():
    covmat = CovMat.random(100)
    sqrtm = covmat.sqrtm
    invsqrtm = covmat.invsqrtm
    expm = covmat.expm
    logm = covmat.logm
    pown = covmat.powm(2)
Ejemplo n.º 13
0
def test_tangent():
    covmat = CovMat.random(10)
    covmats = CovMats.random(10, 10)
    old = tangent_space(covmats.numpy_array, covmat.numpy_array)
    covmats.reset_covmats_fields()
    new = TangentSpace.tangent(covmats, covmat)

    return _get_state(old, new, "tangent space")
Ejemplo n.º 14
0
import sys
import os

sys.path.append(
    os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from Utils.Distance import Distance
from Utils.CovMat import CovMat
from oldPyRiemann.distance import distance_logdet, distance_logeuclid

m1 = CovMat.random(10)
m2 = CovMat.random(10)


def test_distance_logdet():
    new_dist = Distance.log_determinant(m1, m2)
    m1.reset_fields()
    m2.reset_fields()
    old_dist = distance_logdet(
        m1.numpy_array, m2.numpy_array
    )  # TypeError: unsupported operand type(s) for /: 'CovMat' and 'float'
    #new_dist = Distance.log_determinant(m1.numpy_array, m2.numpy_array) # AttributeError: 'numpy.ndarray' object has no attribute 'determinant

    return _get_state(old_dist, new_dist, "log determinant")


def test_log_euclidean():
    old_dist = distance_logeuclid(m1.numpy_array, m2.numpy_array)
    m1.reset_fields()
    m2.reset_fields()
    new_dist = Distance.log_euclidean(m1, m2)
Ejemplo n.º 15
0
import os
import sys
import timeit

sys.path.append(
    os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from Utils.CovMat import CovMat
from Utils.CovMats import CovMats

size = [10, 25, 50, 75, 100, 250, 500, 750, 1000]

# WARMUP
print("Warm up...")
for i in range(0, 10):
    warm_up_covmat = CovMat.random(1000)
    warm_up_covmat.expm

for i in range(0, len(size)):
    covmats = CovMats.random(10, size[i])

    t = timeit.Timer(
        "mean_wasserstein(covmats.numpy_array)",
        setup=
        "from __main__ import covmats; from oldPyRiemann.mean import mean_wasserstein; import Utils.OpenBLAS"
    )
    old_time = t.timeit(number=size[len(size) - i - 1]) / size[len(size) - i -
                                                               1]

    t = timeit.Timer(
        "covmats.reset_fields(); Mean.wasserstein(covmats)",
Ejemplo n.º 16
0
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from Utils.Geodesic import Geodesic
from Utils.CovMat import CovMat
from oldPyRiemann.geodesic import geodesic_logeuclid

m1 = CovMat.random(100)
m2 = CovMat.random(100)


def test_geodesic_log_euclidean():
    old_dist = geodesic_logeuclid(m1.numpy_array, m2.numpy_array, 0.5)
    m1.reset_fields()
    m2.reset_fields()
    new_dist = Geodesic.log_euclidean(m1, m2, 0.5)

    return _get_state(old_dist, new_dist, "geodesic riemannian")


def _get_state(old, new, func_name):
    if abs(old.sum() - new.sum()) < 1e-10:
        print("%s : PASS" % func_name)
        return True
    else:
        print("%s : FAIL" % func_name)
        return False
Ejemplo n.º 17
0
 def random(length, matrices_order, data_type=DataType.double):
     return CovMats(
         [CovMat.random(matrices_order, data_type) for i in range(length)])
Ejemplo n.º 18
0
 def randomize(self, data_type=DataType.double):
     self.__covmats_from_list([
         CovMat.random(self.matrices_order, data_type)
         for i in range(self.length)
     ])
Ejemplo n.º 19
0
 def identity(covmats):
     return CovMat.identity(covmats.matrices_order)
Ejemplo n.º 20
0
 def euclidean(covmats, sample_weight=None):
     return CovMat(covmats.average(0, sample_weight), False)
Ejemplo n.º 21
0
import os
import sys
import timeit

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from Utils.CovMat import CovMat

# WARMUP
print("Warm up...")
for i in range(0, 10):
    warm_up_covmat = CovMat.random(1000)
    warm_up_covmat.expm

size = [10, 25, 50, 75, 100, 250, 500]
matrix_order = 0

for i in range(0, len(size)):
    matrix_order = size[i]
    t = timeit.Timer("compute(matrix_order, 5)",
                     setup="from Benchmark.Custom.LogEuclideanDataAug.LogEuclideanDataAugNew import compute; from __main__ import matrix_order")
    old_time = t.timeit(number=5) / 5

    t = timeit.Timer("compute(matrix_order, 5)",
                     setup="from Benchmark.Custom.LogEuclideanDataAug.LogEuclideanDataAugNew import compute; from __main__ import matrix_order")
    new_time = t.timeit(number=5) / 5

    print("matrix size : " + str(size[i]) + "x" + str(size[i]) + "\t\told time : " + str(
        old_time) + " sec\t\t" + "new time : " + str(new_time) + " sec\t\t" + "speed up : " + str(
        old_time / new_time))
Ejemplo n.º 22
0
 def random(length, matrices_order, data_type=DataType.double):
     return CovMats([CovMat.random(matrices_order, data_type) for i in range(length)])
Ejemplo n.º 23
0
 def log_euclidean(covmats, sample_weight=None):
     logm_covmats = CovMats([covmat.logm for covmat in covmats], False)
     return CovMat(logm_covmats.average(0, sample_weight), False).expm
Ejemplo n.º 24
0
 def randomize(self, data_type=DataType.double):
     self.__covmats_from_list([CovMat.random(self.matrices_order, data_type) for i in range(self.length)])
Ejemplo n.º 25
0
import os
import sys
import timeit

sys.path.append(
    os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from Utils.CovMat import CovMat

size = [10, 25, 50, 75, 100, 250, 500, 750, 1000]

# WARMUP
print("Warm up...")
for i in range(0, 10):
    warm_up_covmat = CovMat.random(1000)
    warm_up_covmat.expm

for i in range(0, len(size)):
    covmat1 = CovMat.random(size[i])
    covmat2 = CovMat.random(size[i])

    t = timeit.Timer(
        "distance_wasserstein(covmat1.numpy_array, covmat2.numpy_array)",
        setup=
        "from __main__ import covmat1; from __main__ import covmat2; from oldPyRiemann.distance import distance_wasserstein; import Utils.OpenBLAS"
    )
    old_time = t.timeit(number=size[len(size) - i - 1]) / size[len(size) - i -
                                                               1]

    covmat1 = CovMat.random(size[i])
    covmat2 = CovMat.random(size[i])
Ejemplo n.º 26
0
import os
import sys
import timeit

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from Utils.CovMat import CovMat

size = [10, 25, 50, 75, 100, 250, 500, 750, 1000]

# WARMUP
print("Warm up...")
for i in range(0, 10):
    warm_up_covmat = CovMat.random(1000)
    warm_up_covmat.expm

for i in range(0, len(size)):
    covmat1 = CovMat.random(size[i])
    covmat2 = CovMat.random(size[i])

    t = timeit.Timer("distance_riemann(covmat1.numpy_array, covmat2.numpy_array)",
                     setup="from __main__ import covmat1; from __main__ import covmat2; from oldPyRiemann.distance import distance_riemann; import Utils.OpenBLAS")
    old_time = t.timeit(number=size[len(size) - i - 1]) / size[len(size) - i - 1]

    covmat1 = CovMat.random(size[i])
    covmat2 = CovMat.random(size[i])
    t = timeit.Timer("covmat1.reset_fields(); covmat2.reset_fields(); Distance.riemannian(covmat1, covmat2)",
                     setup="from Utils.Distance import Distance; from __main__ import covmat1; from __main__ import covmat2")
    new_time = t.timeit(number=size[len(size) - i - 1]) / size[len(size) - i - 1]

    print("matrix size : " + str(size[i]) + "x" + str(size[i]) + "\t\told time : " + str(
Ejemplo n.º 27
0
 def identity(covmats):
     return CovMat.identity(covmats.matrices_order)