Skip to content

Commit

Permalink
Add global fixture with binary label data
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing committed May 25, 2017
1 parent 380c06f commit ddd4519
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/shogun/features/DataGenerator.cpp
Expand Up @@ -129,7 +129,7 @@ SGMatrix<float64_t> CDataGenerator::generate_sym_mix_gauss(index_t m,

return result;
}
#ifdef HAVE_LAPACK

SGMatrix<float64_t> CDataGenerator::generate_gaussians(index_t m, index_t n, index_t dim)
{
/* evtl. allocate space */
Expand Down Expand Up @@ -161,4 +161,3 @@ SGMatrix<float64_t> CDataGenerator::generate_gaussians(index_t m, index_t n, ind

return result;
}
#endif /* HAVE_LAPACK */
2 changes: 0 additions & 2 deletions src/shogun/features/DataGenerator.h
Expand Up @@ -85,7 +85,6 @@ class CDataGenerator: public CSGObject
float64_t d, float64_t angle,
SGMatrix<float64_t> target=SGMatrix<float64_t>());

#ifdef HAVE_LAPACK
/** Produces samples of gaussians
* The functions produces m number of samples of each gaussians (n number) with
* the given dimension.
Expand All @@ -97,7 +96,6 @@ class CDataGenerator: public CSGObject
* of the first gaussian, m number of second etc.
*/
static SGMatrix<float64_t> generate_gaussians(index_t m, index_t n, index_t dim);
#endif /* HAVE_LAPACK */

virtual const char* get_name() const { return "DataGenerator"; }

Expand Down
14 changes: 9 additions & 5 deletions src/shogun/mathematics/linalg/LinalgNamespace.h
Expand Up @@ -416,7 +416,8 @@ T dot(const SGVector<T>& a, const SGVector<T>& b)
return infer_backend(a, b)->dot(a, b);
}

/** Performs the operation C = A .* B where ".*" denotes elementwise multiplication
/** Performs the operation C = A .* B where ".*" denotes elementwise
* multiplication
* on matrix blocks.
*
* This version returns the result in-place.
Expand Down Expand Up @@ -498,7 +499,8 @@ void element_prod(SGMatrix<T>& a, SGMatrix<T>& b, SGMatrix<T>& result)
infer_backend(a, b)->element_prod(a, b, result);
}

/** Performs the operation C = A .* B where ".*" denotes elementwise multiplication.
/** Performs the operation C = A .* B where ".*" denotes elementwise
* multiplication.
*
* This version returns the result in a newly created matrix.
*
Expand Down Expand Up @@ -928,9 +930,11 @@ T sum_symmetric(const Block<SGMatrix<T>>& a, bool no_diag=false)
/**
* Method that computes colwise sum of co-efficients of a dense matrix
*
* @param mat a matrix whose colwise sum has to be computed
* @param no_diag If true, diagonal entries are excluded from the sum. Default: false
* @return The colwise sum of co-efficients computed as \f$s_j=\sum_{i}b_{i,j}\f$
* @param Mat a matrix whose colwise sum has to be computed
* @param no_diag If true, diagonal entries are excluded from the sum. Default:
* false
* @return The colwise sum of co-efficients computed as
* \f$s_j=\sum_{i}b_{i,j}\f$
*/
template <typename T>
SGVector<T> colwise_sum(const SGMatrix<T>& mat, bool no_diag=false)
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/base/Some_unittest.cc
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include <shogun/base/some.h>
#include <shogun/kernel/GaussianKernel.h>
#include <gtest/gtest.h>

#ifdef HAVE_CXX11
using namespace shogun;
Expand All @@ -21,10 +21,10 @@ TEST(Some,basic)
// reference is held
EXPECT_EQ(2, kernel->ref_count());
}
EXPECT_TRUE(raw);
// last references now
EXPECT_EQ(1, raw->ref_count());
SG_UNREF(raw);
EXPECT_TRUE(raw);
// last references now
EXPECT_EQ(1, raw->ref_count());
SG_UNREF(raw);
}

TEST(Some,reassignment)
Expand Down
18 changes: 11 additions & 7 deletions tests/unit/base/main_unittest.cc
@@ -1,15 +1,18 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <shogun/base/init.h>
#include <shogun/io/SGIO.h>

#include "environments/LinearTestEnvironment.h"

using namespace shogun;
using ::testing::Test;
using ::testing::UnitTest;
using ::testing::TestCase;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::TestEventListener;
using ::testing::Environment;

