partialator.c 12.3 KB
Newer Older
1
/*
2
 * partialator.c
3
 *
4
 * Scaling and post refinement for coherent nanocrystallography
5
 *
Thomas White's avatar
Thomas White committed
6
 * (c) 2006-2011 Thomas White <taw@physics.org>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


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

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
Thomas White's avatar
Thomas White committed
23
#include <assert.h>
Thomas White's avatar
Tidy up    
Thomas White committed
24
#include <pthread.h>
25
#include <gsl/gsl_errno.h>
26

Thomas White's avatar
Thomas White committed
27
#include "utils.h"
28
#include "hdf5-file.h"
Thomas White's avatar
Thomas White committed
29
#include "symmetry.h"
Thomas White's avatar
Thomas White committed
30
#include "stream.h"
31
#include "geometry.h"
32
#include "peaks.h"
Thomas White's avatar
Thomas White committed
33
#include "thread-pool.h"
Thomas White's avatar
Thomas White committed
34
#include "beam-parameters.h"
35
#include "post-refinement.h"
36
#include "hrs-scaling.h"
Thomas White's avatar
Thomas White committed
37
#include "reflist.h"
Thomas White's avatar
Thomas White committed
38
#include "reflist-utils.h"
Thomas White's avatar
Thomas White committed
39
#include "scaling-report.h"
40
41
42
43
44
45


static void show_help(const char *s)
{
	printf("Syntax: %s [options]\n\n", s);
	printf(
46
"Scaling and post refinement for coherent nanocrystallography.\n"
47
48
49
"\n"
"  -h, --help                 Display this help message.\n"
"\n"
Thomas White's avatar
Thomas White committed
50
51
"  -i, --input=<filename>     Specify the name of the input 'stream'.\n"
"                              (must be a file, not e.g. stdin)\n"
Thomas White's avatar
Thomas White committed
52
"  -o, --output=<filename>    Output filename.  Default: facetron.hkl.\n"
Thomas White's avatar
Thomas White committed
53
"  -g. --geometry=<file>      Get detector geometry from file.\n"
54
55
56
57
"  -b, --beam=<file>          Get beam parameters from file, which provides\n"
"                              initial values for parameters, and nominal\n"
"                              wavelengths if no per-shot value is found in \n"
"                              an HDF5 file.\n"
58
"  -y, --symmetry=<sym>       Merge according to symmetry <sym>.\n"
59
"  -n, --iterations=<n>       Run <n> cycles of scaling and post-refinement.\n"
60
61
62
"      --reference=<file>     Refine images against reflections in <file>,\n"
"                              instead of taking the mean of the intensity\n"
"                              estimates.\n"
63
"\n"
Thomas White's avatar
Thomas White committed
64
65
66
67
"  -j <n>                     Run <n> analyses in parallel.\n");
}


Thomas White's avatar
Thomas White committed
68
struct refine_args
Thomas White's avatar
Thomas White committed
69
{
70
	RefList *full;
Thomas White's avatar
Thomas White committed
71
72
73
74
	struct image *image;
};


Thomas White's avatar
Thomas White committed
75
struct queue_args
Thomas White's avatar
Thomas White committed
76
{
Thomas White's avatar
Thomas White committed
77
78
79
80
81
82
83
84
85
86
87
	int n;
	int n_done;
	int n_total_patterns;
	struct image *images;
	struct refine_args task_defaults;
};


static void refine_image(void *task, int id)
{
	struct refine_args *pargs = task;
88
	struct image *image = pargs->image;
Thomas White's avatar
Thomas White committed
89
	image->id = id;
90

91
	pr_refine(image, pargs->full);
92
93
94
}


Thomas White's avatar
Thomas White committed
95
static void *get_image(void *vqargs)
96
{
Thomas White's avatar
Thomas White committed
97
98
	struct refine_args *task;
	struct queue_args *qargs = vqargs;
Thomas White's avatar
Thomas White committed
99

Thomas White's avatar
Thomas White committed
100
101
	task = malloc(sizeof(struct refine_args));
	memcpy(task, &qargs->task_defaults, sizeof(struct refine_args));
102

Thomas White's avatar
Thomas White committed
103
	task->image = &qargs->images[qargs->n];
104

Thomas White's avatar
Thomas White committed
105
106
107
108
	qargs->n++;

	return task;
}
Thomas White's avatar
Thomas White committed
109

110

Thomas White's avatar
Thomas White committed
111
112
113
114
115
116
static void done_image(void *vqargs, void *task)
{
	struct queue_args *qargs = vqargs;

	qargs->n_done++;

Thomas White's avatar
Thomas White committed
117
	progress_bar(qargs->n_done, qargs->n_total_patterns, "Refining");
Thomas White's avatar
Thomas White committed
118
119
120
121
122
	free(task);
}


static void refine_all(struct image *images, int n_total_patterns,
123
                       struct detector *det,
Thomas White's avatar
Thomas White committed
124
                       RefList *full, int nthreads)
Thomas White's avatar
Thomas White committed
125
126
127
128
129
130
131
132
133
134
{
	struct refine_args task_defaults;
	struct queue_args qargs;

	task_defaults.full = full;
	task_defaults.image = NULL;

	qargs.task_defaults = task_defaults;
	qargs.n = 0;
	qargs.n_done = 0;
Thomas White's avatar
Thomas White committed
135
136
	qargs.n_total_patterns = n_total_patterns;
	qargs.images = images;
Thomas White's avatar
Thomas White committed
137

Thomas White's avatar
Thomas White committed
138
	/* Don't have threads which are doing nothing */
