text:
//          Copyright Gavin Band 2008 - 2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <string>
#include <vector>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/math/distributions/chi_squared.hpp>
#include "genfile/Error.hpp"
#include "genfile/ToGP.hpp"
#include "genfile/VariantDataReader.hpp"
#include "metro/likelihood/Multinomial.hpp"
#include "components/SNPSummaryComponent/SNPHWE.hpp"
#include "components/SNPSummaryComponent/SNPSummaryComputation.hpp"
#include "components/SNPSummaryComponent/HWEComputation.hpp"
#include "components/SNPSummaryComponent/IntensitySummaryComputation.hpp"
#include "components/SNPSummaryComponent/ClusterFitComputation.hpp"

// #define DEBUG_SNP_SUMMARY_COMPUTATION 1

namespace snp_summary_component {

	namespace {
		struct AlleleCountClient: public genfile::VariantDataReader::PerSampleSetter {
			AlleleCountClient( std::vector< double >* counts ):
				m_counts( counts ),
				m_number_of_alleles( 0 )
			{
				assert( counts != 0 ) ;
			}

			~AlleleCountClient() throw() {}

			void set_counts( std::vector< double >* counts ) {
				assert( counts != 0 ) ;
				m_counts = counts ;
			}

			void initialise( std::size_t number_of_samples, std::size_t number_of_alleles ) {
				m_number_of_alleles = number_of_alleles ;
				assert( m_counts->size() == number_of_alleles ) ;
			}

			bool set_sample( std::size_t i ) {
				m_ploidy = 0 ;
				m_table = 0 ;
				return true ;
			}

			void set_number_of_entries( uint32_t ploidy, std::size_t n, OrderType const order_type, ValueType const value_type ) {
				assert( order_type == genfile::ePerUnorderedGenotype ) ;
				assert( value_type == genfile::eProbability ) ;

				std::map< uint32_t, genfile::impl::Enumeration >::iterator where = m_tables.find( ploidy ) ;
				if( where == m_tables.end() ) {
					std::pair< std::map< uint32_t, genfile::impl::Enumeration >::iterator, bool >
						result = m_tables.insert( std::make_pair( ploidy, genfile::impl::enumerate_unphased_genotypes( ploidy ) )) ;
					assert( result.second ) ;
					where = result.first ;
				}
				m_ploidy = ploidy ;
				m_table = &(where->second) ;
			}

			void set_value( std::size_t value_i, genfile::MissingValue const value ) {
				set_value( value_i, 0.0 ) ;
			}

			void set_value( std::size_t value_i, double const value ) {
				assert( value_i <= std::size_t( std::numeric_limits< uint16_t >::max() )) ;
				genfile::impl::Enumeration const& enumeration = *m_table ;
				std::size_t const& maxAlleles = enumeration.first.second ;
				assert( m_number_of_alleles <= maxAlleles ) ;
				uint16_t const encodedGenotype = enumeration.second.second[ uint16_t( value_i ) ] ;
				uint32_t const bitsPerAllele = enumeration.first.first ;
				uint16_t mask = uint16_t( 0xFFFF ) >> ( 16 - bitsPerAllele ) ;
				// Dosage of the 1st allele in the genotype is not encoded directly.
				// We compute it as the ploidy minus the dosage of other alleles.
				uint16_t dosage_of_nonref_alleles = 0 ;
				for( std::size_t allele = 1; allele < m_number_of_alleles; ++allele ) {
					uint16_t const dosage = ( encodedGenotype >> ( (allele-1) * bitsPerAllele )) & mask ;
					(*m_counts)[allele] += value * dosage ;
					dosage_of_nonref_alleles += dosage ;
				}
				(*m_counts)[0] += value * (m_ploidy - dosage_of_nonref_alleles) ;
			}

			void finalise() {}
		private:
			std::vector< double >* m_counts ;
			std::size_t m_number_of_alleles ;
			uint32_t m_ploidy ;
			std::map< uint32_t, genfile::impl::Enumeration > m_tables ;
			genfile::impl::Enumeration* m_table ;
		} ;
	}

	struct AlleleCountComputation: public SNPSummaryComputation {
	public:

		AlleleCountComputation():
			m_counter( &m_counts )
		{}

		void list_variables( NameCallback callback ) const {
			using genfile::string_utils::to_string ;
			// By default we list 10 alleles
			callback( "number_of_alleles" ) ;
			for( std::size_t i = 0; i < 10; ++i ) {
				callback( "allele" + to_string(i+1) + "_count" ) ;
			}
		}

		void operator()(
			VariantIdentifyingData const& snp,
			Genotypes const& genotypes,
			Ploidy const& ploidy,
			genfile::VariantDataReader& data_reader,
			ResultCallback callback
		) {
			using genfile::string_utils::to_string ;
			m_counts = std::vector< double >( snp.number_of_alleles(), 0.0 ) ;
			m_counter.set_counts( &m_counts ) ;
			callback( "number_of_alleles", int64_t( snp.number_of_alleles() )) ;
			data_reader.get( ":genotypes:", genfile::to_GP_unphased( m_counter ) ) ;
			std::size_t i = 0 ;
			for( ; i < snp.number_of_alleles(); ++i ) {
				callback( "allele" + to_string(i+1) + "_count", m_counts[i] ) ;
			}
			for( ; i < 10; ++i ) {
				callback( "allele" + to_string(i+1) + "_count", genfile::MissingValue() ) ;
			}
		}

		std::string get_summary( std::string const& prefix, std::size_t column_width ) const {
			return prefix + "AlleleCountComputation" ;
		}
	private:
		std::vector< double > m_counts ;
		AlleleCountClient m_counter ;
	} ;

	struct AlleleFrequencyComputation: public SNPSummaryComputation
	{
		AlleleFrequencyComputation( std::string const& what ):
			m_compute_counts( what == "everything" || what == "counts" ),
			m_compute_frequencies( what == "everything" )
		{
			assert( what == "counts" || what == "everything" ) ;
		}
		void operator()(
			VariantIdentifyingData const& snp,
			Genotypes const& genotypes,
			Ploidy const& ploidy,
			genfile::VariantDataReader&,
			ResultCallback callback
		) {
			genfile::Chromosome const& chromosome = snp.get_position().chromosome() ;
			if( snp.number_of_alleles() == 2 ) {
				bool const allDiploid = ( ploidy.array() == 2 ).cast< int >().sum() == ploidy.size() ;
				if( allDiploid ) {
					compute_autosomal_frequency( snp, genotypes, callback ) ;
				} else {
					compute_sex_chromosome_frequency( snp, genotypes, ploidy, callback ) ;
				}
			}
		}

		void compute_sex_chromosome_frequency(
			VariantIdentifyingData const& snp,
			Genotypes const& genotypes,
			Ploidy const& ploidy,
			ResultCallback callback
		) {
			Genotypes haploid_genotypes = genotypes ;
			Genotypes diploid_genotypes = genotypes ;
			assert( std::size_t( genotypes.rows() ) == ploidy.size() ) ;

			for( int i = 0; i < genotypes.rows(); ++i ) {
				if( ploidy(i) != 1 ) {
					haploid_genotypes.row(i).setZero() ;
				}
				if( ploidy(i) != 2 ) {
					diploid_genotypes.row(i).setZero() ;
				}
			}

			double const a_allele_count = haploid_genotypes.col(0).sum()
				+ ( ( 2.0 * diploid_genotypes.col(0).sum() ) + diploid_genotypes.col(1).sum() ) ;
			double const b_allele_count = haploid_genotypes.col(1).sum()
				+ ( ( 2.0 * diploid_genotypes.col(2).sum() ) + diploid_genotypes.col(1).sum() ) ;

			if( m_compute_counts ) {
				callback( "alleleA_count", a_allele_count ) ;
				callback( "alleleB_count", b_allele_count ) ;
			}

			if( m_compute_frequencies ) {
				double const total_allele_count = ( haploid_genotypes.sum() + 2.0 * diploid_genotypes.sum() ) ;
				double const a_allele_freq = a_allele_count / total_allele_count ;
				double const b_allele_freq = b_allele_count / total_allele_count ;

				callback( "alleleA_frequency", a_allele_freq ) ;
				callback( "alleleB_frequency", b_allele_freq ) ;

				if( a_allele_freq < b_allele_freq ) {
					callback( "minor_allele_frequency", a_allele_freq ) ;
					callback( "minor_allele", snp.get_allele(0) ) ;
					callback( "major_allele", snp.get_allele(1) ) ;
				}
				else if( a_allele_freq > b_allele_freq ) {
					callback( "minor_allele_frequency", b_allele_freq ) ;
					callback( "minor_allele", snp.get_allele(1) ) ;
					callback( "major_allele", snp.get_allele(0) ) ;
				} else {
					callback( "minor_allele_frequency", a_allele_freq ) ;
				}
			}
		}

		void compute_autosomal_frequency( VariantIdentifyingData const& snp, Genotypes const& genotypes, ResultCallback callback ) {
			double const a_allele_count = ( 2.0 * genotypes.col(0).sum() ) + genotypes.col(1).sum() ;
			double const b_allele_count = ( 2.0 * genotypes.col(2).sum() ) + genotypes.col(1).sum() ;

			if( m_compute_counts ) {
				callback( "alleleA_count", a_allele_count ) ;
				callback( "alleleB_count", b_allele_count ) ;
			}

			if( m_compute_frequencies ) {
				double const total_allele_count = ( 2.0 * genotypes.sum() ) ;
				double const a_allele_freq = a_allele_count / total_allele_count ;
				double const b_allele_freq = b_allele_count / total_allele_count ;

				callback( "alleleA_frequency", a_allele_freq ) ;
				callback( "alleleB_frequency", b_allele_freq ) ;

				if( a_allele_freq < b_allele_freq ) {
					callback( "minor_allele_frequency", a_allele_freq ) ;
					callback( "minor_allele", snp.get_allele(0) ) ;
					callback( "major_allele", snp.get_allele(1) ) ;
				}
				else if( a_allele_freq > b_allele_freq ) {
					callback( "minor_allele_frequency", b_allele_freq ) ;
					callback( "minor_allele", snp.get_allele(1) ) ;
					callback( "major_allele", snp.get_allele(0) ) ;
				} else {
					callback( "minor_allele_frequency", a_allele_freq ) ;
				}
			}
		}

