text:
// Copyright(c) 2021, Richardson Lab at Duke
// Licensed under the Apache 2 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 permissionsand
// limitations under the License.

#include <cmath>
#include <scitbx/constants.h>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include "SpatialQuery.h"

namespace molprobity {
  namespace probe {

const Coord molprobity::probe::SpatialQuery::DEFAULT_BIN_SIZE = 3;  ///< Default size of a grid bin in X, Y, and Z.

void SpatialQuery::initialize(Point lowerBounds, Point upperBounds, Point binSize)
{
  // Set the bin size
  m_binSize = binSize;

  // Determine the proper location and size for the grid.
  for (size_t i = 0; i < 3; i++) {

    // Upper bounds must be at or above lower bounds
    double val = lowerBounds[i];
    m_lowerBounds[i] = val;
    if (upperBounds[i] < val) { upperBounds[i] = val; }

    // If the bin size is less than or equal to zero, set it to 1.
    if (binSize[i] <= 0) { binSize[i] = 1; }
    m_binSize[i] = binSize[i];

    // Find the grid size on each axis, which must be large enough to span the whole
    // range and must have at least one entry on each axis.
    m_gridSize[i] = static_cast<size_t>(ceil((upperBounds[i] - lowerBounds[i]) / m_binSize[i]));
    if (m_gridSize[i] == 0) { m_gridSize[i] = 1; }
  }

  // Allocate the space for the grid.  It will start with empty vectors for each element
  m_grid.resize(m_gridSize[0] * m_gridSize[1] * m_gridSize[2]);
}

SpatialQuery::SpatialQuery(Point lowerBounds, Point upperBounds, Point binSize)
{
  initialize(lowerBounds, upperBounds, binSize);
}

SpatialQuery::SpatialQuery(scitbx::af::shared<iotbx::pdb::hierarchy::atom> atoms)
{
  // Compute the parameters needed and initialize the grid
  Point lowerBounds(1e10, 1e10, 1e10);
  Point upperBounds(-1e10, -1e10, -1e10);
  for (scitbx::af::shared<iotbx::pdb::hierarchy::atom>::const_iterator a = atoms.begin();
       a != atoms.end(); a++) {
    Point loc = a->data->xyz;
    for (size_t i = 0; i < 3; i++) {
      if (loc[i] < lowerBounds[i]) { lowerBounds[i] = loc[i]; }
      if (loc[i] > upperBounds[i]) { upperBounds[i] = loc[i]; }
    }
  }

  // Make sure that we don't have more than 50 bins on a given axis
  Point binSize(DEFAULT_BIN_SIZE, DEFAULT_BIN_SIZE, DEFAULT_BIN_SIZE);
  for (size_t i = 0; i < 3; i++) {
    Coord minSize = (upperBounds[i] - lowerBounds[i]) / 50;
    if (binSize[i] < minSize) { binSize[i] = minSize; }
  }
  initialize(lowerBounds, upperBounds, binSize);

  // Add all of the atoms in the model
  for (scitbx::af::shared<iotbx::pdb::hierarchy::atom>::const_iterator a = atoms.begin();
       a != atoms.end(); a++) {
    add(*a);
  }
}

bool SpatialQuery::add(iotbx::pdb::hierarchy::atom a)
{
  // Look up the index of the grid element for the location of the atom.
  // Attempt to insert the atom at that location.
  // Return whether the insertion was a success.
  return m_grid[grid_index(a.data->xyz)].insert(a).second;
}

bool SpatialQuery::remove(iotbx::pdb::hierarchy::atom a)
{
  // Look up the index of the grid element for the location of the atom.
  // Attempt to remove the atom at that location.
  // Return whether the insertion was a success (a 1 means a success).
  return m_grid[grid_index(a.data->xyz)].erase(a) == 1;
}

scitbx::af::shared<iotbx::pdb::hierarchy::atom> SpatialQuery::neighbors(
  Point const& p, double min_distance, double max_distance)
{
  // Find the range of bins that are within the maximum distance of the point.
  // Handle cases where the point is at any location relative to the grid: inside,
  // outside to left or right on one or more axes.
  // We overestimate here, counting all bins within range in X, Y, or Z.
  boost::array<size_t, 3> lowerIndex, upperIndex;
  for (size_t i = 0; i < 3; i++) {
    Coord lower = p[i] - max_distance;
    if (lower < m_lowerBounds[i]) { lowerIndex[i] = 0; }
    else { lowerIndex[i] = static_cast<size_t>(floor((lower - m_lowerBounds[i]) / m_binSize[i])); }
    if (lowerIndex[i] >= m_gridSize[i]) { lowerIndex[i] = m_gridSize[i] - 1; }

    Coord upper = p[i] + max_distance;
    if (upper < m_lowerBounds[i]) { upperIndex[i] = 0; }
    else { upperIndex[i] = static_cast<size_t>(floor((upper - m_lowerBounds[i]) / m_binSize[i])); }
    if (upperIndex[i] >= m_gridSize[i]) { upperIndex[i] = m_gridSize[i] - 1; }
  }

  // Look through each atom in each potential bin and add it to the result if
  // it is within the distance range.  We square the distances ranges and compare
  // against these to avoid having to do square roots.
  double min2 = min_distance * min_distance;
  double max2 = max_distance * max_distance;
  scitbx::af::shared<iotbx::pdb::hierarchy::atom> ret;
  for (size_t x = lowerIndex[0]; x <= upperIndex[0]; x++) {
    for (size_t y = lowerIndex[1]; y <= upperIndex[1]; y++) {
      for (size_t z = lowerIndex[2]; z <= upperIndex[2]; z++) {
        size_t index = x + m_gridSize[0] * (y + m_gridSize[1] * (z));
        for (std::set<iotbx::pdb::hierarchy::atom, atom_less>::const_iterator a = m_grid[index].begin();
             a != m_grid[index].end(); a++) {
          double dist2 = (a->data->xyz - p).length_sq();
          if ((dist2 >= min2) && (dist2 <= max2)) {
            ret.push_back(*a);
          }
        }
      }
    }
  }

  return ret;
}

//============================================================================================
// Test data and code below here.

std::string SpatialQuery::test()
{
  // Construct a model with some atoms in it so that we can use it for our tests.
  // Make it a bunch of Hydrogen atoms in a regular 3D grid
  iotbx::pdb::hierarchy::model m;
  iotbx::pdb::hierarchy::chain c;
  iotbx::pdb::hierarchy::residue_group rg;
  iotbx::pdb::hierarchy::atom_group ag;
  size_t numAtoms = 10;
  Coord spacing = 5;
  for (int x = 0; x < numAtoms; x++) {
    for (int y = 0; y < numAtoms; y++) {
      for (int z = 0; z < numAtoms; z++) {
        Point v(x * spacing, y * spacing, z * spacing);
        iotbx::pdb::hierarchy::atom a(v, v);
        ag.append_atom(a);
      }
    }
  }
  rg.append_atom_group(ag);
  c.append_residue_group(rg);
  m.append_chain(c);

  // Test creation of a grid and see if it is the size that we expect it to be
  {
    Point lower(-10, -10, -10);
    Point upper( 11,  11,  11);
    Point binSize(5, 5, 5);
    SpatialQuery q(lower, upper, binSize);
    if (q.m_lowerBounds != lower) {
      return "molprobity::probe::SpatialQuery::test(): Unexpected lower bound on standard construction";
    }
    boost::array<size_t, 3> expected = { { 5, 5, 5 } };
    if (q.m_gridSize != expected) {
      return "molprobity::probe::SpatialQuery::test(): Unexpected grid size on standard construction";
    }
  }

  // Test creation of a grid with upper below lower
  {
    Point lower(-10, -10, -10);
    Point upper(-11, -11, -11);
    Point binSize(5, 5, 5);
    SpatialQuery q(lower, upper, binSize);
    if (q.m_lowerBounds != lower) {
      return "molprobity::probe::SpatialQuery::test(): Unexpected lower bound on inverted construction";
    }
    boost::array<size_t, 3> expected = { { 1, 1, 1 } };
    if (q.m_gridSize != expected) {
      return "molprobity::probe::SpatialQuery::test(): Unexpected grid size on inverted construction";
    }
  }

  // Test adding and removing elements, making sure we cannot re-insert or
  // re-remove the same element
  {
    Point lower(-10, -10, -10);
    Point upper(11, 11, 11);
    Point binSize(5, 5, 5);
    SpatialQuery q(lower, upper, binSize);

    iotbx::pdb::hierarchy::atom a(lower, lower);
    if (!q.add(a)) {
      return "molprobity::probe::SpatialQuery::test(): Could not add atom";
    }
    if (q.add(a)) {
      return "molprobity::probe::SpatialQuery::test(): Could double add atom";
    }
    if (!q.remove(a)) {
      return "molprobity::probe::SpatialQuery::test(): Could not remove atom";
    }
    if (q.remove(a)) {
      return "molprobity::probe::SpatialQuery::test(): Could double remove atom";
    }
  }

  // Test checking for neighbors in the case where they are in our bin.
  {
    Point lower(-10, -10, -10);
    Point upper(11, 11, 11);
    Point binSize(5, 5, 5);
    SpatialQuery q(lower, upper, binSize);
    atom_less lessThan; ///< Comparison object to see if one atom is less than another

    iotbx::pdb::hierarchy::atom a(lower, lower), b(upper, upper);
    q.add(a);
    q.add(b);

    scitbx::af::shared<iotbx::pdb::hierarchy::atom> n1 = q.neighbors(lower, 0, 1);
    if ((n1.size() != 1) || (lessThan(n1[0],a)) || (lessThan(a, n1[0]))) {
      return "molprobity::probe::SpatialQuery::test(): Did not find expected lower neighbor";
    }

    scitbx::af::shared<iotbx::pdb::hierarchy::atom> n2 = q.neighbors(upper, 0, 1);
    if ((n2.size() != 1) || (lessThan(n2[0], b)) || (lessThan(b, n2[0]))) {
      return "molprobity::probe::SpatialQuery::test(): Did not find expected upper neighbor";
    }

    // Check for the case where we are right on top of them to be sure they are excluded if
    // our minimum distance is greater than 0.
    scitbx::af::shared<iotbx::pdb::hierarchy::atom> n3 = q.neighbors(lower, 0.1, 1);
    if (n3.size() != 0) {
      return "molprobity::probe::SpatialQuery::test(): Found lower neighbor when not expected";
    }
  }

  // Test the hierarchy-based constructor, which will be used for later tests as well.
  {
    // There is only one atom group/alternate conformation here, so we can just grab its atoms
    SpatialQuery q(m.chains()[0].atoms());
    if (q.m_lowerBounds != Point(0, 0, 0)) {
      return "molprobity::probe::SpatialQuery::test(): Unexpected lower bound on model-based construction";
    }
    size_t expectedSize = static_cast<size_t>((numAtoms - 1) * spacing / DEFAULT_BIN_SIZE);
    boost::array<size_t, 3> expected = { { expectedSize, expectedSize, expectedSize } };
    if (q.m_gridSize != expected) {
      return "molprobity::probe::SpatialQuery::test(): Unexpected grid size on model-based construction";
    }

    // Test checking for neighbors when they are in all bins.
    // Test checking for neighbors when the point is inside the grid and when it is
    // outside of the grid on any side.
    for (int x = -90; x <= 110; x += 100) {
      for (int y = -90; y <= 110; y += 100) {
        for (int z = -90; z <= 110; z += 100) {
          Point p(x, y, z);
          scitbx::af::shared<iotbx::pdb::hierarchy::atom> n = q.neighbors(p, 0, 2000);
          if (n.size() != numAtoms * numAtoms * numAtoms) {
            return std::string("molprobity::probe::SpatialQuery::test(): Wrong number of neighbors in full-grid test at ")
              + boost::lexical_cast<std::string>(x) + ", " + boost::lexical_cast<std::string>(y) + ", "
              + boost::lexical_cast<std::string>(z)
              + " (found " + boost::lexical_cast<std::string>(n.size()) + ", expected "
              + boost::lexical_cast<std::string>(numAtoms * numAtoms * numAtoms) + ")";
          }
        }
      }
    }

    // Test checking for neighbors when they are in multiple bins.  Check at one corner at slightly over the
    // requested spacing so we should get us and our three neighbors.
    {
      Point p(0, 0, 0);
      scitbx::af::shared<iotbx::pdb::hierarchy::atom> n = q.neighbors(p, 0, spacing+0.1);
      if (n.size() != 4) {
        return "molprobity::probe::SpatialQuery::test(): Wrong number of neighbors in multi-element test";
      }
    }
  }

  // All tests passed.
  return "";
}

std::string SpatialQuery_test()
{
  std::string ret;

  /// Test SpatialQuery class
  ret = SpatialQuery::test();
  if (!ret.empty()) {
    return std::string("molprobity::probe::SpatialQuery_test(): failed: ") + ret;
  }

  // All tests passed.
  return "";
}

} // end namespace probe
} // end namespace molprobity


