Skip to content

Commit

Permalink
[WIP] drop obtain_from and use either ctors or .as<>
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Feb 6, 2018
1 parent 56ae5e1 commit e7fa3c2
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 75 deletions.
9 changes: 7 additions & 2 deletions src/shogun/features/CombinedDotFeatures.h
@@ -1,8 +1,8 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann,
* Vladislav Horbatiuk, Evgeniy Andreev, Yuyu Zhang, Evan Shelhamer,
* Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann,
* Vladislav Horbatiuk, Evgeniy Andreev, Yuyu Zhang, Evan Shelhamer,
* Björn Esser, Evangelos Anagnostopoulos
*/

Expand Down Expand Up @@ -147,6 +147,11 @@ class CCombinedDotFeatures : public CDotFeatures
* @return feature class
*/
virtual EFeatureClass get_feature_class() const
{
return this->class_type();
}

static EFeatureClass class_type()
{
return C_COMBINED_DOT;
}
Expand Down
56 changes: 26 additions & 30 deletions src/shogun/features/DenseFeatures.cpp
@@ -1,10 +1,10 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Saurabh Mahindre, Soumyajit De, Heiko Strathmann,
* Sergey Lisitsyn, Sanuj Sharma, Chiyuan Zhang, Viktor Gal,
* Michele Mazzoni, Vladislav Horbatiuk, Kevin Hughes, Weijie Lin,
* Fernando Iglesias, Björn Esser, Evgeniy Andreev,
* Authors: Soeren Sonnenburg, Saurabh Mahindre, Soumyajit De, Heiko Strathmann,
* Sergey Lisitsyn, Sanuj Sharma, Chiyuan Zhang, Viktor Gal,
* Michele Mazzoni, Vladislav Horbatiuk, Kevin Hughes, Weijie Lin,
* Fernando Iglesias, Björn Esser, Evgeniy Andreev,
* Christopher Goldsworthy
*/

Expand Down Expand Up @@ -61,6 +61,28 @@ template<class ST> CDenseFeatures<ST>::CDenseFeatures(CFile* loader) :
load(loader);
}

template<class ST> CDenseFeatures<ST>::CDenseFeatures(CDotFeatures* features) :
CDotFeatures()
{
init();

auto num_feat = features->get_dim_feature_space();
auto num_vec = features->get_num_vectors();

ASSERT(num_feat>0 && num_vec>0)
feature_matrix = SGMatrix<ST>(num_feat, num_vec);
for (auto i = 0; i < num_vec; i++)
{
SGVector<float64_t> v = features->get_computed_dot_feature_vector(i);
ASSERT(num_feat==v.vlen)

for (auto j = 0; j < num_feat; j++)
feature_matrix.matrix[i * int64_t(num_feat) + j] = (ST) v.vector[j];
}
num_features = num_feat;
num_vectors = num_vec;
}

template<class ST> CFeatures* CDenseFeatures<ST>::duplicate() const
{
return new CDenseFeatures<ST>(*this);
Expand Down Expand Up @@ -366,30 +388,6 @@ template<class ST> ST* CDenseFeatures<ST>::get_transposed(int32_t &num_feat, int
return fm;
}

template<class ST> void CDenseFeatures<ST>::obtain_from_dot(CDotFeatures* df)
{
m_subset_stack->remove_all_subsets();

int32_t num_feat = df->get_dim_feature_space();
int32_t num_vec = df->get_num_vectors();

ASSERT(num_feat>0 && num_vec>0)

free_feature_matrix();
feature_matrix = SGMatrix<ST>(num_feat, num_vec);

for (int32_t i = 0; i < num_vec; i++)
{
SGVector<float64_t> v = df->get_computed_dot_feature_vector(i);
ASSERT(num_feat==v.vlen)

for (int32_t j = 0; j < num_feat; j++)
feature_matrix.matrix[i * int64_t(num_feat) + j] = (ST) v.vector[j];
}
num_features = num_feat;
num_vectors = num_vec;
}

template<class ST> bool CDenseFeatures<ST>::apply_preprocessor(bool force_preprocessing)
{
if (m_subset_stack->has_subsets())
Expand Down Expand Up @@ -468,8 +466,6 @@ template<class ST> void CDenseFeatures<ST>::initialize_cache()
}
}

template<class ST> EFeatureClass CDenseFeatures<ST>::get_feature_class() const { return C_DENSE; }

