Browse code

Implemented sample funciton in random.h and random.cpp, changed sampleIndices in GapRunner.cpp accordingly

Tiger Gao authored on 05/06/2018 19:59:57
Showing 3 changed files

... ...
@@ -13,7 +13,52 @@
13 13
 // samples gene names as well
14 14
 static std::vector< std::vector<unsigned> > sampleIndices(unsigned n, unsigned nSets)
15 15
 {
16
-    // TODO implement
16
+    std::vector< std::vector<unsigned> > sampleIndices;
17
+    std::vector<unsigned> toBeSampled;
18
+    for (unsigned i = 1; i < n; ++i)
19
+    {
20
+        toBeSampled.push_back(i);
21
+    }
22
+
23
+    for (unsigned i = 0; i < (n - 1); ++i)
24
+    {
25
+        std::vector<unsigned> set = gaps::random::sample(toBeSampled, (int) (n - 1) / (nSets - 1));
26
+        sampleIndices.push_back(set);
27
+        for(std::vector<unsigned>::iterator it = set.begin(); it != set.end(); ++it)
28
+        {
29
+            unsigned index = *it;
30
+            std::vector<unsigned>::iterator it2 = toBeSampled.begin();
31
+            while(it2 != toBeSampled.end())
32
+            {
33
+                if(*it2 == index)
34
+                {
35
+                    it2 = toBeSampled.erase(it);
36
+                }
37
+                else
38
+                    it2++;
39
+            }
40
+        }
41
+    }
42
+    std::vector<unsigned> set = gaps::random::sample(toBeSampled, (n - 1) % (nSets - 1));
43
+    sampleIndices.push_back(set);
44
+    for(std::vector<unsigned>::iterator it = set.begin(); it != set.end(); ++it)
45
+    {
46
+        unsigned index = *it;
47
+        std::vector<unsigned>::iterator it2 = toBeSampled.begin();
48
+        while(it2 != toBeSampled.end())
49
+        {
50
+            if(*it2 == index)
51
+            {
52
+                it2 = toBeSampled.erase(it);
53
+            }
54
+            else
55
+                it2++;
56
+        }
57
+    }
58
+
59
+    return sampleIndices;
60
+
61
+    /*
17 62
     std::vector< std::vector<unsigned> > sampleIndices;
18 63
     std::vector<unsigned> sampled;
19 64
 
... ...
@@ -56,6 +101,7 @@ static std::vector< std::vector<unsigned> > sampleIndices(unsigned n, unsigned n
56 101
     }
57 102
 
58 103
     return sampleIndices;
104
+    */
59 105
 }
60 106
 
61 107
 GapsRunner::GapsRunner(const Rcpp::NumericMatrix &D, const Rcpp::NumericMatrix &S,
... ...
@@ -178,4 +178,23 @@ float gaps::random::inverseGammaSample(float a, float b, float mean, float sd)
178 178
         u = gaps::random::uniform(a, b);
179 179
     }
180 180
     return gaps::random::q_gamma(u, mean, sd);
181
-}
182 181
\ No newline at end of file
182
+}
183
+
184
+std::vector<unsigned> sample(const std::vector<unsigned> &elements, unsigned n) {
185
+    std::vector<unsigned> sampleVect;
186
+    std::vector<unsigned> sampledIndices;
187
+    for (unsigned i = 0; i < n; ++i)
188
+    {
189
+        while(true)
190
+        {
191
+            unsigned sampleIndex = gaps::random::uniform64(0, elements.size());
192
+            if (find(sampledIndices.begin(), sampledIndices.end(), sampleIndex) == sampledIndices.end())
193
+            {
194
+                sampleVect.push_back(elements.at(sampleIndex));
195
+                sampledIndices.push_back(sampleIndex);
196
+                break;
197
+            }
198
+        }
199
+    }
200
+    return sampleVect;
201
+}
... ...
@@ -34,7 +34,9 @@ namespace gaps
34 34
 
35 35
         void save(Archive &ar);
36 36
         void load(Archive &ar);
37
+
38
+        std::vector<unsigned> sample(const std::vector<unsigned> &elements, unsigned n);
37 39
     } // namespace random
38 40
 } // namespace gaps
39 41
 
40
-#endif
41 42
\ No newline at end of file
43
+#endif