aboutsummaryrefslogtreecommitdiff
path: root/src/statistics.c
blob: d78dde9e88cfd4e82ee09c0dc90a48b7247aff26 (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
/*
 * statistics.c
 *
 * Structure-factor statistics
 *
 * (c) 2006-2010 Thomas White <taw@physics.org>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


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

#include <math.h>
#include <stdlib.h>

#include "statistics.h"
#include "utils.h"


double stat_scale_intensity(const double *ref1, const unsigned int *c1,
                            const double *ref2, const unsigned int *c2)
{
	double top = 0.0;
	double bot = 0.0;
	int i;

	/* Start from i=1 -> skip central beam */
	for ( i=1; i<LIST_SIZE; i++ ) {

		if ( c1[i] && c2[i] ) {
			double i1, i2;
			i1 = ref1[i] / (double)c1[i];
			i2 = ref2[i] / (double)c2[i];
			top += i1 * i2;
			bot += i2 * i2;
		} /* else reflection not common so don't worry about it */

	}

	return top/bot;
}


double stat_r2(const double *ref1, const unsigned int *c1,
               const double *ref2, const unsigned int *c2, double *scalep)
{
	double top = 0.0;
	double bot = 0.0;
	double scale;
	int i;
	scale = stat_scale_intensity(ref1, c1, ref2, c2);
	*scalep = scale;

	/* Start from i=1 -> skip central beam */
	for ( i=1; i<LIST_SIZE; i++ ) {

		if ( c1[i] && c2[i] ) {

			double i1, i2;
			i1 = ref1[i] / (scale*(double)c1[i]);
			i2 = ref2[i] / (double)c2[i];

			top += pow(fabs(i1 - i2), 2.0);
			bot += pow(i1, 2.0);

		} /* else reflection not measured so don't worry about it */

	}

	return sqrt(top/bot);
}


double stat_scale_sqrti(const double *ref1, const unsigned int *c1,
                            const double *ref2, const unsigned int *c2)
{
	double top = 0.0;
	double bot = 0.0;
	int i;

	/* Start from i=1 -> skip central beam */
	for ( i=1; i<LIST_SIZE; i++ ) {

		if ( c1[i] && c2[i] ) {

			double f1, f2;

			if ( (ref1[i]<0.0) || (ref2[i]<0.0) ) continue;

			f1 = sqrt(ref1[i]) / (double)c1[i];
			f2 = sqrt(ref2[i]) / (double)c2[i];

			top += f1 * f2;
			bot += f2 * f2;

		} /* else reflection not common so don't worry about it */

	}

	return top/bot;
}


double stat_rmerge(const double *ref1, const unsigned int *c1,
                   const double *ref2, const unsigned int *c2, double *scalep)
{
	double top = 0.0;
	double bot = 0.0;
	double scale;
	int i;
	scale = stat_scale_sqrti(ref1, c1, ref2, c2);
	*scalep = scale;

	/* Start from i=1 -> skip central beam */
	for ( i=1; i<LIST_SIZE; i++ ) {

		if ( c1[i] && c2[i] ) {

			double f1, f2;

			if ( (ref1[i]<0.0) || (ref2[i]<0.0) ) continue;

			f1 = sqrt(ref1[i]) / (scale*(double)c1[i]);
			f2 = sqrt(ref2[i]) / (double)c2[i];

			top += fabs(f1 - f2);
			bot += f1 + f2;

		} /* else reflection not measured so don't worry about it */

	}

	return 2.0*top/bot;
}