text:
#include "stun/optimizer.h"
#include "config/load.h"
#include <boost/ut.hpp>

using namespace boost::ut;

suite optimizer = [] {
    "Optimizer type traits"_test = [] {
        using angonoka::stun::Optimizer;
        expect(std::is_nothrow_destructible_v<Optimizer>);
        expect(!std::is_default_constructible_v<Optimizer>);
        expect(std::is_copy_constructible_v<Optimizer>);
        expect(std::is_copy_assignable_v<Optimizer>);
        expect(std::is_nothrow_move_constructible_v<Optimizer>);
        expect(std::is_nothrow_move_assignable_v<Optimizer>);
    };

    "basic Optimizer operations"_test = [] {
        using namespace angonoka::stun;

        // clang-format off
        constexpr auto text =
            "agents:\n"
            "  Bob:\n"
            "  Jack:\n"
            "tasks:\n"
            "  - name: Task 1\n"
            "    duration: 1h\n"
            "  - name: Task 2\n"
            "    duration: 1h";
        // clang-format on
        const auto config = angonoka::load_text(text);

        const auto params = to_schedule_params(config);
        Optimizer optimizer{params, BatchSize{5}, MaxIdleIters{10}};

        expect(optimizer.normalized_makespan() == 2.F);
        expect(optimizer.estimated_progress() == 0.F);
        expect(optimizer.schedule()[1].agent_id == 0);

        optimizer.update();

        expect(optimizer.estimated_progress() == 0.F);

        while (!optimizer.has_converged()) optimizer.update();

        // Might be non-deterministic
        expect(optimizer.normalized_makespan() == 1.F);
        expect(optimizer.estimated_progress() == 1.F);
        // Each task has a different agent
        expect(
            optimizer.schedule()[1].agent_id
            != optimizer.schedule()[0].agent_id);

        should("reset") = [&] {
            optimizer.reset();

            expect(optimizer.normalized_makespan() == 2.F);
            expect(optimizer.estimated_progress() == 0.F);
        };

        should("rebind params") = [&] {
            expect(&optimizer.params() == &params);
            const auto params2 = params;
            optimizer.params(params2);
            expect(&optimizer.params() == &params2);

            while (!optimizer.has_converged()) optimizer.update();
        };
    };

    "Optimizer special memeber functions"_test = [] {
        using namespace angonoka::stun;

        // clang-format off
        constexpr auto text =
            "agents:\n"
            "  Bob:\n"
            "  Jack:\n"
            "tasks:\n"
            "  - name: Task 1\n"
            "    duration: 1h\n"
            "  - name: Task 2\n"
            "    duration: 1h";
        // clang-format on
        const auto config = angonoka::load_text(text);

        const auto params = to_schedule_params(config);

        should("copy ctor") = [&] {
            Optimizer optimizer{
                params,
                BatchSize{5},
                MaxIdleIters{10}};
            Optimizer other{optimizer};

            expect(other.normalized_makespan() == 2.F);

            while (!optimizer.has_converged()) optimizer.update();

            expect(other.normalized_makespan() == 2.F);
        };

        should("copy assignment") = [&] {
            Optimizer optimizer{
                params,
                BatchSize{5},
                MaxIdleIters{10}};
            Optimizer other{params, BatchSize{5}, MaxIdleIters{10}};
            other = optimizer;

            expect(other.normalized_makespan() == 2.F);

            while (!optimizer.has_converged()) optimizer.update();

            expect(other.normalized_makespan() == 2.F);

            while (!other.has_converged()) other.update();
            expect(other.normalized_makespan() == 1.F);

            {
                Optimizer optimizer2{
                    params,
                    BatchSize{5},
                    MaxIdleIters{10}};
                other = optimizer2;
            }

            expect(other.normalized_makespan() == 2.F);
        };

        should("move ctor") = [&] {
            Optimizer optimizer{
                params,
                BatchSize{5},
                MaxIdleIters{10}};
            Optimizer other{std::move(optimizer)};

            expect(other.normalized_makespan() == 2.F);

            while (!other.has_converged()) other.update();

            expect(other.normalized_makespan() == 1.F);
        };

        should("move assignment") = [&] {
            Optimizer optimizer{
                params,
                BatchSize{5},
                MaxIdleIters{10}};
            Optimizer other{params, BatchSize{5}, MaxIdleIters{10}};
            other = std::move(optimizer);

            expect(other.normalized_makespan() == 2.F);

            while (!other.has_converged()) other.update();

            expect(other.normalized_makespan() == 1.F);
        };

        should("destructive move assignment") = [&] {
            Optimizer optimizer{
                params,
                BatchSize{5},
                MaxIdleIters{10}};

            expect(optimizer.normalized_makespan() == 2.F);

            {
                Optimizer other{
                    params,
                    BatchSize{5},
                    MaxIdleIters{10}};
                optimizer = std::move(other);
            }

            expect(optimizer.normalized_makespan() == 2.F);

            while (!optimizer.has_converged()) optimizer.update();

            expect(optimizer.normalized_makespan() == 1.F);
        };

        should("self copy") = [&] {
            Optimizer optimizer{
                params,
                BatchSize{5},
                MaxIdleIters{10}};
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wself-assign-overloaded"
            optimizer = optimizer;
#pragma clang diagnostic pop

            expect(optimizer.normalized_makespan() == 2.F);
        };

        // self move is not supported
    };
};

max_stars_repo_name: coffee-lord/angonoka

max_issues_repo_name: coffee-lord/angonoka

max_forks_repo_name: coffee-lord/angonoka


text:
/*

Copyright (c) 2005-2017, University of Oxford.
All rights reserved.

University of Oxford means the Chancellor, Masters and Scholars of the
University of Oxford, having an administrative office at Wellington
Square, Oxford OX1 2JD, UK.

This file is part of Chaste.

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 University of Oxford 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 HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef CorriasBuistSMCModified_HPP_
#define CorriasBuistSMCModified_HPP_

#include "ChasteSerialization.hpp"
#include <boost/serialization/base_object.hpp>
#include "AbstractCardiacCell.hpp"
#include "AbstractStimulusFunction.hpp"
/**
 * This class is a modified version of the model of a gastric
 * Smooth Muscle Cell.
 *
 * Reference publication is:
 *
 * Corrias A, Buist ML.
 * "A quantitative model of gastric smooth muscle cellular activation."
 * Ann Biomed Eng. 2007 Sep;35(9):1595-607. Epub 2007 May 8.
 *
 * Modifications include:
 * - ability to include/exclude built-in fake ICC stimulus
 * - ability to set K+ channels-affecting CO concentrations
 */
class CorriasBuistSMCModified : public AbstractCardiacCell
{
    friend class boost::serialization::access;
    /**
     * Boost Serialization method for archiving/checkpointing.
     * Archives the object and its member variables.
     *
     * @param archive  The boost archive.
     * @param version  The current version of this class.
     */
    template<class Archive>
    void serialize(Archive & archive, const unsigned int version)
    {
        archive & boost::serialization::base_object<AbstractCardiacCell >(*this);
    }

private:

    /**
     * Scale factor for CO-affected currents
     * Note that this the number that multiply the currents, hence it is not [CO],
     * but a function of [CO] (for example, 2.8*[CO] - 0.1)
     */
    double mScaleFactorCarbonMonoxide;

    /**
     * True if the fake built-in ICC stimulus is present
     */
    bool mFakeIccStimulusPresent;

    double Cm;/**< membrane capacitance, pF*/

    double Asurf_in_cm_square;/**< Surface area in cm^2*/
    double Asurf;/**< surface area (mm^2)*/

    double VolCell;/**< cell volume (mm^3)*/
    double hCa;/**< conc for half inactivation of fCa */
    double sCa;/**< lope factor for inactivation of fCa */

    /* concentrations */
    double Ki;       /**< intra K conc (mM)*/
    double Nai;        /**< intra Na conc (mM)*/
    double ACh;       /**< acetylcholine conc (mM)*/
    double CaiRest;     /**< baseline Ca conc (mM)*/

    /* maximum conductances*/
    double gLVA_max; /**< max conductance of ILVA*/                 // (0.18 nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gCaL_max; /**< max conductance of ICaL*/                 // (65.0 nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gBK_max; /**< max conductance of IBK)*/                 // (45.7 nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gKb_max; /**< max conductance of IKb*/                  // (0.0144 nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gKA_max; /**< max conductance of IKA*/                  // (9.0  nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gKr_max; /**< max conductance of IKr*/                  // (35.0 nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gNa_max;  /**< max conductance of INa*/                  // (3.0  nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gnsCC_max; /**< max conductance of InsCC*/                // (50.0 nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double gcouple;   /**<  coupling conductance bewteen fake ICC and SMC*/        // 1.3 nS * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2
    double JCaExt_max;     /**< max flux of CaSR (mM/ms)*/

    /* Temperature corrections */
    double Q10Ca;         /**< (dim)*/
    double Q10K;         /**< (dim)*/ //1.365
    double Q10Na;        /**< (dim)*/
    double Texp;       /**< (degK)*/

    double T_correct_Ca ;/**< temperature correction for Ca (dim)*/
    double T_correct_K ;  /**< temperature correction for K (dim)*/
    double T_correct_Na;/**< temperature correction for Na (dim)*/
    double T_correct_gBK; /**< temperature correction for gBK*/  // (nS) * 1e-6 (mS/nS) / Asurf (mm2) = mS/mm2

    /* Nernst potentials */
    double EK;                  /**< Nernst potential for K (mV)*/
    double ENa ;               /**< Nernst potential for Na (mV)*/
    double EnsCC;                          /**< Nernst potential for nsCC (mV)*/

    double Ca_o;         /**<  mM */
    double K_o;          /**< mM */
    double Na_o;         /**< mM */

    /* Nernst parameters */
    double R;    /**<  pJ/nmol/K*/
    double T;       /**<  degK*/
    double F;     /**<  nC/nmol*/
    double FoRT;     /**<  1/mV*/
    double RToF;    /**<  mV*/

public:

    /**
     * Constructor
     *
     * @param pSolver is a pointer to the ODE solver
     * @param pIntracellularStimulus is a pointer to the intracellular stimulus
     */
    CorriasBuistSMCModified(boost::shared_ptr<AbstractIvpOdeSolver> pSolver, boost::shared_ptr<AbstractStimulusFunction> pIntracellularStimulus);

    /**
     * Destructor
     */
    ~CorriasBuistSMCModified();

    /**
     * Now empty
     */
    void VerifyStateVariables();

    /**
     * Calculates the ionic current
     *
     * @param pStateVariables the state variables of this model
     * @return the total ionic current
     */
    double GetIIonic(const std::vector<double>* pStateVariables=NULL);

    /**
     * Compute the RHS of the FitHugh-Nagumo system of ODEs
     *
     * @param time  the current time, in milliseconds
     * @param rY  current values of the state variables
     * @param rDY  to be filled in with derivatives
     */
    void EvaluateYDerivatives(double time, const std::vector<double>& rY, std::vector<double>& rDY);

    /**
     * Set whether we want the fake ICC stimulus or not.
     * It changes the member variable mFakeIccStimulusPresent (which is true by default).
     *
     * @param present - true if we want the fake ICC stimulus, false otherwise
     */
    void SetFakeIccStimulusPresent(bool present);

    /**
     * @return true if the fake ICC stimulus is present
     */
    bool GetFakeIccStimulusPresent();

    /**
     * @return the Carbon Monoxide scale for
     */
    double SetCarbonMonoxideScaleFactor();

    /**
     * Set the carbon monoxide scale factor.
     * This will multiply the following currents: I_kr, I_Ka, Ibk
     *
     * @param scaleFactor the scale factor that multiply the currents.
     */
    void SetCarbonMonoxideScaleFactor(double scaleFactor);

    /**
     * @return the Carbon Monoxide scale factor
     */
    double GetCarbonMonoxideScaleFactor();
};

// Needs to be included last
#include "SerializationExportWrapper.hpp"
CHASTE_CLASS_EXPORT(CorriasBuistSMCModified)

namespace boost
{
    namespace serialization
    {
        template<class Archive>
        inline void save_construct_data(
            Archive & ar, const CorriasBuistSMCModified * t, const unsigned int fileVersion)
        {
            const boost::shared_ptr<AbstractIvpOdeSolver> p_solver = t->GetSolver();
            const boost::shared_ptr<AbstractStimulusFunction> p_stimulus = t->GetStimulusFunction();
            ar << p_solver;
            ar << p_stimulus;
        }

