<commit_before>// $Id$
//
//  Copyright (C) 2001-2006 Rational Discovery LLC
//
//  @@ All Rights Reserved @@
//
#include <iostream>
#include <sstream>
#include <fstream>
#include <ios>
#include "BitVects.h"
#include "BitOps.h"
#include "BitVectUtils.h"
#include <cmath>
#include "DiscreteValueVect.h"
#include <RDGeneral/Invariant.h>
#include <RDGeneral/RDLog.h>
#include <RDBoost/Exceptions.h>
#include <DataStructs/SparseIntVect.h>


using namespace std;
using namespace RDKit;
template< typename T >
inline void TXTMSG(const char *__a__,T __b__){  BOOST_LOG(rdInfoLog) << (__a__) << " " << (__b__) << std::endl; }

bool feq(double v1,double v2,double tol=1e-4){
  return fabs(v1-v2)<1e-4;
}

template<typename T> void Test(T arg){
  T t1(20);
  TXTMSG("Set 10:",t1.SetBit(10));
  TXTMSG("Set 11:",t1.SetBit(11));
  TXTMSG("Set 14:",t1.SetBit(14));
  TXTMSG("Set 10:",t1.SetBit(10));
  TXTMSG("Get 14:",t1.GetBit(14));
  TXTMSG("Num:",t1.GetNumBits());
  TXTMSG("NumOn:",t1.GetNumOnBits());
  TXTMSG("NumOff:",t1.GetNumOffBits());

  IntVect onBits;
  t1.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  T t2(t1);
  //onBits = t2.GetOnBits();
  TXTMSG("t2[19]:",t2[19]);
  TXTMSG("t2[14]:",t2[14]);

  t2 = t1;
  //onBits = t2.GetOnBits();
  TXTMSG("t2[19]:",t2[19]);
  t2.UnSetBit(14);
  TXTMSG("t2[14]:",t2[14]);
  t2.SetBit(15);
  t2.SetBit(17);


  cout << "t1: ";
  t1.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "t2: ";
  t2.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "t1|t2: ";
  T t3=t1|t2;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "t1&t2: ";
  t3=t1 & t2;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;
  
  cout << "t1^t2: ";
  t3=t1 ^ t2;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "~t1: ";
  t3= ~t1;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  try{
    t3.GetBit(4000);
  } catch (IndexErrorException) {
    cout << " except " << endl;
  } catch (...) {
    cout << " ERROR EXCEPT " << endl;
  }

  T t4(t1.ToString());
  cout << "tan(t1,t4): " << TanimotoSimilarity(t1,t4) << endl;
  
  T *t5 = FoldFingerprint(t1);
  TEST_ASSERT(t5->GetNumBits() == t1.GetNumBits()/2);
  TEST_ASSERT(t5->GetBit(0));
  TEST_ASSERT(t5->GetBit(1));
  TEST_ASSERT(t5->GetBit(4));
  TEST_ASSERT(!t5->GetBit(2));
  TEST_ASSERT(!t5->GetBit(3));
  delete t5;
}


template<typename T> void TaniTest(T &arg){
  std::string fps[4] = {
    ".b+HHa.EgU6+ibEIr89.CpX0g8FZiXH+R0+Ps.mr6tg.2",
    ".b7HEa..ccc+gWEIr89.8lV8gOF3aXFFR.+Ps.mZ6lg.2",
    ".H+nHq2EcY09y5EIr9e.8p50h0NgiWGNx4+Hm+Gbslw.2",
    ".1.HHa..cUI6i5E2rO8.Op10d0NoiWGVx.+Hm.Gb6lo.2",
  };
  double dists[] = {
    1.0,0.788991,0.677165,0.686957,
    1.0,0.578125,0.591304,
    1.0,0.732759,
    1.0
  };
  int idx=0;
  for(int i=0;i<4;i++){
    T v1(256);
    FromDaylightString(v1,fps[i]);
    for(int j=i;j<4;j++){
      T v2(256);
      FromDaylightString(v2,fps[j]);
      double tani=TanimotoSimilarity(v1,v2);
      TEST_ASSERT(feq(tani,dists[idx]));
      tani = SimilarityWrapper(v1,v2,TanimotoSimilarity);
      TEST_ASSERT(feq(tani,dists[idx]));
      tani = SimilarityWrapper(v1,v2,TanimotoSimilarity,true);
      TEST_ASSERT(feq(tani,1.-dists[idx]));
      idx++;
    }
  }
}


