-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pouzat-ENP-2016-C.html
587 lines (502 loc) · 19.8 KB
/
Pouzat-ENP-2016-C.html
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-05-17 mar. 17:42 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The analysis in C</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Christophe Pouzat" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">The analysis in C</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgheadline2">1. Introduction</a>
<ul>
<li><a href="#orgheadline1">1.1. Getting the data</a></li>
</ul>
</li>
<li><a href="#orgheadline5">2. Getting the <code>mean</code> and <code>variance</code> of each pixel for each exposure time</a>
<ul>
<li><a href="#orgheadline3">2.1. A remark on the code presentation</a></li>
<li><a href="#orgheadline4">2.2. The code</a></li>
</ul>
</li>
<li><a href="#orgheadline6">3. Weighted least-square fit of the Variance versus the Mean</a></li>
</ul>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2"><span class="section-number-2">1</span> Introduction</h2>
<div class="outline-text-2" id="text-1">
<p>
The idea here is to perform the analysis using a mixture of <code>C</code> code and basic <code>POSIX</code> functions like the <code>graph</code> of the <a href="https://www.gnu.org/software/plotutils/">plotutils</a> package.
</p>
</div>
<div id="outline-container-orgheadline1" class="outline-3">
<h3 id="orgheadline1"><span class="section-number-3">1.1</span> Getting the data</h3>
<div class="outline-text-3" id="text-1-1">
<p>
The data we are going to use as illustrations were recorded by Andreas Pippow <a href="http://cecad.uni-koeln.de/Prof-Peter-Kloppenburg.82.0.html">Kloppenburg Lab, University of Cologne</a> and are freely available in <a href="http://www.hdfgroup.org/HDF5/">HDF5</a> format at the following URLs:
</p>
<ul class="org-ul">
<li><a href="http://xtof.disque.math.cnrs.fr/data/CCD_calibration.hdf5">http://xtof.disque.math.cnrs.fr/data/CCD_calibration.hdf5</a> for the calibration data.</li>
<li><a href="http://xtof.disque.math.cnrs.fr/data/Data_POMC.hdf5">http://xtof.disque.math.cnrs.fr/data/Data_POMC.hdf5</a> for the POMC data.</li>
</ul>
<p>
We download them to our local hard-drive with:
</p>
<div class="org-src-container">
<pre class="src src-shell" id="orgsrcblock1">wge http://xtof.disque.math.cnrs.fr/data/CCD_calibration.hdf5
wget http://xtof.disque.math.cnrs.fr/data/Data_POMC.hdf5
</pre>
</div>
<p>
The data sets can then be quickly inspected and (partly) plotted with <a href="https://www.hdfgroup.org/products/java/hdfview/">HDFView</a>, a <code>JAVA</code> utility. They can also be explored using few command line functions that come with the <code>hdf5 library</code>:
</p>
<div class="org-src-container">
<pre class="src src-shell" id="orgsrcblock2">h5ls CCD_calibration.hdf5
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">100ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">10ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">20ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">30ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">40ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">50ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">60ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">70ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">80ms</td>
<td class="org-left">Group</td>
</tr>
<tr>
<td class="org-left">90ms</td>
<td class="org-left">Group</td>
</tr>
</tbody>
</table>
<p>
We see that the <code>CCD_calibration.hdf5</code> file contains 10 groups corresponding to each of the 10 exposure times (from 10 to 100 ms). Let us look more closely at the "10ms" group:
</p>
<div class="org-src-container">
<pre class="src src-shell" id="orgsrcblock3">h5ls CCD_calibration.hdf5/10ms
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">stack</td>
<td class="org-left">Dataset</td>
<td class="org-left">{60,</td>
<td class="org-left">80,</td>
<td class="org-left">100}</td>
</tr>
<tr>
<td class="org-left">time</td>
<td class="org-left">Dataset</td>
<td class="org-left">{100}</td>
<td class="org-left"> </td>
<td class="org-left"> </td>
</tr>
</tbody>
</table>
<p>
This group like all the others contains two data sets, <code>stack</code> (the stack of 100 images taken by the 60 x 80 CCD chip) and <code>time</code> (the vector containing the onset time of each exposure).
</p>
</div>
</div>
</div>
<div id="outline-container-orgheadline5" class="outline-2">
<h2 id="orgheadline5"><span class="section-number-2">2</span> Getting the <code>mean</code> and <code>variance</code> of each pixel for each exposure time</h2>
<div class="outline-text-2" id="text-2">
</div><div id="outline-container-orgheadline3" class="outline-3">
<h3 id="orgheadline3"><span class="section-number-3">2.1</span> A remark on the code presentation</h3>
<div class="outline-text-3" id="text-2-1">
<p>
The <a href="https://en.wikipedia.org/wiki/Literate_programming">literate programming</a> approach is used here. This means that the code is broken into "manageable" pieces that are individually explained, they are then pasted together to give the code that will actually get compiled. These manageable pieces are called blocs and each bloc gets a name like: <code><<name-of-the-bloc>></code> upon definition. It is then referred to by this name when used in subsequent codes. See Schulte, Davison, Dye and Dominik (2010) <a href="https://www.jstatsoft.org/article/view/v046i03">A Multi-Language Computing Environment for Literate Programming and Reproducible Research </a>for further explanations.
</p>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-3">
<h3 id="orgheadline4"><span class="section-number-3">2.2</span> The code</h3>
<div class="outline-text-3" id="text-2-2">
<p>
We use the functions of the <a href="http://www.gnu.org/software/gsl/">GSL</a> (<i>Gnu Scientific Library</i>) for all the "non trivial" computations. The <code>C</code> code doing the job will output its results to <code>stdout</code> (the standard output). This code will have to work on individual stacks of images. For each exposure time, this image stack must be read from the data file. We write next the few lines of codes allocating the necessary memory and populating it with the image stack data. The following pre-defined variables appear in this code (<code><<read-image-stack>></code>):
</p>
<ul class="org-ul">
<li><code>file_id</code> (hid_t): the stream to the <code>HDF5</code> data file.</li>
<li><code>dsetnames</code> (vector of strings): the names of the data sets to analyze.</li>
<li><code>dset_idx</code> (int): the index of the data set.</li>
</ul>
<div class="org-src-container">
<pre class="src src-C" id="orgsrcblock4">/* read dataset */
int rank;
H5LTget_dataset_ndims(file_id,dsetnames[dset_idx],&rank);
hsize_t dims[rank];
H5LTget_dataset_info(file_id,dsetnames[dset_idx],dims,NULL,NULL);
int n_elt = 1;
for (int i=0; i < rank; i++) n_elt *= dims[i];
int data[n_elt];
H5LTread_dataset_int(file_id,dsetnames[dset_idx],data);
</pre>
</div>
<p>
After opening the <code>HDF5</code> file containing the data set, our code must extract the stack collected at each exposure duration and for each of the 60 x 80 pixels of the chip the <code>mean</code> and <code>variance</code> computed over the successive exposures are obtained. So we start by defining a function that takes:
</p>
<ul class="org-ul">
<li><code>data</code> (int *): a pointer to one stack (containing integers).</li>
<li><code>row_idx</code> (size_t): the row index of the pixel.</li>
<li><code>dim_1</code> (size_t): the number of columns of the stack of images.</li>
<li><code>col_idx</code> (size_t): the column index of the pixel.</li>
<li><code>dim_2</code> (size_t): the number of images of the stack of images.</li>
<li><code>mean</code> (double *): a pointer to the computed mean.</li>
<li><code>var</code> (double *): a pointer to the computed variance.</li>
</ul>
<p>
The function returns 0 is everything goes fine. The definition of bloc <code><<mean_and_var>></code> follows:
</p>
<div class="org-src-container">
<pre class="src src-C" id="orgsrcblock5">int mean_and_var(int data[], size_t row_idx, size_t dim_1, size_t col_idx, size_t dim_2,
double *mean, double *var ) {
double data_dbl[dim_2];
for (size_t k=0; k < dim_2; k++) data_dbl[k] = (double) data[dim_1*dim_2*row_idx+dim_2*col_idx+k];
*mean = gsl_stats_mean(data_dbl,1,dim_2);
*var = gsl_stats_variance_m(data_dbl,1,dim_2,*mean);
return 0;
}
</pre>
</div>
<p>
The header of our file is here (<code><<mean_var_CCD_calibration-header>></code>):
</p>
<div class="org-src-container">
<pre class="src src-C" id="orgsrcblock6">#include <string.h>
#include <stdio.h>
#include <gsl/gsl_statistics.h>
#include <hdf5.h>
#include <hdf5_hl.h>
</pre>
</div>
<p>
And the <code>main</code> function definition becomes:
</p>
<div class="org-src-container">
<pre class="src src-C" id="orgsrcblock7"><<mean_var_CCD_calibration-header>>
#define FILE "CCD_calibration.hdf5"
char *dsetnames[]={"/10ms/stack","/20ms/stack","/30ms/stack","/40ms/stack","/50ms/stack",
"/60ms/stack","/70ms/stack","/80ms/stack","/90ms/stack","/100ms/stack"};
<<mean_and_var>>
int main( void )
{
/* open file from ex_lite1.c */
hid_t file_id = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
for (int dset_idx=0; dset_idx < 10; dset_idx++) {
<<read-image-stack>>
for (size_t i=0; i < dims[0]; i++) {
for (size_t j=0; j < dims[1]; j++) {
double mean,var;
mean_and_var(data, i, dims[1], j, dims[2], &mean, &var);
/*printf("%g \t %g \n",mean,var);*/
fwrite(&mean,sizeof(double),1,stdout);
fwrite(&var,sizeof(double),1,stdout);
}
}
}
/* close file */
H5Fclose (file_id);
return 0;
}
</pre>
</div>
<p>
The code is saved in a file named <code>mean_var_CCD_calibration.c</code> and is compiled with (assuming that the <code>LD_LIBRARY_PATH</code> variable is properly set):
</p>
<div class="org-src-container">
<pre class="src src-shell" id="orgsrcblock8">gcc -W -g -o mean_var_CCD_calibration mean_var_CCD_calibration.c -lhdf5 -lhdf5_hl -lgsl -lgslcblas -lm
</pre>
</div>
<p>
It is then used in combination with the <a href="http://www.gnu.org/software/plotutils/manual/en/html_node/graph.html#graph">graph</a> function to give:
</p>
<div class="org-src-container">
<pre class="src src-shell" id="orgsrcblock9">./mean_var_CCD_calibration | graph -T X -m 0 -I d -L "Variance vs Mean (calibration data set)" -X "Mean (ADU)" -Y "Variance"
</pre>
</div>
<div class="figure">
<p><img src="VvsM.png" alt="VvsM.png" />
</p>
</div>
</div>
</div>
</div>
<div id="outline-container-orgheadline6" class="outline-2">
<h2 id="orgheadline6"><span class="section-number-2">3</span> Weighted least-square fit of the Variance versus the Mean</h2>
<div class="outline-text-2" id="text-3">
<p>
The <code>C</code> code doing the job is defined in blocks as before. The header (<code><<wlstsq_CCD_calibration-header>></code>) is slightly different since we need <code>gsl_fit</code>:
</p>
<div class="org-src-container">
<pre class="src src-C" id="orgsrcblock10">#include <string.h>
#include <stdio.h>
#include <gsl/gsl_statistics.h>
#include <gsl/gsl_fit.h>
#include <hdf5.h>
#include <hdf5_hl.h>
</pre>
</div>
<p>
Then the <code>main</code> function containing file looks like the previous one. Function <code>mean_and_var</code> is used again. Two vectors <code>mean</code> and <code>var</code> containing the mean and variance at each pixel for each exposure time are affected first. Then a vector <code>w</code> containing the <i>weights</i> (inverse of the variance) is affected and a weighted least square fit is performed with function <a href="http://www.gnu.org/software/gsl/manual/html_node/Linear-regression-with-a-constant-term.html#Linear-regression-with-a-constant-term">gsl_fit_wlinear</a>.
</p>
<div class="org-src-container">
<pre class="src src-C" id="orgsrcblock11"><<wlstsq_CCD_calibration-header>>
#define FILE "CCD_calibration.hdf5"
#define RANK 3
#define N0 60
#define N1 80
#define N2 100
char *dsetnames[]={"/10ms/stack","/20ms/stack","/30ms/stack","/40ms/stack","/50ms/stack",
"/60ms/stack","/70ms/stack","/80ms/stack","/90ms/stack","/100ms/stack"};
<<mean_and_var>>
int main( void )
{
hid_t file_id = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
int rank = RANK;
hsize_t dims[RANK] = {N0,N1,N2};
int n_elt = N0*N1*N2;
int data[n_elt];
double mean[N0*N1*10];
double var[N0*N1*10];
size_t r_idx=0;
for (int dset_idx=0; dset_idx < 10; dset_idx++) {
/* read dataset */
H5LTread_dataset_int(file_id,dsetnames[dset_idx],data);
for (size_t i=0; i < N0; i++) {
for (size_t j=0; j < N1; j++) {
mean_and_var(data, i, N1, j, N2, &mean[r_idx], &var[r_idx]);
r_idx++;
}
}
}
double w[N0*N1*10];
for (int i=0; i < N0*N1*10; i++) w[i] = 1/var[i];
double intercept,slope,sigma_intercept,sigma_cross,sigma_slope,chisq;
gsl_fit_wlinear (mean, 1, w, 1, var, 1, N0*N1*10,
&intercept, &slope, &sigma_intercept, &sigma_cross, &sigma_slope,
&chisq);
printf ("# best fit: Var = %g + %g * Mean\n", intercept, slope);
printf ("# covariance matrix:\n");
printf ("# [ %g, %g\n# %g, %g]\n",
sigma_intercept, sigma_cross, sigma_cross, sigma_slope);
printf ("# chisq = %g\n", chisq);
printf ("# degrees of freedom = %d\n\n", N0*N1*10-3);
printf ("# gain: %g \n# read-out-variance: %g\n",slope,intercept/slope/slope);
/* close file */
H5Fclose (file_id);
return 0;
}
</pre>
</div>
<p>
The code is saved in a file named <code>wlstsq_CCD_calibration.c</code> and is compiled with:
</p>
<div class="org-src-container">
<pre class="src src-shell" id="orgsrcblock12">gcc -W -g -o wlstsq_CCD_calibration wlstsq_CCD_calibration.c -lhdf5 -lhdf5_hl -lgsl -lgslcblas -lm
</pre>
</div>
<p>
It is simply used with:
</p>
<div class="org-src-container">
<pre class="src src-shell" id="orgsrcblock13">./wlstsq_CCD_calibration
</pre>
</div>
<pre class="example">
# best fit: Var = 5.76432 + 0.142805 * Mean
# covariance matrix:
# [ 0.0111791, -5.38295e-06
# -5.38295e-06, 4.06825e-09]
# chisq = 301438
# degrees of freedom = 47997
# gain: 0.142805
# read-out-variance: 282.658
</pre>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="date">Date: <span class="timestamp-wrapper"><span class="timestamp"><2016-05-16 lun.></span></span></p>
<p class="author">Author: Christophe Pouzat</p>
<p class="date">Created: 2016-05-17 mar. 17:42</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>