Skip to content

Commit

Permalink
getFeature to use templates to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
anilbey committed Oct 17, 2023
1 parent e911eb8 commit 837e852
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 59 deletions.
82 changes: 29 additions & 53 deletions efel/cppcore/cfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,60 +420,36 @@ int cFeature::calc_features(const string& name) {
}
}

int cFeature::getFeatureInt(string strName, vector<int>& vec) {
const auto& dataMap = mapIntData;
// 1) Check if the feature is in the map.
vec = getMapData<int>(strName, dataMap);
if (!vec.empty())
{
logger << "Reusing computed value of " << strName << "." << endl;
return vec.size();
}
else
{
// 2) If it's not in the map, compute.
logger << "Going to calculate feature " << strName << " ..." << endl;
if (calc_features(strName) < 0) {
logger << "Failed to calculate feature " << strName << ": " << GErrorStr << endl;
return -1;
}
vec = getMapData<int>(strName, dataMap);
if (vec.empty())
GErrorStr += "Feature [" + strName + "] data is missing\n";

logger << "Calculated feature " << strName << ":" << vec << endl;

return vec.size();
}
}
template <typename T>
int cFeature::getFeature(string strName, vector<T>& vec) {
const map<string, vector<T>>* dataMap;
if constexpr (std::is_same_v<T, int>)
dataMap = &mapIntData;
else // cppcore sends only int or double, no other types
dataMap = &mapDoubleData;

// 1) Check if the feature is in the map.
vec = getMapData<T>(strName, *dataMap);
if (!vec.empty()) {
logger << "Reusing computed value of " << strName << "." << endl;
return vec.size();
} else {
// 2) If it's not in the map, compute.
logger << "Going to calculate feature " << strName << " ..." << endl;
if (calc_features(strName) < 0) {
logger << "Failed to calculate feature " << strName << ": " << GErrorStr << endl;
return -1;
}
vec = getMapData<T>(strName, *dataMap);
if (vec.empty())
GErrorStr += "Feature [" + strName + "] data is missing\n";

int cFeature::getFeatureDouble(string strName, vector<double>& vec) {
const auto& dataMap = mapDoubleData;
// 1) Check if the feature is in the map.
vec = getMapData<double>(strName, dataMap);
if (!vec.empty())
{
logger << "Reusing computed value of " << strName << "." << endl;
return vec.size();
}
else
{
// 2) If it's not in the map, compute.
logger << "Going to calculate feature " << strName << " ..." << endl;
if (calc_features(strName) < 0) {
logger << "Failed to calculate feature " << strName << ": " << GErrorStr << endl;
return -1;
logger << "Calculated feature " << strName << ":" << vec << endl;
return vec.size();
}
vec = getMapData<double>(strName, dataMap);
if (vec.empty())
GErrorStr += "Feature [" + strName + "] data is missing\n";

logger << "Calculated feature " << strName << ":" << vec << endl;

return vec.size();
}
}


int cFeature::setFeatureString(const string& key, const string& value) {
logger << "Set " << key << ": " << value << endl;
mapStrData[key] = value;
Expand Down Expand Up @@ -582,7 +558,7 @@ double cFeature::getDistance(string strName, double mean, double std,
// Check if a the trace doesn't contain any spikes outside of the stimulus
// interval
if (trace_check) {
retVal = getFeatureInt("trace_check", feature_veci);
retVal = getFeature<int>("trace_check", feature_veci);
if (retVal < 0) {
return error_dist;
}
Expand All @@ -596,10 +572,10 @@ double cFeature::getDistance(string strName, double mean, double std,
}

if (featureType == "int") {
retVal = getFeatureInt(strName, feature_veci);
retVal = getFeature<int>(strName, feature_veci);
intFlag = 1;
} else {
retVal = getFeatureDouble(strName, feature_vec);
retVal = getFeature<double>(strName, feature_vec);
intFlag = 0;
}
// printf("\n Calculating distance for [%s] values [", strName.c_str());
Expand Down
4 changes: 2 additions & 2 deletions efel/cppcore/cfeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class cFeature {
cFeature(const string& depFile, const string& outdir);
int getmapfptrVec(string strName, vector<feature_function>& vFptr);
int calc_features(const string& name);
template <typename T>
int getFeature(string strName, vector<T>& vec);
int setFeatureInt(string strName, vector<int>& intVec);
int getFeatureInt(string strName, vector<int>& vec);
int setFeatureDouble(string strName, vector<double>& DoubleVec);
int getFeatureDouble(string strName, vector<double>& vec);
int setFeatureString(const string& key, const string& value);
int getFeatureString(const string& key, string& value);
void getTraces(const string& wildcard, vector<string>& traces);
Expand Down
4 changes: 2 additions & 2 deletions efel/cppcore/cppcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ _getfeature(PyObject* self, PyObject* args, const string &type) {

if (feature_type == "int") {
vector<int> values;
return_value = pFeature->getFeatureInt(string(feature_name), values);
return_value = pFeature->getFeature<int>(string(feature_name), values);
PyList_from_vectorint(values, py_values);
} else if (feature_type == "double") {
vector<double> values;
return_value = pFeature->getFeatureDouble(string(feature_name), values);
return_value = pFeature->getFeature<double>(string(feature_name), values);
PyList_from_vectordouble(values, py_values);
} else {
PyErr_SetString(PyExc_TypeError, "Unknown feature name");
Expand Down
4 changes: 2 additions & 2 deletions efel/cppcore/efel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int setFeatureDouble(const char *strName, double *A, unsigned nValue) {

int getFeatureInt(const char *strName, int **A) {
vector<int> vec;
if (pFeature->getFeatureInt(string(strName), vec) < 0) {
if (pFeature->getFeature<int>(string(strName), vec) < 0) {
return -1;
}
*A = new int[vec.size()];
Expand All @@ -81,7 +81,7 @@ int getFeatureInt(const char *strName, int **A) {
int getFeatureDouble(const char *strName, double **A) {
vector<double> vec;
// printf("\nInside featureLibrary.. Before getdouble [%s ]\n", strName);
if (pFeature->getFeatureDouble(string(strName), vec) < 0) {
if (pFeature->getFeature<double>(string(strName), vec) < 0) {
return -1;
}
*A = new double[vec.size()];
Expand Down

0 comments on commit 837e852

Please sign in to comment.