text:
#include <pcl/point_types.h>
#include <pcl/conversions.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/filter_indices.h>
#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/PointCloud2.h>

#include <ros/ros.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>

#include <sstream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>

#include <shape_reconstruction/SRUtils.h>

#include <opencv2/highgui/highgui.hpp>

#include <regex>

using namespace std;
using namespace boost::filesystem;

namespace omip {

void compareAlignedDensePointClouds(SRPointCloud::Ptr& gt_pc, SRPointCloud::Ptr& seg_pc, int& tp, int& tn, int& fp, int& fn, int& error) {
    tp=0, tn=0, fp=0, fn=0, error=0;

    // TODO compare to ground truth
    if (gt_pc->size() != seg_pc->size()) {
        ROS_ERROR(" Cannot compare; sizes differ!");
        return;
    } else {
        for (int i = 0; i < gt_pc->size(); i++) {
            SRPoint& p_gt =  gt_pc->points[i];
            SRPoint& p_seg =  seg_pc->points[i];

            bool gt_nan = !(!isnan(p_gt.x) && !isnan(p_gt.y) && !isnan(p_gt.z));
            bool seg_nan = !(!isnan(p_seg.x) && !isnan(p_seg.y) && !isnan(p_seg.z));

            if (gt_nan && seg_nan)
                tn++;
            else if (gt_nan && !seg_nan)
                fp++;
            else if (!gt_nan && seg_nan)
                fn++;
            else if ( p_gt.x == p_seg.x && p_gt.y == p_seg.y && p_gt.z == p_seg.z )
                tp++;
            else
                error++;
        }
    }

    if (error>0){
        ROS_ERROR_STREAM(endl << "          ERROR non matching points: " << error << " - are the point clouds really aligned?");
    }
}

std::string getTimeStampString(const ros::Time& t) {
    double sec = t.toSec();

    char c[255];
    sprintf(c, "%.3f\n", sec);

    return string(c);
}

void writeCSV( std::ofstream& out, std::vector<std::string>& labels, std::vector<std::vector<double> >& values ) {
    assert (out.is_open());

    out.precision(25);

    out << "#";
    for (vector<string >::iterator it = labels.begin(); it != labels.end(); it++) {
        out << *it << " ";
    }
    out << endl;

    for (vector<vector<double> >::iterator it = values.begin(); it != values.end(); it++) {
        for (vector<double>::iterator it2 = it->begin(); it2 != it->end(); it2++) {
            out << *it2 << " ";
        }
        out << endl;
    }
}

void get_ochs_result_file_list(const string& ochs_result_dir, vector<string>& dense_recons) {
    cout << "Listing Ochs results:  " << ochs_result_dir << endl;
    path ochs_dir(ochs_result_dir);
    directory_iterator end_iter;

    //vector<path> dense_recons;
    dense_recons.clear();

    if ( !exists(ochs_dir) || !is_directory(ochs_dir)) {
        cout << "ERROR: directory does not exist" << endl;
        return;
    }
    for( directory_iterator dir_iter(ochs_dir) ; dir_iter != end_iter ; ++dir_iter) {
        if (!is_regular_file(dir_iter->status()) ) {
            continue;
        }
        //const directory_entry& entry = *dir_iter;
        const path& p = dir_iter->path();
        if (p.leaf().string().find("dense") != string::npos) {
            dense_recons.push_back(p.string());
        }
    }

    std::sort(dense_recons.begin(), dense_recons.end());

    for (int i = 0; i < dense_recons.size(); i++) {
        cout << "   " << dense_recons[i] << endl;
    }

}

void getOchsSegments(const cv::Mat img, SRPointCloud::ConstPtr orig_pc,  vector<SRPointCloud::Ptr>& segments) {
    // collect colors
    segments.clear();

    map<string,SRPointCloud::Ptr> colors;
    int idx=0;
    for (int h=0; h<img.rows; h++) {
        for (int w=0; w<img.cols; w++) {
            cv::Vec3b c = img.at<cv::Vec3b>(h,w);
            ostringstream hash;
            hash << static_cast<int>(c[0]) << static_cast<int>(c[1]) << static_cast<int>(c[2]);
            map<string,SRPointCloud::Ptr>::iterator it = colors.find(hash.str());
            if (it == colors.end()) {
                cout << "create new point cloud #" << colors.size() << " for hash " << hash.str() << endl;
                SRPointCloud::Ptr pc(new SRPointCloud);
                pc->width = img.cols;
                pc->height = img.rows;
                for (int h=0; h<img.rows; h++) {
                    for (int w=0; w<img.cols; w++) {
                        SRPoint po; po.data[0] = po.data[1] = po.data[2] = std::numeric_limits<float>::quiet_NaN();
                        pc->points.push_back(po);
                    }
                }

//                cout << pcl::isFinite(pc->points[0]) << endl;
//                cout << "orig_pc: " << orig_pc->width << ", " << orig_pc->height << " (" << orig_pc->points.size() << ")" << endl;
//                cout << orig_pc->points.at(0) << endl;
//                cout << "pc: " << pc->width << ", " << pc->height << " (" << pc->points.size() << ")" << endl;

                colors[hash.str()] = pc;
            }
            SRPointCloud::Ptr pc = colors[hash.str()];
            SRPoint &p = pc->points.at(idx);
            p = orig_pc->points.at(idx);

            idx++;
        }
    }

    for (map<string,SRPointCloud::Ptr>::iterator it = colors.begin(); it != colors.end(); it++) {
        segments.push_back(it->second);
    }

}

void process(rosbag::Bag& results_bag, rosbag::Bag& gt_bag, const path& output_dir, const string& ochs_result_dir="") {
    static const int rb_max = 5;

    vector<string> labels;
    labels.push_back("time");
    labels.push_back("tp");
    labels.push_back("tn");
    labels.push_back("fp");
    labels.push_back("fn");

    map<string, vector<vector<double> > > results;

    // find topics
    std::vector<std::string> topics;

    topics.push_back("/camera/depth_registered/points");
    topics.push_back("/real_selected_points");

    std::vector<std::string> segment_topics;
    segment_topics.push_back("/shape_recons/segment_rb");
    segment_topics.push_back("/shape_recons/segment_depth_rb");
    segment_topics.push_back("/shape_recons/segment_color_rb");
    segment_topics.push_back("/shape_recons/segment_ext_d_and_c_rb");
    segment_topics.push_back("/shape_recons/segment_ext_d_rb");
    segment_topics.push_back("/shape_recons/segment_ext_c_rb");

    segment_topics.push_back("/shape_recons/segment_naive_rb");
    segment_topics.push_back("/shape_recons/segment_naive_depth_rb");
    segment_topics.push_back("/shape_recons/segment_naive_color_rb");

    for (vector<string>::iterator it = segment_topics.begin(); it != segment_topics.end(); it++) {
        for (int i = 2; i < rb_max; i++) {
            stringstream ss;
            ss << *it << i;
            topics.push_back(ss.str());

            cout << "       adding topic " << ss.str() << endl;
        }
    }
    cout << endl;

    rosbag::View view_results(results_bag, rosbag::TopicQuery(topics));
    rosbag::View view_gt(gt_bag, rosbag::TopicQuery(topics));

    vector<rosbag::View::const_iterator> gt_pc_list;
    vector<rosbag::View::const_iterator> res_full_pc_list;
    map< string, vector<rosbag::View::const_iterator> > res_seg_pc_map;

    // collect
    cout << "COLLECTING ground truth " << endl;
    rosbag::View::const_iterator git = view_gt.begin();
    for (; git != view_gt.end(); git++) {
        // get ground truth point cloud
        rosbag::MessageInstance const& m = *git;
        assert (m.getTopic() == "/real_selected_points");

        sensor_msgs::PointCloud2ConstPtr gt_msg = m.instantiate<sensor_msgs::PointCloud2>();
        cout << "    found gt at time: " << gt_msg->header.stamp << endl;
        gt_pc_list.push_back(git);
    }
    cout << endl;

    cout << "COLLECTING segmentation results" << endl;
    rosbag::View::const_iterator rit = view_results.begin();
    for (; rit != view_results.end(); rit++) {
        // get ground truth point cloud
        rosbag::MessageInstance const& m = *rit;
        if (m.getTopic() == "/camera/depth_registered/points") {
            sensor_msgs::PointCloud2ConstPtr pc_msg = m.instantiate<sensor_msgs::PointCloud2>();
            res_full_pc_list.push_back(rit);
            cout << " Found PC at time      " << pc_msg->header.stamp << endl;
        } else {
            sensor_msgs::PointCloud2ConstPtr pc_msg = m.instantiate<sensor_msgs::PointCloud2>();
            res_seg_pc_map[getTimeStampString(pc_msg->header.stamp)].push_back(rit);
            cout << " Found segment " << m.getTopic() << " at time " << pc_msg->header.stamp << endl;

            SRPointCloud::Ptr seg_pc(new SRPointCloud);
            rosbag::MessageInstance const& m = *rit;
            sensor_msgs::PointCloud2ConstPtr seg_msg = m.instantiate<sensor_msgs::PointCloud2>();
            pcl::fromROSMsg(*seg_msg, *seg_pc);

//            SRPointCloud::Ptr seg_pc_no_nan(new SRPointCloud);
//            std::vector<int> not_nan_indices;
//            pcl::removeNaNFromPointCloud<SRPoint>(*seg_pc,*seg_pc_no_nan, not_nan_indices);
//            cout << "   Segment contains " << seg_pc_no_nan->size() << " not-nan points" << endl;

        }
    }

    cout << "SUMMARY" << endl;
    cout << "  ground truth:   " << gt_pc_list.size() << " full point clouds " << endl;
    cout << "      segments:   " << res_full_pc_list.size() << " full point clouds " << endl;
    cout << "                  " << res_seg_pc_map.size() << " entries " << endl;

    cout << "---------------------------" << endl << endl;

    if (gt_pc_list.size() != res_full_pc_list.size()) {
        ROS_WARN_STREAM("Ground truth and result bag have different sizes!");
        if (gt_pc_list.size() > res_full_pc_list.size())
            ROS_WARN_STREAM("Assuming results contain a subset, but start at the same time step");
        else {
            ROS_ERROR_STREAM("Results bag has more than ground truth! aborting");
            return;
        }
    }

    // collect ochs results
    vector<string> ochs_results;
    if (ochs_result_dir != "") {
        get_ochs_result_file_list(ochs_result_dir, ochs_results);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // now iterate over the two
    vector<rosbag::View::const_iterator>::iterator gvit = gt_pc_list.begin();
    vector<rosbag::View::const_iterator>::iterator rvit = res_full_pc_list.begin();
    int idx=0;

    for (; gvit != gt_pc_list.end() && rvit != res_full_pc_list.end(); gvit++, rvit++, idx++) {
        // get ground truth PC
        rosbag::MessageInstance const& m_gt = **gvit;
        SRPointCloud::Ptr gt_pc_with_nan(new SRPointCloud);
        sensor_msgs::PointCloud2ConstPtr gt_msg = m_gt.instantiate<sensor_msgs::PointCloud2>();
        pcl::fromROSMsg(*gt_msg, *gt_pc_with_nan);

//        SRPointCloud::Ptr gt_pc;
//        gt_pc.reset(new SRPointCloud);
//        std::vector<int> not_nan_indices;
//        pcl::removeNaNFromPointCloud<SRPoint>(*gt_pc_with_nan,*gt_pc, not_nan_indices);
//        cout << "Ground truth contains " << gt_pc->size() << " not-nan points" << endl;

        // get full scene PC
        rosbag::MessageInstance const& m_full = **rvit;
        sensor_msgs::PointCloud2ConstPtr full_msg = m_full.instantiate<sensor_msgs::PointCloud2>();

        cout << "TIME: " << full_msg->header.stamp << endl;
        cout << "    time ground truth: " << gt_msg->header.stamp << endl;

        // get ochs segments and take best
        if (!ochs_results.empty()) {
            cout << "   " <<  "Looking at Ochs result: " << idx << endl;

            SRPointCloud::Ptr full_pc(new SRPointCloud);
            pcl::fromROSMsg(*full_msg, *full_pc);

            cv::Mat ochs_result_current = cv::imread(ochs_results[idx]);
            vector<SRPointCloud::Ptr> ochs_segments;
            getOchsSegments(ochs_result_current, full_pc,  ochs_segments);

            // select best segment
            int best_ochs_segment = -1;
            double best_f_score = 0.;
            vector<double> res;
            for (int i= 0; i < ochs_segments.size(); i++) {
                int tp, tn, fp, fn, error;
                compareAlignedDensePointClouds(gt_pc_with_nan, ochs_segments[i], tp, tn, fp, fn, error);
                double precision=0.0, recall=0.0, fscore=0.0;

                precision = ((double)tp) / (tp+fp);
                recall = ((double)tp) / (tp+fn);
                fscore = 2* ((double) precision*recall)/(precision+recall);

                cout << "   " << "  ochs segment " << i << " p=" << precision << ", r=" << recall << ", f=" << fscore << endl;
                if (fscore>best_f_score) {
                    best_ochs_segment = i;
                    best_f_score = fscore;
                    cout << "     " << i << " is best so far" << endl;
                    res.clear();
                    res.push_back(full_msg->header.stamp.toSec());
                    res.push_back(tp); res.push_back(tn); res.push_back(fp); res.push_back(fn);
                }
            }
            results[ "ochs" ].push_back(res);

            // <debug>
            cv::imshow("ochs segmentation", ochs_result_current);
            for (int i= 0; i < ochs_segments.size(); i++) {
                stringstream nm;
                nm << "segment " << i;
                SRPointCloud::Ptr sgm_pc = ochs_segments[i];
                cv::Mat sgm_dm(480,640,CV_32FC1);
                OrganizedPC2DepthMap(sgm_pc, sgm_dm);
                cv::imshow(nm.str(), sgm_dm);
            }
            //cv::waitKey(-1);
            //cv::waitKey(50);
            // </debug>
        }

        // compare also SR segments to ground truth
        vector<rosbag::View::const_iterator>& segment_list = res_seg_pc_map[getTimeStampString(full_msg->header.stamp)];
        cout << " Segment list contains " << segment_list.size() << " points" << endl;
        if (segment_list.empty()) {
            ROS_WARN_STREAM("No segments at time " << full_msg->header.stamp);
            continue;
        }

        vector<rosbag::View::const_iterator>::iterator svit = segment_list.begin();
        for (; svit != segment_list.end(); svit++) {

            SRPointCloud::Ptr seg_pc(new SRPointCloud);
            rosbag::MessageInstance const& m_res = **svit;
            sensor_msgs::PointCloud2ConstPtr seg_msg = m_res.instantiate<sensor_msgs::PointCloud2>();
            pcl::fromROSMsg(*seg_msg, *seg_pc);

//            SRPointCloud::Ptr seg_pc_no_nan(new SRPointCloud);
//            std::vector<int> not_nan_indices;
//            pcl::removeNaNFromPointCloud<SRPoint>(*seg_pc,*seg_pc_no_nan, not_nan_indices);
//            cout << "   Segment contains " << seg_pc_no_nan->size() << " not-nan points" << endl;

            int tp,fp,tn,fn,error;
            compareAlignedDensePointClouds(gt_pc_with_nan, seg_pc, tp, tn, fp, fn, error);

            /*
            cv::Mat gt_mat(480, 640, CV_8UC3), seg_mat(480, 640, CV_8UC3);
            omip::OrganizedPC2ColorMap(gt_pc_with_nan, gt_mat);
            omip::OrganizedPC2ColorMap(seg_pc, seg_mat);

            cv::Mat gt_depth_mat(480, 640, CV_32FC1), seg_depth_mat(480, 640, CV_32FC1), seg_depth_mat_rev(480, 640, CV_32FC1);
            omip::OrganizedPC2DepthMap(gt_pc_with_nan, gt_depth_mat);
            omip::OrganizedPC2DepthMap(seg_pc, seg_depth_mat);
            omip::OrganizedPC2DepthMap(seg_pc, seg_depth_mat_rev);

            cv::imshow("Ground truth", gt_mat);
            cv::imshow(m_res.getTopic(), seg_mat);

            cv::imshow("Ground truth depth", gt_depth_mat);
            cv::imshow(m_res.getTopic()  + " depth", seg_depth_mat);
            cv::imshow(m_res.getTopic()  + " depth rev", seg_depth_mat_rev);
            cv::waitKey(-1);
            */

            cout << m_res.getTopic() << endl;
            cout << "  tp= " << tp;
            cout << "  tn= " << tn;
            cout << "  fp= " << fp;
            cout << "  fn= " << fn << endl;

            vector<double> res;
            res.push_back(seg_msg->header.stamp.toSec());
            res.push_back(tp); res.push_back(tn); res.push_back(fp); res.push_back(fn);
            results[ m_res.getTopic() ].push_back(res);
        }
    }

    for (map<string, vector<vector<double> > >::iterator it = results.begin(); it != results.end(); it++) {
        cout << "Writing out results for " << it->first << endl;
        cout << "  has " << it->second.size() << " entries" << endl;
        // prepare output file
        vector<string> strs;
        boost::split(strs, it->first,boost::is_any_of("/"));

        path output_path = output_dir;;
        output_path /=  strs[strs.size()-1] + ".txt";

        cout <<"  Output path: " << output_path.string() << endl;

        ofstream output;
        output.open(output_path.string().c_str());
        writeCSV(output, labels, it->second);
        output.close();
    }
}

}

// Main program
int main(int argc, char** argv)
{
    if (argc < 3) {
        cout << "Usage: rosrun statistics <full_pc_and_segmentation> <ground_truth> [ochs_results_dir]" << endl;
        return 0;
    }

    path results_bag_path(argv[1]);
    if (!exists(results_bag_path.string())) {
        cerr << " results bag does not exist:  " << results_bag_path.string() << endl;
        return -1;
    }
    path gt_bag_path(argv[2]);
    if (!exists(gt_bag_path.string())) {
        cerr << " ground truth bag does not exist:  " << gt_bag_path.string() << endl;
        return -1;
    }

    string ochs_result_dir("");
    if (argc > 3) {
        ochs_result_dir = argv[3];
    }

    // prepare out path
    path output_dir = results_bag_path.parent_path();
    output_dir /= results_bag_path.stem().string();

    if (!boost::filesystem::exists(output_dir)) {
        if (!boost::filesystem::create_directories(output_dir)) {
            ROS_ERROR_NAMED("statistics", "Output directory for statistics does not exist and cannot be created!");
            return -1;
        }
    }

    // open rosbags
    rosbag::Bag results_bag;
    results_bag.open(results_bag_path.string(), rosbag::bagmode::Read);

    rosbag::Bag gt_bag;
    gt_bag.open(gt_bag_path.string(), rosbag::bagmode::Read);

    omip::process(results_bag, gt_bag, output_dir, ochs_result_dir);
    results_bag.close();
    gt_bag.close();

    return (0);
}

ext: cpp

max_stars_repo_name: tu-rbo/omip

max_issues_repo_name: tu-rbo/omip

max_forks_repo_name: tu-rbo/omip


text:
// Autogenerated from KST: please remove this line if doing any edits by hand!

#include <boost/test/unit_test.hpp>
#include "instance_io_user.h"
#include <iostream>
#include <fstream>
#include <vector>

BOOST_AUTO_TEST_CASE(test_instance_io_user) {
    std::ifstream ifs("src/instance_io.bin", std::ifstream::binary);
    kaitai::kstream ks(&ifs);
    instance_io_user_t* r = new instance_io_user_t(&ks);

    BOOST_CHECK_EQUAL(r->qty_entries(), 3);
    BOOST_CHECK_EQUAL(r->entries()->at(0)->name(), std::string("the"));
    BOOST_CHECK_EQUAL(r->entries()->at(1)->name(), std::string("rainy"));
    BOOST_CHECK_EQUAL(r->entries()->at(2)->name(), std::string("day it is"));

    delete r;
}


text:
#ifndef TIN_CONTROLLERS_TERMINAL_TERMINALSESSION_HPP
#define TIN_CONTROLLERS_TERMINAL_TERMINALSESSION_HPP

#include <boost/asio.hpp>
#include "../main/typedefs.hpp"
#include "../../../../terminal/src/terminal_message.hpp"

using boost::asio::ip::tcp;

namespace tin { namespace controllers { namespace terminal
{
    class TerminalSession :
    	public std::enable_shared_from_this <TerminalSession>
    {
    public:
        TerminalSession(
            tin::controllers::main::ControllerQueue &controllerQueue,
            tcp::socket socket);
        void start();
        void do_write(const char* message);
    private:
        tin::controllers::main::ControllerQueue& controllerQueue;
    	void do_read_header();
        void do_read_body();
    	tcp::socket socket_;
        terminal_message msg_;
    };
}}}

#endif	/* TIN_CONTROLLERS_TERMINAL_TERMINALSESSION_HPP */

ext: hpp

max_stars_repo_name: mdziekon/eiti-tin-ftp-stattr

max_issues_repo_name: mdziekon/eiti-tin-ftp-stattr

max_forks_repo_name: mdziekon/eiti-tin-ftp-stattr


text:
/*
    Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
    Use, modification and distribution are subject to the Boost Software License,
    Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt).
*/

/*************************************************************************************************/

#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_READ_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_READ_HPP

////////////////////////////////////////////////////////////////////////////////////////
/// \file
/// \brief
/// \author Christian Henning, Andreas Pokorny, Lubomir Bourdev \n
///
/// \date   2007-2012 \n
///
////////////////////////////////////////////////////////////////////////////////////////

#include <boost/gil/extension/io/png/tags.hpp>

#include <boost/gil.hpp>

#include <boost/gil/io/base.hpp>
#include <boost/gil/io/reader_base.hpp>
#include <boost/gil/io/conversion_policies.hpp>
#include <boost/gil/io/device.hpp>
#include <boost/gil/io/typedefs.hpp>
#include <boost/gil/io/row_buffer_helper.hpp>

#include <boost/gil/extension/io/png/detail/reader_backend.hpp>
#include <boost/gil/extension/io/png/detail/is_allowed.hpp>

namespace boost { namespace gil {

#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(push)
#pragma warning(disable:4512) //assignment operator could not be generated
#endif

///
/// PNG Reader
///
template< typename Device
        , typename ConversionPolicy
        >
class reader< Device
            , png_tag
            , ConversionPolicy
            >
    : public reader_base< png_tag
                        , ConversionPolicy >
    , public reader_backend< Device
                           , png_tag
                           >
{
private:

    typedef reader< Device
                  , png_tag
                  , ConversionPolicy
                  > this_t;

    typedef typename ConversionPolicy::color_converter_type cc_t;

public:

    typedef reader_backend< Device, png_tag > backend_t;

public:

    reader( const Device&                         io_dev
          , const image_read_settings< png_tag >& settings
          )
    : reader_base< png_tag
                 , ConversionPolicy
                 >()
    , backend_t( io_dev
               , settings
               )
    {}

    reader( const Device&                                          io_dev
          , const typename ConversionPolicy::color_converter_type& cc
          , const image_read_settings< png_tag >&                  settings
          )
    : reader_base< png_tag
                 , ConversionPolicy
                 >( cc )
    , backend_t( io_dev
               , settings
               )
    {}

    template< typename View >
    void apply( const View& view )
    {
        // The info structures are filled at this point.

        // Now it's time for some transformations.

        if( little_endian() )
        {
            if( this->_info._bit_depth == 16 )
            {
                // Swap bytes of 16 bit files to least significant byte first.
                png_set_swap( this->get_struct() );
            }

            if( this->_info._bit_depth < 8 )
            {
                // swap bits of 1, 2, 4 bit packed pixel formats
                png_set_packswap( this->get_struct() );
            }
        }

        if( this->_info._color_type == PNG_COLOR_TYPE_PALETTE )
        {
            png_set_palette_to_rgb( this->get_struct() );
        }

        if( png_get_valid( this->get_struct(), this->get_info(), PNG_INFO_tRNS ) )
        {
            png_set_tRNS_to_alpha( this->get_struct() );
        }

        // Tell libpng to handle the gamma conversion for you.  The final call
        // is a good guess for PC generated images, but it should be configurable
        // by the user at run time by the user.  It is strongly suggested that
        // your application support gamma correction.
        if( this->_settings._apply_screen_gamma )
        {
            // png_set_gamma will change the image data!

#ifdef BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
        png_set_gamma( this->get_struct()
                     , this->_settings._screen_gamma
                     , this->_info._file_gamma
                     );
#else
        png_set_gamma( this->get_struct()
                     , this->_settings._screen_gamma
                     , this->_info._file_gamma
                     );
#endif // BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
        }

        // Turn on interlace handling.  REQUIRED if you are not using
        // png_read_image().  To see how to handle interlacing passes,
        // see the png_read_row() method below:
        this->_number_passes = png_set_interlace_handling( this->get_struct() );

        // The above transformation might have changed the bit_depth and color type.
        png_read_update_info( this->get_struct()
                            , this->get_info()
                            );

        this->_info._bit_depth = png_get_bit_depth( this->get_struct()
                                                  , this->get_info()
                                                  );

        this->_info._num_channels = png_get_channels( this->get_struct()
                                                    , this->get_info()
                                                    );

        this->_info._color_type = png_get_color_type( this->get_struct()
                                                    , this->get_info()
                                                    );

        this->_scanline_length = png_get_rowbytes( this->get_struct()
                                                 , this->get_info()
                                                 );

        switch( this->_info._color_type )
        {
            case PNG_COLOR_TYPE_GRAY:
            {
                switch( this->_info._bit_depth )
                {
                    case  1: read_rows< gray1_image_t::view_t::reference >( view ); break;
                    case  2: read_rows< gray2_image_t::view_t::reference >( view ); break;
                    case  4: read_rows< gray4_image_t::view_t::reference >( view ); break;
                    case  8: read_rows< gray8_pixel_t  >( view ); break;
                    case 16: read_rows< gray16_pixel_t >( view ); break;
                    default: io_error( "png_reader::read_data(): unknown combination of color type and bit depth" );
                }

                break;
            }
            case PNG_COLOR_TYPE_GA:
            {
                #ifdef BOOST_GIL_IO_ENABLE_GRAY_ALPHA
                switch( this->_info._bit_depth )
                {
                    case  8: read_rows< gray_alpha8_pixel_t > ( view ); break;
                    case 16: read_rows< gray_alpha16_pixel_t >( view ); break;
                    default: io_error( "png_reader::read_data(): unknown combination of color type and bit depth" );
                }
                #else
                    io_error( "gray_alpha isn't enabled. Use ENABLE_GRAY_ALPHA when building application." );
                #endif // BOOST_GIL_IO_ENABLE_GRAY_ALPHA

                break;
            }
            case PNG_COLOR_TYPE_RGB:
            {
                switch( this->_info._bit_depth )
                {
                    case 8:  read_rows< rgb8_pixel_t > ( view ); break;
                    case 16: read_rows< rgb16_pixel_t >( view ); break;
                    default: io_error( "png_reader::read_data(): unknown combination of color type and bit depth" );
                }

                break;
            }
            case PNG_COLOR_TYPE_RGBA:
            {
                switch( this->_info._bit_depth )
                {
                    case  8: read_rows< rgba8_pixel_t > ( view ); break;
                    case 16: read_rows< rgba16_pixel_t >( view ); break;
                    default: io_error( "png_reader_color_convert::read_data(): unknown combination of color type and bit depth" );
                }

                break;
            }
            default: io_error( "png_reader_color_convert::read_data(): unknown color type" );
        }

        // read rest of file, and get additional chunks in info_ptr
        png_read_end( this->get_struct()
                    , NULL
                    );
    }

private:

    template< typename ImagePixel
            , typename View
            >
    void read_rows( const View& view )
    {
        typedef detail::row_buffer_helper_view< ImagePixel > row_buffer_helper_t;

        typedef typename row_buffer_helper_t::iterator_t it_t;

        typedef typename is_same< ConversionPolicy
                                , detail::read_and_no_convert
                                >::type is_read_and_convert_t;

        io_error_if( !detail::is_allowed< View >( this->_info
                                                , is_read_and_convert_t()
                                                )
                   , "Image types aren't compatible."
                   );

        std::size_t rowbytes = png_get_rowbytes( this->get_struct()
                                               , this->get_info()
                                               );

        row_buffer_helper_t buffer( rowbytes
                                  , true
                                  );

        png_bytep row_ptr = (png_bytep)( &( buffer.data()[0]));

        for( std::size_t pass = 0; pass < this->_number_passes; pass++ )
        {
            if( pass == this->_number_passes - 1 )
            {
                // skip lines if necessary
                for( std::ptrdiff_t y = 0; y < this->_settings._top_left.y; ++y )
                {
                    // Read the image using the "sparkle" effect.
                    png_read_rows( this->get_struct()
                                 , &row_ptr
                                 , NULL
                                 , 1
                                 );
                }

                for( std::ptrdiff_t y = 0
                   ; y < this->_settings._dim.y
                   ; ++y
                   )
                {
                    // Read the image using the "sparkle" effect.
                    png_read_rows( this->get_struct()
                                 , &row_ptr
                                 , NULL
                                 , 1
                                 );

                    it_t first = buffer.begin() + this->_settings._top_left.x;
                    it_t last  = first + this->_settings._dim.x; // one after last element

                    this->_cc_policy.read( first
                                         , last
                                         , view.row_begin( y ));
                }

                // Read the rest of the image. libpng needs that.
                std::ptrdiff_t remaining_rows = static_cast< std::ptrdiff_t >( this->_info._height )
                                              - this->_settings._top_left.y
                                              - this->_settings._dim.y;
                for( std::ptrdiff_t y = 0
                   ; y < remaining_rows
                   ; ++y
                   )
                {
                    // Read the image using the "sparkle" effect.
                    png_read_rows( this->get_struct()
                                 , &row_ptr
                                 , NULL
                                 , 1
                                 );
                }
            }
            else
            {
                for( int y = 0; y < view.height(); ++y )
                {
                    // Read the image using the "sparkle" effect.
                    png_read_rows( this->get_struct()
                                 , &row_ptr
                                 , NULL
                                 , 1
                                 );
                }
            }
        }
    }
};

namespace detail {

struct png_type_format_checker
{
    png_type_format_checker( png_bitdepth::type   bit_depth
                           , png_color_type::type color_type
                           )
    : _bit_depth ( bit_depth  )
    , _color_type( color_type )
    {}

