-
Notifications
You must be signed in to change notification settings - Fork 4
/
sequencialDeconvolution.cpp
317 lines (283 loc) · 8.5 KB
/
sequencialDeconvolution.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
// The "more usual approach":
// - smoothing
// - SNIP (baseline removal)
// - l1-deconvolution
//
#include <csv.h>
#include <cxxopts.hpp>
#include <fstream>
#include <sstream>
#include "seqDeconv.hpp"
#include "sgFilter.hpp"
#include "snip.hpp"
namespace JointDeconv
{
}; // TODO: to remove
using namespace JointDeconv;
int main(int argc, char* argv[])
{
//////////////////////
// Argument parsing //
//////////////////////
const auto to_string = [](const auto& any) {
std::stringstream converter;
converter << any;
return std::string(converter.str());
};
std::string command_line;
for (int i = 0; i < argc; i++)
{
command_line += argv[i];
command_line += ' ';
}
cxxopts::Options options(argv[0],
"A more classical approach "
"smoothing->SNIP->deconvolution (for comparison "
"purpose only), contact vincent.picaud@cea.fr");
// Default parameter values
//
double sigma_left = 10;
double sigma_right = 10;
Size_t snip_halfWindowSize = 20;
double peakMinHeight = 0.01;
double lambda_1 = 0.1;
double lambda_2 = 0.00001;
double mu = 500;
double eps = 1e-4;
Size_t max_iter = 5000;
bool gnuplot = false;
options.add_options()
//
("i,input",
"Input file (two columns X,Y)",
cxxopts::value<std::string>(),
"FILE")
//
("o,output",
"Output file",
cxxopts::value<std::string>()->default_value("$(FILE).out"),
"OUTPUT FILE")
//
("snip",
"Snip half window size (>0), used to compute baseline",
cxxopts::value<Size_t>(snip_halfWindowSize),
to_string(snip_halfWindowSize))
//
("sigma_left",
"Peak shape factor (>0)",
cxxopts::value<double>(sigma_left),
to_string(sigma_left))
//
("sigma_right",
"Peak shape factor (>0)",
cxxopts::value<double>(sigma_right),
to_string(sigma_right))
//
("peakMinHeight",
"Minimal height to accept peak (>=0)",
cxxopts::value<double>(peakMinHeight),
to_string(peakMinHeight))
//
("lambda_1",
"lambda_1 penalty term (>=0)",
cxxopts::value<double>(lambda_1),
to_string(lambda_1))
//
("lambda_2",
"lambda_2 penalty term (>=0)",
cxxopts::value<double>(lambda_2),
to_string(lambda_2))
//
("mu", "mu penalty term (>0)", cxxopts::value<double>(mu), to_string(mu))
//
("eps", "eps goal (>=0)", cxxopts::value<double>(eps), to_string(eps))
//
("max_iter",
"maximum number of iterations (>0)",
cxxopts::value<Size_t>(max_iter),
to_string(max_iter))
//
("p,gnuplot", "Gnuplot script", cxxopts::value<bool>(gnuplot))
//
("help", "Print help");
options.parse_positional("input");
// CAVEAT: after options.parse(argc, argv); argc is set=1
// -> we have to check its value before
//
bool show_help = argc == 1;
options.parse(argc, argv);
show_help |= options.count("help") > 0;
if (show_help)
{
std::cout << options.help() << std::endl;
exit(0);
}
// Sanity check
//
if (options.count("input") == 0)
{
std::cout << "#Error: missing input FILE" << std::endl;
return EXIT_FAILURE;
}
if (sigma_left <= 0)
{
std::cout << "#Error: sigma_left= " << sigma_left
<< " is not a positive number" << std::endl;
return EXIT_FAILURE;
}
if (sigma_right <= 0)
{
std::cout << "#Error: sigma_right= " << sigma_right
<< " is not a positive number" << std::endl;
return EXIT_FAILURE;
}
if (peakMinHeight < 0)
{
std::cout << "#Error: peakMinHeight= " << peakMinHeight
<< " is not a nonegative number" << std::endl;
return EXIT_FAILURE;
}
if (lambda_1 < 0)
{
std::cout << "#Error: lambda_1= " << lambda_1
<< " is not a nonnegative number" << std::endl;
return EXIT_FAILURE;
}
if (lambda_2 < 0)
{
std::cout << "#Error: lambda_2= " << lambda_2
<< " is not a nonnegative number" << std::endl;
return EXIT_FAILURE;
}
if (mu <= 0)
{
std::cout << "#Error: mu= " << mu << " is not a positive number"
<< std::endl;
return EXIT_FAILURE;
}
if (eps < 0)
{
std::cout << "#Error: eps= " << eps << " is not a nonnegative number"
<< std::endl;
return EXIT_FAILURE;
}
if (max_iter <= 0)
{
std::cout << "#Error: max_iter= " << max_iter << " is not a positive number"
<< std::endl;
return EXIT_FAILURE;
}
// Generates output filename
//
const std::string output_filename((options.count("output") == 0)
? options["input"].as<std::string>() +
".out"
: options["output"].as<std::string>());
///////////////////
// Read CSV file //
///////////////////
std::vector<std::pair<double, double>> data;
try
{
io::CSVReader<2,
io::trim_chars<' ', '\t'>,
io::no_quote_escape<','>,
io::throw_on_overflow,
io::single_line_comment<'#'>>
in(options["input"].as<std::string>());
double X_i, Y_i;
while (in.read_row(X_i, Y_i))
{
data.push_back(std::make_pair(X_i, Y_i));
}
}
catch (std::exception& e)
{
std::cerr << "#Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
///////////////////////////////////////
// Call the deconvolution subroutine //
///////////////////////////////////////
const Size_t n = data.size();
if (n <= 2)
{
std::cerr << "#Error: spectrum must have at least 2 points" << std::endl;
return EXIT_FAILURE;
}
Vector x(n);
Vector y(n);
Vector y_smoothed(n);
Vector deconvolvedPeak(n);
Vector convolvedPeak(n);
Vector baseline(n);
for (Index_t i = 0; i < n; ++i)
{
x[i] = data[i].first;
y[i] = data[i].second;
}
data.clear();
SeqDeconv_InputParameters inputParameters;
inputParameters.lambda_1 = lambda_1;
inputParameters.lambda_2 = lambda_2;
inputParameters.solver_inputParameters.iter_max = max_iter;
inputParameters.solver_inputParameters.eps_goal = eps;
// Smooth
sgFilter(y, y_smoothed);
// SNIP
snip(y_smoothed, baseline, snip_halfWindowSize);
// Deconv
y -= baseline;
seqDeconv_GaussianPeaks(x,
y,
peakMinHeight,
sigma_left,
sigma_right,
deconvolvedPeak,
convolvedPeak,
inputParameters);
y += baseline;
//////////////////
// Write output //
//////////////////
std::ofstream output(output_filename);
output << "# Generated by: " << command_line << std::endl;
for (Index_t i = 0; i < n; ++i)
{
output << x[i] << ", " << y[i] << ", " << deconvolvedPeak[i] << ", "
<< baseline[i] << ", " << convolvedPeak[i] << ", "
<< baseline[i] + convolvedPeak[i] << std::endl;
}
output.close();
// Gnuplot
// load "test.csv.out.gnuplot"
if (gnuplot)
{
std::ofstream output_gnuplot(output_filename + ".gnuplot");
output_gnuplot << "set datafile separator ','" << std::endl;
output_gnuplot << "plot \"" << output_filename
<< "\" u 1 : 2 w l t \"Y raw\" lc \"blue\"" << std::endl;
output_gnuplot << "replot \"" << output_filename
<< "\" u 1 : 6 w l t \"reconstructed\" lw 2 lc \"grey30\""
<< std::endl;
output_gnuplot << "replot \"" << output_filename
<< "\" u 1 : 4 w l t \"baseline\" lw 2 lc \"grey70\""
<< std::endl;
output_gnuplot << "replot \"" << output_filename
<< "\" u 1 : 5 w l t \"convolved peaks\" lc \"green\""
<< std::endl;
output_gnuplot << "replot \"" << output_filename
<< "\" u 1 : 3 w i t \"peaks\" lw 2 lc \"red\"" << std::endl;
output_gnuplot << "set terminal eps" << std::endl;
output_gnuplot << "set output \"" << output_filename + ".eps"
<< "\"" << std::endl;
output_gnuplot << "replot" << std::endl;
output_gnuplot << "set terminal png" << std::endl;
output_gnuplot << "set output \"" << output_filename + ".png"
<< "\"" << std::endl;
output_gnuplot << "replot" << std::endl;
output_gnuplot << "set terminal qt" << std::endl;
output_gnuplot << "set output" << std::endl;
}
return EXIT_SUCCESS;
}