Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new factory for creating objects
Populate for kernel, machine, features, labels
- Loading branch information
Showing
11 changed files
with
187 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
%{ | ||
#include <shogun/util/factory.h> | ||
%} | ||
%include <shogun/util/factory.h> | ||
|
||
%template(features) shogun::features<float64_t>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* This software is distributed under BSD 3-clause license (see LICENSE file). | ||
* | ||
* Authors: Heiko Strathmann | ||
*/ | ||
#ifndef FACTORY_H_ | ||
#define FACTORY_H_ | ||
|
||
#include <shogun/base/class_list.h> | ||
#include <shogun/features/DenseFeatures.h> | ||
#include <shogun/labels/DenseLabels.h> | ||
#include <shogun/io/CSVFile.h> | ||
#include <shogun/io/SGIO.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
CKernel* kernel(const std::string& name); | ||
CMachine* machine(const std::string& name); | ||
CFeatures* features(CFile* file, EPrimitiveType primitive_type=PT_FLOAT64); | ||
template <class T> CFeatures* features(SGMatrix<T> mat); | ||
CLabels* labels(CFile* file); | ||
|
||
CFile* csv_file(std::string fname, char rw='r'); | ||
|
||
#define BASE_CLASS_FACTORY(T, factory_name) \ | ||
T* factory_name(const std::string& name) \ | ||
{ \ | ||
return create_object<T>(name.c_str()); \ | ||
} | ||
|
||
BASE_CLASS_FACTORY(CKernel, kernel) | ||
BASE_CLASS_FACTORY(CMachine, machine) | ||
|
||
template <class T> | ||
CFeatures* features(SGMatrix<T> mat) | ||
{ | ||
CFeatures* features = new CDenseFeatures<T>(mat); | ||
SG_REF(features); | ||
return features; | ||
} | ||
|
||
CFeatures* features(CFile* file, EPrimitiveType primitive_type) | ||
{ | ||
REQUIRE(file, "No file provided.\n"); | ||
CFeatures* result = nullptr; | ||
|
||
if (dynamic_cast<CCSVFile*>(file)) | ||
{ | ||
switch (primitive_type) | ||
{ | ||
case PT_FLOAT64: | ||
result = new CDenseFeatures<float64_t>(); | ||
break; | ||
default: | ||
SG_SNOTIMPLEMENTED | ||
} | ||
result->load(file); | ||
} | ||
else | ||
SG_SERROR("Cannot load features from %s.\n", file->get_name()); | ||
|
||
SG_REF(result); | ||
return result; | ||
} | ||
|
||
CLabels* labels(CFile* file) | ||
{ | ||
REQUIRE(file, "No file provided.\n"); | ||
CLabels* result = nullptr; | ||
|
||
if (dynamic_cast<CCSVFile*>(file)) | ||
{ | ||
CDenseLabels* result_ = new CDenseLabels(); | ||
result_->load(file); | ||
result = result_; | ||
} | ||
else | ||
SG_SERROR("Cannot load labels from file %s.\n", file->get_name()); | ||
|
||
SG_REF(result); | ||
return result; | ||
} | ||
|
||
CFile* csv_file(std::string fname, char rw) | ||
{ | ||
CFile* result = new CCSVFile(fname.c_str(), rw); | ||
SG_REF(result); | ||
return result; | ||
} | ||
|
||
} | ||
#endif // FACTORY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <shogun/base/class_list.h> | ||
#include <shogun/kernel/GaussianKernel.h> | ||
#include <shogun/classifier/svm/LibSVM.h> | ||
#include <shogun/features/DenseFeatures.h> | ||
#include <shogun/util/factory.h> | ||
#include "utils/Utils.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace shogun; | ||
|
||
class GaussianKernel; | ||
|
||
TEST(Factory,kernel) | ||
{ | ||
auto* obj = kernel("GaussianKernel"); | ||
EXPECT_TRUE(obj != nullptr); | ||
EXPECT_TRUE(dynamic_cast<CGaussianKernel*>(obj) != nullptr); | ||
delete obj; | ||
} | ||
|
||
TEST(Factory, machine) | ||
{ | ||
auto* obj = machine("LibSVM"); | ||
EXPECT_TRUE(obj != nullptr); | ||
EXPECT_TRUE(dynamic_cast<CLibSVM*>(obj) != nullptr); | ||
delete obj; | ||
} | ||
|
||
TEST(Factory, features_from_matrix) | ||
{ | ||
SGMatrix<float64_t> mat(2,3); | ||
auto* obj = features(mat); | ||
EXPECT_TRUE(obj != nullptr); | ||
EXPECT_TRUE(dynamic_cast<CDenseFeatures<float64_t>*>(obj) != nullptr); | ||
delete obj; | ||
} | ||
|
||
TEST(Factory, features_dense_from_file) | ||
{ | ||
std::string filename = "Factory-features_from_file.XXXXXX"; | ||
|
||
SGMatrix<float64_t> mat(2,3); | ||
mat.set_const(1); | ||
generate_temp_filename(const_cast<char*>(filename.c_str())); | ||
auto file_save=some<CCSVFile>(filename.c_str(), 'w'); | ||
mat.save(file_save); | ||
file_save->close(); | ||
|
||
auto file_load = some<CCSVFile>(filename.c_str(), 'r'); | ||
auto* obj = features(file_load, PT_FLOAT64); | ||
EXPECT_TRUE(obj != nullptr); | ||
auto* cast = dynamic_cast<CDenseFeatures<float64_t>*>(obj); | ||
ASSERT_TRUE(cast != nullptr); | ||
auto loaded_mat = cast->get_feature_matrix(); | ||
EXPECT_TRUE(loaded_mat.equals(mat)); | ||
delete obj; | ||
} | ||
|
||
TEST(Factory, labels_from_file) | ||
{ | ||
std::string filename = "Factory-labels_from_file.XXXXXX"; | ||
|
||
SGVector<float64_t> vec(3); | ||
vec.set_const(1); | ||
generate_temp_filename(const_cast<char*>(filename.c_str())); | ||
auto file_save=some<CCSVFile>(filename.c_str(), 'w'); | ||
vec.save(file_save); | ||
file_save->close(); | ||
|
||
auto file_load = some<CCSVFile>(filename.c_str(), 'r'); | ||
auto* obj = labels(file_load); | ||
EXPECT_TRUE(obj != nullptr); | ||
auto* cast = dynamic_cast<CDenseLabels*>(obj); | ||
ASSERT_TRUE(cast != nullptr); | ||
auto loaded_vec = cast->get_labels(); | ||
EXPECT_TRUE(loaded_vec.equals(vec)); | ||
delete obj; | ||
} |