text:
#pragma once
#ifndef IDOCK_ATOM_HPP
#define IDOCK_ATOM_HPP

#include <array>
#include <boost/filesystem/fstream.hpp>
using namespace std;

//! Represents an atom of either receptor or ligand.
class atom
{
private:
	static const size_t n = 31; //!< Number of AutoDock4 atom types.
	static const array<string, n> ad_strings; //!< AutoDock4 atom type strings, e.g. H, HD, C, A.
	static const array<float, n> ad_covalent_radii; //!< Covalent radii of AutoDock4 atom types.
	static const array<size_t, n> ad_to_xs; //!< AutoDock4 to XScore atom type conversion.
	static const array<size_t, n> ad_to_rf; //!< AutoDock4 to RF-Score atom type conversion.
public:
	size_t serial; //!< Atom serial.
	string name; //!< Atom name, 4 characters wide.
	array<float, 3> coord; //!< Coordinate.
	size_t ad; //!< AutoDock4 atom type.
	size_t xs; //!< XScore atom type.
	size_t rf; //!< RF-Score atom type.
	vector<atom> hydrogens; //!< Hydrogens connected to the current atom.

	//! Constructs an atom from an ATOM/HETATM line in PDBQT format.
	explicit atom(const string& line);

	//! Returns true if the AutoDock4 atom type is not supported.
	bool ad_unsupported() const;

	//! Returns true if the XScore atom type is not supported.
	bool xs_unsupported() const;

	//! Returns true if the RF-Score atom type is not supported.
	bool rf_unsupported() const;

	//! Returns true if the atom is a nonpolar hydrogen atom, which is connected to a carbon atom.
	bool is_nonpolar_hydrogen() const;

	//! Returns true if the atom is a polar hydrogen atom, which is connected to a heavy atom other than carbon.
	bool is_polar_hydrogen() const;

	//! Returns true if the atom is a hydrogen atom, either polar or nonpolar.
	bool is_hydrogen() const;

	//! Returns true if the atom is a hetero atom, i.e. non-carbon heavy atom.
	bool is_hetero() const;

	//! Revises the XScore atom type of a nitrogen or an oxygen to its corresponding variant of hydrogen bond donor.
	void donorize();

	//! Revises the XScore atom type of a carbon to its corresponding non-hydrophobic variant.
	void dehydrophobicize();

	//! Returns the covalent radius of the current AutoDock4 atom type.
	float covalent_radius() const;

	//! Returns true if the current atom is covalently bonded to a given atom, i.e. their distance is within a certain threshold which depends on their covalent radii.
	bool has_covalent_bond(const atom& a) const;

	//! Outputs an ATOM line in PDBQT format.
	void output(boost::filesystem::ofstream& ofs, const array<float, 3>& coord) const;
};

#endif

ext: hpp


text:
#include <boost/python.hpp>
#include <boost/cstdint.hpp>

#include <Magick++/Include.h>

using namespace boost::python;

void __StorageType()
{
    enum_< Magick::StorageType >("StorageType")
        .value("CharPixel", Magick::CharPixel)
        .value("ShortPixel", Magick::ShortPixel)
        .value("IntegerPixel", Magick::IntegerPixel)
        .value("FloatPixel", Magick::FloatPixel)
        .value("DoublePixel", Magick::DoublePixel)
    ;
}

ext: cpp

max_stars_repo_name: veryhappythings/pgmagick

max_issues_repo_name: veryhappythings/pgmagick

max_forks_repo_name: veryhappythings/pgmagick


text:
//https://github.com/influxdb/influxdb/pull/3125

//servers.localhost.cpu.loadavg.10
//Template:.host.resource.measurement*
//Output : measurement = loadavg.10 tags = host = localhost resource = cpu

#include <fstream>
#include <assert.h>
#include <boost/make_shared.hpp>
#include <iostream>
#include <iomanip>
#include <string>
#include <assert.h>
#include <thread>
#include <chrono>
#include <sstream>
#include <array>
#include <stdexcept>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/asio.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/bind.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/program_options.hpp>
#include <boost/endian/arithmetic.hpp>
#include <boost/uuid/string_generator.hpp>
#include <boost/chrono/thread_clock.hpp>
#include <boost/tokenizer.hpp>
#include <boost/timer/timer.hpp>
#include <csi_http_client/http_client.h>
#include <csi_kafka/highlevel_consumer.h>
#include <csi_kafka/consumer_coordinator.h>
#include <csi_kafka/internal/utility.h>

#define CONSUMER_GROUP "kafka2influx"

//measurement[, tag_key1 = tag_value1...] field_key = field_value[, field_key2 = field_value2][timestamp]
//
//For example :
//
//measurement, tkey1 = tval1, tkey2 = tval2 fkey = fval, fkey2 = fval2 1234567890000000000

//https://influxdb.com/docs/v0.9/write_protocols/write_syntax.html

size_t _remaining_records = 0;

static boost::uuids::string_generator uuid_from_string;

typedef boost::tokenizer<boost::char_separator<char> > tokenizer;

struct tag
{
    enum tag_t { TAG, MEASUREMENT, EMPTY };

    tag(std::string s) : wildcard(false), type(EMPTY)
    {
        if (s == "measurement")
        {
            type = MEASUREMENT;
        }
        else if (s == "measurement*")
        {
            type = MEASUREMENT;
            wildcard = true;
        }
        else if (s.size())
        {
            type = TAG;
            name = s;
        }
    }

    std::string name;
    tag_t       type;
    bool        wildcard;
};

struct tag2
{
    std::string name;
    int         index;
};

std::vector<tag> parse_template(const std::string& s)
{
    std::vector<tag> v;
    boost::char_separator<char> sep(".", 0, boost::keep_empty_tokens);
    {
        tokenizer tok(s, sep);
        for (tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
            v.emplace_back(*beg);
        }
    }
    return v;
}