template<class ST> bool CDenseFeatures<ST>::reshape(int32_t p_num_features, int32_t p_num_vectors)
{
if (m_subset_stack->has_subsets())
Expand Down
28 changes: 17 additions & 11 deletions src/shogun/features/DenseFeatures.h
@@ -1,8 +1,8 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soumyajit De, Heiko Strathmann, Saurabh Mahindre, Soeren Sonnenburg,
* Vladislav Horbatiuk, Yuyu Zhang, Kevin Hughes, Evgeniy Andreev,
* Authors: Soumyajit De, Heiko Strathmann, Saurabh Mahindre, Soeren Sonnenburg,
* Vladislav Horbatiuk, Yuyu Zhang, Kevin Hughes, Evgeniy Andreev,
* Thoralf Klein, Fernando Iglesias, Björn Esser, Sergey Lisitsyn
*/

Expand Down Expand Up @@ -87,6 +87,12 @@ template<class ST> class CDenseFeatures: public CDotFeatures
*/
CDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec);

/** constructor from DotFeatures
*
* @param features DotFeatures object
*/
CDenseFeatures(CDotFeatures* features);

/** constructor loading features from file
*
* @param loader File object via which to load data
Expand Down Expand Up @@ -255,14 +261,6 @@ template<class ST> class CDenseFeatures: public CDotFeatures
*/
ST* get_transposed(int32_t &num_feat, int32_t &num_vec);

/** obtain dense features from other dotfeatures
*
* removes any subset before
*
* @param df dotfeatures to obtain features from
*/
void obtain_from_dot(CDotFeatures* df);

/** apply preprocessor
*
* applies preprocessors to ALL features (subset removed before and
Expand Down Expand Up @@ -311,7 +309,15 @@ template<class ST> class CDenseFeatures: public CDotFeatures
*
* @return feature class DENSE
*/
virtual EFeatureClass get_feature_class() const;
virtual EFeatureClass get_feature_class() const
{
return this->class_type();
}

static EFeatureClass class_type()
{
return C_DENSE;
}

/** get feature type
*
Expand Down
39 changes: 32 additions & 7 deletions src/shogun/features/Features.h
@@ -1,8 +1,8 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn,
* Saurabh Mahindre, Evgeniy Andreev, Wu Lin, Vladislav Horbatiuk,
* Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn,
* Saurabh Mahindre, Evgeniy Andreev, Wu Lin, Vladislav Horbatiuk,
* Yuyu Zhang, Björn Esser, Soumyajit De
*/

Expand Down Expand Up @@ -335,19 +335,44 @@ class CFeatures : public CSGObject
*/
virtual bool support_compatible_class() const {return false;}

/** Given a class in right hand side, does this class support compatible computation?
/** Given a class in right hand side, does this class support compatible computation?
*
* for example, is this->dot(rhs_prt) valid,
* where rhs_prt is the class in right hand side
* where rhs_prt is the class in right hand side
*
* @param rhs the class in right hand side
* @param rhs the class in right hand side
* @return whether this class supports compatible computation
*/
virtual bool get_feature_class_compatibility (EFeatureClass rhs) const;

