Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-16 09:21:06

0001 // Author: Martin Føll, University of Oslo (UiO) & CERN 01/2026
0002 
0003 /*************************************************************************
0004  * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers.               *
0005  * All rights reserved.                                                  *
0006  *                                                                       *
0007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0009  *************************************************************************/
0010 
0011 #ifndef ROOT_INTERNAL_ML_RSAMPLER
0012 #define ROOT_INTERNAL_ML_RSAMPLER
0013 
0014 #include <memory>
0015 #include <string>
0016 #include <vector>
0017 
0018 #include "ROOT/ML/RFlat2DMatrix.hxx"
0019 
0020 // Forward decls
0021 namespace ROOT::Experimental::Internal::ML {
0022 class RFlat2DMatrixOperators;
0023 }
0024 
0025 namespace ROOT::Experimental::Internal::ML {
0026 /**
0027 \class ROOT::Experimental::Internal::ML::RSampler
0028 
0029 \brief Implementation of different sampling strategies.
0030 */
0031 
0032 class RSampler {
0033 private:
0034    std::vector<RFlat2DMatrix> &fDatasets;
0035    std::string fSampleType;
0036    float fSampleRatio;
0037    bool fReplacement;
0038    bool fShuffle;
0039    std::size_t fSetSeed;
0040    std::size_t fNumEntries;
0041 
0042    std::size_t fMajor;
0043    std::size_t fMinor;
0044    std::size_t fNumMajor;
0045    std::size_t fNumMinor;
0046    std::size_t fNumResampledMajor;
0047    std::size_t fNumResampledMinor;
0048 
0049    std::vector<std::size_t> fSamples;
0050 
0051    std::unique_ptr<RFlat2DMatrixOperators> fTensorOperators;
0052 
0053 public:
0054    RSampler(std::vector<RFlat2DMatrix> &datasets, const std::string &sampleType, float sampleRatio,
0055             bool replacement = false, bool shuffle = true, std::size_t setSeed = 0);
0056 
0057    ~RSampler();
0058 
0059    void SetupSampler();
0060 
0061    void Sampler(RFlat2DMatrix &SampledTensor);
0062 
0063    void SetupRandomUndersampler();
0064 
0065    void SetupRandomOversampler();
0066 
0067    void RandomUndersampler(RFlat2DMatrix &ShuffledTensor);
0068 
0069    void RandomOversampler(RFlat2DMatrix &ShuffledTensor);
0070 
0071    void SampleWithReplacement(std::size_t n_samples, std::size_t max);
0072 
0073    void SampleWithoutReplacement(std::size_t n_samples, std::size_t max);
0074 
0075    std::size_t GetNumEntries() { return fNumEntries; }
0076 };
0077 
0078 } // namespace ROOT::Experimental::Internal::ML
0079 #endif // ROOT_INTERNAL_ML_RSAMPLER