		std::string get_summary( std::string const& prefix, std::size_t column_width ) const {
			return prefix + "AlleleFrequencyComputation" ;
		}
	private:
		bool const m_compute_counts ;
		bool const m_compute_frequencies ;
	} ;

	// What proportion of the mass on a genotype is due to high-confidence calls?
	struct CallMassComputation: public SNPSummaryComputation
	{

		CallMassComputation( double const threshhold = 0.9 ):
			m_threshhold(threshhold)
		{}

		void operator()( VariantIdentifyingData const& snp, Genotypes const& genotypes, Ploidy const& ploidy, genfile::VariantDataReader&, ResultCallback callback ) {
			genfile::Chromosome const& chromosome = snp.get_position().chromosome() ;
			bool const allDiploid = ( ploidy.array() == 2 ).cast< int >().sum() == ploidy.size() ;
			if( allDiploid ) {
				compute_autosomal_call_mass( snp, genotypes, callback ) ;
			} else {
				return ;
			}
		}

		void compute_autosomal_call_mass( VariantIdentifyingData const& snp, Genotypes const& genotypes, ResultCallback callback ) {
			Eigen::VectorXd const masses = genotypes.colwise().sum() ;
			m_hcGenotypes = ( genotypes.array() > m_threshhold ).cast< double >()  * genotypes.array() ;
			Eigen::VectorXd const hcMasses = m_hcGenotypes.colwise().sum() ;

			callback( "AA_mass_propn", hcMasses(0)/masses(0) ) ;
			callback( "AB_mass_propn", hcMasses(1)/masses(1) ) ;
			callback( "BB_mass_propn", hcMasses(2)/masses(2) ) ;

			callback( "non-AA-mass_propn", (hcMasses(1)+hcMasses(2))/(masses(1)+masses(2))) ;
			callback( "non-BB-mass_propn", (hcMasses(1)+hcMasses(0))/(masses(1)+masses(0))) ;
		}

		std::string get_summary( std::string const& prefix, std::size_t column_width ) const {
			return prefix + "CallMassComputation" ;
		}
	private:
		double const m_threshhold ;
		Genotypes m_hcGenotypes ;
	} ;

	struct MissingnessComputation: public SNPSummaryComputation {
		MissingnessComputation( double call_threshhold = 0.9 ): m_call_threshhold( call_threshhold ) {}
		void operator()(
			VariantIdentifyingData const& snp,
			Genotypes const& genotypes,
			Ploidy const& ploidy,
			genfile::VariantDataReader&,
			ResultCallback callback
		) {
			assert( std::size_t( genotypes.rows() ) == ploidy.size() ) ;

			double missingness = double( genotypes.rows() ) - genotypes.array().sum() ;
			callback( "missing_proportion", missingness / double( genotypes.rows() ) ) ;

			bool const allDiploid = ( ploidy.array() == 2 ).cast< int >().sum() == ploidy.size() ;
			if( allDiploid ) {
				callback( "A", 0 ) ;
				callback( "B", 0 ) ;
				if( snp.number_of_alleles() == 2 ) {
					callback( "AA", genotypes.col(0).sum() ) ;
					callback( "AB", genotypes.col(1).sum() ) ;
					callback( "BB", genotypes.col(2).sum() ) ;
				}
				callback( "NULL", genotypes.rows() - genotypes.sum() ) ;
			} else {
				compute_haploid_diploid_counts( snp, genotypes, ploidy, callback ) ;
			}
			callback( "total", genfile::VariantEntry::Integer( genotypes.rows() )) ;
		}

		void compute_haploid_diploid_counts(
			VariantIdentifyingData const& snp,
			Genotypes const& genotypes,
			Ploidy const& ploidy,
			ResultCallback callback
		) {
			std::map< int, Eigen::VectorXd > counts ;
			std::map< int, double > null_counts ;
			counts[ -1 ] = Eigen::VectorXd::Zero( 3 ) ;
			counts[ 0 ] = Eigen::VectorXd::Zero( 3 ) ;
			counts[ 1 ] = Eigen::VectorXd::Zero( 3 ) ;
			counts[ 2 ] = Eigen::VectorXd::Zero( 3 ) ;
			char countKey[5] = { '.', '.', 'm', 'f', '.' } ;
			std::map< int, std::size_t > sample_counts ;

			for( std::size_t i = 0; i < ploidy.size(); ++i ) {
				counts[ ploidy(i) ] += genotypes.row( i ) ;
				null_counts[ ploidy(i) ] += ( 1 - genotypes.row(i).sum() ) ;
				++sample_counts[ ploidy(i) ] ;
#if DEBUG_SNP_SUMMARY_COMPUTATION
				if( ploidy(i) == 1 && genotypes(i,2) != 0 ) {
					std::cerr << "! ( MissingnessComputation::compute_sex_chromosome_counts() ): individual " << (i+1) << "is male but has genotype " << genotypes.row(i) << "!!\n" ;
				}
#endif
			}

			callback( "A", counts[ 1 ]( 0 ) ) ;
			callback( "B", counts[ 1 ]( 1 ) ) ;
			callback( "AA", counts[ 2 ]( 0 ) ) ;
			callback( "AB", counts[ 2 ]( 1 ) ) ;
			callback( "BB", counts[ 2 ]( 2 ) ) ;
			callback( "NULL", null_counts[ 'm' ] + null_counts[ 'f' ] ) ;
			callback( "unknown_ploidy", counts[ -1 ].sum() + null_counts[ -1 ] ) ;
			assert( counts[ 1 ]( 2 ) == 0 ) ;
		}

		std::string get_summary( std::string const& prefix = "", std::size_t column_width = 20 ) const { return prefix + "MissingnessComputation" ; }
	private:
		double const m_call_threshhold ;
	} ;

	struct InfoComputation: public SNPSummaryComputation {
		void operator()(
			VariantIdentifyingData const& snp,
			Genotypes const& genotypes,
			Ploidy const& ploidy,
			genfile::VariantDataReader&,
			ResultCallback callback
		) {
			if( snp.number_of_alleles() == 2 ) {
				bool const allDiploid = ( ploidy.array() == 2 ).cast< int >().sum() == ploidy.size() ;
				if( allDiploid ) {
					compute_autosomal_info( snp, genotypes, callback ) ;
				} else {
					compute_sex_chromosome_info( snp, genotypes, ploidy, callback ) ;
				}
			} else {
				// we don't compute for multiallelics currently
			}
		}

		void compute_autosomal_info( VariantIdentifyingData const& snp, Genotypes const& genotypes, ResultCallback callback ) {
			double const theta_mle = ( genotypes.col( 1 ).sum() + 2.0 * genotypes.col( 2 ).sum() ) / ( 2.0 * genotypes.sum() ) ;
			double const theta_est = theta_mle ;

			Eigen::VectorXd const impute_fallback_distribution = Eigen::VectorXd::Zero( 3 ) ;
			Eigen::VectorXd fallback_distribution = Eigen::VectorXd::Zero( 3 ) ;
			fallback_distribution( 0 ) = ( 1 - theta_est ) * ( 1 - theta_est ) ;
			fallback_distribution( 1 ) = 2.0 * theta_est * ( 1 - theta_est ) ;
			fallback_distribution( 2 ) = theta_est * theta_est ;

			//std::cerr << "theta = " << theta_est << ", fallback_distribution = " << fallback_distribution.transpose() << ".\n" ;

			Eigen::VectorXd const levels = Eigen::VectorXd::LinSpaced( 3, 0, 2 ) ;

			double const info = 1.0 - (
				compute_sum_of_variances( levels, genotypes, fallback_distribution )
				/ ( genotypes.rows() * 2.0 * theta_est * ( 1 - theta_est ) )
			) ;

			double const impute_info = 1.0 - (
				compute_sum_of_variances( levels, genotypes, impute_fallback_distribution )
				/ ( genotypes.sum() * 2.0 * theta_mle * ( 1 - theta_mle ) )
			) ;

			callback( "info", info ) ;
			callback( "impute_info", impute_info ) ;
		}

