Commit ca7e6892 authored by Thomas Purcell's avatar Thomas Purcell
Browse files

Merge branch 'Masking' into 'Joe-Temp'

Masking Edits

See merge request !79
parents c8d39701 caf06c46
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -693,6 +693,7 @@ public:
    inline void set_loss_type(const std::string& loss_type_input){
        _loss_type = get_loss_type_from_string(loss_type_input);
    }

#ifdef PY_BINDINGS
    // DocString: model_prop_train
    /**
+108 −5
Original line number Diff line number Diff line
@@ -49,7 +49,29 @@ ModelClassifier::ModelClassifier(const std::string& prop_label,

{



    double score;
    switch(loss->type()) {
        case LOSS_TYPE::DAVIES_BOULDIN:
            score = (*loss)(feats);
            std::cout << "DBI: " << score << std::endl;
            break;
        case LOSS_TYPE::SILHOUETTE_SCORE:
            score = (*loss)(feats);
            std::cout << "Sil Score: " << score << std::endl;
            break;
        case LOSS_TYPE::CALINSKI:
            score = (*loss)(feats);
            std::cout << "CH Score " << score << std::endl;
            break;
        case LOSS_TYPE::CONVEX_HULL:
            set_n_misclassified();
            break;
        default:
            std::cerr << "Unknown loss type" << std::endl;
            break;
    }
}

ModelClassifier::ModelClassifier(std::string prop_label,
@@ -82,7 +104,7 @@ ModelClassifier::ModelClassifier(std::string prop_label,
      _train_n_convex_overlap(0),
      _test_n_convex_overlap(0)
{

    _loss_type = get_loss_type_from_string(loss_type_input);
    make_loss(prop_train, prop_test, task_sizes_train, task_sizes_test);
    _n_class = _loss->n_class();

@@ -95,8 +117,10 @@ ModelClassifier::ModelClassifier(std::string prop_label,
        _coefs.push_back(std::vector<double>(_loss->n_dim()));
        std::copy_n(&_loss->coefs()[cc * _loss->n_dim()], _loss->n_dim(), _coefs.back().data());
    }
    if (_loss->type() == LOSS_TYPE::CONVEX_HULL){
        set_n_misclassified();
    }
}

void ModelClassifier::set_n_misclassified()
{
@@ -169,8 +193,28 @@ void ModelClassifier::make_loss(std::vector<double>& prop_train,
                                std::vector<int>& task_sizes_train,
                                std::vector<int>& task_sizes_test)
{
    switch(_loss_type) {
        case LOSS_TYPE::DAVIES_BOULDIN:
            _loss = std::make_shared<LossFunctionDaviesBouldin>(
                prop_train, prop_test, task_sizes_train, task_sizes_test, _n_dim);
            break;
        case LOSS_TYPE::SILHOUETTE_SCORE:
            _loss = std::make_shared<LossFunctionSilhouetteScore>(
                prop_train, prop_test, task_sizes_train, task_sizes_test, _n_dim);
            break;
        case LOSS_TYPE::CALINSKI:
            _loss = std::make_shared<LossFunctionCalinskiHarabasz>(
                prop_train, prop_test, task_sizes_train, task_sizes_test, _n_dim);
            break;
        case LOSS_TYPE::CONVEX_HULL:
            _loss = std::make_shared<LossFunctionConvexHull>(
            prop_train, prop_test, task_sizes_train, task_sizes_test, _n_dim);
            break;
        default:
            std::cerr << "Unknown loss type" << std::endl;
            break;
    }

}

double ModelClassifier::eval_from_feature_vals(std::vector<double>& feature_vals,
@@ -208,6 +252,7 @@ std::vector<double> ModelClassifier::eval_from_feature_vals(

    return result;
}

void ModelClassifier::set_error_from_file(std::string error_line, bool train)
{
    std::vector<std::string> split_line = str_utils::split_string_trim(error_line);
@@ -285,6 +330,38 @@ std::ostream& operator<<(std::ostream& outStream, const ModelClassifier& model)
std::string ModelClassifier::error_summary_string(bool train) const
{
    std::stringstream error_stream;
    switch(_loss->type()) {
        case LOSS_TYPE::DAVIES_BOULDIN:
            if (train){
                error_stream << "#Davies Bouldin Loss : " << (*_loss)(_feats) << ";";
                error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified_test() << std::endl;
            }
            else{
                error_stream << "#Davies Bouldin Test Loss : " << (_loss->test_loss(_feats)) << ";";
                error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified_test() << std::endl;
            }
            break;
        case LOSS_TYPE::SILHOUETTE_SCORE:
            if (train){
                error_stream << "#Sil Score Train Loss : " << (_loss->test_loss(_feats)) << ";";
                error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified_test() << std::endl;
            }
            else{
                error_stream << "#Sil Score Test Loss : " << (_loss->test_loss(_feats)) << ";";
                error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified_test() << std::endl;
            }
            break;
        case LOSS_TYPE::CALINSKI:
            if (train){
                error_stream << "#Calinski Harabasz Loss : " << (*_loss)(_feats) << ";";
                error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified_test() << std::endl;
            }
            else{
                error_stream << "#Calinski Harabasz Test Loss : " << (_loss->test_loss(_feats)) << ";";
                error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified_test() << std::endl;
            }
            break;
        case LOSS_TYPE::CONVEX_HULL:
            if (train)
            {
                error_stream << "# # Samples in Convex Hull Overlap Region: " << _train_n_convex_overlap
@@ -297,7 +374,11 @@ std::string ModelClassifier::error_summary_string(bool train) const
                             << ";";
                error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified_test() << std::endl;
            }

            break;
        default:
            std::cerr << "Unknown loss type" << std::endl;
            break;
    }
    return error_stream.str();
}

@@ -305,14 +386,35 @@ std::string ModelClassifier::error_summary_string(std::vector<double> prop,
                                                 std::vector<double> prop_est) const
{
    std::stringstream error_stream;

    switch (_loss->type()) {
        case LOSS_TYPE::CONVEX_HULL: {
            error_stream << "# # Samples in Convex Hull Overlap Region: Unknown;";
            std::vector<int> misclassified(prop.size());
            std::transform(
        prop.begin(), prop.end(), prop_est.begin(), misclassified.begin(), [](double p, double pe) {
            return static_cast<int>(p != pe);
        });
                prop.begin(), prop.end(), prop_est.begin(), misclassified.begin(),
                [](double p, double pe) { return static_cast<int>(p != pe); });
            int n_svm_misclassified = std::accumulate(misclassified.begin(), misclassified.end(), 0);
            error_stream << "# Samples SVM Misclassified: " << n_svm_misclassified << std::endl;
            break;
        }
        case LOSS_TYPE::DAVIES_BOULDIN: {
            error_stream << "Davies Bouldin Loss : " << (*_loss)(_feats);
            break;
        }
        case LOSS_TYPE::SILHOUETTE_SCORE: {
            error_stream << "Sil Score Loss : " << (*_loss)(_feats);
            break;
        }
        case LOSS_TYPE::CALINSKI: {
            error_stream << "Calinski Harabasz Loss : " << (*_loss)(_feats);
            break;
        }
        default: {
            std::cerr << "Unknown loss type" << std::endl;
            break;
        }
    }

    return error_stream.str();
}
@@ -331,6 +433,7 @@ std::string ModelClassifier::write_coefs() const
                ->first.size()));
    coef_head_stream << std::setw(task_header_w + 2) << std::left << "# Task";


    for (size_t cc = 0; cc < _coefs[0].size() - 1; ++cc)
    {
        coef_head_stream << std::setw(24) << " w" + std::to_string(cc);
+0 −1
Original line number Diff line number Diff line
@@ -156,7 +156,6 @@ void SISSOClassifier::add_models(std::vector<std::vector<std::string>> train_fil
                                 std::vector<std::vector<std::string>> test_files,
                                 std::map<int, int> index_map)
{

    _models.clear();
    if(train_files.size() != test_files.size())
    {
+4 −0
Original line number Diff line number Diff line
@@ -322,6 +322,10 @@ public:
        return _loss_type;
    }

    inline LOSS_TYPE get_raw_loss_type()const{
        return get_loss_type_from_string(_loss_type);
    }

#ifdef PY_BINDINGS
    // DocString: sisso_di_prop_train_py
    /**
+0 −1
Original line number Diff line number Diff line
@@ -527,7 +527,6 @@ void InputParser::generate_phi_0(std::vector<std::string> headers,
        std::vector<std::string> split_line;
        boost::algorithm::split(split_line, line, [](char c) { return c == ','; });
        samples.push_back(split_line[0]);

        // Check that the rows are all have the same number of columns as the header line
        if (split_line.size() != headers.size() + 1)
        {
Loading