Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/class_instantiation_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,13 @@
"folder": null,
"exceptions": null
},
{
"template": "class MedianDegree<$id_type, $nnz_type, $value_type, $float_type>",
"filename": "median_degree.inc",
"ifdef": null,
"folder": null,
"exceptions": null
},
{
"template": "class StandardDeviationDegreeColumn<$id_type, $nnz_type, $value_type, $float_type>",
"filename": "standard_deviation_degree_column.inc",
Expand All @@ -554,6 +561,26 @@
"exceptions": null
},
{
"template": "class StandardDeviationDegree<$id_type, $nnz_type, $value_type, $float_type>",
"filename": "standard_deviation_degree.inc",
"ifdef": null,
"folder": null,
"exceptions": null
},
{
"template": "class CoefficientOfVariationDegree<$id_type, $nnz_type, $value_type, $float_type>",
"filename": "coefficient_of_variation_degree.inc",
"ifdef": null,
"folder": null,
"exceptions": null
},
{
"template": "class GeometricAvgDegree<$id_type, $nnz_type, $value_type, $float_type>",
"filename": "geometric_avg_degree.inc",
"ifdef": null,
"folder": null,
"exceptions": null
},
"template": "class CoefficientOfVariationDegreeColumn<$id_type, $nnz_type, $value_type, $float_type>",
"filename": "coefficient_of_variation_degree_column.inc",
"ifdef": null,
Expand Down
147 changes: 147 additions & 0 deletions src/sparsebase/feature/coefficient_of_variation_degree.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include "sparsebase/feature/coefficient_of_variation_degree.h"

#include <memory>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include <cmath>

#include "sparsebase/utils/parameterizable.h"