    template< typename Image >
    bool apply()
    {
        typedef is_read_supported< typename get_pixel_type< typename Image::view_t >::type
                                 , png_tag
                                 > is_supported_t;

        return is_supported_t::_bit_depth  == _bit_depth
            && is_supported_t::_color_type == _color_type;
    }

private:

    png_bitdepth::type   _bit_depth;
    png_color_type::type _color_type;
};

struct png_read_is_supported
{
    template< typename View >
    struct apply : public is_read_supported< typename get_pixel_type< View >::type
                                           , png_tag
                                           >
    {};
};

} // namespace detail

///
/// PNG Dynamic Image Reader
///
template< typename Device
        >
class dynamic_image_reader< Device
                          , png_tag
                          >
    : public reader< Device
                   , png_tag
                   , detail::read_and_no_convert
                   >
{
    typedef reader< Device
                  , png_tag
                  , detail::read_and_no_convert
                  > parent_t;

public:

    dynamic_image_reader( const Device&                         io_dev
                        , const image_read_settings< png_tag >& settings
                        )
    : parent_t( io_dev
              , settings
              )
    {}

    template< typename Images >
    void apply( any_image< Images >& images )
    {
        detail::png_type_format_checker format_checker( this->_info._bit_depth
                                                      , this->_info._color_type
                                                      );

        if( !construct_matched( images
                              , format_checker
                              ))
        {
            io_error( "No matching image type between those of the given any_image and that of the file" );
        }
        else
        {
            this->init_image( images
                            , this->_settings
                            );

            detail::dynamic_io_fnobj< detail::png_read_is_supported
                                    , parent_t
                                    > op( this );

            apply_operation( view( images )
                           , op
                           );
        }
    }
};

#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(pop)
#endif

} // namespace gil
} // namespace boost

#endif

max_forks_repo_name: ZeroInfinite/turicreate


text:
#include "../src/equivalence-class.h"
#include "../src/utils.h"
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(EcTestSuite)

BOOST_AUTO_TEST_CASE(ec_equality1)
{
    std::bitset<RRType::N> bs;
    bs.set(RRType::A);
    bs.set(RRType::TXT);

    EC ec1;
    ec1.name = LabelUtils::StringToLabels("a.b.c.");
    ec1.excluded = {};
    ec1.nonExistent = false;
    ec1.rrTypes = bs;

    EC ec2;
    ec2.name = LabelUtils::StringToLabels("a.b.c.");
    ec2.excluded = {};
    ec2.nonExistent = false;
    ec2.rrTypes = bs;

    BOOST_CHECK(ec1 == ec2);
}

BOOST_AUTO_TEST_CASE(ec_inequality1)
{
    EC ec1;
    ec1.name = LabelUtils::StringToLabels("a.b.c.");
    ec1.excluded = {};
    ec1.nonExistent = false;

    EC ec2;
    ec2.name = LabelUtils::StringToLabels("b.c.");
    ec2.excluded = {};
    ec2.nonExistent = false;

    BOOST_CHECK(!(ec1 == ec2));
}

BOOST_AUTO_TEST_CASE(ec_inequality2)
{
    std::bitset<RRType::N> bs1;
    bs1.set(RRType::A);

    std::bitset<RRType::N> bs2;
    bs2.set(RRType::A);
    bs2.set(RRType::TXT);

    EC ec1;
    ec1.name = LabelUtils::StringToLabels("a.b.c.");
    ec1.excluded = {};
    ec1.nonExistent = false;
    ec1.rrTypes = bs1;

    EC ec2;
    ec2.name = LabelUtils::StringToLabels("a.b.c.");
    ec2.excluded = {};
    ec2.nonExistent = false;
    ec2.rrTypes = bs2;

    BOOST_CHECK(!(ec1 == ec2));
}

BOOST_AUTO_TEST_CASE(ec_inequality3)
{
    std::bitset<RRType::N> bs1;
    bs1.set(RRType::A);
    bs1.set(RRType::TXT);

    std::bitset<RRType::N> bs2;
    bs2.set(RRType::A);

    EC ec1;
    ec1.name = LabelUtils::StringToLabels("a.b.c.");
    ec1.excluded = {};
    ec1.nonExistent = false;
    ec1.rrTypes = bs1;

    EC ec2;
    ec2.name = LabelUtils::StringToLabels("a.b.c.");
    ec2.excluded = {};
    ec2.nonExistent = false;
    ec2.rrTypes = bs2;

    BOOST_CHECK(!(ec1 == ec2));
}

BOOST_AUTO_TEST_SUITE_END()


text:
//
// Original author: Matt Chambers <matt.chambers42@gmail.com>
//
// Copyright 2020 Matt Chambers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "DiaNNSpecLibReader.h"
#include "pwiz/data/common/Unimod.hpp"
#include "libraries/csv.h"
#include <boost/type_index.hpp>

namespace BiblioSpec
{
const int LATEST_SUPPORTED_VERSION = -3;

// code adapted from CC4-by-licensed diann.cpp by Vadim Demichev (https://github.com/vdemichev/DiaNN)
namespace {

template <class F, class T> void read_vector(F &in, std::vector<T> &v) {
    int size = 0; in.read((char*)&size, sizeof(int));
    if (size) {
        v.resize(size);
        in.read((char*)&(v[0]), size * sizeof(T));
    }
}

template<class F> void read_string(F &in, std::string &s) {
    int size = 0; in.read((char*)&size, sizeof(int));
    if (size) {
        s.resize(size);
        in.read((char*)&(s[0]), size);
    }
}

template <class F, class T> void read_array(F &in, std::vector<T> &a, int v = 0) {
    int size = 0; in.read((char*)&size, sizeof(int));
    Verbosity::debug("array of %d %s", size, boost::typeindex::type_index(typeid(T)).pretty_name().c_str());
    if (size) {
        a.resize(size);
        for (int i = 0; i < size; i++) a[i].read(in, v);
    }
}

template<class F> void read_strings(F &in, std::vector<std::string> &strs) {
    int size = 0; in.read((char*)&size, sizeof(int));
    if (size) {
        strs.resize(size);
        for (int i = 0; i < size; i++) read_string(in, strs[i]);
    }
}

std::vector<std::pair<std::string, std::string> > UniMod;
std::vector<std::pair<std::string, int> > UniModIndex;
std::vector<int> UniModIndices;

struct MOD {
    std::string name;
    std::string aas;
    float mass = 0.0;
    int label = 0;

