void write_node(const tree_type & tree,
         const typename tree_type::iterator & node_iter,
         std::ostream& out) const {
     bool is_leaf = node_iter.is_leaf();
     if (!is_leaf) {
         out << "(";
         int ch_count = 0;
         for (auto chi = tree.children_begin(node_iter);
                 chi != tree.children_end(node_iter);
                 ++chi, ++ch_count) {
             if (ch_count > 0) {
                 out << ",";
                 if (!this->compact_spaces_) {
                     out << " ";
                 }
             }
             this->write_node(tree, chi, out);
         }
         out << ")";
     }
     if (this->node_value_label_getter_ && (is_leaf || !this->suppress_internal_node_labels_)) {
         out << this->node_value_label_getter_(*node_iter);
     }
     if (this->node_value_edge_length_getter_ && !this->suppress_edge_lengths_) {
         out << ":" << std::fixed << std::setprecision(this->edge_length_precision_) << this->node_value_edge_length_getter_(*node_iter);
         out.copyfmt(std::ios(NULL)); // restore state
     }
 }
static
void
add_gvcf_filters(const gvcf_options& opt,
                 const cdmap_t& chrom_depth,
                 std::ostream& os) {

    using namespace VCF_FILTERS;

    write_vcf_filter(os,get_label(IndelConflict),"Locus is in region with conflicting indel calls");
    write_vcf_filter(os,get_label(SiteConflict),"Site genotype conflicts with proximal indel call. This is typically a heterozygous SNV call made inside of a heterozygous deletion");


    if (opt.is_min_gqx) {
        std::ostringstream oss;
        oss << "Locus GQX is less than " << opt.min_gqx << " or not present";
        write_vcf_filter(os,get_label(LowGQX),oss.str().c_str());
    }

    if (opt.is_max_base_filt) {
        std::ostringstream oss;
        oss << "The fraction of basecalls filtered out at a site is greater than " << opt.max_base_filt;
        write_vcf_filter(os,get_label(HighBaseFilt),oss.str().c_str());
    }

    if (opt.is_max_snv_sb) {
        std::ostringstream oss;
        oss << "SNV strand bias value (SNVSB) exceeds " << opt.max_snv_sb;
        write_vcf_filter(os,get_label(HighSNVSB),oss.str().c_str());
    }
    if (opt.is_max_snv_hpol) {
        std::ostringstream oss;
        oss << "SNV contextual homopolymer length (SNVHPOL) exceeds " << opt.max_snv_hpol;
        write_vcf_filter(os,get_label(HighSNVHPOL),oss.str().c_str());
    }

    if (opt.is_max_ref_rep) {
        std::ostringstream oss;
        oss << "Locus contains an indel allele occurring in a homopolymer or dinucleotide track with a reference repeat greater than " << opt.max_ref_rep;
        write_vcf_filter(os,get_label(HighRefRep),oss.str().c_str());
    }

    if (opt.is_max_depth_factor && (! chrom_depth.empty())) {
        std::ostringstream oss;
        oss << "Locus depth is greater than " << opt.max_depth_factor << "x the mean chromosome depth";
        write_vcf_filter(os,get_label(HighDepth),oss.str().c_str());

        std::ofstream tmp_os;
        tmp_os.copyfmt(os);
        os << std::fixed << std::setprecision(2);

        cdmap_t::const_iterator i(chrom_depth.begin()), i_end(chrom_depth.end());
        for (; i!=i_end; ++i) {
            const std::string& chrom(i->first);
            const double max_depth(opt.max_depth_factor*i->second);
            os << "##MaxDepth_" << chrom << '=' << max_depth << "\n";
        }

        os.copyfmt(tmp_os);
    }
}
Esempio n. 3
0
    //
    // Generate hex dump of a "payload"
    //
    static const void dumpPayload(std::ostream &os, const SerfPayload &p)
    {
        int count = 0;
        std::ios init(NULL);
	    init.copyfmt(os);
        os << std::hex << std::setfill('0') << std::setw(2);
        SerfPayload::const_iterator i = p.begin();
        for (; i != p.end(); ++i, ++count) {
            if (count && (count % 8) == 0) os << "\n             ";            
            os << "0x" << (static_cast<int>(*i) & 0xff) << " ";
        }
        os.copyfmt(init);
        os << std::endl;
    }