class FailurePrinter : public TestEventListener {
public:
Expand Down Expand Up @@ -37,17 +40,17 @@ class FailurePrinter : public TestEventListener {

void FailurePrinter::OnTestPartResult(const TestPartResult& test_part_result)
{
if (test_part_result.failed())
{
_listener->OnTestPartResult(test_part_result);
printf("\n");
}
if (test_part_result.failed())
{
_listener->OnTestPartResult(test_part_result);
printf("\n");
}
}

void FailurePrinter::OnTestEnd(const TestInfo& test_info)
{
if (test_info.result()->Failed())
_listener->OnTestEnd(test_info);
_listener->OnTestEnd(test_info);
}

int main(int argc, char** argv)
Expand All @@ -67,6 +70,7 @@ int main(int argc, char** argv)

init_shogun_with_defaults();
sg_io->set_loglevel(MSG_WARN);
::testing::AddGlobalTestEnvironment(new LinearTestEnvironment());
int ret = RUN_ALL_TESTS();
exit_shogun();

Expand Down
26 changes: 9 additions & 17 deletions tests/unit/classifier/svm/SVMOcas_unittest.cc
Expand Up @@ -3,42 +3,34 @@
#include <shogun/features/DenseFeatures.h>
#include <gtest/gtest.h>

#include "environments/LinearTestEnvironment.h"

using namespace shogun;

#ifdef USE_GPL_SHOGUN
#ifdef HAVE_LAPACK
TEST(SVMOcasTest,train)
{
index_t num_samples = 50;
index_t num_samples = 100;
CMath::init_random(5);
SGMatrix<float64_t> data =
CDataGenerator::generate_gaussians(num_samples, 2, 2);
CDenseFeatures<float64_t> features(data);
std::shared_ptr<GaussianCheckerboard> mockData =
LinearTestEnvironment::instance().getBinaryLabelData();

SGVector<index_t> train_idx(num_samples), test_idx(num_samples);
SGVector<float64_t> labels(num_samples);
for (index_t i = 0, j = 0; i < data.num_cols; ++i)
{
if (i % 2 == 0)
train_idx[j] = i;
else
test_idx[j++] = i;

labels[i/2] = (i < data.num_cols/2) ? 1.0 : -1.0;
}

CDenseFeatures<float64_t>* train_feats = (CDenseFeatures<float64_t>*)features.copy_subset(train_idx);
CDenseFeatures<float64_t>* test_feats = (CDenseFeatures<float64_t>*)features.copy_subset(test_idx);
CDenseFeatures<float64_t>* train_feats = mockData->get_features_train();
CDenseFeatures<float64_t>* test_feats = mockData->get_features_test();

CBinaryLabels* ground_truth = new CBinaryLabels(labels);
CBinaryLabels* ground_truth = mockData->get_labels_test();

CSVMOcas* ocas = new CSVMOcas(1.0, train_feats, ground_truth);
ocas->parallel->set_num_threads(1);
ocas->set_epsilon(1e-5);
ocas->train();
float64_t objective = ocas->compute_primal_objective();

EXPECT_NEAR(objective, 0.022321841487323236, 1e-2);
EXPECT_NEAR(objective, 0.024344632618686062, 1e-2);

CLabels* pred = ocas->apply(test_feats);
for (int i = 0; i < num_samples; ++i)
Expand Down
114 changes: 114 additions & 0 deletions tests/unit/environments/GaussianCheckerboard.h
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* 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.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* 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 HOLDER 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.
*
* Authors: 2016 MikeLing, Viktor Gal, Sergey Lisitsyn, Heiko Strathmann
*/

#ifndef BINARY_LABEL_DATA_HPP
#define BINARY_LABEL_DATA_HPP

#include <shogun/features/DataGenerator.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/labels/BinaryLabels.h>

using namespace shogun;

class GaussianCheckerboard
{
public:
GaussianCheckerboard(const int32_t num_samples)
{
SGMatrix<float64_t> data =
CDataGenerator::generate_gaussians(num_samples, 2, 2);
CDenseFeatures<float64_t> features(data);

SGVector<index_t> train_idx(num_samples), test_idx(num_samples);
SGVector<float64_t> labels(num_samples);
for (index_t i = 0, j = 0; i < data.num_cols; ++i)
{
if (i % 2 == 0)
train_idx[j] = i;
else
test_idx[j++] = i;

labels[i / 2] = (i < data.num_cols / 2) ? 1.0 : -1.0;
}

features_train =
(CDenseFeatures<float64_t>*)features.copy_subset(train_idx);
features_test =
(CDenseFeatures<float64_t>*)features.copy_subset(test_idx);

CBinaryLabels temp_labels = CBinaryLabels(labels);
labels_train = (CBinaryLabels*)temp_labels.clone();
labels_test = (CBinaryLabels*)temp_labels.clone();
}

~GaussianCheckerboard()
{
}

/* get the traning features */
CDenseFeatures<float64_t>* get_features_train()
{
return features_train;
}

/* get the test features */
CDenseFeatures<float64_t>* get_features_test()
{
return features_test;
}

/* get the test labels */
CBinaryLabels* get_labels_train()
{
return labels_train;
}

/* get the traning labels */
CBinaryLabels* get_labels_test()
{
return labels_test;
}

protected:
// data for training
CDenseFeatures<float64_t>* features_train;

// data for testing
CDenseFeatures<float64_t>* features_test;

// traning label
CBinaryLabels* labels_train;

// testing label
CBinaryLabels* labels_test;
};
#endif
66 changes: 66 additions & 0 deletions tests/unit/environments/LinearTestEnvironment.h
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* 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.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* 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 HOLDER 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.
*
* Authors: 2016 MikeLing, Viktor Gal, Sergey Lisitsyn, Heiko Strathmann
*/

#ifndef LINEARTESTENVIRONMENT_HPP
#define LINEARTESTENVIRONMENT_HPP

#include "GaussianCheckerboard.h"
#include <gtest/gtest.h>
#include <memory>

using namespace shogun;
using namespace std;
using ::testing::Environment;
class LinearTestEnvironment : public ::testing::Environment
{
public:
LinearTestEnvironment()
{
mBinaryLabelData = std::shared_ptr<GaussianCheckerboard>(
new GaussianCheckerboard(100, 2, 2));
}

static LinearTestEnvironment& instance()
{
static LinearTestEnvironment mInstance;
return mInstance;
}

std::shared_ptr<GaussianCheckerboard> getBinaryLabelData() const
{
return mBinaryLabelData;
}

protected:
std::shared_ptr<GaussianCheckerboard> mBinaryLabelData;
};
#endif

0 comments on commit ddd4519

Please sign in to comment.