    MOD() {};
    MOD(const std::string &_name, float _mass, int _label = 0) { name = _name, mass = _mass; label = _label; }
    void init(const std::string &_name, const std::string &_aas, float _mass, int _label = 0) { name = _name, aas = _aas, mass = _mass; label = _label; }
    friend bool operator < (const MOD &left, const MOD &right) { return left.name < right.name || (left.name == right.name && left.mass < right.mass); }
};
std::vector<MOD> FixedMods, VarMods;

std::vector<MOD> Modifications = {
    MOD("UniMod:4", (float)57.021464),
    MOD("Carbamidomethyl (C)", (float)57.021464),
    MOD("Carbamidomethyl", (float)57.021464),
    MOD("CAM", (float)57.021464),
    MOD("+57", (float)57.021464),
    MOD("+57.0", (float)57.021464),
    MOD("UniMod:26", (float)39.994915),
    MOD("PCm", (float)39.994915),
    MOD("UniMod:5", (float)43.005814),
    MOD("Carbamylation (KR)", (float)43.005814),
    MOD("+43", (float)43.005814),
    MOD("+43.0", (float)43.005814),
    MOD("CRM", (float)43.005814),
    MOD("UniMod:7", (float)0.984016),
    MOD("Deamidation (NQ)", (float)0.984016),
    MOD("Deamidation", (float)0.984016),
    MOD("Dea", (float)0.984016),
    MOD("+1", (float)0.984016),
    MOD("+1.0", (float)0.984016),
    MOD("UniMod:35", (float)15.994915),
    MOD("Oxidation (M)", (float)15.994915),
    MOD("Oxidation", (float)15.994915),
    MOD("Oxi", (float)15.994915),
    MOD("+16", (float)15.994915),
    MOD("+16.0", (float)15.994915),
    MOD("Oxi", (float)15.994915),
    MOD("UniMod:1", (float)42.010565),
    MOD("Acetyl (Protein N-term)", (float)42.010565),
    MOD("+42", (float)42.010565),
    MOD("+42.0", (float)42.010565),
    MOD("UniMod:255", (float)28.0313),
    MOD("AAR", (float)28.0313),
    MOD("UniMod:254", (float)26.01565),
    MOD("AAS", (float)26.01565),
    MOD("UniMod:122", (float)27.994915),
    MOD("Frm", (float)27.994915),
    MOD("UniMod:1301", (float)128.094963),
    MOD("+1K", (float)128.094963),
    MOD("UniMod:1288", (float)156.101111),
    MOD("+1R", (float)156.101111),
    MOD("UniMod:27", (float)-18.010565),
    MOD("PGE", (float)-18.010565),
    MOD("UniMod:28", (float)-17.026549),
    MOD("PGQ", (float)-17.026549),
    MOD("UniMod:526", (float)-48.003371),
    MOD("DTM", (float)-48.003371),
    MOD("UniMod:325", (float)31.989829),
    MOD("2Ox", (float)31.989829),
    MOD("UniMod:342", (float)15.010899),
    MOD("Amn", (float)15.010899),
    MOD("UniMod:1290", (float)114.042927),
    MOD("2CM", (float)114.042927),
    MOD("UniMod:359", (float)13.979265),
    MOD("PGP", (float)13.979265),
    MOD("UniMod:30", (float)21.981943),
    MOD("NaX", (float)21.981943),
    MOD("UniMod:401", (float)-2.015650),
    MOD("-2H", (float)-2.015650),
    MOD("UniMod:528", (float)14.999666),
    MOD("MDe", (float)14.999666),
    MOD("UniMod:385", (float)-17.026549),
    MOD("dAm", (float)-17.026549),
    MOD("UniMod:23", (float)-18.010565),
    MOD("Dhy", (float)-18.010565),
    MOD("UniMod:129", (float)125.896648),
    MOD("Iod", (float)125.896648),
    MOD("Phosphorylation (ST)", (float)79.966331),
    MOD("UniMod:21", (float)79.966331),
    MOD("+80", (float)79.966331),
    MOD("+80.0", (float)79.966331),
    MOD("UniMod:259", (float)8.014199, 1),
    MOD("Lys8", (float)8.014199, 1),
    MOD("UniMod:267", (float)10.008269, 1),
    MOD("Arg10", (float)10.008269, 1),
    MOD("UniMod:268", (float)6.013809, 1),
    MOD("UniMod:269", (float)10.027228, 1)
};

inline size_t closing_bracket(const std::string &name, char symbol, size_t pos) {
    size_t end;
    int par;
    char close = (symbol == '(' ? ')' : ']');
    for (end = pos + 1, par = 1; end < name.size(); end++) {
        char s = name[end];
        if (s == close) {
            par--;
            if (!par) break;
        }
        else if (s == symbol) par++;
    }
    return end;
}

inline std::string get_aas(const std::string &name, vector<SeqMod>& mods)
{
    size_t i, j, end;
    std::string result, mod;
    int modPrefixLength = 8; // length of "(UniMod:"

    for (i = j = 0; i < name.size(); i++) {
        char symbol = name[i];
        if (symbol < 'A' || symbol > 'Z') {
            if (symbol != '(' && symbol != '[') continue;
            end = closing_bracket(name, symbol, i);

            if (name.find("(UniMod:", i, modPrefixLength) != i)
                throw std::runtime_error("unable to handle mod in library entry: " + name);

            i += modPrefixLength;
            mod = name.substr(i, end-i);
            CVID unimodCvid = (CVID) (UNIMOD_unimod_root_node + lexical_cast<int>(mod));
            mods.emplace_back(SeqMod((int) j, pwiz::data::unimod::modification(unimodCvid).deltaMonoisotopicMass()));
            continue;
        }
        ++j;
        result.push_back(symbol);
    }
    return result;
}

inline void to_eg(std::string &eg, const std::string &name) {
    size_t i, j;

    eg.clear();
    for (i = 0; i < name.size(); i++) {
        char symbol = name[i];
        if (symbol < 'A' || symbol > 'Z') {
            if (symbol != '(' && symbol != '[') continue;
            i++;

            size_t end = closing_bracket(name, symbol, i);
            if (end == std::string::npos) throw std::runtime_error(std::string("incorrect peptide name format: ") + name);

            std::string mod = name.substr(i, end - i);
            for (j = 0; j < Modifications.size(); j++) if (Modifications[j].name == mod) {
                if (!Modifications[j].label) eg += std::string("(") + UniMod[j].second + std::string(")");
                break;
            }
            if (j == Modifications.size()) eg += std::string("(") + mod + std::string(")"); // no warning here
            i = end;
            continue;
        }
        else eg.push_back(symbol);
    }
}

inline std::string to_eg(const std::string &name) {
    std::string eg;
    to_eg(eg, name);
    return eg;
}

inline std::string to_charged_eg(const std::string &name, int charge) { return to_eg(name) + std::to_string(charge); }

enum {
loss_none, loss_H2O, loss_NH3, loss_CO,
loss_N, loss_other
};

const int fFromFasta = 1 << 0;
const int fPredictedSpectrum = 1 << 1;
const int fPredictedRT = 1 << 2;

class Product {
public:
float mz = 0.0;
float height = 0.0;
char charge = 0, type = 0, index = 0, loss = 0;

Product() {}
Product(float _mz, float _height, int _charge) {
    mz = _mz;
    height = _height;
    charge = _charge;
}
Product(float _mz, float _height, int _charge, int _type, int _index, int _loss) {
    mz = _mz;
    height = _height;
    charge = _charge;
    type = _type;
    index = _index;
    loss = _loss;
}
void init(float _mz, float _height, int _charge) {
    mz = _mz;
    height = _height;
    charge = _charge;
}
friend inline bool operator < (const Product &left, const Product &right) { return left.mz < right.mz; }

inline int ion_code() { return (((((int)type) * 20 + (int)charge)) * (loss_other + 1) + (int)loss) * 100 + (int)index + 1; }
};

class Peptide {
    public:
    int index = 0, charge = 0, length = 0, no_cal = 0;
    float mz = 0.0, iRT = 0.0, sRT = 0.0, lib_qvalue = 0.0;
    float iIM = 0.0, sIM = 0.0;
    std::vector<Product> fragments;

