-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DataManager.cpp
326 lines (290 loc) · 10.1 KB
/
DataManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2014 - 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/
#include <memory>
#include <shogun/io/SGIO.h>
#include <shogun/features/Features.h>
#include <shogun/statistical_testing/internals/Block.h>
#include <shogun/statistical_testing/internals/DataManager.h>
#include <shogun/statistical_testing/internals/NextSamples.h>
#include <shogun/statistical_testing/internals/DataFetcher.h>
#include <shogun/statistical_testing/internals/FeaturesUtil.h>
#include <shogun/statistical_testing/internals/DataFetcherFactory.h>
using namespace shogun;
using namespace internal;
// TODO add nullptr check before calling the methods on actual fetchers
// this would be where someone calls the other methofds before setiing the sameples
DataManager::DataManager(size_t num_distributions)
{
SG_SDEBUG("Data manager instance initialized with %d data sources!\n", num_distributions);
fetchers.resize(num_distributions);
std::fill(fetchers.begin(), fetchers.end(), nullptr);
}
DataManager::~DataManager()
{
}
index_t DataManager::get_num_samples() const
{
SG_SDEBUG("Entering!\n");
index_t n=0;
using fetcher_type=const std::unique_ptr<DataFetcher>;
if (std::any_of(fetchers.begin(), fetchers.end(), [](fetcher_type& f) { return f->m_num_samples==0; }))
SG_SERROR("number of samples from all the distributions are not set!")
else
std::for_each(fetchers.begin(), fetchers.end(), [&n](fetcher_type& f) { n+=f->m_num_samples; });
SG_SDEBUG("Leaving!\n");
return n;
}
index_t DataManager::get_min_blocksize() const
{
SG_SDEBUG("Entering!\n");
index_t min_blocksize=0;
using fetcher_type=const std::unique_ptr<DataFetcher>;
if (std::any_of(fetchers.begin(), fetchers.end(), [](fetcher_type& f) { return f->m_num_samples==0; }))
SG_SERROR("number of samples from all the distributions are not set!")
else
{
index_t divisor=0;
std::function<index_t(index_t, index_t)> gcd=[&gcd](index_t m, index_t n)
{
return n==0?m:gcd(n, m%n);
};
for (size_t i=0; i<fetchers.size(); ++i)
divisor=gcd(divisor, fetchers[i]->m_num_samples);
min_blocksize=get_num_samples()/divisor;
}
SG_SDEBUG("min blocksize is %d!", min_blocksize);
SG_SDEBUG("Leaving!\n");
return min_blocksize;
}
void DataManager::set_blocksize(index_t blocksize)
{
SG_SDEBUG("Entering!\n");
auto n=get_num_samples();
REQUIRE(n>0,
"Total number of samples is 0! Please set the number of samples!\n");
REQUIRE(blocksize>0 && blocksize<=n,
"The blocksize has to be within [0, %d], given = %d!\n",
n, blocksize);
REQUIRE(n%blocksize==0,
"Total number of samples (%d) has to be divisble by the blocksize (%d)!\n",
n, blocksize);
for (size_t i=0; i<fetchers.size(); ++i)
{
index_t m=fetchers[i]->m_num_samples;
REQUIRE((blocksize*m)%n==0,
"Blocksize (%d) cannot be even distributed with a ratio of %f!\n",
blocksize, m/n);
fetchers[i]->fetch_blockwise().with_blocksize(blocksize*m/n);
SG_SDEBUG("block[%d].size = ", i, blocksize*m/n);
}
SG_SDEBUG("Leaving!\n");
}
void DataManager::set_num_blocks_per_burst(index_t num_blocks_per_burst)
{
SG_SDEBUG("Entering!\n");
REQUIRE(num_blocks_per_burst>0,
"Number of blocks per burst (%d) has to be greater than 0!\n",
num_blocks_per_burst);
index_t blocksize=0;
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [&blocksize](fetcher_type& f)
{
blocksize+=f->m_block_details.m_blocksize;
});
REQUIRE(blocksize>0,
"Blocksizes are not set!\n");
index_t max_num_blocks_per_burst=get_num_samples()/blocksize;
if (num_blocks_per_burst>max_num_blocks_per_burst)
{
SG_SINFO("There can only be %d blocks per burst given the blocksize (%d)!\n", max_num_blocks_per_burst, blocksize);
SG_SINFO("Setting num blocks per burst to be %d instead!\n", max_num_blocks_per_burst);
num_blocks_per_burst=max_num_blocks_per_burst;
}
for (size_t i=0; i<fetchers.size(); ++i)
fetchers[i]->fetch_blockwise().with_num_blocks_per_burst(num_blocks_per_burst);
SG_SDEBUG("Leaving!\n");
}
InitPerFeature DataManager::samples_at(size_t i)
{
SG_SDEBUG("Entering!\n");
REQUIRE(i<fetchers.size(),
"Value of i (%d) should be between 0 and %d, inclusive!",
i, fetchers.size()-1);
SG_SDEBUG("Leaving!\n");
return InitPerFeature(fetchers[i]);
}
CFeatures* DataManager::samples_at(size_t i) const
{
SG_SDEBUG("Entering!\n");
REQUIRE(i<fetchers.size(),
"Value of i (%d) should be between 0 and %d, inclusive!",
i, fetchers.size()-1);
SG_SDEBUG("Leaving!\n");
return fetchers[i]->m_samples;
}
index_t& DataManager::num_samples_at(size_t i)
{
SG_SDEBUG("Entering!\n");
REQUIRE(i<fetchers.size(),
"Value of i (%d) should be between 0 and %d, inclusive!",
i, fetchers.size()-1);
SG_SDEBUG("Leaving!\n");
return fetchers[i]->m_num_samples;
}
const index_t DataManager::num_samples_at(size_t i) const
{
SG_SDEBUG("Entering!\n");
REQUIRE(i<fetchers.size(),
"Value of i (%d) should be between 0 and %d, inclusive!",
i, fetchers.size()-1);
SG_SDEBUG("Leaving!\n");
return fetchers[i]->m_num_samples;
}
const index_t DataManager::blocksize_at(size_t i) const
{
SG_SDEBUG("Entering!\n");
REQUIRE(i<fetchers.size(),
"Value of i (%d) should be between 0 and %d, inclusive!",
i, fetchers.size()-1);
SG_SDEBUG("Leaving!\n");
return fetchers[i]->m_block_details.m_blocksize;
}
const bool DataManager::is_blockwise() const
{
SG_SDEBUG("Entering!\n");
bool blockwise=true;
for (size_t i=0; i<fetchers.size(); ++i)
blockwise&=!fetchers[i]->m_block_details.m_full_data;
SG_SDEBUG("Leaving!\n");
return blockwise;
}
void DataManager::set_blockwise(bool blockwise)
{
SG_SDEBUG("Entering!\n");
for (size_t i=0; i<fetchers.size(); ++i)
fetchers[i]->m_block_details.m_full_data=!blockwise;
SG_SDEBUG("Leaving!\n");
}
void DataManager::set_train_test_ratio(float64_t train_test_ratio)
{
SG_SDEBUG("Entering!\n");
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [&train_test_ratio](fetcher_type& f)
{
f->set_train_test_ratio(train_test_ratio);
});
SG_SDEBUG("Leaving!\n");
}
float64_t DataManager::get_train_test_ratio() const
{
REQUIRE(fetchers[0]!=nullptr, "Please set the samples first!\n");
return fetchers[0]->get_train_test_ratio();
}
void DataManager::set_train_mode(bool train_mode)
{
SG_SDEBUG("Entering!\n");
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [&train_mode](fetcher_type& f)
{
f->set_train_mode(train_mode);
});
SG_SDEBUG("Leaving!\n");
}
void DataManager::set_xvalidation_mode(bool xvalidation_mode)
{
SG_SDEBUG("Entering!\n");
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [&xvalidation_mode](fetcher_type& f)
{
f->set_xvalidation_mode(xvalidation_mode);
});
SG_SDEBUG("Leaving!\n");
}
index_t DataManager::get_num_folds() const
{
REQUIRE(fetchers[0]!=nullptr, "Please set the samples first!\n");
return fetchers[0]->get_train_test_ratio();
}
void DataManager::use_fold(index_t idx)
{
SG_SDEBUG("Entering!\n");
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [&idx](fetcher_type& f)
{
f->use_fold(idx);
});
SG_SDEBUG("Leaving!\n");
}
void DataManager::start()
{
SG_SDEBUG("Entering!\n");
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [](fetcher_type& f) { f->start(); });
SG_SDEBUG("Leaving!\n");
}
NextSamples DataManager::next()
{
SG_SDEBUG("Entering!\n");
// sets the number of feature objects (number of distributions)
NextSamples next_samples(fetchers.size());
// fetch a number of blocks (per burst) from each distribution
for (size_t i=0; i<fetchers.size(); ++i)
{
auto feats=fetchers[i]->next();
if (feats!=nullptr)
{
ASSERT(feats->ref_count()==0);
auto blocksize=fetchers[i]->m_block_details.m_blocksize;
auto num_blocks_curr_burst=feats->get_num_vectors()/blocksize;
// use same number of blocks from all the distributions
if (next_samples.m_num_blocks==0)
next_samples.m_num_blocks=num_blocks_curr_burst;
else
ASSERT(next_samples.m_num_blocks==num_blocks_curr_burst);
next_samples[i]=Block::create_blocks(feats, num_blocks_curr_burst, blocksize);
}
}
SG_SDEBUG("Leaving!\n");
return next_samples;
}
void DataManager::end()
{
SG_SDEBUG("Entering!\n");
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [](fetcher_type& f) { f->end(); });
SG_SDEBUG("Leaving!\n");
}
void DataManager::reset()
{
SG_SDEBUG("Entering!\n");
using fetcher_type=std::unique_ptr<DataFetcher>;
std::for_each(fetchers.begin(), fetchers.end(), [](fetcher_type& f) { f->reset(); });
SG_SDEBUG("Leaving!\n");
}