        template<class Archive>
        inline void load_construct_data(
            Archive & ar, CorriasBuistSMCModified * t, const unsigned int fileVersion)
        {
            boost::shared_ptr<AbstractIvpOdeSolver> p_solver;
            boost::shared_ptr<AbstractStimulusFunction> p_stimulus;
            ar >> p_solver;
            ar >> p_stimulus;
            ::new(t)CorriasBuistSMCModified(p_solver, p_stimulus);
        }
    }
}

#endif // CorriasBuistSMCModified_HPP_

ext: hpp

max_stars_repo_name: gonayl/Chaste

max_issues_repo_name: gonayl/Chaste

max_forks_repo_name: gonayl/Chaste


text:
/**
 * @file
 * Defines the `coverage_Graph`, a graph data structure containing:
 *  - Sequence nodes (`coverage_Node`). Each node has:
 *      - Nucleotide sequence
 *      - Outgoing edges (pointers) to other nodes
 *      - Coverage array to store per base coverage for each node
 *      - Site and allele ID
 *      - A position which refers to that in the original Multiple Sequence
 * Alignment
 *  - A bubble map (`coverage_Graph::bubble_map`), used to order the variant
 * sites for both coverage recording and genotyping
 *  - A parental map (`coverage_Graph::par_map`), used for recording grouped
 * allele counts coverage
 *  - A target map (`coverage_Graph::target_map), used to place new
 * `gram::SearchState`s at variant sites during quasimap.
 *  - A random access array (`coverage_Graph::random_access`) used to place a
 * mapped instance in the graph for per base coverage recording.
 */
#ifndef COV_GRAPH_HPP
#define COV_GRAPH_HPP

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/make_shared.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/vector.hpp>

#include "linearised_prg.hpp"
#include "prg/types.hpp"

using namespace gram;

/**
 * The building blocks of a `coverage_Graph`
 * Contain sequence, site & allele ID, coverage array
 */
class coverage_Node {
 public:
  coverage_Node();

  coverage_Node(std::size_t pos);

  coverage_Node(std::string const seq, int const pos, int const site_ID = 0,
                int const allele_ID = ALLELE_UNKNOWN);

  /**
   * Compare pointers to `coverage_Node`; used in topological ordering (lastmost
   * sequence position first) Equivalence in a set is defined using this, so we
   * also test whether the pointers are the same objects.
   */
  friend bool operator>(const covG_ptr& lhs, const covG_ptr& rhs);

  friend bool compare_nodes(coverage_Node const& f, coverage_Node const& s);

  friend bool operator==(coverage_Node const& f, coverage_Node const& s);

  friend std::ostream& operator<<(std::ostream& out, coverage_Node const& node);

  bool has_sequence() const { return sequence.size() != 0; }
  bool is_in_bubble() const {
    return allele_ID != ALLELE_UNKNOWN && site_ID != 0;
  }
  bool is_bubble_start() const {
    return next.size() > 1 && sequence.size() == 0;
  }
  bool is_bubble_end() const {
    return next.size() == 1 && sequence.size() == 0;
  }

  /*
   * Getters
   */
  std::size_t get_pos() const { return pos; }
  std::string get_sequence() const { return sequence; }
  std::size_t get_sequence_size() const { return sequence.size(); }
  int get_coverage_space() const { return coverage.size(); }
  PerBaseCoverage const& get_coverage() const { return coverage; }
  PerBaseCoverage& get_ref_to_coverage() { return coverage; }
  Marker get_site_ID() const { return site_ID; }
  AlleleId get_allele_ID() const { return allele_ID; }
  std::vector<covG_ptr> const& get_edges() const { return next; }
  std::size_t get_num_edges() const { return next.size(); }

  /*
   * Setters
   */
  void set_pos(std::size_t pos) { this->pos = pos; }
  void mark_as_boundary() { is_site_boundary = true; }
  void set_coverage(PerBaseCoverage const& new_cov) {
    assert(new_cov.size() == coverage.size() &&
           new_cov.size() == sequence.size());
    coverage = new_cov;
  }

  void add_sequence(std::string const& new_seq);
  void add_edge(covG_ptr const target) { next.emplace_back(target); }
  void clear_edges() { next.clear(); }

 private:
  std::string sequence;
  Marker site_ID;
  AlleleId allele_ID;
  std::size_t pos;
  PerBaseCoverage coverage;
  bool is_site_boundary;
  std::vector<covG_ptr> next;

  // Boost serialisation
  friend class boost::serialization::access;
  template <typename Archive>
  void serialize(Archive& ar, const unsigned int version) {
    ar& sequence;
    ar& site_ID;
    ar& allele_ID;
    ar& pos;
    ar& coverage;
    ar& is_site_boundary;
    ar& next;  // Array of shared pointers, needs custom includes
  }
};

enum class marker_type { sequence, site_entry, allele_end, site_end };

struct node_access {
  covG_ptr node;        // The referred to node in the `coverage_Graph`
  std::size_t offset;   // The character's offset relative to the start of the
                        // `coverage_Node` it belongs to
  VariantLocus target;  // If the preceding character is a variant marker, gives
                        // what it is.
 private:
  // Boost serialisation
  friend class boost::serialization::access;
  template <typename Archive>
  void serialize(Archive& ar, const unsigned int version) {
    ar& node;
    ar& offset;
    ar& target;
  }
};

struct targeted_marker {
  Marker ID{0};
  AlleleId direct_deletion_allele{
      ALLELE_UNKNOWN};  // ALLELE_UNKNOWN if not a direct deletion
  friend bool operator==(targeted_marker const& f, targeted_marker const& s);

 private:
  // Boost serialisation
  friend class boost::serialization::access;
  template <typename Archive>
  void serialize(Archive& ar, const unsigned int version) {
    ar& ID;
    ar& direct_deletion_allele;
  }
};

/**
 * This class implements a DAG of `coverage_Node`s.
 * It is used to record coverage & to perform genotyping in gramtools.
 **/
class coverage_Graph {
 public:
  covG_ptr root;

  coverage_Graph() = default;

  coverage_Graph(coverage_Graph&) = default;
  coverage_Graph(coverage_Graph&&) = default;
  coverage_Graph& operator=(coverage_Graph const&) = default;
  coverage_Graph& operator=(coverage_Graph&&) =
      default;  // Needs to be declared, because user-defined destructor exists.

  ~coverage_Graph();

  /**
   * Build a coverage graph from a PRG String int vector.
   */
  coverage_Graph(PRG_String const& vec_in);

  /** Maps the start of a local bubble, to its end.
   * Children nodes appear before parent nodes.
   * Use : genotyping
   */
  covG_ptr_map bubble_map;

  /**
   * Maps a site ID to a Locus which is its immediate parent in the graph.
   * Only populated for bubbles nested inside another bubble.
   * Use : equivalence class coverage recording
   */
  parental_map par_map;

  /**
   * A vector of the same size as the PRG string, giving access to the
   * corresponding node in the graph. Use : per base coverage recording
   */
  access_vec random_access;

  /**
   * Map from a variant marker to all variant markers it is directly linked to.
   * Note the following convention: the target marker is even if it signals a
   * site entry (in backward searching), and odd if it signals a site exit. Ie,
   * all allele markers that are not the last one of a site get marked as site
   * (odd) markers. Use: read mapping
   */
  target_m target_map;

  bool is_nested{false}; /**< Upon construction, gets set to true if graph has
                            nested bubbles */

  friend bool operator==(coverage_Graph const& f, coverage_Graph const& s);

 private:
  // Boost serialisation
  friend class boost::serialization::access;
  template <typename Archive>
  void serialize(Archive& ar, const unsigned int version) {
    /* Important: bubble_map should serialise before root.
     Indeed, `ar & root` recursively serialises all nodes from the root,
     which can overflow the stack and segfault for large graphs.
     Whereas bubble_map is ordered by 'rightmost' node first, so serialisation
     Is less recursive (note serialisation of the same pointer twice is ignored
     by boost)
     */
    ar& bubble_map;
    ar& root;
    ar& par_map;
    ar& random_access;
    ar& target_map;
    ar& is_nested;
  }
};

/**
 * A class in charge of mechanics of building coverage graph
 * It is designed for use by DEVELOPER only
 */
class cov_Graph_Builder {
 public:
  /*
   * All that's needed to make the object is under here
   */
  cov_Graph_Builder(PRG_String const& prg_string);

  cov_Graph_Builder() = default;

  // These will get transferred to the coverage_Graph
  covG_ptr root;
  covG_ptr_map bubble_map;
  parental_map par_map;
  access_vec random_access;
  target_m target_map;

  void make_root(); /**< Start state: set up the globals such as `cur_Node` &
                       `backWire` */
  void make_sink(); /**< End state: final wiring & pointers to null */
  /**
   * Function call dispatcher based on marker at position @param pos.
   * Called once per element in `PRG_String`.
   * @param pos index into the `PRG_String`
   */
  void process_marker(uint32_t const& pos);
  void setup_random_access(uint32_t const& pos);
  void add_sequence(Marker const& m);
  marker_type find_marker_type(uint32_t const& pos);
  void enter_site(Marker const& m);
  void end_allele(Marker const& m);
  void exit_site(Marker const& m);
  /**
   * A convenience function for reaching the end of an allele: called by both
   * `end_allele` & `exit_site`
   */
  covG_ptr reach_allele_end(Marker const& m);

  void wire(covG_ptr const& target); /** Build 1 or 2 edges depending on whether
                                        `cur_Node` contains sequence. */

  /**
   * Makes the map of marker targets used during mapping
   */
  void map_targets();
  void make_site_entry_target(marker_type prev_t, Marker prev_m, Marker cur_m);
  void make_site_exit_target(marker_type prev_t, Marker prev_m, Marker cur_m,
                             AlleleId cur_allele_ID);
  void make_allele_end_target(marker_type prev_t, Marker prev_m, Marker cur_m,
                              AlleleId cur_allele_ID);
  /**
   * Site exit points can point to different variant markers (site start & end)
   * Here we test for prior presence of the allele marker in the `target_map`
   * and insert or update accordingly
   */
  void add_exit_target(Marker cur_m, targeted_marker const new_t_m);

  /*
   * variables & data structures
   */
  marker_vec linear_prg;
  std::unordered_map<Marker, int> end_positions;

  covG_ptr backWire;  // Pointer to the most recent node needing edge building
  covG_ptr cur_Node;

  // For assigning position to nodes
  std::size_t cur_pos{0};
  bool first_allele{false};

  VariantLocus cur_Locus;  // For building parental map

  marker_to_node bubble_starts;
  marker_to_node bubble_ends;
};

#endif  // COV_GRAPH_HPP

max_stars_repo_name: bricoletc/gramtools

max_issues_repo_name: bricoletc/gramtools

max_forks_repo_name: bricoletc/gramtools


text:
#include <bits/types/FILE.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <tgmath.h>
#include "image_ppm.h"
#include <filesystem>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

typedef Matrix<u_char, Dynamic, Dynamic> MatrixImg;
typedef Vector<u_char,Dynamic> ImgLine;
typedef Matrix<ImgLine,Dynamic,Dynamic> FlattenedImages;

unsigned char max(u_char a, u_char b){
    if (a<b) return b;
    else return a;
}

unsigned char min(u_char a, u_char b){
    if (a>b) return b;
    else return a;
}

unsigned char LBPvalue(OCTET* image, int x, int y, int nW, int nH){
    int count =0;
    int* binaryNumber = new int[8];

    for(int i =-1;i<2;i++){
        for (int j=-1;j<2;j++){
            if (i!=0 && j!=0){
                if (image[(x+i)*nW+j+y]<image[x*nW+y]){
                    binaryNumber[count]=0;
                }
                if (image[(x+i)*nW+j+y]>=image[x*nW+y]){
                    binaryNumber[count]=1;
                }
                count++;
            }
        }
    }
    unsigned char res = (unsigned char)(1*binaryNumber[7] + 2*binaryNumber[6] + 4*binaryNumber[5] + 8*binaryNumber[4] + 16*binaryNumber[3] + 32*binaryNumber[2] + 64*binaryNumber[1] + 128*binaryNumber[0]);
    return (res);
}

double minutie(int* hist, int size,int nH, int nW, int gridX, int gridY){
    double res =0.0;
    for (int i =0;i<size;i++){
        res+=i*(double)hist[i]/((double)(nW*nH)/(double)(gridY*gridX));//(double)size;
    }
    return res;
}

double divergenceLK(int* hist1, int* hist2, int size){
    double res = 0.0;
    for (int k=0;k<size;k++){
        res+=(hist1[k]+1.0)*log((hist1[k]+1.0)/(hist2[k]+1.0));
    }
    return res;
}

int main(){

}

