| Bsoft 2.0.5 Bernard's software package

mg_random.cpp
Go to the documentation of this file.
1
9 #include "mg_random.h"
10 #include "linked_list.h"
11 #include "random_numbers.h"
12 #include "utilities.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17
28 int project_random_defocus(Bproject* project, double std)
29 {
30 if ( !project ) return 0;
31
32 Bfield* field;
33 Bmicrograph* mg;
34 long n(0);
35 double rnum, rdev(0);
36
37 if ( verbose & VERB_PROCESS ) {
38 cout << "Introducing random errors into defocus average:" << endl;
39 cout << "Target standard deviation: " << std << " A" << endl;
40 }
41
42 random_seed();
43
44 for ( field = project->field; field; field = field->next )
45 for ( mg = field->mg; mg; mg = mg->next ) {
46 if ( !mg->ctf ) mg->ctf = new CTFparam;
47 do {
48 rnum = random_gaussian(0, std);
49 } while ( 2*fabs(rnum) > mg->ctf->defocus_average() );
50 mg->ctf->defocus_average(mg->ctf->defocus_average() + rnum);
51 n++;
52 rdev += rnum*rnum;
53 }
54
55 rdev = sqrt(rdev/n);
56
57 if ( verbose & VERB_PROCESS )
58 cout << "Standard deviation: " << rdev << " A" << endl;
59
60 return 0;
61 }
62
73 int project_random_origins(Bproject* project, double std)
74 {
75 if ( !project ) return 0;
76
77 Bfield* field;
78 Bmicrograph* mg;
79 Breconstruction* rec;
80 Bparticle* part;
81 long n(0);
82 double ori_dev(0);
83 Vector3<float> rvec;
84
85 if ( verbose & VERB_PROCESS ) {
86 cout << "Introducing random errors into origins:" << endl;
87 cout << "Target standard deviation: " << std << " pixels" << endl;
88 }
89
90 random_seed();
91
92 if ( project->select ) {
93 for ( rec = project->rec; rec; rec = rec->next ) {
94 for ( part = rec->part; part; part = part->next ) {
95 rvec = vector3_xy_random_gaussian(0.0, std);
96 part->ori += rvec;
97 n++;
98 ori_dev += rvec.length2();
99 }
100 }
101 } else {
102 for ( field = project->field; field; field = field->next ) {
103 for ( mg = field->mg; mg; mg = mg->next ) {
104 for ( part = mg->part; part; part = part->next ) {
105 rvec = vector3_xy_random_gaussian(0.0, std);
106 part->ori += rvec;
107 n++;
108 ori_dev += rvec.length2();
109 }
110 }
111 }
112 }
113
114 ori_dev = sqrt(ori_dev/n);
115
116 if ( verbose & VERB_PROCESS )
117 cout << "Standard deviation: " << ori_dev << " pixels" << endl << endl;
118
119 return 0;
120 }
121
130 int project_random_views(Bproject* project)
131 {
132 if ( !project ) return 0;
133
134 Bfield* field;
135 Bmicrograph* mg;
136 Breconstruction* rec;
137 Bparticle* part;
138
139 random_seed(); // Seed random generator = process ID
140
141 if ( verbose & VERB_PROCESS )
142 cout << "Replacing particle views with random views" << endl << endl;
143
144 if ( project->select ) {
145 for ( rec = project->rec; rec; rec = rec->next ) {
146 for ( part = rec->part; part; part = part->next ) {
147 if ( verbose & VERB_FULL )
148 cout << "Original view: " << part->view << endl;
149 part->view = random_view();
150 if ( verbose & VERB_FULL )
151 cout << "Random view: " << part->view << endl;
152 }
153 }
154 } else {
155 for ( field = project->field; field; field = field->next ) {
156 for ( mg = field->mg; mg; mg = mg->next ) {
157 for ( part = mg->part; part; part = part->next ) {
158 if ( verbose & VERB_FULL )
159 cout << "Original view: " << part->view << endl;
160 part->view = random_view();
161 if ( verbose & VERB_FULL )
162 cout << "Random view: " << part->view << endl;
163 }
164 }
165 }
166 }
167
168 return 0;
169 }
170
179 int project_random_helical_views(Bproject* project)
180 {
181 if ( !project ) return 0;
182
183 Bfield* field;
184 Bmicrograph* mg;
185 Breconstruction* rec;
186 Bparticle* part;
187 Euler euler;
188 double irm = TWOPI/get_rand_max();
189
190 random_seed(); // Seed random generator = process ID
191
192 if ( verbose & VERB_PROCESS )
193 cout << "Replacing particle views with random helical views" << endl << endl;
194
195 if ( project->select ) {
196 for ( rec = project->rec; rec; rec = rec->next ) {
197 for ( part = rec->part; part; part = part->next ) {
198 if ( verbose & VERB_FULL )
199 cout << "Original view: " << part->view << endl;
200 euler = Euler(part->view);
201 euler[2] = random()*irm;
202 part->view = euler.view();
203 if ( verbose & VERB_FULL )
204 cout << "Random view: " << part->view << endl;
205 }
206 }
207 } else {
208 for ( field = project->field; field; field = field->next ) {
209 for ( mg = field->mg; mg; mg = mg->next ) {
210 for ( part = mg->part; part; part = part->next ) {
211 if ( verbose & VERB_FULL )
212 cout << "Original view: " << part->view << endl;
213 euler = Euler(part->view);
214 euler[1] = M_PI_2;
215 euler[2] = random()*irm;
216 part->view = euler.view();
217 if ( verbose & VERB_FULL )
218 cout << "Random view: " << part->view << endl;
219 }
220 }
221 }
222 }
223
224 return 0;
225 }
226
227 double particle_random_view(Bparticle* part, double std)
228 {
229 Vector3<double> vec = part->view.vector3();
230 Vector3<double> axis = vector3_random(-1.0, 1.0);
231
232 axis.normalize();
233 axis = axis.cross(vec);
234 axis.normalize();
235
236 double angle = random_gaussian(0.0, std);
237 Matrix3 mat = Matrix3(axis, angle);
238
239 axis = mat * vec;
240 part->view = View(axis[0], axis[1], axis[2], part->view.angle());
241
242 return axis.angle(vec);
243 }
244
255 int project_random_views(Bproject* project, double std)
256 {
257 if ( !project ) return 0;
258
259 Bfield* field;
260 Bmicrograph* mg;
261 Breconstruction* rec;
262 Bparticle* part;
263 long n(0);
264 double angle, ang_dev(0);
265
266 if ( verbose & VERB_PROCESS ) {
267 cout << "Introducing random errors into views:" << endl;
268 cout << "Target standard deviation: " << std*180.0/M_PI << " degrees" << endl;
269 }
270
271 random_seed();
272
273 if ( project->select ) {
274 for ( rec = project->rec; rec; rec = rec->next ) {
275 for ( part = rec->part; part; part = part->next ) {
276 // angle = particle_random_view(part, std);
277 angle = random_view_error(part->view, std);
278 n++;
279 ang_dev += angle*angle;
280 }
281 }
282 } else {
283 for ( field = project->field; field; field = field->next ) {
284 for ( mg = field->mg; mg; mg = mg->next ) {
285 for ( part = mg->part; part; part = part->next ) {
286 // angle = particle_random_view(part, std);
287 angle = random_view_error(part->view, std);
288 n++;
289 ang_dev += angle*angle;
290 }
291 }
292 }
293 }
294
295 ang_dev = sqrt(ang_dev/n);
296
297 if ( verbose & VERB_PROCESS )
298 cout << "Standard deviation: " << ang_dev*180.0/M_PI << " degrees" << endl << endl;
299
300 return 0;
301 }
302
313 int project_random_magnification(Bproject* project, double std)
314 {
315 if ( !project ) return 0;
316
317 Bfield* field;
318 Bmicrograph* mg;
319 Bparticle* part;
320 long n(0);
321 double rnum, rdev(0);
322
323 if ( std > 0.5 ) std = 0.5;
324
325 if ( verbose & VERB_PROCESS ) {
326 cout << "Introducing random errors into defocus average:" << endl;
327 cout << "Target standard deviation: " << std << " A" << endl;
328 }
329
330 random_seed();
331
332 for ( field = project->field; field; field = field->next )
333 for ( mg = field->mg; mg; mg = mg->next )
334 for ( part = mg->part; part; part = part->next ) {
335 do {
336 rnum = random_gaussian(0, std);
337 } while ( fabs(rnum) > 0.5 );
338 part->mag += rnum;
339 n++;
340 rdev += rnum*rnum;
341 }
342
343 rdev = sqrt(rdev/n);
344
345 if ( verbose & VERB_PROCESS )
346 cout << "Standard deviation: " << rdev << " A" << endl << endl;
347
348 return 0;
349 }
350
362 int project_random_symmetry_related_views(Bproject* project, Bsymmetry* sym)
363 {
364 if ( !project ) return 0;
365
366 Bfield* field;
367 Bmicrograph* mg;
368 Breconstruction* rec;
369 Bparticle* part;
370 View* v;
371 double r; // Stores a random value in range [0,1) or [0,n)
372 int y; // Stores a random integer in range [0,n-1]
373 int i;
374 double irm = 1.0L/(get_rand_max() + 1.0L);
375 int nviews = 1;
376 for ( i=0; i<sym->n; i++ ) nviews *= sym->op[i].order; // Number of symmetry-related views
377
378 random_seed(); // Seed random generator = process ID
379
380 if ( verbose & VERB_PROCESS ) {
381 cout << "Substituting original orientation by randomly assigning one of the symmetry-related views:" << endl;
382 cout << "Symmetry: " << sym->label << endl;
383 }
384
385 if ( project->select ) {
386 for ( rec = project->rec; rec; rec = rec->next ) {
387 for ( part = rec->part; part; part = part->next ) {
388 if ( verbose & VERB_FULL )
389 cout << "Original view vector: " << part->view << endl;
390
391 v = symmetry_get_all_views(sym, part->view); // Get symmetry-related views
392
393 r = nviews*irm*random();
394 y = (int) r; // y is a random integer in the range [0,n-1]
395
396 for ( i=0; i<y ; i++, v=v->next) ;
397 part->view = *v;
398 part->view.next = NULL;
399
400 kill_list((char *) v, sizeof(View));
401
402 if ( verbose & VERB_FULL )
403 cout << "View chosen #" << i << ": " << part->view << endl;
404 }
405 }
406 } else {
407 for ( field = project->field; field; field = field->next ) {
408 for ( mg = field->mg; mg; mg = mg->next ) {
409 for ( part = mg->part; part; part = part->next ) {
410 if ( verbose & VERB_FULL )
411 cout << "Original view vector: " << part->view << endl;
412
413 v = symmetry_get_all_views(sym, part->view); // Get symmetry-related views
414
415 r = nviews*irm*random();
416 y = (int) r; // y is a random integer in the range [0,n-1]
417
418 for ( i=0; i<y ; i++, v=v->next) ;
419 part->view = *v;
420 part->view.next = NULL;
421
422 kill_list((char *) v, sizeof(View));
423
424 if ( verbose & VERB_FULL )
425 cout << "View chosen #" << i << ": " << part->view << endl;
426 }
427 }
428 }
429 }
430
431 return 0;
432 }
433
M_PI_2
#define M_PI_2
Definition: utilities.h:62
project_random_magnification
int project_random_magnification(Bproject *project, double std)
Introduces random errors into particle magnifications.
Definition: mg_random.cpp:313
Vector3::cross
Vector3 cross(const Vector3 &v)
Definition: Vector3.h:250
vector3_random
Vector3< double > vector3_random(const double min, const double max)
Generates a random vector within a defined cube.
Definition: random_numbers.cpp:440
project_random_origins
int project_random_origins(Bproject *project, double std)
Introduces random errors into particle origins.
Definition: mg_random.cpp:73
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
Bparticle::mag
double mag
Definition: mg_processing.h:94
Vector3::normalize
double normalize()
Definition: Vector3.h:236
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
Euler
Definition: Euler.h:28
vector3_xy_random_gaussian
Vector3< double > vector3_xy_random_gaussian(double avg, double std)
Generates a random vector within a random gaussian-distributed length in the xy plane.
Definition: random_numbers.cpp:522
Bparticle::view
View view
Definition: mg_processing.h:101
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:304
View::next
View * next
Definition: View.h:28
Bsymmetry::n
int n
Definition: symmetry.h:42
utilities.h
Header file for general utilities.
mg_random.h
Header file for applying some randomization to parameters.
Breconstruction
Definition: mg_processing.h:280
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Matrix3
Definition: Matrix3.h:17
project
Bproject * project
Definition: bshow.cpp:31
particle_random_view
double particle_random_view(Bparticle *part, double std)
Definition: mg_random.cpp:227
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bfield::next
Bfield * next
Definition: mg_processing.h:258
project_random_defocus
int project_random_defocus(Bproject *project, double std)
Introduces random errors into micrograph defocus averages.
Definition: mg_random.cpp:28
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:239
random_view
View random_view()
Calculates a random view.
Definition: View.cpp:101
Bfield
Definition: mg_processing.h:257
M_PI
#define M_PI
Definition: utilities.h:59
linked_list.h
Header file for generalized linked list functions.
random_gaussian
double random_gaussian(double avg, double std)
Generates a value with a gaussian random distribution.
Definition: random_numbers.cpp:123
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:238
Bproject::field
Bfield * field
Definition: mg_processing.h:343
Vector3::angle
double angle(Vector3< T2 > &v)
Definition: Vector3.h:257
verbose
int verbose
project_random_helical_views
int project_random_helical_views(Bproject *project)
Replace the original particle orientations by random side views for a helix.
Definition: mg_random.cpp:179
Bmicrograph
Definition: mg_processing.h:200
Bsymop::order
int order
Definition: symmetry.h:26
irm
double irm(1.0L/get_rand_max())
View::vector3
Vector3< double > vector3()
Definition: View.h:126
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:100
Vector3::length2
double length2()
Definition: Vector3.h:221
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Bsymmetry
Definition: symmetry.h:38
Bparticle
Definition: mg_processing.h:89
random_numbers.h
Header file for functions for creating random images.
Bproject::select
int select
Definition: mg_processing.h:339
project_random_views
int project_random_views(Bproject *project)
Replace the original particle orientations by random views.
Definition: mg_random.cpp:130
Vector3< float >
View::angle
double angle()
Definition: View.h:95
View
Definition: View.h:26
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:100
Euler::view
View view()
Definition: Euler.h:62
Bsymmetry::op
Bsymop * op
Definition: symmetry.h:43
project_random_symmetry_related_views
int project_random_symmetry_related_views(Bproject *project, Bsymmetry *sym)
Replaces the original orientations by symmetry-related views.
Definition: mg_random.cpp:362
random_view_error
double random_view_error(View &v, double std)
Calculates a new view with a random error from the given view.
Definition: View.cpp:180
Bparticle::next
Bparticle * next
Definition: mg_processing.h:90
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:281
TWOPI
#define TWOPI
Definition: utilities.h:68
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:448
Bproject
Definition: mg_processing.h:333
CTFparam
Definition: ctf.h:26
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201