std::vector<tag2> ordered_tags(const std::vector<tag>& v)
{
    std::vector<std::string> s;
    for (std::vector<tag>::const_iterator i = v.begin(); i != v.end(); ++i)
    if (i->type == tag::TAG)
        s.push_back(i->name);
    std::sort(s.begin(), s.end());

    std::vector<tag2> result;
    for (std::vector<std::string>::const_iterator i = s.begin(); i != s.end(); ++i)
    {
        int index = 0;
        for (std::vector<tag>::const_iterator j = v.begin(); j != v.end(); ++j, ++index)
        {
            if (*i == j->name)
            {
                tag2 x;
                x.index = index;
                x.name = *i;
                result.push_back(x);
                continue;
            }
        }
    }
    return result;
}

int measurement_index(const std::vector<tag>& v)
{
    int index = 0;
    for (std::vector<tag>::const_iterator i = v.begin(); i != v.end(); ++i, ++index)
    {
        if (i->type == tag::MEASUREMENT)
            return index;
    }
    return -1;
}

std::vector<std::string> parse_metric(const std::string& s)
{
    std::vector<std::string> v;
    boost::char_separator<char> sep(".", 0, boost::keep_empty_tokens);
    {
        tokenizer tok(s, sep);
        for (tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
            v.emplace_back(*beg);
        }
    }
    return v;
}

//this will parse a line in 3 parts metric_name value timestamp
std::vector<std::string> parse_graphite(const std::string& s)
{
    std::vector<std::string> v;
    boost::char_separator<char> sep(" ", 0, boost::keep_empty_tokens);
    {
        tokenizer tok(s, sep);
        for (tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
            v.emplace_back(*beg);
        }
    }
    return v;
}

std::string build_message(const std::vector<tag2>& tags, int message_index, bool wildcard, const std::string s)
{
    std::string message;
    auto parts = parse_graphite(s);
    if (parts.size() != 3)
    {
        std::string what = std::string("parse error - bad graphite format: ") + s;
        throw std::invalid_argument(what.c_str());
    }

    std::vector<std::string> tokens = parse_metric(parts[0]);
    if (tokens.size() < message_index)
    {
        std::string what = std::string("parse error - to few tags: ") + s;
        throw std::invalid_argument(what.c_str());
    }

    //measurement name
    if (wildcard)
    {
        for (std::vector<std::string>::const_iterator i = tokens.begin() + message_index; i != tokens.end(); ++i)
        {
            message += *i;
            if (i != tokens.end() - 1)
            {
                message += ".";
            }
        }
    }
    else
    {
        message += tokens[message_index];
    }

    for (std::vector<tag2>::const_iterator i = tags.begin(); i != tags.end(); ++i)
    {
        message += "," + i->name + "=" + tokens[i->index];
    }

    //add value and time (in sec) and linefeed
    message += " value=" + parts[1] + " " + parts[2] +"\n";
    return message;
}

// --topic collectd.graphite --broker 10.1.47.4 --influxdb 10.1.47.16:8086 --database collectd --template "hostgroup.host...resource.measurement*"

int main(int argc, char** argv)
{
    boost::log::trivial::severity_level log_level;
    //boost::log::add_console_log(std::cerr, boost::log::keywords::format = ">> %Message%", auto_flush=true);
    boost::log::add_console_log(std::cout, boost::log::keywords::auto_flush = true);
    boost::program_options::options_description desc("options");
    desc.add_options()
        ("help", "produce help message")
        ("topic", boost::program_options::value<std::string>(), "topic")
        ("broker", boost::program_options::value<std::string>(), "broker")
        ("consumer_group", boost::program_options::value<std::string>()->default_value(CONSUMER_GROUP), "consumer_group")
        ("template", boost::program_options::value<std::string>(), "template")
        ("influxdb", boost::program_options::value<std::string>()->default_value("localhost:8086"), "influxdb")
		("database", boost::program_options::value<std::string>(), "database")
        ("batch_size", boost::program_options::value<int>()->default_value(200), "batch_size")
        ("reset_offset", boost::program_options::value<bool>()->default_value(false), "reset_offset")
        ("log_level", boost::program_options::value<boost::log::trivial::severity_level>(&log_level)->default_value(boost::log::trivial::info), "log level to output");
        ;

    boost::program_options::variables_map vm;
    boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
    boost::program_options::notify(vm);

    boost::log::core::get()->set_filter(boost::log::trivial::severity >= log_level);
    BOOST_LOG_TRIVIAL(info) << "loglevel " << log_level;

    if (vm.count("help"))
    {
        std::cout << desc << std::endl;
        return 0;
    }

    std::string topic;
    if (vm.count("topic"))
    {
        topic = vm["topic"].as<std::string>();
    }
    else
    {
        std::cout << "--topic must be specified" << std::endl;
        return 0;
    }

    int32_t kafka_port = 9092;
    std::vector<csi::kafka::broker_address> kafka_brokers;
    if (vm.count("broker"))
    {
        std::string s = vm["broker"].as<std::string>();
        size_t last_colon = s.find_last_of(':');
        if (last_colon != std::string::npos)
            kafka_port = atoi(s.substr(last_colon + 1).c_str());
        s = s.substr(0, last_colon);

        // now find the brokers...
        size_t last_separator = s.find_last_of(',');
        while (last_separator != std::string::npos)
        {
            std::string host = s.substr(last_separator + 1);
            kafka_brokers.push_back(csi::kafka::broker_address(host, kafka_port));
            s = s.substr(0, last_separator);
            last_separator = s.find_last_of(',');
        }
        kafka_brokers.push_back(csi::kafka::broker_address(s, kafka_port));
    }
    else
    {
        std::cout << "--broker must be specified" << std::endl;
        return 0;
    }

    std::string consumer_group;
    if (vm.count("consumer_group"))
    {
        consumer_group = vm["consumer_group"].as<std::string>();
    }
    else
    {
        std::cout << "--consumer_group must be specified" << std::endl;
        return 0;
    }

    bool reset_offset = false;
    if (vm.count("reset_offset"))
    {
        reset_offset = vm["reset_offset"].as<bool>();
    }

	std::string influxuri;
	if (vm.count("influxdb"))
	{
		influxuri = vm["influxdb"].as<std::string>();
	}
	else
	{
		std::cout << "--influxdb must be specified" << std::endl;
		return 0;
	}

	std::string database;
	if (vm.count("database"))
	{
		database = vm["database"].as<std::string>();
	}
	else
	{
		std::cout << "--database must be specified" << std::endl;
		return 0;
	}

    std::vector<tag> tags;
    if (vm.count("template"))
    {
        std::string templ = vm["template"].as<std::string>();
        tags = parse_template(templ);
    }
    else
    {
        std::cout << "--template must be specified" << std::endl;
        return 0;
    }

    int batch_size;
    if (vm.count("batch_size"))
    {
        batch_size = vm["batch_size"].as<int>();
    }
    else
    {
        std::cout << "--batch_size must be specified" << std::endl;
        return 0;
    }

	auto ot = ordered_tags(tags);
	int  mi = measurement_index(tags);
	bool wildcard = true;

    std::string kafka_broker_str = "";
    for (std::vector<csi::kafka::broker_address>::const_iterator i = kafka_brokers.begin(); i != kafka_brokers.end(); ++i)
    {
        kafka_broker_str += i->host_name + ":" + std::to_string(i->port);
        if (i != kafka_brokers.end() - 1)
            kafka_broker_str += ", ";
    }

    BOOST_LOG_TRIVIAL(info) << "kafka broker(s)   : " << kafka_broker_str;
    BOOST_LOG_TRIVIAL(info) << "topic             : " << topic;
    BOOST_LOG_TRIVIAL(info) << "consumer_group    : " << consumer_group;
    BOOST_LOG_TRIVIAL(info) << "reset_offset      : " << reset_offset;
    BOOST_LOG_TRIVIAL(info) << "template          : " << vm["template"].as<std::string>();
    BOOST_LOG_TRIVIAL(info) << "measurement index : " << mi;
    BOOST_LOG_TRIVIAL(info) << "influxdb          : " << influxuri;
    BOOST_LOG_TRIVIAL(info) << "database          : " << database;
    BOOST_LOG_TRIVIAL(info) << "batch_size        : " << batch_size;

    boost::asio::io_service ios;
    std::auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ios));
    boost::thread bt(boost::bind(&boost::asio::io_service::run, &ios));

    try
    {
        csi::kafka::consumer_coordinator coordinator(ios, consumer_group);
        auto coordinator_connect_res = coordinator.connect(kafka_brokers, 1000);
        if (coordinator_connect_res)
        {
            std::cerr << coordinator_connect_res.message() << std::endl;
            return -1;
        }

        auto offset_res = coordinator.get_consumer_offset(topic);
        if (offset_res)
        {
            std::cerr << to_string(offset_res.ec) << std::endl;
            return -1;
        }

        int32_t ec;
        auto offsets = parse(offset_res, topic, ec);
        if (ec)
        {
            std::cerr << to_string((csi::kafka::error_codes) ec) << std::endl;
            return -1;
        }

        if (reset_offset)
        {
            for (std::vector<csi::kafka::topic_offset>::iterator i = offsets.begin(); i != offsets.end(); ++i)
                i->offset = csi::kafka::earliest_available_offset;
            auto res = coordinator.commit_consumer_offset(-1, "kafka2influx", topic, offsets, "reset_offset_command");
            if (res)
            {
                std::cerr << to_string(res.ec) << std::endl;
                return -1;
            }
        }

        csi::kafka::highlevel_consumer consumer(ios, topic, 1000, 100000);
        consumer.connect(kafka_brokers);
        consumer.connect_forever(kafka_brokers);
        consumer.set_offset(offsets); // start where we left...

        std::map<int32_t, int64_t> cursor;

        csi::http_client http_handler(ios);

        size_t metrics_counter = 0;

        boost::chrono::system_clock::time_point last = boost::chrono::system_clock::now();
        boost::chrono::milliseconds sixty_seconds(60 * 1000);

        while (true)
        {
            std::vector<std::string> to_send;

            auto r = consumer.fetch();
            for (std::vector<csi::kafka::rpc_result<csi::kafka::fetch_response>>::const_iterator i = r.begin(); i != r.end(); ++i)
            {
                if (i->ec)
                {
                    BOOST_LOG_TRIVIAL(fatal) << "fetch failed - exiting fast";
                    return 1; // test with quick death
                    //continue; // or die??
                }
                for (std::vector<csi::kafka::fetch_response::topic_data>::const_iterator j = (*i)->topics.begin(); j != (*i)->topics.end(); ++j)
                {
                    for (std::vector<std::shared_ptr<csi::kafka::fetch_response::topic_data::partition_data>>::const_iterator k = j->partitions.begin(); k != j->partitions.end(); ++k)
                    {
                        if ((*k)->error_code)
                        {
                            BOOST_LOG_TRIVIAL(fatal) << "fetch failed, partition: " << (*k)->partition_id << ", exiting fast";
                            return 1; // test with quick death
                            continue; // or die??
                        }
                        for (std::vector<std::shared_ptr<csi::kafka::basic_message>>::const_iterator m = (*k)->messages.begin(); m != (*k)->messages.end(); ++m)
                        {
                            if ((*m)->value.is_null())
                                continue;
                            std::string line((const char*)(*m)->value.data(), (*m)->value.size());

                            // might be several messages in a line
                            boost::char_separator<char> sep("\n\r");
                            {
                                tokenizer tok(line, sep);
                                for (tokenizer::iterator n = tok.begin(); n != tok.end(); ++n)
                                {
                                    try
                                    {
                                        auto s = build_message(ot, mi, wildcard, *n);
                                        if (s.size())
                                        {
                                            to_send.push_back(std::move(s));
                                        }
                                        else
                                        {
                                            assert(false); // should never get here
                                            BOOST_LOG_TRIVIAL(error) << "could not parse: " << *n;
                                        }
                                    }
                                    catch (std::exception& e)
                                    {
                                        BOOST_LOG_TRIVIAL(error) << e.what();
                                    }
                                }
                            }
                            cursor[(*m)->partition] = (*m)->offset;
                        }
                    }
                }
            }

			//time to send or more that 1000 msgs
			std::string uri = influxuri + "/write?db=" + database + "&precision=s"; // TBD create it if it does not exist
			while (to_send.size())
			{
				auto request = csi::create_http_request(csi::http::POST, uri, {}, std::chrono::milliseconds(60000));
				avro::StreamWriter writer(request->tx_content());

                size_t items_to_send = std::min<size_t>(to_send.size(), batch_size);
				std::vector<std::string>::const_iterator cursor = to_send.begin();
				for (size_t i = 0; i != items_to_send; ++i, ++cursor)
					writer.writeBytes((const uint8_t*)cursor->data(), cursor->size());
				writer.flush();

                size_t max_no_of_retries = 60; // ~10min
                size_t no_of_retries = 0;

                while (true) // until we managed to send
                {
                    auto result = http_handler.perform(request);
                    if (result->ok())
                    {
                        //std::cerr << "influx insert ok, items: " << items_to_send << ", time: " << result->milliseconds() << " ms" << std::endl;
                        metrics_counter += items_to_send;
                        to_send.erase(to_send.begin(), to_send.begin() + items_to_send);
                        //time to commit kafka cursor? every sec?
                        //commit cursors....
                        //boost::this_thread::sleep(boost::posix_time::milliseconds(200));
                        break; // this is good
                    }

                    if (!result->transport_result())
                    {
                        BOOST_LOG_TRIVIAL(warning) << "transport failed, " << " retry_count: " << no_of_retries;
                        boost::this_thread::sleep(boost::posix_time::milliseconds(10000));
                        no_of_retries++;

                    }
                    else
                    {
                        BOOST_LOG_TRIVIAL(error) << "http post: " << result->uri() << " result: " << result->http_result() << " (" << to_string(result->http_result()) << "), " << " retry_count: " << no_of_retries;
                        boost::this_thread::sleep(boost::posix_time::milliseconds(10000));
                        no_of_retries++;
                    }

                    //max no of retries and die??
                    if (no_of_retries > max_no_of_retries)
                    {
                        BOOST_LOG_TRIVIAL(error) << "no_of_retries > max_no_of_retries - exiting....";
                        boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
                        return 1;
                    }
                }
			}

            boost::chrono::system_clock::time_point now = boost::chrono::system_clock::now();
            boost::chrono::milliseconds elapsed = boost::chrono::duration_cast<boost::chrono::milliseconds>(now - last);
            if (elapsed>sixty_seconds)
            {
                BOOST_LOG_TRIVIAL(info) << "nr of metrics: " << metrics_counter;
                metrics_counter = 0;
                last = now;

                auto res = coordinator.commit_consumer_offset(-1, "kafka2influx", topic, cursor, "commit");

            }

			/*
            this is not working because of kafka lib
			auto res4 = client.commit_consumer_offset(CONSUMER_GROUP, 1, CONSUMER_ID, TOPIC_NAME, 0, 22, "nisse", 44);
			if (res4)
			{
				std::cerr << to_string(res4.ec) << std::endl;
				return -1;
			}
			*/
        }
    }
    catch (std::exception& e)
    {
        BOOST_LOG_TRIVIAL(error) << "exception: " << e.what() << " : exiting";
    }
    ios.stop();
    return 0;
}


