[arrayfire] 04/248: Additional unit tests for mean

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Nov 17 15:53:48 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch dfsg-clean
in repository arrayfire.

commit 8cd7964f06f1bef77ddf6aa6381361042d7b6291
Author: pradeep <pradeep at arrayfire.com>
Date:   Tue Jun 30 17:12:17 2015 -0400

    Additional unit tests for mean
---
 test/data     |   2 +-
 test/mean.cpp | 172 ++++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 131 insertions(+), 43 deletions(-)

diff --git a/test/data b/test/data
index ab1a8b6..a695974 160000
--- a/test/data
+++ b/test/data
@@ -1 +1 @@
-Subproject commit ab1a8b6d585eedeed6eb99eb1c663c27ffcb4330
+Subproject commit a695974e183c388b420b19f2e5a56445f253cd06
diff --git a/test/mean.cpp b/test/mean.cpp
index 15a2c35..967e7c9 100644
--- a/test/mean.cpp
+++ b/test/mean.cpp
@@ -13,6 +13,9 @@
 #include <af/traits.hpp>
 #include <string>
 #include <vector>
+#include <ctime>
+#include <iostream>
+#include <algorithm>
 #include <testHelpers.hpp>
 
 using std::string;
@@ -67,7 +70,7 @@ struct meanOutType {
 };
 
 template<typename T>
-void meanDimTest(string pFileName, dim_t dim)
+void meanDimTest(string pFileName, dim_t dim, bool isWeighted=false)
 {
     typedef typename meanOutType<T>::type outType;
     if (noDoubleTests<T>()) return;
@@ -79,32 +82,52 @@ void meanDimTest(string pFileName, dim_t dim)
 
     readTestsFromFile<int,float>(pFileName, numDims, in, tests);
 
-    af::dim4 dims      = numDims[0];
-    af_array outArray  = 0;
-    af_array inArray   = 0;
+    if (!isWeighted) {
+        af::dim4 dims = numDims[0];
+        vector<T> input(in[0].begin(), in[0].end());
 
-    vector<T> input(in[0].begin(), in[0].end());
+        af::array inArray(dims, &(input.front()));
 
-    ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(input.front()),
-                dims.ndims(), dims.get(), (af_dtype)af::dtype_traits<T>::af_type));
+        af::array outArray = af::mean(inArray, dim);
 
-    ASSERT_EQ(AF_SUCCESS, af_mean(&outArray, inArray, dim));
+        outType *outData = new outType[dims.elements()];
 
-    outType *outData = new outType[dims.elements()];
+        outArray.host((void*)outData);
 