    void init(float _mz, float _iRT, int _charge, int _index) {
        mz = _mz;
        iRT = _iRT;
        sRT = 0.0;
        charge = _charge;
        index = _index;
    }

    inline void free() {
        std::vector<Product>().swap(fragments);
    }

    template <class F> void read(F &in, int v) {
        in.read((char*)&index, sizeof(int));
        in.read((char*)&charge, sizeof(int));
        in.read((char*)&length, sizeof(int));

        in.read((char*)&mz, sizeof(float));
        in.read((char*)&iRT, sizeof(float));
        in.read((char*)&sRT, sizeof(float));

        if (v <= -2) {
            in.read((char*)&lib_qvalue, sizeof(float));
            in.read((char*)&iIM, sizeof(float));
            in.read((char*)&sIM, sizeof(float));
        }

        read_vector(in, fragments);
    }
};

class Isoform {
public:
    std::string id;
    mutable std::string name, gene, description;
    mutable std::set<int> precursors; // precursor indices in the library
    int name_index = 0, gene_index = 0;
    bool swissprot = true;

    Isoform() {}
    Isoform(const std::string &_id) { id = _id; }
    Isoform(const std::string &_id, const std::string &_name, const std::string &_gene, const std::string &_description, bool _swissprot) {
        id = _id;
        name = _name;
        gene = _gene;
        description = _description;
        swissprot = _swissprot;
    }

