aboutsummaryrefslogtreecommitdiff
path: root/src/hrs-scaling.c
blob: 1d91fb3a89a2aef4222f67ebd42100edf3281306 (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
/*
 * hrs-scaling.c
 *
 * Intensity scaling using generalised HRS target function
 *
 * (c) 2006-2011 Thomas White <taw@physics.org>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */

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


#include <stdlib.h>
#include <assert.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_blas.h>

#include "image.h"
#include "peaks.h"
#include "symmetry.h"
#include "geometry.h"
#include "cell.h"
#include "utils.h"
#include "reflist.h"


/* Maximum number of iterations of scaling per macrocycle. */
#define MAX_CYCLES (50)

/* ESD of restraint driving scale factors to unity */
#define SCALING_RESTRAINT (1.0)


static double iterate_scale(struct image *images, int n, RefList *reference)
{
	double max_shift = 0.0;
	int frame;

	assert(reference != NULL);

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

		struct image *image = &images[frame];
		Reflection *refl;
		RefListIterator *iter;
		double num = 0.0;
		double den = 0.0;
		double new_sf;
		double corr = 0.0;

		if ( image->pr_dud ) continue;

		for ( refl = first_refl(image->reflections, &iter);
		      refl != NULL;
		      refl = next_refl(refl, iter) )
		{
			signed int h, k, l;
			double Ih, Ihl;
			Reflection *r;

			if ( !get_scalable(refl) ) continue;

			/* Look up by asymmetric indices */
			get_indices(refl, &h, &k, &l);
			r = find_refl(reference, h, k, l);
			if ( r == NULL ) {
				ERROR("%3i %3i %3i isn't in the "
				      "reference list, so why is it "
				      "marked as scalable?\n", h, k, l);
				Ih = 0.0;
			} else {
				if ( get_redundancy(r) < 2 ) continue;
				Ih = get_intensity(r);
			}

			Ihl = get_intensity(refl) / get_partiality(refl);

			num += Ih * Ihl;
			den += Ih * Ih;

		}

		new_sf = num / den;
		if ( !isnan(new_sf) && !isinf(new_sf) ) {
			corr = fabs(image->osf - new_sf);
			image->osf = new_sf;
		}
		if ( corr > max_shift ) max_shift = corr;

	}

	return max_shift;
}


static RefList *estimate_full_intensities(struct image *images, int n)
{
	RefList *full;
	int frame;
	Reflection *refl;
	RefListIterator *iter;

	full = reflist_new();

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

		double G;

		if ( images[frame].pr_dud ) continue;
		G = images[frame].osf;

		for ( refl = first_refl(images[frame].reflections, &iter);
		      refl != NULL;
		      refl = next_refl(refl, iter) )
		{
			Reflection *f;
			signed int h, k, l;
			double total;
			int red;
			double Ihj;

			if ( !get_scalable(refl) ) continue;

			get_indices(refl, &h, &k, &l);
			f = find_refl(full, h, k, l);
			if ( f == NULL ) {
				f = add_refl(full, h, k, l);
				total = 0.0;
				red = 0;
			} else {
				total = get_temp1(f);
				red = get_redundancy(f);
			}

			Ihj = get_intensity(refl) / get_partiality(refl);
			total += Ihj / G;
			red++;

			set_temp1(f, total);
			set_redundancy(f, red);
		}

	}

	for ( refl = first_refl(full, &iter);
	      refl != NULL;
	      refl = next_refl(refl, iter) )
	{
		double Ih = get_temp1(refl) / (double)get_redundancy(refl);
		set_int(refl, Ih);

	}

	return full;
}


static UNUSED void show_scale_factors(struct image *images, int n)
{
	int i;
	for ( i=0; i<n; i++ ) {
		STATUS("Image %4i: scale factor %5.2f\n", i, images[i].osf);
	}
}


static UNUSED double total_dev(struct image *image, const RefList *full)
{
	double dev = 0.0;

	/* For each reflection */
	Reflection *refl;
	RefListIterator *iter;

	for ( refl = first_refl(image->reflections, &iter);
	      refl != NULL;
	      refl = next_refl(refl, iter) ) {

		double G, p;
		signed int h, k, l;
		Reflection *full_version;
		double I_full, I_partial;

		if ( !get_scalable(refl) ) continue;

		get_indices(refl, &h, &k, &l);
		assert((h!=0) || (k!=0) || (l!=0));

		full_version = find_refl(full, h, k, l);
		if ( full_version == NULL ) continue;
		/* Some reflections may have recently become scalable, but
		 * scale_intensities() might not yet have been called, so the
		 * full version may not have been calculated yet. */

		G = image->osf;
		p = get_partiality(refl);
		I_partial = get_intensity(refl);
		I_full = get_intensity(full_version);
		//STATUS("%3i %3i %3i  %5.2f  %5.2f  %5.2f  %5.2f  %5.2f\n",
		//       h, k, l, G, p, I_partial, I_full,
		//       I_partial - p*G*I_full);

		dev += pow(I_partial - p*G*I_full, 2.0);

	}

	return dev;
}


static UNUSED void plot_graph(struct image *image, RefList *reference)
{
	double sc;

	for ( sc=0.0; sc<3.0; sc+=0.1 ) {

		image->osf = sc;
		STATUS("%5.2f: %e\n", sc, total_dev(image, reference));

	}
}


/* Scale the stack of images */
RefList *scale_intensities(struct image *images, int n, RefList *gref)
{
	int i;
	double max_corr;
	RefList *full = NULL;

	for ( i=0; i<n; i++ ) {
		images[i].osf = 1.0;
	}

	/* No reference -> create an initial list to refine against */
	if ( gref == NULL ) {
		full = estimate_full_intensities(images, n);
	}

	/* Iterate */
	i = 0;
	do {

		RefList *reference;

		/* Refine against reference or current "full" estimates */
		if ( gref != NULL ) {
			reference = gref;
		} else {
			reference = full;
		}

		max_corr = iterate_scale(images, n, reference);
		STATUS("Scaling iteration %2i: max correction = %5.2f\n",
		       i+1, max_corr);

		/* No reference -> generate list for next iteration */
		if ( gref == NULL ) {
			reflist_free(full);
			full = estimate_full_intensities(images, n);
		}

		//show_scale_factors(images, n);

		i++;

	} while ( (max_corr > 0.01) && (i < MAX_CYCLES) );

	if ( gref != NULL ) {
		full = estimate_full_intensities(images, n);
	} /* else we already did it */

	return full;
}