Skip to content

Commit

Permalink
fixing setK crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael ZBYSZYNSKI committed Nov 16, 2023
1 parent 78ccce9 commit 5d2c1e2
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 93 deletions.
189 changes: 97 additions & 92 deletions src/classification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,125 +14,130 @@
#endif

template<typename T>
classificationTemplate<T>::classificationTemplate()
classificationTemplate<T>::classificationTemplate()
{
modelSet<T>::numInputs = -1;
modelSet<T>::numOutputs = -1;
modelSet<T>::isTraining = false;
classificationType = knn; //this is the default algorithm
modelSet<T>::numInputs = -1;
modelSet<T>::numOutputs = -1;
modelSet<T>::isTraining = false;
classificationType = knn; //this is the default algorithm
};

template<typename T>
classificationTemplate<T>::classificationTemplate(classificationTypes classification_type)
classificationTemplate<T>::classificationTemplate(classificationTypes classification_type)
{
modelSet<T>::numInputs = -1;
modelSet<T>::numOutputs = -1;
modelSet<T>::isTraining = false;
classificationType = classification_type;
modelSet<T>::numInputs = -1;
modelSet<T>::numOutputs = -1;
modelSet<T>::isTraining = false;
classificationType = classification_type;
};

template<typename T>
classificationTemplate<T>::classificationTemplate(const int &num_inputs, const int &num_outputs) //TODO: this feature isn't really useful
{
modelSet<T>::numInputs = num_inputs;
modelSet<T>::numOutputs = num_outputs;
modelSet<T>::isTraining = false;
std::vector<size_t> whichInputs;

for (size_t i = 0; i < modelSet<T>::numInputs; ++i)
{
whichInputs.push_back(i);
}
std::vector<trainingExampleTemplate<T> > trainingSet;

for (size_t i = 0; i < modelSet<T>::numOutputs; ++i)
{
modelSet<T>::myModelSet.push_back(new knnClassification<T>(modelSet<T>::numInputs, whichInputs, trainingSet, 1));
}
{
modelSet<T>::numInputs = num_inputs;
modelSet<T>::numOutputs = num_outputs;
modelSet<T>::isTraining = false;
std::vector<size_t> whichInputs;
for (size_t i = 0; i < modelSet<T>::numInputs; ++i)
{
whichInputs.push_back(i);
}
std::vector<trainingExampleTemplate<T> > trainingSet;
for (size_t i = 0; i < modelSet<T>::numOutputs; ++i)
{
modelSet<T>::myModelSet.push_back(new knnClassification<T>(modelSet<T>::numInputs, whichInputs, trainingSet, 1));
}
};

template<typename T>
classificationTemplate<T>::classificationTemplate(const std::vector<trainingExampleTemplate<T> > &trainingSet)
classificationTemplate<T>::classificationTemplate(const std::vector<trainingExampleTemplate<T> > &trainingSet)
{
modelSet<T>::numInputs = -1;
modelSet<T>::numOutputs = -1;
modelSet<T>::isTraining = false;
train(trainingSet);
modelSet<T>::numInputs = -1;
modelSet<T>::numOutputs = -1;
modelSet<T>::isTraining = false;
train(trainingSet);
};

template<typename T>
bool classificationTemplate<T>::train(const std::vector<trainingExampleTemplate<T> > &training_set)
bool classificationTemplate<T>::train(const std::vector<trainingExampleTemplate<T> > &training_set)
{
//TODO: time this process?
modelSet<T>::reset();

if (training_set.size() > 0)
//TODO: time this process?
modelSet<T>::reset();

if (training_set.size() > 0)
{
//create model(s) here
modelSet<T>::numInputs = int(training_set[0].input.size());
modelSet<T>::numOutputs = int(training_set[0].output.size());

for (int i = 0; i < modelSet<T>::numInputs; ++i)
{
//create model(s) here
modelSet<T>::numInputs = int(training_set[0].input.size());
modelSet<T>::numOutputs = int(training_set[0].output.size());

for (int i = 0; i < modelSet<T>::numInputs; ++i)
{
modelSet<T>::inputNames.push_back("inputs-" + std::to_string(i + 1));
}
modelSet<T>::numOutputs = int(training_set[0].output.size());

for ( auto example : training_set)
{
if (example.input.size() != modelSet<T>::numInputs)
{
throw std::length_error("unequal feature vectors in input.");
return false;
}
if (example.output.size() != modelSet<T>::numOutputs)
{
throw std::length_error("unequal output vectors.");
return false;
}
}
std::vector<size_t> whichInputs;

for (int j = 0; j < modelSet<T>::numInputs; ++j)
{
whichInputs.push_back(j);
}

for (int i = 0; i < modelSet<T>::numOutputs; ++i)
{
if (classificationType == svm)
{
modelSet<T>::myModelSet.push_back(new svmClassification<T>(modelSet<T>::numInputs));
}
else
{
modelSet<T>::myModelSet.push_back(new knnClassification<T>(modelSet<T>::numInputs, whichInputs, training_set, 1));
}
}

return modelSet<T>::train(training_set);
modelSet<T>::inputNames.push_back("inputs-" + std::to_string(i + 1));
}
modelSet<T>::numOutputs = int(training_set[0].output.size());

for ( auto example : training_set)
{
if (example.input.size() != modelSet<T>::numInputs)
{
throw std::length_error("unequal feature vectors in input.");
return false;
}
if (example.output.size() != modelSet<T>::numOutputs)
{
throw std::length_error("unequal output vectors.");
return false;
}
}
std::vector<size_t> whichInputs;

for (int j = 0; j < modelSet<T>::numInputs; ++j)
{
whichInputs.push_back(j);
}
return false;

for (int i = 0; i < modelSet<T>::numOutputs; ++i)
{
if (classificationType == svm)
{
modelSet<T>::myModelSet.push_back(new svmClassification<T>(modelSet<T>::numInputs));
}
else
{
modelSet<T>::myModelSet.push_back(new knnClassification<T>(modelSet<T>::numInputs, whichInputs, training_set, 1));
}
}

return modelSet<T>::train(training_set);
}
return false;
}

template<typename T>
std::vector<int> classificationTemplate<T>::getK()
std::vector<int> classificationTemplate<T>::getK()
{
std::vector<int> kVector;

for (baseModel<T>* model : modelSet<T>::myModelSet)
{
knnClassification<T>* kNNModel = dynamic_cast<knnClassification<T>*>(model); //FIXME: I really dislike this design
kVector.push_back(kNNModel->getK());
}
return kVector;
std::vector<int> kVector;

for (const baseModel<T>* model : modelSet<T>::myModelSet)
{
kVector.push_back(dynamic_cast<const knnClassification<T>*>(model)->getK()); //FIXME: I really dislike this design
}
return kVector;
}

template<typename T>
void classificationTemplate<T>::setK(const int whichModel, const int newK)
void classificationTemplate<T>::setK(const int whichModel, const int newK)
{
knnClassification<T>* kNNModel = dynamic_cast<knnClassification<T>*>(modelSet<T>::myModelSet[whichModel]); //FIXME: I really dislike this design
kNNModel->setK(newK);
if (modelSet<T>::myModelSet.size() > whichModel)
{
dynamic_cast<knnClassification<T>*>(modelSet<T>::myModelSet[whichModel])->setK(newK); //FIXME: I really dislike this design
}
else
{
throw std::length_error("model not in model set.");
}
}

//explicit instantiation
Expand Down
13 changes: 12 additions & 1 deletion test/rapidLibTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,18 @@ int main(int argc, const char * argv[])
//std::cout << myDTW.getCosts()[0] << std::endl;
//std::cout << myDTW.getCosts()[1] << std::endl;


////////////////////////////////////////////////////////////////////////

rapidLib::classification setTest;
try
{
setTest.setK(0, 2);
}
catch (...)
{
std::cout << "setting k on empty set doesn't crash" <<std::endl;
}

////////////////////////////////////////////////////////////////////////
//#define layerTest 1
#ifdef layerTest
Expand Down

0 comments on commit 5d2c1e2

Please sign in to comment.