    friend inline bool operator < (const Isoform &left, const Isoform &right) { return left.id < right.id; }

    template <class F> void read(F &in, int v = 0) {
        int sp = 0, size = 0;
        in.read((char*)&sp, sizeof(int));
        in.read((char*)&size, sizeof(int));
        swissprot = sp;

        read_string(in, id);
        read_string(in, name);
        read_string(in, gene);
        in.read((char*)&name_index, sizeof(int));
        in.read((char*)&gene_index, sizeof(int));
        precursors.clear();
        for (int i = 0; i < size; i++) {
            int pr = -1;
            in.read((char*)&pr, sizeof(int));
            if (pr >= 0) precursors.insert(pr);
        }
    }
};

class PG {
public:
    std::string ids;
    mutable std::string names, genes;
    mutable std::vector<int> precursors; // precursor indices in the library
    mutable std::set<int> proteins;
    std::vector<int> name_indices, gene_indices;

    PG() {}

    PG(std::string _ids) {
        ids = _ids;
    }

    friend inline bool operator < (const PG &left, const PG &right) { return left.ids < right.ids; }

    template <class F> void read(F &in, int v = 0) {
        int size_p = 0;
        in.read((char*)&size_p, sizeof(int));

        read_string(in, ids);
        read_string(in, names);
        read_string(in, genes);
        read_vector(in, name_indices);
        read_vector(in, gene_indices);
        read_vector(in, precursors);
        for (int i = 0; i < size_p; i++) {
            int p = -1;
            in.read((char*)&p, sizeof(int));
            if (p >= 0) proteins.insert(p);
        }
    }
};

class Library {
    public:
    std::string name, fasta_names;
    std::vector<Isoform> proteins;
    std::vector<PG> protein_ids;
    std::vector<PG> protein_groups;
    std::vector<PG> gene_groups;
    std::vector<int> gg_index;
    std::vector<std::string> precursors; // precursor IDs in canonical format; library-based entries only
    std::vector<std::string> names;
    std::vector<std::string> genes;