namespace sparsebase::feature {

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
CoefficientOfVariationDegree<IDType, NNZType, ValueType,
FeatureType>::CoefficientOfVariationDegree() {
Register();
this->params_ =
std::shared_ptr<CoefficientOfVariationDegreeParams>(new CoefficientOfVariationDegreeParams());
this->pmap_.insert({get_id_static(), this->params_});
}
template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::CoefficientOfVariationDegree(
CoefficientOfVariationDegreeParams params) {
CoefficientOfVariationDegree();
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::CoefficientOfVariationDegree(
const CoefficientOfVariationDegree &d) {
Register();
this->params_ = d.params_;
this->pmap_ = d.pmap_;
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::CoefficientOfVariationDegree(
const std::shared_ptr<CoefficientOfVariationDegreeParams> p) {
Register();
this->params_ = p;
this->pmap_[get_id_static()] = p;
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
void CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::Register() {
this->RegisterFunction(
{format::CSR<IDType, NNZType, ValueType>::get_id_static()},
GetCoefficientOfVariationDegreeCSR);
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
std::unordered_map<std::type_index, std::any>
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::Extract(
format::Format *format, std::vector<context::Context *> c,
bool convert_input) {
return {{this->get_id(), std::forward<FeatureType *>(
GetCoefficientOfVariationDegree(format, c, convert_input))}};
};

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
std::vector<std::type_index>
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::get_sub_ids() {
return {typeid(CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>)};
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
std::vector<utils::Extractable *>
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::get_subs() {
return {
new CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>(*this)};
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
std::type_index
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::get_id_static() {
return typeid(CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>);
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
CoefficientOfVariationDegree<IDType, NNZType, ValueType,
FeatureType>::~CoefficientOfVariationDegree() = default;

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
std::tuple<std::vector<std::vector<format::Format *>>, FeatureType *>
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::
GetCoefficientOfVariationDegreeCached(format::Format *format,
std::vector<context::Context *> contexts,
bool convert_input) {
CoefficientOfVariationDegreeParams params;
return this->CachedExecute(&params, contexts, convert_input, false,
format); // func(sfs, this->params_.get());
}
template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
FeatureType *
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::GetCoefficientOfVariationDegree(
format::Format *format, std::vector<context::Context *> contexts,
bool convert_input) {
CoefficientOfVariationDegreeParams params;
return this->Execute(&params, contexts, convert_input,
format); // func(sfs, this->params_.get());
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
FeatureType *
CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::GetCoefficientOfVariationDegree(
object::Graph<IDType, NNZType, ValueType> *obj,
std::vector<context::Context *> contexts, bool convert_input) {
format::Format *format = obj->get_connectivity();
return this->Execute(this->params_.get(), contexts, convert_input,
format); // func(sfs, this->params_.get());
}

template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
FeatureType *CoefficientOfVariationDegree<IDType, NNZType, ValueType, FeatureType>::
GetCoefficientOfVariationDegreeCSR(std::vector<format::Format *> formats,
utils::Parameters *params) {
auto csr = formats[0]->AsAbsolute<format::CSR<IDType, NNZType, ValueType>>();
IDType num_vertices = csr->get_dimensions()[0];
FeatureType avg_degree;
auto *rows = csr->get_row_ptr();
NNZType degree_sum = rows[num_vertices] - rows[0];
avg_degree = degree_sum / (FeatureType)num_vertices;
FeatureType standard_deviation_degree = 0;
for (int i = 0; i < num_vertices; i++) {
standard_deviation_degree += (rows[i + 1] - rows[i] - avg_degree)*(rows[i + 1] - rows[i] - avg_degree);
}
return new FeatureType(sqrt(standard_deviation_degree)/avg_degree);
}

#if !defined(_HEADER_ONLY)
#include "init/coefficient_of_variation_degree.inc"
#endif
} // namespace sparsebase::feature
105 changes: 105 additions & 0 deletions src/sparsebase/feature/coefficient_of_variation_degree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <vector>

#include "sparsebase/config.h"
#include "sparsebase/feature/feature_preprocess_type.h"
#include "sparsebase/format/csr.h"
#include "sparsebase/object/object.h"
#include "sparsebase/utils/parameterizable.h"

#ifndef SPARSEBASE_PROJECT_COEFFICIENT_OF_VARIATION_DEGREE_DISTRIBUTION_H
#define SPARSEBASE_PROJECT_COEFFICIENT_OF_VARIATION_DEGREE_DISTRIBUTION_H
namespace sparsebase::feature {

//! An empty struct used for the parameters of CoefficientOfVariationDegree
struct CoefficientOfVariationDegreeParams : utils::Parameters {};
//! Find the coefficient of variation degree of the graph representation of a format object
/*!
*
* @tparam FeatureType the type in which the distribution value are returned --
* should be a floating type
*/
template <typename IDType, typename NNZType, typename ValueType,
typename FeatureType>
class CoefficientOfVariationDegree
: public feature::FeaturePreprocessType<FeatureType *> {
public:
//! An empty struct used for the parameters of CoefficientOfVariationDegree
typedef CoefficientOfVariationDegreeParams ParamsType;
CoefficientOfVariationDegree();
CoefficientOfVariationDegree(CoefficientOfVariationDegreeParams);
CoefficientOfVariationDegree(const CoefficientOfVariationDegree &);
CoefficientOfVariationDegree(std::shared_ptr<CoefficientOfVariationDegreeParams>);
virtual std::unordered_map<std::type_index, std::any> Extract(
format::Format *format, std::vector<context::Context *>,
bool convert_input);
virtual std::vector<std::type_index> get_sub_ids();
virtual std::vector<utils::Extractable *> get_subs();
static std::type_index get_id_static();

//! Coefficient of variation degree generation executor function that carries out function
//! matching
/*!
*
* @param format a single format pointer to any format
* @param contexts vector of contexts that can be used for extracting
* @param convert_input whether or not to convert the input format if that is
* needed.
* @return coefficient of variation degree in the graph representation of `format`
*/
FeatureType *GetCoefficientOfVariationDegree(format::Format *format,
std::vector<context::Context *> contexts,
bool convert_input);
//! Coefficient of variation degree generation executor function that carries out function
//! matching on a Graph
/*!
*
* @param object a single format pointer to any format
* @param contexts vector of contexts that can be used for extracting
* @param convert_input whether or not to convert the input format if that is
* needed.
* @return coefficient of variation degree in the graph
*/
FeatureType *GetCoefficientOfVariationDegree(
object::Graph<IDType, NNZType, ValueType> *object,
std::vector<context::Context *> contexts, bool convert_input);
//! Coefficient of variation degree generation executor function that carries out function
//! matching with cached outputs
/*!
* Generates the coefficient of variation degree of the passed format. If the input format
* was converted to other format types, the converting results are also
* returned with the output @param format a single format pointer to any
* format @param contexts vector of contexts that can be used for extracting
* features.
* @param convert_input whether or not to convert the input format if that is
* needed.
* @return A tuple with the first element being a vector of Format*
* where each pointer in the output points at the format that the corresponds
* Format object from the the input was converted to. If an input Format
* wasn't converted, the output pointer will point at nullptr. The second
* element is a pointer pointing to coefficient of variation degree
*/
std::tuple<std::vector<std::vector<format::Format *>>, FeatureType *>
GetCoefficientOfVariationDegreeCached(format::Format *format,
std::vector<context::Context *> contexts,
bool convert_input);

static FeatureType
*
//! Coefficient of variation degree generation implementation function for CSRs
/*!
*
* @param format a single format pointer to any format
* @return coefficient of variation degree in the graph representation of `formats[0]`
*/
GetCoefficientOfVariationDegreeCSR(std::vector<format::Format *> formats,
utils::Parameters *params);
~CoefficientOfVariationDegree();

protected:
void Register();
};
} // namespace sparsebase::feature
#ifdef _HEADER_ONLY
#include "sparsebase/feature/coefficient_of_variation_degree.cc"
#endif
#endif // SPARSEBASE_PROJECT_COEFFICIENT_OF_VARIATION_DEGREE_DISTRIBUTION_H
Loading