		void compute_sex_chromosome_info(
			VariantIdentifyingData const& snp,
			Genotypes const& genotypes,
			Ploidy const& ploidy,
			ResultCallback callback
		) {
			Eigen::MatrixXd hap_or_diploid( genotypes.rows(), 2 ) ;
			for( std::size_t i = 0; i < ploidy.size(); ++i ) {
				if( ploidy(i) == 1 ) {
					hap_or_diploid(i,0) = 1 ;
					hap_or_diploid(i,1) = 0 ;
				}
				else if( ploidy[ i ] == 2 ) {
					hap_or_diploid(i,0) = 0 ;
					hap_or_diploid(i,1) = 1 ;
				}
				else {
					// Don't understand, so treat ploidy as missing.
					// individuals with missing sex do not contribute to the computation.
					// I think this is the least surprising thing to do.
					hap_or_diploid(i,0) = 0 ;
					hap_or_diploid(i,1) = 0 ;
				}
			}

			double const a_allele_count_diploid = (
					( genotypes.col( 1 ) + 2.0 * genotypes.col( 0 ) ).array() * ( hap_or_diploid.col(1).array() )
				).sum() ;
			double const b_allele_count_diploid = (
					( genotypes.col( 1 ) + 2.0 * genotypes.col( 2 ) ).array() * ( hap_or_diploid.col(1).array() )
				).sum() ;
			double const a_allele_count_haploid = 	(
					genotypes.col( 0 ).array() * hap_or_diploid.col(0).array()
				).sum() ;
			double const b_allele_count_haploid = 	(
					genotypes.col( 1 ).array() * hap_or_diploid.col(0).array()
				).sum() ;

			// MLE estimate of allele frequency
			double const theta_mle = ( b_allele_count_diploid + b_allele_count_haploid )
				/ ( a_allele_count_diploid + b_allele_count_diploid + a_allele_count_haploid + b_allele_count_haploid ) ;

			// For the new info measure, could regularise by data augmentation, adding one allele of each type.
			// This makes info better behaved at low frequencies.
			// Note adding one of each allele constitutes minimal prior information that the variant is polymorphic.
			// double const theta_est = ( b_allele_count_diploid + b_allele_count_haploid + 1 )
			//	/ ( a_allele_count_diploid + b_allele_count_diploid + a_allele_count_haploid + b_allele_count_haploid + 2 ) ;

			// ...but don't actually do that; it is less conservative at rare SNPs.
			double const theta_est = theta_mle ;

			Eigen::VectorXd diploid_fallback_distribution = Eigen::VectorXd::Zero( 3 ) ;
			diploid_fallback_distribution( 0 ) = ( 1 - theta_est ) * ( 1 - theta_est ) ;
			diploid_fallback_distribution( 1 ) = 2.0 * theta_est * ( 1 - theta_est ) ;
			diploid_fallback_distribution( 2 ) = theta_est * theta_est ;

			Eigen::VectorXd haploid_fallback_distribution = Eigen::VectorXd::Zero( 3 ) ;
			haploid_fallback_distribution( 0 ) = 1 - theta_est ;
			haploid_fallback_distribution( 1 ) = theta_est ;

			//std::cerr << "theta = " << theta_mle << ", fallback_distribution = " << fallback_distribution.transpose() << ".\n" ;

			Eigen::VectorXd const levels = Eigen::VectorXd::LinSpaced( 3, 0, 2 ) ;

			double info = 0.0 ;
			Eigen::VectorXd const haploids = ( hap_or_diploid.col( 0 ).array() == 1 ).cast< double >() ;
			Eigen::VectorXd const diploids = ( hap_or_diploid.col( 1 ).array() == 1 ).cast< double >() ;
			{
				if( haploids.sum() > 0 ) {
					info -= compute_sum_of_variances( levels, genotypes, haploid_fallback_distribution, haploids )
						/ ( theta_est * ( 1 - theta_est ) ) ;
				}

				if( diploids.sum() > 0 ) {
					info -= compute_sum_of_variances( levels, genotypes, diploid_fallback_distribution, diploids )
						/ ( 2.0 * theta_est * ( 1 - theta_est ) ) ;
				}

				double const totalProb = (
					( genotypes.block( 0, 0, genotypes.rows(), 2 ).rowwise().sum().array() * haploids.array() ).sum()
					+ ( genotypes.rowwise().sum().array() * diploids.array() ).sum()
				) ;

				info = 1.0 + (info / (haploids.sum() + diploids.sum() )) ;
			}

			{
				// IMPUTE-style MLE, treat all samples as diploid ignoring gender.
				// Haploids are encoded in the 0/1 columns here so we have to reverse-engineer the
				// original info computation to allow for this.
				double const autosomal_theta_mle = (
					( 2.0 * genotypes.col( 1 ).array() * haploids.array() )
					+ (( genotypes.col( 1 ) + 2.0 * genotypes.col( 2 ) ).array() * ( 1.0 - haploids.array() ))
				).sum() / ( 2.0 * genotypes.sum() ) ;

#if DEBUG
				std::cerr << "autosomal_theta_mle  = " << std::setprecision(10) << autosomal_theta_mle << ".\n" ;
				std::cerr << genotypes << ".\n" ;
#endif
				Eigen::VectorXd haploidLevels = Eigen::VectorXd::Zero(3) ;
				haploidLevels(1) = 2 ;
				double const impute_info = 1.0 - (
					(
						compute_sum_of_variances( haploidLevels, genotypes, Eigen::VectorXd::Zero( 3 ), haploids )
						+ compute_sum_of_variances( levels, genotypes, Eigen::VectorXd::Zero( 3 ), 1.0 - haploids.array() )
					) / ( genotypes.sum() * 2.0 * autosomal_theta_mle * ( 1 - autosomal_theta_mle ) )
				) ;

				callback( "info", info ) ;
				callback( "impute_info", impute_info ) ;
			}
		}

		std::string get_summary( std::string const& prefix = "", std::size_t column_width = 20 ) const { return prefix + "InfoComputation" ; }

	private:
		double compute_sum_of_variances( Eigen::VectorXd const& levels, Eigen::MatrixXd const& probabilities, Eigen::VectorXd const& fallback ) const {
			return compute_sum_of_variances( levels, probabilities, fallback, Eigen::VectorXd::Constant( probabilities.rows(), 1 ) ) ;
		}

		// treat the rows of the probabilities matrix as probabilities.
		// distribution for individual i is taken as a mixture of the distribution given by row i of probabilities,
		// and the fallback distribution if the sum of row i < 1, appropriately weighted.
		// Only individuals with inclusion = 1 are used.
		double compute_sum_of_variances( Eigen::VectorXd const& levels, Eigen::MatrixXd const& probabilities, Eigen::VectorXd const& fallback, Eigen::VectorXd const& inclusion ) const {
			assert( levels.size() == fallback.size() ) ;
			assert( levels.size() == probabilities.cols() ) ;
			assert( inclusion.size() == probabilities.rows() ) ;
			Eigen::VectorXd levels_squared = ( levels.array() * levels.array() ) ;

			double result = 0.0 ;
			for( int i = 0; i < probabilities.rows(); ++i ) {
				if( inclusion( i ) == 1 ) {
					double const c = probabilities.row( i ).sum() ;
					result += compute_variance( levels, levels_squared, probabilities.row( i ).transpose() + ( 1 - c ) * fallback ) ;
				}
			}
			return result ;
		}

		double compute_variance( Eigen::VectorXd const& levels, Eigen::VectorXd const& levels_squared, Eigen::VectorXd const& probs ) const {
			double const mean = ( probs.transpose() * levels )(0) ;
			double const variance = ( probs.transpose() * levels_squared )(0) - ( mean * mean ) ;
			// std::cerr << "compute_variance: levels = " << levels.transpose() << ", levels_squared = " << levels_squared.transpose() << ", probs = " << probs.transpose() << ", variance = " << variance << ".\n" ;
			return variance ;
		}
	} ;
}

SNPSummaryComputation::UniquePtr SNPSummaryComputation::create(
	std::string const& name
) {
	UniquePtr result ;
	if( name == "allele-frequencies" ) { result.reset( new snp_summary_component::AlleleFrequencyComputation( "everything" )) ; }
	else if( name == "allele-counts" ) { result.reset( new snp_summary_component::AlleleFrequencyComputation( "counts" )) ; }
	else if( name == "HWE" ) { result.reset( new snp_summary_component::HWEComputation()) ; }
	else if( name == "missingness" ) { result.reset( new snp_summary_component::MissingnessComputation()) ; }
	else if( name == "info" ) { result.reset( new snp_summary_component::InfoComputation()) ; }
	else if( name == "call-mass-proportion" ) { result.reset( new snp_summary_component::CallMassComputation()) ; }
	else if( name == "intensity-stats" ) { result.reset( new snp_summary_component::IntensitySummaryComputation() ) ; }
	else if( name == "multi-allele-counts" ) { result.reset( new snp_summary_component::AlleleCountComputation() ) ; }
	else {
		throw genfile::BadArgumentError( "SNPSummaryComputation::create()", "name=\"" + name + "\"" ) ;
	}
	return result ;
}

max_stars_repo_name: CreRecombinase/qctool

max_issues_repo_name: CreRecombinase/qctool

max_forks_repo_name: CreRecombinase/qctool


text:
#define BIORBD_API_EXPORTS
#include "Utils/Equation.h"

#include <boost/lexical_cast.hpp>
#include "Utils/Error.h"

std::vector<biorbd::utils::Equation> biorbd::utils::Equation::prepareMathSymbols(){
    // Classés en ordre de priorité des opérations
    std::vector<biorbd::utils::Equation> symbols;
    symbols.push_back("(");
    symbols.push_back(")");
    symbols.push_back("e");
    symbols.push_back("/");
    symbols.push_back("*");
    symbols.push_back("+");
    symbols.push_back("-");

    return symbols;
}

biorbd::utils::Equation::Equation() :
    biorbd::utils::String("")
{

}

biorbd::utils::Equation::Equation(const char *c) :
    biorbd::utils::String(c)
{

}

biorbd::utils::Equation::Equation(const biorbd::utils::String &s) :
    biorbd::utils::String(s)
{

}

biorbd::utils::Equation::Equation(const std::basic_string<char> &c) :
    biorbd::utils::String(c)
{

}

std::vector<biorbd::utils::Equation> biorbd::utils::Equation::splitIntoEquation(
        biorbd::utils::Equation wholeEq,
        const std::map<biorbd::utils::Equation, double>& variables)
{
    // variable de sortie
    std::vector<biorbd::utils::Equation> eq;

    // Remplacer les variables par un nombre
    replaceVar(wholeEq, variables);

    // Déclaration des marqueurs arithmétiques
    const std::vector<biorbd::utils::Equation>& symbols(prepareMathSymbols());

    // Sur tout le long du string
    while (1){
        int firstIdx(static_cast<int>(wholeEq.size())+1);// Mettre un index trop grand
        unsigned int toStop(0); // Mettre stop a 0
        // Balayer chaque symbols pour voir s'ils se trouvent dans l'equation
        for (unsigned int i=0; i<symbols.size(); ++i){
            int idx = static_cast<int>(wholeEq.find(symbols[i]));
            if (idx < 0){ // Si non, le noter
                ++toStop;
                continue;
            }
            if (idx < firstIdx && idx != -1) // si oui, regarder s'il est avant un autre symbol
                firstIdx = idx;
        }
        if (toStop == symbols.size()){ // S'il ne reste aucun symbole, sortir du while
            eq.push_back(wholeEq);
            break;
        }
        if (wholeEq.size() == 1){
            eq.push_back(wholeEq(0));
            break;
        }
        if (firstIdx+1 == static_cast<int>(wholeEq.size())){
            eq.push_back(wholeEq.substr(0,static_cast<unsigned int>(firstIdx)));
            eq.push_back(wholeEq(static_cast<unsigned int>(firstIdx)));
            break;
        }
        else if (firstIdx == 0){
            if (!wholeEq(0).compare("-")){
                // Si l'équation commencer par un "-"
                std::vector<biorbd::utils::Equation> tp(splitIntoEquation(wholeEq.substr(1), variables));
                if (eq.size() != 0 && !eq[eq.size()-1].compare("e") ){
                    // special case of scientific notation (1e-x)
                    eq.push_back(wholeEq(0));
                    wholeEq = wholeEq.substr(1);
                } else if (!tp[0].compare("(")) {
                    // Resolve now the parenthese to apply the minus after
                    size_t idx = wholeEq.find_first_of(")");
                    biorbd::utils::Equation newWholeEq(wholeEq.substr(2, idx-2));
                    double res(-1*resolveEquation(splitIntoEquation(newWholeEq, variables)));
                    wholeEq = std::to_string(res) + wholeEq.substr(idx+1);
                } else {
                    tp[0] = "-" + tp[0];
                    eq.insert(eq.end(), tp.begin(), tp.end());
                    return eq;
                }
            }
            else if (!wholeEq(0).compare("+")){
                // Si l'équation commencer par un +
                std::vector<biorbd::utils::Equation> tp(splitIntoEquation(wholeEq.substr(1), variables));
                if (eq.size() != 0) // S'il n'y a rien avant, est faux dans les cas 1ex
                    eq.push_back("+");
                eq.insert(eq.end(), tp.begin(), tp.end());
                return eq;
            }
            else{
                eq.push_back(wholeEq(0));
                wholeEq = wholeEq.substr(1);
            }
        }
        else{
            eq.push_back(wholeEq.substr(0,static_cast<unsigned int>(firstIdx))); // Prendre tout ce qui vient avant le symbole
            eq.push_back(wholeEq(static_cast<unsigned int>(firstIdx))); // Récupérer le symbole
            wholeEq = wholeEq.substr(static_cast<unsigned int>(firstIdx)+1); // Garder tout ce qu'il y a apres le symbole et recommencer
        }

    }

    // Remplacer les constantes par un nombre
    replaceCste(eq);

    // Retourner l'équation
    return eq;
}