ext: cpp

max_stars_repo_name: JPhilippot/FaceRecognition

max_issues_repo_name: JPhilippot/FaceRecognition

max_forks_repo_name: JPhilippot/FaceRecognition


text:
#ifndef  BOOST_SERIALIZATION_DETAIL_IS_DEFAULT_CONSTRUCTIBLE_HPP
#define BOOST_SERIALIZATION_DETAIL_IS_DEFAULT_CONSTRUCTIBLE_HPP

// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# pragma once
#endif

/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// is_default_constructible.hpp: serialization for loading stl collections
//
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org for updates, documentation, and revision history.

#include <boost/config.hpp>

#if ! defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
    #include <type_traits>
    namespace boost{
    namespace serialization {
    namespace detail {

    template<typename T>
    struct is_default_constructible : public std::is_default_constructible<T> {};

    } // detail
    } // serializaition
    } // boost
#else
    // we don't have standard library support for is_default_constructible
    // so we fake it by using boost::has_trivial_construtor.  But this is not
    // actually correct because it's possible that a default constructor
    // to be non trivial. So when using this, make sure you're not using your
    // own definition of of T() but are using the actual default one!
    #include <boost/type_traits/has_trivial_constructor.hpp>
    namespace boost{
    namespace serialization {
    namespace detail {

    template<typename T>
    struct is_default_constructible : public boost::has_trivial_constructor<T> {};

    } // detail
    } // serializaition
    } // boost

#endif

#endif //  BOOST_SERIALIZATION_DETAIL_IS_DEFAULT_CONSTRUCTIBLE_HPP


text:
/**
 * @file    FileUtilities.cpp
 * @author  Marvin Smith
 * @date    1/11/2014
*/
#include "FilesystemUtilities.hpp"

#include <cstdio>
#include <cstdlib>
#include <iostream>

#include <boost/filesystem.hpp>

namespace bf=boost::filesystem;

/**
 * Run a standard system command and grab the output
*/
bool run_command( const std::string& command, std::string& message ){

    /// create output message
    message="";

    // run the command on the file descriptor
    FILE* pipe = popen(command.c_str(), "r");
    if( !pipe ){
        message = "command failed to run.";
        return false;
    }
    else{

        // create our output
        char buffer[128];

        // iterate over the stream output
        while(!feof(pipe)) {
            if(fgets(buffer, 128, pipe) != NULL)
                message += buffer;
        }
        pclose(pipe);

    }

    return true;
}

/**
 * Check if a file exists
 */
bool file_exists( const std::string& filename ){
    return bf::exists( bf::path( filename ));
}

/**
 * Create a directory if does not exist.
 */
void path_mkdir( const boost::filesystem::path& pathname ){
    bf::create_directories( pathname );
}

max_stars_repo_name: marvins/LLNMS

max_issues_repo_name: marvins/LLNMS

max_forks_repo_name: marvins/LLNMS


text:
//==================================================================================================
/*!

  Copyright 2015 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//==================================================================================================
#include <boost/simd/constant/log_2lo.hpp>
#include <boost/simd/constant/log_2hi.hpp>
#include <boost/simd/constant/log_2.hpp>
#include <boost/simd/constant/eps.hpp>
#include <boost/simd/as.hpp>
#include <simd_test.hpp>

STF_CASE_TPL( "Check log_2lo behavior for integral types"
            , (std::uint32_t)(std::uint64_t)
              (std::int32_t)(std::int64_t)
            )
{
  using boost::simd::as;
  using boost::simd::detail::log_2lo;
  using boost::simd::Log_2lo;
  T ref = T(0);
  STF_TYPE_IS(decltype(Log_2lo<T>()), T);
  STF_EQUAL(Log_2lo<T>(), ref);
  STF_EQUAL(log_2lo( as(T{}) ),ref);
}

STF_CASE_TPL( "Check log_2lo behavior for double"
            , (double)
            )
{
  using boost::simd::as;
  using boost::simd::detail::log_2lo;
  using boost::simd::Log_2lo;
  T ref = T(1.9082149292705877e-10);
  #ifdef HAS_LONG_DOUBLE
  using boost::simd::Log_2hi;
  using boost::simd::Eps;
  long double L_2 = 0.693147180559945309417232121458;
  STF_LESS((double)((long double)(Log_2lo<double>())+(long double)(Log_2hi<double>()))- L_2, Eps<double>()/2);
  #endif
  STF_TYPE_IS(decltype(Log_2lo<T>()), T);
  STF_IEEE_EQUAL(Log_2lo<T>(), ref);
  STF_IEEE_EQUAL(log_2lo( as(T{}) ), ref);
}

STF_CASE_TPL( "Check log_2lo behavior for float"
            , (float)
            )
{
  using boost::simd::as;
  using boost::simd::detail::log_2lo;
  using boost::simd::Log_2lo;
  using boost::simd::Log_2hi;
  using boost::simd::Log_2;
  using boost::simd::Eps;
  STF_LESS(float(double(Log_2lo<float>())+double(Log_2hi<float>())- Log_2<double>()), Eps<float>());
  T ref = T(-2.1219444170128554e-4);
  STF_TYPE_IS(decltype(Log_2lo<T>()), T);
  STF_IEEE_EQUAL(Log_2lo<T>(), ref);
  STF_IEEE_EQUAL(log_2lo( as(T{}) ), ref);
}


text:
// Software License for MTL
//
// Copyright (c) 2007 The Trustees of Indiana University.
//               2008 Dresden University of Technology and the Trustees of Indiana University.
//               2010 SimuNova UG (haftungsbeschränkt), www.simunova.com.
// All rights reserved.
// Authors: Peter Gottschling and Andrew Lumsdaine
//
// This file is part of the Matrix Template Library
//
// See also license.mtl.txt in the distribution.

#include <boost/numeric/mtl/mtl.hpp>
# include "boost/rational.hpp"
# include "boost/range.hpp"

typedef boost::rational<long>                              t_Q;
typedef mtl::dense_vector<t_Q, mtl::vec::parameters<> > t_dVecQ;

//! Struct containing data about the refinment of a face.
struct st_test
{
    t_dVecQ vecQ;

    //! Constructor for refinement of a triangle.
    st_test( const t_dVecQ& vecQ_new )
    {
        vecQ = vecQ_new;
        std::cout << "vecQ: " << vecQ << "\n";       // uses size -> ambiguity with size in boost/range
        std::cout << mtl::size(vecQ_new) << "\n";
    }
};

// using test as function name causes ambiguities  on some compilers
// (type_traits/has_new_operator.hpp:24: error: ‘template<class U, U x> struct boost::detail::test' is not a function)
void assign_test( const t_dVecQ& vecQ )
{
    t_dVecQ vecQ_temp;
    vecQ_temp = vecQ;
    std::cout << "vecQ_temp: " << vecQ_temp << "\n";
}

void test2()
{
   t_dVecQ vQ0(2,3);
    t_dVecQ vQ1(2,2);
    t_dVecQ vQ2(2,1);
    std::cout << "vQ0: " << vQ0 << "\n";
    std::cout << "vQ1: " << vQ1 << "\n";
    std::cout << "size(vQ1): " << mtl::size(vQ1) << "\n";
    vQ0 = vQ1 + vQ2;
    vQ0 = vQ1 - vQ2;
    vQ0 += vQ1;
    vQ0 -= vQ1;
    std::cout << "vQ0: " << vQ0 << "\n";
    t_dVecQ vQ3( vQ0 - vQ1 );
    std::cout << "vQ2: " << vQ2 << "\n";
    std::cout << "size(vQ2): " << mtl::size(vQ2) << "\n";
}

int main(int , char**)
{
    t_Q Q0(1,3);
    std::cout << "Q0: " << Q0 << "\n";

    t_dVecQ vecQ0(2,Q0);
    std::cout << "vecQ0: " << vecQ0 << "\n";

    t_Q Q1(1,2);
    std::cout << "Q1: " << Q1 << "\n";
    t_dVecQ vecQ1(3,Q1);
    std::cout << "vecQ1: " << vecQ1 << "\n";
    vecQ1[mtl::irange(2)] = vecQ0;
    std::cout << "vecQ1: " << vecQ1 << "\n";

    assign_test(vecQ1);
    st_test test(vecQ1);

    // std::cout << "size(vecQ1): " << mtl::size(vecQ1) << "\n";

    test2();

    return 0;
}


text:
#ifdef _USE_PCL_
#include "LidarRegistration.h"
#include <pcl/registration/icp.h>           //ICP类相关头文件
#include <pcl/registration/icp_nl.h>        //非线性ICP 相关头文件
#include <pcl/registration/transforms.h>      //变换矩阵类头文件

#include <pcl/features/fpfh.h>
#include <pcl/registration/ia_ransac.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <boost/thread/thread.hpp>
#include <pcl/features/fpfh_omp.h> //包含fpfh加速计算的omp(多核并行计算)
#include <pcl/registration/correspondence_estimation.h>
#include <pcl/registration/correspondence_rejection_features.h> //特征的错误对应关系去除
#include <pcl/registration/correspondence_rejection_sample_consensus.h> //随机采样一致性去除

long LidarRegistration::LidarRegistration_ICP(pcl::PointCloud<pcl::PointXYZ>::Ptr ref_cloud,
                          pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud,
                          const char* pathRegistration)
{
    pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp;   //创建IterativeClosestPoint的对象
    icp.setInputCloud(input_cloud);                                 //cloud_in设置为点云的源点
    icp.setInputTarget(ref_cloud);                                  //cloud_out设置为与cloud_in对应的匹配目标
    icp.setMaxCorrespondenceDistance(800);
    icp.setMaximumIterations(30);

    pcl::PointCloud<pcl::PointXYZ> registration;                    //存储经过配准变换点云后的点云
    icp.align(registration);                                        //打印配准相关输入信息
    std::cout << "has converged:" << icp.hasConverged() <<" score: " <<icp.getFitnessScore() << std::endl;
    std::cout << icp.getFinalTransformation() << std::endl;

    //配准点云保存到文件中
    pcl::io::savePCDFileASCII (pathRegistration, registration); //将点云保存到PCD文件中
    return 0;
}

long LidarRegistration::LidarRegistration_FPFH(pcl::PointCloud<pcl::PointXYZ>::Ptr ref_cloud,
                          pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud,
                          const char* pathRegistration)
{
    clock_t start,end,time;
    start  = clock();
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());

    pcl::PointCloud<pcl::FPFHSignature33>::Ptr source_fpfh =  compute_fpfh_feature(input_cloud,tree);
    pcl::PointCloud<pcl::FPFHSignature33>::Ptr target_fpfh =  compute_fpfh_feature(ref_cloud,tree);

    //对齐(占用了大部分运行时间)
    pcl::SampleConsensusInitialAlignment<pcl::PointXYZ,pcl::PointXYZ,pcl::FPFHSignature33> sac_ia;
    sac_ia.setInputSource(input_cloud);
    sac_ia.setSourceFeatures(source_fpfh);
    sac_ia.setInputTarget(ref_cloud);
    sac_ia.setTargetFeatures(target_fpfh);
    pcl::PointCloud<pcl::PointXYZ>::Ptr align (new pcl::PointCloud<pcl::PointXYZ>());
    //sac_ia.setNumberOfSamples(20);  //设置每次迭代计算中使用的样本数量（可省）,可节省时间
    sac_ia.setCorrespondenceRandomness(6); //设置计算协方差时选择多少近邻点，该值越大，协防差越精确，但是计算效率越低.(可省)
    sac_ia.align(*align);
    end = clock();
    pcl::io::savePCDFile (pathRegistration, *align);
    printf("calculate time is: %lf\n",float (end-start)/CLOCKS_PER_SEC);
}

pcl::PointCloud<pcl::FPFHSignature33>::Ptr LidarRegistration::compute_fpfh_feature(pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud,pcl::search::KdTree<pcl::PointXYZ>::Ptr tree)
{
    //法向量
    pcl::PointCloud<pcl::Normal>::Ptr point_normal (new  pcl::PointCloud<pcl::Normal>());
    pcl::NormalEstimation<pcl::PointXYZ,pcl::Normal> est_normal;
    est_normal.setInputCloud(input_cloud);
    est_normal.setSearchMethod(tree);
    est_normal.setKSearch(10);
    est_normal.compute(*point_normal);
    //fpfh 估计
    pcl::PointCloud<pcl::FPFHSignature33>::Ptr fpfh (new pcl::PointCloud<pcl::FPFHSignature33>);
    //pcl::FPFHEstimation<pcl::PointXYZ,pcl::Normal,pcl::FPFHSignature33> est_target_fpfh;
    pcl::FPFHEstimationOMP<pcl::PointXYZ,pcl::Normal,pcl::FPFHSignature33> est_fpfh;
    est_fpfh.setNumberOfThreads(4); //指定4核计算
    // pcl::search::KdTree<pcl::PointXYZ>::Ptr tree4 (new pcl::search::KdTree<pcl::PointXYZ> ());
    est_fpfh.setInputCloud(input_cloud);
    est_fpfh.setInputNormals(point_normal);
    est_fpfh.setSearchMethod(tree);
    est_fpfh.setKSearch(10);
    est_fpfh.compute(*fpfh);

    return fpfh;
}

#endif


text:
#include "Manager.h"
#include "Timer.h"
#include "SearchNormal.h"
#include "SentenceStats.h"

#include <boost/foreach.hpp>

using namespace std;

namespace Moses
{
/**
 * Organizing main function
 *
 * /param source input sentence
 * /param transOptColl collection of translation options to be used for this sentence
 */
