text:
/**********************************************************************
*  Copyright (c) 2008-2014, Alliance for Sustainable Energy.
*  All rights reserved.
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
**********************************************************************/

#include <gtest/gtest.h>
#include <osversion/test/OSVersionFixture.hpp>
#include <osversion/VersionTranslator.hpp>

#include <model/Model.hpp>
#include <model/Component.hpp>
#include <model/Component_Impl.hpp>
#include <model/ComponentData.hpp>
#include <model/Construction.hpp>
#include <model/Construction_Impl.hpp>
#include <model/Material.hpp>
#include <model/Material_Impl.hpp>
#include <model/ConstructionBase.hpp>
#include <model/ConstructionBase_Impl.hpp>
#include <model/Building.hpp>
#include <model/Building_Impl.hpp>
#include <model/Version.hpp>
#include <model/Version_Impl.hpp>

#include <utilities/bcl/RemoteBCL.hpp>
#include <utilities/bcl/LocalBCL.hpp>
#include <utilities/bcl/OnDemandGenerator.hpp>
#include <utilities/bcl/BCLComponent.hpp>

#include <utilities/idf/IdfObject.hpp>
#include <utilities/idd/OS_Version_FieldEnums.hxx>

#include <utilities/core/Compare.hpp>

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

#include <resources.hxx>
#include <OpenStudio.hxx>

using namespace openstudio;
using namespace model;
using namespace osversion;

