-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialGMM.hpp
670 lines (586 loc) · 18.8 KB
/
SpatialGMM.hpp
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#pragma once
#include <mpi.h>
#include <array>
#include <fstream>
#include <iostream>
#include <limits>
#include <random>
#include <string>
#include <vector>
#include "utils.h"
#include "GMM3D.hpp"
class Configuration {
public:
void setDataPath(std::string path) { m_dataPath = path; }
std::string getDataPath() { return m_dataPath; }
void setRawDataFilename(std::string filename) { m_rawDataFilename = filename; }
std::string getRawDataFilename() { return m_rawDataFilename; }
void setDimensions(int x, int y, int z) { m_dimensions = std::array<int, 3>{x, y, z}; }
std::array<int, 3> getDimensions() { return m_dimensions; }
void setBlockSize(int size) { blockSize = size; }
int getBlockSize() { return blockSize; }
void setVariableName(std::string varName) { m_variableName = varName; }
std::string getVariableName() { return m_variableName; }
private:
std::string m_dataPath;
std::string m_rawDataFilename;
std::array<int, 3> m_dimensions;
int blockSize;
std::string m_variableName;
};
namespace SpatialGMM {
template<typename T>
class Bin {
public:
void buildGMMs(std::vector<std::array<float, 3>>& dataset) {
int maxClusters = 4;
T bestBIC = std::numeric_limits<T>::max();
GMM3D<T> gmm;
for (int n = 1; n <= maxClusters; n++) {
gmm.fit(n, dataset);
gmm.setValid();
T bic = BayesInformationCriteria<T>(gmm, dataset);
if(std::isnan(bic)) {
gmm.setInvalid();
break;
}
if (bic < bestBIC) {
bestBIC = bic;
}
else {
break;
}
}
m_GMM = gmm;
}
GMM3D<T> getGMM() { return m_GMM; }
void setGMM(GMM3D<T> gmm) { m_GMM = gmm; }
void setProbability(T p) { m_probability = p; }
T getProbability() { return m_probability; }
T predict(float x, float y, float z) {
return m_GMM.predict({ x, y, z });
}
private:
T m_probability;
GMM3D<T> m_GMM;
};
/**
* @param m_bins: store spatial locations as float numbers
* @param m_minValue
* @param m_maxValue
* @param m_Id: global blockId
* @param m_dims: dimension
* @param m_dataLocal
*/
template<typename T>
class DataBlock {
public:
void setblockId(int xId, int yId, int zId) {
m_Id[0] = xId;
m_Id[1] = yId;
m_Id[2] = zId;
}
std::array<int, 3> getBlockId() { return m_Id; }
void setData(std::vector<T>& data) {
m_dataLocal = data;
}
std::vector<T> getData() { return m_dataLocal; }
// for each bin, build a spatial distribution by GMM
void buildGMMsPerBin(int blockSize) {
int numOfBins = m_bins.size();
for (int binId = 0; binId < numOfBins; binId++) {
if (m_bins[binId].getProbability() <= 0.0) {
continue;
}
int startX = m_Id[0] * blockSize;
int startY = m_Id[1] * blockSize;
int startZ = m_Id[2] * blockSize;
int endX = startX + m_dims[0];
int endY = startY + m_dims[1];
int endZ = startZ + m_dims[2];
if (endX >= m_dims[0]) {
endX = m_dims[0] - 1;
}
if (endY >= m_dims[1]) {
endY = m_dims[1] - 1;
}
if (endZ >= m_dims[2]) {
endZ = m_dims[2] - 1;
}
T binMin = m_minValue + (m_maxValue - m_minValue) * static_cast<T>(binId) / static_cast<T>(numOfBins);
T binMax =
m_minValue + (m_maxValue - m_minValue) * static_cast<T>(binId + 1) / static_cast<T>(numOfBins);
std::vector<std::array<float, 3>> dataset;
// get the data points in this bin
for (int blockZ = 0; blockZ < m_dims[0]; ++blockZ) {
for (int blockY = 0; blockY < m_dims[1]; ++blockY) {
for (int blockX = 0; blockX < m_dims[2]; ++blockX) {
T value = m_dataLocal[blockX + blockY * m_dims[0] + blockZ * m_dims[0] * m_dims[1]];
if (value >= binMin && value < binMax) {
//dataset.push_back(
// { {float(blockX + startX), float(blockY + startY), float(blockZ + startZ)} });
//dataset.push_back(
// { {float(blockX) / float(m_dims[0]), float(blockY) / float(m_dims[1]), float(blockZ) / float(m_dims[2])}});
dataset.push_back(
{ float(blockX), float(blockY), float(blockZ) });
}
if (value == binMax && binId == numOfBins - 1) {
//dataset.push_back(
// { {float(blockX + startX), float(blockY + startY), float(blockZ + startZ)} });
//dataset.push_back(
// { {float(blockX) / float(m_dims[0]), float(blockY) / float(m_dims[1]), float(blockZ) / float(m_dims[2])} });
dataset.push_back(
{ float(blockX), float(blockY), float(blockZ) });
}
}
}
}
if (dataset.size() > 0) {
m_bins[binId].buildGMMs(dataset);
}
}
}
void buildHistogram() {
// calculate min and max of local data block
T min = m_dataLocal[0];
for (int i = 1; i < m_dataLocal.size(); ++i) {
if (m_dataLocal[i] < min) {
min = m_dataLocal[i];
}
}
T max = m_dataLocal[0];
for (int i = 1; i < m_dataLocal.size(); ++i) {
if (m_dataLocal[i] > max) {
max = m_dataLocal[i];
}
}
int numOfBins = 128;
m_bins.resize(numOfBins);
std::vector<T> histogram(numOfBins);
// calculate histogram
for (int i = 0; i < m_dataLocal.size(); ++i) {
T value = m_dataLocal[i];
if (value <= min) {
histogram[0] += 1.0f;
}
else if (value >= max) {
histogram[numOfBins - 1] += 1.0f;
}
else {
// find the bin that contains the value
for (int j = 0; j < numOfBins; ++j) {
T binMin = min + (max - min) * static_cast<T>(j) / static_cast<T>(numOfBins);
T binMax =
min + (max - min) * (static_cast<T>(j + 1)) / static_cast<T>(numOfBins);
if (value >= binMin && value < binMax) {
histogram[j] += 1.0f;
break;
}
}
}
}
for (int i = 0; i < numOfBins; ++i) {
m_bins[i].setProbability(histogram[i] / static_cast<T>(m_dataLocal.size()));
}
}
void setMinMax(T minVal, T maxVal) {
m_minValue = minVal;
m_maxValue = maxVal;
}
T getMin() {
return m_minValue;
}
T getMax() {
return m_maxValue;
}
void setDims(int x, int y, int z) {
m_dims = std::array<int, 3>{ x, y, z };
}
std::array<int, 3> getDims() {
return m_dims;
}
std::vector<Bin<float>> getBins() { return m_bins; }
/**
* export format
* - blockId[0], blockId[1], blockId[2]
* - blockDims[0], blockDims[1], blockDims[2]
* - numOfBins
* - minValue
* - maxValue
* - histogram
* - binId
* - probability
* - numOfGaussians
* - GaussiansList
* - weight
* - means
* - covariance(upper triangular)
*/
std::vector<T> exportStatistics(){
std::vector<T> statistics;
statistics.push_back(m_Id[0]);
statistics.push_back(m_Id[1]);
statistics.push_back(m_Id[2]);
statistics.push_back(m_dims[0]);
statistics.push_back(m_dims[1]);
statistics.push_back(m_dims[2]);
statistics.push_back(m_bins.size());
statistics.push_back(m_minValue);
statistics.push_back(m_maxValue);
for (int i = 0; i < m_bins.size(); ++i) {
Bin<float> bin = m_bins[i];
if (bin.getProbability() <= 1e-9) {
statistics.push_back(-1);
continue;
}
statistics.push_back(i);
statistics.push_back(bin.getProbability());
GMM3D<T> gmm = bin.getGMM();
int numKernels = gmm.numComponents();
statistics.push_back(numKernels);
for (int k = 0; k < numKernels; k++) {
statistics.push_back(gmm.getWeight(k));
}
for (int k = 0; k < numKernels; k++) {
Gaussian3D<T> gaussian = gmm.getGaussian(k);
std::array<T, 3> mean = gaussian.mean();
statistics.push_back(mean[0]);
statistics.push_back(mean[1]);
statistics.push_back(mean[2]);
}
for (int k = 0; k < numKernels; k++) {
Gaussian3D<T> gaussian = gmm.getGaussian(k);
std::array<std::array<T, 3>, 3> covariance = gaussian.covariance();
// export upper triangular matrix
for (int i = 0; i < 3; ++i) {
for (int j = i; j < 3; ++j) {
statistics.push_back(covariance[i][j]);
}
}
}
}
return statistics;
}
void setBins(std::vector<Bin<float>> bins) {
m_bins = bins;
}
private:
std::vector<Bin<float>> m_bins;
T m_minValue;
T m_maxValue;
std::array<int, 3> m_Id;
std::array<int, 3> m_dims;
std::vector<T> m_dataLocal;
};
template<typename T>
using Representation = std::vector<DataBlock<T>>;
template<typename T>
Representation<T> DataModeling(std::array<int, 3> dims, int blockSize, std::string filename, Endian endian) {
Representation<T> dataBlockRepresentation;
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int size = dims[0] * dims[1] * dims[2];
std::ifstream file(filename, std::ios::binary);
double start_time = MPI_Wtime();
int numGroups[3] = { dims[0] / blockSize, dims[1] / blockSize,
dims[2] / blockSize };
for (int z = 0; z < numGroups[2]; z++) {
for (int y = 0; y < numGroups[1]; y++) {
for (int x = 0; x < numGroups[0]; x++) {
int blockId = x + y * numGroups[0] + z * numGroups[1] * numGroups[0];
int processId = blockId % world_size;
if (processId == world_rank) {
DataBlock<T> block;
block.setblockId(x, y, z);
dataBlockRepresentation.push_back(block);
}
}
}
}
for (DataBlock<T>& dataBlock : dataBlockRepresentation) {
std::array<int, 3> blockId = dataBlock.getBlockId();
int startX = blockId[0] * blockSize;
int startY = blockId[1] * blockSize;
int startZ = blockId[2] * blockSize;
int endX = startX + blockSize;
int endY = startY + blockSize;
int endZ = startZ + blockSize;
if (endX >= dims[0]) {
endX = dims[0] - 1;
}
if (endY >= dims[1]) {
endY = dims[1] - 1;
}
if (endZ >= dims[2]) {
endZ = dims[2] - 1;
}
dataBlock.setDims(endX - startX, endY - startY, endZ - startZ);
std::vector<T> dataLocal;
std::array<int, 3> blockDims = { endX - startX, endY - startY,
endZ - startZ };
// get local data block
for (int blockZ = 0; blockZ < blockDims[2]; ++blockZ) {
for (int blockY = 0; blockY < blockDims[1]; ++blockY) {
for (int blockX = 0; blockX < blockDims[0]; ++blockX) {
int id = (startX + blockX + (startY + blockY) * dims[0] +
(startZ + blockZ) * dims[0] * dims[1]);
file.seekg(id * sizeof(T), std::ios::beg);
T val;
// read a value to buf
if (file.read(reinterpret_cast<char*>(&val), sizeof(T))) {
if (endian == Endian::Big) {
val = reverseByteOrder(val);
}
dataLocal.push_back(*reinterpret_cast<T*>(&val));
}
else {
std::cout << "Error: read failed" << std::endl;
return {};
}
}
}
}
T minValue = dataLocal[0];
T maxValue = dataLocal[0];
for (auto& val : dataLocal) {
if (val < minValue) {
minValue = val;
}
else if (val > maxValue) {
maxValue = val;
}
}
dataBlock.setMinMax(minValue, maxValue);
dataBlock.setData(dataLocal);
dataBlock.buildHistogram();
dataBlock.buildGMMsPerBin(blockSize);
}
double end_time = MPI_Wtime();
if (world_rank == 0) {
file.close();
std::cout << "Time elapsed: " << end_time - start_time << "s" << std::endl;
}
return dataBlockRepresentation;
}
/**
* @brief Estimate the pdf of a point
* @param x, y, z: coordinates of the point
* @param binId: id of the histogram bin
*/
template<typename T>
std::vector<float> ValueEstimation(DataBlock<T>& dataBlock, float x, float y, float z) {
std::vector<Bin<float>> bins = dataBlock.getBins();
std::vector<float> pdf(bins.size(), 0.0);
for (int i = 0; i < bins.size(); i++) {
float Hi = bins[i].getProbability();
if (Hi > 0.0) {
pdf[i] = bins[i].predict(x, y, z) * Hi;
}
}
float sumOfProb = 0.0;
for (auto& val : pdf) {
sumOfProb += val;
}
for (auto& val : pdf) {
val /= sumOfProb;
}
return pdf;
}
template<typename T>
void ExportReconstructedVolume(std::string outputFilename, Representation<T>& dataBlocks, std::array<int, 3> dims, int blockSize) {
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int size = dims[0] * dims[1] * dims[2];
if (world_rank == 0) {
std::ofstream outputFile(outputFilename, std::ios::binary);
for (int i = 0; i < size; ++i) {
float val = 0.0f;
outputFile.write(reinterpret_cast<char*>(&val), sizeof(float));
}
outputFile.close();
}
std::ofstream outputFile(outputFilename, std::ios::binary);
for (DataBlock<T>& dataBlock : dataBlocks) {
std::array<int, 3> blockId = dataBlock.getBlockId();
std::array<int, 3> blockDims = dataBlock.getDims();
int startX = blockId[0] * blockSize;
int startY = blockId[1] * blockSize;
int startZ = blockId[2] * blockSize;
int endX = startX + blockDims[0];
int endY = startY + blockDims[1];
int endZ = startZ + blockDims[2];
if (endX >= dims[0]) {
endX = dims[0] - 1;
}
if (endY >= dims[1]) {
endY = dims[1] - 1;
}
if (endZ >= dims[2]) {
endZ = dims[2] - 1;
}
// output data reconstructed to file
std::vector<float> reconstructedVolume(blockDims[0] * blockDims[1] * blockDims[2]);
std::vector<Bin<float>> bins = dataBlock.getBins();
// adjust the histogram
for (Bin<float>& bin : bins) {
float probability = bin.getProbability();
if (probability > 0.0 && !std::isnan(probability)) {
float factor = 0.0;
for (int z = 0; z < blockDims[2]; ++z) {
for (int y = 0; y < blockDims[1]; ++y) {
for (int x = 0; x < blockDims[0]; ++x) {
//float vx = static_cast<float>(x + startX);
//float vy = static_cast<float>(y + startY);
//float vz = static_cast<float>(z + startZ);
//float vx = static_cast<float>(x) / static_cast<float>(blockDims[0]);
//float vy = static_cast<float>(y) / static_cast<float>(blockDims[1]);
//float vz = static_cast<float>(z) / static_cast<float>(blockDims[2]);
float vx = static_cast<float>(x);
float vy = static_cast<float>(y);
float vz = static_cast<float>(z);
float probability = bin.predict(vx, vy, vz);
if (!std::isnan(probability)) {
factor += probability;
}
}
}
}
if (factor > 0.0) {
bin.setProbability(probability / factor);
}
else {
bin.setProbability(0.0);
}
}
}
for (int z = 0; z < blockDims[2]; ++z) {
for (int y = 0; y < blockDims[1]; ++y) {
for (int x = 0; x < blockDims[0]; ++x) {
//float vx = static_cast<float>(x + startX);
//float vy = static_cast<float>(y + startY);
//float vz = static_cast<float>(z + startZ);
//float vx = static_cast<float>(x) / static_cast<float>(blockDims[0]);
//float vy = static_cast<float>(y) / static_cast<float>(blockDims[1]);
//float vz = static_cast<float>(z) / static_cast<float>(blockDims[2]);
float vx = static_cast<float>(x);
float vy = static_cast<float>(y);
float vz = static_cast<float>(z);
std::vector<float> pdf = ValueEstimation(dataBlock, vx, vy, vz);
T minValue = dataBlock.getMin();
T maxValue = dataBlock.getMax();
T dx = (maxValue - minValue) / (float)(pdf.size());
//// pick the most likely bin
//for (int i = 0; i < pdf.size(); ++i)
//{
// reconstructedVolume[x + y * blockDims[0] + z * blockDims[0] * blockDims[1]] +=
// (minValue + static_cast<float>(i) * dx + 0.5 * dx) * pdf[i];
//}
int mostLikelyBin = 0;
for (int i = 0; i < pdf.size(); ++i)
{
if (!std::isnan(pdf[i]) && pdf[mostLikelyBin] < pdf[i]) {
mostLikelyBin = i;
}
}
reconstructedVolume[x + y * blockDims[0] + z * blockDims[0] * blockDims[1]] =
(minValue + static_cast<float>(mostLikelyBin) * dx);
}
}
}
// export reconstructed volume
for (int blockZ = 0; blockZ < blockDims[2]; ++blockZ) {
for (int blockY = 0; blockY < blockDims[1]; ++blockY) {
for (int blockX = 0; blockX < blockDims[0]; ++blockX) {
int id = (startX + blockX + (startY + blockY) * dims[0] +
(startZ + blockZ) * dims[0] * dims[1]);
outputFile.seekp(id * sizeof(float), std::ios::beg);
float val =
reconstructedVolume[blockX + blockY * blockDims[0] + blockZ * blockDims[0] * blockDims[1]];
outputFile.write(reinterpret_cast<char*>(&val), sizeof(float));
}
}
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (world_rank == 0)
outputFile.close();
}
/**
* Save parameters to JSON file.
*/
template<typename T>
void SaveParameters(std::string filename, Representation<T>& data) {
std::ofstream outputFile(filename);
outputFile << "[" << std::endl;
int id = 0;
int numBlocks = data.size();
for (DataBlock<T>& dataBlock : data) {
outputFile << "{";
std::array<int, 3> blockId = dataBlock.getBlockId();
outputFile << "\"BlockId\":[" << blockId[0] << "," << blockId[1] << "," << blockId[2] << "]," << std::endl;
std::vector<Bin<float>> bins = dataBlock.getBins();
outputFile << "\"H\":[";
std::vector<int> validBinIds;
for(int i = 0; i < bins.size(); ++i){
if (bins[i].getProbability() > 1e-9) {
validBinIds.push_back(i);
}
}
for (int i = 0; i < validBinIds.size(); i++) {
int binId = validBinIds[i];
T probability = bins[binId].getProbability();
outputFile << "{\"I\":" << i << ",\"V\":" << probability << "}";
if (i != validBinIds.size() - 1) {
outputFile << ",";
}
}
outputFile << "],";
outputFile << "\"D\":["; // spatial GMM per bin
for (int i = 0; i < validBinIds.size(); i++) {
int binId = validBinIds[i];
T probability = bins[binId].getProbability();
outputFile << "{";
outputFile << "\"I\":" << i << ",";
outputFile << "\"V\":{";
// "w": adjustment factor of SGMM(TODO)
outputFile << "\"w\": 1.0,";
outputFile << "\"gmm\":{";
GMM3D<float> gmm = bins[binId].getGMM();
outputFile << "\"K\":" << gmm.numComponents() << ",";
outputFile << "\"g\":[";
for (int k = 0; k < gmm.numComponents(); k++) {
outputFile << "{\"w\":" << gmm.getWeight(k) << ",";
Gaussian3D<T> gaussian = gmm.getGaussian(k);
std::array<T, 3> mean = gaussian.mean();
outputFile << "\"m\":[" << mean[0] << "," << mean[1] << "," << mean[2] << "],";
std::array<std::array<T, 3>, 3> variance = gaussian.covariance();
outputFile << "\"s\":[" << variance[0][0] << "," << variance[0][1] << "," << variance[0][2]
<< "," << variance[1][1] << "," << variance[1][2] << "," << variance[2][2] << "]" << std::endl; // upper
outputFile << "}"; // one gaussian ends
if (k != gmm.numComponents() - 1) {
outputFile << ",";
}
}
outputFile << "]"; // gaussians end
outputFile << "}"; // gmm end
outputFile << "}"; // V end
outputFile << "}"; // dataBlock end
if (i != validBinIds.size() - 1) {
outputFile << ",";
}
}
outputFile << "]"; // "D" end
outputFile << "}" << std::endl; // one data block ends
if(id != numBlocks - 1) {
outputFile << "," << std::endl;
}
id++;
}
outputFile << "]" << std::endl;
outputFile.close();
}
}