aboutsummaryrefslogtreecommitdiff
path: root/tests/integration_check.c
blob: ed11efd7ffd2e153ab993d6a94db42d9eb7fc1ef (plain)
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
/*
 * integration_check.c
 *
 * Check peak integration
 *
 * (c) 2011 Thomas White <taw@physics.org>
 * (c) 2011 Andrew Martin <andrew.martin@desy.de>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#include <stdlib.h>
#include <stdio.h>

#include "../src/image.h"
#include "../src/peaks.h"
#include "../src/utils.h"
#include "../src/beam-parameters.h"


/* The third integration check draws a Poisson background and checks that, on
 * average, it gets subtracted by the background subtraction. */
static void third_integration_check(struct image *image, int n_trials,
                                    int *fail)
{
	double mean_intensity = 0.0;
	double mean_bg = 0.0;
	double mean_max = 0.0;
	double mean_sigma = 0.0;
	int i;
	int fs, ss;

	for ( i=0; i<n_trials; i++ ) {

		double intensity, bg, max, sigma;
		double fsp, ssp;

		for ( fs=0; fs<image->width; fs++ ) {
		for ( ss=0; ss<image->height; ss++ ) {
			image->data[fs+image->width*ss] = poisson_noise(10.0);
		}
		}
		integrate_peak(image, 64, 64, &fsp, &ssp, &intensity,
			       &bg, &max, &sigma, 0, 1, 1);

		mean_intensity += intensity;
		mean_bg += bg;
		mean_max += max;
		mean_sigma += sigma;

	}
	mean_intensity /= n_trials;
	mean_bg /= n_trials;
	mean_max /= n_trials;
	mean_sigma /= n_trials;

	STATUS("  Third check (mean values): intensity = %.2f, bg = %.2f,"
	       " max = %.2f, sigma = %.2f\n",
	       mean_intensity, mean_bg, mean_max, mean_sigma);
	if ( fabs(mean_intensity) > 5.0 ) {
		ERROR("Mean intensity should be close to zero.\n");
		*fail = 1;
	}
	if ( fabs(mean_bg-10.0) > 0.3 ) {
		ERROR("Mean background should be close to ten.\n");
		*fail = 1;
	}
	if ( fabs(mean_intensity) > mean_sigma ) {
		ERROR("Mean intensity should be less than mean sigma.\n");
		*fail = 1;
	}
}


/* The fourth integration check draws a Poisson background and draws a peak on
 * top of it, then checks that the intensity of the peak is correctly recovered
 * accounting for the background. */
static void fourth_integration_check(struct image *image, int n_trials,
                                     int *fail)
{
	double mean_intensity = 0.0;
	double mean_bg = 0.0;
	double mean_max = 0.0;
	double mean_sigma = 0.0;
	int i;
	int fs, ss;
	int pcount = 0;

	for ( i=0; i<n_trials; i++ ) {

		double intensity, bg, max, sigma;
		double fsp, ssp;

		for ( fs=0; fs<image->width; fs++ ) {
		for ( ss=0; ss<image->height; ss++ ) {
			int idx = fs+image->width*ss;
			image->data[idx] = 0.0;
			if ( (fs-64)*(fs-64) + (ss-64)*(ss-64) > 9*9 ) {
				image->data[idx] = poisson_noise(10.0);
			} else {
				image->data[idx] += 1000.0;
				pcount++;
			}
		}
		}
		integrate_peak(image, 64, 64, &fsp, &ssp, &intensity,
			       &bg, &max, &sigma, 0, 1, 1);

		mean_intensity += intensity;
		mean_bg += bg;
		mean_max += max;
		mean_sigma += sigma;

	}
	mean_intensity /= n_trials;
	mean_bg /= n_trials;
	mean_max /= n_trials;
	mean_sigma /= n_trials;
	pcount /= n_trials;

	STATUS(" Fourth check (mean values): intensity = %.2f, bg = %.2f,"
	       " max = %.2f, sigma = %.2f\n",
	       mean_intensity, mean_bg, mean_max, mean_sigma);
	if ( fabs(mean_intensity - pcount*1000.0) > 4000.0 ) {
		ERROR("Mean intensity should be close to %f\n", pcount*1000.0);
		*fail = 1;
	}
	if ( fabs(mean_bg-10.0) > 0.3 ) {
		ERROR("Mean background should be close to ten.\n");
		*fail = 1;
	}
	if ( fabs(mean_intensity) < mean_sigma ) {
		ERROR("Mean intensity should be greater than mean sigma.\n");
		*fail = 1;
	}
}


