forked from cmdupuis3/EDGI_PCA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheof.tpp
581 lines (482 loc) · 16.5 KB
/
eof.tpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/***********************************************************************
* GNU Lesser General Public License
*
* This file is part of the EDGI prototype package, developed by the
* GFDL Flexible Modeling System (FMS) group.
*
* EDGI is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* EDGI is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with EDGI. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
// Note: This is not intended to be a standalone implementation file.
#include "error.hpp"
#include "utils.hpp"
#include "debug.hpp"
#include <iterator>
//==============================================================================
// Local Helper Functions
//==============================================================================
template<typename S>
void subtract_mean(size_t len, S* data) {
S mean = 0;
for (size_t i = 0; i < len; i++) {
mean += data[i];
}
mean /= len;
for (size_t i = 0; i < len; i++) {
data[i] -= mean;
}
}
/*
template<typename S>
void get_mean()
template<typename S>
void standardize(size_t len, S* data) {
}
*/
template<typename S>
std::function<bool(S)> always_false() {
return [](S) {
return false;
};
}
//==============================================================================
// Private Methods
//==============================================================================
template<typename S, typename T>
template<typename F>
void eof_t<S, T>::match_dimensions(
const dimension_t<T>* dim0,
const dimension_t<T>* dim1,
F cmp
) {
//if (this->interp != nullptr) {
// TODO interpolate!
//} else {
if (dim0->get_size() != dim1->get_size()) {
throw eof_error_t("Input dimension lengths do not match");
}
size_t size = dim0->get_size();
const T* vals0 = dim0->get_values();
const T* vals1 = dim1->get_values();
for (size_t i = 0; i < size; i++) {
if (!cmp(vals0[i], vals1[i])) {
throw eof_error_t("Input dimension values do not match");
}
}
//}
}
template<typename S, typename T>
void eof_t<S, T>::match_dimension_in_all_variables(
std::vector<variable_t<S, T>*> input_vars,
std::string dim_name
) {
this->match_dimension_in_all_variables(input_vars, dim_name, [](T x, T y) { return x == y; });
}
/**
* Checks that all input variables have the same dimension `dim_name`. Dimension
* values are compared using the comparison function `cmp`. If any two
* dimensions don't match, throw an exception
*/
template<typename S, typename T>
template<typename F>
void eof_t<S, T>::match_dimension_in_all_variables(
std::vector<variable_t<S, T>*> input_vars,
std::string dim_name,
F cmp
) {
for (const variable_t<S, T>* var : input_vars) {
if (!var->has_dim(dim_name)) {
throw eof_error_t("At least one variable doesn't contain dimension \"" + dim_name + "\"");
}
}
// Check every pair of variables for equality in the selected dimension
// (since comparing dimension values for equality may not be transitive)
for (size_t i = 0; i < input_vars.size(); i++) {
const variable_t<S, T>* var0 = input_vars[i];
const dimension_t<T>* dim0 = var0->get_dim(var0->find_dim(dim_name));
for (size_t j = i + 1; j < input_vars.size(); j++) {
const variable_t<S, T>* var1 = input_vars[j];
const dimension_t<T>* dim1 = var1->get_dim(var1->find_dim(dim_name));
this->match_dimensions(dim0, dim1, cmp);
}
}
}
template<typename S, typename T>
void eof_t<S, T>::covariance_kernal(
size_t num_vars,
matrix_t<S>** matrices,
matrix_t<S>* cov,
const size_t num_threads){
size_t len = matrices[0]->get_rows();
S slice1[num_threads][len];
S slice2[num_threads][len];
// Start the Covariance Matrix timer
time_t start = time(nullptr);
int thread, xmax, ymax;
size_t row, row_offset;
size_t col, col_offset;
matrix_t<S>* m;
matrix_t<S>* n;
// For every slice (`slice1`) in dimension `dim` of every variable
for (size_t i = 0; i < num_vars; i++) {
m = matrices[i];
row_offset = i*(m->get_cols());
xmax = m->get_cols();
// For every slice (`slice2`) in dimension `dim` of every variable
for (size_t j = 0; j < num_vars; j++) {
n = matrices[j];
col_offset = j*(n->get_cols());
ymax = n->get_cols();
#pragma omp parallel for private(thread)
for (size_t x = 0; x < xmax; x++) {
thread = omp_get_thread_num();
m->get_col(x, (S*) slice1[thread]);
/*
#pragma omp critical
{
std::cout << slice1[thread][0] << " " << std::endl;
}*/
subtract_mean(len, slice1[thread]);
for (size_t y = 0; y < ymax; y++) {
n->get_col(y, (S*) slice2[thread]);
subtract_mean(len, slice2[thread]);
// Calculate the covariance of those two slices
cov->at(x + row_offset, y + col_offset) = dot_product(slice1[thread], slice2[thread], len) / (S) (len - 1);
}
}
}
}
// Print the time required to compute the covariance matrix
time_t end = time(nullptr);
double time = difftime(end,start);
std::cout << "covmat: " << time << "s; ";
}
template<typename S, typename T>
void eof_t<S, T>::spectral_covariance_kernal(
size_t num_vars,
matrix_t<S>** matrices,
matrix_t<S>* cov,
int omegas_len,
T* omegas,
const size_t num_threads){
size_t len = matrices[0]->get_rows();
S slice1[num_threads][len];
S slice2[num_threads][len];
// Start the Covariance Matrix timer
time_t start = time(nullptr);
int thread, xmax, ymax;
size_t row, row_offset;
size_t col, col_offset;
matrix_t<S>* m;
matrix_t<S>* n;
// For every slice (`slice1`) in dimension `dim` of every variable
for (size_t i = 0; i < num_vars; i++) {
m = matrices[i];
row_offset = i*(m->get_cols());
xmax = m->get_cols();
// For every slice (`slice2`) in dimension `dim` of every variable
for (size_t j = 0; j < num_vars; j++) {
n = matrices[j];
col_offset = j*(n->get_cols());
ymax = n->get_cols();
#pragma omp parallel for private(thread)
for (size_t x = 0; x < xmax; x++) {
thread = omp_get_thread_num();
m->get_col(x, (S*) slice1[thread]);
subtract_mean(len, slice1[thread]);
for (size_t y = 0; y < ymax; y++) {
n->get_col(y, (S*) slice2[thread]);
subtract_mean(len, slice2[thread]);
// Calculate the covariance of those two slices
cov->at(x + row_offset, y + col_offset) = convolve(slice1[thread], slice2[thread], omegas, len);
}
}
}
}
// Print the time required to compute the covariance matrix
time_t end = time(nullptr);
double time = difftime(end,start);
std::cout << "covmat: " << time << "s; ";
}
template<typename S, typename T>
void eof_t<S, T>::circular_covariance_kernal(
size_t num_vars,
matrix_t<S>** matrices,
matrix_t<S>* cov,
const size_t num_threads){
if (!std::is_same<S,T>::value){
FATAL("Circular covariance is currently only implemented for real-valued data.")
}else{
size_t len = matrices[0]->get_rows();
S slice1[num_threads][len];
S slice2[num_threads][len];
// Start the Covariance Matrix timer
time_t start = time(nullptr);
int thread, xmax, ymax;
size_t row, row_offset;
size_t col, col_offset;
matrix_t<S>* m;
matrix_t<S>* n;
// For every slice (`slice1`) in dimension `dim` of every variable
for (size_t i = 0; i < num_vars; i++) {
m = matrices[i];
row_offset = i*(m->get_cols());
xmax = m->get_cols();
// For every slice (`slice2`) in dimension `dim` of every variable
for (size_t j = 0; j < num_vars; j++) {
n = matrices[j];
col_offset = j*(n->get_cols());
ymax = n->get_cols();
#pragma omp parallel for private(thread)
for (size_t x = 0; x < xmax; x++) {
thread = omp_get_thread_num();
m->get_col(x, (S*) slice1[thread]);
//subtract_mean(len, slice1[thread]);
for (size_t y = 0; y < ymax; y++) {
n->get_col(y, (S*) slice2[thread]);
//subtract_mean(len, slice2[thread]);
// Calculate the covariance of those two slices
cov->at(x + row_offset, y + col_offset) = circ_cov(slice1[thread], slice2[thread], len);
}
}
}
}
// Print the time required to compute the covariance matrix
time_t end = time(nullptr);
double time = difftime(end,start);
std::cout << "covmat: " << time << "s; ";
}
}
/**
* TODO
*/
template<typename S, typename T>
void eof_t<S, T>::make_covariance_matrix(
std::vector<variable_t<S, T>*> input_vars,
std::string dim,
matrix_t<S>* cov,
matrix_reducer_t<S>** reducers,
const size_t num_threads,
bool is_circular,
bool is_spectral,
int omegas_len,
T* omegas
) {
size_t size = 0;
size_t num_vars = input_vars.size();
matrix_t<S>** matrices;
matrices = new matrix_t<S>*[num_vars];
for (size_t i = 0; i < num_vars; i++) {
variable_t<S, T>* var = input_vars[i];
matrix_t<S>* unreduced = var->to_matrix(dim);
cout << endl << unreduced->get_cols() << endl;
if (var->has_missing_value()) {
reducers[i] = new matrix_reducer_t<S>(unreduced, var->get_missing_value());
} else {
reducers[i] = new matrix_reducer_t<S>(unreduced, always_false<S>());
}
cout << endl << reducers[i]->get_reduced_cols() << endl;
size += reducers[i]->get_reduced_cols();
matrices[i] = reducers[i]->reduce(unreduced);
delete unreduced;
}
cov->set_shape(size, size);
// TODO interpolate here? The data is organized into neat matrices so this
// is probably the best place to interpolate
// This assumes that all matrices have the same number of rows. If we
// need to, we've already interpolated
// kernel calls
if(is_circular){
this->circular_covariance_kernal(num_vars, matrices, cov, num_threads);
}else{
if(is_spectral){
if(!omegas){
FATAL("Data is spectral, but no frequency data is present!")
}else{
this->spectral_covariance_kernal(num_vars, matrices, cov, omegas_len, omegas, num_threads);
}
}else{
this->covariance_kernal(num_vars, matrices, cov, num_threads);
}
}
for (int i = 0; i < num_vars; i++) {
delete matrices[i];
}
}
/**
* TODO
*/
template<typename S, typename T>
std::vector<variable_t<S, T>*> eof_t<S, T>::get_eofs(
std::vector<variable_t<S, T>*> input_vars,
std::string input_dim,
dimension_t<T>* eof_dim,
matrix_t<S>* u,
matrix_reducer_t<S>** reducers
) {
std::vector<variable_t<S, T>*> output_vars;
size_t col = 0;
for (size_t i = 0; i < input_vars.size(); i++) {
variable_t<S, T>* var = input_vars[i];
size_t size = reducers[i]->get_reduced_cols();
matrix_t<S>* mat = u->get_submatrix(0, col, u->get_rows(), size);
matrix_t<S>* restored = reducers[i]->restore(mat, 10.f*var->get_absmax());
variable_t<S, T>* output = var->from_matrix(restored, input_dim, eof_dim);
// Copy all variable variables and dimension attributes where applicable
output->set_attrs(var->get_num_attrs(), var->get_attrs());
for (size_t j = 0; j < var->get_num_dims(); j++){
if (var->get_dim(j) != eof_dim){
attribute_t** attrs = new attribute_t*[var->get_dim(j)->get_num_attrs()];
for (size_t k = 0; k < var->get_dim(j)->get_num_attrs(); k++){
attrs[k] = new attribute_t(*(var->get_dim(j)->get_attr(k)));
}
for (size_t k = 0; k < output->get_num_dims(); k++){
if (var->get_dim(j) == output->get_dim(k)){
output->set_dim_attrs(k, var->get_dim(j)->get_num_attrs(), attrs);
}
}
}
}
delete restored;
delete mat;
output_vars.push_back(output);
col += size;
}
return output_vars;
}
//==============================================================================
// Public Methods
//==============================================================================
/**
* TODO
*/
template<typename S, typename T>
eof_t<S, T>::eof_t() {
this->svd = new basic_svd_t<T>();
//this->interp = nullptr;
}
/**
* TODO
*/
template<typename S, typename T>
eof_t<S, T>::~eof_t() {
delete this->svd;
/*
if (this->interp != nullptr) {
delete this->interp;
}
*/
}
/**
* TODO
*/
template<typename S, typename T>
void eof_t<S, T>::set_svd(svd_t<T>* svd) {
delete this->svd;
this->svd = svd;
}
/**
* TODO
*/
/*
template<typename S, typename T>
void eof_t<S, T>::set_interp(interp_t<S>* interp) {
if (this->interp != nullptr) {
delete this->interp;
}
this->interp = interp;
}
*/
/**
* TODO
*/
/*
template<typename S, typename T>
void eof_t<S, T>::no_interp() {
if (this->interp != nullptr) {
delete this->interp;
}
}
*/
/**
* TODO
*/
template<typename S, typename T>
std::vector<variable_t<S, T>*> eof_t<S, T>::calculate(
variable_t<S, T>* input_var,
const std::string input_dim,
const size_t input_nthreads,
bool is_circular,
bool is_spectral,
int omegas_len,
T* omegas
) {
return this->calculate({input_var}, input_dim, input_nthreads, is_circular, is_spectral, omegas_len, omegas);
}
/**
* TODO
*/
template<typename S, typename T>
std::vector<variable_t<S, T>*> eof_t<S, T>::calculate(
std::initializer_list<variable_t<S, T>*> input_vars_list,
const std::string input_dim,
const size_t input_nthreads,
bool is_circular,
bool is_spectral,
int omegas_len,
T* omegas
) {
std::vector<variable_t<S, T>*> input_vars(std::begin(input_vars_list), std::end(input_vars_list));
return this->calculate(input_vars, input_dim, input_nthreads, is_circular, is_spectral, omegas_len, omegas);
}
/**
* TODO
*/
template<typename S, typename T>
std::vector<variable_t<S, T>*> eof_t<S, T>::calculate(
std::vector<variable_t<S, T>*> input_vars,
const std::string input_dim,
const size_t input_nthreads,
bool is_circular,
bool is_spectral,
int omegas_len,
T* omegas
) {
if (input_vars.size() == 0) {
throw eof_error_t("No variables to be analyzed");
}
if(is_spectral){
if(omegas_len == -1){
FATAL("Number of spectral frequencies was not specified.")
}
if(!omegas){
FATAL("Spectral frequencies were not specified.")
}
}
this->match_dimension_in_all_variables(input_vars, input_dim);
matrix_t<S> cov;
matrix_reducer_t<S>* reducers[input_vars.size()];
this->make_covariance_matrix(input_vars, input_dim, &cov, reducers, input_nthreads, is_circular, is_spectral, omegas_len, omegas);
matrix_t<T> s;
matrix_t<S> u;
this->svd->calculate(&cov, &u, &s, nullptr);
const T* row = s.get_row(0);
std::string output_dim = "eigenvalues";
dimension_t<T> eof_dim(output_dim, s.get_cols(), row, 0, nullptr);
std::vector<variable_t<S, T>*> output_vars = this->get_eofs(input_vars, input_dim, &eof_dim, &u, reducers);
delete[] row;
for (size_t i = 0; i < input_vars.size(); i++) {
delete reducers[i];
}
return output_vars;
}