void biorbd::utils::Equation::replaceCste(
        std::vector<biorbd::utils::Equation> &eq)
{
    for (unsigned int i=0; i<eq.size(); ++i)
        if (!eq[i].tolower().compare("pi"))
            eq[i] = boost::lexical_cast< std::string>(PI);
}

void biorbd::utils::Equation::replaceVar(
        biorbd::utils::Equation &eq,
        const std::map<biorbd::utils::Equation, double>& variables)
{
    for (auto var : variables)
        while (eq.find(var.first) != std::string::npos){
            size_t pos(eq.find(var.first));
            size_t length(var.first.length());
            eq = eq.substr(0, pos) + "(" +
                    std::to_string(var.second) + ")" +
                    eq.substr(pos + length);
        }
}

double biorbd::utils::Equation::resolveEquation(
        std::vector<biorbd::utils::Equation> wholeEq)
{
    return resolveEquation(wholeEq,0);
}

double biorbd::utils::Equation::resolveEquation(
        std::vector<biorbd::utils::Equation> eq,
        unsigned int math)
{
    // Si on a traité tout
    if (eq.size() == 1)
        return boost::lexical_cast<double>(eq[0]);

    // Déclaration des marqueurs arithmétiques
    const std::vector<biorbd::utils::Equation>& symbols(prepareMathSymbols());
    std::vector<biorbd::utils::Equation> eq2;
    bool continuer(true);

    for (unsigned int j=0; j<eq.size(); ++j){
        if (!eq[j].compare(symbols[math]) && continuer){
            if (j==0 && (!symbols[math].compare("+") || !symbols[math].compare("-"))){
                // Écraser la valeur précédente
                if (!symbols[math].compare("+"))
                    eq2[j-1] = boost::lexical_cast<std::string>(0.0 + boost::lexical_cast<double>(eq[j+1]));
                else if (!symbols[math].compare("-"))
                    eq2[j-1] = boost::lexical_cast<std::string>(0.0 - boost::lexical_cast<double>(eq[j+1]));
            }
            else{
                // Écraser la valeur précédente
                if (!symbols[math].compare("(")){
                    std::vector<biorbd::utils::Equation> eq_tp;
                    bool foundIdx(false);
                    int cmpValues(0);
                    unsigned int cmpOpen(0);
                    for (unsigned int k=j+1; k<eq.size();++k){
                        if (!eq[k].compare("("))
                            cmpOpen++;
                        else if (!eq[k].compare(")")){
                            if (cmpOpen == 0){
                                foundIdx = true;
                                break;
                            }
                            else
                                cmpOpen--;
                        }

                        eq_tp.push_back(eq[k]);
                        ++cmpValues;
                    }
                    biorbd::utils::Error::check(foundIdx, "You must close brackets!");

                    eq2.push_back(boost::lexical_cast<std::string>(boost::lexical_cast<std::string>(resolveEquation(eq_tp))));
                    j+=static_cast<unsigned int>(cmpValues);
                }
                else if (!symbols[math].compare("/"))
                    eq2[j-1] = boost::lexical_cast<std::string>(boost::lexical_cast<double>(eq[j-1]) / boost::lexical_cast<double>(eq[j+1]));
                else if (!symbols[math].compare("*"))
                    eq2[j-1] = boost::lexical_cast<std::string>(boost::lexical_cast<double>(eq[j-1]) * boost::lexical_cast<double>(eq[j+1]));
                else if (!symbols[math].compare("+"))
                    eq2[j-1] = boost::lexical_cast<std::string>(boost::lexical_cast<double>(eq[j-1]) + boost::lexical_cast<double>(eq[j+1]));
                else if (!symbols[math].compare("-"))
                    eq2[j-1] = boost::lexical_cast<std::string>(boost::lexical_cast<double>(eq[j-1]) - boost::lexical_cast<double>(eq[j+1]));
                else if (!symbols[math].compare("e")){
                    if (!eq[j+1].compare("-")){
                        eq2[j-1] = boost::lexical_cast<std::string>(boost::lexical_cast<double>(eq[j-1]) * pow(10,-1*boost::lexical_cast<double>(eq[j+2])));
                        ++j;
                    }
                    else if (!eq[j+1].compare("+")){
                        eq2[j-1] = boost::lexical_cast<std::string>(boost::lexical_cast<double>(eq[j-1]) * pow(10,boost::lexical_cast<double>(eq[j+2])));
                        ++j;
                    }
                    else
                        eq2[j-1] = boost::lexical_cast<std::string>(boost::lexical_cast<double>(eq[j-1]) * pow(10,boost::lexical_cast<double>(eq[j+1])));

                }
            }

            j+=2;
            continuer = false;
        }
        if (j<eq.size())
            eq2.push_back(eq[j]);
    }

    if (continuer)
        return resolveEquation(eq2, ++math);
    else
        return resolveEquation(eq2, math);
}

double biorbd::utils::Equation::resolveEquation(
        biorbd::utils::Equation wholeEq,
        const std::map<biorbd::utils::Equation, double>& variables)
{
    return resolveEquation(splitIntoEquation(wholeEq, variables));
}
double biorbd::utils::Equation::resolveEquation(
        biorbd::utils::Equation wholeEq)
{
    std::map<biorbd::utils::Equation, double> dumb;
    return resolveEquation(splitIntoEquation(wholeEq, dumb));
}

ext: cpp

max_stars_repo_name: vincentdelpech/biorbd

max_issues_repo_name: vincentdelpech/biorbd

max_forks_repo_name: vincentdelpech/biorbd


text: #include <boost/text/unencoded_rope.hpp>

max_stars_repo_name: eightysquirrels/text

max_issues_repo_name: eightysquirrels/text

max_forks_repo_name: eightysquirrels/text


text: #include <boost/mpl/aux_/preprocessed/msvc60/or.hpp>

ext: hpp

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include "clear_global_translator.hpp"

#include <boost_python_exception/auto_translation/exception_translator.hpp>

using boost_python_exception::auto_translation::exception_translator::clear;

namespace test { namespace fixtures {

clear_global_translator::clear_global_translator()
{
    clear();
}

clear_global_translator::~clear_global_translator()
{
    clear();
}

}
}


text:
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <backendsCommon/test/EndToEndTestImpl.hpp>

#include <backendsCommon/test/ActivationEndToEndTestImpl.hpp>
#include <backendsCommon/test/ArgMinMaxEndToEndTestImpl.hpp>
#include <backendsCommon/test/ComparisonEndToEndTestImpl.hpp>
#include <backendsCommon/test/ConcatEndToEndTestImpl.hpp>
#include <backendsCommon/test/DepthToSpaceEndToEndTestImpl.hpp>
#include <backendsCommon/test/DequantizeEndToEndTestImpl.hpp>
#include <backendsCommon/test/DetectionPostProcessEndToEndTestImpl.hpp>
#include <backendsCommon/test/ElementwiseUnaryEndToEndTestImpl.hpp>
#include <backendsCommon/test/FillEndToEndTestImpl.hpp>
#include <backendsCommon/test/InstanceNormalizationEndToEndTestImpl.hpp>
#include <backendsCommon/test/PreluEndToEndTestImpl.hpp>
#include <backendsCommon/test/QLstmEndToEndTestImpl.hpp>
#include <backendsCommon/test/QuantizedLstmEndToEndTestImpl.hpp>
#include <backendsCommon/test/SpaceToDepthEndToEndTestImpl.hpp>
#include <backendsCommon/test/SplitterEndToEndTestImpl.hpp>
#include <backendsCommon/test/TransposeConvolution2dEndToEndTestImpl.hpp>

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(NeonEndToEnd)

std::vector<armnn::BackendId> defaultBackends = {armnn::Compute::CpuAcc};

// Abs
BOOST_AUTO_TEST_CASE(NeonAbsEndToEndTestFloat32)
{
    std::vector<float> expectedOutput =
    {
        1.f, 1.f, 1.f, 1.f, 5.f, 5.f, 5.f, 5.f,
        3.f, 3.f, 3.f, 3.f, 4.f, 4.f, 4.f, 4.f
    };

    ElementwiseUnarySimpleEndToEnd<armnn::DataType::Float32>(defaultBackends,
                                                             UnaryOperation::Abs,
                                                             expectedOutput);
}

// Constant
BOOST_AUTO_TEST_CASE(ConstantUsage_Neon_Float32)
{
    BOOST_TEST(ConstantUsageFloat32Test(defaultBackends));
}

#if defined(ARMNNREF_ENABLED)

// This test unit needs the reference backend, it's not available if the reference backend is not built