text:
/*
 * Copyright (c) 2011-2021, The DART development contributors
 * All rights reserved.
 *
 * The list of contributors can be found at:
 *   https://github.com/dartsim/dart/blob/master/LICENSE
 *
 * This file is provided under the following "BSD-style" License:
 *   Redistribution and use in source and binary forms, with or
 *   without modification, are permitted provided that the following
 *   conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *   POSSIBILITY OF SUCH DAMAGE.
 */

// For problem
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>

#include <Eigen/Dense>
#include <gtest/gtest.h>

#include "dart/common/Console.hpp"
#include "dart/config.hpp"
#include "dart/dynamics/FreeJoint.hpp"
#include "dart/dynamics/InverseKinematics.hpp"
#include "dart/dynamics/Skeleton.hpp"
#include "dart/optimizer/Function.hpp"
#include "dart/optimizer/GradientDescentSolver.hpp"
#include "dart/optimizer/Problem.hpp"

#include "TestHelpers.hpp"
#if HAVE_NLOPT
  #include "dart/optimizer/nlopt/NloptSolver.hpp"
#endif
#if HAVE_IPOPT
  #include "dart/optimizer/ipopt/IpoptSolver.hpp"
#endif
#if HAVE_SNOPT
  #include "dart/optimizer/snopt/SnoptSolver.hpp"
#endif

using namespace std;
using namespace Eigen;
using namespace dart::optimizer;
using namespace dart::dynamics;

//==============================================================================
/// \brief class SampleObjFunc
class SampleObjFunc : public Function
{
public:
  /// \brief Constructor
  SampleObjFunc() : Function() {}

  /// \brief Destructor
  virtual ~SampleObjFunc() {}

  /// \copydoc Function::eval
  double eval(const Eigen::VectorXd& _x) override
  {
    return std::sqrt(_x[1]);
  }

  /// \copydoc Function::evalGradient
  void evalGradient(
      const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad) override
  {
    _grad[0] = 0.0;
    _grad[1] = 0.5 / std::sqrt(_x[1]);
  }
};

//==============================================================================
class SampleConstFunc : public Function
{
public:
  /// \brief Constructor
  SampleConstFunc(double _a, double _b) : Function(), mA(_a), mB(_b) {}

  /// \brief Destructor
  virtual ~SampleConstFunc() {}

  /// \copydoc Function::eval
  double eval(const Eigen::VectorXd& _x) override
  {
    return ((mA * _x[0] + mB) * (mA * _x[0] + mB) * (mA * _x[0] + mB) - _x[1]);
  }

  /// \copydoc Function::evalGradient
  void evalGradient(
      const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad) override
  {
    _grad[0] = 3 * mA * (mA * _x[0] + mB) * (mA * _x[0] + mB);
    _grad[1] = -1.0;
  }

private:
  /// \brief Data
  double mA;

  /// \brief Data
  double mB;
};

//==============================================================================
TEST(Optimizer, GradientDescent)
{
  std::shared_ptr<Problem> prob = std::make_shared<Problem>(2);

  prob->setLowerBounds(Eigen::Vector2d(-HUGE_VAL, 0));
  prob->setInitialGuess(Eigen::Vector2d(1.234, 5.678));

  FunctionPtr obj = std::make_shared<SampleObjFunc>();
  prob->setObjective(obj);

  GradientDescentSolver solver(prob);
  EXPECT_TRUE(solver.solve());

  double minF = prob->getOptimumValue();
  Eigen::VectorXd optX = prob->getOptimalSolution();

  EXPECT_NEAR(minF, 0, 1e-6);
  EXPECT_EQ(optX.size(), static_cast<int>(prob->getDimension()));
  EXPECT_NEAR(optX[0], 1.234, 0.0);
  EXPECT_NEAR(optX[1], 0.0, solver.getTolerance());
}

