Changeset 6bed1f


Ignore:
Timestamp:
Nov 8, 2012, 1:13:53 PM (12 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
9fd889
Parents:
cf18c5
git-author:
Frederik Heber <heber@…> (07/27/12 15:49:06)
git-committer:
Frederik Heber <heber@…> (11/08/12 13:13:53)
Message:

Changed Histogram's cstor to take offset and binwidth instead of CountBins.

  • CountBins was nonsense anyway, could have been taken from samples.size() as well.
  • with these two values, we may also add the samples lateron.
Location:
src/Fragmentation/Histogram
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Fragmentation/Histogram/Histogram.cpp

    rcf18c5 r6bed1f  
    9898}
    9999
    100 Histogram::Histogram(const samples_t &samples, const int _CountBins) :
    101     binwidth(0.5),
    102     CountBins(_CountBins)
     100Histogram::Histogram(const samples_t &samples, const BinLowerEnd _offset, const double _binwidth) :
     101    binwidth(_binwidth),
     102    offset(_offset)
    103103{
    104104  // build histogram from samples
    105105  if (!samples.empty()) {
    106     if (CountBins == -1) {
    107       CountBins = ceil(pow(samples.size(), 1./3.));
    108     }
    109     // 2. get min and max and determine width
     106    // 1. get min and max and determine width
    110107    samples_t::const_iterator maxiter = max_element(samples.begin(), samples.end());
    111108    samples_t::const_iterator miniter = min_element(samples.begin(), samples.end());
     
    113110        "Histogram::Histogram() - cannot find min/max despite non-empty range.");
    114111//    LOG(1, "DEBUG: min is " << *miniter << " and max is " << *maxiter << ".");
    115     // as the logic to calculate is a bit more complex, we cannot (sensibly) place
    116     // it in the cstor call in the init list above. However, the binwidth is from
    117     // this point on fixed through the existence of this instance. Hence, we rather
    118     // keep it constant nonetheless and ust const_cast here.
    119     const_cast<double &>(binwidth) = (*maxiter - *miniter)/(CountBins-1);
    120 
    121     // 3. create each bin
    122     BinCreator_t BinCreator( *miniter, binwidth );
    123     std::vector<Bin_t> vectorbins;
    124     // we need one extra bin for get...Bin()'s find to work properly
    125     vectorbins.resize(CountBins+1, Bin_t( make_pair(0., 0.) ) );
    126     std::generate( vectorbins.begin(), vectorbins.end(), BinCreator );
    127     bins.insert(vectorbins.begin(), vectorbins.end());
    128 
    129     // 4. place each sample into bin (make count normalized, i.e. always equal to
    130     // surface area of 1)
     112
     113    // 2. create each bin
     114    {
     115      std::vector<Bin_t> vectorbins;
     116      BinCreator_t BinCreator( getLowerEnd(*miniter), binwidth );
     117      // we need one extra bin for get...Bin()'s find to work properly
     118      const int CountBins = ceil((*maxiter - getLowerEnd(*miniter))/binwidth)+1;
     119      vectorbins.resize(CountBins+1, Bin_t( make_pair(0., 0.) ) );
     120      std::generate( vectorbins.begin(), vectorbins.end(), BinCreator );
     121      bins.insert(vectorbins.begin(), vectorbins.end());
     122    }
     123
     124    // 3. place each sample into bin
    131125    BOOST_FOREACH( double value, samples) {
    132126      const Bins_t::iterator biniter = getLowerEndBin(value);
    133127      ASSERT( biniter != bins.end(),
    134128          "Histogram::Histogram() - cannot find bin for value from given samples.");
     129      // (make bin count normalized, i.e. always equal to surface area of 1)
    135130      biniter->second += 1./binwidth;
    136131    }
     
    293288Histogram::BinLowerEnd Histogram::getLowerEnd(const double _value) const
    294289{
    295   // we need just one bin, to have an offset
    296   const double offset = bins.begin()->first;
    297290  BinLowerEnd start = _value - offset;
    298291  // then divide by binwidth
  • src/Fragmentation/Histogram/Histogram.hpp

    rcf18c5 r6bed1f  
    4949  typedef std::map< BinLowerEnd, BinWeight > Bins_t;
    5050public:
     51  Histogram() :
     52    binwidth(0.5),
     53    offset(0.)
     54  {}
     55
     56  /** Constructor for class Histogram.
     57   *
     58   * @param offset where the bins should start
     59   * @param binwidth width of the bins
     60   */
     61  Histogram(const BinLowerEnd _offset, const double _binwidth) :
     62    binwidth(_binwidth),
     63    offset(_offset)
     64  {}
     65
    5166  /** Constructor for class Histogram.
    5267   *
    5368   * @param samples samples to put in the histogram.
    54    * @param CountBins number of bins (if negative, we choose number by statistical means)
     69   * @param offset where the bins should start
     70   * @param binwidth width of the bins
    5571   */
    56   Histogram(const samples_t &samples, const int _CountBins=-1);
     72  Histogram(const samples_t &samples, const BinLowerEnd _offset, const double _binwidth);
    5773
    5874  /** Adding another histogram onto this one.
     
    142158  //!> width of bin
    143159  const double binwidth;
    144   //!> number of bins
    145   int CountBins;
     160  //!> offset for the start of the bins
     161  const BinLowerEnd offset;
    146162};
    147163
  • src/Fragmentation/Histogram/unittests/HistogramUnitTest.cpp

    rcf18c5 r6bed1f  
    8484  sampled_values += 1.,2.,3.,4.;
    8585  CPPUNIT_ASSERT_EQUAL( (size_t)4, sampled_values.size() );
    86   histogram = new Histogram(sampled_values, 4);
     86  histogram = new Histogram(sampled_values, 1., 1.);
    8787  CPPUNIT_ASSERT_EQUAL( (size_t)4+1, histogram->bins.size() );
    8888  CPPUNIT_ASSERT( histogram->bins.end() != histogram->bins.begin() );
     
    9999  sampled_values += 1.,2.,3.,4.;
    100100  CPPUNIT_ASSERT_EQUAL( (size_t)4, sampled_values.size() );
    101   histogram = new Histogram(sampled_values, 4);
     101  histogram = new Histogram(sampled_values, 1., 1.);
    102102
    103103  // check values inside
     
    133133  sampled_values += 1.,2.,3.,4.;
    134134  CPPUNIT_ASSERT_EQUAL( (size_t)4, sampled_values.size() );
    135   histogram = new Histogram(sampled_values, 4);
     135  histogram = new Histogram(sampled_values, 1., 1.);
    136136
    137137  // check values inside
     
    172172  Histogram::samples_t sampled_values;
    173173  sampled_values += 1.,2.,3.,4.;
    174   histogram = new Histogram(sampled_values, 4);
     174  histogram = new Histogram(sampled_values, 1., 1.);
    175175
    176176  // go through each bin and check against getLowerEnd()
     
    189189void HistogramTest::isEmpty_Test()
    190190{
    191   histogram = new Histogram(std::vector<double>());
     191  histogram = new Histogram(1., 1.);
    192192  // test on empty histogramgram
    193193  CPPUNIT_ASSERT( histogram->isEmpty() );
     
    197197  sampled_values += 1.,2.,3.,4.;
    198198  delete histogram;
    199   histogram = new Histogram(sampled_values, 4);
     199  histogram = new Histogram(sampled_values, 1., 1.);
    200200  CPPUNIT_ASSERT( !histogram->isEmpty() );
    201201
     
    206206void HistogramTest::areaTest()
    207207{
    208   histogram = new Histogram(std::vector<double>());
     208  histogram = new Histogram(1., 1.);
    209209  // test on empty histogramgram
    210210  CPPUNIT_ASSERT_EQUAL( 0., histogram->area() );
     
    214214  sampled_values += 1.,2.,3.,4., 4.;
    215215  delete histogram;
    216   histogram = new Histogram(sampled_values, 4);
     216  histogram = new Histogram(sampled_values, 1., 1.);
    217217  CPPUNIT_ASSERT_EQUAL( 5., histogram->area() );
    218218  Histogram::samples_t more_values;
    219219  more_values += 1.75,2.5,3.;
    220   Histogram otherhistogram(more_values, 3);
     220  Histogram otherhistogram(more_values, 1.75, .625);
    221221  CPPUNIT_ASSERT_EQUAL( 3., otherhistogram.area() );
    222222
     
    230230  Histogram::samples_t sampled_values;
    231231  sampled_values += 1.,2.,3.,4.;
    232   histogram = new Histogram(sampled_values, 4);
     232  histogram = new Histogram(sampled_values, 1., 1.);
    233233  Histogram::samples_t more_values;
    234234  more_values += 1.75,2.5,3.;
    235   Histogram otherhistogram(more_values, 3);
     235  Histogram otherhistogram(more_values, 1.75, .625);
    236236
    237237  // check that size increases
     
    252252  Histogram::samples_t sampled_values;
    253253  sampled_values += 1.,2.,3.,4.;
    254   histogram = new Histogram(sampled_values, 4);
     254  histogram = new Histogram(sampled_values, 1., 1.);
    255255
    256256  // add upon larger
     
    258258    Histogram::samples_t more_values;
    259259    more_values += 0.5, 1.25, 2.5,3., 4.2;
    260     Histogram otherhistogram(more_values, 5);
     260    Histogram otherhistogram(more_values, 0.5, .625);
    261261    const double areas = histogram->area() + otherhistogram.area();
    262262
     
    271271    Histogram::samples_t more_values;
    272272    more_values += 1.75,2.5,3.;
    273     Histogram otherhistogram(more_values, 3);
     273    Histogram otherhistogram(more_values, 1.75, .625);
    274274    const double areas = histogram->area() + otherhistogram.area();
    275275
     
    288288  Histogram::samples_t sampled_values;
    289289  sampled_values += 1.,2.,3.,4.;
    290   histogram = new Histogram(sampled_values, 4);
     290  histogram = new Histogram(sampled_values, 1., 1.);
    291291
    292292  // subtract smaller
    293293  {
    294294    Histogram::samples_t more_values;
    295     more_values += 1.75,2.5,3.;
    296     Histogram otherhistogram(more_values, 3);
     295    more_values += 0.5, 1.25, 2.5,3., 4.2;
     296    Histogram otherhistogram(more_values, 0.5, .625);
    297297    const double difference = otherhistogram.area() - histogram->area();
    298298
     
    306306  {
    307307    Histogram::samples_t more_values;
    308     more_values += 0.5, 1.25, 2.5,3., 4.2;
    309     Histogram otherhistogram(more_values, 5);
     308    more_values += 1.75,2.5,3.;
     309    Histogram otherhistogram(more_values, 1.75, .625);
    310310    const double difference = otherhistogram.area() - histogram->area();
    311311
Note: See TracChangeset for help on using the changeset viewer.