Skip to content

Commit

Permalink
added data-fetchers and kernel-manager unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lambday committed Jul 29, 2016
1 parent 829fb92 commit ea9104c
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 4 deletions.
@@ -0,0 +1,64 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/

#include <memory>
#include <cstring>
#include <shogun/lib/SGMatrix.h>
#include <shogun/features/Features.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/features/streaming/StreamingDenseFeatures.h>
#include <shogun/statistical_testing/internals/DataFetcher.h>
#include <shogun/statistical_testing/internals/StreamingDataFetcher.h>
#include <shogun/statistical_testing/internals/DataFetcherFactory.h>
#include <gtest/gtest.h>

using namespace shogun;
using namespace internal;

TEST(DataFetcherFactory, get_instance)
{
const index_t dim=1;
const index_t num_vec=1;

SGMatrix<float64_t> data_p(dim, num_vec);
data_p(0, 0)=0;

using feat_type=CDenseFeatures<float64_t>;
auto feats_p=new feat_type(data_p);

std::unique_ptr<DataFetcher> fetcher(DataFetcherFactory::get_instance(feats_p));
ASSERT_TRUE(strcmp(fetcher->get_name(), "DataFetcher")==0);

CStreamingDenseFeatures<float64_t> *streaming_p=new CStreamingDenseFeatures<float64_t>(feats_p);
SG_REF(streaming_p);

std::unique_ptr<DataFetcher> streaming_fetcher(DataFetcherFactory::get_instance(streaming_p));
ASSERT_TRUE(strcmp(streaming_fetcher->get_name(), "StreamingDataFetcher")==0);
}
140 changes: 140 additions & 0 deletions tests/unit/statistical_testing/internals/DataFetcher_unittest.cc
@@ -0,0 +1,140 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/

#include <memory>
#include <algorithm>
#include <shogun/lib/SGMatrix.h>
#include <shogun/features/Features.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/statistical_testing/internals/DataFetcher.h>
#include <gtest/gtest.h>

using namespace shogun;
using namespace internal;

TEST(DataFetcher, full_data)
{
const index_t dim=3;
const index_t num_vec=8;

SGMatrix<float64_t> data_p(dim, num_vec);
std::iota(data_p.matrix, data_p.matrix+dim*num_vec, 0);

using feat_type=CDenseFeatures<float64_t>;
auto feats_p=new feat_type(data_p);

DataFetcher fetcher(feats_p);

fetcher.start();
auto curr=fetcher.next();
ASSERT_TRUE(curr!=nullptr);

auto tmp=dynamic_cast<feat_type*>(curr.get());
ASSERT_TRUE(tmp!=nullptr);

curr=fetcher.next();
ASSERT_TRUE(curr==nullptr);
fetcher.end();
}

TEST(DataFetcher, block_data)
{
const index_t dim=3;
const index_t num_vec=8;
const index_t blocksize=2;
const index_t num_blocks_per_burst=2;

SGMatrix<float64_t> data_p(dim, num_vec);
std::iota(data_p.matrix, data_p.matrix+dim*num_vec, 0);

using feat_type=CDenseFeatures<float64_t>;
auto feats_p=new feat_type(data_p);

DataFetcher fetcher(feats_p);

fetcher.fetch_blockwise()
.with_blocksize(blocksize)
.with_num_blocks_per_burst(num_blocks_per_burst);

fetcher.start();
auto curr=fetcher.next();
ASSERT_TRUE(curr!=nullptr);
while (curr!=nullptr)
{
auto tmp=dynamic_cast<feat_type*>(curr.get());
ASSERT_TRUE(tmp!=nullptr);
ASSERT_TRUE(tmp->get_num_vectors()==blocksize*num_blocks_per_burst);
curr=fetcher.next();
}
fetcher.end();
}