template<typename T> void ProbeTest(T &arg){
  int sz=1000;
  T t1(sz),t2(sz);
  for(int i=0;i<sz;i+=2){
    t1.SetBit(i);
    if(i<3*sz/4) t2.SetBit(i);
  }
  std::string pkl=t1.ToString();
  TEST_ASSERT(AllProbeBitsMatch(t1,pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2,pkl));
  TEST_ASSERT(AllProbeBitsMatch(t1.ToString(),pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString(),pkl));
  TEST_ASSERT(AllProbeBitsMatch(t1.ToString().c_str(),pkl.c_str()));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString().c_str(),pkl.c_str()));
  pkl = t2.ToString();
  TEST_ASSERT(!AllProbeBitsMatch(t1,pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2,pkl));
  TEST_ASSERT(!AllProbeBitsMatch(t1.ToString(),pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString(),pkl));
  TEST_ASSERT(!AllProbeBitsMatch(t1.ToString().c_str(),pkl.c_str()));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString().c_str(),pkl.c_str()));<commit_msg>this makes it build under windows<commit_after>// $Id$
//
//  Copyright (C) 2001-2006 Rational Discovery LLC
//
//  @@ All Rights Reserved @@
//
#include <iostream>
#include <sstream>
#include <fstream>
#include <ios>
#include "BitVects.h"
#include "BitOps.h"
#include "BitVectUtils.h"
#include <cmath>
#include "DiscreteValueVect.h"
#include <RDGeneral/Invariant.h>
#include <RDGeneral/RDLog.h>
#include <RDBoost/Exceptions.h>
#include <DataStructs/SparseIntVect.h>


using namespace std;
using namespace RDKit;
template< typename T >
inline void TXTMSG(const char *__a__,T __b__){  BOOST_LOG(rdInfoLog) << (__a__) << " " << (__b__) << std::endl; }

bool feq(double v1,double v2,double tol=1e-4){
  return fabs(v1-v2)<1e-4;
}

template<typename T> void Test(T arg){
  T t1(20);
  TXTMSG("Set 10:",t1.SetBit(10));
  TXTMSG("Set 11:",t1.SetBit(11));
  TXTMSG("Set 14:",t1.SetBit(14));
  TXTMSG("Set 10:",t1.SetBit(10));
  TXTMSG("Get 14:",t1.GetBit(14));
  TXTMSG("Num:",t1.GetNumBits());
  TXTMSG("NumOn:",t1.GetNumOnBits());
  TXTMSG("NumOff:",t1.GetNumOffBits());

  IntVect onBits;
  t1.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  T t2(t1);
  //onBits = t2.GetOnBits();
  TXTMSG("t2[19]:",t2[19]);
  TXTMSG("t2[14]:",t2[14]);

  t2 = t1;
  //onBits = t2.GetOnBits();
  TXTMSG("t2[19]:",t2[19]);
  t2.UnSetBit(14);
  TXTMSG("t2[14]:",t2[14]);
  t2.SetBit(15);
  t2.SetBit(17);


  cout << "t1: ";
  t1.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "t2: ";
  t2.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "t1|t2: ";
  T t3=t1|t2;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "t1&t2: ";
  t3=t1 & t2;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;
  
  cout << "t1^t2: ";
  t3=t1 ^ t2;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  cout << "~t1: ";
  t3= ~t1;
  t3.GetOnBits(onBits);
  std::copy(onBits.begin(),onBits.end(),std::ostream_iterator<int>(std::cout,", "));
  std::cout << std::endl;

  try{
    t3.GetBit(4000);
  } catch (IndexErrorException) {
    cout << " except " << endl;
  } catch (...) {
    cout << " ERROR EXCEPT " << endl;
  }

  T t4(t1.ToString());
  cout << "tan(t1,t4): " << TanimotoSimilarity(t1,t4) << endl;
  
  T *t5 = FoldFingerprint(t1);
  TEST_ASSERT(t5->GetNumBits() == t1.GetNumBits()/2);
  TEST_ASSERT(t5->GetBit(0));
  TEST_ASSERT(t5->GetBit(1));
  TEST_ASSERT(t5->GetBit(4));
  TEST_ASSERT(!t5->GetBit(2));
  TEST_ASSERT(!t5->GetBit(3));
  delete t5;
}