//==============================================================================
#if HAVE_NLOPT
TEST(Optimizer, BasicNlopt)
{
  // Problem reference: http://ab-initio.mit.edu/wiki/index.php/NLopt_Tutorial

  std::shared_ptr<Problem> prob = std::make_shared<Problem>(2);

  prob->setLowerBounds(Eigen::Vector2d(-HUGE_VAL, 0));
  prob->setInitialGuess(Eigen::Vector2d(1.234, 5.678));

  FunctionPtr obj = std::make_shared<SampleObjFunc>();
  prob->setObjective(obj);

  FunctionPtr const1 = std::make_shared<SampleConstFunc>(2, 0);
  FunctionPtr const2 = std::make_shared<SampleConstFunc>(-1, 1);
  prob->addIneqConstraint(const1);
  prob->addIneqConstraint(const2);

  NloptSolver solver(prob, NloptSolver::LD_MMA);
  EXPECT_TRUE(solver.solve());

  double minF = prob->getOptimumValue();
  Eigen::VectorXd optX = prob->getOptimalSolution();

  EXPECT_NEAR(minF, 0.544330847, 1e-6);
  EXPECT_EQ(static_cast<std::size_t>(optX.size()), prob->getDimension());
  EXPECT_NEAR(optX[0], 0.333334, 1e-6);
  EXPECT_NEAR(optX[1], 0.296296, 1e-6);
}
#endif

//==============================================================================
#if HAVE_IPOPT
TEST(Optimizer, BasicIpopt)
{
  std::shared_ptr<Problem> prob = std::make_shared<Problem>(2);

  prob->setLowerBounds(Eigen::Vector2d(-HUGE_VAL, 0));
  prob->setInitialGuess(Eigen::Vector2d(1.234, 5.678));

  FunctionPtr obj = std::make_shared<SampleObjFunc>();
  prob->setObjective(obj);

  FunctionPtr const1 = std::make_shared<SampleConstFunc>(2, 0);
  FunctionPtr const2 = std::make_shared<SampleConstFunc>(-1, 1);
  prob->addIneqConstraint(const1);
  prob->addIneqConstraint(const2);

  IpoptSolver solver(prob);
  solver.solve();

  double minF = prob->getOptimumValue();
  Eigen::VectorXd optX = prob->getOptimalSolution();

  EXPECT_NEAR(minF, 0.544330847, 1e-6);
  EXPECT_EQ(static_cast<std::size_t>(optX.size()), prob->getDimension());
  EXPECT_NEAR(optX[0], 0.333334, 1e-6);
  EXPECT_NEAR(optX[1], 0.296296, 1e-6);
}
#endif

//==============================================================================
#if HAVE_SNOPT
TEST(Optimizer, BasicSnopt)
{
  dterr << "SNOPT is not implemented yet.\n";
  return;
}
#endif

//==============================================================================
bool compareStringAndFile(
    const std::string& content, const std::string& fileName)
{
  std::ifstream ifs(fileName, std::ifstream::in);
  EXPECT_TRUE(ifs.is_open());

  auto itr = content.begin();

  char c = ifs.get();
  while (ifs.good())
  {
    if (*itr != c)
      return false;

    c = ifs.get();
    itr++;
  }

  ifs.close();

  return true;
}

//==============================================================================
TEST(Optimizer, OutStream)
{
  std::shared_ptr<Problem> prob = std::make_shared<Problem>(2);

  prob->setLowerBounds(Eigen::Vector2d(-HUGE_VAL, 0));
  prob->setInitialGuess(Eigen::Vector2d(1.234, 5.678));

  FunctionPtr obj = std::make_shared<SampleObjFunc>();
  prob->setObjective(obj);

  GradientDescentSolver solver(prob);
  solver.setIterationsPerPrint(50);

  // Print the progess to a std::string
  std::stringstream ss;
  solver.setOutStream(&ss);
  EXPECT_TRUE(solver.solve());
  std::string outputString = ss.str();

  // Print the progress to a file
  std::string outputFile = "test_optimizer_outstream.txt";
  std::ofstream ofs(outputFile);
  EXPECT_TRUE(ofs.is_open());
  solver.setOutStream(&ofs);
  EXPECT_TRUE(solver.solve());
  ofs.close();

  // Compare them
  EXPECT_TRUE(compareStringAndFile(outputString, outputFile));

  std::remove(outputFile.c_str());
}

max_stars_repo_name: dartsim/dart

max_issues_repo_name: dartsim/dart

max_forks_repo_name: dartsim/dart


text:
/*
 * Copyright (c) 2020 Karim, karimdavoodi@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <boost/filesystem/operations.hpp>
#include <chrono>
#include <ctime>
#include <iostream>
#include <vector>
#include <thread>
#include <boost/format.hpp>
#include "utils.hpp"

#define SNAPSHOT_PERIOD (60*10)

using namespace std;

bool gst_capture_udp_in_jpg(string in_multicast, int port, string pic_path);
void start_snapshot(const json& channel, live_setting live_config);

/*
 *   The main()
 *      - check license
 *      - read channels from mongoDB
 *      - start thread for each active channel
 *      - wait to join
 * */
int main()
{
    Mongo db;
    vector<thread> pool;
    live_setting live_config;
    CHECK_LICENSE;
    Util::init(db);
    if(!Util::get_live_config(db, live_config, "dvb")){
        LOG(error) << "Error in live config! Exit.";
        return -1;
    }
    Util::check_path(string(MEDIA_ROOT) + "Snapshot");

    json channels_net = json::parse(db.find_mony("live_output_network",
                "{\"active\":true}"));
    json channels_dvb = json::parse(db.find_mony("live_output_dvb",
                "{\"active\":true}"));
    for(auto& chan : channels_net ){
        chan["name"] = Util::get_channel_name(chan["input"], chan["inputType"]);
    }
    for(auto& chan : channels_dvb ){
        chan["name"] = Util::get_channel_name(chan["input"], chan["inputType"]);
    }
    while(true){
        auto start = time(nullptr);
        for(auto& chan : channels_net ){
            start_snapshot(chan, live_config);
        }
        for(auto& chan : channels_dvb ){
            start_snapshot(chan, live_config);
        }
        auto duration = time(nullptr) - start;
        if(duration < SNAPSHOT_PERIOD){
            LOG(info) << "Wait for next snapshot for " << SNAPSHOT_PERIOD - duration;
            Util::wait((SNAPSHOT_PERIOD - duration) * 1000);
        }
    }
    Util::wait_forever();
}
/*
 *  The channel thread function
 *
 *  @param channel : config of channel
 *  @param live_config : general live streamer config
 *
 * */
void start_snapshot(const json& channel, live_setting live_config)
{
    Mongo db;
    auto now = time(nullptr);
    auto now_tm = localtime(&now);
    live_config.type_id = channel["inputType"].get<int>();
    auto in_multicast = Util::get_multicast(live_config, channel["input"]);
    long channel_id = channel["_id"].get<long>();
    auto pic_path = boost::format("%sSnapshot/%ld.jpg")
        % MEDIA_ROOT % channel_id;

    if(boost::filesystem::exists(pic_path.str())){
        boost::filesystem::remove(pic_path.str());
        LOG(info) << "Delete old snapshut:" << pic_path.str();
    }

    LOG(debug) << "Try to snapsot from " << channel["name"];
    bool succesfull = gst_capture_udp_in_jpg(in_multicast, INPUT_PORT, pic_path.str());

    if(succesfull){
        string name = channel["name"].get<string>();
        LOG(info) << "Capture " << name << " in " << pic_path.str();
        json media = json::object();
        media["_id"] = channel_id;
        media["name"] = name;
        media["format"] = CONTENT_FORMAT_JPG;
        media["type"] = CONTENT_TYPE_SNAPSHOT;
        media["price"] = 0;
        media["date"] = now_tm->tm_year;
        media["languages"] = json::array();
        media["platform"] = json::array();
        media["category"] = channel["category"];
        media["description"] = {
            {"en",{
                      { "name" ,name },
                      { "description" ,"" }
                  }},
            {"fa",{
                      { "name" ,name },
                      { "description" ,"" }
                  }},
            {"ar",{
                      { "name" ,name },
                      { "description" ,"" }
                  }}
        };
        db.insert_or_replace_id("storage_contents_info", channel_id, media.dump());
    }
    json report = json::object();
    report["_id"] = std::chrono::system_clock::now().time_since_epoch().count();
    report["time"] = time(nullptr);
    report["systemId"] = Util::get_systemId(db);
    report["inputId"] = channel["input"];
    report["inputType"] = channel["inputType"];
    report["status"] = succesfull ? 100 : 0;
    report["snapshot"] = succesfull ?  channel_id : 0;
    db.insert("report_channels", report.dump());
    // Update report_output_channels
    json report_output_channel = json::parse(db.find_id("report_output_channels",channel_id));
    if(!report_output_channel["name"].is_null()){
        report_output_channel["inputStatus"] =  report["status"];
        report_output_channel["inputSnapshot"] =  report["snapshot"];
        db.insert_or_replace_id("report_output_channels", channel_id,
                report_output_channel.dump());
    }
    Util::wait(1000);
}


text:
// Copyright (c) 2020 Patrick Hart, Julian Bernhard,
// Klemens Esterle, Tobias Kessler
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

#ifndef BARK_ML_COMMONS_COMMONS_HPP_
#define BARK_ML_COMMONS_COMMONS_HPP_

#include <memory>
#include <tuple>
#include <vector>
#include <Eigen/Dense>

namespace bark_ml {
namespace commons {

template<typename T>
inline const T Norm(const T& val, const T& min_val, const T& max_val) {
  return (val - min_val) / (max_val-min_val);
}

}  // namespace commons
}  // namespace bark_ml