    int skipped = 0;
    double iRT_min = 0.0, iRT_max = 0.0;
    bool gen_decoys = true, gen_charges = true, infer_proteotypicity = true, from_speclib = false;

    std::map<std::string, int> eg;
    std::vector<int> elution_groups; // same length as entries, indices of the elution groups
    std::vector<int> co_elution;
    std::vector<std::pair<int, int> > co_elution_index;

    class Entry {
        public:
        Library * lib;
        Peptide target, decoy;
        int entry_flags = 0, proteotypic = 0;
        std::string name; // precursor id
        std::set<PG>::iterator prot;
        int pid_index = 0, pg_index = 0, eg_id, best_run = -1, peak = 0, apex = 0, window = 0;
        float qvalue = 0.0, pg_qvalue = 0.0, best_fr_mz = 0.0, ptm_qvalue, site_conf;

        // temporary variables used while processing rows from a single run in the report file
        float curQValue, curPEP, curRT, curRTStart, curRTStop, curIM;

        friend bool operator < (const Entry &left, const Entry &right) { return left.name < right.name; }

        inline void free() {
            target.free();
            decoy.free();
        }

        template <class F> void read(F &in, int v) {
            target.read(in, v);
            int dc = 0; in.read((char*)&dc, sizeof(int));
            if (dc) decoy.read(in, v);

            int ff = 0, prt = 0;
            in.read((char*)&ff, sizeof(int));
            in.read((char*)&prt, sizeof(int));
            entry_flags = ff, proteotypic = prt;
            in.read((char*)&pid_index, sizeof(int));
            read_string(in, name);
            if (v <= -3) {
                in.read((char*)&pg_qvalue, sizeof(float));
                in.read((char*)&ptm_qvalue, sizeof(float));
                in.read((char*)&site_conf, sizeof(float));
            }
        }
    };

    std::vector<Entry> entries;
    std::map<string, std::reference_wrapper<Entry>> entryByModPeptideAndCharge;

    template<class F> void read(F &in) {
        int gd = 0, gc = 0, ip = 0, version = 0;

        in.read((char*)&version, sizeof(int));
        if (version >= 0) { gd = version; version = 0; }
        else in.read((char*)&gd, sizeof(int));

        if (version < LATEST_SUPPORTED_VERSION)
            Verbosity::error("speclib file has version %d, but BiblioSpec only supports up to version %d", -1*version, -1*LATEST_SUPPORTED_VERSION);
        else
            Verbosity::debug("speclib file has version %d",  -1*version);

        in.read((char*)&gc, sizeof(int));
        in.read((char*)&ip, sizeof(int));
        gen_decoys = gd, gen_charges = gc, infer_proteotypicity = ip;

        read_string(in, name);
        read_string(in, fasta_names);
        read_array(in, proteins);
        read_array(in, protein_ids);
        read_strings(in, precursors);
        read_strings(in, names);
        read_strings(in, genes);
        in.read((char*)&iRT_min, sizeof(double));
        in.read((char*)&iRT_max, sizeof(double));
        read_array(in, entries, version);
        for (auto &e : entries)
        {
            e.lib = this;
            entryByModPeptideAndCharge.emplace(e.name, std::ref(e));
        }
        if (version <= -1 && in.peek() != std::char_traits<char>::eof()) read_vector(in, elution_groups);
    }

    void assemble_elution_groups() {
        Verbosity::debug("Assembling elution groups");
        eg.clear(), elution_groups.clear(), elution_groups.reserve(entries.size());
        for (auto &e : entries) {
            auto name = to_eg(e.name);
            auto egp = eg.insert(std::pair<std::string, int>(name, (int) eg.size()));
            elution_groups.push_back(e.eg_id = egp.first->second);
        }
        eg.clear();
    }

    void elution_group_index() {
        size_t i, egt = 0, peg;
        for (i = 0; i < elution_groups.size(); i++) if (elution_groups[i] > egt) egt = elution_groups[i]; egt++;
        co_elution_index.resize(egt, std::pair<int, int>(0, 1));
        std::set<std::pair<int, int> > ce;
        for (i = 0; i < elution_groups.size(); i++) ce.insert(std::pair<int, int>(elution_groups[i], (int) i));
        co_elution.resize(ce.size()); i = peg = 0;
        if (co_elution_index.size()) co_elution_index[0].second = 0;
        for (auto it = ce.begin(); it != ce.end(); it++, i++) {
            co_elution[i] = it->second;
            if (it->first != (int) peg) co_elution_index[it->first].first = (int) i;
            else co_elution_index[it->first].second++;
            peg = it->first;
        }
        ce.clear();
    }

    bool load(const char * file_name) {

        name = std::string(file_name);
        Verbosity::debug("Loading spectral library %s", name.c_str());

        std::ifstream speclib(file_name, std::ifstream::binary);
        if (speclib.fail()) {
            Verbosity::error("cannot read the file");
            return false;
        }
        read(speclib);
        speclib.close();

        from_speclib = true;
        /*if (PGLevel != PGLevelSet) {
            if (PGLevelSet == 2 && genes.size() >= 2) PGLevel = 2;
            else if (PGLevelSet == 1 && names.size() >= 2) PGLevel = 1;
        }*/
        if (fasta_names.size()) Verbosity::debug("Library annotated with sequence database(s): %s", fasta_names.c_str());
        //if (!fasta_files.size() && (genes.size() >= 2 || names.size() >= 2)) library_protein_stats();
        if (!elution_groups.size()) assemble_elution_groups();

        int gc = gen_charges;
        gen_charges = false;
        for (auto &e : entries) {
            e.entry_flags &= ~fFromFasta;
            if (gc) for (auto &f : e.target.fragments) if (!(int)f.charge) gen_charges = true, gc = 0;
        }

        int ps = (int) proteins.size(), pis = (int) protein_ids.size();
        if (ps) if (!proteins[0].id.size()) ps--;
        if (pis) if (!protein_ids[0].ids.size()) pis--;

        elution_group_index();

        Verbosity::status("Spectral library loaded: %d protein isoforms, %d protein groups and %d precursors in %d elution groups.", ps, pis, entries.size(), co_elution_index.size());

        return true;
    }
};

} // end code adapted from diann.cpp

class DiaNNSpecLibReader::Impl
{
    public:
    Impl(const char* specLibFile) : specLibFile_(specLibFile) {}

