-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunpackMatrix.cpp
More file actions
257 lines (232 loc) · 9.29 KB
/
Copy pathunpackMatrix.cpp
File metadata and controls
257 lines (232 loc) · 9.29 KB
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
/* Copyright (C) 2010 Ion Torrent Systems, Inc. All Rights Reserved */
/**
* unpackMatrix.cpp
* Exported interface: unpackMatrix
* This is the file that contains all linear algebra on the decompression side
*
* @author Magnus Jedvert
* @version 1.1 april 2012
*/
#include <armadillo>
#include <vector>
#include <cassert>
#include "matrixRounding.h"
#include "unpackMatrix.h"
#include "VencoLossless.h"
#include "BitHandler.h"
//#include "ByteHandler.h"
using namespace std;
using namespace arma;
//using namespace BPacker;
// typedef uint8_t u8; // defined in arma for some reason
// typedef uint16_t u16;
typedef int16_t i16;
typedef int32_t i32;
/**
* unpackMatrix - Reverse of packMatrix. Stores the matrix in memory pointed
* to by dst. Pops the information from src.
*/
void unpackMatrix(ByteUnpacker &src, u16 *dst, size_t L) {
// extract key numbers:
const vector<size_t> header = src.pop<size_t>(5);
const size_t nGood = header[0];
const size_t nBad = header[1];
const size_t RANK_GOOD = header[2];
const size_t rankBad = header[3];
const bool uses16Bit = header[4];
const int N = nGood + nBad;
// assert(rankBad == L);
// dont copy the memory, use it directly instead:
#define POP_CONSTRUCTOR(T, H, W) src.popPtr<T>(H*W), H, W, false, true
// extract basis:
const fmat basis( POP_CONSTRUCTOR(float, L, rankBad) );
const fmat basisGood = basis.cols(0, RANK_GOOD-1);
// extract partion:
const Col<u8> partion( src.popPtr<u8>(N), N, false, true );
const uvec goodIdx = find(partion == 0);
const uvec badIdx = find(partion == 1);
assert( goodIdx.n_elem == nGood && badIdx.n_elem == nBad);
// make a matrix using the memory pointed to by dst:
Mat<uint16_t> restored(dst, L, N, false, true);
if ( uses16Bit ) {
// extract scores:
const Mat<i16> scoreGood( POP_CONSTRUCTOR(i16, nGood, RANK_GOOD) );
const Mat<i16> scoreBad( POP_CONSTRUCTOR(i16, nBad, rankBad) );
// fill this matrix with the matrix multiplications:
//restored.cols(goodIdx) = convToValidU16( basisGood * convToFloat(scoreGood).t() );
//restored.cols(badIdx) = convToValidU16( basis * convToFloat(scoreBad).t() );
restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
restored.cols(badIdx) = convToValidU16( basis * scoreBad.t() );
}
else {
// extract scores:
const Mat<i32> scoreGood( POP_CONSTRUCTOR(i32, nGood, RANK_GOOD) );
const Mat<i32> scoreBad( POP_CONSTRUCTOR(i32, nBad, rankBad) );
// fill this matrix with the matrix multiplications:
//restored.cols(goodIdx) = convToValidU16( basisGood * conv_to<fmat>::from(scoreGood).t() );
//restored.cols(badIdx) = convToValidU16( basis * conv_to<fmat>::from(scoreBad).t() );
restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
restored.cols(badIdx) = convToValidU16( basis * scoreBad.t() );
}
Mat<uint16_t> gtmp = restored.cols(goodIdx);
gtmp.save("svdp_decomGood.txt",raw_ascii);
Mat<uint16_t> btmp = restored.cols(badIdx);
btmp.save("svdp_decomBad.txt",raw_ascii);
}
#define POP_CONSTRUCTOR(T, H, W) src.popPtr<T>(H*W), H, W, false, true
void unpackMatrixPlus(ByteUnpacker &src, u16 *dst, size_t L) {
// extract key numbers:
const vector<size_t> header = src.pop<size_t>(5);
const size_t nGood = header[0];
const size_t nBad = header[1];
const size_t RANK_GOOD = header[2];
//const size_t rankBad = header[3];
const bool uses16Bit = header[4];
const int N = nGood + nBad;
//assert(rankBad == L);
// extract partion:
const Col<u8> partion( src.popPtr<u8>(N), N, false, true );
const uvec goodIdx = find(partion == 0);
const uvec badIdx = find(partion == 1);
assert( goodIdx.n_elem == nGood && badIdx.n_elem == nBad);
// make a matrix using the memory pointed to by dst:
Mat<u16> restored(dst, L, N, false, true);
//decompress the good traces:
if ( nGood > 0) {
// extract basis:
vector<size_t> gSize = src.pop<size_t>(2);
vector<uint8_t> gHuffOut = src.pop<uint8_t>(gSize[0]);
BitUnpacker gbitPacker(gHuffOut);
vector<uint8_t> tmpGood;
tmpGood.resize(gSize[1]);
gbitPacker.get_compressed(&tmpGood[0],gSize[1]);
ByteUnpacker gbp((char*)tmpGood.data());
const Mat<float> basisGood( gbp.popPtr<float>( L * RANK_GOOD), L, RANK_GOOD, false, true);
if ( uses16Bit ) {
// extract scores:
const Mat<int16_t> scoreGood( gbp.popPtr<int16_t>( nGood * RANK_GOOD), nGood, RANK_GOOD, false, true);
// fill this matrix with the matrix multiplications:
restored.cols(goodIdx) = convToValidU16( basisGood * convToFloat(scoreGood).t() );
//restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
}
else {
// extract scores:
const Mat<int32_t> scoreGood( gbp.popPtr<int32_t>(nGood * RANK_GOOD), nGood, RANK_GOOD, false, true);
// fill this matrix with the matrix multiplications:
restored.cols(goodIdx) = convToValidU16( basisGood * conv_to<fmat>::from(scoreGood).t() );
//restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
}
}
//Decompress the bad data, output the uncompressed to restored.cols(badIdx);
if ( nBad > 0)
{
vector<size_t> sSize = src.pop<size_t>(1);
const vector<uint8_t> deltaIn = src.pop<uint8_t>(sSize[0]);
Decompressor dc;
size_t nCols;
size_t nRows;
size_t oL;
vector<uint16_t> deltaOut;
dc.decompress(deltaIn,nRows,nCols, oL, deltaOut);
Mat<uint16_t> dataBadMat(&deltaOut[0], L, nBad, false, true);
restored.cols(badIdx) = dataBadMat;
}
}
/* unpackMatrixPlusV2 - */
void unpackMatrixPlusV2(ByteUnpacker &src, u16 *dst, size_t L) {
// extract key numbers:
const vector<size_t> header = src.pop<size_t>(5);
const size_t nGood = header[0];
const size_t nBad = header[1];
const size_t RANK_GOOD = header[2];
//const size_t rankBad = header[3];
const bool uses16Bit = header[4];
const int N = nGood + nBad;
//assert(rankBad == L);
// extract partion:
const Col<u8> partion( src.popPtr<u8>(N), N, false, true );
const uvec goodIdx = find(partion == 0);
const uvec badIdx = find(partion == 1);
assert( goodIdx.n_elem == nGood && badIdx.n_elem == nBad);
// make a matrix using the memory pointed to by dst:
Mat<u16> restored(dst, L, N, false, true);
//decompress the good traces:
if ( nGood > 0) {
// extract basis:
const fmat basisGood(POP_CONSTRUCTOR(float, L, RANK_GOOD));
if ( uses16Bit ) {
// extract scores:
const Mat<int16_t> scoreGood( POP_CONSTRUCTOR(i16, nGood,RANK_GOOD));
restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
}
else {
// extract scores:
const Mat<int32_t> scoreGood( POP_CONSTRUCTOR(i32, nGood,RANK_GOOD));
restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
//restored.cols(goodIdx) = convToValidU16( basisGood * conv_to<fmat>::from(scoreGood).t() );
}
}
//Decompress the bad data, output the uncompressed to restored.cols(badIdx);
if ( nBad > 0)
{
vector<size_t> sSize = src.pop<size_t>(1);
const vector<uint8_t> deltaIn = src.pop<uint8_t>(sSize[0]);
Decompressor dc;
size_t nCols;
size_t nRows;
size_t oL;
vector<uint16_t> deltaOut;
dc.byteDecompress(deltaIn,nRows,nCols, oL, deltaOut);
Mat<uint16_t> dataBadMat(&deltaOut[0], L, nBad, false, true);
restored.cols(badIdx) = dataBadMat;
}
}
void unpackMatrixPlusV3(ByteUnpacker &src, u16 *dst, size_t L) {
// extract key numbers:
const vector<size_t> header = src.pop<size_t>(5);
const size_t nGood = header[0];
const size_t nBad = header[1];
const size_t RANK_GOOD = header[2];
//const size_t rankBad = header[3];
const bool uses16Bit = header[4];
const int N = nGood + nBad;
//assert(rankBad == L);
const fmat basisGood( POP_CONSTRUCTOR(float, L, RANK_GOOD) );
// extract partion:
const Col<u8> partion( src.popPtr<u8>(N), N, false, true );
const uvec goodIdx = find(partion == 0);
const uvec badIdx = find(partion == 1);
assert( goodIdx.n_elem == nGood && badIdx.n_elem == nBad);
// make a matrix using the memory pointed to by dst:
Mat<u16> restored(dst, L, N, false, true);
//decompress the good traces:
if ( nGood > 0) {
if ( uses16Bit ) {
// extract scores:
const Mat<i16> scoreGood( POP_CONSTRUCTOR(i16, nGood, RANK_GOOD) );
//restored.cols(goodIdx) = convToValidU16( basisGood * convToFloat(scoreGood).t() );
restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
}
else {
// extract scores:
const Mat<i32> scoreGood( POP_CONSTRUCTOR(i32, nGood, RANK_GOOD) );
// fill this matrix with the matrix multiplications:
//restored.cols(goodIdx) = convToValidU16( basisGood * conv_to<fmat>::from(scoreGood).t() );
restored.cols(goodIdx) = convToValidU16( basisGood * scoreGood.t() );
}
}
//Decompress the bad data, output the uncompressed to restored.cols(badIdx);
if ( nBad > 0)
{
vector<size_t> sSize = src.pop<size_t>(1);
const vector<uint8_t> deltaIn = src.pop<uint8_t>(sSize[0]);
Decompressor dc;
size_t nCols;
size_t nRows;
size_t oL;
vector<uint16_t> deltaOut;
dc.decompress(deltaIn,nRows,nCols, oL, deltaOut);
Mat<uint16_t> dataBadMat(&deltaOut[0], L, nBad, false, true);
restored.cols(badIdx) = dataBadMat;
}
}