#endif  // BARK_ML_COMMONS_COMMONS_HPP_

max_stars_repo_name: mansoorcheema/bark-ml


text:
/**
 * $Id$
 *
 * Copyright (C)
 * 2014 - $Date$
 *     Martin Wolf <boostnumpy@martin-wolf.org>
 *
 * @file    boost/numpy/dstream/detail/callable.hpp
 * @version $Revision$
 * @date    $Date$
 * @author  Martin Wolf <boostnumpy@martin-wolf.org>
 *
 * @brief This file defines the callable template that is the actual
 *        implementation of the iteration procedure.
 *
 *        This file is 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).
 */
#if !defined(BOOST_PP_IS_ITERATING)

#ifndef BOOST_NUMPY_DSTREAM_DETAIL_CALLABLE_HPP_INCLUDED
#define BOOST_NUMPY_DSTREAM_DETAIL_CALLABLE_HPP_INCLUDED

#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/preprocessor/facilities/intercept.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>

#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/integral_c.hpp>

#include <boost/python/args.hpp>
#include <boost/python/object.hpp>

#include <boost/numpy/limits.hpp>
#include <boost/numpy/mpl/types_from_fctptr_signature.hpp>
#include <boost/numpy/detail/callable_caller.hpp>
#include <boost/numpy/dstream/detail/callable_call.hpp>

namespace boost {
namespace numpy {
namespace dstream {
namespace detail {

template <unsigned n_kwargs, unsigned in_arity>
struct is_correct_number_of_kwargs
{
    typedef typename boost::mpl::equal_to<
                  boost::mpl::integral_c<unsigned, n_kwargs>
                , boost::mpl::integral_c<unsigned, in_arity>
            >::type
            type;
};

template <class KW>
python::detail::keywords<1 + KW::size>
make_self_plus_kwargs(KW const & kwargs)
{
    python::detail::keywords<1 + KW::size> self_plus_kw;
    self_plus_kw.elements[0].name = "self";
    std::copy(kwargs.elements, kwargs.elements+KW::size, self_plus_kw.elements+1);
    return self_plus_kw;
}

template <
      unsigned InArity
    , class FTypes
    , class MappingDefinition
    , class WiringModel
    , class ThreadAbility
>
struct callable_in_arity;

#define BOOST_PP_ITERATION_PARAMS_1 \
    (3, (1, BOOST_NUMPY_LIMIT_INPUT_ARITY, <boost/numpy/dstream/detail/callable.hpp>))
#include BOOST_PP_ITERATE()

template <
      class F
    , class FTypes
    , class MappingDefinition
    , template <
           class _MappingDefinition
         , class _FTypes
      >
      class WiringModelTemplate
    , class ThreadAbility
>
struct callable_base_select
{
    typedef typename WiringModelTemplate<MappingDefinition, FTypes>::type
            wiring_model_t;

    typedef typename callable_in_arity<
                  MappingDefinition::in::arity
                , FTypes
                , MappingDefinition
                , wiring_model_t
                , ThreadAbility
            >::template impl<
                  FTypes::is_mfp
                , MappingDefinition::maps_to_void
                , ThreadAbility::threads_allowed_t::value
                , F
            >::type
            type;
};

template <
      class F
    , class FTypes
    , class MappingDefinition
    , template <
            class _MappingDefinition
          , class _FTypes
      >
      class WiringModelTemplate
    , class ThreadAbility
>
struct callable
  : callable_base_select<F, FTypes, MappingDefinition, WiringModelTemplate, ThreadAbility>::type
{
    typedef typename callable_base_select<F, FTypes, MappingDefinition, WiringModelTemplate, ThreadAbility>::type
            base_t;

    callable(F f)
      : base_t(f)
    {}
};

}// namespace detail
}// namespace dstream
}// namespace numpy
}// namespace boost

#endif // ! BOOST_NUMPY_DSTREAM_DETAIL_CALLABLE_HPP_INCLUDED
#else

#define IN_ARITY BOOST_PP_ITERATION()

#define BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KWSIZE, FINARITY)     \
    typedef typename is_correct_number_of_kwargs<KWSIZE, FINARITY>::type       \
            is_correct_number_of_kwargs_t;                                     \
    BOOST_MPL_ASSERT_MSG(                                                      \
          is_correct_number_of_kwargs_t::value                                 \
        , THE_NUMBER_OF_KEYWORD_ARGUMENTS_MUST_MATCH_THE_NUMBER_OF_FUNCTION_ARGUMENTS \
        , (KW)                                                                 \
    );

template <class FTypes, class MappingDefinition, class WiringModel, class ThreadAbility>
struct callable_in_arity<IN_ARITY, FTypes, MappingDefinition, WiringModel, ThreadAbility>
{
    typedef numpy::detail::callable_caller<
              IN_ARITY
            , typename FTypes::class_type
            , typename FTypes::return_type
            , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, typename FTypes::arg_type)
            >
            f_caller_t;

    typedef typename callable_call<
                  FTypes
                , f_caller_t
                , MappingDefinition
                , WiringModel
                , ThreadAbility
                >::type
            callable_call_t;

    template <
          bool is_member_function
        , bool has_void_return
        , bool allows_threads
        , class F
    >
    struct impl;

    //--------------------------------------------------------------------------
    // Partial specialization for member function with void-return and threads
    // allowed.
    template <class F>
    struct impl<true, true, true, F>
    {
        typedef impl<true, true, true, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , typename FTypes::class_type &
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                , unsigned
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size+2>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return ( make_self_plus_kwargs(kwargs)
                   , python::arg("nthreads")
                   );
        }

        python::object
        operator()(
              typename FTypes::class_type & self
            , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
            , unsigned nthreads
        ) const
        {
            f_caller_t const f_caller(m_f);

            python::object out_obj;

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };

    //--------------------------------------------------------------------------
    // Partial specialization for member function with void-return and threads
    // forbidden.
    template <class F>
    struct impl<true, true, false, F>
    {
        typedef impl<true, true, false, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , typename FTypes::class_type &
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size+1>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return make_self_plus_kwargs(kwargs);
        }

        python::object
        operator()(
              typename FTypes::class_type & self
            , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
        ) const
        {
            f_caller_t const f_caller(m_f);

            python::object out_obj;
            unsigned const nthreads = 1;

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };

    //--------------------------------------------------------------------------
    // Partial specialization for member function with non-void-return and
    // threads allowed.
    template <class F>
    struct impl<true, false, true, F>
    {
        typedef impl<true, false, true, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , typename FTypes::class_type &
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                , unsigned
                , python::object &
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size+3>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return ( make_self_plus_kwargs(kwargs)
                   , python::arg("nthreads")=1
                   , python::arg("out")=python::object()
                   );
        }

        python::object
        operator()(
              typename FTypes::class_type & self
            , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
            , unsigned nthreads
            , python::object & out_obj
        ) const
        {
            f_caller_t const f_caller(m_f);

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };

    //--------------------------------------------------------------------------
    // Partial specialization for member function with non-void-return and
    // threads forbidden.
    template <class F>
    struct impl<true, false, false, F>
    {
        typedef impl<true, false, false, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , typename FTypes::class_type &
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                , python::object &
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size+2>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return ( make_self_plus_kwargs(kwargs)
                   , python::arg("out")=python::object()
                   );
        }

        python::object
        operator()(
              typename FTypes::class_type & self
            , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
            , python::object & out_obj
        ) const
        {
            f_caller_t const f_caller(m_f);
            unsigned const nthreads = 1;

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };

    //--------------------------------------------------------------------------
    // Partial specialization for stand-alone function with non-void-return and
    // threads forbidden.
    template <class F>
    struct impl<false, false, false, F>
    {
        typedef impl<false, false, false, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                , python::object &
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size+1>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return ( kwargs
                   , python::arg("out")=python::object()
                   );
        }

        python::object
        operator()(
              BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
            , python::object & out_obj
        ) const
        {
            f_caller_t const f_caller(m_f);
            typename FTypes::class_type self;
            unsigned const nthreads = 1;

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };

    //--------------------------------------------------------------------------
    // Partial specialization for stand-alone function with void-return and
    // threads forbidden.
    template <class F>
    struct impl<false, true, false, F>
    {
        typedef impl<false, true, false, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return kwargs;
        }

        python::object
        operator()(
              BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
        ) const
        {
            f_caller_t const f_caller(m_f);
            typename FTypes::class_type self;
            python::object out_obj;
            unsigned const nthreads = 1;

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };

    //--------------------------------------------------------------------------
    // Partial specialization for stand-alone function with void-return and
    // threads allowed.
    template <class F>
    struct impl<false, true, true, F>
    {
        typedef impl<false, true, true, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                , unsigned
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size+1>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return ( kwargs
                   , python::arg("nthreads")=1
                   );
        }

        python::object
        operator()(
              BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
            , unsigned nthreads
        ) const
        {
            f_caller_t const f_caller(m_f);
            typename FTypes::class_type self;
            python::object out_obj;

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };

    //--------------------------------------------------------------------------
    // Partial specialization for stand-alone function with non-void-return and
    // threads allowed.
    template <class F>
    struct impl<false, false, true, F>
    {
        typedef impl<false, false, true, F>
                type;

        typedef boost::mpl::vector<
                  python::object
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & BOOST_PP_INTERCEPT)
                , unsigned
                , python::object &
                >
                signature_t;

        F m_f;
        impl(F f) : m_f(f) {}

        template <class KW>
        static
        python::detail::keywords<KW::size+2>
        make_kwargs(KW const & kwargs)
        {
            BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs(KW::size, IN_ARITY)

            return ( kwargs
                   , python::arg("nthreads")=1
                   , python::arg("out")=python::object()
                   );
        }

        python::object
        operator()(
              BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, python::object const & in_obj)
            , unsigned nthreads
            , python::object & out_obj
        ) const
        {
            f_caller_t const f_caller(m_f);
            typename FTypes::class_type self;

            return callable_call_t::call(
                  f_caller
                , self
                , BOOST_PP_ENUM_PARAMS_Z(1, IN_ARITY, in_obj)
                , out_obj
                , nthreads);
        }
    };
};