    Library specLib;
    const char* specLibFile_;
};

DiaNNSpecLibReader::DiaNNSpecLibReader(BlibBuilder& maker, const char* specLibFile, const ProgressIndicator* parent_progress)
    : BuildParser(maker, specLibFile, parent_progress),
    impl_(new Impl(specLibFile))
{
    setSpecFileName(specLibFile, false);
    lookUpBy_ = INDEX_ID;
    // point to self as spec reader
    delete specReader_;
    specReader_ = this;
}

DiaNNSpecLibReader::~DiaNNSpecLibReader()
{
    specReader_ = NULL; // so the parent class doesn't try to delete itself
}

bool DiaNNSpecLibReader::parseFile()
{
    {
        ifstream specLibStream(impl_->specLibFile_, ios::binary);
        impl_->specLib.read(specLibStream);
    }

    typedef io::CSVReader<8, io::trim_chars<' ', ' '>, io::no_quote_escape<'\t'>> ReportReaderType;

    auto diannReportFilepath = bal::replace_last_copy(string(impl_->specLibFile_), "-lib.tsv.speclib", "-report.tsv");
    /*if (diannReportFilepath == impl_->specLibFile_)
        throw BlibException(true, "unable to determine DIA-NN report filename for '%s': speclib must end in -lib.tsv.speclib and report must end in -report.tsv", impl_->specLibFile_);
    if (!bfs::exists(diannReportFilepath))
        throw BlibException(true, "could not find DIA-NN report file '%s' for '%s'", bfs::path(diannReportFilepath).filename().string().c_str(), impl_->specLibFile_);*/
    if (diannReportFilepath == impl_->specLibFile_ || !bfs::exists(diannReportFilepath))
    {
        // iterate all TSV files in the same directory as the speclib, check the ones sharing the most leading characters, and look for the report headers in them
        vector<bfs::path> siblingTsvFiles;
        pwiz::util::expand_pathmask(bfs::path(impl_->specLibFile_).parent_path() / "*.tsv", siblingTsvFiles);
        multimap<int, bfs::path> tsvFilepathBySharedPrefixLength;
        auto speclibFilename = bfs::path(impl_->specLibFile_).filename().string();
        for (const auto& tsvFilepath : siblingTsvFiles)
        {
            if (!bfs::is_regular_file(tsvFilepath))
                continue;
            if (bal::contains(tsvFilepath.filename().string(), "first-pass") && !bal::contains(speclibFilename, "first-pass"))
                continue;
            int sharedPrefixLength = 0, i;
            auto tsvFilename = tsvFilepath.filename().string();
            for (i = 0; i < tsvFilename.length() && i < speclibFilename.length(); ++i)
                if (tsvFilename[i] != speclibFilename[i])
                    break;
            sharedPrefixLength = i;
            tsvFilepathBySharedPrefixLength.emplace(sharedPrefixLength, tsvFilepath);
        }

        diannReportFilepath.clear();
        for (const auto& kvp : boost::make_iterator_range(tsvFilepathBySharedPrefixLength.rbegin(), tsvFilepathBySharedPrefixLength.rend()))
        {
            if (kvp.first < 1)
                break;

            io::CSVReader<1, io::trim_chars<' ', ' '>, io::no_quote_escape<'\t'>> reportReader(kvp.second.string().c_str());
            reportReader.read_header(io::ignore_extra_column | io::ignore_missing_column, "Precursor.Id");
            if (reportReader.has_column("Precursor.Id"))
            {
                diannReportFilepath = kvp.second.string();
                break;
            }
        }

        if (diannReportFilepath.empty())
            throw BlibException(true, "unable to determine DIA-NN report filename for '%s': the TSV report is required to read speclib files and must be in the same directory as the speclib and share some leading characters (e.g. somedata-tsv.speclib and somedata-report.tsv)",
                impl_->specLibFile_);
    }

    auto& speclib = impl_->specLib;
    set<string> processedRuns;
    map<string, int> runNameByIndex;
    map<int, string> runIndexByName;
    bool hasSkippedRuns = false;
    // read runs one at a time from the report file in a loop until all runs have been processed
    do
    {
        ReportReaderType reportReader(diannReportFilepath.c_str());
        reportReader.read_header(io::ignore_extra_column, "Run", "Precursor.Id", "Q.Value", "PEP", "RT", "RT.Start", "RT.Stop", "IM");
        char *run, *precursorId;
        float qValue, pep, rt, rtStart, rtStop, im;
        string currentRun;
        hasSkippedRuns = false;
        while (reportReader.read_row(run, precursorId, qValue, pep, rt, rtStart, rtStop, im))
        {
            if (currentRun.empty())
            {
                // skip rows from runs that have already been processed
                if (processedRuns.count(run) > 0)
                    continue;
                currentRun = run;
            }
            else if (!bal::equals(currentRun.c_str(), run))
            {
                // skip rows not from the current runs being processed; another loop iteration will be required
                hasSkippedRuns = processedRuns.count(run) == 0;
                continue;
            }

            auto findItr = speclib.entryByModPeptideAndCharge.find(precursorId);
            if (findItr == speclib.entryByModPeptideAndCharge.end())
                throw BlibException(false, "could not find precursorId '%s' in speclib; is '%s' the correct report TSV file?", precursorId, bfs::path(diannReportFilepath).filename().string().c_str());

            auto& speclibEntry = findItr->second.get();
            speclibEntry.curQValue = qValue;
            speclibEntry.curPEP = pep;
            speclibEntry.curRT = rt;
            speclibEntry.curRTStart = rtStart;
            speclibEntry.curRTStop = rtStop;
            speclibEntry.curIM = im;

            PSM* psm = new PSM;
            psm->charge = speclibEntry.target.charge;
            psm->unmodSeq = get_aas(speclibEntry.name, psm->mods);
            psm->specIndex = speclibEntry.target.index;
            psm->score = qValue;
            psms_.emplace_back(psm);
        }

        // insert PSMs for the current run
        buildTables(GENERIC_QVALUE, currentRun);
        processedRuns.insert(currentRun);
        currentRun.clear();
    }
    while (hasSkippedRuns);

    return true;
}

// SpecFileReader methods
/**
    * Implemented to satisfy SpecFileReader interface.  Since spec and
    * results files are the same, no need to open a new one.
    */
void DiaNNSpecLibReader::openFile(const char* filename, bool mzSort) {}

void DiaNNSpecLibReader::setIdType(SPEC_ID_TYPE type) {}

/**
    * Return a spectrum via the returnData argument.  If not found in the
    * spectra map, return false and leave returnData unchanged.
    */
bool DiaNNSpecLibReader::getSpectrum(int identifier, SpecData& returnData, SPEC_ID_TYPE findBy, bool getPeaks)
{
    const auto& entry = impl_->specLib.entries.at(identifier);

    returnData.charge = entry.target.charge;
    returnData.id = entry.target.index;
    returnData.numPeaks = entry.target.fragments.size();
    returnData.mz = entry.target.mz;
    returnData.retentionTime = entry.curRT;
    returnData.startTime = entry.curRTStart;
    returnData.endTime = entry.curRTStop;
    returnData.ionMobility = entry.curIM;
    if (returnData.ionMobility > 0)
        returnData.ionMobilityType = IONMOBILITY_INVERSEREDUCED_VSECPERCM2;

    if (!getPeaks)
        return true;

    returnData.mzs = new double[returnData.numPeaks];
    returnData.intensities = new float[returnData.numPeaks];
    for (size_t i=0; i < returnData.numPeaks; ++i)
    {
        const auto& product = entry.target.fragments[i];
        returnData.mzs[i] = product.mz;
        returnData.intensities[i] = product.height;
    }
    return true;
}

/**
    * Only specific spectra can be accessed from the DiaNNSpecLibReader.
    */
bool DiaNNSpecLibReader::getSpectrum(string identifier, SpecData& returnData, bool getPeaks)
{
    Verbosity::warn("DiaNNSpecLibReader cannot fetch spectra by string identifier, "
        "only by spectrum index.");
    return false;
}

/**
    * Only specific spectra can be accessed from the DiaNNSpecLibReader.
    */
bool DiaNNSpecLibReader::getNextSpectrum(SpecData& returnData, bool getPeaks)
{
    Verbosity::warn("DiaNNSpecLibReader does not support sequential file reading.");
    return false;
}

} // namespace BiblioSpec

max_stars_repo_name: mhhur/pwiz

max_issues_repo_name: mhhur/pwiz

max_forks_repo_name: mhhur/pwiz


text:
//  Copyright 2017 Peter Dimov.
//
//  Distributed under the Boost Software License, Version 1.0.
//
//  See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt

#include <boost/config/pragma_message.hpp>

BOOST_PRAGMA_MESSAGE("first message")

#define MSG2 "second message"
BOOST_PRAGMA_MESSAGE(MSG2)

#include <boost/config.hpp> // BOOST_STRINGIZE

#define MSG3 third message
BOOST_PRAGMA_MESSAGE(BOOST_STRINGIZE(MSG3))


text:
#ifndef _TESTSHP_CPP__
#define _TESTSHP_CPP__

#include <iostream>
#include <vector>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <time.h>
#include <stdlib.h>
#include "OrientedGraph.h"
#include "UnorientedGraphValuedEdge.hpp"
#include "GraphManager.h"
#include "AdjacencyMatrixUnoriented.h"

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
                               boost::property<boost::edge_weight_t,int> > BoostGraph;
typename BoostGraph::vertex_descriptor u, v;
typedef typename BoostGraph ::edge_property_type Weight;

int randInt()
{
    return rand() % 256;
}

void TestShortestPaths(double probability)
{
    const int SIZE = 10000;
    UnorientedGraphValuedEdge<int>* g = new UnorientedGraphValuedEdge<int>(SIZE, new AdjacencyMatrixUnoriented());
    g->RandomizeGraph(probability, &randInt);
    clock_t start_t, end_t;
    std::cout << "Probability: " << probability << std::endl;
    double sumBFS = 0;
    double sumDj = 0;
    double sumBDj = 0;
    std::vector<int> requests;
    for(int i = 0; i < 20; ++i)
    {
        int u = rand() % SIZE;
        int v = rand() % SIZE;
        requests.push_back(u);
        start_t = clock();
        g->BFS(u, v);
        end_t = clock();
        sumBFS += (float)(end_t - start_t)/CLOCKS_PER_SEC;

        start_t = clock();
        g->Dijkstra(u, v);
        end_t = clock();
        sumDj += (float)(end_t - start_t)/CLOCKS_PER_SEC;
    }
    BoostGraph gb(g->Size());
    for(int i = 0; i < g->Size(); ++i)
        for(int j = i + 1; j < g->Size(); ++j)
            if(g->CheckEdge(i, j))
            {
                boost::add_edge(boost::vertex(i, gb), boost::vertex(j,gb), Weight(g->GetEdgeValue(i, j)), gb);
            }

    std::vector<BoostGraph::vertex_descriptor> p(boost::num_vertices(gb));
    std::vector<int> d(boost::num_vertices(gb));

    auto bgl = boost::predecessor_map(boost::make_iterator_property_map(p.begin(),
                            boost::get(boost::vertex_index, gb))).
                            distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, gb)));
    delete g;
    for(int i = 0; i < 20; ++i)
    {
        BoostGraph::vertex_descriptor s = boost::vertex(u, gb);
        start_t = clock();
        boost::dijkstra_shortest_paths(gb, s, bgl);
        end_t = clock();
        sumBDj += (float)(end_t - start_t)/CLOCKS_PER_SEC;
    }
    std::cout << "\tBFS 1-K " << sumBFS << std::endl;
    std::cout << "\tDijkstra " << sumDj << std::endl;
    std::cout << "\tboost::Dijkstra " << sumBDj << std::endl;
}