TEST_F(OSVersionFixture,VersionTranslator_ExampleModel) {
  osversion::VersionTranslator translator;

  // iterate through osversion subfolders
  openstudio::path resources = resourcesPath() / toPath("osversion");
  for (openstudio::directory_iterator it(resources); it != openstudio::directory_iterator(); ++it) {
    if (boost::filesystem::is_directory(it->status())) {
      // run version translator on each example.osm
      openstudio::path modelPath = it->path() / toPath("example.osm");
      model::OptionalModel result = translator.loadModel(modelPath);
      EXPECT_TRUE(result);
      // check warnings, errors, affected objects
      EXPECT_TRUE(translator.errors().empty());
      EXPECT_TRUE(translator.warnings().empty());
      // print info for visual inspection
      ASSERT_TRUE(result);
      LOG(Debug,"Updated '" << toString(modelPath) << "' to OpenStudio Version "
          << result->version().str() << ".");
      LOG(Debug,"Deprecated objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.deprecatedObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"Untranslated objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.untranslatedObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"New objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.newObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"Refactored objects: ");
      BOOST_FOREACH(const IdfObjectPair& p,translator.refactoredObjects()) {
        LOG(Debug,p.first << "replaced with" << std::endl << std::endl << p.second);
      }
      // make sure save and load is ok
      modelPath = it->path() / toPath("example_updated.osm");
      result->save(modelPath,true);
      result = model::Model::load(modelPath);
      EXPECT_TRUE(result);
    }
  }
}

TEST_F(OSVersionFixture,VersionTranslator_ExampleComponent) {
  osversion::VersionTranslator translator;

  // iterate through osversion subfolders
  openstudio::path resources = resourcesPath() / toPath("osversion");
  for (openstudio::directory_iterator it(resources); it != openstudio::directory_iterator(); ++it) {
    if (boost::filesystem::is_directory(it->status())) {
      // run version translator on each example.osm
      openstudio::path componentPath = it->path() / toPath("example.osc");
      EXPECT_TRUE(exists(componentPath));
      model::OptionalComponent result = translator.loadComponent(componentPath);
      EXPECT_TRUE(result);
      // check warnings, errors, affected objects
      EXPECT_TRUE(translator.errors().empty());
      EXPECT_TRUE(translator.warnings().empty());
      // print info for visual inspection
      ASSERT_TRUE(result);
      LOG(Debug,"Updated '" << toString(componentPath) << "' to OpenStudio Version "
          << result->version().str() << ".");
      LOG(Debug,"Deprecated objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.deprecatedObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"Untranslated objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.untranslatedObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"New objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.newObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"Refactored objects: ");
      BOOST_FOREACH(const IdfObjectPair& p,translator.refactoredObjects()) {
        LOG(Debug,p.first << "replaced with" << std::endl  << std::endl << p.second);
      }
      // make sure component came out ok
      ASSERT_TRUE(result);
      model::Component translated = result.get();
      model::ComponentData contents = translated.componentData();
      ASSERT_NO_THROW(contents.primaryComponentObject());
      model::ModelObject prime = contents.primaryComponentObject();
      ASSERT_TRUE(prime.optionalCast<model::Construction>());
      model::Construction construction = prime.cast<model::Construction>();
      EXPECT_FALSE(construction.layers().empty());
      // make sure save and load is ok
      componentPath = it->path() / toPath("example_updated.osc");
      result->save(componentPath,true);
      result = model::Component::load(componentPath);
      EXPECT_TRUE(result);
    }
  }
}

TEST_F(OSVersionFixture,VersionTranslator_FutureVersion_ExampleModel) {
  osversion::VersionTranslator translator;

  model::Model model = model::exampleModel();

  // it is not generally a good idea to mess with the version like this
  boost::optional<WorkspaceObject> object = model.versionObject();
  ASSERT_TRUE(object);
  boost::optional<model::Version> version = object->optionalCast<model::Version>();
  ASSERT_TRUE(version);
  VersionString vs(version->versionIdentifier());

  int major = vs.major();
  int minor = vs.minor();
  boost::optional<int> patch = vs.patch();
  ASSERT_TRUE(patch);

  std::stringstream ss;

  // version translate current model
  ss.str("");
  ss << model;
  boost::optional<model::Model> m2 = translator.loadModel(ss);
  EXPECT_TRUE(m2);

  // increment patch
  VersionString nextPatch(major,minor,*patch+1);
  EXPECT_TRUE(vs.isNextVersion(nextPatch));
  EXPECT_TRUE(version->setString(OS_VersionFields::VersionIdentifier, nextPatch.str()));
  ss.str("");
  ss << model;
  m2 = translator.loadModel(ss);
  EXPECT_TRUE(m2);

  // increment minor
  VersionString nextMinor(major,minor+1,0);
  EXPECT_TRUE(vs.isNextVersion(nextMinor));
  EXPECT_TRUE(version->setString(OS_VersionFields::VersionIdentifier, nextMinor.str()));
  ss.str("");
  ss << model;
  m2 = translator.loadModel(ss);
  EXPECT_TRUE(m2);

  // increment major
  VersionString nextMajor(major+1,0,0);
  EXPECT_TRUE(version->setString(OS_VersionFields::VersionIdentifier, nextMajor.str()));
  ss.str("");
  ss << model;
  m2 = translator.loadModel(ss);
  EXPECT_TRUE(m2);

  // too far ahead
  VersionString aStepTooFar(major,minor+1,1);
  EXPECT_TRUE(version->setString(OS_VersionFields::VersionIdentifier, aStepTooFar.str()));
  ss.str("");
  ss << model;
  m2 = translator.loadModel(ss);
  EXPECT_FALSE(m2);

  aStepTooFar = VersionString(major+1,1,0);
  EXPECT_TRUE(version->setString(OS_VersionFields::VersionIdentifier, aStepTooFar.str()));
  ss.str("");
  ss << model;
  m2 = translator.loadModel(ss);
  EXPECT_FALSE(m2);

  aStepTooFar = VersionString(major+1,4);
  EXPECT_TRUE(version->setString(OS_VersionFields::VersionIdentifier, aStepTooFar.str()));
  ss.str("");
  ss << model;
  m2 = translator.loadModel(ss);
  EXPECT_FALSE(m2);
}

TEST_F(OSVersionFixture,VersionTranslator_AllDefaultObjects) {
  // iterate through osversion subfolders
  osversion::VersionTranslator translator;

  // iterate through osversion subfolders
  openstudio::path resources = resourcesPath() / toPath("osversion");
  for (openstudio::directory_iterator it(resources); it != openstudio::directory_iterator(); ++it) {
    if (boost::filesystem::is_directory(it->status())) {
      // load each IddFile and create an IdfFile containing one of each of its objects
      openstudio::path iddPath = it->path() / toPath("OpenStudio.idd");
      IddFile iddFile = IddFile::load(iddPath).get();
      IdfFile idfFile(iddFile);
      BOOST_FOREACH(const IddObject& iddObj,iddFile.objects()) {
        if (iddObj.name() != "OS:ComponentData"){
          idfFile.addObject(IdfObject(iddObj));
        }
      }
      openstudio::path modelPath = toPath("temp.osm");
      idfFile.save(modelPath,true);
      // run version translator
      model::OptionalModel result = translator.loadModel(modelPath);
      EXPECT_TRUE(result);

      // number of warnings varies. to check, need to compose starting version from it->path().
      // does not seem worth the effort at the moment, log messages already in log
      //EXPECT_TRUE(translator.warnings().empty());

      // check warnings, errors, affected objects, log messages already in log
      EXPECT_TRUE(translator.errors().empty());

      // print info for visual inspection
      ASSERT_TRUE(result);
      LOG(Debug,"Updated '" << toString(modelPath) << "' to OpenStudio Version "
          << result->version().str() << ".");
      LOG(Debug,"Deprecated objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.deprecatedObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"Untranslated objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.untranslatedObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"New objects: ");
      BOOST_FOREACH(const IdfObject& object,translator.newObjects()) {
        LOG(Debug,object);
      }
      LOG(Debug,"Refactored objects: ");
      BOOST_FOREACH(const IdfObjectPair& p,translator.refactoredObjects()) {
        LOG(Debug,p.first << "replaced with" << std::endl << std::endl << p.second);
      }
    }
  }
}

TEST_F(OSVersionFixture,VersionTranslator_0_7_4_NameRefsTranslated) {
  // Translator adds handle fields, but leaves initial name references as-is.
  //
  // This should be okay, because WorkspaceObject code looks to match name references
  // on import.
  //
  // The only exception to this is \object-list AllObjects fields. For 0.7.4, only
  // OS:ComponentData is affected, and it is explicitly translated field by field.

  // Start with 0_7_3 example.osm
  openstudio::path modelPath = resourcesPath() / toPath("osversion/0_7_3/example.osm");

  // Translate to current version.
  osversion::VersionTranslator translator;
  model::OptionalModel result = translator.loadModel(modelPath);
  ASSERT_TRUE(result);
  model::Model model = *result;
  EXPECT_EQ(VersionString("0.7.3"),translator.originalVersion());

  // Confirm that expected pointers are still there
  model::ConstructionVector constructions = model.getModelObjects<model::Construction>();
  EXPECT_FALSE(constructions.empty());
  BOOST_FOREACH(const model::Construction construction,constructions) {
    ASSERT_FALSE(construction.layers().empty());
    model::Material material = construction.layers()[0];
    ASSERT_FALSE(material.attributeNames().empty());
  }
}

TEST_F(OSVersionFixture,Profile_ModelLoading_LatestVersion) {
  VersionString thisVersion(openStudioVersion());
  openstudio::path modelPath = exampleModelPath(thisVersion);

  model::OptionalModel oModel = model::Model::load(modelPath);

  osversion::VersionTranslator translator;
  oModel = translator.loadModel(modelPath);
}

TEST_F(OSVersionFixture,Profile_ModelLoading_OldestVersion) {
  VersionString oldestVersion("0.7.0");
  openstudio::path modelPath = exampleModelPath(oldestVersion);

  OptionalIddFile oIddFile = IddFile::load(iddPath(oldestVersion));
  ASSERT_TRUE(oIddFile);
  OptionalIdfFile oIdfFile = IdfFile::load(modelPath,*oIddFile);
  ASSERT_TRUE(oIdfFile);

  osversion::VersionTranslator translator;
  model::OptionalModel oModel = translator.loadModel(modelPath);
  ASSERT_TRUE(oModel);
}

TEST_F(OSVersionFixture,ModelLoading_PreserveHandles) {
  VersionString firstVersionWithHandlesEmbedded("0.7.4");
  openstudio::path modelPath = exampleModelPath(firstVersionWithHandlesEmbedded);

  OptionalIddFile oIddFile = IddFile::load(iddPath(firstVersionWithHandlesEmbedded));
  ASSERT_TRUE(oIddFile);
  OptionalIdfFile oIdfFile = IdfFile::load(modelPath,*oIddFile);
  ASSERT_TRUE(oIdfFile);
  Handle buildingHandle1 = oIdfFile->getObjectsByType(oIddFile->getObject("OS:Building").get())[0].handle();

  osversion::VersionTranslator translator;
  model::OptionalModel oModel = translator.loadModel(modelPath);
  ASSERT_TRUE(oModel);
  Handle buildingHandle2 = oModel->getUniqueModelObject<model::Building>().handle();
  EXPECT_TRUE(buildingHandle1 == buildingHandle2);
}

TEST_F(OSVersionFixture,Profile_ComponentLoading_LatestVersion) {
  VersionString thisVersion(openStudioVersion());
  openstudio::path componentPath = exampleComponentPath(thisVersion);

  model::OptionalComponent oComponent = model::Component::load(componentPath);

  osversion::VersionTranslator translator;
  oComponent = translator.loadComponent(componentPath);
}

TEST_F(OSVersionFixture,Profile_ComponentLoading_OldestVersion) {
  VersionString oldestVersion("0.7.0");
  openstudio::path componentPath = exampleComponentPath(oldestVersion);

  OptionalIddFile oIddFile = IddFile::load(iddPath(oldestVersion));
  ASSERT_TRUE(oIddFile);
  OptionalIdfFile oIdfFile = IdfFile::load(componentPath,*oIddFile);

  osversion::VersionTranslator translator;
  model::OptionalComponent oComponent = translator.loadComponent(componentPath);
}

TEST_F(OSVersionFixture,PrimaryObject) {

  openstudio::path path = resourcesPath() / toPath("osversion/unknown.osc");

  ASSERT_TRUE(boost::filesystem::exists(path));

  osversion::VersionTranslator translator;
  boost::optional<model::Component> component = translator.loadComponent(path);

  ASSERT_TRUE(component);

  model::Model m;

  ASSERT_FALSE(component->primaryObject().handle().isNull());

  model::ModelObject mo = component->primaryObject().clone(m);

  ASSERT_FALSE(mo.handle().isNull());

  boost::optional<model::ConstructionBase> cb = mo.optionalCast<model::ConstructionBase>();

  EXPECT_TRUE(cb);
}

TEST_F(OSVersionFixture,KeepHandles) {

  openstudio::path iddpath = resourcesPath() / toPath("osversion/0_7_4/OpenStudio.idd");
  openstudio::path path = resourcesPath() / toPath("osversion/0_7_4/example.osm");

  // open as idf
  boost::optional<IddFile> oldIdd = IddFile::load(iddpath);
  ASSERT_TRUE(oldIdd);
  boost::optional<IdfFile> oldIdf = IdfFile::load(path);
  ASSERT_TRUE(oldIdf);

  osversion::VersionTranslator vt;
  boost::optional<model::Model> model = vt.loadModel(path);
  ASSERT_TRUE(model);

  // spot check a few model objects
  std::vector<IdfObject> idfObjects = oldIdf->getObjectsByName("OS:RunPeriod 1");
  ASSERT_EQ(1u, idfObjects.size());
  std::vector<WorkspaceObject> workspaceObjects = model->getObjectsByName("OS:RunPeriod 1");
  ASSERT_EQ(1u, workspaceObjects.size());
  EXPECT_TRUE(idfObjects[0].handle() == workspaceObjects[0].handle());

  idfObjects = oldIdf->getObjectsByName("OS:Space 1");
  ASSERT_EQ(1u, idfObjects.size());
  workspaceObjects = model->getObjectsByName("OS:Space 1");
  ASSERT_EQ(1u, workspaceObjects.size());
  EXPECT_TRUE(idfObjects[0].handle() == workspaceObjects[0].handle());

  idfObjects = oldIdf->getObjectsByName("OS:Surface 1");
  ASSERT_EQ(1u, idfObjects.size());
  workspaceObjects = model->getObjectsByName("OS:Surface 1");
  ASSERT_EQ(1u, workspaceObjects.size());
  EXPECT_TRUE(idfObjects[0].handle() == workspaceObjects[0].handle());
}

TEST_F(OSVersionFixture,OnDemandComponent) {
  RemoteBCL remoteBCL;
  bool ok = remoteBCL.downloadOnDemandGenerator("bb8aa6a0-6a25-012f-9521-00ff10704b07");
  EXPECT_TRUE(ok);
  boost::optional<OnDemandGenerator> oGenerator = remoteBCL.waitForOnDemandGenerator();
  ASSERT_TRUE(oGenerator);
  OnDemandGenerator generator = *oGenerator;

  generator.setArgumentValue("NREL_reference_building_vintage","ASHRAE_90.1-2004");
  generator.setArgumentValue("Climate_zone","ClimateZone 1-8");
  generator.setArgumentValue("NREL_reference_building_primary_space_type","SmallOffice");
  generator.setArgumentValue("NREL_reference_building_secondary_space_type","WholeBuilding");

  boost::optional<BCLComponent> oBCLComponent = LocalBCL::instance().getOnDemandComponent(generator);
  if (!oBCLComponent) {
    oBCLComponent = remoteBCL.getOnDemandComponent(generator);
  }
  ASSERT_TRUE(oBCLComponent);
  BCLComponent bclComponent = *oBCLComponent;

  StringVector fileStrs = bclComponent.files("osc");
  ASSERT_FALSE(fileStrs.empty());
  openstudio::path oscPath = toPath(fileStrs[0]);

  VersionTranslator translator;
  OptionalComponent oComponent = translator.loadComponent(oscPath);
  ASSERT_TRUE(oComponent);
  Component component = *oComponent;
  EXPECT_EQ(IddObjectType("OS:SpaceType"),component.primaryObject().iddObjectType());
}

max_stars_repo_name: ORNL-BTRIC/OpenStudio

max_issues_repo_name: ORNL-BTRIC/OpenStudio

max_forks_repo_name: ORNL-BTRIC/OpenStudio


text:
#include <boost/python.hpp>
#include <prelude/runtime/chunk.hpp>
#include <prelude/runtime/cuarray.hpp>
#include <prelude/runtime/type_holder.hpp>
#include <prelude/runtime/make_sequence.hpp>
#include <prelude/runtime/mempool.hpp>
#include "cunp.hpp"
#include "np_inspect.hpp"
#include "type.hpp"
#include "monotype.hpp"
#include "type_printer.hpp"
#include <sstream>
#include <boost/python/slice.hpp>
#include "utility/isinstance.hpp"
#include <boost/scoped_ptr.hpp>

#include <thrust/system/omp/memory.h>
#include <boost/version.hpp>

using std::shared_ptr;
using std::make_shared;
using std::static_pointer_cast;
using std::ostringstream;
using std::ostream;
using std::vector;
using std::string;
using std::map;

typedef boost::shared_ptr<copperhead::cuarray> sp_cuarray;

namespace copperhead {

namespace detail {
template<typename T>
bool isinstance(PyObject* in) {
    boost::python::extract<T> extractor(in);
    if (extractor.check()) {
        return true;
    } else {
        return false;
    }
}
}

void desc_lens(PyObject* in, vector<size_t>& lens,
               shared_ptr<const backend::type_t>& el_type,
               vector<np_array_info>& leaves,
               size_t level=0) {

    //Are we at a leaf level?
    if (level == lens.size() - 1) {
        np_array_info in_props = inspect_array(in);
        if (std::get<0>(in_props) == NULL) {
            throw std::invalid_argument("Can't create cuarray from this object");
        }
        shared_ptr<const backend::type_t> obs_el_type = std::get<2>(in_props);
        if (backend::detail::isinstance<backend::sequence_t>(*obs_el_type)) {
            obs_el_type = (static_pointer_cast<const backend::sequence_t>(obs_el_type))->sub().ptr();
        }
        //If element type isn't correct
        if ((obs_el_type != el_type) &&
            //And we didn't find an empty leaf (which is allowed)
            (std::get<1>(in_props) != 0)) {
            throw std::invalid_argument("Can't create cuarray from this object, it was not homogenously typed");
        }
        lens[level] += std::get<1>(in_props);
        leaves.push_back(in_props);
    } else if (PyList_Check(in)) {
        size_t cur_len = PyList_Size(in);
        lens[level] += cur_len;
        for(size_t i = 0; i < cur_len; i++) {
            desc_lens(PyList_GetItem(in, i), lens, el_type, leaves, level+1);
        }
    } else {
        throw std::invalid_argument("Can't create cuarray from this object, it was not homogeneously nested");
    }
}

size_t get_el_size(shared_ptr<const backend::type_t> p) {
    if (p == backend::int32_mt) {
        return sizeof(int);
    } else if (p == backend::int64_mt) {
        return sizeof(long);
    } else if (p == backend::float32_mt) {
        return sizeof(float);
    } else if (p == backend::float64_mt) {
        return sizeof(double);
    } else if (p == backend::bool_mt) {
        return sizeof(bool);
    }
    return 0;
}

void populate_array(PyObject* in,
                    vector<boost::shared_ptr<chunk> >& locals,
                    size_t el_size,
                    vector<np_array_info>::const_iterator& leaves,
                    vector<size_t>& offsets, size_t level = 0) {
    //Are we at a leaf level?
    if (level == offsets.size() - 1) {
        np_array_info in_props = *leaves;
        ++leaves;
        char* local = (char*)locals[level]->ptr();
        size_t size = std::get<1>(in_props);
        memcpy(local + (offsets[level] * el_size), std::get<0>(in_props), size * el_size);
        offsets[level] += size;
    } else {
        size_t cur_len = PyList_Size(in);
        size_t* desc = (size_t*)locals[level]->ptr();
        for(size_t i = 0; i < cur_len; i++) {
            desc[offsets[level]] = offsets[level+1];
            ++offsets[level];
            populate_array(PyList_GetItem(in, i), locals, el_size, leaves, offsets, level+1);
        }
    }
}

shared_ptr<const backend::type_t> examine_leaf_array(PyObject* leaf) {
    np_array_info leaf_props = inspect_array(leaf);
    if (std::get<0>(leaf_props) == NULL) {
        throw std::invalid_argument("Can't create cuarray from this object");
    }

    shared_ptr<const backend::type_t> indicated_type = std::get<2>(leaf_props);
    if (indicated_type == backend::void_mt) {
        size_t indicated_length = std::get<1>(leaf_props);
        if (indicated_length != 0) {
            throw std::invalid_argument("Can't create cuarray from this object");
        }
        //An empty sequence. Following numpy, declare it do be a Float64 sequence.
        return backend::float64_mt;
    }

    //Derive type
    shared_ptr<const backend::sequence_t> seq_type =
        static_pointer_cast<const backend::sequence_t>(std::get<2>(leaf_props));

    shared_ptr<const backend::type_t> el_type = seq_type->sub().ptr();
    if (el_type == backend::void_mt) {
        throw std::invalid_argument("Can't create cuarray from this object");
    }
    return el_type;
}

sp_cuarray make_cuarray_PyObject(PyObject* in) {
    if (detail::isinstance<sp_cuarray>(in)) {
        //XXX Do a deep copy (following numpy)

        return boost::python::extract<sp_cuarray>(in);
    }

    //Establish nesting depth
    int depth = -1;
    PyObject* previous = in;
    PyObject* patient = in;

    shared_ptr<const backend::type_t> el_type;
    shared_ptr<const backend::type_t> in_type;
    size_t el_size;

    while ((patient != NULL) && (PyList_Check(patient))) {
        ++depth;
        previous = patient;

        size_t length = PyList_Size(patient);
        if (length > 0) {
            patient = PyList_GetItem(patient, 0);
        } else {
            patient = NULL;
        }
    }

    if (((patient != NULL) && isnumpyarray(patient))) {
        //Leaf element is a numpy array
        ++depth;
        el_type = examine_leaf_array(patient);
    } else {
        //Leaf element is not a numpy array,
        //back up one level in nesting and try to convert
        //it to a numpy array
        el_type = examine_leaf_array(previous);
    }

    el_size = get_el_size(el_type);
    in_type = el_type;
    for(int i = -1; i < depth; i++) {
        in_type = std::make_shared<backend::sequence_t>(in_type);
    }

    //Derive descriptor lengths, type check, derive leaf pointers
    vector<size_t> lens(depth+1, 1);
    lens[depth] = 0;
    vector<np_array_info> leaves;

    desc_lens(in, lens, el_type, leaves);

    //Construct array
    type_holder* th = new type_holder();
    th->m_t = in_type;

    sp_cuarray result(
        new cuarray(th));
    //Allocate descriptors
    for(int i = 0; i < depth; i++) {
        result->add_chunk(boost::shared_ptr<chunk>(new chunk(cpp_tag(), sizeof(size_t) * lens[i])), true);
#ifdef CUDA_SUPPORT
        result->add_chunk(boost::shared_ptr<chunk>(new chunk(cuda_tag(), sizeof(size_t) * lens[i])), false);
#endif
    }

    //Allocate data
    result->add_chunk(boost::shared_ptr<chunk>(new chunk(cpp_tag(), el_size * lens[depth])), true);
#ifdef CUDA_SUPPORT
    result->add_chunk(boost::shared_ptr<chunk>(new chunk(cuda_tag(), el_size * lens[depth])), false);
#endif

    std::vector<boost::shared_ptr<chunk> >& local_chunks = result->get_chunks(cpp_tag(), false);
    //Create descriptors and data
    vector<size_t> offsets(depth+1, 0);
    auto leaves_iterator = leaves.cbegin();
    populate_array(in, local_chunks, el_size, leaves_iterator, offsets);

    //Tack on tail descriptors
    for(int i = 0; i < depth; i++) {
        size_t* local = (size_t*)local_chunks[i]->ptr();
        local[lens[i]-1] = offsets[i+1];
    }

    result->m_l = lens;

    return result;
}

shared_ptr<backend::type_t> type_derive(const cuarray& in) {
    //const_pointer_cast necessary here because boost::python
    //doesn't deal well with shared_ptr<const T>.
    //However, since we don't expose any methods for mutating
    //backend::type_t objects in Python, this shouldn't cause
    //issues
    return std::const_pointer_cast<backend::type_t>(in.m_t->m_t);
}

sp_cuarray make_index_view(sp_cuarray& in, long index) {

    //This function only operates on nested sequences. Ensure type complies
    shared_ptr<const backend::sequence_t> seq_t =
        static_pointer_cast<const backend::sequence_t>(in->m_t->m_t);
    shared_ptr<const backend::type_t> sub_t = seq_t->sub().ptr();
    if (!backend::detail::isinstance<backend::sequence_t>(*sub_t)) {
        throw std::invalid_argument("Internal error, can't index sequence (index_view)");
    }

    //Assemble resulting view
    type_holder* th = new type_holder();
    th->m_t = sub_t;

    sp_cuarray result(new cuarray(th));

    bool local_valid = in->clean(cpp_tag());
    std::vector<boost::shared_ptr<chunk> >& local_chunks = in->get_chunks(cpp_tag(), false);
#ifdef CUDA_SUPPORT
    bool remote_valid = in->clean(cuda_tag());
    std::vector<boost::shared_ptr<chunk> >& remote_chunks = in->get_chunks(cuda_tag(), false);
#endif
    //Index into outermost descriptor
    size_t* root_desc = (size_t*)local_chunks[0]->ptr() + in->m_o;
    size_t begin = root_desc[index];
    result->m_o = begin;
    size_t end = root_desc[index+1];
    size_t length = end - begin;

    //Will return a nested sequence?
    if (in->m_l.size() > 2) {
        length++; //Account for tail descriptor entry
    }
    result->push_back_length(length);
    //Copy remaining lengths
    for(size_t i = 2;
        i < in->m_l.size();
        i++) {
        result->push_back_length(in->m_l[i]);
    }
    //Copy buffers for view
    for(size_t i = 1;
        i < in->m_l.size();
        i++) {
        result->add_chunk(local_chunks[i], local_valid);
#ifdef CUDA_SUPPORT
        result->add_chunk(remote_chunks[i], remote_valid);
#endif
    }
    return result;
}

template<typename T>
PyObject* deref_scalar(const boost::shared_ptr<chunk>& s, long index) {
    const T* src = reinterpret_cast<T*>(s->ptr());
    return make_scalar(src[index]);
}

PyObject* deref_zip_sequence(shared_ptr<const backend::type_t> t,
                             typename vector<boost::shared_ptr<chunk> >::const_iterator& leaf,
                             long index) {
    if (t == backend::int32_mt) {
        PyObject* result = deref_scalar<int>(*leaf, index);
        leaf++;
        return result;
    } else if (t == backend::int64_mt) {
        PyObject* result = deref_scalar<long>(*leaf, index);
        leaf++;
        return result;
    } else if (t == backend::float32_mt) {
        PyObject* result = deref_scalar<float>(*leaf, index);
        leaf++;
        return result;
    } else if (t == backend::float64_mt) {
        PyObject* result = deref_scalar<double>(*leaf, index);
        leaf++;
        return result;
    } else if (t == backend::bool_mt) {
        PyObject* result = deref_scalar<bool>(*leaf, index);
        leaf++;
        return result;
    } else if (backend::detail::isinstance<backend::tuple_t>(*t)) {
        const backend::tuple_t& tup = boost::get<const backend::tuple_t&>(*t);
        int arity = tup.arity();
        PyObject* result = PyTuple_New(arity);
        int i = 0;
        for(auto j = tup.begin(); j!= tup.end(); i++, j++) {
            PyTuple_SetItem(result, i, deref_zip_sequence(j->ptr(), leaf, index));
        }
        return result;
    } else {
        throw std::invalid_argument("Internal error, can't index sequence (deref_zip_sequence)");
    }

}

PyObject* getitem_idx(sp_cuarray& in, long index) {
    //Handle negative indices as Python does
    size_t length = in->size();

    if (index < 0) {
        index += length;
    }
    //Bounds check
    if ((index < 0) || (index >= long(length))) {
         PyErr_SetString(PyExc_IndexError, "Index out of range");
         boost::python::throw_error_already_set();
    }

    shared_ptr<const backend::sequence_t> seq_t = static_pointer_cast<const backend::sequence_t>(in->m_t->m_t);
    shared_ptr<const backend::type_t> sub_t = seq_t->sub().ptr();

    if (sub_t == backend::int32_mt) {
        sequence<cpp_tag, int> s = make_sequence<sequence<cpp_tag, int> >(in, cpp_tag(), false);
        return make_scalar(s[index]);
    } else if (sub_t == backend::int64_mt) {
        sequence<cpp_tag, long> s = make_sequence<sequence<cpp_tag, long> >(in, cpp_tag(), false);
        return make_scalar(s[index]);
    } else if (sub_t == backend::float32_mt) {
        sequence<cpp_tag, float> s = make_sequence<sequence<cpp_tag, float> >(in, cpp_tag(), false);
        return make_scalar(s[index]);
    } else if (sub_t == backend::float64_mt) {
        sequence<cpp_tag, double> s = make_sequence<sequence<cpp_tag, double> >(in, cpp_tag(), false);
        return make_scalar(s[index]);
    } else if (sub_t == backend::bool_mt) {
        sequence<cpp_tag, bool> s = make_sequence<sequence<cpp_tag, bool> >(in, cpp_tag(), false);
        return make_scalar(s[index]);
    } else if (backend::detail::isinstance<backend::tuple_t>(*sub_t)) {
        auto leaf = in->get_chunks(cpp_tag(), false).cbegin();
        return deref_zip_sequence(sub_t, leaf, index);
    } else {
        sp_cuarray sub_array = make_index_view(in, index);
        return boost::python::converter::shared_ptr_to_python(sub_array);
    }
}

// void setitem_idx(sp_cuarray& in, size_t index, PyObject* value) {
//     shared_ptr<backend::sequence_t> seq_t = static_pointer_cast<backend::sequence_t>(in->m_t);
//     shared_ptr<backend::type_t> sub_t = seq_t->p_sub();
//     if (sub_t == backend::int32_mt) {
//         sequence<int> s = make_sequence<sequence<int> >(in, true, false);
//         s[index] = unpack_scalar_int(value);
//     } else if (sub_t == backend::int64_mt) {
//         sequence<long> s = make_sequence<sequence<long> >(in, true, false);
//         s[index] = unpack_scalar_long(value);
//     } else if (sub_t == backend::float32_mt) {
//         sequence<float> s = make_sequence<sequence<float> >(in, true, false);
//         s[index] = unpack_scalar_float(value);
//     } else if (sub_t == backend::float64_mt) {
//         sequence<double> s = make_sequence<sequence<double> >(in, true, false);
//         s[index] = unpack_scalar_double(value);
//     } else if (sub_t == backend::bool_mt) {
//         sequence<bool> s = make_sequence<sequence<bool> >(in, true, false);
//         s[index] = unpack_scalar_bool(value);
//     }

// }

#if BOOST_VERSION < 105300

template<class T>
T* get_pointer(shared_ptr<T> const &p) {
    return p.get();
}

#endif

class cuarray_iterator {
private:
    sp_cuarray source;
    size_t index;
    size_t length;
public:
    cuarray_iterator(sp_cuarray& _source) : source(_source), index(0) {
        length = source->size();
    }
    PyObject* next() {
        if (index >= length) {
            PyErr_SetString(PyExc_StopIteration, "No more data.");
            boost::python::throw_error_already_set();
        }
        return getitem_idx(source, index++);
    }
};

shared_ptr<cuarray_iterator>
make_iterator(sp_cuarray& in) {
    return make_shared<cuarray_iterator>(in);
}

template<typename T>
std::ostream& operator<<(std::ostream& os, sequence<cpp_tag, T, 0>& in) {
    os << "[";
    for(size_t i = 0; i < in.size(); i++) {
        os << in[i];
        if (i + 1 != in.size()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

template<typename T, int D>
std::ostream& operator<<(std::ostream& os, sequence<cpp_tag, T, D>& in) {
    os << "[";
    for(size_t i = 0; i < in.size(); i++) {
        sequence<cpp_tag, T, D-1> cur = in[i];
        os << cur;
        if (i + 1 != in.size()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

void print_zip_element(sp_cuarray& in, size_t index, ostream& os) {
    shared_ptr<const backend::sequence_t> seq_t = static_pointer_cast<const backend::sequence_t>(in->m_t->m_t);
    shared_ptr<const backend::type_t> sub_t = seq_t->sub().ptr();
    auto leaf = in->get_chunks(cpp_tag(), false).cbegin();
    PyObject* el = deref_zip_sequence(sub_t, leaf, index);
    PyObject* repr = PyObject_Repr(el);
    Py_DECREF(el);
    os << PyString_AsString(repr);
    Py_DECREF(repr);
}

void print_array(sp_cuarray& in, ostream& os) {
    shared_ptr<const backend::sequence_t> seq_t = static_pointer_cast<const backend::sequence_t>(in->m_t->m_t);
    shared_ptr<const backend::type_t> sub_t = seq_t->sub().ptr();
    if (sub_t == backend::int32_mt) {
        sequence<cpp_tag, int> s = make_sequence<sequence<cpp_tag, int> >(in, cpp_tag(), false);
        os << s;
    } else if (sub_t == backend::int64_mt) {
        sequence<cpp_tag, long> s = make_sequence<sequence<cpp_tag, long> >(in, cpp_tag(), false);
        os << s;
    } else if (sub_t == backend::float32_mt) {
        sequence<cpp_tag, float> s = make_sequence<sequence<cpp_tag, float> >(in, cpp_tag(), false);
        os << s;
    } else if (sub_t == backend::float64_mt) {
        sequence<cpp_tag, double> s = make_sequence<sequence<cpp_tag, double> >(in, cpp_tag(), false);
        os << s;
    } else if (sub_t == backend::bool_mt) {
        sequence<cpp_tag, bool> s = make_sequence<sequence<cpp_tag, bool> >(in, cpp_tag(), false);
        os << s;
    } else if (backend::detail::isinstance<backend::tuple_t>(*sub_t)) {
        os << "[";
        size_t length = in->m_l[0];
        for(size_t i = 0; i < length; i++) {
            print_zip_element(in, i, os);
            if (i + 1 < length) {
                os << ", ";
            }
        }
        os << "]";
    } else {
        os << "[";
        size_t length = in->m_l[0] - 1;
        for(size_t i = 0; i < length; i++) {
            sp_cuarray el = make_index_view(in, i);
            print_array(el, os);
            if (i + 1 < length) {
                os << ", ";
            }
        }
        os << "]";

    }
}

void print_type(sp_cuarray&in, ostream& os) {
    backend::repr_type_printer tp(os);
    boost::apply_visitor(tp, *(in->m_t->m_t));
}

string repr_cuarray(sp_cuarray& in) {
    ostringstream os;
    os << "cuarray(";
    print_array(in, os);
    os << ", type=";
    print_type(in, os);
    os << ")";
    return os.str();
}

string str_cuarray(sp_cuarray& in) {
    ostringstream os;
    print_array(in, os);
    return os.str();
}

bool clean(cuarray& in, boost::python::object place) {
    boost::python::object bpo_tag = place.attr("tag")();
    //Extra parentheses here are for C++11/boost::variant WAR needed
    //for Boost 1.53.0
    copperhead::system_variant tag =
        boost::python::extract<copperhead::system_variant>(bpo_tag)();
    return in.clean(tag);
}

void cuarray_copy(cuarray& in, boost::python::object place, bool invalidate) {
    boost::python::object bpo_tag = place.attr("tag")();
    //Extra parentheses here are for C++11/boost::variant WAR needed
    //for Boost 1.53.0
    copperhead::system_variant tag = boost::python::extract<copperhead::system_variant>(bpo_tag)();
    in.get_chunks(tag, invalidate);
}

sp_cuarray force(sp_cuarray &in, boost::python::object place) {
    cuarray_copy(*in, place, false);
    //XXX What we really need here is an event system
    //This is much too big a hammer
#ifdef CUDA_SUPPORT
    cudaThreadSynchronize();
#endif
    return in;
}

}

BOOST_PYTHON_MODULE(cudata) {
    using namespace boost::python;
    using namespace copperhead;
    class_<cuarray, boost::shared_ptr<cuarray>, boost::noncopyable >("cuarray", no_init)
        .def("__init__", make_constructor(make_cuarray_PyObject))
        .def("__repr__", repr_cuarray)
        .def("__str__", str_cuarray)
        .def("__getitem__", &getitem_idx)
        //.def("__setitem__", &setitem_idx)
        .add_property("type", type_derive)
        .def("__iter__", make_iterator);

    class_<cuarray_iterator, shared_ptr<cuarray_iterator> >
        ("cuarrayiterator", no_init)
        .def("next", &cuarray_iterator::next)
        ;
    def("take_down", &take_down);
    def("force", &force);
}

ext: cpp

max_stars_repo_name: bryancatanzaro/copperhead

max_issues_repo_name: bryancatanzaro/copperhead

max_forks_repo_name: bryancatanzaro/copperhead


text:
#include <string>
#include <iostream>
#include <boost/program_options.hpp>
#include "../lib/Database.hpp"
#include "../lib/PageDownloader.hpp"
#include "../lib/Library.hpp"
#include "../lib/Bookmark.hpp"
#include "../lib/Bmrk.hpp"
#include "../lib/Tag.hpp"

namespace po = boost::program_options;
using std::cin;
using std::cout;
using namespace bmrk;

String getUserRootDir() {
  if (::getenv("BMRK_DIR")) {
    return ::getenv("BMRK_DIR");
  }

  String homeDir = ::getenv("HOME");
  /* linux */
  return homeDir += "/.config/bmrk";
  // FIXME support other OS
}

void tryChange(String& str) {
  String line;
  cout << "Change? ";
  std::getline(cin, line);
  if (!line.empty()) {
    str = line;
  }
}

int main(int argc, char* argv[]) {
  po::options_description desc{"bmrk"};

  desc.add_options()                                          //
      ("interactive,i", "Interactive")                        //
      ("url,u", po::value<String>(), "Url")                   //
      ("title,t", po::value<String>(), "Title")               //
      ("tags,a", po::value<String>(), "Comma separated tags") //
      ("notes,n", po::value<String>(), "Notes");              //

  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);

  Config config({{"root", getUserRootDir()}});

  auto db = std::make_shared<Database>(config);
  auto dwl = std::make_shared<PageDownloader>();

  Bmrk bmrk{dwl, db};

  auto url = vm.count("url") ? vm["url"].as<String>() : "";
  if (url.empty()) {
    // FIXME here we need a more friendly error
    throw std::runtime_error("url is mandatory");
  }

  auto bm = bmrk.createBookmarkFromUrl(url).get();
	auto bmd = bm->data();

  auto title = vm.count("title") ? vm["title"].as<String>() : bmd.title;
  auto tags = vm.count("tags") ? vm["tags"].as<String>() : formatTags(bmd.tags);
  auto notes = vm.count("notes") ? vm["notes"].as<String>() : bmd.notes;

  if (vm.count("interactive")) {
		std::cerr << "interactive\n";
    String line;

    cout << "Url: " << bmd.url << "\n";

    cout << "Title: " << title << "\n";
    tryChange(title);

    cout << "Tags: " << tags << "\n";
    tryChange(tags);

    cout << "Notes: " << notes << "\n";
    tryChange(notes);
  }

  bmrk.add(std::make_shared<Bookmark>(bmd));

  return 0;
}

max_stars_repo_name: alepez/bmrk

max_issues_repo_name: alepez/bmrk

max_forks_repo_name: alepez/bmrk


text:
// Copyright (C) 2018 The Regents of the University of California (Regents).
// All rights reserved.
//
// 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.
//
//     * Neither the name of The Regents or University of California nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// 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 HOLDERS 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.
//
// Please contact the author of this library if you have any questions.
// Author: Victor Fragoso (victor.fragoso@mail.wvu.edu)

#include "theia/sfm/estimators/estimate_rigid_transformation_2d_3d.h"

#include <Eigen/Core>
#include <Eigen/Geometry>
#include <memory>
#include <vector>
#include <glog/logging.h>

#include "theia/sfm/estimators/camera_and_feature_correspondence_2d_3d.h"
#include "theia/sfm/estimators/feature_correspondence_2d_3d.h"
#include "theia/sfm/pose/upnp.h"
#include "theia/sfm/rigid_transformation.h"
#include "theia/solvers/estimator.h"
#include "theia/solvers/sample_consensus_estimator.h"
#include "theia/util/util.h"

namespace theia {
namespace {

// An estimator for computing the absolute pose from 2 feature
// correspondences. The feature correspondences should be normalized by the
// focal length with the principal point at (0, 0) and rotated into the same
// coordinate system as the points.
class NonCentralCameraPoseEstimator
    : public Estimator<CameraAndFeatureCorrespondence2D3D,
                       RigidTransformation> {
 public:
  NonCentralCameraPoseEstimator() {
    estimator_.reset(new class Upnp);
  }
  ~NonCentralCameraPoseEstimator() = default;

  // Get the minimum number of samples needed to generate a model.
  virtual double SampleSize() const override { return 4; }

  // Given a set of data points, estimate the model. Users should implement this
  // function appropriately for the task being solved. Returns true for
  // successful model estimation (and outputs model), false for failed
  // estimation. Typically, this is a minimal set, but it is not required to be.
  bool EstimateModel(
      const std::vector<CameraAndFeatureCorrespondence2D3D>& data,
      std::vector<RigidTransformation>* models) const override {
    CHECK_GE(data.size(), static_cast<size_t>(SampleSize()));
    CHECK_NOTNULL(models)->clear();
    // Input datum for Upnp.
    std::vector<Eigen::Vector3d> ray_directions;
    std::vector<Eigen::Vector3d> ray_origins;
    std::vector<Eigen::Vector3d> world_points;

    // Prepare data.
    ray_directions.reserve(data.size());
    ray_origins.reserve(data.size());
    world_points.reserve(data.size());
    for (const CameraAndFeatureCorrespondence2D3D& correspondence : data) {
      ray_directions.emplace_back(correspondence.camera.PixelToUnitDepthRay(
          correspondence.observation).normalized());
      ray_origins.emplace_back(correspondence.camera.GetPosition());
      world_points.emplace_back(correspondence.point3d.hnormalized());
    }

    // Estimate pose.
    std::vector<Eigen::Quaterniond> soln_rotations;
    std::vector<Eigen::Vector3d> soln_translations;
    if (!estimator_->EstimatePose(ray_origins,
                                  ray_directions,
                                  world_points,
                                  &soln_rotations,
                                  &soln_translations)) {
      return false;
    }

    // Generate models.
    models->resize(soln_rotations.size());
    for (int i = 0; i < soln_rotations.size(); ++i) {
      models->at(i).rotation = soln_rotations[i].toRotationMatrix();
      models->at(i).translation = soln_translations[i];
    }
    return true;
  }

  // Given a model and a data point, calculate the error. Users should implement
  // this function appropriately for the task being solved.
  double Error(const CameraAndFeatureCorrespondence2D3D& data,
               const RigidTransformation& model) const override {
    Eigen::Vector2d reprojection;
    // If the point is reprojected behind the camera, return the maximum
    // possible error.
    const Eigen::Vector4d transformed_point =
        (model.rotation * data.point3d.hnormalized() +
         model.translation).homogeneous();
    if (data.camera.ProjectPoint(transformed_point, &reprojection) < 0) {
      return std::numeric_limits<double>::max();
    }

    // Return the squared reprojection error.
    return (data.observation - reprojection).squaredNorm();
  }

 private:
  // Upnp estimator.
  std::unique_ptr<class Upnp> estimator_;

  DISALLOW_COPY_AND_ASSIGN(NonCentralCameraPoseEstimator);
};

}  // namespace

bool EstimateRigidTransformation2D3D(
    const RansacParameters& ransac_params,
    const RansacType& ransac_type,
    const std::vector<CameraAndFeatureCorrespondence2D3D>& correspondences,
    RigidTransformation* estimated_pose,
    RansacSummary* ransac_summary) {
  NonCentralCameraPoseEstimator pose_estimator;
  std::unique_ptr<SampleConsensusEstimator<NonCentralCameraPoseEstimator>>
      ransac = CreateAndInitializeRansacVariant(ransac_type,
                                                ransac_params,
                                                pose_estimator);
  // Estimate the pose.
  return ransac->Estimate(correspondences,
                          estimated_pose,
                          ransac_summary);
}

bool EstimateRigidTransformation2D3D(
    const RansacParameters& ransac_params,
    const RansacType& ransac_type,
    const std::vector<FeatureCorrespondence2D3D>& normalized_correspondences,
    RigidTransformation* estimated_pose,
    RansacSummary* ransac_summary) {
  const int num_correspondences = normalized_correspondences.size();
  std::vector<CameraAndFeatureCorrespondence2D3D> correspondences(
      num_correspondences);
  for (int i = 0; i < num_correspondences; ++i) {
    correspondences[i].point3d =
        normalized_correspondences[i].world_point.homogeneous();
    correspondences[i].observation =
        normalized_correspondences[i].feature;
    correspondences[i].camera.SetPosition(Eigen::Vector3d::Zero());
    correspondences[i].camera.SetOrientationFromRotationMatrix(
        Eigen::Matrix3d::Identity());
    correspondences[i].camera.SetFocalLength(1.0);
    correspondences[i].camera.SetPrincipalPoint(0.0, 0.0);
  }
  return EstimateRigidTransformation2D3D(ransac_params,
                                         ransac_type,
                                         correspondences,
                                         estimated_pose,
                                         ransac_summary);
}

}  // namespace theia

ext: cc

max_stars_repo_name: maxchernet/TheiaSfM

max_issues_repo_name: maxchernet/TheiaSfM

max_forks_repo_name: maxchernet/TheiaSfM


text:
//  (C) Copyright Gennadiy Rozental 2001.
//  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)

//  See http://www.boost.org/libs/test for the library home page.
//
/// @file
/// @brief Defines testing result collector components
///
/// Defines classes for keeping track (@ref test_results) and collecting
/// (@ref results_collector_t) the states of the test units.
// ***************************************************************************

#ifndef BOOST_TEST_RESULTS_COLLECTOR_HPP_071894GER
#define BOOST_TEST_RESULTS_COLLECTOR_HPP_071894GER

// Boost.Test
#include <boost/test/tree/observer.hpp>

#include <boost/test/detail/global_typedef.hpp>
#include <boost/test/detail/fwd_decl.hpp>

#include <boost/test/utils/class_properties.hpp>

#include <boost/test/detail/suppress_warnings.hpp>

//____________________________________________________________________________//

namespace boost {
namespace unit_test {

namespace {

// ************************************************************************** //
/// First failed assertion debugger hook
///
/// This function is a placeholder where user can set a breakpoint in debugger to catch the
/// very first assertion failure in each test case
// ************************************************************************** //
inline void first_failed_assertion() {}
}

// ************************************************************************** //
/// @brief Collection of attributes constituting test unit results
///
/// This class is a collection of attributes describing a test result.
///
/// The attributes presented as public properties on
/// an instance of the class. In addition summary conclusion methods are presented to generate simple answer to pass/fail question

class BOOST_TEST_DECL test_results {
public:
    test_results();

    /// Type representing counter like public property
    typedef BOOST_READONLY_PROPERTY( counter_t, (results_collector_t)
                                                (test_results)
                                                (results_collect_helper) ) counter_prop;
    /// Type representing boolean like public property
    typedef BOOST_READONLY_PROPERTY( bool,      (results_collector_t)
                                                (test_results)
                                                (results_collect_helper) ) bool_prop;

    counter_prop    p_assertions_passed;        //!< Number of successful assertions
    counter_prop    p_assertions_failed;        //!< Number of failing assertions
    counter_prop    p_warnings_failed;          //!< Number of warnings
    counter_prop    p_expected_failures;
    counter_prop    p_test_cases_passed;        //!< Number of successfull test cases
    counter_prop    p_test_cases_warned;        //!< Number of warnings in test cases
    counter_prop    p_test_cases_failed;        //!< Number of failing test cases
    counter_prop    p_test_cases_skipped;       //!< Number of skipped test cases
    counter_prop    p_test_cases_aborted;       //!< Number of aborted test cases
    counter_prop    p_duration_microseconds;    //!< Duration of the test in microseconds
    bool_prop       p_aborted;                  //!< Indicates that the test unit execution has been aborted
    bool_prop       p_skipped;                  //!< Indicates that the test unit execution has been skipped

    /// Returns true if test unit passed
    bool            passed() const;

    /// Returns true if test unit skipped
    ///
    /// For test suites, this indicates if the test suite itself has been marked as
    /// skipped, and not if the test suite contains any skipped test.
    bool            skipped() const;

    /// Returns true if the test unit was aborted (hard failure)
    bool            aborted() const;

    /// Produces result code for the test unit execution
    ///
    /// This methhod return one of the result codes defined in @c boost/cstdlib.hpp
    /// @returns
    ///   - @c boost::exit_success on success,
    ///   - @c boost::exit_exception_failure in case test unit
    ///     was aborted for any reason (incuding uncaught exception)
    ///   - and @c boost::exit_test_failure otherwise
    int             result_code() const;

    //! Combines the results of the current instance with another
    //!
    //! Only the counters are updated and the @c p_aborted and @c p_skipped are left unchanged.
    void            operator+=( test_results const& );

    //! Resets the current state of the result
    void            clear();
};

// ************************************************************************** //
/// @brief Collects and combines the test results
///
/// This class collects and combines the results of the test unit during the execution of the
/// test tree. The results_collector_t::results() function combines the test results on a subtree
/// of the test tree.
///
/// @see boost::unit_test::test_observer
class BOOST_TEST_DECL results_collector_t : public test_observer {
public:

    virtual void        test_start( counter_t );

    virtual void        test_unit_start( test_unit const& );
    virtual void        test_unit_finish( test_unit const&, unsigned long );
    virtual void        test_unit_skipped( test_unit const&, const_string );
    virtual void        test_unit_aborted( test_unit const& );

    virtual void        assertion_result( unit_test::assertion_result );
    virtual void        exception_caught( execution_exception const& );

    virtual int         priority() { return 3; }

    /// Results access per test unit
    ///
    /// @param[in] tu_id id of a test unit
    test_results const& results( test_unit_id tu_id ) const;

    /// Singleton pattern
    BOOST_TEST_SINGLETON_CONS( results_collector_t )
};

BOOST_TEST_SINGLETON_INST( results_collector )

} // namespace unit_test
} // namespace boost

#include <boost/test/detail/enable_warnings.hpp>

#endif // BOOST_TEST_RESULTS_COLLECTOR_HPP_071894GER

ext: hpp


text:
#include "catch2/catch.hh"

#if defined(ENABLE_LIB_BOOST)

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>

namespace logging = boost::log;

TEST_CASE("BoostLog" , "Debug"){
    logging::core::get()->set_filter(
        logging::trivial::severity >= logging::trivial::info);

    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}
#endif

max_stars_repo_name: gwonhyeong/tlab

max_issues_repo_name: gwonhyeong/tlab

max_forks_repo_name: gwonhyeong/tlab


text:
/*

Copyright (c) 2011-2016, Arvid Norberg
All rights reserved.

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.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

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 OWNER 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.

*/

#ifndef TORRENT_THREAD_POOL
#define TORRENT_THREAD_POOL

#include "libtorrent/config.hpp"
#include "libtorrent/thread.hpp"
#include <boost/atomic.hpp>
#include <deque>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

namespace libtorrent
{

	template <class T>
	struct thread_pool
	{
		thread_pool() : m_num_threads(0) {}
		virtual ~thread_pool() {}
		void stop() { set_num_threads(0, true); }
		void set_num_threads(int n, bool wait = true)
		{
			if (n == m_num_threads) return;

			if (n > m_num_threads)
			{
				while (m_num_threads < n)
				{
					++m_num_threads;
					m_threads.push_back(boost::shared_ptr<thread>(
						new thread(boost::bind(&thread_pool::thread_fun, this, int(m_num_threads)-1))));
				}
			}
			else
			{
				while (m_num_threads > n) { --m_num_threads; }
				mutex::scoped_lock l(m_mutex);
				m_cond.notify_all();
				l.unlock();
				if (wait)
				{
					for (int i = m_num_threads; i < int(m_threads.size()); ++i)
						m_threads[i]->join();
				}
				// this will detach the threads
				m_threads.resize(m_num_threads);
			}
		}

		// returns true if the job was posted, and false if it was
		// processed immediately
		bool post_job(T& e)
		{
			if (m_num_threads == 0)
			{
				// if we don't have any worker threads
				// just do the work immediately
				process_job(e, false);
				return false;
			}
			else
			{
				retain_job(e);
				mutex::scoped_lock l(m_mutex);
				m_queue.push_back(e);
				// we only need to signal if the threads
				// may have been put to sleep. If the size
				// previous to adding the new job was > 0
				// they don't need waking up.
				if (m_queue.size() == 1)
					m_cond.notify();
				return true;
			}
		}

	protected:

		virtual void process_job(T const& j, bool post) = 0;
		virtual void retain_job(T&) {}

	private:

		void thread_fun(int thread_id)
		{
			for (;;)
			{
				mutex::scoped_lock l(m_mutex);
				while (m_queue.empty() && thread_id < m_num_threads) m_cond.wait(l);

				// if the number of wanted thread is decreased,
				// we may stop this thread
				// when we're terminating the last hasher thread (id=0), make sure
				// we finish up all queud jobs first
				if ((thread_id != 0 || m_queue.empty()) && thread_id >= m_num_threads) break;

				TORRENT_ASSERT(!m_queue.empty());
				T e = m_queue.front();
				m_queue.pop_front();
				l.unlock();

				process_job(e, true);
			}

#ifdef TORRENT_DEBUG
			if (thread_id == 0)
			{
				// when we're terminating the last hasher thread, make sure
				// there are no more scheduled jobs
				mutex::scoped_lock l(m_mutex);
				TORRENT_ASSERT(m_queue.empty());
			}
#endif
		}

		// the mutex only protects m_cond and m_queue
		// all other members are only used from a single
		// thread (the user of this class, i.e. the disk
		// thread).
		mutex m_mutex;
		condition_variable m_cond;
		std::deque<T> m_queue;

		std::vector<boost::shared_ptr<thread> > m_threads;
		// this is a counter which is atomically incremented
		// by each thread as it's started up, in order to
		// assign a unique id to each thread
		boost::atomic<int> m_num_threads;
	};

}

#endif // TORRENT_THREAD_POOL


text:
/* Copyright © 2018 Apple Inc. All rights reserved.
 *
 * Use of this source code is governed by a BSD-3-clause license that can
 * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
 */

#define BOOST_TEST_MODULE test_od_data_iterator

#include <toolkits/object_detection/od_data_iterator.hpp>

#include <boost/test/unit_test.hpp>
#include <model_server/lib/image_util.hpp>
#include <core/util/test_macros.hpp>

namespace turi {
namespace object_detection {
namespace {

using neural_net::image_box;
using neural_net::labeled_image;

constexpr size_t IMAGE_HEIGHT = 64;
constexpr size_t IMAGE_WIDTH = 128;

// Returns an SFrame with columns "test_image" and "test_annotations".
data_iterator::parameters create_data(size_t num_rows) {

  data_iterator::parameters result;

  flex_list images(num_rows);
  flex_list annotations(num_rows);

  std::vector<unsigned char> buffer(IMAGE_HEIGHT * IMAGE_WIDTH * 3);
  for (size_t i = 0; i < num_rows; ++i) {

    // Each pixel has R, G, and B value equal to the row index (modulo 256).
    std::fill(buffer.begin(), buffer.end(),
              static_cast<unsigned char>(i % 256));
    images[i] = flex_image(reinterpret_cast<char*>(buffer.data()), IMAGE_HEIGHT,
                           IMAGE_WIDTH, 3, buffer.size(),
                           IMAGE_TYPE_CURRENT_VERSION,
                           static_cast<int>(Format::RAW_ARRAY));

    // Each image has one annotation, with the label "foo" and a bounding box
    // with height and width 16. As the row index increases, the box moves to
    // the right until eventually resetting to the left and moving down.
    annotations[i] = flex_list();
    annotations[i].push_back(flex_dict({
          {"label", "foo"},
          {"coordinates", flex_dict({ {"x", 8 + i % 112},
                                      {"y", 8 + i / 112},
                                      {"width", 16},
                                      {"height", 16}      })},
        }));
  }

  result.annotations_column_name = "test_annotations";
  result.image_column_name = "test_image";
  result.data =  gl_sframe({
      {"test_image", gl_sarray(images)},
      {"test_annotations", gl_sarray(annotations)},
  });
  result.shuffle = false;

  return result;
}

// TODO: Directly test data_iterator::convert_annotations_to_yolo, checking for
// corner cases.

BOOST_AUTO_TEST_CASE(test_simple_data_iterator) {

  static constexpr size_t NUM_ROWS = 4;
  static constexpr size_t BATCH_SIZE = 4;

  data_iterator::parameters params = create_data(NUM_ROWS);

  std::vector<std::string> class_labels = { "foo" };

  simple_data_iterator data_source(params);
  TS_ASSERT_EQUALS(data_source.class_labels(), class_labels);
  TS_ASSERT_EQUALS(data_source.num_instances(), 4);

  auto assert_batch = [](const std::vector<labeled_image>& batch,
                         size_t row_offset) {
    for (size_t i = 0; i < batch.size(); ++i) {

      const labeled_image& example = batch[i];
      size_t row = (row_offset + i) % NUM_ROWS;

      TS_ASSERT_EQUALS(example.image->Height(), IMAGE_HEIGHT);
      TS_ASSERT_EQUALS(example.image->Width(), IMAGE_WIDTH);
      TS_ASSERT_EQUALS(example.image->Size(), IMAGE_HEIGHT * IMAGE_WIDTH * 3);

      // The first byte of the first pixel should contain the row index.
      std::vector<float> image_data(example.image->Size());
      example.image->WriteHWC(MakeSpan(image_data));
      TS_ASSERT_DELTA(image_data[0] * 255.f, row % 256, 0.5f);

      TS_ASSERT_EQUALS(example.annotations.size(), 1);
      TS_ASSERT_EQUALS(example.annotations[0].identifier, 0);
      TS_ASSERT_EQUALS(example.annotations[0].confidence, 1.f);
      TS_ASSERT_EQUALS(example.annotations[0].bounding_box,
                       image_box(static_cast<float>(row % 112) / IMAGE_WIDTH,
                                 static_cast<float>(row / 112) / IMAGE_HEIGHT,
                                 16.f / IMAGE_WIDTH,
                                 16.f / IMAGE_HEIGHT));
    }
  };

  size_t row_offset = 0;
  std::vector<labeled_image> batch = data_source.next_batch(BATCH_SIZE);
  TS_ASSERT_EQUALS(batch.size(), BATCH_SIZE);
  assert_batch(batch, row_offset);

  row_offset += BATCH_SIZE;
  batch = data_source.next_batch(BATCH_SIZE);
  TS_ASSERT_EQUALS(batch.size(), BATCH_SIZE);
  assert_batch(batch, 0);

}

BOOST_AUTO_TEST_CASE(test_simple_data_iterator_with_different_coordinate_systems) {

  auto create_data_opts = [](const std::string annotation_origin,
                         const std::string annotation_scale,
                         const std::string annotation_position,
                         float x, float y, float w, float h) {

    data_iterator::parameters result;

    flex_list images(1);
    flex_list annotations(1);
    std::vector<unsigned char> buffer(IMAGE_HEIGHT * IMAGE_WIDTH * 3);

    // Each pixel has R, G, and B value equal to the row index (modulo 256).
    std::fill(buffer.begin(), buffer.end(),
              static_cast<unsigned char>(0 % 256));
    images[0] = flex_image(reinterpret_cast<char*>(buffer.data()), IMAGE_HEIGHT,
                           IMAGE_WIDTH, 3, buffer.size(),
                           IMAGE_TYPE_CURRENT_VERSION,
                           static_cast<int>(Format::RAW_ARRAY));

    // Setting input for Image Origin
    if (annotation_origin == "top_left") {
        result.annotation_origin = data_iterator::annotation_origin_enum::TOP_LEFT;
    }
    if (annotation_origin == "bottom_left") {
        result.annotation_origin = data_iterator::annotation_origin_enum::BOTTOM_LEFT;
    }

    // Setting input for Annotation Scale
    if (annotation_scale == "pixel") {
        result.annotation_scale = data_iterator::annotation_scale_enum::PIXEL;
    }
    if (annotation_scale == "normalized") {
        result.annotation_scale = data_iterator::annotation_scale_enum::NORMALIZED;
    }

    // Setting input for Annotation Position
    if (annotation_position == "center") {
        result.annotation_position = data_iterator::annotation_position_enum::CENTER;
    }
    if (annotation_position == "top_left") {
        result.annotation_position = data_iterator::annotation_position_enum::TOP_LEFT;
    }
    if (annotation_position == "bottom_left") {
        result.annotation_position = data_iterator::annotation_position_enum::BOTTOM_LEFT;
    }

    // Each image has one annotation, with the label "foo" and a bounding box
    // with height and width 16. As the row index increases, the box moves to
    // the right until eventually resetting to the left and moving down.
    annotations[0] = flex_list();
    annotations[0].push_back(flex_dict({
          {"label", "foo"},
          {"coordinates", flex_dict({ {"x", x},
                                      {"y", y},
                                      {"width", w},
                                      {"height", h}      })},
        }));

    result.annotations_column_name = "test_annotations";
    result.image_column_name = "test_image";
    result.data =  gl_sframe({
        {"test_image", gl_sarray(images)},
        {"test_annotations", gl_sarray(annotations)},
    });

    result.shuffle = false;

    simple_data_iterator data_source(result);
    std::vector<labeled_image> batch = data_source.next_batch(1);
    const labeled_image& example = batch[0];

    return example;

  };

  // Case 1 | Input: ('top_left', 'pixel', 'center') → Output: ('top_left', 'normalized', 'top_left')
  const labeled_image& case1_example = create_data_opts("top_left", "pixel", "center", 28, 18, 16, 16);
  TS_ASSERT_EQUALS(case1_example.annotations[0].bounding_box,
                      image_box(static_cast<float>(20) / IMAGE_WIDTH,
                                 static_cast<float>(10) / IMAGE_HEIGHT,
                                 16.f / IMAGE_WIDTH,
                                 16.f / IMAGE_HEIGHT
                                 ));

  // Case 2 | Input: ('bottom_left', 'pixel', 'center') → Output: ('top_left',
  // 'normalized', 'top_left')
  const labeled_image& case2_example =
      create_data_opts("bottom_left", "pixel", "center", 28, 18, 16, 16);
  // A 16x16 box 10 pixels from the bottom edge begins (has its top edge at) 26
  // pixels from the bottom edge.
  TS_ASSERT_EQUALS(
      case2_example.annotations[0].bounding_box,
      image_box(static_cast<float>(20) / IMAGE_WIDTH,
                static_cast<float>(IMAGE_HEIGHT - 10 - 16) / IMAGE_HEIGHT,
                16.f / IMAGE_WIDTH, 16.f / IMAGE_HEIGHT));

  // Case 3 | Input: ('bottom_left', 'pixel', 'bottom_left') → Output:
  // ('top_left', 'normalized', 'top_left')
  const labeled_image& case3_example =
      create_data_opts("bottom_left", "pixel", "bottom_left", 20, 38, 16, 16);
  TS_ASSERT_EQUALS(case3_example.annotations[0].bounding_box,
                      image_box(static_cast<float>(20) / IMAGE_WIDTH,
                                 static_cast<float>(10) / IMAGE_HEIGHT,
                                 16.f / IMAGE_WIDTH,
                                 16.f / IMAGE_HEIGHT
                                 ));

  // Case 4 | Input: ('bottom_left', 'normalized', 'bottom_left') → Output:
  // ('top_left', 'normalized', 'top_left')
  const labeled_image& case4_example = create_data_opts(
      "bottom_left", "normalized", "bottom_left", 20. / IMAGE_WIDTH,
      38. / IMAGE_HEIGHT, 16. / IMAGE_WIDTH, 16. / IMAGE_HEIGHT);
  TS_ASSERT_EQUALS(case4_example.annotations[0].bounding_box,
                      image_box(static_cast<float>(20) / IMAGE_WIDTH,
                                 static_cast<float>(10) / IMAGE_HEIGHT,
                                 16.f / IMAGE_WIDTH,
                                 16.f / IMAGE_HEIGHT
                                 ));

  // Case 5 | Input: ('top_left', 'pixel', 'top_left') → Output: ('top_left',
  // 'normalized', 'top_left')
  const labeled_image& case5_example =
      create_data_opts("top_left", "pixel", "top_left", 20, 10, 16, 16);
  TS_ASSERT_EQUALS(case5_example.annotations[0].bounding_box,
                   image_box(static_cast<float>(20) / IMAGE_WIDTH,
                             static_cast<float>(10) / IMAGE_HEIGHT,
                             16.f / IMAGE_WIDTH, 16.f / IMAGE_HEIGHT));
}

BOOST_AUTO_TEST_CASE(test_simple_data_iterator_with_expected_classes) {

  static constexpr size_t NUM_ROWS = 1;
  static constexpr size_t BATCH_SIZE = 1;

  data_iterator::parameters params = create_data(NUM_ROWS);

  std::vector<std::string> class_labels = { "bar", "foo" };
  params.class_labels = class_labels;

  simple_data_iterator data_source(params);
  TS_ASSERT_EQUALS(data_source.class_labels(), class_labels);
  TS_ASSERT_EQUALS(data_source.num_instances(), 1);

  std::vector<labeled_image> batch = data_source.next_batch(BATCH_SIZE);
  TS_ASSERT_EQUALS(batch.size(), BATCH_SIZE);

  // Even though the data only contained one label, "foo", it should receive
  // identifier 1 because we specified the class labels upfront.
  TS_ASSERT_EQUALS(batch[0].annotations.size(), 1);
  TS_ASSERT_EQUALS(batch[0].annotations[0].identifier, 1);
}

BOOST_AUTO_TEST_CASE(test_simple_data_iterator_with_unexpected_classes) {

  static constexpr size_t NUM_ROWS = 1;

  data_iterator::parameters params = create_data(NUM_ROWS);
  params.class_labels = { "bar" };

  // The data contains the label "foo", which is not among the expected class
  // labels. SHOULDN'T throw anything.
  simple_data_iterator it_unexpected(params);
  TS_ASSERT_EQUALS(it_unexpected.class_labels().size(), 1);
}

}  // namespace
}  // namespace object_detection
}  // namespace turi

ext: cxx

max_stars_repo_name: cookingcodewithme/turicreate

max_issues_repo_name: cookingcodewithme/turicreate

max_forks_repo_name: cookingcodewithme/turicreate


text:
// Copyright (c) 2020 - present Advanced Micro Devices, Inc. All rights reserved.
//
// 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 <cmath>
#include <cstddef>
#include <iostream>
#include <sstream>

#include "./rider.h"
#include "rocfft.h"
#include <boost/program_options.hpp>
namespace po = boost::program_options;

int main(int argc, char* argv[])
{
    // This helps with mixing output of both wide and narrow characters to the screen
    std::ios::sync_with_stdio(false);

    // Control output verbosity:
    int verbose;

    // hip Device number for running tests:
    int deviceId;

    // Transform type parameters:
    rocfft_transform_type transformType;
    rocfft_array_type     itype;
    rocfft_array_type     otype;

    // Number of performance trial samples
    int ntrial;

    // Number of batches:
    size_t nbatch = 1;

    // Scale for transform
    double scale = 1.0;

    // Transform length:
    std::vector<size_t> length;

    // Transform input and output strides:
    std::vector<size_t> istride;
    std::vector<size_t> ostride;

    // Offset to start of buffer (or buffers, for planar format):
    std::vector<size_t> ioffset;
    std::vector<size_t> ooffset;

    // Input and output distances:
    size_t idist;
    size_t odist;

    // Declare the supported options.

    // clang-format doesn't handle boost program options very well:
    // clang-format off
    po::options_description opdesc("rocfft rider command line options");
    opdesc.add_options()("help,h", "produces this help message")
        ("version,v", "Print queryable version information from the rocfft library")
        ("device", po::value<int>(&deviceId)->default_value(0), "Select a specific device id")
        ("verbose", po::value<int>(&verbose)->default_value(0), "Control output verbosity")
        ("ntrial,N", po::value<int>(&ntrial)->default_value(1), "Trial size for the problem")
        ("notInPlace,o", "Not in-place FFT transform (default: in-place)")
        ("double", "Double precision transform (default: single)")
        ("transformType,t", po::value<rocfft_transform_type>(&transformType)
         ->default_value(rocfft_transform_type_complex_forward),
         "Type of transform:\n0) complex forward\n1) complex inverse\n2) real "
         "forward\n3) real inverse")
        ( "idist", po::value<size_t>(&idist)->default_value(0),
          "input distance between successive members when batch size > 1")
        ( "odist", po::value<size_t>(&odist)->default_value(0),
          "output distance between successive members when batch size > 1")
        ("scale", po::value<double>(&scale)->default_value(1.0), "Specify the scaling factor ")
        ( "batchSize,b", po::value<size_t>(&nbatch)->default_value(1),
          "If this value is greater than one, arrays will be used ")
        ( "itype", po::value<rocfft_array_type>(&itype)
          ->default_value(rocfft_array_type_unset),
          "Array type of input data:\n0) interleaved\n1) planar\n2) real\n3) "
          "hermitian interleaved\n4) hermitian planar")
        ( "otype", po::value<rocfft_array_type>(&otype)
          ->default_value(rocfft_array_type_unset),
          "Array type of output data:\n0) interleaved\n1) planar\n2) real\n3) "
          "hermitian interleaved\n4) hermitian planar")
        ("length",  po::value<std::vector<size_t>>(&length)->multitoken(), "Lengths.")
        ("istride", po::value<std::vector<size_t>>(&istride)->multitoken(), "Input strides.")
        ("ostride", po::value<std::vector<size_t>>(&ostride)->multitoken(), "Output strides.")
        ("ioffset", po::value<std::vector<size_t>>(&ioffset)->multitoken(), "Input offsets.")
        ("ooffset", po::value<std::vector<size_t>>(&ooffset)->multitoken(), "Output offsets.");
    // clang-format on

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, opdesc), vm);
    po::notify(vm);

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

    if(vm.count("version"))
    {
        char v[256];
        rocfft_get_version_string(v, 256);
        std::cout << "version " << v << std::endl;
        return 0;
    }

    if(!vm.count("length"))
    {
        std::cout << "Please specify transform length!" << std::endl;
        std::cout << opdesc << std::endl;
        return 0;
    }

    const rocfft_result_placement place
        = vm.count("notInPlace") ? rocfft_placement_notinplace : rocfft_placement_inplace;
    const rocfft_precision precision
        = vm.count("double") ? rocfft_precision_double : rocfft_precision_single;

    if(vm.count("notInPlace"))
    {
        std::cout << "out-of-place\n";
    }
    else
    {
        std::cout << "in-place\n";
    }

    if(vm.count("ntrial"))
    {
        std::cout << "Running profile with " << ntrial << " samples\n";
    }

    if(vm.count("length"))
    {
        std::cout << "length:";
        for(auto& i : length)
            std::cout << " " << i;
        std::cout << "\n";
    }

    if(vm.count("istride"))
    {
        std::cout << "istride:";
        for(auto& i : istride)
            std::cout << " " << i;
        std::cout << "\n";
    }
    if(vm.count("ostride"))
    {
        std::cout << "ostride:";
        for(auto& i : ostride)
            std::cout << " " << i;
        std::cout << "\n";
    }

    if(idist > 0)
    {
        std::cout << "idist: " << idist << "\n";
    }
    if(odist > 0)
    {
        std::cout << "odist: " << odist << "\n";
    }

    if(vm.count("ioffset"))
    {
        std::cout << "ioffset:";
        for(auto& i : ioffset)
            std::cout << " " << i;
        std::cout << "\n";
    }
    if(vm.count("ooffset"))
    {
        std::cout << "ooffset:";
        for(auto& i : ooffset)
            std::cout << " " << i;
        std::cout << "\n";
    }

    std::cout << std::flush;

    // Set default data formats if not yet specified:
    const size_t dim     = length.size();
    auto         ilength = length;
    if(transformType == rocfft_transform_type_real_inverse)
    {
        ilength[dim - 1] = ilength[dim - 1] / 2 + 1;
    }
    if(istride.size() == 0)
    {
        istride = compute_stride(ilength,
                                 1,
                                 place == rocfft_placement_inplace
                                     && transformType == rocfft_transform_type_real_forward);
    }
    auto olength = length;
    if(transformType == rocfft_transform_type_real_forward)
    {
        olength[dim - 1] = olength[dim - 1] / 2 + 1;
    }
    if(ostride.size() == 0)
    {
        ostride = compute_stride(olength,
                                 1,
                                 place == rocfft_placement_inplace
                                     && transformType == rocfft_transform_type_real_inverse);
    }
    check_set_iotypes(place, transformType, itype, otype);
    if(idist == 0)
    {
        idist = set_idist(place, transformType, length, istride);
    }
    if(odist == 0)
    {
        odist = set_odist(place, transformType, length, ostride);
    }

    if(verbose > 0)
    {
        std::cout << "FFT  params:\n";
        std::cout << "\tilength:";
        for(auto i : ilength)
            std::cout << " " << i;
        std::cout << "\n";
        std::cout << "\tistride:";
        for(auto i : istride)
            std::cout << " " << i;
        std::cout << "\n";
        std::cout << "\tidist: " << idist << std::endl;

        std::cout << "\tolength:";
        for(auto i : olength)
            std::cout << " " << i;
        std::cout << "\n";
        std::cout << "\tostride:";
        for(auto i : ostride)
            std::cout << " " << i;
        std::cout << "\n";
        std::cout << "\todist: " << odist << std::endl;
    }

    // Create column-major parameters for rocFFT:
    auto length_cm  = length;
    auto istride_cm = istride;
    auto ostride_cm = ostride;
    for(int idx = 0; idx < dim / 2; ++idx)
    {
        const auto toidx = dim - idx - 1;
        std::swap(istride_cm[idx], istride_cm[toidx]);
        std::swap(ostride_cm[idx], ostride_cm[toidx]);
        std::swap(length_cm[idx], length_cm[toidx]);
    }

    // Create FFT description
    rocfft_plan_description desc = NULL;
    LIB_V_THROW(rocfft_plan_description_create(&desc), "rocfft_plan_description_create failed");
    LIB_V_THROW(rocfft_plan_description_set_data_layout(desc,
                                                        itype,
                                                        otype,
                                                        ioffset.data(),
                                                        ooffset.data(),
                                                        istride_cm.size(),
                                                        istride_cm.data(),
                                                        idist,
                                                        ostride_cm.size(),
                                                        ostride_cm.data(),
                                                        odist),
                "rocfft_plan_description_data_layout failed");

    // Create the plan
    rocfft_plan plan = NULL;
    rocfft_plan_create(
        &plan, place, transformType, precision, length_cm.size(), length_cm.data(), nbatch, desc);

    // Get work buffer size and allocated info-associated work buffer is necessary
    size_t workBufferSize = 0;
    LIB_V_THROW(rocfft_plan_get_work_buffer_size(plan, &workBufferSize),
                "rocfft_plan_get_work_buffer_size failed");
    rocfft_execution_info info = NULL;
    LIB_V_THROW(rocfft_execution_info_create(&info), "rocfft_execution_info_create failed");
    void* wbuffer = NULL;
    {
        HIP_V_THROW(hipMalloc(&wbuffer, workBufferSize), "Creating intermediate Buffer failed");
        LIB_V_THROW(rocfft_execution_info_set_work_buffer(info, wbuffer, workBufferSize),
                    "rocfft_execution_info_set_work_buffer failed");
    }

    // Input data:
    const auto input = compute_input(precision, itype, length, istride, idist, nbatch);

    if(verbose > 1)
    {
        std::cout << "GPU input:\n";
        printbuffer(precision, itype, input, ilength, istride, nbatch, idist);
    }

    // GPU input and output buffers:
    std::vector<void*> ibuffer = alloc_buffer(precision, itype, idist, nbatch);
    std::vector<void*> obuffer = (place == rocfft_placement_inplace)
                                     ? ibuffer
                                     : alloc_buffer(precision, otype, odist, nbatch);

    // Run the transform several times and record the execution time:
    std::vector<double> gpu_time(ntrial);

    hipEvent_t start, stop;
    HIP_V_THROW(hipEventCreate(&start), "hipEventCreate failed");
    HIP_V_THROW(hipEventCreate(&stop), "hipEventCreate failed");
    for(int itrial = 0; itrial < gpu_time.size(); ++itrial)
    {

        // Copy the input data to the GPU:
        for(int idx = 0; idx < input.size(); ++idx)
        {
            HIP_V_THROW(
                hipMemcpy(
                    ibuffer[idx], input[idx].data(), input[idx].size(), hipMemcpyHostToDevice),
                "hipMemcpy failed");
        }

        HIP_V_THROW(hipEventRecord(start), "hipEventRecord failed");

        rocfft_execute(plan, ibuffer.data(), obuffer.data(), info);

        HIP_V_THROW(hipEventRecord(stop), "hipEventRecord failed");
        HIP_V_THROW(hipEventSynchronize(stop), "hipEventSynchronize failed");

        float time;
        hipEventElapsedTime(&time, start, stop);
        gpu_time[itrial] = time;

        if(verbose > 2)
        {
            auto output = allocate_host_buffer(precision, otype, olength, ostride, odist, nbatch);
            for(int idx = 0; idx < output.size(); ++idx)
            {
                hipMemcpy(
                    output[idx].data(), obuffer[idx], output[idx].size(), hipMemcpyDeviceToHost);
            }
            std::cout << "GPU output:\n";
            printbuffer(precision, otype, output, olength, ostride, nbatch, odist);
        }
    }

    std::cout << "\nExecution gpu time:";
    for(const auto& i : gpu_time)
    {
        std::cout << " " << i;
    }
    std::cout << " ms" << std::endl;

    std::cout << "Execution gflops:  ";
    const double totsize
        = std::accumulate(length.begin(), length.end(), 1, std::multiplies<size_t>());
    const double k
        = ((itype == rocfft_array_type_real) || (otype == rocfft_array_type_real)) ? 2.5 : 5.0;
    const double opscount = (double)nbatch * k * totsize * log(totsize) / log(2.0);
    for(const auto& i : gpu_time)
    {
        std::cout << " " << opscount / (1e6 * i);
    }
    std::cout << std::endl;

    // Clean up:
    rocfft_plan_description_destroy(desc);
    rocfft_execution_info_destroy(info);
    rocfft_plan_destroy(plan);
    hipFree(wbuffer);
    for(auto& buf : ibuffer)
        hipFree(buf);
    for(auto& buf : obuffer)
        hipFree(buf);
}

max_stars_repo_name: eidenyoshida/rocFFT

max_issues_repo_name: eidenyoshida/rocFFT

max_forks_repo_name: eidenyoshida/rocFFT


text:
#define BOOST_TEST_MODULE UnitTests
#define BOOST_TEST_DYN_LINK

#include <fstream>
#include <iostream>
#include <iterator>

#include <boost/test/unit_test.hpp>

#include <boost/filesystem.hpp>
#include <files.h>

#include "utils.h"

BOOST_AUTO_TEST_SUITE(files)

BOOST_AUTO_TEST_CASE(recursive_search)
{
	using namespace boost::filesystem;

	utils::scoped_directory test_root(temp_directory_path() / unique_path());

	path f1 = test_root.path() / "file1.txt";
	path f2 = test_root.path() / "file2.txt";

	utils::scoped_directory subdir1(test_root.path() / "directory1");
	path f3 = subdir1.path() / "file3.txt";
	path f4 = subdir1.path() / "file4.txt";

	std::vector<path> files { f1, f2, f3, f4 };

	for (const path& p: files) {
		std::ofstream f(p.native());
		f << "";
	}

	std::vector<std::string> result = zubr::files::find_all_files(test_root.path().native(), "");

	BOOST_REQUIRE_EQUAL(4, result.size());
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: tzdybal/wisentr

max_issues_repo_name: tzdybal/wisentr

max_forks_repo_name: tzdybal/wisentr


text:
/*
 *
 * 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.)
 *
 * See http://www.boost.org/libs/iostreams for documentation.
 *
 * Defines the classes operation_sequence and operation, in the namespace
 * boost::iostreams::test, for verifying that all elements of a sequence of
 * operations are executed, and that they are executed in the correct order.
 *
 * File:        libs/iostreams/test/detail/operation_sequence.hpp
 * Date:        Mon Dec 10 18:58:19 MST 2007
 * Copyright:   2007-2008 CodeRage, LLC
 * Author:      Jonathan Turkanis
 * Contact:     turkanis at coderage dot com
 */

#ifndef BOOST_IOSTREAMS_TEST_OPERATION_SEQUENCE_HPP_INCLUDED
#define BOOST_IOSTREAMS_TEST_OPERATION_SEQUENCE_HPP_INCLUDED

#include <boost/config.hpp>  // make sure size_t is in namespace std
#include <cstddef>
#include <climits>
#include <map>
#include <stdexcept>
#include <string>
#include <utility>  // pair
#include <vector>
#include <boost/lexical_cast.hpp>
#include <boost/preprocessor/iteration/local.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/weak_ptr.hpp>

#ifndef BOOST_IOSTREAMS_TEST_MAX_OPERATION_ERROR
# define BOOST_IOSTREAMS_TEST_MAX_OPERATION_ERROR 20
#endif

#define BOOST_CHECK_OPERATION_SEQUENCE(seq) \
    BOOST_CHECK_MESSAGE(seq.is_success(), seq.message()) \
    /**/

namespace boost { namespace iostreams { namespace test {

// Simple exception class with error code built in to type
template<int Code>
struct operation_error { };

class operation_sequence;

// Represent an operation in a sequence of operations to be executed
class operation {
public:
    friend class operation_sequence;
    operation() : pimpl_() { }
    void execute();
private:
    static void remove_operation(operation_sequence& seq, int id);

    struct impl {
        impl(operation_sequence& seq, int id, int error_code = -1)
            : seq(seq), id(id), error_code(error_code)
            { }
        ~impl() { remove_operation(seq, id); }
        impl& operator=(const impl&); // Supress VC warning 4512
        operation_sequence&  seq;
        int                  id;
        int                  error_code;
    };
    friend struct impl;

    operation(operation_sequence& seq, int id, int error_code = -1)
        : pimpl_(new impl(seq, id, error_code))
        { }

    shared_ptr<impl> pimpl_;
};

// Represents a sequence of operations to be executed in a particular order
class operation_sequence {
public:
    friend class operation;
    operation_sequence() { reset(); }

    //
    // Returns a new operation.
    // Parameters:
    //
    //   id - The operation id, determining the position
    //        of the new operation in the operation sequence
    //   error_code - If supplied, indicates that the new
    //        operation will throw operation_error<error_code>
    //        when executed. Must be an integer between 0 and
    //        BOOST_IOSTREAMS_TEST_MAX_OPERATION_ERROR,
    //        inclusive.
    //
    operation new_operation(int id, int error_code = INT_MAX);

    bool is_success() const { return success_; }
    bool is_failure() const { return failed_; }
    std::string message() const;
    void reset();
private:
    void execute(int id);
    void remove_operation(int id);
    operation_sequence(const operation_sequence&);
    operation_sequence& operator=(const operation_sequence&);

    typedef weak_ptr<operation::impl>  ptr_type;
    typedef std::map<int, ptr_type>    map_type;

    map_type          operations_;
    std::vector<int>  log_;
    std::size_t       total_executed_;
    int               last_executed_;
    bool              success_;
    bool              failed_;
};

//--------------Implementation of operation-----------------------------------//

void operation::execute()
{
    pimpl_->seq.execute(pimpl_->id);
    switch (pimpl_->error_code) {

    // Implementation with one or more cleanup operations
    #define BOOST_PP_LOCAL_MACRO(n) \
    case n: throw operation_error<n>(); \
    /**/

    #define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_TEST_MAX_OPERATION_ERROR)
    #include BOOST_PP_LOCAL_ITERATE()
    #undef BOOST_PP_LOCAL_MACRO

    default:
        break;
    }
}

inline void operation::remove_operation(operation_sequence& seq, int id)
{
    seq.remove_operation(id);
}

//--------------Implementation of operation_sequence--------------------------//

inline operation operation_sequence::new_operation(int id, int error_code)
{
    using namespace std;
    if ( error_code < 0 ||
         error_code > BOOST_IOSTREAMS_TEST_MAX_OPERATION_ERROR &&
             error_code != INT_MAX )
    {
        throw runtime_error( string("The error code ") +
                             lexical_cast<string>(error_code) +
                             " is out of range" );
    }
    if (last_executed_ != INT_MIN)
        throw runtime_error( "Operations in progress; call reset() "
                             "before creating more operations" );
    map_type::const_iterator it = operations_.find(id);
    if (it != operations_.end())
        throw runtime_error( string("The operation ") +
                             lexical_cast<string>(id) +
                             " already exists" );
    operation op(*this, id, error_code);
    operations_.insert(make_pair(id, ptr_type(op.pimpl_)));
    return op;
}

inline std::string operation_sequence::message() const
{
    using namespace std;
    if (success_)
        return "success";
    std::string msg = failed_ ?
        "operations occurred out of order: " :
        "operation sequence is incomplete: ";
    typedef vector<int>::size_type size_type;
    for (size_type z = 0, n = log_.size(); z < n; ++z) {
        msg += lexical_cast<string>(log_[z]);
        if (z < n - 1)
            msg += ',';
    }
    return msg;
}

inline void operation_sequence::reset()
{
    log_.clear();
    total_executed_ = 0;
    last_executed_ = INT_MIN;
    success_ = false;
    failed_ = false;
}

inline void operation_sequence::execute(int id)
{
    log_.push_back(id);
    if (!failed_ && last_executed_ < id) {
        if (++total_executed_ == operations_.size())
            success_ = true;
        last_executed_ = id;
    } else {
        success_ = false;
        failed_ = true;
    }
}

inline void operation_sequence::remove_operation(int id)
{
    using namespace std;
    map_type::iterator it = operations_.find(id);
    if (it == operations_.end())
        throw runtime_error( string("No such operation: ") +
                             lexical_cast<string>(id) );
    operations_.erase(it);
}

} } } // End namespace boost::iostreams::test.

#endif // #ifndef BOOST_IOSTREAMS_TEST_OPERATION_SEQUENCE_HPP_INCLUDED