BOOST_AUTO_TEST_CASE(FallbackToCpuRef)
{
    using namespace armnn;

    // Create runtime in which test will run and allow fallback to CpuRef.
    IRuntime::CreationOptions options;
    IRuntimePtr runtime(IRuntime::Create(options));

    // Builds up the structure of the network.
    INetworkPtr net(INetwork::Create());

    IConnectableLayer* input = net->AddInputLayer(0);

    // This layer configuration isn't supported by CpuAcc but we allow fallback to CpuRef so it shoud pass.
    NormalizationDescriptor descriptor;
    IConnectableLayer* pooling = net->AddNormalizationLayer(descriptor);

    IConnectableLayer* output = net->AddOutputLayer(0);

    input->GetOutputSlot(0).Connect(pooling->GetInputSlot(0));
    pooling->GetOutputSlot(0).Connect(output->GetInputSlot(0));

    input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
    pooling->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));

    // optimize the network
    std::vector<BackendId> backends = {Compute::CpuAcc, Compute::CpuRef};
    IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec());

    // Load it into the runtime. It should pass.
    NetworkId netId;
    BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success);
}

#endif

BOOST_AUTO_TEST_CASE(NeonGreaterSimpleEndToEndTest)
{
    const std::vector<uint8_t> expectedOutput({ 0, 0, 0, 0,  1, 1, 1, 1,
                                                0, 0, 0, 0,  0, 0, 0, 0 });

    ComparisonSimpleEndToEnd<armnn::DataType::Float32>(defaultBackends,
                                                       ComparisonOperation::Greater,
                                                       expectedOutput);
}

BOOST_AUTO_TEST_CASE(NeonGreaterSimpleEndToEndUint8Test)
{
    const std::vector<uint8_t> expectedOutput({ 0, 0, 0, 0,  1, 1, 1, 1,
                                                0, 0, 0, 0,  0, 0, 0, 0 });

    ComparisonSimpleEndToEnd<armnn::DataType::QAsymmU8>(defaultBackends,
                                                               ComparisonOperation::Greater,
                                                               expectedOutput);
}

BOOST_AUTO_TEST_CASE(NeonGreaterBroadcastEndToEndTest)
{
    const std::vector<uint8_t> expectedOutput({ 0, 1, 0, 0, 0, 1,
                                                1, 1, 1, 1, 1, 1 });

    ComparisonBroadcastEndToEnd<armnn::DataType::Float32>(defaultBackends,
                                                          ComparisonOperation::Greater,
                                                          expectedOutput);
}

BOOST_AUTO_TEST_CASE(NeonGreaterBroadcastEndToEndUint8Test)
{
    const std::vector<uint8_t> expectedOutput({ 0, 1, 0, 0, 0, 1,
                                                1, 1, 1, 1, 1, 1 });

    ComparisonBroadcastEndToEnd<armnn::DataType::QAsymmU8>(defaultBackends,
                                                                  ComparisonOperation::Greater,
                                                                  expectedOutput);
}

BOOST_AUTO_TEST_CASE(NeonConcatEndToEndDim0Test)
{
    ConcatDim0EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonConcatEndToEndDim0Uint8Test)
{
    ConcatDim0EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonConcatEndToEndDim1Test)
{
    ConcatDim1EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonConcatEndToEndDim1Uint8Test)
{
    ConcatDim1EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonConcatEndToEndDim3Test)
{
    ConcatDim3EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonConcatEndToEndDim3Uint8Test)
{
    ConcatDim3EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

// DepthToSpace
BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNchwFloat32)
{
    DepthToSpaceEndToEnd<armnn::DataType::Float32>(defaultBackends, armnn::DataLayout::NCHW);
}

BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNchwFloat16)
{
    DepthToSpaceEndToEnd<armnn::DataType::Float16>(defaultBackends, armnn::DataLayout::NCHW);
}

BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNchwUint8)
{
    DepthToSpaceEndToEnd<armnn::DataType::QAsymmU8>(defaultBackends, armnn::DataLayout::NCHW);
}

BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNchwInt16)
{
    DepthToSpaceEndToEnd<armnn::DataType::QSymmS16>(defaultBackends, armnn::DataLayout::NCHW);
}

BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNhwcFloat32)
{
    DepthToSpaceEndToEnd<armnn::DataType::Float32>(defaultBackends, armnn::DataLayout::NHWC);
}

BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNhwcFloat16)
{
    DepthToSpaceEndToEnd<armnn::DataType::Float16>(defaultBackends, armnn::DataLayout::NHWC);
}

BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNhwcUint8)
{
    DepthToSpaceEndToEnd<armnn::DataType::QAsymmU8>(defaultBackends, armnn::DataLayout::NHWC);
}

BOOST_AUTO_TEST_CASE(DephtToSpaceEndToEndNhwcInt16)
{
    DepthToSpaceEndToEnd<armnn::DataType::QSymmS16>(defaultBackends, armnn::DataLayout::NHWC);
}