#undef BOOST_NUMPY_DSTREAM__is_correct_number_of_kwargs

#undef IN_ARITY

#endif // BOOST_PP_IS_ITERATING

max_stars_repo_name: IceCube-SPNO/BoostNumpy

max_issues_repo_name: IceCube-SPNO/BoostNumpy

max_forks_repo_name: IceCube-SPNO/BoostNumpy


text:
#include <boost/test/unit_test.hpp>
#include <rushhour_trial_trial.h>
#include <rushhour_schedule_fixed.h>
#include <rushhour_tests_steadyclockrange.h>
#include <unordered_set>

namespace rushhour {
namespace trial {

BOOST_AUTO_TEST_CASE(Trial_NoErrorsOnFixedSchedule) {
  schedule::Fixed schedule(150.0, 200);

  std::vector<bool> prepare_called(schedule.size(), false);
  auto do_prepare = [&](std::uint32_t index) {
    prepare_called[index] = true;
  };

  std::vector<bool> send_called(schedule.size(), false);
  auto do_send = [&](CompletionReport* completion) {
    send_called[completion->index()] = true;
    completion->succeeded();
  };

  tests::SteadyClockRange clock_range;
  clock_range.begin();
  auto results = Trial::run(schedule, do_prepare, do_send, std::chrono::milliseconds(5000), 3);
  clock_range.end();

  BOOST_CHECK_EQUAL(schedule.size(), results.size());

  double rate_sum = 0.0;

  auto prev = clock_range.begin_time();
  for (auto& result : results) {
    BOOST_CHECK_GT(result.elapsed().count(), 0);
    BOOST_CHECK(result.succeeded());

    BOOST_CHECK(clock_range.contains(result.started()));
    BOOST_CHECK(clock_range.contains(result.ended()));

    BOOST_CHECK(result.started() >= prev);
    std::chrono::nanoseconds delta = result.started() - prev;
    rate_sum += 1.0e9 / delta.count();
    prev = result.started();
  }

  double rate_avg = rate_sum / results.size();
  BOOST_CHECK_CLOSE(150.0, rate_avg, 5.0);

  for (auto called : prepare_called) {
    BOOST_CHECK(called);
  }

  for (auto called : send_called) {
    BOOST_CHECK(called);
  }
}

}  // namespace trial
}  // namespace rushhour

max_stars_repo_name: nwbruce/rushhour

max_issues_repo_name: nwbruce/rushhour

max_forks_repo_name: nwbruce/rushhour


text:
// Copyright (c) 2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <stats/stats.h>

#include <test/util/setup_common.h>
#include <util/system.h>

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(stats_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(stats)
{
    CStats::DefaultStats()->setMaxMemoryUsageTarget(CStats::DEFAULT_MAX_STATS_MEMORY);

    uint64_t start = GetTime();
    SetMockTime(start);

    CStats::DefaultStats()->addMempoolSample(0, 1, 1);
    SetMockTime(start + 1);
    CStats::DefaultStats()->addMempoolSample(0, 2, 2); //1second should be to short
    SetMockTime(start + 5);
    CStats::DefaultStats()->addMempoolSample(3, 4, 3);

    uint64_t queryFromTime = start;
    uint64_t queryToTime = start + 3600;
    mempoolSamples_t samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);

    BOOST_CHECK_EQUAL(samples[0].m_time_delta, 0);
    BOOST_CHECK_EQUAL(samples[1].m_time_delta, 5);
    BOOST_CHECK_EQUAL(samples[1].m_tx_count, 3);
    BOOST_CHECK_EQUAL(samples[1].m_dyn_mem_usage, 4);

    // check retrieving a subset of the available samples
    queryFromTime = start;
    queryToTime = start;
    samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
    BOOST_CHECK_EQUAL(samples.size(), 1);

    // add some samples
    for (int i = 0; i < 10000; i++) {
        SetMockTime(start + 10 + i * 5);
        CStats::DefaultStats()->addMempoolSample(i, i + 1, i + 2);
    }

    queryFromTime = start + 3600;
    queryToTime = start + 3600;
    samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
    BOOST_CHECK_EQUAL(samples.size(), 1); //get a single sample

    queryFromTime = start;
    queryToTime = start + 3600;
    samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
    BOOST_CHECK(samples.size() >= 3600 / 5);

    // reduce max memory and add 100 samples to ensure it triggers the cleanup
    CStats::DefaultStats()->setMaxMemoryUsageTarget(10 * 1024);
    for (int i = 10000; i < 10100; i++) {
        SetMockTime(start + 10 + i * 5);
        CStats::DefaultStats()->addMempoolSample(i, i + 1, i + 2);
    }

    queryFromTime = start;
    queryToTime = start + 100;
    samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
    BOOST_CHECK_EQUAL(samples.size(), 1);

    queryFromTime = 0; // no range limits
    queryToTime = 0;   // no range  limits
    samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
    BOOST_CHECK_EQUAL(samples.size() < 1000, true);
}

BOOST_AUTO_TEST_SUITE_END()


text:
#ifndef COMMIT_HPP
#define COMMIT_HPP

#include <string>
#include <ctime>
#include <map>
#include <boost/serialization/map.hpp>

namespace boost {
    namespace serialization {
        class access;
    }
}

class Commit {
    public:
        Commit();
        Commit(const std::string& msg);

        std::string hash() const;
        std::string log_string() const;
        std::string tracked_files_string() const;
        std::pair<std::string, std::string> parent_ids() const;
        std::map<std::string, std::string> get_map() const;
        bool map_contains(const std::string& key) const;

        bool find_in_map_and_get_iter(const std::string& key, std::map<std::string, std::string>::iterator& it);
        void put_to_map(const std::string& key, const std::string& value);
        void remove_from_map(const std::string& key);
        void set_second_parent(const std::string& commit_id);

    private:
        std::time_t datetime;
        std::string message;
        std::string first_parent_ref;
        std::string second_parent_ref; // for merges
        std::map<std::string, std::string> name_id_map;

        friend class boost::serialization::access;

        template<class Archive>
        void serialize(Archive& ar, const unsigned int version) {
            ar & datetime & message & first_parent_ref & second_parent_ref & name_id_map;
        }

};

#endif // COMMIT_HPP

max_stars_repo_name: davidkml/vc-made-simple

max_issues_repo_name: davidkml/vc-made-simple

max_forks_repo_name: davidkml/vc-made-simple


text:
/** @file webots_api.hpp
 *  @brief The header file for webots API
 */
#ifndef CPP_WEBOTS_API_WEBOTS_API_H_
#define CPP_WEBOTS_API_WEBOTS_API_H_

#include <Eigen/Eigen>
#include <opencv2/opencv.hpp>
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<errno.h>
#include <math.h>
#include <fstream>
#include <vector>
#include <utility> // std::pair
#include <sstream>

/// libboost include files
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

/// webots include files
#include <webots/Camera.hpp> 		//#include <webots/camera.h>
#include <webots/Device.hpp> 		//#include <webots/device.h>
#include <webots/Display.hpp> 		//#include <webots/display.h>
#include <webots/GPS.hpp> 		//#include <webots/gps.h>
#include <webots/vehicle/Driver.hpp> 	//#include <webots/vehicle/driver.h>
#include <webots/Robot.hpp> 		//#include <webots/robot.h>

#include "paths.hpp"
#include "config_webots.hpp"

using namespace std;
using namespace webots;
using namespace cv;

/// @brief Class for webots API. Inherits the base class pathsIMACS to load the paths to image.
class webotsAPI : pathsIMACS {
public:
    /// Flags to enable various 'features'
    bool enable_display = false; //!< flag for whether the display is enabled in webots scene
    bool has_camera = false;	 //!< flag for whether the webots scene has a camera
    bool has_gps = false;	 //!< flag for whether the webots scene has a gps enabled

    /// Variables required for camera object
    Camera *camera_front; 	/// WbDeviceTag camera_front; Device tag for the front camera
    int camera_width = -1; 	/// width of the camera
    int camera_height = -1;	/// height of the camera
    double camera_fov = -1.0;	/// camera field-of-view

    /// Variables for gps --> to get location
    GPS *gps; 					/// WbDeviceTag gps;
    double gps_coords[3] = {0.0, 0.0, 0.0};	/// x,y,z GPS coordinates
    double gps_speed = 0.0;			/// speed calculated from GPS
    std::vector<long double> positionx;		/// position x (x-coordinate in city.wbt)
    std::vector<long double> positiony;		/// position y (z-coordinate in city.wbt)
    enum { X, Y, Z };   //!< to be used as array indices for GPS coordinates

    /// For speedometer in webots
    Display *display; 			/// WbDeviceTag display; speedometer display
    int display_width = 0;		/// speedometer display width
    int display_height = 0;		/// speedometer display height
    ImageRef *speedometer_image = NULL; /// speedometer image

