Skip to content

Commit

Permalink
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 7, 2018
1 parent e01566c commit 2fb90ab
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 122 deletions.
Expand Up @@ -33,7 +33,7 @@ int main(int argc, char** argv)
CFeatures* neg = gen_n->get_streamed_features(num_pos);
CFeatures* pos = gen_p->get_streamed_features(num_neg);
CDenseFeatures<float64_t>* train_feats =
CDenseFeatures<float64_t>::obtain_from_generic(neg->create_merged_copy(pos));
neg->create_merged_copy(pos)->as<CDenseFeatures<float64_t>>();

SGVector<float64_t> tl(num_neg+num_pos);
tl.set_const(1);
Expand Down
2 changes: 1 addition & 1 deletion examples/undocumented/libshogun/minibatchKMeans.cpp
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char **argv)
for (index_t i=0; i<result->get_num_labels(); ++i)
SG_SPRINT("cluster index of vector %i: %f\n", i, result->get_label(i));

CDenseFeatures<float64_t>* centers=CDenseFeatures<float64_t>::obtain_from_generic(distance->get_lhs());
CDenseFeatures<float64_t>* centers=distance->get_lhs()->as<CDenseFeatures<float64_t>>();
SGMatrix<float64_t> centers_matrix=centers->get_feature_matrix();
centers_matrix.display_matrix(centers_matrix.matrix,
centers_matrix.num_rows, centers_matrix.num_cols, "learnt centers using Lloyd's KMeans");
Expand Down
28 changes: 28 additions & 0 deletions src/shogun/base/SGObject.h
Expand Up @@ -424,6 +424,34 @@ class CSGObject
*/
std::vector<std::string> parameter_names() const;

/**
* Utility method to specialize the feature to the required type.
*
* @param sgo CSGObject base type
* @return The requested type if casting was successful.
*/
template<class T> static T* as(CSGObject* sgo)
{
REQUIRE(sgo, "No object provided!\n");
return sgo->as<T>();
}

/**
* Utility method to specialize the feature to the required type.
*
* @param sgo CSGObject base type
* @return The requested type if casting was successful.
*/
template<class T> T* as()
{
if (typeid(T) != typeid(*this))
{
SG_SERROR("The object (%s) is not of the requested type %s!\n",
typeid(*this).name(), typeid(T).name());
}
return (T*)this;
}

#ifndef SWIG
/**
* Get parameters observable
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/clustering/KMeans.cpp
@@ -1,8 +1,8 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Heiko Strathmann, Soeren Sonnenburg, Saurabh Mahindre,
* Sergey Lisitsyn, Evan Shelhamer, Soumyajit De, Fernando Iglesias,
* Authors: Heiko Strathmann, Soeren Sonnenburg, Saurabh Mahindre,
* Sergey Lisitsyn, Evan Shelhamer, Soumyajit De, Fernando Iglesias,
* Björn Esser, parijat
*/

Expand Down Expand Up @@ -39,8 +39,8 @@ CKMeans::~CKMeans()