SearchNormal::
SearchNormal(Manager& manager, const TranslationOptionCollection &transOptColl)
  : Search(manager)
  , m_hypoStackColl(manager.GetSource().GetSize() + 1)
  , m_transOptColl(transOptColl)
{
  VERBOSE(1, "Translating: " << m_source << endl);

  // initialize the stacks: create data structure and set limits
  std::vector < HypothesisStackNormal >::iterator iterStack;
  for (size_t ind = 0 ; ind < m_hypoStackColl.size() ; ++ind) {
    HypothesisStackNormal *sourceHypoColl = new HypothesisStackNormal(m_manager);
    sourceHypoColl->SetMaxHypoStackSize(this->m_options.search.stack_size,
                                        this->m_options.search.stack_diversity);
    sourceHypoColl->SetBeamWidth(this->m_options.search.beam_width);
    m_hypoStackColl[ind] = sourceHypoColl;
  }
}

SearchNormal::~SearchNormal()
{
  RemoveAllInColl(m_hypoStackColl);
}

bool
SearchNormal::
ProcessOneStack(HypothesisStack* hstack)
{
  if (this->out_of_time()) return false;
  SentenceStats &stats = m_manager.GetSentenceStats();
  HypothesisStackNormal &sourceHypoColl
  = *static_cast<HypothesisStackNormal*>(hstack);

  // the stack is pruned before processing (lazy pruning):
  VERBOSE(3,"processing hypothesis from next stack");
  IFVERBOSE(2) stats.StartTimeStack();
  sourceHypoColl.PruneToSize(m_options.search.stack_size);
  VERBOSE(3,std::endl);
  sourceHypoColl.CleanupArcList();
  IFVERBOSE(2)  stats.StopTimeStack();

  // go through each hypothesis on the stack and try to expand it
  // BOOST_FOREACH(Hypothesis* h, sourceHypoColl)
  HypothesisStackNormal::const_iterator h;
  for (h = sourceHypoColl.begin(); h != sourceHypoColl.end(); ++h)
    ProcessOneHypothesis(**h);
  return true;
}

/**
 * Main decoder loop that translates a sentence by expanding
 * hypotheses stack by stack, until the end of the sentence.
 */
void SearchNormal::Decode()
{
  // initial seed hypothesis: nothing translated, no words produced
  const Bitmap &initBitmap = m_bitmaps.GetInitialBitmap();
  Hypothesis *hypo = new Hypothesis(m_manager, m_source, m_initialTransOpt, initBitmap, m_manager.GetNextHypoId());

  m_hypoStackColl[0]->AddPrune(hypo);

  // go through each stack
  BOOST_FOREACH(HypothesisStack* hstack, m_hypoStackColl) {
    if (!ProcessOneStack(hstack)) return;
    IFVERBOSE(2) OutputHypoStackSize();
    actual_hypoStack = static_cast<HypothesisStackNormal*>(hstack);
  }
}

/** Find all translation options to expand one hypothesis, trigger expansion
 * this is mostly a check for overlap with already covered words, and for
 * violation of reordering limits.
 * \param hypothesis hypothesis to be expanded upon
 */
void
SearchNormal::
ProcessOneHypothesis(const Hypothesis &hypothesis)
{
  // since we check for reordering limits, its good to have that limit handy
  bool isWordLattice = m_source.GetType() == WordLatticeInput;

  const Bitmap &hypoBitmap = hypothesis.GetWordsBitmap();
  const size_t hypoFirstGapPos = hypoBitmap.GetFirstGapPos();
  size_t const sourceSize = m_source.GetSize();

  ReorderingConstraint const&
  ReoConstraint = m_source.GetReorderingConstraint();

  // no limit of reordering: only check for overlap
  if (m_options.reordering.max_distortion < 0) {

    for (size_t startPos = hypoFirstGapPos ; startPos < sourceSize ; ++startPos) {
      TranslationOptionList const* tol;
      size_t endPos = startPos;
      for (tol = m_transOptColl.GetTranslationOptionList(startPos, endPos);
           tol && endPos < sourceSize;
           tol = m_transOptColl.GetTranslationOptionList(startPos, ++endPos)) {
        if (tol->size() == 0
            || hypoBitmap.Overlap(Range(startPos, endPos))
            || !ReoConstraint.Check(hypoBitmap, startPos, endPos)) {
          continue;
        }

        //TODO: does this method include incompatible WordLattice hypotheses?
        ExpandAllHypotheses(hypothesis, startPos, endPos);
      }
    }
    return; // done with special case (no reordering limit)
  }

  // There are reordering limits. Make sure they are not violated.

  Range prevRange = hypothesis.GetCurrSourceWordsRange();
  for (size_t startPos = hypoFirstGapPos ; startPos < sourceSize ; ++startPos) {

    // don't bother expanding phrases if the first position is already taken
    if(hypoBitmap.GetValue(startPos)) continue;

    size_t maxSize = sourceSize - startPos;
    size_t maxSizePhrase = m_options.search.max_phrase_length;
    maxSize = (maxSize < maxSizePhrase) ? maxSize : maxSizePhrase;
    size_t closestLeft = hypoBitmap.GetEdgeToTheLeftOf(startPos);

    if (isWordLattice) {
      // first question: is there a path from the closest translated word to the left
      // of the hypothesized extension to the start of the hypothesized extension?
      // long version:
      // - is there anything to our left?
      // - is it farther left than where we're starting anyway?
      // - can we get to it?

      // closestLeft is exclusive: a value of 3 means 2 is covered, our
      // arc is currently ENDING at 3 and can start at 3 implicitly
      if (closestLeft != 0 && closestLeft != startPos
          && !m_source.CanIGetFromAToB(closestLeft, startPos))
        continue;

      if (prevRange.GetStartPos() != NOT_FOUND &&
          prevRange.GetStartPos() > startPos &&
          !m_source.CanIGetFromAToB(startPos, prevRange.GetStartPos()))
        continue;
    }

    Range currentStartRange(startPos, startPos);
    if(m_source.ComputeDistortionDistance(prevRange, currentStartRange)
        > m_options.reordering.max_distortion)
      continue;

    TranslationOptionList const* tol;
    size_t endPos = startPos;
    for (tol = m_transOptColl.GetTranslationOptionList(startPos, endPos);
         tol && endPos < sourceSize;
         tol = m_transOptColl.GetTranslationOptionList(startPos, ++endPos)) {
      Range extRange(startPos, endPos);
      if (tol->size() == 0
          || hypoBitmap.Overlap(extRange)
          || !ReoConstraint.Check(hypoBitmap, startPos, endPos)
          || (isWordLattice && !m_source.IsCoveragePossible(extRange))) {
        continue;
      }

      // ask second question here: we already know we can get to our
      // starting point from the closest thing to the left. We now ask the
      // follow up: can we get from our end to the closest thing on the
      // right?
      //
      // long version: is anything to our right? is it farther
      // right than our (inclusive) end? can our end reach it?
      bool isLeftMostEdge = (hypoFirstGapPos == startPos);

      size_t closestRight = hypoBitmap.GetEdgeToTheRightOf(endPos);
      if (isWordLattice) {
        if (closestRight != endPos
            && ((closestRight + 1) < sourceSize)
            && !m_source.CanIGetFromAToB(endPos + 1, closestRight + 1)) {
          continue;
        }
      }

      if (isLeftMostEdge) {
        // any length extension is okay if starting at left-most edge
        ExpandAllHypotheses(hypothesis, startPos, endPos);
      } else { // starting somewhere other than left-most edge, use caution
        // the basic idea is this: we would like to translate a phrase
        // starting from a position further right than the left-most
        // open gap. The distortion penalty for the following phrase
        // will be computed relative to the ending position of the
        // current extension, so we ask now what its maximum value will
        // be (which will always be the value of the hypothesis starting
        // at the left-most edge).  If this value is less than the
        // distortion limit, we don't allow this extension to be made.
        Range bestNextExtension(hypoFirstGapPos, hypoFirstGapPos);

        if (m_source.ComputeDistortionDistance(extRange, bestNextExtension)
            > m_options.reordering.max_distortion) continue;

        // everything is fine, we're good to go
        ExpandAllHypotheses(hypothesis, startPos, endPos);
      }
    }
  }
}

/**
 * Expand a hypothesis given a list of translation options
 * \param hypothesis hypothesis to be expanded upon
 * \param startPos first word position of span covered
 * \param endPos last word position of span covered
 */

void
SearchNormal::
ExpandAllHypotheses(const Hypothesis &hypothesis, size_t startPos, size_t endPos)
{
  // early discarding: check if hypothesis is too bad to build
  // this idea is explained in (Moore&Quirk, MT Summit 2007)
  float expectedScore = 0.0f;

  const Bitmap &sourceCompleted = hypothesis.GetWordsBitmap();
  float estimatedScore = m_transOptColl.GetEstimatedScores().CalcEstimatedScore( sourceCompleted, startPos, endPos );

  const Range &hypoRange = hypothesis.GetCurrSourceWordsRange();
  //cerr << "DOING " << sourceCompleted << " [" << hypoRange.GetStartPos() << " " << hypoRange.GetEndPos() << "]"
  //		  " [" << startPos << " " << endPos << "]" << endl;

  if (m_options.search.UseEarlyDiscarding()) {
    // expected score is based on score of current hypothesis
    expectedScore = hypothesis.GetScore();

    // add new future score estimate
    expectedScore += estimatedScore;
  }

  // loop through all translation options
  const TranslationOptionList* tol
  = m_transOptColl.GetTranslationOptionList(startPos, endPos);
  if (!tol || tol->size() == 0) return;

  // Create new bitmap
  const TranslationOption &transOpt = **tol->begin();
  const Range &nextRange = transOpt.GetSourceWordsRange();
  const Bitmap &nextBitmap = m_bitmaps.GetBitmap(sourceCompleted, nextRange);

  TranslationOptionList::const_iterator iter;
  for (iter = tol->begin() ; iter != tol->end() ; ++iter) {
    const TranslationOption &transOpt = **iter;
    ExpandHypothesis(hypothesis, transOpt, expectedScore, estimatedScore, nextBitmap);
  }
}

/**
 * Expand one hypothesis with a translation option.
 * this involves initial creation, scoring and adding it to the proper stack
 * \param hypothesis hypothesis to be expanded upon
 * \param transOpt translation option (phrase translation)
 *        that is applied to create the new hypothesis
 * \param expectedScore base score for early discarding
 *        (base hypothesis score plus future score estimation)
 */