-    ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
+        vector<outType> currGoldBar(tests[0].begin(), tests[0].end());
+        size_t nElems = currGoldBar.size();
+        for (size_t elIter=0; elIter<nElems; ++elIter) {
+            ASSERT_NEAR(::real(currGoldBar[elIter]), ::real(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
+            ASSERT_NEAR(::imag(currGoldBar[elIter]), ::imag(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
+        }
 
-    vector<outType> currGoldBar(tests[0].begin(), tests[0].end());
-    size_t nElems = currGoldBar.size();
-    for (size_t elIter=0; elIter<nElems; ++elIter) {
-        ASSERT_NEAR(::real(currGoldBar[elIter]), ::real(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
-        ASSERT_NEAR(::imag(currGoldBar[elIter]), ::imag(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
-    }
+        // cleanup
+        delete[] outData;
+    } else {
+        af::dim4 dims  = numDims[0];
+        af::dim4 wdims = numDims[1];
+        vector<T> input(in[0].begin(), in[0].end());
+        vector<float> weights(in[1].begin(), in[1].end());
+
+        af::array inArray(dims, &(input.front()));
+        af::array wtsArray(wdims, &(weights.front()));
+
+        af::array outArray = af::mean(inArray, wtsArray, dim);
+
+        outType *outData = new outType[dims.elements()];
+
+        outArray.host((void*)outData);
+
+        vector<outType> currGoldBar(tests[0].begin(), tests[0].end());
+        size_t nElems = currGoldBar.size();
+        for (size_t elIter=0; elIter<nElems; ++elIter) {
+            ASSERT_NEAR(::real(currGoldBar[elIter]), ::real(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
+            ASSERT_NEAR(::imag(currGoldBar[elIter]), ::imag(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
+        }
 
-    // cleanup
-    delete[] outData;
-    ASSERT_EQ(AF_SUCCESS, af_release_array(inArray));
-    ASSERT_EQ(AF_SUCCESS, af_release_array(outArray));
+        // cleanup
+        delete[] outData;
+    }
 }
 
 TYPED_TEST(Mean, Dim0Matrix)
@@ -112,6 +135,16 @@ TYPED_TEST(Mean, Dim0Matrix)
     meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim0_matrix.test"), 0);
 }
 
+TYPED_TEST(Mean, Wtd_Dim0Matrix)
+{
+    meanDimTest<TypeParam>(/*string(TEST_DIR"/mean/wtd_mean_dim0_mat.test")*/"/home/pradeep/gitroot/arrayfire_data/mean/wtd_mean_dim0_mat.test", 0, true);
+}
+
+TYPED_TEST(Mean, Wtd_Dim1Matrix)
+{
+    meanDimTest<TypeParam>(/*string(TEST_DIR"/mean/wtd_mean_dim0_mat.test")*/"/home/pradeep/gitroot/arrayfire_data/mean/wtd_mean_dim1_mat.test", 1, true);
+}
+
 TYPED_TEST(Mean, Dim1Cube)
 {
     meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim1_cube.test"), 1);
@@ -137,13 +170,8 @@ TYPED_TEST(Mean, Dim2HyperCube)
     meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim2_hypercube.test"), 2);
 }
 
-//////////////////////////////// CPP ////////////////////////////////////
-// test mean_all interface using cpp api
-
-#include <iostream>
-
 template<typename T>
-void testCPPMean(T const_value, af::dim4 dims)
+void meanAllTest(T const_value, af::dim4 dims)
 {
     typedef typename meanOutType<T>::type outType;
     if (noDoubleTests<T>()) return;
@@ -168,42 +196,102 @@ void testCPPMean(T const_value, af::dim4 dims)
     ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
 }
 
-TEST(Mean, CPP_f64)
+TEST(MeanAll, f64)
 {
-    testCPPMean<double>(2.1, af::dim4(10, 10, 1, 1));
+    meanAllTest<double>(2.1, af::dim4(10, 10, 1, 1));
 }
 
-TEST(Mean, CPP_f32)
+TEST(MeanAll, f32)
 {
-    testCPPMean<float>(2.1f, af::dim4(10, 5, 2, 1));
+    meanAllTest<float>(2.1f, af::dim4(10, 5, 2, 1));
 }
 
-TEST(Mean, CPP_s32)
+TEST(MeanAll, s32)
 {
-    testCPPMean<int>(2, af::dim4(5, 5, 2, 2));
+    meanAllTest<int>(2, af::dim4(5, 5, 2, 2));
 }
 
-TEST(Mean, CPP_u32)
+TEST(MeanAll, u32)
 {
-    testCPPMean<unsigned>(2, af::dim4(100, 1, 1, 1));
+    meanAllTest<unsigned>(2, af::dim4(100, 1, 1, 1));
 }
 
-TEST(Mean, CPP_s8)
+TEST(MeanAll, s8)
 {
-    testCPPMean<char>(2, af::dim4(5, 5, 2, 2));
+    meanAllTest<char>(2, af::dim4(5, 5, 2, 2));
 }
 
-TEST(Mean, CPP_u8)
+TEST(MeanAll, u8)
 {
-    testCPPMean<uchar>(2, af::dim4(100, 1, 1, 1));
+    meanAllTest<uchar>(2, af::dim4(100, 1, 1, 1));
 }
 
-TEST(Mean, CPP_cfloat)
+TEST(MeanAll, c32)
 {
-    testCPPMean<cfloat>(cfloat(2.1f), af::dim4(10, 5, 2, 1));
+    meanAllTest<cfloat>(cfloat(2.1f), af::dim4(10, 5, 2, 1));
+}
+
+TEST(MeanAll, c64)
+{
+    meanAllTest<cdouble>(cdouble(2.1), af::dim4(10, 10, 1, 1));
+}
+
+
+template<typename T>
+T random() { return T(std::rand()%10); }
+
+template<> cfloat random<cfloat>() { return cfloat(float(std::rand()%10), float(std::rand()%10)); }
+
+template<> cdouble random<cdouble>() { return cdouble(double(std::rand()%10), double(std::rand()%10)); }
+
+template<typename T>
+class WeightedMean : public ::testing::Test
+{
+    public:
+        virtual void SetUp() {}
+};
+
+// register the type list
+TYPED_TEST_CASE(WeightedMean, TestTypes);
+
+template<typename T, typename wtsType>
+void weightedMeanAllTest(af::dim4 dims)
+{
+    typedef typename meanOutType<T>::type outType;
+
+    if (noDoubleTests<T>()) return;
+    if (noDoubleTests<outType>()) return;
+    if (noDoubleTests<wtsType>()) return;
+
+    using af::array;
+    using af::mean;
+
+    std::srand(std::time(0));
+
+    vector<T> data(dims.elements());
+    vector<wtsType> wts(dims.elements());
+    std::generate(data.begin(), data.end(), random<T>);
+    std::generate(wts.begin(), wts.end(), random<wtsType>);
+
+    outType wtdSum = outType(0);
+    wtsType wtsSum = wtsType(0);
+
+    for(int i = 0; i < (int)data.size(); i++) {
+        wtdSum = wtdSum + data[i]*wts[i];
+        wtsSum = wtsSum + wts[i];
+    }
+
+    outType gold = wtdSum / wtsSum;
+
+    array a(dims, &(data.front()));
+    array w(dims, &(wts.front()));
+    outType output = mean<outType>(a, w);
+
+    ASSERT_NEAR(::real(output), ::real(gold), 1.0e-3);
+    ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
 }
 
-TEST(Mean, CPP_cdouble)
+TYPED_TEST(WeightedMean, Basic)
 {
-    testCPPMean<cdouble>(cdouble(2.1), af::dim4(10, 10, 1, 1));
+    weightedMeanAllTest<TypeParam, float>(af::dim4(66, 66, 31, 17));
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list