-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathenv.hh
357 lines (316 loc) · 8.23 KB
/
env.hh
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#ifndef ENV_HH
#define ENV_HH
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <error.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <list>
#include <vector>
#include "matrix.hh"
#include "log.hh"
typedef uint8_t yval_t;
typedef D2Array<yval_t> AdjMatrix;
typedef D2Array<double> Matrix;
typedef D3Array<double> D3;
typedef D2Array<KV> MatrixKV;
typedef std::pair<uint32_t, uint32_t> LocIndiv;
typedef D1Array<yval_t> YArray;
typedef std::map<uint32_t, YArray *> YArrayMap;
typedef std::map<KV, bool> SNPMap;
typedef std::map<LocIndiv, bool> LocIndivMap;
typedef std::map<uint32_t, uint32_t> IDMap;
typedef std::map<uint32_t, uint32_t> FreqMap;
typedef std::map<string, uint32_t> FreqStrMap;
typedef std::map<string, uint32_t> StrMap;
typedef std::map<uint32_t, string> StrMapInv;
typedef D1Array<std::vector<uint32_t> *> SparseMatrix;
typedef std::map<uint32_t, bool> NodeMap;
typedef std::map<uint32_t, bool> BoolMap;
typedef std::map<uint64_t, bool> BoolMap64;
typedef std::map<uint32_t, uint32_t> NodeValMap;
typedef std::map<uint32_t, vector<uint32_t> > MapVec;
typedef MapVec SparseMatrix2;
typedef std::map<uint32_t, bool> SampleMap;
typedef map<uint32_t, vector<uint32_t> > SNPByLoc;
typedef vector<uint32_t> IndivList;
//typedef std::map<Edge, int> CountMap;
//typedef std::map<Edge, double> ValueMap;
typedef std::map<uint32_t, string> StrMapInv;
class Env {
public:
Env(uint32_t N, uint32_t K, uint32_t L,
bool batch,
bool force_overwrite_dir, string dfname,
string label,
string etype,
uint32_t rfreq, bool logl, bool loadcmp,
double seed, bool file_suffix,
bool save_beta, bool adagrad, uint32_t nthreads,
bool use_test_set,
bool compute_beta, string locations_file,
double stop_threshold);
~Env() { fclose(_plogf); }
static string prefix;
static Logger::Level level;
uint32_t n;
uint32_t k;
uint32_t l;
uint32_t t;
uint32_t blocks;
uint32_t indiv_sample_size;
uint32_t nthreads;
bool batch_mode;
double meanchangethresh;
double alpha;
double validation_ratio;
double heldout_indiv_ratio;
double test_ratio;
double eta0_dense;
double eta1_dense;
double eta0_regular;
double eta1_regular;
double eta0_uniform;
double eta1_uniform;
double eta0_sparse;
double eta1_sparse;
double eta0;
double eta1;
int reportfreq;
double epsilon;
double logepsilon;
double tau0;
double nodetau0;
double nodekappa;
double kappa;
uint32_t online_iterations;
bool terminate;
string datfname;
string label;
string eta_type;
bool use_validation_stop;
bool use_training_stop;
bool use_test_set;
bool compute_logl;
bool loadcmp;
double seed;
bool file_suffix;
bool save_beta;
bool adagrad;
bool compute_beta;
string locations_file;
double stop_threshold;
template<class T> static void plog(string s, const T &v);
static string file_str(string fname);
private:
static FILE *_plogf;
};
template<class T> inline void
Env::plog(string s, const T &v)
{
fprintf(_plogf, "%s: %s\n", s.c_str(), v.s().c_str());
fflush(_plogf);
}
template<> inline void
Env::plog(string s, const double &v)
{
fprintf(_plogf, "%s: %.9f\n", s.c_str(), v);
fflush(_plogf);
}
template<> inline void
Env::plog(string s, const bool &v)
{
fprintf(_plogf, "%s: %s\n", s.c_str(), v ? "True": "False");
fflush(_plogf);
}
template<> inline void
Env::plog(string s, const int &v)
{
fprintf(_plogf, "%s: %d\n", s.c_str(), v);
fflush(_plogf);
}
template<> inline void
Env::plog(string s, const unsigned &v)
{
fprintf(_plogf, "%s: %d\n", s.c_str(), v);
fflush(_plogf);
}
template<> inline void
Env::plog(string s, const short unsigned int &v)
{
fprintf(_plogf, "%s: %d\n", s.c_str(), v);
fflush(_plogf);
}
template<> inline void
Env::plog(string s, const uint64_t &v)
{
fprintf(_plogf, "%s: %lu\n", s.c_str(), v);
fflush(_plogf);
}
inline string
Env::file_str(string fname)
{
string s = prefix + fname;
return s;
}
inline
Env::Env(uint32_t N, uint32_t K, uint32_t L,
bool batch,
bool force_overwrite_dir, string dfname,
string lbl,
string etype,
uint32_t rfreq, bool logl, bool lcmp,
double seedv, bool file_suffixv,
bool save_betav, bool adagradv,
uint32_t nthreadsv,
bool use_test_setv, bool compute_betav,
string locations_filev,
double stop_thresholdv)
: n(N),
k(K),
l(L),
t(2),
blocks(100),
indiv_sample_size(N/blocks),
nthreads(nthreadsv),
batch_mode(batch),
meanchangethresh(0.001),
alpha((double)1.0/k),
heldout_indiv_ratio(0.001),
validation_ratio(0.005),
test_ratio(0.005),
eta0_dense(4700.59),
eta1_dense(0.77),
eta0_regular(3.87),
eta1_regular(1.84),
eta0_uniform(1.00),
eta1_uniform(1.00),
eta0_sparse(0.97),
eta1_sparse(6.33),
eta0(eta0_uniform),
eta1(eta1_uniform),
reportfreq(rfreq),
epsilon(1e-30),
logepsilon(log(epsilon)),
tau0(1), //default 1
kappa(0.5),
nodetau0(1), //default 1
nodekappa(0.5),
online_iterations(10), //default 10
terminate(false),
datfname(dfname),
label(lbl),
eta_type(etype),
use_validation_stop(true),
use_training_stop(false),
compute_logl(logl),
loadcmp(lcmp),
seed(seedv),
file_suffix(file_suffixv),
save_beta(save_betav),
adagrad(adagradv),
use_test_set(use_test_setv),
compute_beta(compute_betav),
locations_file(locations_filev),
stop_threshold(stop_thresholdv)
{
ostringstream sa;
sa << "n" << n << "-";
sa << "k" << k << "-";
sa << "l" << l;
if (label != "")
sa << "-" << label;
else if (datfname.length() > 3) {
string q = datfname.substr(0,2);
if (q == "..")
q = "xx";
sa << "-" << q;
}
if (seed != 0)
sa << "-" << "seed" << seed;
prefix = sa.str();
level = Logger::TEST;
fprintf(stdout, "+ Creating directory %s\n", prefix.c_str());
assert (Logger::initialize(prefix, "infer.log",
force_overwrite_dir, level) >= 0);
fflush(stdout);
_plogf = fopen(file_str("/param.txt").c_str(), "w");
if (!_plogf) {
printf("cannot open param file:%s\n", strerror(errno));
exit(-1);
}
if (N > 10000) {
blocks = 100;
indiv_sample_size = N / blocks;
} else {
blocks = 10;
indiv_sample_size = N / blocks;
}
plog("n", n);
plog("k", k);
plog("t", t);
plog("l", l);
plog("nthreads", nthreads);
plog("tau0", tau0);
plog("nodetau0", nodetau0);
plog("kappa", kappa);
plog("nodekappa", nodekappa);
plog("alpha", alpha);
plog("heldout_indiv_ratio", heldout_indiv_ratio);
plog("validation_ratio", validation_ratio);
plog("online_iterations", online_iterations);
plog("GSL seed", seed);
plog("file suffix", file_suffix);
plog("save beta", save_beta);
plog("adagrad", adagrad);
plog("indiv sample size", indiv_sample_size);
plog("blocks", blocks);
plog("compute_beta", compute_beta);
plog("stop_threshold", stop_threshold);
string ndatfname = file_str("/network.dat");
unlink(ndatfname.c_str());
assert (symlink(datfname.c_str(), ndatfname.c_str()) >= 0);
fprintf(stderr, "+ done initializing env\n");
}
/*
src: http://www.delorie.com/gnu/docs/glibc/libc_428.html
Subtract the `struct timeval' values X and Y,
storing the result in RESULT.
Return 1 if the difference is negative, otherwise 0.
*/
inline int
timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
{
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_usec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
inline void
timeval_add (struct timeval *result, const struct timeval *x)
{
result->tv_sec += x->tv_sec;
result->tv_usec += x->tv_usec;
if (result->tv_usec >= 1000000) {
result->tv_sec++;
result->tv_usec -= 1000000;
}
}
#endif