void SearchNormal::ExpandHypothesis(const Hypothesis &hypothesis,
                                    const TranslationOption &transOpt,
                                    float expectedScore,
                                    float estimatedScore,
                                    const Bitmap &bitmap)
{
  SentenceStats &stats = m_manager.GetSentenceStats();

  Hypothesis *newHypo;
  if (! m_options.search.UseEarlyDiscarding()) {
    // simple build, no questions asked
    IFVERBOSE(2) {
      stats.StartTimeBuildHyp();
    }
    newHypo = new Hypothesis(hypothesis, transOpt, bitmap, m_manager.GetNextHypoId());
    IFVERBOSE(2) {
      stats.StopTimeBuildHyp();
    }
    if (newHypo==NULL) return;

    IFVERBOSE(2) {
      m_manager.GetSentenceStats().StartTimeOtherScore();
    }
    newHypo->EvaluateWhenApplied(estimatedScore);
    IFVERBOSE(2) {
      m_manager.GetSentenceStats().StopTimeOtherScore();

      // TODO: these have been meaningless for a while.
      // At least since commit 67fb5c
      // should now be measured in SearchNormal.cpp:254 instead, around CalcFutureScore2()
      // CalcFutureScore2() also called in BackwardsEdge::Initialize().
      //
      // however, CalcFutureScore2() should be quick
      // since it uses dynamic programming results in SquareMatrix
      m_manager.GetSentenceStats().StartTimeEstimateScore();
      m_manager.GetSentenceStats().StopTimeEstimateScore();
    }
  } else
    // early discarding: check if hypothesis is too bad to build
  {
    // worst possible score may have changed -> recompute
    size_t wordsTranslated = hypothesis.GetWordsBitmap().GetNumWordsCovered() + transOpt.GetSize();
    float allowedScore = m_hypoStackColl[wordsTranslated]->GetWorstScore();
    if (m_options.search.stack_diversity) {
      WordsBitmapID id = hypothesis.GetWordsBitmap().GetIDPlus(transOpt.GetStartPos(), transOpt.GetEndPos());
      float allowedScoreForBitmap = m_hypoStackColl[wordsTranslated]->GetWorstScoreForBitmap( id );
      allowedScore = std::min( allowedScore, allowedScoreForBitmap );
    }
    allowedScore += m_options.search.early_discarding_threshold;

    // add expected score of translation option
    expectedScore += transOpt.GetFutureScore();

    // check if transOpt score push it already below limit
    if (expectedScore < allowedScore) {
      IFVERBOSE(2) {
        stats.AddNotBuilt();
      }
      return;
    }

    // build the hypothesis without scoring
    IFVERBOSE(2) {
      stats.StartTimeBuildHyp();
    }
    newHypo = new Hypothesis(hypothesis, transOpt, bitmap, m_manager.GetNextHypoId());
    if (newHypo==NULL) return;
    IFVERBOSE(2) {
      stats.StopTimeBuildHyp();
    }

    // ... and check if that is below the limit
    if (expectedScore < allowedScore) {
      IFVERBOSE(2) {
        stats.AddEarlyDiscarded();
      }
      delete newHypo;
      return;
    }

  }

  // logging for the curious
  IFVERBOSE(3) {
    newHypo->PrintHypothesis();
  }

  // add to hypothesis stack
  size_t wordsTranslated = newHypo->GetWordsBitmap().GetNumWordsCovered();
  IFVERBOSE(2) {
    stats.StartTimeStack();
  }
  m_hypoStackColl[wordsTranslated]->AddPrune(newHypo);
  IFVERBOSE(2) {
    stats.StopTimeStack();
  }
}

const std::vector < HypothesisStack* >& SearchNormal::GetHypothesisStacks() const
{
  return m_hypoStackColl;
}

/**
 * Find best hypothesis on the last stack.
 * This is the end point of the best translation, which can be traced back from here
 */
const Hypothesis *SearchNormal::GetBestHypothesis() const
{
  if (interrupted_flag == 0) {
    const HypothesisStackNormal &hypoColl = *static_cast<HypothesisStackNormal*>(m_hypoStackColl.back());
    return hypoColl.GetBestHypothesis();
  } else {
    const HypothesisStackNormal &hypoColl = *actual_hypoStack;
    return hypoColl.GetBestHypothesis();
  }
}

/**
 * Logging of hypothesis stack sizes
 */
void SearchNormal::OutputHypoStackSize()
{
  std::vector < HypothesisStack* >::const_iterator iterStack = m_hypoStackColl.begin();
  TRACE_ERR( "Stack sizes: " << (int)(*iterStack)->size());
  for (++iterStack; iterStack != m_hypoStackColl.end() ; ++iterStack) {
    TRACE_ERR( ", " << (int)(*iterStack)->size());
  }
  TRACE_ERR( endl);
}

void SearchNormal::OutputHypoStack()
{
  // all stacks
  int i = 0;
  vector < HypothesisStack* >::iterator iterStack;
  for (iterStack = m_hypoStackColl.begin() ; iterStack != m_hypoStackColl.end() ; ++iterStack) {
    HypothesisStackNormal &hypoColl = *static_cast<HypothesisStackNormal*>(*iterStack);
    TRACE_ERR( "Stack " << i++ << ": " << endl << hypoColl << endl);
  }
}

}


text:
#include <stan/math/prim/mat.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/math/distributions.hpp>

using Eigen::Dynamic;
using Eigen::Matrix;
using std::vector;

TEST(ProbDistributionsMultiNormal,NotVectorized) {
  Matrix<double,Dynamic,1> y(3,1);
  y << 2.0, -2.0, 11.0;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 1.0, -1.0, 3.0;
  Matrix<double,Dynamic,Dynamic> Sigma(3,3);
  Sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 0.0,
    0.0, 0.0, 5.0;
  EXPECT_FLOAT_EQ(-11.73908, stan::math::multi_normal_log(y,mu,Sigma));
}

TEST(ProbDistributionsMultiNormal,Vectorized) {
  vector< Matrix<double,Dynamic,1> > vec_y(2);
  vector< Matrix<double,1,Dynamic> > vec_y_t(2);
  Matrix<double,Dynamic,1> y(3);
  Matrix<double,1,Dynamic> y_t(3);
  y << 2.0, -2.0, 11.0;
  vec_y[0] = y;
  vec_y_t[0] = y;
  y << 4.0, -2.0, 1.0;
  vec_y[1] = y;
  vec_y_t[1] = y;
  y_t = y;

  vector< Matrix<double,Dynamic,1> > vec_mu(2);
  vector< Matrix<double,1,Dynamic> > vec_mu_t(2);
  Matrix<double,Dynamic,1> mu(3);
  Matrix<double,1,Dynamic> mu_t(3);
  mu << 1.0, -1.0, 3.0;
  vec_mu[0] = mu;
  vec_mu_t[0] = mu;
  mu << 2.0, -1.0, 4.0;
  vec_mu[1] = mu;
  vec_mu_t[1] = mu;
  mu_t = mu;

  Matrix<double,Dynamic,Dynamic> Sigma(3,3);
  Sigma << 10.0, -3.0, 0.0,
    -3.0,  5.0, 0.0,
    0.0, 0.0, 5.0;

  //y and mu vectorized
  EXPECT_FLOAT_EQ(-11.928077-6.5378327, stan::math::multi_normal_log(vec_y,vec_mu,Sigma));
  EXPECT_FLOAT_EQ(-11.928077-6.5378327, stan::math::multi_normal_log(vec_y_t,vec_mu,Sigma));
  EXPECT_FLOAT_EQ(-11.928077-6.5378327, stan::math::multi_normal_log(vec_y,vec_mu_t,Sigma));
  EXPECT_FLOAT_EQ(-11.928077-6.5378327, stan::math::multi_normal_log(vec_y_t,vec_mu_t,Sigma));

  //y vectorized
  EXPECT_FLOAT_EQ(-10.44027-6.537833, stan::math::multi_normal_log(vec_y,mu,Sigma));
  EXPECT_FLOAT_EQ(-10.44027-6.537833, stan::math::multi_normal_log(vec_y_t,mu,Sigma));
  EXPECT_FLOAT_EQ(-10.44027-6.537833, stan::math::multi_normal_log(vec_y,mu_t,Sigma));
  EXPECT_FLOAT_EQ(-10.44027-6.537833, stan::math::multi_normal_log(vec_y_t,mu_t,Sigma));

  //mu vectorized
  EXPECT_FLOAT_EQ(-6.26954-6.537833, stan::math::multi_normal_log(y,vec_mu,Sigma));
  EXPECT_FLOAT_EQ(-6.26954-6.537833, stan::math::multi_normal_log(y_t,vec_mu,Sigma));
  EXPECT_FLOAT_EQ(-6.26954-6.537833, stan::math::multi_normal_log(y,vec_mu_t,Sigma));
  EXPECT_FLOAT_EQ(-6.26954-6.537833, stan::math::multi_normal_log(y_t,vec_mu_t,Sigma));
}
TEST(ProbDistributionsMultiNormal,Sigma) {
  Matrix<double,Dynamic,1> y(2,1);
  y << 2.0, -2.0;
  Matrix<double,Dynamic,1> mu(2,1);
  mu << 1.0, -1.0;
  Matrix<double,Dynamic,Dynamic> Sigma(2,2);
  Sigma << 9.0, -3.0, -3.0, 4.0;
  EXPECT_NO_THROW (stan::math::multi_normal_log(y, mu, Sigma));

  // non-symmetric
  Sigma(0, 1) = -2.5;
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
}
TEST(ProbDistributionsMultiNormal,Mu) {
  Matrix<double,Dynamic,1> y(3,1);
  y << 2.0, -2.0, 11.0;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 1.0, -1.0, 3.0;
  Matrix<double,Dynamic,Dynamic> Sigma(3,3);
  Sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 0.0,
    0.0, 0.0, 5.0;
  EXPECT_NO_THROW (stan::math::multi_normal_log(y, mu, Sigma));

  mu(0) = std::numeric_limits<double>::infinity();
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
  mu(0) = -std::numeric_limits<double>::infinity();
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
  mu(0) = std::numeric_limits<double>::quiet_NaN();
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
}
TEST(ProbDistributionsMultiNormal,MultiNormalOneRow) {
  Matrix<double,1,Dynamic> y(1,3);
  y << 2.0, -2.0, 11.0;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 1.0, -1.0, 3.0;
  Matrix<double,Dynamic,Dynamic> Sigma(3,3);
  Sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 0.0,
    0.0, 0.0, 5.0;
  EXPECT_FLOAT_EQ(-11.73908, stan::math::multi_normal_log(y,mu,Sigma));
}

TEST(ProbDistributionsMultiNormal,SigmaMultiRow) {
  Matrix<double,1,Dynamic> y(1,2);
  y << 2.0, -2.0;
  Matrix<double,Dynamic,1> mu(2,1);
  mu << 1.0, -1.0;
  Matrix<double,Dynamic,Dynamic> Sigma(2,2);
  Sigma << 9.0, -3.0, -3.0, 4.0;
  EXPECT_NO_THROW (stan::math::multi_normal_log(y, mu, Sigma));

  // non-symmetric
  Sigma(0, 1) = -2.5;
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
  Matrix<double,Dynamic,1> z(2,1);

  // wrong dimensions
  z << 2.0, -2.0;
  EXPECT_THROW (stan::math::multi_normal_log(z, mu, Sigma), std::domain_error);
}
TEST(ProbDistributionsMultiNormal,MuMultiRow) {
  Matrix<double,1,Dynamic> y(1,3);
  y << 2.0, -2.0, 11.0;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 1.0, -1.0, 3.0;
  Matrix<double,Dynamic,Dynamic> Sigma(3,3);
  Sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 0.0,
    0.0, 0.0, 5.0;
  EXPECT_NO_THROW (stan::math::multi_normal_log(y, mu, Sigma));

  mu(0) = std::numeric_limits<double>::infinity();
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
  mu(0) = -std::numeric_limits<double>::infinity();
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
  mu(0) = std::numeric_limits<double>::quiet_NaN();
  EXPECT_THROW (stan::math::multi_normal_log(y, mu, Sigma), std::domain_error);
}
TEST(ProbDistributionsMultiNormal,SizeMismatch) {
  Matrix<double,1,Dynamic> y(1,3);
  y << 2.0, -2.0, 11.0;
  Matrix<double,Dynamic,1> mu(2,1);
  mu << 1.0, -1.0;
  Matrix<double,Dynamic,Dynamic> Sigma(2,3);
  Sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 0.0;
  EXPECT_THROW(stan::math::multi_normal_log(y, mu, Sigma), std::invalid_argument);
}

TEST(ProbDistributionsMultiNormal, error_check) {
  boost::random::mt19937 rng;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 2.0,
    -2.0,
    11.0;

  Matrix<double,Dynamic,Dynamic> sigma(3,3);
  sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 1.0,
    0.0, 1.0, 3.0;
  EXPECT_NO_THROW(stan::math::multi_normal_rng(mu, sigma,rng));

  mu << stan::math::positive_infinity(),
    -2.0,
    11.0;
  EXPECT_THROW(stan::math::multi_normal_rng(mu,sigma,rng),std::domain_error);

  mu << 2.0,
    -2.0,
    11.0;
  sigma << 9.0, -3.0, 0.0,
    3.0,  4.0, 0.0,
    -2.0, 1.0, 3.0;
  EXPECT_THROW(stan::math::multi_normal_rng(mu,sigma,rng),std::domain_error);

}

