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
|
/*
* process_image.c
*
* The processing pipeline for one image
*
* Copyright © 2012-2015 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
*
* Authors:
* 2010-2015 Thomas White <taw@physics.org>
* 2014 Valerio Mariani
*
* 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 <hdf5.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_statistics_double.h>
#include <gsl/gsl_sort.h>
#include <unistd.h>
#include "utils.h"
#include "hdf5-file.h"
#include "index.h"
#include "peaks.h"
#include "detector.h"
#include "filters.h"
#include "thread-pool.h"
#include "geometry.h"
#include "stream.h"
#include "reflist-utils.h"
#include "process_image.h"
#include "integration.h"
#include "predict-refine.h"
#include "im-sandbox.h"
static void try_refine_autoR(struct image *image, Crystal *cr)
{
double old_R, new_R;
char notes[1024];
refine_radius(cr, image);
old_R = crystal_get_profile_radius(cr);
if ( refine_prediction(image, cr) ) {
crystal_set_user_flag(cr, 1);
return;
}
/* Estimate radius again with better geometry */
refine_radius(cr, image);
new_R = crystal_get_profile_radius(cr);
snprintf(notes, 1024, "predict_refine/R old = %.5f new = %.5f nm^-1",
old_R/1e9, new_R/1e9);
crystal_add_notes(cr, notes);
}
static float **backup_image_data(float **dp, struct detector *det)
{
float **bu;
int i;
bu = malloc(det->n_panels * sizeof(float *));
if ( bu == NULL ) return NULL;
for ( i=0; i<det->n_panels; i++ ) {
size_t data_size;
data_size = det->panels[i].w * det->panels[i].h * sizeof(float);
bu[i] = malloc(data_size);
if ( bu[i] == NULL ) {
free(bu);
ERROR("Failed to allocate pre-filter backup.\n");
return NULL;
}
memcpy(bu[i], dp[i], data_size);
}
return bu;
}
static void restore_image_data(float **dp, struct detector *det, float **bu)
{
int i;
for ( i=0; i<det->n_panels; i++ ) {
size_t data_size;
data_size = det->panels[i].w * det->panels[i].h * sizeof(float);
memcpy(dp[i], bu[i], data_size);
free(bu[i]);
}
free(bu);
}
void process_image(const struct index_args *iargs, struct pattern_args *pargs,
Stream *st, int cookie, const char *tmpdir,
int serial, struct sb_shm *sb_shared)
{
int check;
struct hdfile *hdfile;
struct image image;
int i;
int r;
int ret;
char *rn;
int n_crystals_left;
float **prefilter;
int any_crystals;
image.features = NULL;
image.copyme = iargs->copyme;
image.id = cookie;
image.filename = pargs->filename_p_e->filename;
image.event = pargs->filename_p_e->ev;
image.beam = iargs->beam;
image.det = copy_geom(iargs->det);
image.crystals = NULL;
image.n_crystals = 0;
image.serial = serial;
image.indexed_by = INDEXING_NONE;
hdfile = hdfile_open(image.filename);
if ( hdfile == NULL ) {
ERROR("Couldn't open file: %s\n", image.filename);
return;
}
check = hdf5_read2(hdfile, &image, image.event, 0);
if ( check ) {
return;
}
/* Take snapshot of image before applying horrible noise filters */
prefilter = backup_image_data(image.dp, image.det);
if ( iargs->median_filter > 0 ) {
filter_median(&image, iargs->median_filter);
}
if ( iargs->noisefilter ) {
filter_noise(&image);
}
mark_resolution_range_as_bad(&image, iargs->highres, +INFINITY);
switch ( iargs->peaks ) {
case PEAK_HDF5:
if ( get_peaks(&image, hdfile, iargs->hdf5_peak_path) ) {
ERROR("Failed to get peaks from HDF5 file.\n");
}
if ( !iargs->no_revalidate ) {
validate_peaks(&image, iargs->min_snr,
iargs->pk_inn, iargs->pk_mid,
iargs->pk_out, iargs->use_saturated,
iargs->check_hdf5_snr);
}
break;
case PEAK_CXI:
if ( get_peaks_cxi(&image, hdfile, iargs->hdf5_peak_path,
pargs->filename_p_e) ) {
ERROR("Failed to get peaks from CXI file.\n");
}
if ( !iargs->no_revalidate ) {
validate_peaks(&image, iargs->min_snr,
iargs->pk_inn, iargs->pk_mid,
iargs->pk_out, iargs->use_saturated,
iargs->check_hdf5_snr);
}
break;
case PEAK_ZAEF:
search_peaks(&image, iargs->threshold,
iargs->min_gradient, iargs->min_snr,
iargs->pk_inn, iargs->pk_mid,iargs->pk_out,
iargs->use_saturated);
break;
}
restore_image_data(image.dp, image.det, prefilter);
rn = getcwd(NULL, 0);
r = chdir(tmpdir);
if ( r ) {
ERROR("Failed to chdir to temporary folder: %s\n",
strerror(errno));
hdfile_close(hdfile);
return;
}
/* Index the pattern */
index_pattern(&image, iargs->indm, iargs->ipriv);
r = chdir(rn);
if ( r ) {
ERROR("Failed to chdir: %s\n", strerror(errno));
hdfile_close(hdfile);
return;
}
free(rn);
for ( i=0; i<image.n_crystals; i++ ) {
crystal_set_image(image.crystals[i], &image);
crystal_set_user_flag(image.crystals[i], 0);
}
/* Set beam/crystal parameters */
if ( iargs->fix_divergence >= 0.0 ) {
image.div = iargs->fix_divergence;
} else {
image.div = 0.0;
}
if ( iargs->fix_bandwidth >= 0.0 ) {
image.bw = iargs->fix_bandwidth;
} else {
image.bw = 0.00000001;
}
if ( iargs->fix_profile_r >= 0.0 ) {
for ( i=0; i<image.n_crystals; i++ ) {
crystal_set_profile_radius(image.crystals[i],
iargs->fix_profile_r);
crystal_set_mosaicity(image.crystals[i], 0.0);
}
} else {
for ( i=0; i<image.n_crystals; i++ ) {
crystal_set_profile_radius(image.crystals[i], 0.02e9);
crystal_set_mosaicity(image.crystals[i], 0.0);
}
}
if ( iargs->fix_profile_r < 0.0 ) {
for ( i=0; i<image.n_crystals; i++ ) {
if ( iargs->predict_refine ) {
try_refine_autoR(&image, image.crystals[i]);
} else {
refine_radius(image.crystals[i], &image);
}
}
} else {
for ( i=0; i<image.n_crystals; i++ ) {
if ( iargs->predict_refine ) {
refine_prediction(&image, image.crystals[i]);
}
}
}
/* If there are no crystals left, set the indexing flag back to zero */
n_crystals_left = 0;
for ( i=0; i<image.n_crystals; i++ ) {
if ( crystal_get_user_flag(image.crystals[i]) == 0 ) {
n_crystals_left++;
}
}
if ( n_crystals_left == 0 ) {
image.indexed_by = INDEXING_NONE;
}
/* Integrate! */
integrate_all_4(&image, iargs->int_meth, PMODEL_SCSPHERE,
iargs->push_res,
iargs->ir_inn, iargs->ir_mid, iargs->ir_out,
iargs->int_diag, iargs->int_diag_h,
iargs->int_diag_k, iargs->int_diag_l,
&sb_shared->term_lock);
ret = write_chunk(st, &image, hdfile,
iargs->stream_peaks, iargs->stream_refls,
pargs->filename_p_e->ev);
if ( ret != 0 ) {
ERROR("Error writing stream file.\n");
}
int n = 0;
for ( i=0; i<image.n_crystals; i++ ) {
n += crystal_get_num_implausible_reflections(image.crystals[i]);
}
if ( n > 0 ) {
STATUS("WARNING: %i implausibly negative reflection%s in %s "
"%s\n", n, n>1?"s":"", image.filename,
get_event_string(image.event));
}
/* Count crystals which are still good */
pthread_mutex_lock(&sb_shared->totals_lock);
any_crystals = 0;
for ( i=0; i<image.n_crystals; i++ ) {
if ( crystal_get_user_flag(image.crystals[i]) == 0 ) {
sb_shared->n_crystals++;
any_crystals = 1;
}
}
sb_shared->n_processed++;
sb_shared->n_hadcrystals += any_crystals;
pthread_mutex_unlock(&sb_shared->totals_lock);
for ( i=0; i<image.n_crystals; i++ ) {
cell_free(crystal_get_cell(image.crystals[i]));
reflist_free(crystal_get_reflections(image.crystals[i]));
crystal_free(image.crystals[i]);
}
free(image.crystals);
for ( i=0; i<image.det->n_panels; i++ ) {
free(image.dp[i]);
free(image.bad[i]);
}
free(image.dp);
free(image.bad);
image_feature_list_free(image.features);
free_detector_geometry(image.det);
hdfile_close(hdfile);
}
|