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 7, 2018
1 parent 56ae5e1 commit 4553464
Show file tree
Hide file tree
Showing 26 changed files with 148 additions and 199 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>>();

This comment has been minimized.

Copy link
@karlnapf

karlnapf Feb 7, 2018

Member

yep! no obtain_from_generic in c++ anymore!


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, "Object is null!\n");

This comment has been minimized.

Copy link
@karlnapf

karlnapf Feb 7, 2018

Member

Maybe "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",

This comment has been minimized.

Copy link
@karlnapf

karlnapf Feb 7, 2018

Member

+1

typeid(*this).name(), typeid(T).name());
}
return (T*)this;
}

#ifndef SWIG
/**
* Get parameters observable
Expand Down
13 changes: 0 additions & 13 deletions src/shogun/classifier/mkl/MKLClassification.cpp
Expand Up @@ -38,16 +38,3 @@ void CMKLClassification::init_training()
REQUIRE(m_labels->get_num_labels(), "Number of labels is zero.\n");
REQUIRE(m_labels->get_label_type() == LT_BINARY, "Labels must be binary.\n");
}

CMKLClassification* CMKLClassification::obtain_from_generic(CMachine* machine)
{
if (machine == NULL)
return NULL;

if (machine->get_classifier_type() != CT_MKLCLASSIFICATION)
SG_SERROR("Provided machine is not of type CMKLClassification!")

CMKLClassification* casted = dynamic_cast<CMKLClassification*>(machine);
SG_REF(casted)
return casted;
}
9 changes: 1 addition & 8 deletions src/shogun/classifier/mkl/MKLClassification.h
@@ -1,7 +1,7 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Giovanni De Toni, Evan Shelhamer,
* Authors: Soeren Sonnenburg, Giovanni De Toni, Evan Shelhamer,
* Sergey Lisitsyn
*/
#ifndef __MKLCLASSIFICATION_H__
Expand Down Expand Up @@ -38,13 +38,6 @@ class CMKLClassification : public CMKL
*/
virtual float64_t compute_sum_alpha();

/**
* Helper method used to specialize a base class instance.
* @param machine the machine we want to cast
* @return a MKLClassification machine (already SGREF'ed)
*/
static CMKLClassification* obtain_from_generic(CMachine* machine);

/** @return object name */
virtual const char* get_name() const { return "MKLClassification"; }

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
12 changes: 0 additions & 12 deletions src/shogun/distributions/Distribution.cpp
Expand Up @@ -76,15 +76,3 @@ float64_t CDistribution::update_params_em(float64_t* alpha_k, int32_t len)
SG_NOTIMPLEMENTED
return -1;
}

CDistribution* CDistribution::obtain_from_generic(CSGObject* object)
{
if (!object)
return NULL;

CDistribution* casted=dynamic_cast<CDistribution*>(object);
if (!casted)
return NULL;

return casted;
}
9 changes: 1 addition & 8 deletions src/shogun/distributions/Distribution.h
@@ -1,7 +1,7 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Björn Esser, Thoralf Klein, Fernando Iglesias,
* Authors: Soeren Sonnenburg, Björn Esser, Thoralf Klein, Fernando Iglesias,
* Yuyu Zhang
*/

Expand Down Expand Up @@ -193,13 +193,6 @@ class CDistribution : public CSGObject
*/
virtual float64_t update_params_em(float64_t* alpha_k, int32_t len);

/** obtain from generic
*
* @param object generic object
* @return Distribution object
*/
static CDistribution* obtain_from_generic(CSGObject* object);

protected:
/** feature vectors */
CFeatures* features;
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
16 changes: 1 addition & 15 deletions src/shogun/distributions/Gaussian.cpp
@@ -1,7 +1,7 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Weijie Lin, Alesis Novik, Heiko Strathmann,
* Authors: Soeren Sonnenburg, Weijie Lin, Alesis Novik, Heiko Strathmann,
* Evgeniy Andreev, Viktor Gal, Evan Shelhamer, Björn Esser
*/
#include <shogun/lib/config.h>
Expand Down Expand Up @@ -441,17 +441,3 @@ SGVector<float64_t> CGaussian::sample()
SG_DEBUG("Leaving\n");
return samp;
}

CGaussian* CGaussian::obtain_from_generic(CDistribution* distribution)
{
if (!distribution)
return NULL;

CGaussian* casted=dynamic_cast<CGaussian*>(distribution);
if (!casted)
return NULL;

/* since an additional reference is returned */
SG_REF(casted);
return casted;
}
8 changes: 1 addition & 7 deletions src/shogun/distributions/Gaussian.h
@@ -1,7 +1,7 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Alesis Novik, Heiko Strathmann, Evgeniy Andreev,
* Authors: Soeren Sonnenburg, Alesis Novik, Heiko Strathmann, Evgeniy Andreev,
* Viktor Gal, Weijie Lin, Evan Shelhamer, Thoralf Klein
*/

Expand Down Expand Up @@ -207,12 +207,6 @@ class CGaussian : public CDistribution
*/
SGVector<float64_t> sample();

/** @param distribution is casted to CGaussian, NULL if not possible
* Note that the object is SG_REF'ed
* @return casted CGaussian object
*/
static CGaussian* obtain_from_generic(CDistribution* distribution);

/** @return object name */
virtual const char* get_name() const { return "Gaussian"; }

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
4 changes: 2 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

1 comment on commit 4553464

@karlnapf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good!

Please sign in to comment.