/* The fifth check integrates a Poisson background with background subtraction
 * switched off, and checks that the result is what would be expected. */
static void fifth_integration_check(struct image *image, int n_trials,
                                    int *fail)
{
	double mean_intensity = 0.0;
	double mean_bg = 0.0;
	double mean_max = 0.0;
	double mean_sigma = 0.0;
	int i;
	int fs, ss;
	int pcount = 0;

	for ( i=0; i<n_trials; i++ ) {

		double intensity, bg, max, sigma;
		double fsp, ssp;

		for ( fs=0; fs<image->width; fs++ ) {
		for ( ss=0; ss<image->height; ss++ ) {
			int idx = fs+image->width*ss;
			image->data[idx] = poisson_noise(10.0);
			if ( (fs-64)*(fs-64) + (ss-64)*(ss-64) < 10*10 ) {
				pcount++;
			}
		}
		}
		integrate_peak(image, 64, 64, &fsp, &ssp, &intensity,
			       &bg, &max, &sigma, 0, 1, 0);

		mean_intensity += intensity;
		mean_bg += bg;
		mean_max += max;
		mean_sigma += sigma;

	}
	mean_intensity /= n_trials;
	mean_bg /= n_trials;
	mean_max /= n_trials;
	mean_sigma /= n_trials;
	pcount /= n_trials;

	STATUS("  Fifth check (mean values): intensity = %.2f, bg = %.2f,"
	       " max = %.2f, sigma = %.2f\n",
	       mean_intensity, mean_bg, mean_max, mean_sigma);
	double s = pcount*10.0;
	if ( fabs(mean_intensity - s) > 5.0 ) {
		ERROR("Mean intensity should be close to %f.\n", pcount*10.0);
		*fail = 1;
	}
	if ( fabs(mean_bg-10.0) > 0.3 ) {
		ERROR("Mean background should be close to ten.\n");
		*fail = 1;
	}
}


/* The sixth check is like the fourth check, except that the background
 * subtraction is switched off */
static void sixth_integration_check(struct image *image, int n_trials,
                                    int *fail)
{
	double mean_intensity = 0.0;
	double mean_bg = 0.0;
	double mean_max = 0.0;
	double mean_sigma = 0.0;
	int i;
	int fs, ss;
	int pcount = 0;
	int npcount = 0;

	for ( i=0; i<n_trials; i++ ) {

		double intensity, bg, max, sigma;
		double fsp, ssp;

		for ( fs=0; fs<image->width; fs++ ) {
		for ( ss=0; ss<image->height; ss++ ) {
			int idx = fs+image->width*ss;
			double r = (fs-64)*(fs-64) + (ss-64)*(ss-64);
			image->data[idx] = poisson_noise(10.0);
			if ( r < 9*9 ) {
				image->data[idx] += 1000.0;
				pcount++;
			} else if ( r < 10*10 ) {
				npcount++;
			}
		}
		}
		integrate_peak(image, 64, 64, &fsp, &ssp, &intensity,
			       &bg, &max, &sigma, 0, 1, 0);

		mean_intensity += intensity;
		mean_bg += bg;
		mean_max += max;
		mean_sigma += sigma;

	}
	mean_intensity /= n_trials;
	mean_bg /= n_trials;
	mean_max /= n_trials;
	mean_sigma /= n_trials;
	pcount /= n_trials;
	npcount /= n_trials;

	STATUS("  Sixth check (mean values): intensity = %.2f, bg = %.2f,"
	       " max = %.2f, sigma = %.2f\n",
	       mean_intensity, mean_bg, mean_max, mean_sigma);

	double s = pcount*1010.0 + npcount*10.0;
	if ( fabs(mean_intensity - s) > 4000.0 ) {
		ERROR("Mean intensity should be close to %f.\n", s);
		*fail = 1;
	}
	if ( fabs(mean_bg-10.0) > 0.3 ) {
		ERROR("Mean background should be close to ten.\n");
		*fail = 1;
	}

	STATUS("            (Absolute value of mean sigma not (yet) tested)\n");
}