// Dequantize
BOOST_AUTO_TEST_CASE(DequantizeEndToEndSimpleTest)
{
    DequantizeEndToEndSimple<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(DequantizeEndToEndOffsetTest)
{
    DequantizeEndToEndOffset<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonEluEndToEndTestFloat32)
{
    EluEndToEndTest<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonEluEndToEndTestFloat16)
{
    EluEndToEndTest<armnn::DataType::Float16>(defaultBackends);
}

// HardSwish
BOOST_AUTO_TEST_CASE(NeonHardSwishEndToEndTestFloat32)
{
    HardSwishEndToEndTest<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonHardSwishEndToEndTestFloat16)
{
    HardSwishEndToEndTest<armnn::DataType::Float16>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonHardSwishEndToEndTestQAsymmS8)
{
    HardSwishEndToEndTest<armnn::DataType::QAsymmS8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonHardSwishEndToEndTestQAsymmU8)
{
    HardSwishEndToEndTest<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonPreluEndToEndFloat32Test)
{
    PreluEndToEndNegativeTest<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonPreluEndToEndTestUint8Test)
{
    PreluEndToEndPositiveTest<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSpaceToDepthNhwcEndToEndTest1)
{
    SpaceToDepthNhwcEndToEndTest1(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSpaceToDepthNchwEndToEndTest1)
{
    SpaceToDepthNchwEndToEndTest1(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSpaceToDepthNhwcEndToEndTest2)
{
    SpaceToDepthNhwcEndToEndTest2(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSpaceToDepthNchwEndToEndTest2)
{
    SpaceToDepthNchwEndToEndTest2(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter1dEndToEndTest)
{
    Splitter1dEndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter1dEndToEndUint8Test)
{
    Splitter1dEndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter2dDim0EndToEndTest)
{
    Splitter2dDim0EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter2dDim1EndToEndTest)
{
    Splitter2dDim1EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter2dDim0EndToEndUint8Test)
{
    Splitter2dDim0EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter2dDim1EndToEndUint8Test)
{
    Splitter2dDim1EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter3dDim0EndToEndTest)
{
    Splitter3dDim0EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter3dDim1EndToEndTest)
{
    Splitter3dDim1EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter3dDim2EndToEndTest)
{
    Splitter3dDim2EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter3dDim0EndToEndUint8Test)
{
    Splitter3dDim0EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter3dDim1EndToEndUint8Test)
{
    Splitter3dDim1EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter3dDim2EndToEndUint8Test)
{
    Splitter3dDim2EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim0EndToEndTest)
{
    Splitter4dDim0EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim1EndToEndTest)
{
    Splitter4dDim1EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim2EndToEndTest)
{
    Splitter4dDim2EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim3EndToEndTest)
{
    Splitter4dDim3EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim0EndToEndUint8Test)
{
    Splitter4dDim0EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim1EndToEndUint8Test)
{
    Splitter4dDim1EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim2EndToEndUint8Test)
{
    Splitter4dDim2EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonSplitter4dDim3EndToEndUint8Test)
{
    Splitter4dDim3EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonQuantizedLstmEndToEndTest)
{
    QuantizedLstmEndToEnd(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonTransposeConvolution2dEndToEndFloatNchwTest)
{
    TransposeConvolution2dEndToEnd<armnn::DataType::Float32, armnn::DataType::Float32>(
        defaultBackends, armnn::DataLayout::NCHW);
}

BOOST_AUTO_TEST_CASE(NeonTransposeConvolution2dEndToEndUint8NchwTest)
{
    TransposeConvolution2dEndToEnd<armnn::DataType::QAsymmU8, armnn::DataType::Signed32>(
        defaultBackends, armnn::DataLayout::NCHW);
}

BOOST_AUTO_TEST_CASE(NeonTransposeConvolution2dEndToEndFloatNhwcTest)
{
    TransposeConvolution2dEndToEnd<armnn::DataType::Float32, armnn::DataType::Float32>(
        defaultBackends, armnn::DataLayout::NHWC);
}

BOOST_AUTO_TEST_CASE(NeonTransposeConvolution2dEndToEndUint8NhwcTest)
{
    TransposeConvolution2dEndToEnd<armnn::DataType::QAsymmU8, armnn::DataType::Signed32>(
        defaultBackends, armnn::DataLayout::NHWC);
}

BOOST_AUTO_TEST_CASE(NeonImportNonAlignedInputPointerTest)
{
    ImportNonAlignedInputPointerTest(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonExportNonAlignedOutputPointerTest)
{
    ExportNonAlignedOutputPointerTest(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonImportAlignedPointerTest)
{
    ImportAlignedPointerTest(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonImportOnlyWorkload)
{
    ImportOnlyWorkload(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonExportOnlyWorkload)
{
    ExportOnlyWorkload(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonImportAndExportWorkload)
{
    ImportAndExportWorkload(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonExportOutputWithSeveralOutputSlotConnectionsTest)
{
    ExportOutputWithSeveralOutputSlotConnectionsTest(defaultBackends);
}

// InstanceNormalization
BOOST_AUTO_TEST_CASE(NeonInstanceNormalizationNchwEndToEndTest1)
{
    InstanceNormalizationNchwEndToEndTest1(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonInstanceNormalizationNchwEndToEndTest2)
{
    InstanceNormalizationNchwEndToEndTest2(defaultBackends);
}

// Fill
BOOST_AUTO_TEST_CASE(NeonFillEndToEndTest)
{
    FillEndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(RefFillEndToEndTestFloat16)
{
    FillEndToEnd<armnn::DataType::Float16>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonFillEndToEndTestInt32)
{
    FillEndToEnd<armnn::DataType::Signed32>(defaultBackends);
}

// ArgMinMax
BOOST_AUTO_TEST_CASE(NeonArgMaxSimpleTest)
{
    ArgMaxEndToEndSimple<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinSimpleTest)
{
    ArgMinEndToEndSimple<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis0Test)
{
    ArgMaxAxis0EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis0Test)
{
    ArgMinAxis0EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis1Test)
{
    ArgMaxAxis1EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis1Test)
{
    ArgMinAxis1EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis2Test)
{
    ArgMaxAxis2EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis2Test)
{
    ArgMinAxis2EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis3Test)
{
    ArgMaxAxis3EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis3Test)
{
    ArgMinAxis3EndToEnd<armnn::DataType::Float32>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxSimpleTestQuantisedAsymm8)
{
    ArgMaxEndToEndSimple<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinSimpleTestQuantisedAsymm8)
{
    ArgMinEndToEndSimple<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis0TestQuantisedAsymm8)
{
    ArgMaxAxis0EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis0TestQuantisedAsymm8)
{
    ArgMinAxis0EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis1TestQuantisedAsymm8)
{
    ArgMaxAxis1EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis1TestQuantisedAsymm8)
{
    ArgMinAxis1EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis2TestQuantisedAsymm8)
{
    ArgMaxAxis2EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis2TestQuantisedAsymm8)
{
    ArgMinAxis2EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMaxAxis3TestQuantisedAsymm8)
{
    ArgMaxAxis3EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonArgMinAxis3TestQuantisedAsymm8)
{
    ArgMinAxis3EndToEnd<armnn::DataType::QAsymmU8>(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonStridedSliceInvalidSliceEndToEndTest)
{
    StridedSliceInvalidSliceEndToEndTest(defaultBackends);
}

BOOST_AUTO_TEST_CASE(NeonDetectionPostProcessRegularNmsTest, * boost::unit_test::disabled())
{
    std::vector<float> boxEncodings({
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, -1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f
                                    });
    std::vector<float> scores({
                                  0.0f, 0.9f, 0.8f,
                                  0.0f, 0.75f, 0.72f,
                                  0.0f, 0.6f, 0.5f,
                                  0.0f, 0.93f, 0.95f,
                                  0.0f, 0.5f, 0.4f,
                                  0.0f, 0.3f, 0.2f
                              });
    std::vector<float> anchors({
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 100.5f, 1.0f, 1.0f
                               });
    DetectionPostProcessRegularNmsEndToEnd<armnn::DataType::Float32>(defaultBackends, boxEncodings, scores, anchors);
}

inline void QuantizeData(uint8_t* quant, const float* dequant, const TensorInfo& info)
{
    for (size_t i = 0; i < info.GetNumElements(); i++)
    {
        quant[i] = armnn::Quantize<uint8_t>(dequant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
    }
}

BOOST_AUTO_TEST_CASE(NeonDetectionPostProcessRegularNmsUint8Test, * boost::unit_test::disabled())
{
    armnn::TensorInfo boxEncodingsInfo({ 1, 6, 4 }, armnn::DataType::Float32);
    armnn::TensorInfo scoresInfo({ 1, 6, 3 }, armnn::DataType::Float32);
    armnn::TensorInfo anchorsInfo({ 6, 4 }, armnn::DataType::Float32);

    boxEncodingsInfo.SetQuantizationScale(1.0f);
    boxEncodingsInfo.SetQuantizationOffset(1);
    scoresInfo.SetQuantizationScale(0.01f);
    scoresInfo.SetQuantizationOffset(0);
    anchorsInfo.SetQuantizationScale(0.5f);
    anchorsInfo.SetQuantizationOffset(0);

    std::vector<float> boxEncodings({
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, -1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f
                                    });
    std::vector<float> scores({
                                  0.0f, 0.9f, 0.8f,
                                  0.0f, 0.75f, 0.72f,
                                  0.0f, 0.6f, 0.5f,
                                  0.0f, 0.93f, 0.95f,
                                  0.0f, 0.5f, 0.4f,
                                  0.0f, 0.3f, 0.2f
                              });
    std::vector<float> anchors({
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 100.5f, 1.0f, 1.0f
                               });

    std::vector<uint8_t> qBoxEncodings(boxEncodings.size(), 0);
    std::vector<uint8_t> qScores(scores.size(), 0);
    std::vector<uint8_t> qAnchors(anchors.size(), 0);
    QuantizeData(qBoxEncodings.data(), boxEncodings.data(), boxEncodingsInfo);
    QuantizeData(qScores.data(), scores.data(), scoresInfo);
    QuantizeData(qAnchors.data(), anchors.data(), anchorsInfo);
    DetectionPostProcessRegularNmsEndToEnd<armnn::DataType::QAsymmU8>(defaultBackends, qBoxEncodings,
                                                                             qScores, qAnchors,
                                                                             1.0f, 1, 0.01f, 0, 0.5f, 0);
}

BOOST_AUTO_TEST_CASE(NeonDetectionPostProcessFastNmsTest, * boost::unit_test::disabled())
{
    std::vector<float> boxEncodings({
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, -1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f
                                    });
    std::vector<float> scores({
                                  0.0f, 0.9f, 0.8f,
                                  0.0f, 0.75f, 0.72f,
                                  0.0f, 0.6f, 0.5f,
                                  0.0f, 0.93f, 0.95f,
                                  0.0f, 0.5f, 0.4f,
                                  0.0f, 0.3f, 0.2f
                              });
    std::vector<float> anchors({
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 100.5f, 1.0f, 1.0f
                               });
    DetectionPostProcessFastNmsEndToEnd<armnn::DataType::Float32>(defaultBackends, boxEncodings, scores, anchors);
}

BOOST_AUTO_TEST_CASE(NeonDetectionPostProcessFastNmsUint8Test, * boost::unit_test::disabled())
{
    armnn::TensorInfo boxEncodingsInfo({ 1, 6, 4 }, armnn::DataType::Float32);
    armnn::TensorInfo scoresInfo({ 1, 6, 3 }, armnn::DataType::Float32);
    armnn::TensorInfo anchorsInfo({ 6, 4 }, armnn::DataType::Float32);

    boxEncodingsInfo.SetQuantizationScale(1.0f);
    boxEncodingsInfo.SetQuantizationOffset(1);
    scoresInfo.SetQuantizationScale(0.01f);
    scoresInfo.SetQuantizationOffset(0);
    anchorsInfo.SetQuantizationScale(0.5f);
    anchorsInfo.SetQuantizationOffset(0);

    std::vector<float> boxEncodings({
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, -1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f,
                                        0.0f, 1.0f, 0.0f, 0.0f,
                                        0.0f, 0.0f, 0.0f, 0.0f
                                    });
    std::vector<float> scores({
                                  0.0f, 0.9f, 0.8f,
                                  0.0f, 0.75f, 0.72f,
                                  0.0f, 0.6f, 0.5f,
                                  0.0f, 0.93f, 0.95f,
                                  0.0f, 0.5f, 0.4f,
                                  0.0f, 0.3f, 0.2f
                              });
    std::vector<float> anchors({
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 0.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 10.5f, 1.0f, 1.0f,
                                   0.5f, 100.5f, 1.0f, 1.0f
                               });

    std::vector<uint8_t> qBoxEncodings(boxEncodings.size(), 0);
    std::vector<uint8_t> qScores(scores.size(), 0);
    std::vector<uint8_t> qAnchors(anchors.size(), 0);
    QuantizeData(qBoxEncodings.data(), boxEncodings.data(), boxEncodingsInfo);
    QuantizeData(qScores.data(), scores.data(), scoresInfo);
    QuantizeData(qAnchors.data(), anchors.data(), anchorsInfo);
    DetectionPostProcessFastNmsEndToEnd<armnn::DataType::QAsymmU8>(defaultBackends, qBoxEncodings,
                                                                          qScores, qAnchors,
                                                                          1.0f, 1, 0.01f, 0, 0.5f, 0);
}

BOOST_AUTO_TEST_CASE(NeonQLstmEndToEndTest)
{
    QLstmEndToEnd(defaultBackends);
}

BOOST_AUTO_TEST_SUITE_END()


text:
/*
 * background_ext.cc
 *
 *  Copyright (C) 2013 Diamond Light Source
 *
 *  Author: James Parkhurst
 *
 *  This code is distributed under the BSD license, a copy of which is
 *  included in the root directory of this package.
 */
#include <boost/python.hpp>
#include <boost/python/def.hpp>

namespace dials { namespace algorithms { namespace background { namespace boost_python {

  using namespace boost::python;

  void export_outlier_rejector();
  void export_modeller();
  void export_creator();

  BOOST_PYTHON_MODULE(dials_algorithms_background_simple_ext) {
    export_outlier_rejector();
    export_modeller();
    export_creator();
  }

}}}}  // namespace dials::algorithms::background::boost_python

ext: cc

max_stars_repo_name: TiankunZhou/dials

max_issues_repo_name: TiankunZhou/dials

max_forks_repo_name: TiankunZhou/dials


text:
#include <docopt.h>
#include <OpenNI.h>

#include <functional>
#include <map>
#include <vector>
#include <utility>
#include <stdio.h>
#include <signal.h>
#include <boost/lexical_cast.hpp>
#include <spdlog/spdlog.h>

#include <Camera.hpp>
#include <Space.hpp>

#include <cameras/OpenNI.cc>
#include <cameras/VideoStream.cc>
#include <trackers/Debug.cc>
#include <trackers/ChiliTracker.cc>
#include <trackers/FingerTracker.cc>
#include <spaces/Plane.cc>
#include <publishers/WebSocket.cc>
#include <publishers/TUIO.cc>

using namespace SPRITS;

static bool stop = false;

static const char USAGE[] =
R"(Simple Pluggable Range Imaging Tracking Server.

    Usage:
      SPRITS [options]
      SPRITS [options] (OpenNI|VideoStream)

    Options:
      --help               Show this screen.
      --crop               Crop camera image.
      --debug              Enable debug window.
      --record             Enable camera recording.
      --tuio               Enable TUIO publisher.
      --verbose            Enable verbose logging.
      --websocket=<port>   Enable Websocket publisher [default port: 9002].
      --version            Show version.
)";

int main(int argc, char **argv)
{
	auto console = spdlog::stdout_logger_mt("console", true);
	try
	{
		std::map<std::string, docopt::value> args = docopt::docopt(USAGE, { argv + 1, argv + argc }, true, "SPRITS 1.0");
		console->set_level(args["--verbose"].asBool()?spdlog::level::debug:spdlog::level::info);
		signal(SIGINT, [](int nSig) { stop = true; });
		Camera* cam = args["OpenNI"].asBool()?(Camera*)new OpenNI(args["--crop"].asBool(), args["--debug"].asBool()):(Camera*)new VideoStream(args["--crop"].asBool(), args["--debug"].asBool());
		Space<std::tuple<double, double, double>>* spc = new Plane();
		CameraObserver<std::tuple<double, double, double>>* fpsobs = new ChiliTracker(new Debug3DTracker(cam, spc, args["--record"].asBool(), NewFrameEvent::COLOR));
		std::list<SpaceObserver<std::tuple<double, double, double>>*> publishers;
		if (args["--tuio"].asBool())
			publishers.push_back(new TUIOPublisher(spc));
		if ((args["--websocket"].isBool()) && (args["--websocket"].asBool()))
			publishers.push_back(new WebSocketPublisher(spc));
		else
			publishers.push_back(new WebSocketPublisher(spc, boost::lexical_cast<int>(args["--websocket"].asString())));
		while (!stop)
			cam->update();
		for (auto const& pub : publishers)
			delete pub;
		delete fpsobs;
		delete cam;
	} catch (std::exception& e)
	{
		spdlog::get("console")->critical("ERROR: {}", e.what());
	}
	spdlog::drop_all();
	exit(0);
}

max_stars_repo_name: tommasoturchi/SPRITS

max_issues_repo_name: tommasoturchi/SPRITS

max_forks_repo_name: tommasoturchi/SPRITS


text:
#include "biggles/io.hpp"
#include "biggles/observation.hpp"
#include "biggles/partition.hpp"
#include "biggles/simulate.hpp"
#include "biggles/track.hpp"
#include "biggles/detail/physics.hpp"
#include <boost/foreach.hpp>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <iostream>
#include <sstream>
#include <stdlib.h>

// include this last to stop pre-processor macros breaking things
extern "C" {
#include <ccan/tap/tap.h>
}

using namespace biggles;

boost::shared_ptr<const partition> simulate_partition(const model::parameters& params)
{

    // simulate full data
    partition_ptr_t p(new partition());
    simulate::generate_partition(p, params, 0, 128 + 128, 128.f, 128.f);

    diag("Generated %zu new tracks.", p->tracks().size());
    diag("Generated %zu clutter observations.", p->clutter().size());

    // crop partition to a specific region
    simulate::crop_partition(p, p, 0.f, 128.f, 0.f, 128.f, 64, 64 + 128);

    diag("Cropped to %zu tracks.", p->tracks().size());
    diag("Cropped to %zu clutter observations.", p->clutter().size());

    return p;
}

int main(int argc, char** argv)
{
    plan_tests(2 + 2 + 1 + 3);

    // initialise model parameters
    model::parameters params;
    model::mean_new_tracks_per_frame(params)           = 1.5f;
    model::mean_false_observations_per_frame(params)   = 2.f;
    model::frame_to_frame_survival_probability(params) = 0.95f;
    model::generate_observation_probability(params)    = 0.8f;
    float sigma_R = 0.1f;
    model::observation_error_covariance(params)       << sigma_R * sigma_R, 0, 0, sigma_R * sigma_R;
    model::process_noise_covariance(params) = detail::initQ();

    {
        // serialise it to a string
        std::stringstream write_stream;
        io::write_model_parameters_to_json_stream(write_stream, params);
        std::string serialised_model_parameters(write_stream.str());

        // check length of string is sensible
        diag("Serialised representation is %zu characters.", serialised_model_parameters.size());
        ok1(serialised_model_parameters.size() > 10);

        // de-serialise
        std::stringstream read_stream(serialised_model_parameters);
        boost::shared_ptr<const model::parameters> recovered_model_parameters_ptr(
            io::read_model_parameters_from_json_stream(read_stream));

        // check equality (dangerous!)
        ok1(*recovered_model_parameters_ptr == params);
    }

    // create a simulated partition
    boost::shared_ptr<const partition> source_partition_ptr(simulate_partition(params));
    ok1(source_partition_ptr->clutter().size() > 0);
    ok1(source_partition_ptr->tracks().size() > 0);

    {
        // serialise it to a string
        std::stringstream write_stream;
        io::write_partition_to_json_stream(write_stream, *source_partition_ptr);
        std::string serialised_partition(write_stream.str());

        // check length of string is sensible
        diag("Serialised representation is %zu characters.", serialised_partition.size());
        ok1(serialised_partition.size() > 10);

        // de-serialise
        std::stringstream read_stream(serialised_partition);
        boost::shared_ptr<const partition> recovered_partition_ptr(
            io::read_partition_from_json_stream(read_stream));

        // check that this is a different partition in memory but has the same shape
        ok1(source_partition_ptr != recovered_partition_ptr);
        ok1(recovered_partition_ptr->clutter().size() == source_partition_ptr->clutter().size());
        ok1(recovered_partition_ptr->tracks().size() == source_partition_ptr->tracks().size());

        // FIXME: be a little more thorough in the checking
    }

    return exit_status();
}

max_stars_repo_name: fbi-octopus/biggles

max_issues_repo_name: fbi-octopus/biggles

max_forks_repo_name: fbi-octopus/biggles


text:
//
//  Copyright Silvin Lubecki 2012
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//

#include "depend_pch.h"
#include "GraphMLSerializer.h"
#include "Visitable.h"
#include <xeumeuleu/xml.hpp>
#include <boost/foreach.hpp>

using namespace depend;

// -----------------------------------------------------------------------------
// Name: GraphMLSerializer constructor
// Created: SLI 2012-07-09
// -----------------------------------------------------------------------------
GraphMLSerializer::GraphMLSerializer( const Visitable< DependencyVisitor_ABC >& dependencies, const Visitable< UnitObserver_ABC >& units )
{
    units.Apply( *this );
    dependencies.Apply( *this );
}

// -----------------------------------------------------------------------------
// Name: GraphMLSerializer destructor
// Created: SLI 2012-07-09
// -----------------------------------------------------------------------------
GraphMLSerializer::~GraphMLSerializer()
{
    // NOTHING
}

// -----------------------------------------------------------------------------
// Name: GraphMLSerializer::Serialize
// Created: SLI 2012-07-09
// -----------------------------------------------------------------------------
void GraphMLSerializer::Serialize( xml::xostream& xos ) const
{
    xos << xml::start( "graphml" )
            << xml::start( "graph" )
                << xml::attribute( "id", "G" )
                << xml::attribute( "edgedefault", "directed" );
    BOOST_FOREACH( const std::string& module, modules_ )
        xos     << xml::start( "node" )
                    << xml::attribute( "id", module )
                << xml::end;
    BOOST_FOREACH( const T_Dependencies::value_type& from, dependencies_ )
        BOOST_FOREACH( const std::string& to, from.second )
            xos << xml::start( "edge" )
                    << xml::attribute( "source", from.first )
                    << xml::attribute( "target", to )
                << xml::end;
    xos     << xml::end
        << xml::end;
}

// -----------------------------------------------------------------------------
// Name: GraphMLSerializer::NotifyInternalDependency
// Created: SLI 2012-07-09
// -----------------------------------------------------------------------------
void GraphMLSerializer::NotifyInternalDependency( const std::string& fromModule, const std::string& toModule, const std::string& /*context*/ )
{
    modules_.insert( fromModule );
    modules_.insert( toModule );
    dependencies_[ fromModule ].insert( toModule );
}

// -----------------------------------------------------------------------------
// Name: GraphMLSerializer::NotifyExternalDependency
// Created: SLI 2012-07-09
// -----------------------------------------------------------------------------
void GraphMLSerializer::NotifyExternalDependency( const std::string& fromModule, const std::string& toModule, const std::string& /*context*/ )
{
    modules_.insert( fromModule );
    modules_.insert( toModule );
    dependencies_[ fromModule ].insert( toModule );
}

// -----------------------------------------------------------------------------
// Name: GraphMLSerializer::NotifyUnit
// Created: SLI 2012-07-09
// -----------------------------------------------------------------------------
void GraphMLSerializer::NotifyUnit( const std::string& unit, const std::string& /*context*/ )
{
    modules_.insert( unit );
}

max_stars_repo_name: vinsle/dependency-analysis

max_issues_repo_name: vinsle/dependency-analysis

max_forks_repo_name: vinsle/dependency-analysis


text:
/*
 * File:   histogram_Container_tests.cc
 * Author: ts337
 *
 * Created on 07 February 2011, 12:41
 */
#include <UnitTest++/UnitTest++.h>
#include <stdexcept>
#include "../boost_serialization_body_includes.h"
#include <boost/filesystem.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <iostream>
#include <sstream>
using namespace coela;
using namespace std;

SUITE(Serialization)
{

    string test_suite_output_dir= string(UnitTestSuite::GetSuiteName()) + "_tests/";

    TEST(Run_Standard_Suite_Setup) {
        cout << "*** \""<< UnitTestSuite::GetSuiteName() <<"\" unit tests running ***" <<endl;
        boost::filesystem::create_directories(test_suite_output_dir);
    }

    TEST(basic_variable_serialization) {
        double x=12.45;
        stringstream ss;
        boost_serialization::save_serializable_object_to_ostream(ss,x);
        double y =   boost_serialization::load_serializable_object_from_istream<double>(ss);
        CHECK_EQUAL(x,y);
    }

    class DummyClass {
    public:
        double x,y;
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int /* file_version */) {
            ar  & BOOST_SERIALIZATION_NVP(x)
            & BOOST_SERIALIZATION_NVP(y)
            ;
        }

    };
//
    TEST(basic_class_serialization) {
        DummyClass d;
        d.x=1.2;
        d.y=1.3;

        DummyClass e;

        stringstream ss;

//        boost::archive::text_oarchive oa(ss);
//        oa <<  BOOST_SERIALIZATION_NVP(d);
//        boost::archive::text_iarchive ia(ss);
//        ia >> BOOST_SERIALIZATION_NVP(e);

        boost_serialization::save_serializable_object_to_ostream(ss,d);
        e = boost_serialization::load_serializable_object_from_istream<DummyClass>(ss);

        CHECK_EQUAL(d.x, e.x);
        CHECK_EQUAL(d.y, e.y);

    }
}

max_stars_repo_name: timstaley/coelacanth

max_issues_repo_name: timstaley/coelacanth

max_forks_repo_name: timstaley/coelacanth


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

#include <boost/algorithm/string/join.hpp>

#include "yb/common/ql_protocol_util.h"
#include "yb/common/ql_value.h"

#include "yb/docdb/docdb.h"
#include "yb/docdb/docdb_debug.h"

#include "yb/tablet/tablet-test-util.h"
#include "yb/tablet/tablet.h"
#include "yb/tablet/local_tablet_writer.h"
#include "yb/util/random_util.h"
#include "yb/util/size_literals.h"

DECLARE_int64(db_write_buffer_size);
DECLARE_bool(rocksdb_disable_compactions);
DECLARE_int32(rocksdb_level0_file_num_compaction_trigger);

namespace yb {
namespace tablet {

class TabletSplitTest : public YBTabletTest {
 public:
  TabletSplitTest() : YBTabletTest(Schema({ ColumnSchema("key", INT32, false, true),
                                            ColumnSchema("val", STRING) },
                                          1)) {}

  void SetUp() override {
    FLAGS_db_write_buffer_size = 1_MB;
    FLAGS_rocksdb_level0_file_num_compaction_trigger = -1;
    YBTabletTest::SetUp();
    writer_.reset(new LocalTabletWriter(tablet().get()));
  }

 protected:

  docdb::DocKeyHash InsertRow(int key, const std::string& val, LocalTabletWriter::Batch* batch) {
    QLWriteRequestPB* req = batch->Add();
    req->set_type(QLWriteRequestPB::QL_STMT_INSERT);
    QLAddInt32HashValue(req, key);
    QLAddStringColumnValue(req, kFirstColumnId + 1, val);
    QLSetHashCode(req);
    return req->hash_code();
  }

  Result<std::vector<QLRow>> SelectAll(Tablet* tablet) {
    ReadHybridTime read_time = ReadHybridTime::SingleTime(VERIFY_RESULT(tablet->SafeTime()));
    QLReadRequestPB req;
    QLAddColumns(schema_, {}, &req);
    QLReadRequestResult result;
    EXPECT_OK(tablet->HandleQLReadRequest(
        CoarseTimePoint::max(), read_time, req, TransactionMetadataPB(), &result));

    EXPECT_EQ(QLResponsePB::YQL_STATUS_OK, result.response.status());

    return CreateRowBlock(QLClient::YQL_CLIENT_CQL, schema_, result.rows_data)->rows();
  }

  docdb::DocKeyHash GetRowHashCode(const QLRow& row) {
    std::string tmp;
    AppendToKey(row.column(0).value(), &tmp);
    return YBPartition::HashColumnCompoundValue(tmp);
  }

  std::unique_ptr<LocalTabletWriter> writer_;
};

namespace {

boost::optional<docdb::DocKeyHash> PartitionKeyToHash(const std::string& partition_key) {
  if (partition_key.empty()) {
    return boost::none;
  } else {
    return PartitionSchema::DecodeMultiColumnHashValue(partition_key);
  }
}

} // namespace

TEST_F(TabletSplitTest, SplitTablet) {
  constexpr auto kNumRows = 10000;
  constexpr auto kValuePrefixLength = 1024;
  constexpr auto kRowsPerSourceFlush = kNumRows / 7;
  constexpr auto kNumSplits = 5;

  const auto value_format = RandomHumanReadableString(kValuePrefixLength) + "_$0";
  docdb::DocKeyHash min_hash_code = std::numeric_limits<docdb::DocKeyHash>::max();
  docdb::DocKeyHash max_hash_code = std::numeric_limits<docdb::DocKeyHash>::min();
  {
    LocalTabletWriter::Batch batch;
    for (auto i = 1; i <= kNumRows; ++i) {
      const auto hash_code = InsertRow(i, Format(value_format, i), &batch);
      min_hash_code = std::min(min_hash_code, hash_code);
      max_hash_code = std::max(max_hash_code, hash_code);
      if (i % kRowsPerSourceFlush == 0) {
        ASSERT_OK(writer_->WriteBatch(&batch));
        batch.Clear();
        ASSERT_OK(tablet()->Flush(FlushMode::kSync));
      }
    }
    if (!batch.empty()) {
      ASSERT_OK(writer_->WriteBatch(&batch));
    }
  }

  VLOG(1) << "Source tablet:" << std::endl
          << docdb::DocDBDebugDumpToStr(tablet()->doc_db(), docdb::IncludeBinary::kTrue);
  const auto source_docdb_dump_str = tablet()->TEST_DocDBDumpStr(IncludeIntents::kTrue);
  std::unordered_set<std::string> source_docdb_dump;
  tablet()->TEST_DocDBDumpToContainer(IncludeIntents::kTrue, &source_docdb_dump);

  std::unordered_set<std::string> source_rows;
  for (const auto& row : ASSERT_RESULT(SelectAll(tablet().get()))) {
    source_rows.insert(row.ToString());
  }
  auto source_rows2 = source_rows;

  std::vector<TabletPtr> split_tablets;

  std::shared_ptr<Partition> partition = tablet()->metadata()->partition();
  docdb::KeyBounds key_bounds;
  for (auto i = 1; i <= kNumSplits + 1; ++i) {
    const auto subtablet_id = Format("$0-sub-$1", tablet()->tablet_id(), yb::ToString(i));

    // Last sub tablet will contain only one hash to explicitly test this case.
    if (i <= kNumSplits) {
      const docdb::DocKeyHash split_hash_code =
          min_hash_code + i * static_cast<uint32>(max_hash_code - min_hash_code) / kNumSplits;
      LOG(INFO) << "Split hash code: " << split_hash_code;
      const auto partition_key = PartitionSchema::EncodeMultiColumnHashValue(split_hash_code);
      docdb::KeyBytes encoded_doc_key;
      docdb::DocKeyEncoderAfterTableIdStep(&encoded_doc_key).Hash(
          split_hash_code, std::vector<docdb::PrimitiveValue>());
      partition->set_partition_key_end(partition_key);
      key_bounds.upper = encoded_doc_key;
    } else {
      partition->set_partition_key_end("");
      key_bounds.upper.Clear();
    }

    ASSERT_OK(tablet()->CreateSubtablet(
        subtablet_id, *partition, key_bounds, yb::OpId() /* split_op_id */,
        HybridTime() /* split_hybrid_time */));
    split_tablets.push_back(ASSERT_RESULT(harness_->OpenTablet(subtablet_id)));

    partition->set_partition_key_start(partition->partition_key_end());
    key_bounds.lower = key_bounds.upper;
  }

  for (auto split_tablet : split_tablets) {
    {
      RaftGroupReplicaSuperBlockPB super_block;
      split_tablet->metadata()->ToSuperBlock(&super_block);
      ASSERT_EQ(split_tablet->tablet_id(), super_block.kv_store().kv_store_id());
    }
    const auto split_docdb_dump_str = split_tablet->TEST_DocDBDumpStr(IncludeIntents::kTrue);

    // Before compaction underlying DocDB dump should be the same.
    ASSERT_EQ(source_docdb_dump_str, split_docdb_dump_str);

    // But split tablets should only return relevant data without overlap and no unexpected data.
    const auto& split_partition = split_tablet->metadata()->partition();
    const auto start_hash = PartitionKeyToHash(split_partition->partition_key_start());
    const auto end_hash = PartitionKeyToHash(split_partition->partition_key_end());

    for (const auto& row : ASSERT_RESULT(SelectAll(split_tablet.get()))) {
      const auto hash_code = GetRowHashCode(row);
      if (start_hash) {
        ASSERT_GE(hash_code, *start_hash);
      }
      if (end_hash) {
        ASSERT_LT(hash_code, *end_hash);
      }
      ASSERT_EQ(source_rows.erase(row.ToString()), 1);
    }

    split_tablet->ForceRocksDBCompactInTest();

    VLOG(1) << split_tablet->tablet_id() << " compacted:" << std::endl
            << split_tablet->TEST_DocDBDumpStr(IncludeIntents::kTrue);

    // After compaction split tablets' RocksDB instances should have no overlap and no unexpected
    // data.
    std::unordered_set<std::string> split_docdb_dump;
    split_tablet->TEST_DocDBDumpToContainer(IncludeIntents::kTrue, &split_docdb_dump);
    for (const auto& entry : split_docdb_dump) {
      ASSERT_EQ(source_docdb_dump.erase(entry), 1);
    }

    // Check data returned by tablet.
    for (const auto& row : ASSERT_RESULT(SelectAll(split_tablet.get()))) {
      ASSERT_EQ(source_rows2.erase(row.ToString()), 1);
    }

    // Each split tablet data size should be less than original data size divided by number
    // of split points.
    ASSERT_LT(
        split_tablet->doc_db().regular->GetCurrentVersionDataSstFilesSize(),
        tablet()->doc_db().regular->GetCurrentVersionDataSstFilesSize() / kNumSplits);
  }

  // Split tablets should have all data from the source tablet.
  ASSERT_TRUE(source_rows.empty()) << boost::algorithm::join(source_rows, "\n");
  ASSERT_TRUE(source_rows2.empty()) << boost::algorithm::join(source_rows2, "\n");
  ASSERT_TRUE(source_docdb_dump.empty()) << boost::algorithm::join(source_docdb_dump, "\n");
}

// TODO: Need to test with distributed transactions both pending and committed
// (but not yet applied) during split.
// Split tablets should not return unexpected data for not yet applied, but committed transactions
// before and after compaction.
// Also check that non-relevant intents are cleaned from split intents DB after compaction.
//
// This test would be possible as an integration test when upper layers of tablet splitting are
// implemented.

} // namespace tablet
} // namespace yb

max_stars_repo_name: tichimura/yugabyte-db

max_issues_repo_name: tichimura/yugabyte-db

max_forks_repo_name: tichimura/yugabyte-db