TEST(DataFetcher, reset_functionality)
{
const index_t dim=3;
const index_t num_vec=8;
const index_t blocksize=2;
const index_t num_blocks_per_burst=2;

SGMatrix<float64_t> data_p(dim, num_vec);
std::iota(data_p.matrix, data_p.matrix+dim*num_vec, 0);

using feat_type=CDenseFeatures<float64_t>;
auto feats_p=new feat_type(data_p);

DataFetcher fetcher(feats_p);

fetcher.start();
auto curr=fetcher.next();
ASSERT_TRUE(curr!=nullptr);

auto tmp=dynamic_cast<feat_type*>(curr.get());
ASSERT_TRUE(tmp!=nullptr);

curr=fetcher.next();
ASSERT_TRUE(curr==nullptr);

fetcher.reset();
fetcher.fetch_blockwise()
.with_blocksize(blocksize)
.with_num_blocks_per_burst(num_blocks_per_burst);

fetcher.start();
curr=fetcher.next();
ASSERT_TRUE(curr!=nullptr);
while (curr!=nullptr)
{
tmp=dynamic_cast<feat_type*>(curr.get());
ASSERT_TRUE(tmp!=nullptr);
ASSERT_TRUE(tmp->get_num_vectors()==blocksize*num_blocks_per_burst);
curr=fetcher.next();
}
fetcher.end();
}
Expand Up @@ -41,7 +41,7 @@
using namespace shogun;
using namespace internal;

TEST(DataManager, whole_data_one_distribution_normal_feats)
TEST(DataManager, full_data_one_distribution_normal_feats)
{
const index_t dim=3;
const index_t num_vec=8;
Expand Down Expand Up @@ -71,7 +71,7 @@ TEST(DataManager, whole_data_one_distribution_normal_feats)
mgr.end();
}

TEST(DataManager, whole_data_one_distribution_streaming_feats)
TEST(DataManager, full_data_one_distribution_streaming_feats)
{
const index_t dim=3;
const index_t num_vec=8;
Expand Down Expand Up @@ -104,7 +104,7 @@ TEST(DataManager, whole_data_one_distribution_streaming_feats)
mgr.end();
}

TEST(DataManager, whole_data_two_distributions_normal_feats)
TEST(DataManager, full_data_two_distributions_normal_feats)
{
const index_t dim=3;
const index_t num_vec=8;
Expand Down Expand Up @@ -142,7 +142,7 @@ TEST(DataManager, whole_data_two_distributions_normal_feats)
ASSERT_TRUE(next_burst.empty());
}

TEST(DataManager, whole_data_two_distributions_streaming_feats)
TEST(DataManager, full_data_two_distributions_streaming_feats)
{
const index_t dim=3;
const index_t num_vec=8;
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/statistical_testing/internals/KernelManager_unittest.cc
@@ -0,0 +1,69 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/

#include <shogun/lib/SGMatrix.h>
#include <shogun/features/Features.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/statistical_testing/internals/KernelManager.h>
#include <gtest/gtest.h>

using namespace shogun;
using namespace internal;

TEST(KernelManager, store_precompute_restore)
{
const index_t dim=1;
const index_t num_vec=1;
const index_t num_kernels=1;

SGMatrix<float64_t> data_p(dim, num_vec);
data_p(0, 0)=0;

auto feats=new CDenseFeatures<float64_t>(data_p);
auto kernel=new CGaussianKernel();
kernel->set_width(0.5);

KernelManager kernel_mgr(num_kernels);
const KernelManager& const_kernel_mgr=kernel_mgr;

kernel_mgr.kernel_at(0)=kernel;
ASSERT_TRUE(const_kernel_mgr.kernel_at(0)->get_kernel_type()==K_GAUSSIAN);

CKernel* k=const_kernel_mgr.kernel_at(0);
k->init(feats, feats);
kernel_mgr.precompute_kernel_at(0);
ASSERT_TRUE(const_kernel_mgr.kernel_at(0)!=kernel);
ASSERT_TRUE(const_kernel_mgr.kernel_at(0)->get_kernel_type()==K_CUSTOM);

kernel_mgr.restore_kernel_at(0);
ASSERT_TRUE(const_kernel_mgr.kernel_at(0)==kernel);
ASSERT_TRUE(const_kernel_mgr.kernel_at(0)->get_kernel_type()==K_GAUSSIAN);
}

0 comments on commit ea9104c

Please sign in to comment.