    /// Vehicle steering variables
    double steer90 = 0.0;	 //!< steer value for 90 degree turns
    double offset_value = 100.0; //!< offset value for 90 degree turns.
    double steering_angle = 0.0; //!< vehicle steering angle to actuate

    /// Speed and image quality (if implementing compression)
    double speed = 0.0;		//!< vehicle speed
    int image_quality = 100;	//!< image quality is set as 100 (highest quality possible).

    /**  @brief C++ implementation for converting webots camera image to Matrix. From RGB to BGR.
         @param[in] camera          The camera device (tag/id)
         @param[in] image           The image from the camera (3 colour - RGB)
	 @return    the matrix format (of the image in BGR)
      */
    cv::Mat webots_img_2_Mat(Camera *camera, const unsigned char *image);
public:
    /// @brief constructor for webotsAPI
    webotsAPI(){
    }

    /// @brief destructor for webotsAPI
    ~webotsAPI(){
    }

    /**  @brief C++ implementation for setting the cruising vehicle speed in webots.
         @param[in] driver          The webots driver (id)
         @param[in] speed           The cruising vehicle speed to be set in webots
    */
    void set_speed(Driver *driver, long double speed);

    /**  @brief C++ implementation for setting the vehicle steering angle in webots.
         @param[in] driver          The webots driver (id)
         @param[in] wheel_angle     The vehicle steering angle to be set in webots
    */
    void set_steering_angle(Driver *driver, double wheel_angle);

    /**  @brief C++ implementation to compute GPS speed in webots.
         @param[in] gps          The webots GPS device (tag/id)
    */
    void compute_gps_speed(GPS *gps);

    /**  @brief C++ implementation to compute current GPS coordinates of the vehicle in webots.
         @param[in] gps          The webots GPS device (tag/id)
    */
    void compute_gps_coords(GPS *gps);

    /**  @brief C++ implementation for updating the speedometer display in webots.
         @param[in] display         The speedometer display (tag/id) in webots
         @param[in] driver          The webots driver (id)
    */
    void update_display(Display *display, Driver *driver);

    /**  @brief C++ implementation for vehicle actuation in webots
         @param[in] driver          The webots driver (id)
         @param[in] steering_angle  The computed steering angle by the controller
    */
    void sim_actuate(Driver *driver, long double steering_angle);

    /**  @brief C++ implementation to read CSV file with 2 columns into a vector of <vector<double>>
         @param[in] filename       The csv filename to read from
	 @return    2 column csv file contents are translated into a vector of <vector<double>>
      */
    vector<vector<double>> read_csv(string filename);

    /**  @brief C++ implementation to get the deviation yL from the given (x,y) coordinates of the vehicle and pre-computed reference (GPS coordinates of the centre of lane)
         @param[in] ref           Reference (csv) filename which contains 2 column GPS coordinates of the lane centres of the entire webots scene
         @param[in] positionx     x-position vertex of vehicle
         @param[in] positiony     y-position vertex of vehicle
         @return    the lateral deviation (yL) for the given (x,y) positions
         @note the reference file `ref` needs to be computed apriori and is not part of the current toolchain
      */
    double getyL(string ref, double positionx , double positiony);

    /**  @brief C++ implementation to calculate the current actual lateral deviation of the vehicle from the centre of the lane in webots.
         @param[in] gps          The webots GPS device (tag/id)
         @param[in] world_encode The webots wbt file. 1: city_straight.wbt, 2: city.wbt
	 @return the current lateral deviation (yL)
    */
    long double calculate_actual_deviation(GPS *gps, int world_encode=1);

    /**  @brief C++ implementation to initialise the webots scene.
         @param[in] driver          The webots driver (id)
         @param[in] update_period   The update period/refresh rate for the camera/GPS
    */
    void initialise(Driver *driver, float update_period);
};

#endif

max_stars_repo_name: sajid-mohamed/imacs

max_issues_repo_name: sajid-mohamed/imacs

max_forks_repo_name: sajid-mohamed/imacs


text:
#include <string>
#include <iostream>
#include <sstream>
#include <filesystem>

#include <dlib\dnn.h>
#include <dlib\clustering.h>
#include <dlib\string.h>
#include <dlib\image_io.h>
#include <dlib\image_processing\frontal_face_detector.h>

template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = dlib::add_prev1<block<N, BN, 1, dlib::tag1<SUBNET>>>;

template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = dlib::add_prev2<dlib::avg_pool<2, 2, 2, 2, dlib::skip1<dlib::tag2<block<N, BN, 2, dlib::tag1<SUBNET>>>>>>;

template <int N, template <typename> class BN, int stride, typename SUBNET>
using block = BN<dlib::con<N, 3, 3, 1, 1, dlib::relu<BN<dlib::con<N, 3, 3, stride, stride, SUBNET>>>>>;

template <int N, typename SUBNET> using ares = dlib::relu<residual<block, N, dlib::affine, SUBNET>>;
template <int N, typename SUBNET> using ares_down = dlib::relu<residual_down<block, N, dlib::affine, SUBNET>>;

template <typename SUBNET> using alevel0 = ares_down<256, SUBNET>;
template <typename SUBNET> using alevel1 = ares<256, ares<256, ares_down<256, SUBNET>>>;
template <typename SUBNET> using alevel2 = ares<128, ares<128, ares_down<128, SUBNET>>>;
template <typename SUBNET> using alevel3 = ares<64, ares<64, ares<64, ares_down<64, SUBNET>>>>;
template <typename SUBNET> using alevel4 = ares<32, ares<32, ares<32, SUBNET>>>;

using anet_type = dlib::loss_metric< dlib::fc_no_bias<128, dlib::avg_pool_everything<
	alevel0<
	alevel1<
	alevel2<
	alevel3<
	alevel4<
	dlib::max_pool<3, 3, 2, 2, dlib::relu<dlib::affine<dlib::con<32, 7, 7, 2, 2,
	dlib::input_rgb_image_sized<150>
	>>>>>>>>>>>>;

void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
	// Get the first occurrence
	size_t pos = data.find(toSearch);

	// Repeat till end is reached
	while (pos != std::string::npos)
	{
		// Replace this occurrence of Sub String
		data.replace(pos, toSearch.size(), replaceStr);
		// Get the next occurrence from the current position
		pos = data.find(toSearch, pos + replaceStr.size());
	}
}

int main(int argc, char **argv)
{
	std::string path = argv[1];

	dlib::shape_predictor						m_sp;
	anet_type									m_net;
	dlib::frontal_face_detector					m_detector;

	std::vector<dlib::matrix<dlib::rgb_pixel>>	m_refFaces;
	std::vector<dlib::matrix<float, 0, 1>>		m_refFacesDescriptors;
	std::vector<std::string>					m_refImagesNames;

	dlib::deserialize("./refFaces_data.dat") >> m_refFacesDescriptors;

	std::cout << "Got " << m_refFacesDescriptors.size() << " descriptors" << std::endl;

	return 0;

	std::cout << "Init..." << std::endl;

	dlib::deserialize("./shape_predictor_5_face_landmarks.dat") >> m_sp;
	dlib::deserialize("./dlib_face_recognition_resnet_model_v1.dat") >> m_net;

	m_detector = dlib::get_frontal_face_detector();

	std::cout << "Success" << std::endl;

	for (const auto & entry : std::filesystem::directory_iterator(path)) {
		if (!entry.is_regular_file())
			continue;

		if (entry.path().extension() != ".jpg")
			continue;

		auto imageName = entry.path().filename().string();

		dlib::matrix<dlib::rgb_pixel> img;
		dlib::load_image(img, entry.path().string());

		auto faces = m_detector(img);

		std::cout << "Found " << faces.size() << " in " << imageName << std::endl;

		for (auto face : faces)
		{
			auto shape = m_sp(img, face);
			std::string newName = std::string(imageName.begin(), imageName.end());
			findAndReplaceAll(newName, "_", "-");
			findAndReplaceAll(newName, ".jpg", "@epitech.eu");
			std::cout << "New name went from " << imageName << " to " << newName << std::endl;

			dlib::matrix<dlib::rgb_pixel> face_chip;
			extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);
			m_refFaces.push_back(std::move(face_chip));
			m_refImagesNames.push_back(newName);
		}

	}
	std::cout << "Loaded " << m_refFaces.size() << " face(s) from " << m_refImagesNames.size() << " file(s)" << std::endl;

	std::cout << "Serialize labels" << std::endl;
	std::ofstream outputLabels;
	outputLabels.open("./refFaces_labels.dat");
	dlib::serialize(m_refImagesNames, outputLabels);
	outputLabels.flush();
	outputLabels.close();

	std::cout << "Calculating descriptors" << std::endl;
	m_refFacesDescriptors = m_net(m_refFaces);

	std::cout << "Serialize descriptors" << std::endl;
	std::ofstream outputData;
	outputData.open("./refFaces_data.dat");
	outputData.close();

	dlib::serialize("./refFaces_data.dat") << m_refFacesDescriptors;
	/*outputData.flush();
	outputData.close();*/

	std::cout << "Success !" << std::endl;

	/*std::ifstream inputData;
	inputData.open("./refFaces_data.dat");*/
	dlib::deserialize("./refFaces_data.dat") >> m_refFacesDescriptors;

	std::cout << "Got " << m_refFacesDescriptors.size() << " descriptors" << std::endl;

	return 0;
}

max_stars_repo_name: Oxydros/HoloTek

max_issues_repo_name: Oxydros/HoloTek

max_forks_repo_name: Oxydros/HoloTek