Esempio n. 4
0
std::vector<Real> Objective<Real>::checkHessSym( const Vector<Real> &x,
                                                 const Vector<Real> &hv,
                                                 const Vector<Real> &v,
                                                 const Vector<Real> &w,
                                                 const bool printToStream,
                                                 std::ostream & outStream ) {

  Real tol = std::sqrt(ROL_EPSILON);
  
  // Compute (Hessian at x) times (vector v).
  Teuchos::RCP<Vector<Real> > h = hv.clone();
  this->hessVec(*h, v, x, tol);
  Real wHv = w.dot(h->dual());

  this->hessVec(*h, w, x, tol);
  Real vHw = v.dot(h->dual());

  std::vector<Real> hsymCheck(3, 0);

  hsymCheck[0] = wHv;
  hsymCheck[1] = vHw;
  hsymCheck[2] = std::abs(vHw-wHv);

  // Save the format state of the original outStream.
  Teuchos::oblackholestream oldFormatState;
  oldFormatState.copyfmt(outStream);

  if (printToStream) {
    outStream << std::right
              << std::setw(20) << "<w, H(x)v>"
              << std::setw(20) << "<v, H(x)w>"
              << std::setw(20) << "abs error"
              << "\n";
    outStream << std::scientific << std::setprecision(11) << std::right
              << std::setw(20) << hsymCheck[0]
              << std::setw(20) << hsymCheck[1]
              << std::setw(20) << hsymCheck[2]
              << "\n";
  }

  // Reset format state of outStream.
  outStream.copyfmt(oldFormatState);

  return hsymCheck;

} // checkHessSym
Esempio n. 5
0
void SockAddress::print(std::ostream &out) const
{
    if(m_Type == V4)
        out << (int)m_Data[0] << '.' << (int)m_Data[1] << '.'
            << (int)m_Data[2] << '.' << (int)m_Data[3];
    else /* m_Type == V6 */
    {
        std::ios state(NULL);
        state.copyfmt(out);
        out << std::hex << std::uppercase;
        out << '[' << (int)m_Data[0];
        for(int i = 1; i < 16; ++i)
            out << ':' << (int)m_Data[i];
        out << ']';
        out.copyfmt(state);
    }
}
Esempio n. 6
0
std::vector<std::vector<Real> > Objective<Real>::checkHessVec( const Vector<Real> &x,
                                                               const Vector<Real> &hv,
                                                               const Vector<Real> &v,
                                                               const std::vector<Real> &steps,
                                                               const bool printToStream,
                                                               std::ostream & outStream,
                                                               const int order ) {

  TEUCHOS_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument, 
                              "Error: finite difference order must be 1,2,3, or 4" );

  using Finite_Difference_Arrays::shifts;
  using Finite_Difference_Arrays::weights;


  Real tol = std::sqrt(ROL_EPSILON);

  int numSteps = steps.size();
  int numVals = 4;
  std::vector<Real> tmp(numVals);
  std::vector<std::vector<Real> > hvCheck(numSteps, tmp);

  // Save the format state of the original outStream.
  Teuchos::oblackholestream oldFormatState;
  oldFormatState.copyfmt(outStream);

  // Compute gradient at x.
  Teuchos::RCP<Vector<Real> > g = hv.clone();
  this->update(x);
  this->gradient(*g, x, tol);

  // Compute (Hessian at x) times (vector v).
  Teuchos::RCP<Vector<Real> > Hv = hv.clone();
  this->hessVec(*Hv, v, x, tol);
  Real normHv = Hv->norm();

  // Temporary vectors.
  Teuchos::RCP<Vector<Real> > gdif = hv.clone();
  Teuchos::RCP<Vector<Real> > gnew = hv.clone();
  Teuchos::RCP<Vector<Real> > xnew = x.clone();

  for (int i=0; i<numSteps; i++) {

    Real eta = steps[i]; 

    // Evaluate objective value at x+eta*d.
    xnew->set(x);

    gdif->set(*g);
    gdif->scale(weights[order-1][0]);

    for(int j=0; j<order; ++j) {

        // Evaluate at x <- x+eta*c_i*d.
        xnew->axpy(eta*shifts[order-1][j], v);

        // Only evaluate at shifts where the weight is nonzero  
        if( weights[order-1][j+1] != 0 ) {
            this->update(*xnew);
            this->gradient(*gnew, *xnew, tol); 
            gdif->axpy(weights[order-1][j+1],*gnew);
        }
       
    }

    gdif->scale(1.0/eta);    

    // Compute norms of hessvec, finite-difference hessvec, and error.
    hvCheck[i][0] = eta;
    hvCheck[i][1] = normHv;
    hvCheck[i][2] = gdif->norm();
    gdif->axpy(-1.0, *Hv);
    hvCheck[i][3] = gdif->norm();

    if (printToStream) {
      if (i==0) {
      outStream << std::right
                << std::setw(20) << "Step size"
                << std::setw(20) << "norm(Hess*vec)"
                << std::setw(20) << "norm(FD approx)"
                << std::setw(20) << "norm(abs error)"
                << "\n";
      }
      outStream << std::scientific << std::setprecision(11) << std::right
                << std::setw(20) << hvCheck[i][0]
                << std::setw(20) << hvCheck[i][1]
                << std::setw(20) << hvCheck[i][2]
                << std::setw(20) << hvCheck[i][3]
                << "\n";
    }

  }

  // Reset format state of outStream.
  outStream.copyfmt(oldFormatState);

  return hvCheck;
} // checkHessVec
Esempio n. 7
0
std::vector<std::vector<Real> > Objective<Real>::checkGradient( const Vector<Real> &x,
                                                                const Vector<Real> &g,
                                                                const Vector<Real> &d,
                                                                const std::vector<Real> &steps,
                                                                const bool printToStream,
                                                                std::ostream & outStream,
                                                                const int order ) {

  TEUCHOS_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument, 
                              "Error: finite difference order must be 1,2,3, or 4" );

  using Finite_Difference_Arrays::shifts;
  using Finite_Difference_Arrays::weights;

  Real tol = std::sqrt(ROL_EPSILON);

  int numSteps = steps.size();
  int numVals = 4;
  std::vector<Real> tmp(numVals);
  std::vector<std::vector<Real> > gCheck(numSteps, tmp);

  // Save the format state of the original outStream.
  Teuchos::oblackholestream oldFormatState;
  oldFormatState.copyfmt(outStream);

  // Evaluate objective value at x.
  this->update(x);
  Real val = this->value(x,tol);

  // Compute gradient at x.
  Teuchos::RCP<Vector<Real> > gtmp = g.clone();
  this->gradient(*gtmp, x, tol);
  Real dtg = d.dot(gtmp->dual());

  // Temporary vectors.
  Teuchos::RCP<Vector<Real> > xnew = x.clone();

  for (int i=0; i<numSteps; i++) {

    Real eta = steps[i];

    xnew->set(x);

    // Compute gradient, finite-difference gradient, and absolute error.
    gCheck[i][0] = eta;
    gCheck[i][1] = dtg;

    gCheck[i][2] = weights[order-1][0] * val;

    for(int j=0; j<order; ++j) {
      // Evaluate at x <- x+eta*c_i*d.
      xnew->axpy(eta*shifts[order-1][j], d);

      // Only evaluate at shifts where the weight is nonzero  
      if( weights[order-1][j+1] != 0 ) {
        this->update(*xnew);
        gCheck[i][2] += weights[order-1][j+1] * this->value(*xnew,tol);
      }
    }

    gCheck[i][2] /= eta;

    gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);

    if (printToStream) {
      if (i==0) {
        outStream << std::right
                  << std::setw(20) << "Step size"
                  << std::setw(20) << "grad'*dir"
                  << std::setw(20) << "FD approx"
                  << std::setw(20) << "abs error"
                  << "\n";
      }
      outStream << std::scientific << std::setprecision(11) << std::right
                << std::setw(20) << gCheck[i][0]
                << std::setw(20) << gCheck[i][1]
                << std::setw(20) << gCheck[i][2]
                << std::setw(20) << gCheck[i][3]
                << "\n";
    }

  }

  // Reset format state of outStream.
  outStream.copyfmt(oldFormatState);

  return gCheck;
} // checkGradient
Esempio n. 8
0
inline void RiskMeasureInfo(Teuchos::ParameterList &parlist, std::string &name,
                            int &nStatistic, std::vector<Real> &lower,
                            std::vector<Real> &upper, bool &isBoundActivated,
                            const bool printToStream = false,
                            std::ostream &outStream = std::cout) {
  name = parlist.sublist("SOL").sublist("Risk Measure").get<std::string>("Name");
  Real zero(0);
  lower.clear(); upper.clear();
  nStatistic = 0; isBoundActivated = false;
  if ( name == "CVaR"                           ||
       name == "HMCR"                           ||
       name == "Moreau-Yosida CVaR"             ||
       name == "Log-Exponential Quadrangle"     ||
       name == "Log-Quantile Quadrangle"        ||
       name == "Mean-Variance Quadrangle"       ||
       name == "Quantile-Based Quadrangle"      ||
       name == "Smoothed Worst-Case Quadrangle" ||
       name == "Truncated Mean Quadrangle" ) {
    nStatistic = 1;
    lower.resize(nStatistic,ROL_NINF<Real>());
    upper.resize(nStatistic,ROL_INF<Real>());
  }
  else if ( name == "Quantile-Radius Quadrangle" ) {
    nStatistic = 2;
    lower.resize(nStatistic,ROL_NINF<Real>());
    upper.resize(nStatistic,ROL_INF<Real>());
  }
  else if ( name == "Coherent Exponential Utility" ||
            name == "KL Divergence" ) {
    nStatistic = 1;
    isBoundActivated = true;
    lower.resize(nStatistic,zero);
    upper.resize(nStatistic,ROL_INF<Real>());
  }
  else if ( name == "Chi-Squared Divergence" ) {
    nStatistic = 2;
    isBoundActivated = true;
    lower.resize(nStatistic,ROL_NINF<Real>()); lower[0] = zero;
    upper.resize(nStatistic,ROL_INF<Real>());
  }
  else if ( name == "Mixed-Quantile Quadrangle" ) {
    Teuchos::ParameterList &list
      = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mixed-Quantile Quadrangle");
    Teuchos::Array<Real> prob
      = Teuchos::getArrayFromStringParameter<Real>(list,"Probability Array");
    nStatistic = prob.size();
    lower.resize(nStatistic,ROL_NINF<Real>());
    upper.resize(nStatistic,ROL_INF<Real>());
  }
  else if ( name == "Super Quantile Quadrangle" ||
            name == "Chebyshev-Kusuoka"         ||
            name == "Spectral Risk" ) {
    Teuchos::ParameterList &list
      = parlist.sublist("SOL").sublist("Risk Measure").sublist(name);
    nStatistic = list.get("Number of Quadrature Points",5);
    lower.resize(nStatistic,ROL_NINF<Real>());
    upper.resize(nStatistic,ROL_INF<Real>());
  }
  else if ( name == "Exponential Utility"             ||
            name == "Mean Plus Deviation From Target" ||
            name == "Mean Plus Deviation"             ||
            name == "Mean Plus Variance From Target"  ||
            name == "Mean Plus Variance" ) {
    nStatistic = 0;
  }
  else if ( name == "Convex Combination Risk Measure" ) {
    Teuchos::ParameterList &list
      = parlist.sublist("SOL").sublist("Risk Measure").sublist("Convex Combination Risk Measure");
    // Get convex combination parameters
    Teuchos::Array<Real> lambda
      = Teuchos::getArrayFromStringParameter<Real>(list,"Convex Combination Parameters");
    // Build risk measures
    std::vector<std::string> riskString;
    for (typename Teuchos::Array<Real>::size_type i = 0; i < lambda.size(); ++i) {
      std::ostringstream convert;
      convert << i;
      std::string si = convert.str();
      Teuchos::ParameterList &ilist = list.sublist(si);
      std::string name = ilist.get<std::string>("Name");
      riskString.push_back(name);
    }
    for (typename std::vector<Real>::size_type i = 0; i < riskString.size(); ++i) {
      if ( riskString[i] == "CVaR"                           ||
           riskString[i] == "HMCR"                           ||
           riskString[i] == "Moreau-Yosida CVaR"             ||
           riskString[i] == "Log-Exponential Quadrangle"     ||
           riskString[i] == "Log-Quantile Quadrangle"        ||
           riskString[i] == "Mean-Variance Quadrangle"       ||
           riskString[i] == "Quantile-Based Quadrangle"      ||
           riskString[i] == "Smoothed Worst-Case Quadrangle" ||
           riskString[i] == "Truncated Mean Quadrangle" ) {
        nStatistic += 1;
        lower.push_back(ROL_NINF<Real>());
        upper.push_back(ROL_INF<Real>());
      }
      else if ( riskString[i] == "Quantile-Radius Quadrangle" ) {
        nStatistic += 2;
        lower.push_back(ROL_NINF<Real>()); lower.push_back(ROL_NINF<Real>());
        upper.push_back(ROL_INF<Real>());  upper.push_back(ROL_INF<Real>());
      }
      else if ( riskString[i] == "Coherent Exponential Utility" ||
                riskString[i] == "KL Divergence" ) {
        nStatistic += 1;
        isBoundActivated = true;
        lower.push_back(zero);
        upper.push_back(ROL_INF<Real>());
      }
      else if ( riskString[i] == "Chi-Squared Divergence" ) {
        nStatistic += 2;
        isBoundActivated = true;
        lower.push_back(zero);            lower.push_back(ROL_NINF<Real>());
        upper.push_back(ROL_INF<Real>()); upper.push_back(ROL_INF<Real>());
      }
      else if ( riskString[i] == "Mixed-Quantile Quadrangle" ) {
        Teuchos::ParameterList &MQlist = list.sublist("Mixed-Quantile Quadrangle");
        Teuchos::Array<Real> prob
          = Teuchos::getArrayFromStringParameter<Real>(MQlist,"Probability Array");
        nStatistic += prob.size();
        for (typename Teuchos::Array<Real>::size_type j = 0; j < prob.size(); ++j) {
          lower.push_back(ROL_NINF<Real>());
          upper.push_back(ROL_INF<Real>());
        }
      }
      else if ( riskString[i] == "Super Quantile Quadrangle" ||
                riskString[i] == "Chebyshev-Kusuoka"         ||
                riskString[i] == "Spectral Risk" ) {
        Teuchos::ParameterList &SQlist = list.sublist(riskString[i]);
        int nSQQstat = SQlist.get("Number of Quadrature Points",5);
        nStatistic += nSQQstat;
        for (int j = 0; j < nSQQstat; ++j) {
          lower.push_back(ROL_NINF<Real>());
          upper.push_back(ROL_INF<Real>());
        }
      }
      else if ( riskString[i] == "Exponential Utility"             ||
                riskString[i] == "Mean Plus Deviation From Target" ||
                riskString[i] == "Mean Plus Deviation"             ||
                riskString[i] == "Mean Plus Variance From Target"  ||
                riskString[i] == "Mean Plus Variance" ) {
        nStatistic += 0;
      }
      else {
        TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
          ">>> (ROL::RiskMeasureInfo): Invalid risk measure " << riskString[i] << "!");
      }
    }
  }
  else {
    TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
      ">>> (ROL::RiskMeasureInfo): Invalid risk measure " << name << "!");
  }

  // Print Information
  if ( printToStream ) {
    Teuchos::oblackholestream oldFormatState;
    oldFormatState.copyfmt(outStream);

    outStream << std::endl;
    outStream << std::scientific << std::setprecision(6);
    outStream << std::setfill('-') << std::setw(80) << "-" << std::endl;
    outStream << "  RISK MEASURE INFORMATION" << std::endl;
    outStream << std::setfill('-') << std::setw(80) << "-" << std::endl;
    outStream << "  NAME" << std::endl;
    outStream << "    " << name << std::endl;
    outStream << "  NUMBER OF STATISTICS" << std::endl;
    outStream << "    " << nStatistic << std::endl;
    outStream << "  ARE BOUNDS ACTIVATED" << std::endl;
    outStream << "    " << (isBoundActivated ? "TRUE" : "FALSE") << std::endl;
    if ( isBoundActivated ) {
      outStream << "  STATISTIC LOWER BOUNDS" << std::endl;
      for (int i = 0; i < nStatistic-1; ++i) {
        outStream << "    " << lower[i] << std::endl;
      }
      outStream << "    " << lower[nStatistic-1] << std::endl;
      outStream << "  STATISTIC UPPER BOUNDS" << std::endl;
      for (int i = 0; i < nStatistic-1; ++i) {
        outStream << "    " << upper[i] << std::endl;
      }
      outStream << "    " << upper[nStatistic-1] << std::endl;
    }
    outStream << std::setfill('-') << std::setw(80) << "-" << std::endl;
    outStream << std::endl;

    outStream.copyfmt(oldFormatState);
  }
}