Thomas White's avatar
Whoops    
Thomas White committed
139
	if ( n_total_patterns < nthreads ) nthreads = n_total_patterns;
Thomas White's avatar
Thomas White committed
140

Thomas White's avatar
Thomas White committed
141
	run_threads(nthreads, refine_image, get_image, done_image,
Thomas White's avatar
Thomas White committed
142
	            &qargs, n_total_patterns, 0, 0, 0);
143
144
145
}


146
/* Decide which reflections can be scaled */
147
static int select_scalable_reflections(RefList *list, RefList *reference)
148
149
150
151
152
153
154
155
156
{
	Reflection *refl;
	RefListIterator *iter;
	int nobs = 0;

	for ( refl = first_refl(list, &iter);
	      refl != NULL;
	      refl = next_refl(refl, iter) ) {

157
		int sc = 1;
158
159
		double v;

160
161
162
		/* This means the reflection was not found on the last check */
		if ( get_redundancy(refl) == 0 ) sc = 0;

163
		if ( get_partiality(refl) < 0.1 ) sc = 0;
164
		v = fabs(get_intensity(refl));
165
		if ( v < 0.1 ) sc = 0;
166

167
168
169
		/* If we are scaling against a reference set, we additionally
		 * require that this reflection is in the reference list. */
		if ( reference != NULL ) {
170
171
			signed int h, k, l;
			get_indices(refl, &h, &k, &l);
172
			if ( find_refl(reference, h, k, l) == NULL ) sc = 0;
173
174
		}

175
176
177
		set_scalable(refl, sc);

		if ( sc ) nobs++;
178
179
180
181
182
183
	}

	return nobs;
}


184
static void select_reflections_for_refinement(struct image *images, int n,
185
                                              RefList *full, int have_reference)
186
187
188
189
190
191
192
{
	int i;

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

		Reflection *refl;
		RefListIterator *iter;
193
194
195
196
197
		int n_acc = 0;
		int n_nomatch = 0;
		int n_noscale = 0;
		int n_fewmatch = 0;
		int n_ref = 0;
198

199
200
		if ( images[i].pr_dud ) continue;

201
202
203
204
205
206
207
		for ( refl = first_refl(images[i].reflections, &iter);
		      refl != NULL;
		      refl = next_refl(refl, iter) )
		{
			signed int h, k, l;
			int sc;

208
209
210
211
212
213
			n_ref++;

			/* We require that the reflection itself is scalable
			 * (i.e. sensible partiality and intensity) and that
			 * the "full" estimate of this reflection is made from
			 * at least two parts. */
214
215
			get_indices(refl, &h, &k, &l);
			sc = get_scalable(refl);
216
217
218
			if ( !sc ) {

				n_noscale++;
219
				set_refinable(refl, 0);
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

			} else {

				Reflection *f = find_refl(full, h, k, l);

				if ( f != NULL ) {

					int r = get_redundancy(f);
					if ( (r >= 2) || have_reference ) {
						set_refinable(refl, 1);
						n_acc++;
					} else {
						n_fewmatch++;
					}

				} else {
					n_nomatch++;
237
					set_refinable(refl, 0);
238
				}
239
240
241
242

			}
		}

243
244
245
246
247
248
249
250
		STATUS("Image %4i: %i guide reflections accepted "
		       "(%i not scalable, %i few matches, %i total)\n",
		       i, n_acc, n_noscale, n_fewmatch, n_ref);

		/* This would be a silly situation, since there must be a match
		 * if THIS pattern has a scalable part of the reflection! */
		assert(n_nomatch == 0);

251
252
253
254
	}
}


Thomas White's avatar
Thomas White committed
255
256
257
258
int main(int argc, char *argv[])
{
	int c;
	char *infile = NULL;
Thomas White's avatar
Thomas White committed
259
	char *outfile = NULL;
Thomas White's avatar
Thomas White committed
260
	char *geomfile = NULL;
Thomas White's avatar
Thomas White committed
261
262
	char *sym = NULL;
	FILE *fh;
Thomas White's avatar
Thomas White committed
263
264
	int nthreads = 1;
	struct detector *det;
Thomas White's avatar
Thomas White committed
265
266
	int i;
	int n_total_patterns;
267
268
	struct image *images;
	int n_iter = 10;
Thomas White's avatar
Thomas White committed
269
	struct beam_params *beam = NULL;
270
	RefList *full;
271
272
273
	int n_found = 0;
	int n_expected = 0;
	int n_notfound = 0;
Thomas White's avatar
Thomas White committed
274
	int n_usable_patterns = 0;
275
	int nobs;
276
	char *reference_file = NULL;
277
	RefList *reference = NULL;
278
	int n_dud;
279
	int have_reference = 0;
Thomas White's avatar
Thomas White committed
280
281
	char cmdline[1024];
	SRContext *sr;
Thomas White's avatar
Thomas White committed
282
283
284
285
286

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"input",              1, NULL,               'i'},
Thomas White's avatar
Thomas White committed
287
		{"output",             1, NULL,               'o'},
Thomas White's avatar
Thomas White committed
288
		{"geometry",           1, NULL,               'g'},
Thomas White's avatar
Thomas White committed
289
		{"beam",               1, NULL,               'b'},
290
		{"symmetry",           1, NULL,               'y'},
291
		{"iterations",         1, NULL,               'n'},
292
		{"reference",          1, NULL,                1},
Thomas White's avatar
Thomas White committed
293
294
295
		{0, 0, NULL, 0}
	};

Thomas White's avatar
Thomas White committed
296
297
298
299
300
301
	cmdline[0] = '\0';
	for ( i=1; i<argc; i++ ) {
		strncat(cmdline, argv[i], 1023-strlen(cmdline));
		strncat(cmdline, " ", 1023-strlen(cmdline));
	}

Thomas White's avatar
Thomas White committed
302
	/* Short options */
Thomas White's avatar
Thomas White committed
303
	while ((c = getopt_long(argc, argv, "hi:g:x:j:y:o:b:",
304
305
	                        longopts, NULL)) != -1)
	{
Thomas White's avatar
Thomas White committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

		switch (c) {
		case 'h' :
			show_help(argv[0]);
			return 0;

		case 'i' :
			infile = strdup(optarg);
			break;

		case 'g' :
			geomfile = strdup(optarg);
			break;

		case 'j' :
			nthreads = atoi(optarg);
			break;

324
325
326
327
		case 'y' :
			sym = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
328
329
330
331
		case 'o' :
			outfile = strdup(optarg);
			break;

332
333
334
335
		case 'n' :
			n_iter = atoi(optarg);
			break;

Thomas White's avatar
Thomas White committed
336
337
338
339
340
341
342
343
344
		case 'b' :
			beam = get_beam_parameters(optarg);
			if ( beam == NULL ) {
				ERROR("Failed to load beam parameters"
				      " from '%s'\n", optarg);
				return 1;
			}
			break;

345
		case 1 :
346
			reference_file = strdup(optarg);
347
348
			break;

Thomas White's avatar
Thomas White committed
349
350
351
352
353
354
355
356
357
		case 0 :
			break;

		default :
			return 1;
		}

	}

Thomas White's avatar
Thomas White committed
358
	/* Sanitise input filename and open */
Thomas White's avatar
Thomas White committed
359
360
361
362
363
364
365
366
367
368
369
370
371
	if ( infile == NULL ) {
		infile = strdup("-");
	}
	if ( strcmp(infile, "-") == 0 ) {
		fh = stdin;
	} else {
		fh = fopen(infile, "r");
	}
	if ( fh == NULL ) {
		ERROR("Failed to open input file '%s'\n", infile);
		return 1;
	}

Thomas White's avatar
Thomas White committed
372
373
	/* Sanitise output filename */
	if ( outfile == NULL ) {
Thomas White's avatar
Whoops    
Thomas White committed
374
		outfile = strdup("partialator.hkl");
Thomas White's avatar
Thomas White committed
375
376
	}

Thomas White's avatar
Thomas White committed
377
378
	if ( sym == NULL ) sym = strdup("1");

Thomas White's avatar
Thomas White committed
379
	/* Get detector geometry */
Thomas White's avatar
Thomas White committed
380
381
382
383
384
385
386
	det = get_detector_geometry(geomfile);
	if ( det == NULL ) {
		ERROR("Failed to read detector geometry from '%s'\n", geomfile);
		return 1;
	}
	free(geomfile);

Thomas White's avatar
Thomas White committed
387
388
389
390
391
	if ( beam == NULL ) {
		ERROR("You must provide a beam parameters file.\n");
		return 1;
	}

392
	if ( reference_file != NULL ) {
Thomas White's avatar
Thomas White committed
393

394
		RefList *list;
395

396
		list = read_reflections(reference_file);
397
398
399
400
		if ( list == NULL ) {
			ERROR("Failed to read '%s'\n", reference_file);
			return 1;
		}
401
		free(reference_file);
402
		reference = asymmetric_indices(list, sym);
403
		reflist_free(list);
404
		have_reference = 1;
405

406
407
	}

Thomas White's avatar
Thomas White committed
408
	n_total_patterns = count_patterns(fh);
Thomas White's avatar
Thomas White committed
409
410
411
412
	if ( n_total_patterns == 0 ) {
		ERROR("No patterns to process.\n");
		return 1;
	}
Thomas White's avatar
Thomas White committed
413
414
	STATUS("There are %i patterns to process\n", n_total_patterns);

415
416
	gsl_set_error_handler_off();

417
418
419
420
421
422
	images = malloc(n_total_patterns * sizeof(struct image));
	if ( images == NULL ) {
		ERROR("Couldn't allocate memory for images.\n");
		return 1;
	}

Thomas White's avatar
Thomas White committed
423
424
	sr = sr_header("scaling-report.pdf", infile, cmdline);

425
426
	/* Fill in what we know about the images so far */
	rewind(fh);
427
	nobs = 0;
428
429
	for ( i=0; i<n_total_patterns; i++ ) {

430
		RefList *as;
Thomas White's avatar
Thomas White committed
431
		struct image *cur;
432
		int nn_expected, nn_found, nn_notfound;
433

Thomas White's avatar
Thomas White committed
434
		cur = &images[n_usable_patterns];
435

Thomas White's avatar
Thomas White committed
436
437
438
		cur->det = det;

		if ( read_chunk(fh, cur) != 0 ) {
Thomas White's avatar
Thomas White committed
439
440
			/* Should not happen, because we counted the patterns
			 * earlier. */
Thomas White's avatar
Thomas White committed
441
			ERROR("Failed to read chunk from the input stream.\n");
442
443
444
			return 1;
		}

445
		/* Won't be needing this, if it exists */
Thomas White's avatar
Thomas White committed
446
447
		image_feature_list_free(cur->features);
		cur->features = NULL;
448

Thomas White's avatar
Thomas White committed
449
		/* "n_usable_patterns" will not be incremented in this case */
Thomas White's avatar
Thomas White committed
450
		if ( cur->indexed_cell == NULL ) continue;
451

Thomas White's avatar
Thomas White committed
452
		/* Fill in initial estimates of stuff */
Thomas White's avatar
Thomas White committed
453
454
455
456
457
		cur->div = beam->divergence;
		cur->bw = beam->bandwidth;
		cur->width = det->max_fs;
		cur->height = det->max_ss;
		cur->osf = 1.0;
Thomas White's avatar
Thomas White committed
458
		cur->profile_radius = 0.003e9;
Thomas White's avatar
Thomas White committed
459
		cur->pr_dud = 0;
460

Thomas White's avatar
Thomas White committed
461
		/* Muppet proofing */
Thomas White's avatar
Thomas White committed
462
463
464
		cur->data = NULL;
		cur->flags = NULL;
		cur->beam = NULL;
465

Thomas White's avatar
Thomas White committed
466
		/* This is the raw list of reflections */
Thomas White's avatar
Thomas White committed
467
468
469
		as = asymmetric_indices(cur->reflections, sym);
		reflist_free(cur->reflections);
		cur->reflections = as;
470

471
472
473
474
		update_partialities(cur, &nn_expected, &nn_found, &nn_notfound);
		n_expected += nn_expected;
		n_found += nn_found;
		n_notfound += nn_notfound;
475

476
477
		nobs += select_scalable_reflections(cur->reflections,
		                                    reference);
478

479
		progress_bar(i, n_total_patterns-1, "Loading pattern data");
Thomas White's avatar
Thomas White committed
480
		n_usable_patterns++;
481
482
483

	}
	fclose(fh);
Thomas White's avatar
Thomas White committed
484
485
	STATUS("Found %5.2f%% of the expected peaks (missed %i of %i).\n",
	       100.0 * (double)n_found / n_expected, n_notfound, n_expected);
Thomas White's avatar
Thomas White committed
486

487
	/* Make initial estimates */
488
	STATUS("Performing initial scaling.\n");
489
	full = scale_intensities(images, n_usable_patterns, reference);
Thomas White's avatar
Thomas White committed
490

Thomas White's avatar
Thomas White committed
491
492
	sr_before(sr, images, n_usable_patterns, full);

Thomas White's avatar
Thomas White committed
493
	/* Iterate */
494
	for ( i=0; i<n_iter; i++ ) {
Thomas White's avatar
Thomas White committed
495

496
		int j;
497
		RefList *comp;
498

499
		STATUS("Post refinement cycle %i of %i\n", i+1, n_iter);
Thomas White's avatar
Thomas White committed
500

501
502
503
504
505
		if ( reference == NULL ) {
			comp = full;
		} else {
			comp = reference;
		}
506

Thomas White's avatar
Thomas White committed
507
		/* Refine the geometry of all patterns to get the best fit */
508
509
		select_reflections_for_refinement(images, n_usable_patterns,
		                                  comp, have_reference);
Thomas White's avatar
Thomas White committed
510
		refine_all(images, n_usable_patterns, det, comp, nthreads);
Thomas White's avatar
Thomas White committed
511

512
		nobs = 0;
513
514
515
516
		for ( j=0; j<n_usable_patterns; j++ ) {

			struct image *cur = &images[j];

517
			nobs += select_scalable_reflections(cur->reflections,
518
			                                    reference);
519
520
521

		}

Thomas White's avatar
Thomas White committed
522
		/* Re-estimate all the full intensities */
523
		reflist_free(full);
Thomas White's avatar
Thomas White committed
524
		full = scale_intensities(images, n_usable_patterns,
525
526
		                         reference);

Thomas White's avatar
Thomas White committed
527
528
	}

529
	n_dud = 0;
Thomas White's avatar
Thomas White committed
530
	for ( i=0; i<n_usable_patterns; i++ ) {
531
		if ( images[i].pr_dud ) n_dud++;
Thomas White's avatar
Thomas White committed
532
	}
533
	STATUS("%i images could not be refined on the last cycle.\n", n_dud);
Thomas White's avatar
Thomas White committed
534
535

	/* Output results */
536
	write_reflist(outfile, full, images[0].indexed_cell);
Thomas White's avatar
Thomas White committed
537

Thomas White's avatar
Thomas White committed
538
	sr_after(sr, images, n_usable_patterns, full);
Thomas White's avatar
Thomas White committed
539

Thomas White's avatar
Thomas White committed
540
	/* Clean up */
Thomas White's avatar
Thomas White committed
541
	for ( i=0; i<n_usable_patterns; i++ ) {
Thomas White's avatar
Thomas White committed
542
		reflist_free(images[i].reflections);
Thomas White's avatar
Thomas White committed
543
	}
544
	reflist_free(full);
Thomas White's avatar
Thomas White committed
545
	free(sym);
Thomas White's avatar
Thomas White committed
546
	free(outfile);
Thomas White's avatar
Thomas White committed
547
	free_detector_geometry(det);
548
	free(beam);
549
	if ( reference != NULL ) {
550
		reflist_free(reference);
551
	}
Thomas White's avatar
Thomas White committed
552
	for ( i=0; i<n_usable_patterns; i++ ) {
553
554
555
556
		cell_free(images[i].indexed_cell);
		free(images[i].filename);
	}
	free(images);
Thomas White's avatar
Thomas White committed
557
	free(infile);
558
559
560

	return 0;
}