void TestMy1KBFS(int u, int v)
{
    /*GraphValuedEdge* g = new GraphValuedEdge(10000);
    g->RandomizeUnorientedGraph(0.02);
    g->Graph::WriteToFile("graph.gr");*/
    UnorientedGraphValuedEdge<int>* g = new UnorientedGraphValuedEdge<int>(1000, new AdjacencyMatrixUnoriented());
    g->RandomizeGraph(0.05, &randInt);
    std::cout << g->BFS(u, v) << std::endl;
    delete g;
}
void TestMy1KBFS()
{
    unsigned int u, v;
    std::cin >> u;
    std::cin >> v;
    TestMy1KBFS(u, v);
}
void TestMyDijkstra(int u, int v)
{
    UnorientedGraphValuedEdge<int>* g = new UnorientedGraphValuedEdge<int>(10000, new AdjacencyMatrixUnoriented());
    g->RandomizeGraph(0.05, &randInt);
    std::cout << g->Dijkstra(u,v) << std::endl;
    delete g;
}
void TestMyDijkstra()
{
    unsigned int u, v;
    std::cin >> u;
    std::cin >> v;
    TestMyDijkstra(u, v);
}
void TestBoostDijkstra(int u, int v)
{
    UnorientedGraphValuedEdge<int>* g = new UnorientedGraphValuedEdge<int>(10000, new AdjacencyMatrixUnoriented());
    g->RandomizeGraph(0.05, &randInt);
    BoostGraph gb(g->Size());
    for(int i = 0; i < g->Size(); ++i)
        for(int j = i + 1; j < g->Size(); ++j)
            if(g->CheckEdge(i, j))
            {
                boost::add_edge(boost::vertex(i, gb), boost::vertex(j,gb), Weight(g->GetEdgeValue(i, j)), gb);
            }

    std::vector<BoostGraph::vertex_descriptor> p(boost::num_vertices(gb));
    std::vector<int> d(boost::num_vertices(gb));
    BoostGraph::vertex_descriptor s = boost::vertex(u, gb);
    boost::dijkstra_shortest_paths(gb, s,
                                                boost::predecessor_map(boost::make_iterator_property_map(p.begin(),
                                                                                boost::get(boost::vertex_index, gb))).
                                   distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, gb))));
    std::cout << d[v] << std::endl;
    delete g;
}
void TestBoostDijkstra()
{
    unsigned int u, v;
    std::cin >> u;
    std::cin >> v;
    TestBoostDijkstra(u, v);
}

#endif

max_stars_repo_name: IKholopov/StudyingStuff

max_issues_repo_name: IKholopov/StudyingStuff

max_forks_repo_name: IKholopov/StudyingStuff


text:
/*
** EPITECH PROJECT, 2021
** B-CPP-500-LYN-5-1-babel-
** File description:
** TCP
*/

#include "client/TCP.hpp"
#include "../GUI/App.h"
#include "../GUI/UserMenu.h"

using boost::asio::ip::tcp;

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <thread>

TCP::TCP(App *appCopy, std::string ip) : strand(io_context)
{
    try
    {
        io_context.run();
        tcp::endpoint ep(boost::asio::ip::address::from_string(ip), 2000);
        socket = new tcp::socket(io_context);
        socket->connect(ep);
        std::thread reader(&TCP::async_read, this);
        reader.detach();
    }
    catch (std::exception &e)
    {
        std::cerr << e.what() << std::endl;
        this->_connected = false;
        return;
    }
    this->_connected = true;
    this->copy = appCopy;
}

TCP::~TCP()
{
    this->socket->close();
}

void TCP::doConnect()
{
    try
    {
        tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 2000);
        socket = new tcp::socket(io_context);
        socket->connect(ep);
    }
    catch (std::exception &e)
    {
        std::cerr << e.what() << std::endl;
        this->_connected = false;
        socket->close();
        return;
    }
    this->_connected = true;
}

void TCP::read(const boost::system::error_code &error, size_t bytes_recvd)
{
    std::cout << "read start" << std::endl;
    if (error) {
        std::cout << error.message() << std::endl;
        return;
    }
    auto raw = security::decoder(buf);
    std::cout << "read" << raw << std::endl;
    if (raw.substr(0, raw.find('?')) == "accept") {
        this->copy->setGroupId(std::atoi(raw.substr(raw.find('?')).c_str()));
        UserMenu *menu = this->copy->getUsermenu();
        menu->getCallW()->setScene(Call::RECEIVECALL, "On vous appelle");
        buf.assign(0);
    }
    async_read();
}

void TCP::async_read()
{
    while (true) {
        try {
        this->socket->receive(
            boost::asio::buffer(buf)
        );
        } catch(std::exception &e) {
            this->_connected = false;
            continue;
        }
        this->mutex.lock();
        auto raw = security::decoder(buf);
        buf.assign(0);
        if (raw.substr(0, raw.find('?')) == "accept") {
            std::cout << "find ?" << std::endl;
            this->copy->setGroupId(std::atoi(raw.substr(raw.find('?')).c_str()));
            UserMenu *menu = this->copy->getUsermenu();
            menu->getCallW()->setScene(Call::RECEIVECALL, "On vous appelle");
        }
        queue.push(raw);
        this->mutex.unlock();
    }
    std::cout << "out" << std::endl;
}

std::string TCP::sendCommand(std::string command)
{
    boost::array<std::bitset<16>, max_length> encoded = security::encoder(command);
    boost::system::error_code error;
    std::string raw;

    try {
        socket->send(
            boost::asio::buffer(
                encoded,
                max_length
            )
        );
    } catch(std::exception &e) {
        std::cerr << e.what() << std::endl;
        this->_connected = false;
        return "not connected";
    }
    usleep(10000);
    this->mutex.lock();
    if (queue.empty())
        return "";
    raw = queue.front();
    queue.pop();
    this->mutex.unlock();
    encoded.assign(0);
    return raw;
}