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 8, 2018
1 parent abb0e4a commit d404710
Show file tree
Hide file tree
Showing 26 changed files with 190 additions and 138 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
29 changes: 29 additions & 0 deletions src/shogun/base/SGObject.h
Expand Up @@ -427,6 +427,35 @@ 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, or throws exception.
*/
template<class T> T* as()
{
T* c = dynamic_cast<T*>(this);
if (c)
return c;

SG_SERROR("The object (%s) cannot be casted to the requested type %s!\n",
demangled_type<std::remove_pointer_t<decltype(this)>>(), demangled_type<T>());
return nullptr;
}

#ifndef SWIG
/**
* Get parameters observable
Expand Down
33 changes: 33 additions & 0 deletions src/shogun/base/macros.h
@@ -0,0 +1,33 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Viktor Gal
*/

#ifndef __SG_MACROS_H__
#define __SG_MACROS_H__

#if defined(__GNUC__) || defined(__APPLE__)
#define SG_FORCED_INLINE inline __attribute__((always_inline))
#define SG_FORCED_PACKED __attribute__((__packed__))
#elif defined(_MSC_VER)
#define SG_FORCED_INLINE __forceinline
#define SG_FORCED_PACKED
#else
#define SG_FORCED_INLINE
#define SG_FORCED_PACKED
#endif

// a quick macro for making sure that an object
// does not have a copy-ctor and operator=
#define SG_DELETE_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&) = delete; \
void operator=(const TypeName&) = delete

#ifndef SWIG
#define SG_DEPRECATED(message) [[deprecated(message)]]
#else
#define SG_DEPRECATED(message)
#endif

#endif
3 changes: 2 additions & 1 deletion 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 @@ -43,6 +43,7 @@ class CMKLClassification : public CMKL
* @param machine the machine we want to cast
* @return a MKLClassification machine (already SGREF'ed)
*/
SG_DEPRECATED("use .as template function")
static CMKLClassification* obtain_from_generic(CMachine* machine);

/** @return object name */
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
3 changes: 2 additions & 1 deletion 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 @@ -198,6 +198,7 @@ class CDistribution : public CSGObject
* @param object generic object
* @return Distribution object
*/
SG_DEPRECATED("use .as template function")
static CDistribution* obtain_from_generic(CSGObject* object);

protected:
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
3 changes: 2 additions & 1 deletion 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 @@ -211,6 +211,7 @@ class CGaussian : public CDistribution
* Note that the object is SG_REF'ed
* @return casted CGaussian object
*/
SG_DEPRECATED("use .as template function")
static CGaussian* obtain_from_generic(CDistribution* distribution);

/** @return object name */
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

0 comments on commit d404710

Please sign in to comment.