template<typename T> void TaniTest(T &arg){
  std::string fps[4] = {
    ".b+HHa.EgU6+ibEIr89.CpX0g8FZiXH+R0+Ps.mr6tg.2",
    ".b7HEa..ccc+gWEIr89.8lV8gOF3aXFFR.+Ps.mZ6lg.2",
    ".H+nHq2EcY09y5EIr9e.8p50h0NgiWGNx4+Hm+Gbslw.2",
    ".1.HHa..cUI6i5E2rO8.Op10d0NoiWGVx.+Hm.Gb6lo.2",
  };
  double dists[] = {
    1.0,0.788991,0.677165,0.686957,
    1.0,0.578125,0.591304,
    1.0,0.732759,
    1.0
  };
  int idx=0;
  for(int i=0;i<4;i++){
    T v1(256);
    FromDaylightString(v1,fps[i]);
    for(int j=i;j<4;j++){
      T v2(256);
      FromDaylightString(v2,fps[j]);
      double tani=TanimotoSimilarity(v1,v2);
      TEST_ASSERT(feq(tani,dists[idx]));
      tani = SimilarityWrapper(v1,v2,TanimotoSimilarity<T,T>);
      TEST_ASSERT(feq(tani,dists[idx]));
      tani = SimilarityWrapper(v1,v2,TanimotoSimilarity<T,T>,true);
      TEST_ASSERT(feq(tani,1.-dists[idx]));
      idx++;
    }
  }
}


template<typename T> void ProbeTest(T &arg){
  int sz=1000;
  T t1(sz),t2(sz);
  for(int i=0;i<sz;i+=2){
    t1.SetBit(i);
    if(i<3*sz/4) t2.SetBit(i);
  }
  std::string pkl=t1.ToString();
  TEST_ASSERT(AllProbeBitsMatch(t1,pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2,pkl));
  TEST_ASSERT(AllProbeBitsMatch(t1.ToString(),pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString(),pkl));
  TEST_ASSERT(AllProbeBitsMatch(t1.ToString().c_str(),pkl.c_str()));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString().c_str(),pkl.c_str()));
  pkl = t2.ToString();
  TEST_ASSERT(!AllProbeBitsMatch(t1,pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2,pkl));
  TEST_ASSERT(!AllProbeBitsMatch(t1.ToString(),pkl));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString(),pkl));
  TEST_ASSERT(!AllProbeBitsMatch(t1.ToString().c_str(),pkl.c_str()));
  TEST_ASSERT(AllProbeBitsMatch(t2.ToString().c_str(),pkl.c_str()));
{'fraction_non_alphanumeric': 0.2213967310549777, 'fraction_numerical': 0.05837401825514753, 'mean_word_length': 4.896745932415519, 'pattern_counts': {'":': 0, '<': 151, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6421124', 'n_tokens_mistral': 4441, 'n_tokens_neox': 4242, 'n_words': 498}