TEST(ProbDistributionsMultiNormal, marginalOneChiSquareGoodnessFitTest) {
  boost::random::mt19937 rng;
  Matrix<double,Dynamic,Dynamic> sigma(3,3);
  sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 1.0,
    0.0, 1.0, 3.0;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 2.0,
    -2.0,
    11.0;
  int N = 10000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::normal_distribution<>dist (2.0,3.0);
  boost::math::chi_squared mydist(K-1);

  double loc[K - 1];
  for(int i = 1; i < K; i++)
    loc[i - 1] = quantile(dist, i * std::pow(K, -1.0));

  int count = 0;
  int bin [K];
  double expect [K];
  for(int i = 0 ; i < K; i++) {
    bin[i] = 0;
    expect[i] = N / K;
  }
  Eigen::VectorXd a(mu.rows());
  while (count < N) {
    a = stan::math::multi_normal_rng(mu,sigma,rng);
    int i = 0;
    while (i < K-1 && a(0) > loc[i])
      ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;
  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}

TEST(ProbDistributionsMultiNormal, marginalTwoChiSquareGoodnessFitTest) {
  boost::random::mt19937 rng;
  Matrix<double,Dynamic,Dynamic> sigma(3,3);
  sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 1.0,
    0.0, 1.0, 3.0;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 2.0,
    -2.0,
    11.0;
  int N = 10000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::normal_distribution<>dist (-2.0,2.0);
  boost::math::chi_squared mydist(K-1);

  double loc[K - 1];
  for(int i = 1; i < K; i++)
    loc[i - 1] = quantile(dist, i * std::pow(K, -1.0));

  int count = 0;
  int bin [K];
  double expect [K];
  for(int i = 0 ; i < K; i++) {
    bin[i] = 0;
    expect[i] = N / K;
  }
  Eigen::VectorXd a(mu.rows());
  while (count < N) {
    a = stan::math::multi_normal_rng(mu,sigma,rng);
    int i = 0;
    while (i < K-1 && a(1) > loc[i])
      ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;
  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}

TEST(ProbDistributionsMultiNormal, marginalThreeChiSquareGoodnessFitTest) {
  boost::random::mt19937 rng;
  Matrix<double,Dynamic,Dynamic> sigma(3,3);
  sigma << 9.0, -3.0, 0.0,
    -3.0,  4.0, 1.0,
    0.0, 1.0, 16.0;
  Matrix<double,Dynamic,1> mu(3,1);
  mu << 2.0,
    -2.0,
    11.0;
  int N = 10000;
  int K = boost::math::round(2 * std::pow(N, 0.4));
  boost::math::normal_distribution<>dist (11.0,4.0);
  boost::math::chi_squared mydist(K-1);

  double loc[K - 1];
  for(int i = 1; i < K; i++)
    loc[i - 1] = quantile(dist, i * std::pow(K, -1.0));

  int count = 0;
  int bin [K];
  double expect [K];
  for(int i = 0 ; i < K; i++) {
    bin[i] = 0;
    expect[i] = N / K;
  }
  Eigen::VectorXd a(mu.rows());
  while (count < N) {
    a = stan::math::multi_normal_rng(mu,sigma,rng);
    int i = 0;
    while (i < K-1 && a(2) > loc[i])
      ++i;
    ++bin[i];
    count++;
   }

  double chi = 0;
  for(int j = 0; j < K; j++)
    chi += ((bin[j] - expect[j]) * (bin[j] - expect[j]) / expect[j]);

  EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}

TEST(multiNormalRng, nonPosDefErrorTest) {
  using stan::math::multi_normal_rng;
  Eigen::MatrixXd S(2,2);
  S << 0, 1, 1, 0;  // not pos definite
  Eigen::VectorXd mu(2);
  mu << 1, 2;
  boost::random::mt19937 rng;
  EXPECT_THROW(multi_normal_rng(mu, S, rng), std::domain_error);
}

max_stars_repo_name: yizhang-cae/torsten

max_issues_repo_name: yizhang-cae/torsten

max_forks_repo_name: yizhang-cae/torsten


text:
#ifndef YASMIC_GENERIC_MATRIX_OPERATIONS
#define YASMIC_GENERIC_MATRIX_OPERATIONS

#include <boost/tuple/tuple.hpp>
#include <yasmic/smatrix_traits.hpp>
#include <utility>

namespace yasmic
{
	/**
	 * Simple nonzero class for most of the work.
	 */

	template <class Index, class Value, class NonzeroIndex>
	struct simple_nonzero
	{
		simple_nonzero()
			: _row(0), _column(0), _val(0), _nzi(0)
		{}

		simple_nonzero(Index row, Index col, Value val, NonzeroIndex nzi)
			: _row(row), _column(col), _val(val), _nzi(nzi)
		{}

		Index _row;
		Index _column;
		Value _val;
		NonzeroIndex _nzi;

		bool operator!= (const simple_nonzero& other)
		{
			return (_row != other._row || _column || other._column && _val || other._val && _nzi || other._nzi);
		}

		bool operator== (const simple_nonzero& other)
		{
			return (_row == other._row && _column == other._column && _val == other._val && _nzi == other._nzi);
		}

		typedef Index index_type;
		typedef Value value_type;
		typedef NonzeroIndex nz_index_type;
	};

	template <class Index, class Value, class NonzeroIndex>
	simple_nonzero<Index, Value, NonzeroIndex>
	make_simple_nonzero(Index r, Index c, Value v, NonzeroIndex nzi)
	{
		return simple_nonzero<Index, Value, NonzeroIndex>(r, c, v, nzi);
	}

    /**
     * Determine the number of nonzeros in a matrix.
     *
     * This function fowards the call to the matrix class itself,
     * implement a partial specialization if you need other behavior.
     *
     * @param m a reference to the matrix.
     *
     * @return the number of nonzeros in a matrix.
     */
	template <class Matrix>
	inline typename smatrix_traits<Matrix>::size_type
    nnz(Matrix& m)
    {
    	return (m.nnz());
    }

    /**
     * Return the dimensions of the matrix (nrows, ncols).
     *
     * This function fowards the call to the matrix class itself,
     * implement a partial specialization if you need other behavior.
     *
     * @param m a reference to the matrix.
     *
     * @return (nrows, ncols).
     */
    template <class Matrix>
    inline std::pair<typename smatrix_traits<Matrix>::size_type,
                      typename smatrix_traits<Matrix>::size_type>
    dimensions(Matrix& m)
    {
    	return (m.dimensions());
    }

    template <class Matrix>
    inline typename smatrix_traits<Matrix>::size_type
    ncols(Matrix& m)
	{
		typename smatrix_traits<Matrix>::size_type nrows,ncols;

		boost::tie(nrows, ncols) = dimensions(m);

		return (ncols);
	}

	template <class Matrix>
    inline typename smatrix_traits<Matrix>::size_type
	nrows(Matrix& m)
	{
		typename smatrix_traits<Matrix>::size_type nrows,ncols;

		boost::tie(nrows, ncols) = dimensions(m);

		return (nrows);
	}

    template <class Matrix>
    inline typename smatrix_traits<Matrix>::index_type
    row(boost::tuples::tuple<
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
    {
    	return (boost::tuples::get<0>(n));
    }

	template <class Matrix>
	inline typename smatrix_traits<Matrix>::index_type
	row(simple_nonzero<
			typename smatrix_traits<Matrix>::index_type,
			typename smatrix_traits<Matrix>::value_type,
			typename smatrix_traits<Matrix>::nz_index_type> n, const Matrix& f)
	{
		return n._row;
	}

    template <class Matrix>
    inline typename smatrix_traits<Matrix>::index_type
    column(boost::tuples::tuple<
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
    {
    	return (boost::tuples::get<1>(n));
    }

	template <class Matrix>
	inline typename smatrix_traits<Matrix>::index_type
	column(simple_nonzero<
			typename smatrix_traits<Matrix>::index_type,
			typename smatrix_traits<Matrix>::value_type,
			typename smatrix_traits<Matrix>::nz_index_type> n, const Matrix& f)
	{
		return n._column;
	}

	/*template <class Matrix>
    inline typename smatrix_traits<Matrix>::index_type
    column(boost::tuples::tuple<
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
    {
    	return (boost::tuples::get<0>(n));
    }*/

	template <class Matrix>
    inline typename smatrix_traits<Matrix>::index_type
		column(std::pair<
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
    {
    	return (n.first);
    }

    template <class Matrix>
    inline typename smatrix_traits<Matrix>::value_type
    value(boost::tuples::tuple<
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
    {
    	return (boost::tuples::get<2>(n));
    }

	template <class Matrix>
	inline typename smatrix_traits<Matrix>::value_type
	value(simple_nonzero<
			typename smatrix_traits<Matrix>::index_type,
			typename smatrix_traits<Matrix>::value_type,
			typename smatrix_traits<Matrix>::nz_index_type> n, const Matrix& f)
	{
		return n._val;
	}

	/**
	 * Overloaded function to return the value for a row/column non-zero as well.
	 */
	/*template <class Matrix>
    inline typename smatrix_traits<Matrix>::value_type
    row_value(boost::tuples::tuple<
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
    {
    	return (boost::tuples::get<1>(n));
    }*/

	/*template <class Matrix>
	inline typename smatrix_traits<Matrix>::value_type
	row_value(boost::tuples::tuple<
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
	{
		return (boost::tuples::get<1>(n));
	}*/

	template <class Matrix>
    inline typename smatrix_traits<Matrix>::value_type
	value(std::pair<
			typename smatrix_traits<Matrix>::index_type,
            typename smatrix_traits<Matrix>::value_type> n, const Matrix& f)
    {
    	return (n.second);
    }

    template <class Matrix>
    inline std::pair<typename smatrix_traits<Matrix>::nonzero_iterator,
                      typename smatrix_traits<Matrix>::nonzero_iterator>
    nonzeros(Matrix& m)
    {
		return (std::make_pair(m.begin_nonzeros(), m.end_nonzeros()));
    }

    template <class Matrix>
    inline std::pair<typename smatrix_traits<Matrix>::row_iterator,
                     typename smatrix_traits<Matrix>::row_iterator>
    rows(Matrix& m)
    {
        return (std::make_pair(m.begin_rows(), m.end_rows()));
    }

    template <class Matrix>
    inline std::pair<typename smatrix_traits<Matrix>::row_nonzero_iterator,
                      typename smatrix_traits<Matrix>::row_nonzero_iterator>
    row_nonzeros(typename smatrix_traits<Matrix>::index_type r, const Matrix& m)
    {
		typename smatrix_traits<Matrix>::row_nonzero_iterator br = m.begin_row(r);
		typename smatrix_traits<Matrix>::row_nonzero_iterator be = m.end_row(r);
		return std::make_pair(br, be);
    	//return (make_pair(m.begin_row(r), m.end_row(r)));
    }

    template <class Matrix>
    typename smatrix_traits<Matrix>::value_type value(
        typename smatrix_traits<Matrix>::index_type r,
        typename smatrix_traits<Matrix>::index_type c,
        const Matrix& m)
    {
        using namespace yasmic;

        //BOOST_STATIC_ASSERT(0 == 1);

        typedef typename smatrix_traits<Matrix>::index_type itype;
        typedef typename smatrix_traits<Matrix>::value_type vtype;

        typename smatrix_traits<Matrix>::nonzero_iterator nzi, nziend;
		for (boost::tie(nzi,nziend) = nonzeros(m);
			 nzi != nziend; ++nzi)
		{
			if (row(*nzi,m)==r && column(*nzi,m)==c)
            {
                return (value(*nzi,m));
            }
		}

        return (0);
    }

	template <class Matrix, class Iter1, class Iter2>
	void mult(Matrix& m, Iter1 x, Iter2 y)
	{
		using namespace yasmic;

		typedef typename smatrix_traits<Matrix>::index_type itype;
		typedef typename smatrix_traits<Matrix>::size_type stype;
		typedef typename smatrix_traits<Matrix>::value_type vtype;

		stype nr = nrows(m);

		// first zero the vector
		for (itype r=0; r < nr; ++r)
		{
			y[r] = vtype();
		}

		typename smatrix_traits<Matrix>::nonzero_iterator nzi, nziend;
		for (boost::tie(nzi,nziend) = nonzeros(m);
			 nzi != nziend; ++nzi)
		{
			y[row(*nzi,m)] += value(*nzi,m)*x[column(*nzi,m)];
		}
	}

	template <class Matrix, class Iter1, class Iter2>
	void trans_mult(Matrix& m, Iter1 x, Iter2 y)
	{
		using namespace yasmic;

		typedef typename smatrix_traits<Matrix>::index_type itype;
		typedef typename smatrix_traits<Matrix>::size_type stype;
		typedef typename smatrix_traits<Matrix>::value_type vtype;

		stype nc = ncols(m);

		// first zero the vector
		for (itype c=0; c < nc; ++c)
		{
			y[c] = vtype();
		}

		typename smatrix_traits<Matrix>::nonzero_iterator nzi, nziend;
		for (boost::tie(nzi,nziend) = nonzeros(m);
			 nzi != nziend; ++nzi)
		{
			y[column(*nzi,m)] += value(*nzi,m)*x[row(*nzi,m)];
		}
	}

}

#endif // YASMIC_GENERIC_MATRIX_OPERATIONS

max_stars_repo_name: anajmedd/ENSEEIHT-Projects

max_issues_repo_name: anajmedd/ENSEEIHT-Projects

max_forks_repo_name: anajmedd/ENSEEIHT-Projects


text:
/*
 * Copyright (c) 2017 Spotify AB.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#include <boost/test/unit_test.hpp>

#include "GraphImplementation.h"

BOOST_AUTO_TEST_SUITE(GraphImplementationTests)

static void fillTestScore(nfgrapher::Score &s) {
  s.version = nfgrapher::version();
  s.graph.id = "com.nativeformat.graph:123";
  s.graph.loading_policy =
      std::unique_ptr<nfgrapher::LoadingPolicy>(new nfgrapher::LoadingPolicy(
          nfgrapher::LoadingPolicy::ALL_CONTENT_PLAYTHROUGH));
}

BOOST_AUTO_TEST_CASE(testGraphImplementationConstructor) {
  std::shared_ptr<nativeformat::smartplayer::GraphImplementation> graph =
      std::make_shared<nativeformat::smartplayer::GraphImplementation>(
          2, 44100.0, nativeformat::smartplayer::createGraphSessionId());
  BOOST_CHECK(graph);
}

BOOST_AUTO_TEST_CASE(testGraphImplementationSetJson) {
  std::shared_ptr<nativeformat::smartplayer::GraphImplementation> graph =
      std::make_shared<nativeformat::smartplayer::GraphImplementation>(
          2, 44100.0, nativeformat::smartplayer::createGraphSessionId());
  std::mutex mutex;
  std::condition_variable conditional_variable;
  std::atomic<bool> callback_done;
  callback_done = false;
  nfgrapher::Score s;
  fillTestScore(s);
  graph->setJson(
      ((nlohmann::json)s).dump(),
      [&conditional_variable, &callback_done](const nativeformat::Load &load) {
        callback_done = true;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        conditional_variable.notify_one();
      });
  if (!callback_done) {
    std::unique_lock<std::mutex> lock(mutex);
    conditional_variable.wait(lock);
  }
}

BOOST_AUTO_TEST_CASE(testGraphImplementationIdentifier) {
  std::shared_ptr<nativeformat::smartplayer::GraphImplementation> graph =
      std::make_shared<nativeformat::smartplayer::GraphImplementation>(
          2, 44100.0, nativeformat::smartplayer::createGraphSessionId());
  std::mutex mutex;
  std::condition_variable conditional_variable;
  std::atomic<bool> callback_done;
  callback_done = false;
  nfgrapher::Score s;
  fillTestScore(s);
  graph->setJson(
      ((nlohmann::json)s).dump(),
      [&conditional_variable, &callback_done](const nativeformat::Load &load) {
        callback_done = true;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        conditional_variable.notify_one();
      });
  if (!callback_done) {
    std::unique_lock<std::mutex> lock(mutex);
    conditional_variable.wait(lock);
  }
  BOOST_CHECK_EQUAL(s.graph.id, graph->identifier());
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: rohankumardubey/NFSmartPlayer

max_issues_repo_name: rohankumardubey/NFSmartPlayer

max_forks_repo_name: rohankumardubey/NFSmartPlayer


text:
// Copyright (c) 2019 Graphcore Ltd. All rights reserved.
#define BOOST_TEST_MODULE slice_train_0

#include <algorithm>
#include <boost/test/unit_test.hpp>
#include <map>
#include <random>
#include <vector>
#include <popart/builder.hpp>
#include <popart/error.hpp>
#include <popart/ndarraywrapper.hpp>
#include <popart/session.hpp>
#include <popart/sgd.hpp>
#include <popart/testdevice.hpp>

BOOST_AUTO_TEST_CASE(SliceTrain0) {

  // model to train:
  //
  // weights
  //   |
  //   |---- slice ---=| == add ( weight slice 0, data) --------- l1
  //   |               |                                             \.
  //   |---- slice ---=| == add (weight slice 1, data)   ------- l1 -- final
  //                   |                                              /.
  //                   | == add (weight slice 1, weight slice 2)  -- l1
  // data ------------=|
  //
  //
  // data has shape   10 x 4.
  // weight has shape 25 x 4 if offset is false, and
  //                  25 x 5 if offset is true.
  //
  // The slices taken of weights have the data shape, i.e. 10 x 4.
  //
  // Weights when offset = true are,  ( 0 and 1 are the  2 sliced regions)
  //
  // . . . .
  // . . . .
  // . . . .
  // . . . .
  // . . . .
  // 0 0 0 0
  // 0 0 0 0
  // 0 0 0 0
  // 0 0 0 0
  //   |||
  // 1 1 1 1
  // 1 1 1 1
  // 1 1 1 1
  // 1 1 1 1
  //
  //
  // and when offset = false:
  //
  // . . . . .
  // . . . . .
  // . . . . .
  // . . . . .
  // . . . . .
  // . 0 0 0 0
  // . 0 0 0 0
  //   |||
  //   0 0 0 0
  //   0 0 0 0
  // 1 1 1 1 .
  // 1 1 1 1 .
  //   |||
  // 1 1 1 1 .
  // 1 1 1 1 .
  //
  // When offset is false, we expect the PadSum transform to be inapplicable.
  //

  /**
   * \param inplace Is the Inplacing Pattern enabled?
   *
   * \param padsum Is the PadSum Pattern, which converts sums of Pad outputs
   *               into concats where possible, enabled?
   *
   * \param offset See diagram above.
   * */
  auto getFinalWeights = [](bool inplace, bool padsum, bool offset) {
    using namespace popart;

    std::cout << "\n\n\nwith inplace = " << inplace << " padsum = " << padsum
              << " offset = " << offset << std::endl;

    std::map<std::string, std::pair<float, bool>> optParams;
    optParams.insert({"defaultLearningRate", {1.0f, true}});
    auto opt0 = SGD(optParams);

    // names of  weights used in model
    std::string w0name = "__w0__";

    auto builder = Builder::create();
    auto aiOnnx  = builder->aiOnnxOpset9();

    // sample shape: [sampleDim0, sampleDim1]
    int64_t sampleDim0 = 10;
    int64_t sampleDim1 = 4;
    std::vector<int64_t> sampleShape{sampleDim0, sampleDim1};
    TensorInfo sampleInfo{"FLOAT", sampleShape};

    // weigh shape:  [weightDim0, weightDim1]
    int64_t weightDim0 = 2 * sampleDim0 + 5;
    int64_t weightDim1 = sampleDim1 + 1 * (offset == true);
    auto nWeightVals   = weightDim0 * weightDim1;
    std::vector<int64_t> weightShape{weightDim0, weightDim1};
    TensorInfo weightInfo("FLOAT", weightShape);

    // a step of samples: [stepSize, batchSize, sampleDim0, sampleDim1]
    int64_t batchSize = 3;
    int64_t stepSize  = 1;

    std::vector<int64_t> batchShape{batchSize, sampleDim0, sampleDim1};
    TensorInfo batchInfo{"FLOAT", batchShape};

    std::vector<int64_t> stepDataShape{
        stepSize, batchSize, sampleDim0, sampleDim1};
    TensorInfo stepDataInfo{"FLOAT", stepDataShape};

    // Insert input Tensor into ONNX model
    auto input0 = builder->addInputTensor(batchInfo, "0tupni");

    // Note on randomness : generators are guaranteed to be platform
    // independent, so this will generate the same random values everywhere.
    // [0,1,2,3,4,5] - 2.5
    std::mt19937 gen(1011);
    auto getVal = [&gen]() { return static_cast<float>(gen() % 6) - 2.5; };

    // the initial weight values
    std::vector<float> weight0(nWeightVals);

    for (auto &x : weight0) {
      x = getVal();
    }

    // for reading back buffer for weight0
    std::vector<float> rb0(nWeightVals, -777.0f);

    // insert initialized weight Tensor into the ONNX model
    ConstVoidData cvd0({weight0.data(), weightInfo});
    auto w0Id = builder->addInitializedInputTensor(cvd0, w0name);
    WeightsIO weightsRead;
    weightsRead.insert(w0Id, {rb0.data(), weightInfo});

    // slice the weights into 2 parts, each the shape of a sample:
    auto slice0 = aiOnnx.slice(
        {w0Id}, {2 * sampleDim0, sampleDim1 + offset}, {sampleDim0, offset});
    auto slice1 = aiOnnx.slice({w0Id}, {sampleDim0, sampleDim1}, {0, 0});

    float lamb0 = 6.0;
    float lamb1 = 12.0;
    float lamb2 = 24.0;

    // input + weight slices, and sum of weight slices.
    // all with an l1 loss, them summed again:
    auto add0 = aiOnnx.add({slice0, input0});

    auto add1 = aiOnnx.add({slice1, add0});
    auto l1   = builder->aiGraphcoreOpset1().l1loss({add1}, lamb1);

    auto l0   = builder->aiGraphcoreOpset1().l1loss({add0}, lamb0);
    auto add2 = aiOnnx.add({slice0, slice1});
    auto l2   = builder->aiGraphcoreOpset1().l1loss({add2}, lamb2);

    auto lSum = aiOnnx.sum({l0, l1, l2});
    builder->addOutputTensor(lSum);

    auto proto    = builder->getModelProto();
    auto dataFlow = DataFlow(stepSize);

    SessionOptions userOptions;

    auto device = createTestDevice(TEST_TARGET, 1);

    auto session = popart::TrainingSession::createFromOnnxModel(
        proto,
        dataFlow,
        lSum,
        opt0,
        device,
        InputShapeInfo(),
        SessionOptions(),
        popart::Patterns(PatternsLevel::Default)
            .enableInPlace(inplace)
            .enablePattern("PadSum", true));

    session->prepareDevice();

    /*
    const auto sched = session->getIr().getOpSchedule({},
        RequireOptimalSchedule::Yes);
    for (auto x : sched) {
      std::cout << "\n   " << x->str();
    }
    std::cout << std::endl;
    */
    std::vector<float> v_input_x(stepDataInfo.nelms());
    for (auto &x : v_input_x) {
      x = getVal();
    }

    popart::NDArrayWrapper<float> input_x_wrapper(v_input_x.data(),
                                                  stepDataInfo);

    std::map<popart::TensorId, popart::IArray &> inputs = {
        {input0, input_x_wrapper}};

    popart::StepIO stepio(inputs, {});

    session->weightsFromHost();

    // run 1 with opt0
    session->run(stepio);
    session->run(stepio);

    // read final weights back
    session->weightsToHost();
    session->readWeights(weightsRead);

    return rb0;
  };

  //                                  inplace padsum offset
  const auto offset0 = getFinalWeights(true, true, true);
  const auto offset1 = getFinalWeights(true, false, true);
  const auto offset2 = getFinalWeights(false, false, true);
  const auto offset3 = getFinalWeights(false, true, true);

  if (offset0 != offset1 || offset0 != offset2 || offset0 != offset3) {
    throw popart::error("Offset cases do not all agree");
  }

  const auto nooffset0 = getFinalWeights(true, true, false);
  const auto nooffset1 = getFinalWeights(true, false, false);
  const auto nooffset2 = getFinalWeights(false, true, false);
  const auto nooffset3 = getFinalWeights(false, false, false);

  if (nooffset0 != nooffset1 || nooffset0 != nooffset2 ||
      nooffset0 != nooffset3) {
    throw popart::error("Offset cases do not all agree");
  }
}


text:
//////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2007-2015, Image Engine Design Inc. 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 Image Engine Design nor the names of any
//       other contributors to this software 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.
//
//////////////////////////////////////////////////////////////////////////

#include <boost/python.hpp>
#include <cassert>

#include "boost/format.hpp"

#include "maya/MPxNode.h"
#include "maya/MPxLocatorNode.h"
#include "maya/MPxDeformerNode.h"
#include "maya/MPxObjectSet.h"
#include "maya/MPxFieldNode.h"
#include "maya/MPxImagePlane.h"
#include "maya/MGlobal.h"
#undef None // must come after certain Maya includes which include X11/X.h

#include "IECore/Parameterised.h"
#include "IECore/LevelFilteredMessageHandler.h"

#ifdef IECOREMAYA_WITH_RI
// for DL_INTERFACE as used below
#include "ri.h"
#endif

#include "IECoreMaya/IECoreMaya.h"
#include "IECoreMaya/CacheSet.h"
#include "IECoreMaya/ParameterisedHolder.h"
#include "IECoreMaya/TransientParameterisedHolderNode.h"
#include "IECoreMaya/OpHolder.h"
#include "IECoreMaya/PythonCmd.h"
#include "IECoreMaya/ProceduralHolder.h"
#include "IECoreMaya/ProceduralHolderUI.h"
#include "IECoreMaya/SystemExitCmd.h"
#include "IECoreMaya/MessageHandler.h"
#include "IECoreMaya/ObjectData.h"
#include "IECoreMaya/ConverterHolder.h"
#include "IECoreMaya/ImageFile.h"
#include "IECoreMaya/ImagePlaneHolder.h"
#include "IECoreMaya/ParameterisedHolderSetValueCmd.h"
#include "IECoreMaya/ParameterisedHolderModificationCmd.h"
#include "IECoreMaya/DelightProceduralCacheCommand.h"
#include "IECoreMaya/CurveCombiner.h"
#include "IECoreMaya/Box3Manipulator.h"
#include "IECoreMaya/V3Manipulator.h"
#include "IECoreMaya/ParameterisedHolderManipContextCommand.h"
#include "IECoreMaya/MayaTypeIds.h"
#include "IECoreMaya/DrawableHolder.h"
#include "IECoreMaya/DrawableHolderUI.h"
#include "IECoreMaya/GeometryCombiner.h"
#include "IECoreMaya/TransformationMatrixManipulator.h"
#include "IECoreMaya/SceneShape.h"
#include "IECoreMaya/SceneShapeUI.h"
#include "IECoreMaya/SceneShapeInterface.h"

// see ObjectParameterHandler::doUpdate() for an explanation of the necessity for dummy data
static void *dummyDataCreator()
{
	// we never want to create dummy data, we just need the type to be registered.
	// we therefore treat any creation attempt as a failure.
	std::cerr << "IECoreMaya::dummyDataCreator : unexpected attempt to make dummy data." << std::endl;
	exit( EXIT_FAILURE );
	return 0;
}

namespace IECoreMaya
{

static unsigned long g_refCount = 0;

MStatus initialize(MFnPlugin &plugin)
{
	MStatus s = MS::kSuccess;

	if (g_refCount == 0)
	{

		// register plugin

		s = plugin.registerData( ObjectData::typeName, ObjectData::id, ObjectData::creator);
		s = plugin.registerData( "ieDummyData", DummyDataId, dummyDataCreator );

		s = plugin.registerNode( "ieCacheSet", CacheSet::id, CacheSet::creator, CacheSet::initialize,
			MPxNode::kObjectSet );

		s = plugin.registerNode( ParameterisedHolderNode::typeName, ParameterisedHolderNode::id,
			ParameterisedHolderNode::creator, ParameterisedHolderNode::initialize );
		assert( s );

		s = plugin.registerNode( ParameterisedHolderLocator::typeName, ParameterisedHolderLocator::id,
			ParameterisedHolderLocator::creator, ParameterisedHolderLocator::initialize, MPxNode::kLocatorNode );
		assert( s );

		s = plugin.registerNode( ParameterisedHolderDeformer::typeName, ParameterisedHolderDeformer::id,
			ParameterisedHolderDeformer::creator, ParameterisedHolderDeformer::initialize, MPxNode::kDeformerNode );
		assert( s );

		s = plugin.registerNode( ParameterisedHolderField::typeName, ParameterisedHolderField::id,
			ParameterisedHolderField::creator, ParameterisedHolderField::initialize, MPxNode::kFieldNode );
		assert( s );

		s = plugin.registerNode( ParameterisedHolderSet::typeName, ParameterisedHolderSet::id,
			ParameterisedHolderSet::creator, ParameterisedHolderSet::initialize, MPxNode::kObjectSet );
		assert( s );

		s = plugin.registerShape( ParameterisedHolderSurfaceShape::typeName, ParameterisedHolderSurfaceShape::id,
			ParameterisedHolderSurfaceShape::creator, ParameterisedHolderSurfaceShape::initialize, ProceduralHolderUI::creator );
		assert( s );

		s = plugin.registerShape( ParameterisedHolderComponentShape::typeName, ParameterisedHolderComponentShape::id,
			ParameterisedHolderComponentShape::creator, ParameterisedHolderComponentShape::initialize, ProceduralHolderUI::creator );
		assert( s );

		s = plugin.registerShape( DrawableHolder::typeName, DrawableHolder::id,
			DrawableHolder::creator, DrawableHolder::initialize, DrawableHolderUI::creator );
		assert( s );

		s = plugin.registerShape( "ieProceduralHolder", ProceduralHolder::id,
			ProceduralHolder::creator, ProceduralHolder::initialize, ProceduralHolderUI::creator );
		assert( s );

		s = plugin.registerShape( "ieSceneShapeInterface", SceneShapeInterface::id,
			SceneShapeInterface::creator, SceneShapeInterface::initialize, SceneShapeUI::creator );
		assert( s );

		s = plugin.registerShape( "ieSceneShape", SceneShape::id,
			SceneShape::creator, SceneShape::initialize, SceneShapeUI::creator );
		assert( s );

		s = plugin.registerNode( "ieOpHolderNode", OpHolderNode::id,
			OpHolderNode::creator, OpHolderNode::initialize );
		assert( s );

		s = plugin.registerNode( "ieConverterHolder", ConverterHolder::id,
			ConverterHolder::creator, ConverterHolder::initialize );
		assert( s );

		s = plugin.registerNode( TransientParameterisedHolderNode::typeName, TransientParameterisedHolderNode::id,
			TransientParameterisedHolderNode::creator, TransientParameterisedHolderNode::initialize );
		assert( s );

		s = plugin.registerNode( ParameterisedHolderImagePlane::typeName, ParameterisedHolderImagePlane::id,
			ParameterisedHolderImagePlane::creator, ParameterisedHolderImagePlane::initialize, MPxNode::kImagePlaneNode );
		assert( s );

		s = plugin.registerNode( "ieImagePlaneHolder", ImagePlaneHolder::id,
			ImagePlaneHolder::creator, ImagePlaneHolder::initialize, MPxNode::kImagePlaneNode );
		assert( s );

		s = plugin.registerNode( CurveCombiner::typeName, CurveCombiner::id,
			CurveCombiner::creator, CurveCombiner::initialize, MPxNode::kDependNode );
		assert( s );

		s = plugin.registerNode( GeometryCombiner::typeName, GeometryCombiner::id,
			GeometryCombiner::creator, GeometryCombiner::initialize, MPxNode::kDependNode );
		assert( s );

		// This should be used to set the classification of any manipulators that are
		// compatible with the ieParameterisedHolderManipContext to permit the UI code
		// to identify if a Parameter is manipulatable.
		const MString manipClassification( "ieParameterManipulator" );
		// Convention for parameter manipulator names:
		//    ie<manipulatorTypeHint><parameterTypeName>Manipulator

		s = plugin.registerNode( "ieBox3fParameterManipulator", Box3Manipulator::id,
			Box3Manipulator::creator, Box3Manipulator::initialize, MPxNode::kManipContainer, &manipClassification );
		assert( s );

		s = plugin.registerNode( "ieTransformationMatrixfParameterManipulator", TransformationMatrixManipulator::id,
			TransformationMatrixManipulator::creator, TransformationMatrixManipulator::initialize, MPxNode::kManipContainer, &manipClassification );
		assert( s );

		s = plugin.registerNode( "ieV3fParameterManipulator", V3Manipulator::id,
			V3Manipulator::creator, V3Manipulator::initialize, MPxNode::kManipContainer, &manipClassification );
		assert( s );

		s = plugin.registerCommand( "iePython", PythonCmd::creator, PythonCmd::newSyntax );
		PythonCmd::initialize();

		s = plugin.registerCommand( "ieSystemExit", SystemExitCmd::creator );

		s = plugin.registerCommand( "ieParameterisedHolderSetValue", ParameterisedHolderSetValueCmd::creator, ParameterisedHolderSetValueCmd::newSyntax );

		s = plugin.registerCommand( "ieParameterisedHolderModification", ParameterisedHolderModificationCmd::creator );

		s = plugin.registerContextCommand("ieParameterisedHolderManipContext", &ParameterisedHolderManipContextCommand::creator );

#ifdef DL_INTERFACE // we are building for 3delight
		s = plugin.registerCommand( "ieDelightProceduralCache", DelightProceduralCacheCommand::creator, DelightProceduralCacheCommand::newSyntax );
#endif

		MStringArray imageFileExtensions;
		imageFileExtensions.append( "exr" );

		s = plugin.registerImageFile( "ieImageFile", ImageFile::creator, imageFileExtensions);
		assert( s );

		/// \todo This may well need to change, depending on how we allow people to install
		/// the mel files.
		MString cmd = "source \"IECoreMaya/IECoreMaya.mel\";";
		s = MGlobal::executeCommand(cmd);

		if( !getenv( "IECOREMAYA_DISABLEOUTPUTREDIRECTION" ) )
		{
			IECore::MessageHandlerPtr h = new IECoreMaya::MessageHandler;
			h = new IECore::LevelFilteredMessageHandler( h, IECore::LevelFilteredMessageHandler::defaultLevel() );
			IECore::MessageHandler::setDefaultHandler( h );
		}

		if( MGlobal::mayaState() == MGlobal::kInteractive )
		{
			MGlobal::executePythonCommand( "import IECoreMaya; IECoreMaya.Menus.createCortexMenu()" );
		}

	}

	g_refCount ++;

	return MS::kSuccess;
}

MStatus uninitialize(MFnPlugin &plugin)
{
	MStatus s = MS::kSuccess;

	assert( g_refCount > 0 );

	g_refCount --;

	if (g_refCount == 0)
	{
		// unregister plugin

		s = plugin.deregisterNode( CacheSet::id );
		s = plugin.deregisterNode( ParameterisedHolderNode::id );
		s = plugin.deregisterNode( DrawableHolder::id );
		s = plugin.deregisterNode( ParameterisedHolderLocator::id );
		s = plugin.deregisterNode( ParameterisedHolderDeformer::id );
		s = plugin.deregisterNode( ParameterisedHolderField::id );
		s = plugin.deregisterNode( ParameterisedHolderSet::id );
		s = plugin.deregisterNode( ParameterisedHolderSurfaceShape::id );
		s = plugin.deregisterNode( ParameterisedHolderComponentShape::id );
		s = plugin.deregisterNode( ProceduralHolder::id );
		s = plugin.deregisterNode( SceneShapeInterface::id );
		s = plugin.deregisterNode( SceneShape::id );
		s = plugin.deregisterNode( OpHolderNode::id );
		s = plugin.deregisterNode( ConverterHolder::id );
		s = plugin.deregisterNode( TransientParameterisedHolderNode::id );
		s = plugin.deregisterNode( ParameterisedHolderImagePlane::id );
		s = plugin.deregisterNode( ImagePlaneHolder::id );
		s = plugin.deregisterNode( GeometryCombiner::id );
		s = plugin.deregisterNode( CurveCombiner::id );
		s = plugin.deregisterNode( Box3Manipulator::id );
		s = plugin.deregisterNode( TransformationMatrixManipulator::id );
		s = plugin.deregisterNode( V3Manipulator::id );

		s = plugin.deregisterCommand( "iePython" );
		s = plugin.deregisterCommand( "ieSystemExit" );
		PythonCmd::uninitialize();

		s = plugin.deregisterCommand( "ieParameterisedHolderModification" );
		s = plugin.deregisterCommand( "ieParameterisedHolderSetValue" );
		s = plugin.deregisterContextCommand("ieParameterisedHolderManipContext");

#ifdef DL_INTERFACE // we are building for 3delight
		s = plugin.deregisterCommand( "ieDelightProceduralCache" );
#endif

		s = plugin.deregisterData( DummyDataId );
		s = plugin.deregisterData( ObjectData::id );

		s = plugin.deregisterImageFile( "ieImageFile" );

		if( MGlobal::mayaState() == MGlobal::kInteractive )
		{
			MGlobal::executePythonCommand( "import IECoreMaya; IECoreMaya.Menus.removeCortexMenu()" );
		}

	}

	return s;
}

}

max_stars_repo_name: danieldresser-ie/cortex

max_issues_repo_name: danieldresser-ie/cortex

max_forks_repo_name: danieldresser-ie/cortex