Commit a4efed56 authored by André Anjos's avatar André Anjos 💬
Browse files

Add more scatter tests for different call variations

parent 12dbee2c
......@@ -441,7 +441,7 @@ PyObject* py_scatters_nocheck (PyObject*, PyObject* args, PyObject* kwds) {
case NPY_FLOAT32:
{
std::vector<blitz::Array<float,2>> cxxdata;
for (Py_ssize_t i=1; i<PyTuple_GET_SIZE(data); ++i) {
for (Py_ssize_t i=0; i<PyTuple_GET_SIZE(data); ++i) {
cxxdata.push_back(*PyBlitzArrayCxx_AsBlitz<float,2>
((PyBlitzArrayObject*)PyTuple_GET_ITEM(data,i)));
bob::math::scatters_(cxxdata,
......@@ -456,7 +456,7 @@ PyObject* py_scatters_nocheck (PyObject*, PyObject* args, PyObject* kwds) {
case NPY_FLOAT64:
{
std::vector<blitz::Array<double,2>> cxxdata;
for (Py_ssize_t i=1; i<PyTuple_GET_SIZE(data); ++i) {
for (Py_ssize_t i=0; i<PyTuple_GET_SIZE(data); ++i) {
cxxdata.push_back(*PyBlitzArrayCxx_AsBlitz<double,2>
((PyBlitzArrayObject*)PyTuple_GET_ITEM(data,i)));
bob::math::scatters_(cxxdata,
......
......@@ -9,7 +9,7 @@
"""
import os, sys
from xbob.math import scatter, scatters
from xbob.math import scatter, scatter_, scatters, scatters_
import numpy
import nose.tools
......@@ -43,15 +43,84 @@ def test_scatter():
data = numpy.random.rand(50,4)
# This test demonstrates how to use the scatter matrix function of bob.
S, M = scatter(data.T)
S, M = scatter(data)
S = S.as_ndarray()
M = M.as_ndarray()
S /= (data.shape[1]-1)
S /= (data.shape[0]-1)
# Do the same with numpy and compare. Note that with numpy we are computing
# the covariance matrix which is the scatter matrix divided by (N-1).
K = numpy.array(numpy.cov(data))
M_ = means(data.T)
K = numpy.array(numpy.cov(data.T))
M_ = means(data)
assert (abs(S-K) < 1e-10).all()
assert (abs(M-M_) < 1e-10).all()
def test_scatter_variation_1():
data = numpy.random.rand(50,4)
# This test demonstrates how to use the scatter matrix function of bob.
M = numpy.ndarray((data.shape[1],), dtype=float)
S = scatter(data, m=M)
S = S[0].as_ndarray()
S /= (data.shape[0]-1)
# Do the same with numpy and compare. Note that with numpy we are computing
# the covariance matrix which is the scatter matrix divided by (N-1).
K = numpy.array(numpy.cov(data.T))
M_ = means(data)
assert (abs(S-K) < 1e-10).all()
assert (abs(M-M_) < 1e-10).all()
def test_scatter_variation_2():
data = numpy.random.rand(50,4)
# This test demonstrates how to use the scatter matrix function of bob.
S = numpy.ndarray((data.shape[1], data.shape[1]), dtype=float)
M = scatter(data, s=S)
M = M[0].as_ndarray()
S /= (data.shape[0]-1)
# Do the same with numpy and compare. Note that with numpy we are computing
# the covariance matrix which is the scatter matrix divided by (N-1).
K = numpy.array(numpy.cov(data.T))
M_ = means(data)
assert (abs(S-K) < 1e-10).all()
assert (abs(M-M_) < 1e-10).all()
def test_scatter_variation_3():
data = numpy.random.rand(50,4)
# This test demonstrates how to use the scatter matrix function of bob.
S = numpy.ndarray((data.shape[1], data.shape[1]), dtype=float)
M = numpy.ndarray((data.shape[1],), dtype=float)
retval = scatter(data, m=M, s=S)
assert not retval
S /= (data.shape[0]-1)
# Do the same with numpy and compare. Note that with numpy we are computing
# the covariance matrix which is the scatter matrix divided by (N-1).
K = numpy.array(numpy.cov(data.T))
M_ = means(data)
assert (abs(S-K) < 1e-10).all()
assert (abs(M-M_) < 1e-10).all()
def test_fast_scatter():
data = numpy.random.rand(50,4)
# This test demonstrates how to use the scatter matrix function of bob.
S = numpy.ndarray((data.shape[1], data.shape[1]), dtype=float)
M = numpy.ndarray((data.shape[1],), dtype=float)
scatter_(data, S, M)
S /= (data.shape[0]-1)
# Do the same with numpy and compare. Note that with numpy we are computing
# the covariance matrix which is the scatter matrix divided by (N-1).
K = numpy.array(numpy.cov(data.T))
M_ = means(data)
assert (abs(S-K) < 1e-10).all()
assert (abs(M-M_) < 1e-10).all()
......@@ -63,32 +132,62 @@ def test_scatters():
numpy.random.rand(50,4),
]
# Compares bob's implementation against pythonic one
# 1. python
Sw_, Sb_, m_ = py_scatters(data)
# 2.a. bob
Sw, Sb, m = scatters(data)
# 3.a. comparison
assert numpy.allclose(Sw, Sw_)
assert numpy.allclose(Sb, Sb_)
assert numpy.allclose(m, m_)
def test_scatters_variation_1():
data = [
numpy.random.rand(50,4),
numpy.random.rand(50,4),
numpy.random.rand(50,4),
]
Sw_, Sb_, m_ = py_scatters(data)
N = data[0].shape[1]
# 2.b. bob
Sw = numpy.ndarray((N,N), numpy.float64)
Sb = numpy.ndarray((N,N), numpy.float64)
m = numpy.ndarray((N,), numpy.float64)
scatters(data, Sw, Sb, m)
# 3.b comparison
assert not scatters(data, Sw, Sb, m)
assert numpy.allclose(Sw, Sw_)
assert numpy.allclose(Sb, Sb_)
assert numpy.allclose(m, m_)
# 2.c. bob
def test_scatters_variation_2():
data = [
numpy.random.rand(50,4),
numpy.random.rand(50,4),
numpy.random.rand(50,4),
]
Sw_, Sb_, m_ = py_scatters(data)
N = data[0].shape[1]
Sw = numpy.ndarray((N,N), numpy.float64)
Sb = numpy.ndarray((N,N), numpy.float64)
scatters(data, Sw, Sb)
# 3.c comparison
assert len(scatters(data, Sw, Sb)) == 1
assert numpy.allclose(Sw, Sw_)
assert numpy.allclose(Sb, Sb_)
def test_fast_scatters():
data = [
numpy.random.rand(50,4),
numpy.random.rand(50,4),
numpy.random.rand(50,4),
]
Sw_, Sb_, m_ = py_scatters(data)
Sw = numpy.empty_like(Sw_)
Sb = numpy.empty_like(Sb_)
m = numpy.empty_like(m_)
scatters_(data, Sw, Sb, m)
assert numpy.allclose(Sw, Sw_)
assert numpy.allclose(Sb, Sb_)
assert numpy.allclose(m, m_)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment