aboutsummaryrefslogtreecommitdiff
path: root/src/crystfelmergeopts.c
blob: eb5f0665add98fbe17ad32cc14682f8ce106a245 (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
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
/*
 * crystfelmergeopts.h
 *
 * A GTK widget to set merge options
 *
 * Copyright © 2020 Deutsches Elektronen-Synchrotron DESY,
 *                  a research centre of the Helmholtz Association.
 *
 * Authors:
 *  2020 Thomas White <taw@physics.org>
 *
 * This file is part of CrystFEL.
 *
 * CrystFEL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CrystFEL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrystFEL.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include <errno.h>
#include <math.h>

#include <integration.h>
#include <index.h>

#include "crystfelmergeopts.h"
#include "crystfelsymmetryselector.h"
#include "gtk-util-routines.h"


G_DEFINE_TYPE(CrystFELMergeOpts,
              crystfel_merge_opts,
              GTK_TYPE_NOTEBOOK)


static void crystfel_merge_opts_class_init(CrystFELMergeOptsClass *klass)
{
}


static void crystfel_merge_opts_init(CrystFELMergeOpts *mo)
{
}


static int maybe_disable(GtkWidget *toggle, GtkWidget *widget)
{
	gtk_widget_set_sensitive(GTK_WIDGET(widget),
	                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)));
	return FALSE;
}


static void disable_if_not(GtkWidget *toggle, GtkWidget *widget)
{
	maybe_disable(toggle, widget);
	g_signal_connect(G_OBJECT(toggle),
	                 "toggled",
	                 G_CALLBACK(maybe_disable),
	                 widget);
}


static GtkWidget *merge_parameters(CrystFELMergeOpts *mo)
{
	GtkWidget *box;
	GtkWidget *hbox;
	GtkWidget *label;

	box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
	gtk_container_set_border_width(GTK_CONTAINER(box), 8);

	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	label = gtk_label_new("Model:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 0);
	mo->model_combo = gtk_combo_box_text_new();
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->model_combo),
	                   FALSE, FALSE, 0);
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->model_combo),
	                          "process_hkl",
	                          "Simple merging (process_hkl)");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->model_combo),
	                          "unity",
	                          "No partialities (unity)");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->model_combo),
	                          "xsphere",
	                          "Bandwidth integral (xsphere)");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->model_combo),
	                          "offset",
	                          "Monochromatic Ewald sphere offset (offset)");

	/* Symmetry */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	label = gtk_label_new("Symmetry:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 0);
	mo->symmetry = crystfel_symmetry_selector_new();
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->symmetry),
	                   FALSE, FALSE, 0);

	/* Scale, Bscale, post-ref on/off */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	mo->scale = gtk_check_button_new_with_label("Scale intensities");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->scale),
	                   FALSE, FALSE, 0);
	mo->bscale = gtk_check_button_new_with_label("Debye-Waller scaling");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->bscale),
	                   FALSE, FALSE, 0);
	mo->postref = gtk_check_button_new_with_label("Post-refinement");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->postref),
	                   FALSE, FALSE, 0);
	disable_if_not(mo->scale, mo->bscale);

	/* Number of iterations */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	label = gtk_label_new("Number of scaling/post-refinement cycles:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 0);
	mo->niter = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(mo->niter), 4);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->niter),
	                   FALSE, FALSE, 0);

	/* Polarisation horiz/vert/unpolarized beam/no correction */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	label = gtk_label_new("Polarisation:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 0);
	mo->polarisation = gtk_combo_box_text_new();
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->polarisation), "horiz",
	                          "Horizontal e-field (most synchrotrons and FELs)");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->polarisation), "vert",
	                          "Vertical e-field (LCLS-II hard X-ray undulator)");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->polarisation), "horiz50",
	                          "Unpolarized incident beam");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mo->polarisation), "none",
	                          "No correction");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->polarisation),
	                   FALSE, FALSE, 0);

	/* deltaCChalf */
	mo->deltacchalf = gtk_check_button_new_with_label("Reject bad patterns according to ΔCC½");
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(mo->deltacchalf),
	                   FALSE, FALSE, 0);

	/* Detector saturation value */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	mo->use_max_adu = gtk_check_button_new_with_label("Detector saturation cutoff:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->use_max_adu),
	                   FALSE, FALSE, 0);
	mo->max_adu = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(mo->max_adu), 4);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->max_adu),
	                   FALSE, FALSE, 0);
	disable_if_not(mo->use_max_adu, mo->max_adu);

	/* Minimum measurements */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	label = gtk_label_new("Minimum number of measurements per merged reflection:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 0);
	mo->min_measurements = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(mo->min_measurements), 4);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->min_measurements),
	                   FALSE, FALSE, 0);

	/* Custom split */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	mo->custom_split = gtk_check_button_new_with_label("Split datasets after merging");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->custom_split),
	                   FALSE, FALSE, 0);
	mo->custom_split_file = gtk_file_chooser_button_new("Dataset assignments",
	                                                    GTK_FILE_CHOOSER_ACTION_OPEN);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->custom_split_file),
	                   FALSE, FALSE, 0);
	disable_if_not(mo->custom_split, mo->custom_split_file);

	/* Minimum pattern resolution */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	mo->min_res = gtk_check_button_new_with_label("Require minimum estimated pattern resolution");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->min_res),
	                   FALSE, FALSE, 0);
	mo->min_res_val = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(mo->min_res_val), 4);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->min_res_val),
	                   FALSE, FALSE, 0);
	label = gtk_label_new("Å");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 0);
	disable_if_not(mo->min_res, mo->min_res_val);

	/* push-res */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	mo->limit_res = gtk_check_button_new_with_label("Exclude measurements more than");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->limit_res),
	                   FALSE, FALSE, 0);
	mo->push_res = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(mo->push_res), 4);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->push_res),
	                   FALSE, FALSE, 0);
	label = gtk_label_new("nm^-1 above resolution limit");
	gtk_label_set_markup(GTK_LABEL(label), "nm<sup>-1</sup> above resolution limit");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 0);
	disable_if_not(mo->limit_res, mo->push_res);

	/* Detwin */
	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 0);
	mo->detwin = gtk_check_button_new_with_label("Refine indexing assignments");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->detwin),
	                   FALSE, FALSE, 0);
	mo->detwin_sym = crystfel_symmetry_selector_new();
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(mo->detwin_sym),
	                   FALSE, FALSE, 0);
	disable_if_not(mo->detwin, mo->detwin_sym);

	return box;
}


GtkWidget *crystfel_merge_opts_new()
{
	CrystFELMergeOpts *mo;

	mo = g_object_new(CRYSTFEL_TYPE_MERGE_OPTS, NULL);

	gtk_notebook_append_page(GTK_NOTEBOOK(mo),
	                         merge_parameters(mo),
	                         gtk_label_new("Merging"));

	return GTK_WIDGET(mo);
}


static int set_if(GtkWidget *combo, const char *a, const char *new_id)
{
	if ( strcmp(a, new_id) == 0 ) {
		gtk_combo_box_set_active_id(GTK_COMBO_BOX(combo),
		                            new_id);
		return 1;
	}

	return 0;
}


static void set_int(GtkWidget *entry, int val)
{
	char tmp[64];
	snprintf(tmp, 63, "%i", val);
	gtk_entry_set_text(GTK_ENTRY(entry), tmp);
}


static void set_float(GtkWidget *entry, float val)
{
	char tmp[64];
	snprintf(tmp, 63, "%f", val);
	gtk_entry_set_text(GTK_ENTRY(entry), tmp);
}


void crystfel_merge_opts_set_model(CrystFELMergeOpts *opts,
                                   const char *model)
{
	int done = 0;
	done += set_if(opts->model_combo, model, "process_hkl");
	done += set_if(opts->model_combo, model, "unity");
	done += set_if(opts->model_combo, model, "xsphere");
	done += set_if(opts->model_combo, model, "offset");
	done += set_if(opts->model_combo, model, "ggpm");

	if ( done == 0 ) {
		ERROR("Unrecognised model '%s'\n", model);
	}
}


void crystfel_merge_opts_set_polarisation(CrystFELMergeOpts *opts,
                                          const char *polar)
{
	int done = 0;
	done += set_if(opts->polarisation, polar, "horiz");
	done += set_if(opts->polarisation, polar, "vert");
	done += set_if(opts->polarisation, polar, "none");
	done += set_if(opts->polarisation, polar, "horiz50");

	if ( done == 0 ) {
		ERROR("Unrecognised polarisation '%s'\n", polar);
	}
}


void crystfel_merge_opts_set_symmetry(CrystFELMergeOpts *opts,
                                      const char *sym)
{
	crystfel_symmetry_selector_set_group_symbol(CRYSTFEL_SYMMETRY_SELECTOR(opts->symmetry),
	                                            sym);
}


void crystfel_merge_opts_set_scale(CrystFELMergeOpts *opts,
                                   int scale)
{
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opts->scale),
	                             scale);
}


void crystfel_merge_opts_set_bscale(CrystFELMergeOpts *opts,
                                    int bscale)
{
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opts->bscale),
	                             bscale);
}


void crystfel_merge_opts_set_postref(CrystFELMergeOpts *opts,
                                     int postref)
{
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opts->postref),
	                             postref);
}


void crystfel_merge_opts_set_niter(CrystFELMergeOpts *opts,
                                   int niter)
{
	set_int(opts->niter, niter);
}


void crystfel_merge_opts_set_deltacchalf(CrystFELMergeOpts *opts,
                                         int deltacchalf)
{
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opts->deltacchalf),
	                             deltacchalf);
}


void crystfel_merge_opts_set_min_measurements(CrystFELMergeOpts *opts,
                                              int min_measurements)
{
	set_int(opts->min_measurements, min_measurements);
}


void crystfel_merge_opts_set_max_adu(CrystFELMergeOpts *opts,
                                     float max_adu)
{
	set_float(opts->max_adu, max_adu);
}


void crystfel_merge_opts_set_custom_split(CrystFELMergeOpts *opts,
                                          const char *custom_split_file)
{
	if ( custom_split_file != NULL ) {
		gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(opts->custom_split_file),
		                              custom_split_file);
	}
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opts->custom_split),
	                             (custom_split_file != NULL));
}


void crystfel_merge_opts_set_twin_sym(CrystFELMergeOpts *opts,
                                      const char *twin_sym)
{
	crystfel_symmetry_selector_set_group_symbol(CRYSTFEL_SYMMETRY_SELECTOR(opts->detwin_sym),
	                                            twin_sym);
}


void crystfel_merge_opts_set_min_res(CrystFELMergeOpts *opts,
                                     float min_res)
{
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(opts->min_res),
	                             !isinf(min_res));
	set_float(opts->min_res_val, min_res);
}


void crystfel_merge_opts_set_push_res(CrystFELMergeOpts *mo,
                                      float push_res)
{
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mo->limit_res),
	                             !isinf(push_res));
	set_float(mo->push_res, push_res);
}


const char *crystfel_merge_opts_get_model(CrystFELMergeOpts *opts)
{
	return gtk_combo_box_get_active_id(GTK_COMBO_BOX(opts->model_combo));
}


const char *crystfel_merge_opts_get_symmetry(CrystFELMergeOpts *opts)
{
	return crystfel_symmetry_selector_get_group_symbol(CRYSTFEL_SYMMETRY_SELECTOR(opts->symmetry));
}


int crystfel_merge_opts_get_scale(CrystFELMergeOpts *opts)
{
	return get_bool(opts->scale);
}


int crystfel_merge_opts_get_bscale(CrystFELMergeOpts *opts)
{
	return get_bool(opts->bscale);
}


int crystfel_merge_opts_get_postref(CrystFELMergeOpts *opts)
{
	return get_bool(opts->postref);
}


int crystfel_merge_opts_get_niter(CrystFELMergeOpts *opts)
{
	return get_uint(opts->niter);
}


const char *crystfel_merge_opts_get_polarisation(CrystFELMergeOpts *opts)
{
	return gtk_combo_box_get_active_id(GTK_COMBO_BOX(opts->polarisation));
}


int crystfel_merge_opts_get_deltacchalf(CrystFELMergeOpts *opts)
{
	return get_bool(opts->deltacchalf);
}


int crystfel_merge_opts_get_min_measurements(CrystFELMergeOpts *opts)
{
	return get_uint(opts->min_measurements);
}


float crystfel_merge_opts_get_max_adu(CrystFELMergeOpts *opts)
{
	if ( get_bool(opts->use_max_adu) == 0 ) {
		return INFINITY;
	} else {
		return get_float(opts->max_adu);
	}
}


const char *crystfel_merge_opts_get_custom_split(CrystFELMergeOpts *opts)
{
	if ( get_bool(opts->custom_split) == 0 ) {
		return NULL;
	} else {
		return gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(opts->custom_split_file));
	}
}


const char *crystfel_merge_opts_get_twin_sym(CrystFELMergeOpts *opts)
{
	if ( get_bool(opts->detwin) == 0 ) {
		return NULL;
	} else {
		return crystfel_symmetry_selector_get_group_symbol(CRYSTFEL_SYMMETRY_SELECTOR(opts->detwin_sym));
	}
}


float crystfel_merge_opts_get_min_res(CrystFELMergeOpts *opts)
{
	if ( get_bool(opts->min_res) == 0 ) {
		return 0.0;
	} else {
		float min_res = get_float(opts->min_res_val);
		if ( isnan(min_res) ) {
			printf("Invalid value for min-res\n");
			return 0.0;
		} else {
			return min_res;
		}
	}
}


float crystfel_merge_opts_get_push_res(CrystFELMergeOpts *opts)
{
	if ( get_bool(opts->limit_res) == 0 ) {
		return INFINITY;
	} else {
		float push_res = get_float(opts->push_res);
		if ( isnan(push_res) ) {
			printf("Invalid value for push-res\n");
			return INFINITY;
		} else {
			return push_res;
		}
	}
}