int main(int argc, char *argv[])
{
	struct image image;
	double fsp, ssp, intensity, bg, max, sigma;
	int fs, ss;
	FILE *fh;
	unsigned int seed;
	int fail = 0;
	const int n_trials = 1000;

	fh = fopen("/dev/urandom", "r");
	fread(&seed, sizeof(seed), 1, fh);
	fclose(fh);
	srand(seed);

	image.data = malloc(128*128*sizeof(float));
	image.flags = NULL;

	image.det = calloc(1, sizeof(struct detector));
	image.det->n_panels = 1;
	image.det->panels = calloc(1, sizeof(struct panel));

	image.det->panels[0].min_fs = 0;
	image.det->panels[0].max_fs = 128;
	image.det->panels[0].min_ss = 0;
	image.det->panels[0].max_ss = 128;
	image.det->panels[0].fsx = 1.0;
	image.det->panels[0].fsy = 0.0;
	image.det->panels[0].ssx = 0.0;
	image.det->panels[0].ssy = 1.0;
	image.det->panels[0].xfs = 1.0;
	image.det->panels[0].yfs = 0.0;
	image.det->panels[0].xss = 0.0;
	image.det->panels[0].yss = 1.0;
	image.det->panels[0].cnx = -64.0;
	image.det->panels[0].cny = -64.0;
	image.det->panels[0].clen = 1.0;
	image.det->panels[0].res = 1.0;
	image.det->panels[0].integr_radius = 10.0;

	image.width = 128;
	image.height = 128;
	memset(image.data, 0, 128*128*sizeof(float));

	image.beam = calloc(1, sizeof(struct beam_params));
	image.beam->adu_per_photon = 100.0;

	/* First check: no intensity -> zero intensity and bg */
	integrate_peak(&image, 64, 64, &fsp, &ssp, &intensity,
	               &bg, &max, &sigma, 0, 1, 1);
	STATUS("  First check: intensity = %.2f, bg = %.2f, max = %.2f,"
	       " sigma = %.2f\n", intensity, bg, max, sigma);
	if ( intensity != 0.0 ) {
		ERROR("Intensity should be zero.\n");
		fail = 1;
	}
	if ( bg != 0.0 ) {
		ERROR("Background should be zero.\n");
		fail = 1;
	}

	/* Second check: uniform peak gives correct value and no bg */
	for ( fs=0; fs<image.width; fs++ ) {
	for ( ss=0; ss<image.height; ss++ ) {
		if ( (fs-64)*(fs-64) + (ss-64)*(ss-64) > 9*9 ) continue;
		image.data[fs+image.width*ss] = 1000.0;
	}
	}
	integrate_peak(&image, 64, 64, &fsp, &ssp, &intensity,
	               &bg, &max, &sigma, 0, 1, 1);
	STATUS(" Second check: intensity = %.2f, bg = %.2f, max = %.2f,"
	       " sigma = %.2f\n", intensity, bg, max, sigma);
	if ( fabs(intensity - M_PI*9.0*9.0*1000.0) > 4000.0 ) {
		ERROR("Intensity should be close to 1000*pi*integr_r^2\n");
		fail = 1;
	}
	if ( bg != 0.0 ) {
		ERROR("Background should be zero.\n");
		fail = 1;
	}
	if ( max != 1000.0 ) {
		ERROR("Max should be 1000.\n");
		fail = 1;
	}
	if ( sigma != 0.0 ) {
		ERROR("Sigma should be zero.\n");
		fail = 1;
	}

	/* Third check: Poisson background should get mostly subtracted */
	third_integration_check(&image, n_trials, &fail);

	/* Fourth check: peak on Poisson background */
	fourth_integration_check(&image, n_trials, &fail);

	/* Fifth check: Like third check but without background subtraction */
	fifth_integration_check(&image, n_trials, &fail);

	/* Sixth check: Like fourth check but without background subtraction */
	sixth_integration_check(&image, n_trials, &fail);

	free(image.beam);
	free(image.det->panels);
	free(image.det);
	free(image.data);

	if ( fail ) return 1;

	return 0;
}