#ifndef SWIG // SWIG should skip this part
virtual CFeatures* shallow_subset_copy()
{
/**
* Utility method to specialize the feature to the required type.
*
* @param f CFeatures base type
* @return The requested type if casting was successful.
*/
template<class T> static T* as(CFeatures* f)
{
return f->as<T>();
}

/**
* Utility method to specialize the feature to the required type.
*
* @param f CFeatures base type
* @return The requested type if casting was successful.
*/
template<class T> T* as()

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Feb 6, 2018

Author Member

@lisitsyn should do this or do a sort of std::decay and return whatever they want (pointer or given type etc)?

{
REQUIRE(T::class_type() == this->get_feature_class(),

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Feb 6, 2018

Author Member

this of course requires the stupid hacks of moving get_feature_class to be a static method, which most of the case would make sense.

or of course another hack is just to create the T on stack and simple call get_feature_class on it an then drop that object anyways.

This comment has been minimized.

Copy link
@karlnapf

karlnapf Feb 7, 2018

Member

I think I prefer static. Putting CSGobjects on stack always caused problems when I tried it

"Provided features (%s) is not of type %s!\n",

This comment has been minimized.

Copy link
@karlnapf

karlnapf Feb 6, 2018

Member

there is one %s missing, no?

this->get_feature_class());

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Feb 6, 2018

Author Member

i know one param is missing....

return (T*)this;
}

virtual CFeatures* shallow_subset_copy()
{
SG_SNOTIMPLEMENTED;
return NULL;
}
Expand Down
27 changes: 15 additions & 12 deletions src/shogun/features/SparseFeatures.cpp
Expand Up @@ -44,6 +44,21 @@ template<class ST> CSparseFeatures<ST>::CSparseFeatures(const CSparseFeatures &
m_subset_stack=orig.m_subset_stack;
SG_REF(m_subset_stack);
}

template<class ST> CSparseFeatures<ST>::CSparseFeatures(CDenseFeatures<ST>* dense)
{
init();

SGMatrix<ST> fm=dense->get_feature_matrix();
ASSERT(fm.matrix && fm.num_cols>0 && fm.num_rows>0)
set_full_feature_matrix(fm);
}

template<> CSparseFeatures<complex128_t>::CSparseFeatures(CDenseFeatures<complex128_t>* dense)
{
SG_NOTIMPLEMENTED;
}

template<class ST> CSparseFeatures<ST>::CSparseFeatures(CFile* loader)
: CDotFeatures(), feature_cache(NULL)
{
Expand Down Expand Up @@ -316,18 +331,6 @@ template<class ST> bool CSparseFeatures<ST>::apply_preprocessor(bool force_prepr
}
}

template<class ST> void CSparseFeatures<ST>::obtain_from_simple(CDenseFeatures<ST>* sf)
{
SGMatrix<ST> fm=sf->get_feature_matrix();
ASSERT(fm.matrix && fm.num_cols>0 && fm.num_rows>0)
set_full_feature_matrix(fm);
}

template<> void CSparseFeatures<complex128_t>::obtain_from_simple(CDenseFeatures<complex128_t>* sf)
{
SG_NOTIMPLEMENTED;
}

template<class ST> int32_t CSparseFeatures<ST>::get_num_vectors() const
{
return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : sparse_feature_matrix.num_vectors;
Expand Down
15 changes: 5 additions & 10 deletions src/shogun/features/SparseFeatures.h
@@ -1,8 +1,8 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn,
* Evgeniy Andreev, Vladislav Horbatiuk, Yuyu Zhang, Viktor Gal,
* Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn,
* Evgeniy Andreev, Vladislav Horbatiuk, Yuyu Zhang, Viktor Gal,
* Thoralf Klein, Björn Esser, Soumyajit De
*/

Expand Down Expand Up @@ -72,6 +72,9 @@ template <class ST> class CSparseFeatures : public CDotFeatures
/** copy constructor */
CSparseFeatures(const CSparseFeatures & orig);

/** copy constructor from DenseFeatures */
CSparseFeatures(CDenseFeatures<ST>* dense);

/** constructor loading features from file
*
* @param loader File object to load data from
Expand Down Expand Up @@ -249,14 +252,6 @@ template <class ST> class CSparseFeatures : public CDotFeatures
*/
virtual bool apply_preprocessor(bool force_preprocessing=false);

/** obtain sparse features from simple features
*
* subset on input is ignored, subset of this instance is removed
*
* @param sf simple features
*/
void obtain_from_simple(CDenseFeatures<ST>* sf);

/** get number of feature vectors, possibly of subset
*
* @return number of feature vectors
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/DimensionReductionPreprocessor.cpp
Expand Up @@ -51,7 +51,7 @@ SGMatrix<float64_t> CDimensionReductionPreprocessor::apply_to_feature_matrix(CFe
else
{
SG_WARNING("Converter to process was not set.\n")
return ((CDenseFeatures<float64_t>*)features)->get_feature_matrix();
return features.as<CDenseFeatures<float64_t>>()->get_feature_matrix();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/shogun/preprocessor/PCA.cpp
@@ -1,7 +1,7 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann, Viktor Gal,
* Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann, Viktor Gal,
* Evan Shelhamer, Evgeniy Andreev, Marc Zimmermann, Björn Esser
*/
#include <shogun/lib/config.h>
Expand Down Expand Up @@ -287,7 +287,7 @@ SGMatrix<float64_t> CPCA::apply_to_feature_matrix(CFeatures* features)
{
ASSERT(m_initialized)
ASSERT(features != NULL)
SGMatrix<float64_t> m = ((CDenseFeatures<float64_t>*) features)->get_feature_matrix();
SGMatrix<float64_t> m = features->as<CDenseFeatures<float64_t>>()->get_feature_matrix();

This comment has been minimized.

Copy link
@karlnapf

karlnapf Feb 6, 2018

Member

+1

int32_t num_vectors = m.num_cols;
int32_t num_features = m.num_rows;

Expand Down

0 comments on commit e7fa3c2

Please sign in to comment.