void CKMeans::Lloyd_KMeans(SGMatrix<float64_t> centers, int32_t num_centers)
{
CDenseFeatures<float64_t>* lhs=
CDenseFeatures<float64_t>::obtain_from_generic(distance->get_lhs());
CDenseFeatures<float64_t>* lhs =
distance->get_lhs()->as<CDenseFeatures<float64_t>>();

int32_t lhs_size=lhs->get_num_vectors();
int32_t dim=lhs->get_num_features();
Expand Down
32 changes: 16 additions & 16 deletions src/shogun/clustering/KMeansBase.cpp
Expand Up @@ -46,7 +46,7 @@ CKMeansBase::~CKMeansBase()

void CKMeansBase::set_initial_centers(SGMatrix<float64_t> centers)
{
CDenseFeatures<float64_t>* lhs=((CDenseFeatures<float64_t>*) distance->get_lhs());
CDenseFeatures<float64_t>* lhs=distance->get_lhs()->as<CDenseFeatures<float64_t>>();
dimensions=lhs->get_num_features();
REQUIRE(centers.num_cols == k,
"Expected %d initial cluster centers, got %d", k, centers.num_cols);
Expand All @@ -60,9 +60,9 @@ void CKMeansBase::set_random_centers()
{
mus.zero();
CDenseFeatures<float64_t>* lhs=
CDenseFeatures<float64_t>::obtain_from_generic(distance->get_lhs());
distance->get_lhs()->as<CDenseFeatures<float64_t>>();
int32_t lhs_size=lhs->get_num_vectors();

SGVector<int32_t> temp=SGVector<int32_t>(lhs_size);
SGVector<int32_t>::range_fill_vector(temp, lhs_size, 0);
CMath::permute(temp);
Expand Down Expand Up @@ -134,12 +134,12 @@ void CKMeansBase::initialize_training(CFeatures* data)
{
REQUIRE(distance, "Distance is not provided")
REQUIRE(distance->get_feature_type()==F_DREAL, "Distance's features type (%d) should be of type REAL (%d)")

if (data)
distance->init(data, data);

CDenseFeatures<float64_t>* lhs=
CDenseFeatures<float64_t>::obtain_from_generic(distance->get_lhs());
distance->get_lhs()->as<CDenseFeatures<float64_t>>();

REQUIRE(lhs, "Lhs features of distance not provided");
int32_t lhs_size=lhs->get_num_vectors();
Expand Down Expand Up @@ -225,7 +225,7 @@ SGMatrix<float64_t> CKMeansBase::get_cluster_centers()
return SGMatrix<float64_t>();

CDenseFeatures<float64_t>* lhs=
(CDenseFeatures<float64_t>*)distance->get_lhs();
distance->get_lhs()->as<CDenseFeatures<float64_t>>();
SGMatrix<float64_t> centers=lhs->get_feature_matrix();
SG_UNREF(lhs);
return centers;
Expand Down Expand Up @@ -261,17 +261,17 @@ void CKMeansBase::store_model_features()
SGMatrix<float64_t> CKMeansBase::kmeanspp()
{
int32_t lhs_size;
CDenseFeatures<float64_t>* lhs=(CDenseFeatures<float64_t>*)distance->get_lhs();
CDenseFeatures<float64_t>* lhs=distance->get_lhs()->as<CDenseFeatures<float64_t>>();
lhs_size=lhs->get_num_vectors();

SGMatrix<float64_t> centers=SGMatrix<float64_t>(dimensions, k);
centers.zero();
SGVector<float64_t> min_dist=SGVector<float64_t>(lhs_size);
min_dist.zero();

/* First center is chosen at random */
int32_t mu=CMath::random((int32_t) 0, lhs_size-1);
SGVector<float64_t> mu_first=lhs->get_feature_vector(mu);
SGVector<float64_t> mu_first=lhs->get_feature_vector(mu);
for(int32_t j=0; j<dimensions; j++)
centers(j, 0)=mu_first[j];

Expand All @@ -292,21 +292,21 @@ SGMatrix<float64_t> CKMeansBase::kmeanspp()

/* Choose centers with weighted probability */
for(int32_t i=1; i<k; i++)
{
int32_t best_center=0;
{
int32_t best_center=0;
float64_t best_sum=-1.0;
SGVector<float64_t> best_min_dist=SGVector<float64_t>(lhs_size);

/* local tries for best center */
for(int32_t trial=0; trial<n_rands; trial++)
{
float64_t temp_sum=0.0;
float64_t temp_sum=0.0;
float64_t temp_dist=0.0;
SGVector<float64_t> temp_min_dist=SGVector<float64_t>(lhs_size);
int32_t new_center=0;
SGVector<float64_t> temp_min_dist=SGVector<float64_t>(lhs_size);
int32_t new_center=0;
float64_t prob=CMath::random(0.0, 1.0);
prob=prob*sum;

for(int32_t j=0; j<lhs_size; j++)
{
temp_sum+=min_dist[j];
Expand Down Expand Up @@ -339,7 +339,7 @@ SGMatrix<float64_t> CKMeansBase::kmeanspp()
best_center=new_center;
}
}

SGVector<float64_t> vec=lhs->get_feature_vector(best_center);
for(int32_t j=0; j<dimensions; j++)
centers(j, i)=vec[j];
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/clustering/KMeansMiniBatch.cpp
Expand Up @@ -75,7 +75,7 @@ void CKMeansMiniBatch::minibatch_KMeans()
"number of iterations not set to positive value. Current iterations %d \n", minib_iter);

CDenseFeatures<float64_t>* lhs=
CDenseFeatures<float64_t>::obtain_from_generic(distance->get_lhs());
distance->get_lhs()->as<CDenseFeatures<float64_t>>();
CDenseFeatures<float64_t>* rhs_mus=new CDenseFeatures<float64_t>(mus);
CFeatures* rhs_cache=distance->replace_rhs(rhs_mus);
int32_t XSize=lhs->get_num_vectors();
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/distributions/EMMixtureModel.cpp
Expand Up @@ -51,7 +51,7 @@ float64_t CEMMixtureModel::expectation_step()
// for each component
for (int32_t j=0;j<data.alpha.num_cols;j++)
{
CDistribution* jth_component=CDistribution::obtain_from_generic(data.components->get_element(j));
CDistribution* jth_component=data.components->get_element(j)->as<CDistribution>();
alpha_ij[j]=CMath::log(data.weights[j])+jth_component->get_log_likelihood_example(i);
SG_UNREF(jth_component);
};
Expand All @@ -74,7 +74,7 @@ void CEMMixtureModel::maximization_step()
float64_t sum_weights=0;
for (int32_t j=0;j<data.alpha.num_cols;j++)
{
CDistribution* jth_component=CDistribution::obtain_from_generic(data.components->get_element(j));
CDistribution* jth_component=data.components->get_element(j)->as<CDistribution>();

// update mean covariance of components
alpha_j=data.alpha.matrix+j*data.alpha.num_rows;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/distributions/KernelDensity.cpp
Expand Up @@ -57,7 +57,7 @@ CKernelDensity::~CKernelDensity()
bool CKernelDensity::train(CFeatures* data)
{
REQUIRE(data,"Data not supplied\n")
CDenseFeatures<float64_t>* dense_data=CDenseFeatures<float64_t>::obtain_from_generic(data);
CDenseFeatures<float64_t>* dense_data=data->as<CDenseFeatures<float64_t>>();

SG_UNREF(tree);
switch (m_eval)
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/distributions/MixtureModel.cpp
Expand Up @@ -75,7 +75,7 @@ bool CMixtureModel::train(CFeatures* data)
// set training points in all components of the mixture
for (int32_t i=0;i<m_components->get_num_elements();i++)
{
CDistribution* comp=CDistribution::obtain_from_generic(m_components->get_element(i));
CDistribution* comp=m_components->get_element(i)->as<CDistribution>();
comp->set_features(features);

SG_UNREF(comp)
Expand Down Expand Up @@ -123,7 +123,7 @@ float64_t CMixtureModel::get_log_likelihood_example(int32_t num_example)
SGVector<float64_t> log_likelihood_component(m_components->get_num_elements());
for (int32_t i=0;i<m_components->get_num_elements();i++)
{
CDistribution* ith_comp=CDistribution::obtain_from_generic(m_components->get_element(i));
CDistribution* ith_comp=m_components->get_element(i)->as<CDistribution>();
log_likelihood_component[i]=ith_comp->get_log_likelihood_example(num_example)+CMath::log(m_weights[i]);

SG_UNREF(ith_comp);
Expand Down Expand Up @@ -166,7 +166,7 @@ CDistribution* CMixtureModel::get_component(index_t index) const
{
REQUIRE(index<get_num_components(),"index supplied (%d) is greater than total mixture components (%d)\n"
,index,get_num_components())
return CDistribution::obtain_from_generic(m_components->get_element(index));
return m_components->get_element(index)->as<CDistribution>();
}

void CMixtureModel::set_max_iters(int32_t max_iters)
Expand Down
54 changes: 26 additions & 28 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
21 changes: 10 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 @@ -498,7 +496,8 @@ template<class ST> class CDenseFeatures: public CDotFeatures
/** helper method used to specialize a base class instance
*
*/
static CDenseFeatures* obtain_from_generic(CFeatures* const base_features);
[[deprecated("use .as template")]]
static CDenseFeatures* obtain_from_generic(CFeatures* const base_features) ;

#ifndef SWIG // SWIG should skip this part
virtual CFeatures* shallow_subset_copy();
Expand Down

0 comments on commit 2fb90ab

Please sign in to comment.