EMAN2
averager.cpp
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#include "averager.h"
33#include "emdata.h"
34#include "xydata.h"
35#include "ctf.h"
36#include <cstring>
38
39using namespace EMAN;
40
41const string ImageAverager::NAME = "mean";
42const string SigmaAverager::NAME = "sigma";
43const string TomoAverager::NAME = "mean.tomo";
44const string MinMaxAverager::NAME = "minmax";
45const string MedianAverager::NAME = "median";
46const string IterAverager::NAME = "iterative";
47const string CtfCWautoAverager::NAME = "ctfw.auto";
48const string CtfCAutoAverager::NAME = "ctf.auto";
49const string CtfWtAverager::NAME = "ctf.weight";
50const string CtfWtFiltAverager::NAME = "ctf.weight.autofilt";
51const string FourierWeightAverager::NAME = "weightedfourier";
52const string LocalWeightAverager::NAME = "localweight";
53
55{
56 force_add<ImageAverager>();
57 force_add<SigmaAverager>();
58 force_add<MinMaxAverager>();
59 force_add<MedianAverager>();
60 force_add<LocalWeightAverager>();
61 force_add<IterAverager>();
62 force_add<CtfCWautoAverager>();
63 force_add<CtfCAutoAverager>();
64 force_add<CtfWtAverager>();
65 force_add<CtfWtFiltAverager>();
66 force_add<TomoAverager>();
67 force_add<FourierWeightAverager>();
68// force_add<XYZAverager>();
69}
70
71void Averager::mult(const float& s)
72{
73 if ( result != 0 )
74 {
75 result->mult(s);
76 }
77 else throw NullPointerException("Error, attempted to multiply the result image, which is NULL");
78}
79
80// pure virtual causing boost issues here, so this is just a dummy method
82{
83return;
84}
85
86void Averager::add_image_list(const vector<EMData*> & image_list)
87{
88 for (size_t i = 0; i < image_list.size(); i++) {
89 add_image(image_list[i]);
90 }
91}
92
94 : norm_image(0),nimg(0),overlap(0)
95{
96
97}
98
100{
101 if (!image) {
102 return;
103 }
104
105 if (!image->is_complex()) {
106 image=image->do_fft();
107 image->set_attr("free_me",(int)1);
108 }
109
110 if (result!=0 && !EMUtil::is_same_size(image, result)) {
111 LOGERR("%s Averager can only process same-size Images",
112 get_name().c_str());
113 return;
114 }
115
116 int nx = image->get_xsize();
117 int ny = image->get_ysize();
118 int nz = image->get_zsize();
119// size_t image_size = (size_t)nx * ny * nz;
120
121 if (norm_image == 0) {
122// printf("init average %d %d %d",nx,ny,nz);
123 result = image->copy_head();
124 result->to_zero();
125
126 norm_image = image->copy_head();
127 norm_image->to_zero();
128
129 thresh_sigma = (float)params.set_default("thresh_sigma", 0.5);
130 overlap=0.0f;
131 nimg=0;
132 }
133
134 float *result_data = result->get_data();
135 float *norm_data = norm_image->get_data();
136 float *data = image->get_data();
137
138 vector<float> threshv;
139 threshv=image->calc_radial_dist(nx/2,0,1,4);
140 for (int i=0; i<nx/2; i++) threshv[i]*=threshv[i]*thresh_sigma; // The value here is amplitude, we square to make comparison less expensive
141
142 size_t j=0;
143 // Add any values above threshold to the result image, and add 1 to the corresponding pixels in the norm image
144 int k=0;
145 for (int z=0; z<nz; z++) {
146 for (int y=0; y<ny; y++) {
147 for (int x=0; x<nx; x+=2, j+=2) {
148 float rf=Util::hypot3(x/2,y<ny/2?y:ny-y,z<nz/2?z:nz-z); // origin at 0,0; periodic
149 int r=int(rf);
150 if (r>ny/2) continue;
151 float f=data[j]; // real
152 float g=data[j+1]; // imag
153 float inten=f*f+g*g;
154
155 if (inten<threshv[r]) continue;
156
157 k+=1;
158 result_data[j] +=f;
159 result_data[j+1]+=g;
160
161 norm_data[j] +=1.0;
162 norm_data[j+1]+=1.0;
163 }
164 }
165 }
166// printf("%d %d\n",k,nx*ny*nz);
167 overlap+=(float)k/(nx*ny*nz);
168 nimg++;
169
170 if (image->has_attr("free_me")) delete image;
171}
172
174{
175 if (norm_image==0 || result==0 || nimg==0) return NULL;
176
177 int nx = result->get_xsize();
178 int ny = result->get_ysize();
179 int nz = result->get_zsize();
180 size_t image_size = (size_t)nx * ny * nz;
181
182 float *result_data = result->get_data();
183 float *norm_data = norm_image->get_data();
184
185// printf("finish average %d %d %d",nx,ny,nz);
186 // normalize the average
187 for (size_t j = 0; j < image_size; j++) {
188 if (norm_data[j]==0.0) result_data[j]=0.0;
189 else result_data[j]/=norm_data[j];
190 }
191
192 norm_image->update();
193 result->update();
194
195 EMData *ret;
196 if ((int)params.set_default("doift", 1))
197 ret = result->do_ift();
198 else
199 ret = result->copy();
200
201 ret->set_attr("ptcl_repr",norm_image->get_attr("maximum"));
202 ret->set_attr("mean_coverage",(float)(overlap/nimg));
203 if ((int)params.set_default("save_norm", 0))
204 norm_image->write_image("norm.hdf");
205
206 if (params.has_key("normout")) {
207 EMData *normout=(EMData*) params["normout"];
208 normout->set_data(norm_image->copy()->get_data());
209 normout->update();
210 }
211
212 delete result;
213 delete norm_image;
214 result=0;
215 norm_image=0;
216
217 return ret;
218}
219
220
222 : sigma_image(0), ignore0(0), normimage(0), freenorm(0), nimg(0)
223{
224
225}
226
228{
229 if (!image) {
230 return;
231 }
232
233 if (nimg >= 1 && !EMUtil::is_same_size(image, result)) {
234 LOGERR("%sAverager can only process same-size Image",
235 get_name().c_str());
236 return;
237 }
238
239 nimg++;
240
241 int nx = image->get_xsize();
242 int ny = image->get_ysize();
243 int nz = image->get_zsize();
244 size_t image_size = (size_t)nx * ny * nz;
245
246 if (nimg == 1) {
247 result = image->copy_head();
248 result->set_size(nx, ny, nz);
249 sigma_image = params.set_default("sigma", (EMData*)0);
250 ignore0 = params["ignore0"];
251
252 normimage = params.set_default("normimage", (EMData*)0);
253 if (ignore0 && normimage==0) { normimage=new EMData(nx,ny,nz); freenorm=1; }
254 if (normimage) normimage->to_zero();
255 }
256
257 float *result_data = result->get_data();
258 float *sigma_image_data = 0;
259 if (sigma_image) {
260 sigma_image->set_size(nx, ny, nz);
261 sigma_image_data = sigma_image->get_data();
262 }
263
264 float * image_data = image->get_data();
265
266 if (!ignore0) {
267 for (size_t j = 0; j < image_size; ++j) {
268 float f = image_data[j];
269 result_data[j] += f;
270 if (sigma_image_data) {
271 sigma_image_data[j] += f * f;
272 }
273 }
274 }
275 else {
276 for (size_t j = 0; j < image_size; ++j) {
277 float f = image_data[j];
278 if (f) {
279 result_data[j] += f;
280 if (sigma_image_data) {
281 sigma_image_data[j] += f * f;
282 }
283 normimage->set_value_at_fast(j,normimage->get_value_at(j)+1.0);
284 }
285 }
286 }
287}
288
290{
291 if (result && nimg > 1) {
292 size_t image_size = (size_t)result->get_xsize() * result->get_ysize() * result->get_zsize();
293 float * result_data = result->get_data();
294
295 if (!ignore0) {
296 for (size_t j = 0; j < image_size; ++j) {
297 result_data[j] /= nimg;
298 }
299
300 if (sigma_image) {
301 float * sigma_image_data = sigma_image->get_data();
302
303 for (size_t j = 0; j < image_size; ++j) {
304 float f1 = sigma_image_data[j] / nimg;
305 float f2 = result_data[j];
306 sigma_image_data[j] = sqrt(f1 - f2 * f2);
307 }
308
309 sigma_image->update();
310 }
311 }
312 else {
313 for (size_t j = 0; j < image_size; ++j) {
314 if (normimage->get_value_at(j)>0) result_data[j] /= normimage->get_value_at(j);
315 }
316 if (sigma_image) {
317 float * sigma_image_data = sigma_image->get_data();
318
319 for (size_t j = 0; j < image_size; ++j) {
320 float f1 = 0;
321 if (normimage->get_value_at(j)>0) f1=sigma_image_data[j] / normimage->get_value_at(j);
322 float f2 = result_data[j];
323 sigma_image_data[j] = sqrt(f1 - f2 * f2);
324 }
325
326 sigma_image->update();
327 }
328 }
329
330 result->update();
331
332 result->set_attr("ptcl_repr",nimg);
333
334 if (freenorm) { delete normimage; normimage=(EMData*)0; }
335 nimg=0;
336
337 return result;
338 }
339
340 return nullptr;
341}
342
343
344
345
347 : mean_image(0), ignore0(0), normimage(0), freenorm(0), nimg(0)
348{
349
350}
351
353{
354 if (!image) {
355 return;
356 }
357
358 if (nimg >= 1 && !EMUtil::is_same_size(image, mean_image)) {
359 LOGERR("%sAverager can only process same-size Image",
360 get_name().c_str());
361 return;
362 }
363
364 nimg++;
365
366 int nx = image->get_xsize();
367 int ny = image->get_ysize();
368 int nz = image->get_zsize();
369 size_t image_size = (size_t)nx * ny * nz;
370
371 if (nimg == 1) {
372 mean_image = image->copy_head();
373 mean_image->set_size(nx, ny, nz);
374
375 result = image->copy_head();
376 result->set_size(nx, ny, nz);
377
378 ignore0 = params["ignore0"];
379 normimage = params.set_default("normimage", (EMData*)0);
380 if (ignore0 && normimage==0) { normimage=new EMData(nx,ny,nz); freenorm=1; }
381 if (normimage) normimage->to_zero();
382 }
383
384 float *mean_image_data = mean_image->get_data();
385 float *result_data = result->get_data();
386 float * image_data = image->get_data();
387
388 if (!ignore0) {
389 for (size_t j = 0; j < image_size; ++j) {
390 float f = image_data[j];
391 mean_image_data[j] += f;
392 if (result_data) {
393 result_data[j] += f * f;
394 }
395 }
396 }
397 else {
398 for (size_t j = 0; j < image_size; ++j) {
399 float f = image_data[j];
400 if (f) {
401 mean_image_data[j] += f;
402 if (result_data) {
403 result_data[j] += f * f;
404 }
405 normimage->set_value_at_fast(j,normimage->get_value_at(j)+1.0);
406 }
407 }
408 }
409}
410
412{
413 if (mean_image && nimg > 1) {
414 size_t image_size = (size_t)mean_image->get_xsize() * mean_image->get_ysize() * mean_image->get_zsize();
415 float * mean_image_data = mean_image->get_data();
416 if (!ignore0) {
417 for (size_t j = 0; j < image_size; ++j) {
418 mean_image_data[j] /= nimg;
419 }
420
421 float * result_data = result->get_data();
422
423 for (size_t j = 0; j < image_size; ++j) {
424 float f1 = result_data[j] / nimg;
425 float f2 = mean_image_data[j];
426 result_data[j] = sqrt(f1 - f2 * f2);
427 }
428
429 result->update();
430 }
431 else {
432 for (size_t j = 0; j < image_size; ++j) {
433 if (normimage->get_value_at(j)>0) mean_image_data[j] /= normimage->get_value_at(j);
434 }
435
436 float * result_data = result->get_data();
437
438 for (size_t j = 0; j < image_size; ++j) {
439 float f1 = 0;
440 if (normimage->get_value_at(j)>0) f1=result_data[j] / normimage->get_value_at(j);
441 float f2 = mean_image_data[j];
442 result_data[j] = sqrt(f1 - f2 * f2);
443
444 result->update();
445 }
446 }
447
448 mean_image->update();
449 mean_image->set_attr("ptcl_repr",nimg);
450
451 result->set_attr("ptcl_repr",nimg);
452
453 if (freenorm) { delete normimage; normimage=(EMData*)0; }
454
455 return result;
456 }
457 else {
458 LOGERR("%sAverager requires >=2 images", get_name().c_str());
459 }
460}
461
463 : normimage(0), freenorm(0), nimg(0)
464{
465
466}
467
469{
470 if (!image) {
471 return;
472 }
473
474
475
476 EMData *img=image->do_fft();
477 if (nimg >= 1 && !EMUtil::is_same_size(img, result)) {
478 LOGERR("%sAverager can only process same-size Image",
479 get_name().c_str());
480 return;
481 }
482
483 nimg++;
484
485 int nx = img->get_xsize();
486 int ny = img->get_ysize();
487 int nz = 1;
488// size_t image_size = (size_t)nx * ny * nz;
489
490 XYData *weight=(XYData *)image->get_attr("avg_weight");
491
492 if (nimg == 1) {
493 result = new EMData(nx,ny,nz);
494 result->set_complex(true);
495 result->to_zero();
496
497 normimage = params.set_default("normimage", (EMData*)0);
498 if (normimage==0) { normimage=new EMData(nx/2,ny,nz); freenorm=1; }
499 normimage->to_zero();
500 }
501
502 // We're using routines that handle complex image wraparound for us, so we iterate over the half-plane
503 for (int y=-ny/2; y<ny/2; y++) {
504 for (int x=0; x<nx/2; x++) {
505 std::complex<float> v=img->get_complex_at(x,y);
506 float r=Util::hypot2(y/(float)ny,x/(float)nx);
507 float wt=weight->get_yatx(r);
508 result->set_complex_at(x,y,result->get_complex_at(x,y)+v*wt);
509 normimage->set_value_at(x,y+ny/2,normimage->get_value_at(x,y+ny/2)+wt);
510 }
511 }
512
513 delete img;
514}
515
517{
518 EMData *ret = (EMData *)0;
519
520 if (result && nimg >= 1) {
521 // We're using routines that handle complex image wraparound for us, so we iterate over the half-plane
522 int nx = result->get_xsize();
523 int ny = result->get_ysize();
524
525 for (int y=-ny/2; y<ny/2; y++) {
526 for (int x=0; x<nx/2; x++) {
527 float norm=normimage->get_value_at(x,y+ny/2);
528 if (norm<=0) result->set_complex_at(x,y,0.0f);
529 else result->set_complex_at(x,y,result->get_complex_at(x,y)/norm);
530 }
531 }
532
533 result->update();
534// result->mult(1.0f/(float)result->get_attr("sigma"));
535// result->write_image("tmp.hdf",0);
536// printf("%g %g %g\n",(float)result->get_attr("sigma"),(float)result->get_attr("minimum"),(float)result->get_attr("maximum"));
537 ret=result->do_ift();
538 delete result;
539 result=(EMData*) 0;
540 }
541 ret->set_attr("ptcl_repr",nimg);
542
543 if (freenorm) { delete normimage; normimage=(EMData*)0; }
544 nimg=0;
545
546 return ret;
547}
548
550 : normimage(0), freenorm(0), nimg(0),fourier(0)
551{
552
553}
554
556{
557 if (!image) {
558 return;
559 }
560
561 nimg++;
562
563 int nx = image->get_xsize();
564 int ny = image->get_ysize();
565 int nz = image->get_zsize();
566
567 fourier = params.set_default("fourier", (int)0);
568 if (nimg == 1) {
569 result = image->copy_head();
570 result->set_size(nx, ny, nz);
571 result->to_zero();
572
573
574 normimage = params.set_default("normimage", (EMData*)0);
575 if (normimage==0) { normimage=new EMData(nx,ny,nz); freenorm=1; }
576 normimage->to_zero();
577 normimage->add(0.0000001f);
578 }
579
580 images.push_back(image->copy());
581}
582
584{
585 if (nimg==0) return NULL;
586
587 int nx = images.front()->get_xsize();
588 int ny = images.front()->get_ysize();
589 int nz = images.front()->get_zsize();
590
591 float dampnoise = params.set_default("dampnoise", (float)0.5);
592
593 // This is the standard mode where local real-space correlation with the average is used to define a local weight (like a mask)
594 // applied to each image. If fourier>=2 then the same is done, but in "gaussian bands" in Fourier space
595 if (fourier<=1) {
596 EMData *stg1 = new EMData(nx,ny,nz);
597
598 for (std::vector<EMData*>::iterator im = images.begin(); im!=images.end(); ++im) stg1->add(**im);
599 stg1->process_inplace("normalize");
600
601 // std::vector<EMData*> weights;
602 for (std::vector<EMData*>::iterator im = images.begin(); im!=images.end(); ++im) {
603 EMData *imc=(*im)->copy();
604 imc->mult(*stg1);
605 imc->process_inplace("filter.lowpass.gauss",Dict("cutoff_freq",0.02f));
606 imc->process_inplace("threshold.belowtozero",Dict("minval",0.0f));
607 // imc->process_inplace("math.sqrt");
608 // imc->process_inplace("math.pow",Dict("pow",0.25));
609 (*im)->mult(*imc);
610 result->add(**im);
611 normimage->add(*imc);
612 delete imc;
613 delete *im;
614 }
615
616 if (dampnoise>0) {
617 float mean=normimage->get_attr("mean");
618 float max=normimage->get_attr("maximum");
619 normimage->process_inplace("threshold.clampminmax",Dict("minval",mean*dampnoise,"maxval",max));
620 }
621
622 result->div(*normimage);
623
624 result->set_attr("ptcl_repr",nimg);
625
626 if (freenorm) { delete normimage; normimage=(EMData*)0; }
627 nimg=0;
628
629 return result;
630 }
631 else if (fourier<=ny/2) {
632 // we do pretty much the same thing as above, but one Fourier "band" at a time
633
634 for (int r=0; r<fourier; r++) {
635 float cen=r/((float)fourier-1.0)*0.5;
636 float sig=(0.5/fourier)/(2.0*sqrt(log(2.0)));
637 std::vector<EMData*> filt;
638
639 EMData *stg1 = new EMData(nx,ny,nz);
640 for (std::vector<EMData*>::iterator im = images.begin(); im!=images.end(); ++im) {
641 EMData *f=(*im)->process("filter.bandpass.gauss",Dict("center",(float)cen,"sigma",sig));
642 filt.push_back(f);
643 stg1->add(*f);
644 if (r==fourier-1) delete *im; // we don't actually need the unfiltered images again
645 }
646 stg1->process_inplace("normalize");
647 stg1->process("filter.bandpass.gauss",Dict("center",(float)cen,"sigma",sig));
648// stg1->write_image("cmp.hdf",r);
649
650 // std::vector<EMData*> weights;
651 int imn=1;
652 for (std::vector<EMData*>::iterator im = filt.begin(); im!=filt.end(); ++im) {
653 EMData *imc=(*im)->copy();
654 imc->mult(*stg1);
655 imc->process_inplace("filter.lowpass.gauss",Dict("cutoff_freq",0.02f));
656 imc->process_inplace("threshold.belowtozero",Dict("minval",0.0f));
657// imc->write_image("cmp.hdf",imn*fourier+r);
658 // imc->process_inplace("math.sqrt");
659 // imc->process_inplace("math.pow",Dict("pow",0.25));
660 (*im)->mult(*imc);
661 result->add(**im);
662 normimage->add(*imc);
663 delete *im;
664 delete imc;
665 imn++;
666 }
667
668 if (dampnoise>0) {
669 float mean=normimage->get_attr("mean");
670 float max=normimage->get_attr("maximum");
671 normimage->process_inplace("threshold.clampminmax",Dict("minval",mean*dampnoise,"maxval",max));
672 }
673
674 }
675 result->div(*normimage);
676 result->set_attr("ptcl_repr",nimg);
677
678 if (freenorm) { delete normimage; normimage=(EMData*)0; }
679 nimg=0;
680 return result;
681 }
682
683}
684
686{
687
688}
689
691{
692 if (!image) return;
693
694 images.push_back(image->do_fft());
695}
696
698{
699 if (images.size()==0) return NULL;
700
701 int nx = images.front()->get_xsize();
702 int ny = images.front()->get_ysize();
703 int nz = images.front()->get_zsize();
704
705 if (nz!=1) throw ImageDimensionException("IterAverager is for 2-D images only");
706
707 if (result) delete result;
708 result=new EMData(nx-2,ny,nz,0);
709 result -> to_zero();
710
711 EMData *tmp=new EMData(nx-2,ny,nz,0);
712 tmp -> to_zero();
713
714 for (int it=0; it<4; it++) {
715 for (int y=-ny/2+1; y<ny/2-1; y++) {
716 for (int x=0; x<nx-2; x++) {
717 std::complex<double> nv=0;
718 // put the vector on the inside, then we can accumulate into a double easily
719 for (vector<EMData *>::iterator im=images.begin(); im<images.end(); im++) {
720 for (int yy=y-1; yy<=y+1; yy++) {
721 for (int xx=x-1; xx<=x+1; xx++) {
722 nv+=(*im)->get_complex_at(xx,yy)+tmp->get_complex_at(x,y)-tmp->get_complex_at(xx,yy);
723 }
724 }
725 }
726 result->set_complex_at(x,y,std::complex<float>(nv/(9.0*images.size())));
727 }
728 }
729 // Swap the pointers
730 result->write_image("dbug.hdf",-1);
731 EMData *swp=tmp;
732 tmp=result;
733 result=swp;
734 }
735
736 delete result;
737 result=0;
738 for (vector<EMData *>::iterator im=images.begin(); im<images.end(); im++) delete (*im);
739 images.clear();
740 result=tmp->do_ift();
741 delete tmp;
742 return result;
743}
744
745
746#if 0
747EMData *ImageAverager::average(const vector < EMData * >&image_list) const
748{
749 if (image_list.size() == 0) {
750 return 0;
751 }
752
753 EMData *sigma_image = params["sigma"];
754 int ignore0 = params["ignore0"];
755
756 EMData *image0 = image_list[0];
757
758 int nx = image0->get_xsize();
759 int ny = image0->get_ysize();
760 int nz = image0->get_zsize();
761 size_t image_size = (size_t)nx * ny * nz;
762
763 EMData *result = new EMData();
764 result->set_size(nx, ny, nz);
765
766 float *result_data = result->get_data();
767 float *sigma_image_data = 0;
768
769 if (sigma_image) {
770 sigma_image->set_size(nx, ny, nz);
771 sigma_image_data = sigma_image->get_data();
772 }
773
774 int c = 1;
775 if (ignore0) {
776 for (size_t j = 0; j < image_size; ++j) {
777 int g = 0;
778 for (size_t i = 0; i < image_list.size(); i++) {
779 float f = image_list[i]->get_value_at(j);
780 if (f) {
781 g++;
782 result_data[j] += f;
783 if (sigma_image_data) {
784 sigma_image_data[j] += f * f;
785 }
786 }
787 }
788 if (g) {
789 result_data[j] /= g;
790 }
791 }
792 }
793 else {
794 float *image0_data = image0->get_data();
795 if (sigma_image_data) {
796 memcpy(sigma_image_data, image0_data, image_size * sizeof(float));
797
798 for (size_t j = 0; j < image_size; ++j) {
799 sigma_image_data[j] *= sigma_image_data[j];
800 }
801 }
802
803 image0->update();
804 memcpy(result_data, image0_data, image_size * sizeof(float));
805
806 for (size_t i = 1; i < image_list.size(); i++) {
807 EMData *image = image_list[i];
808
809 if (EMUtil::is_same_size(image, result)) {
810 float *image_data = image->get_data();
811
812 for (size_t j = 0; j < image_size; ++j) {
813 result_data[j] += image_data[j];
814 }
815
816 if (sigma_image_data) {
817 for (size_t j = 0; j < image_size; ++j) {
818 sigma_image_data[j] += image_data[j] * image_data[j];
819 }
820 }
821
822 image->update();
823 c++;
824 }
825 }
826
827 for (size_t j = 0; j < image_size; ++j) {
828 result_data[j] /= static_cast < float >(c);
829 }
830 }
831
832 if (sigma_image_data) {
833 for (size_t j = 0; j < image_size; ++j) {
834 float f1 = sigma_image_data[j] / c;
835 float f2 = result_data[j];
836 sigma_image_data[j] = sqrt(f1 - f2 * f2);
837 }
838 }
839
840 if (sigma_image_data) {
841 sigma_image->update();
842 }
843
844 result->update();
845 return result;
846}
847#endif
848
850 : nimg(0),ismax(0),isabs(0)
851{
852
853}
854
856{
857 if (!image) {
858 return;
859 }
860
861 if (nimg >= 1 && !EMUtil::is_same_size(image, result)) {
862 LOGERR("%sAverager can only process same-size Image",
863 get_name().c_str());
864 return;
865 }
866 EMData *owner = params.set_default("owner", (EMData*)0);
867
868 float thisown = image->get_attr_default("ortid",(float)nimg);
869 nimg++;
870
871 size_t nxyz = image->get_size();
872
873 if (nimg == 1) {
874 result = image->copy();
875 if (owner) owner->to_value(thisown);
876 return;
877 }
878
879 float *rdata = result->get_data();
880 float *data = image->get_data();
881 float *owndata = 0;
882 if (owner) owndata=owner->get_data();
883
884 ismax=(int)params.set_default("max",0);
885 isabs=(int)params.set_default("abs",0);
886
887
888 for (size_t i=0; i<nxyz; i++) {
889 float v = isabs?fabs(data[i]):data[i];
890 float rv = isabs?fabs(rdata[i]):rdata[i];
891 if ((ismax && v>rv) || (!ismax && v<rv)) {
892 rdata[i]=data[i];
893 if (owndata) owndata[i]=thisown;
894 }
895 }
896
897}
898
900{
901 result->update();
902 result->set_attr("ptcl_repr",nimg);
903
904 if (result && nimg >= 1) return result;
905
906 return NULL;
907}
908
910{
911
912}
913
915{
916 if (!image) {
917 return;
918 }
919
920 if (!imgs.empty() && !EMUtil::is_same_size(image, imgs[0])) {
921 LOGERR("MedianAverager can only process images of the same size");
922 return;
923 }
924
925 imgs.push_back(image->copy());
926}
927
929{
930 if (imgs.size()==0) return 0;
931 EMData *ret=0;
932
933 if (imgs.size()==1) {
934 ret=imgs[0];
935 imgs.clear();
936 return ret;
937 }
938
939 // special case for n==2
940 if (imgs.size()==2) {
941 imgs[0]->add(*imgs[1]);
942 imgs[0]->mult(0.5f);
943 delete imgs[1];
944 ret=imgs[0];
945 imgs.clear();
946 return ret;
947 }
948
949 int nx=imgs[0]->get_xsize();
950 int ny=imgs[0]->get_ysize();
951 int nz=imgs[0]->get_zsize();
952
953 // special case for n==3
954 if (imgs.size()==3) {
955 for (int z=0; z<nz; z++) {
956 for (int y=0; y<ny; y++) {
957 for (int x=0; x<nx; x++) {
958 float v0=imgs[0]->get_value_at(x,y,z);
959 float v1=imgs[1]->get_value_at(x,y,z);
960 float v2=imgs[2]->get_value_at(x,y,z);
961
962 if (v0<=v1) {
963 if (v0>=v2) continue;
964 if (v1<=v2) imgs[0]->set_value_at(x,y,z,v1);
965 else imgs[0]->set_value_at(x,y,z,v2);
966 }
967 else {
968 if (v0<=v2) continue;
969 if (v2<=v1) imgs[0]->set_value_at(x,y,z,v1);
970 else imgs[0]->set_value_at(x,y,z,v2);
971 }
972 }
973 }
974 }
975
976 delete imgs[1];
977 delete imgs[2];
978 ret=imgs[0];
979 imgs.clear();
980 return ret;
981 }
982
983 ret=imgs[0]->copy();
984 std::vector<float> vals(imgs.size(),0.0f);
985
986
987 for (int z=0; z<nz; z++) {
988 for (int y=0; y<ny; y++) {
989 for (int x=0; x<nx; x++) {
990 int i=0;
991 for (std::vector<EMData *>::iterator it = imgs.begin() ; it != imgs.end(); ++it,++i) {
992 vals[i]=(*it)->get_value_at(x,y,z);
993 }
994
995 std::sort(vals.begin(),vals.end());
996 //printf("%d %d %d %d\n",x,y,z,vals.size());
997 if (vals.size()&1) ret->set_value_at(x,y,z,vals[vals.size()/2]); // for even sizes, not quite right, and should include possibility of local average
998 else ret->set_value_at(x,y,z,(vals[vals.size()/2]+vals[vals.size()/2-1])/2.0f);
999 }
1000 }
1001 }
1002
1003 if (!imgs.empty()) {
1004 for (std::vector<EMData *>::iterator it = imgs.begin() ; it != imgs.end(); ++it) if (*it) delete *it;
1005 imgs.clear();
1006 }
1007
1008
1009 return ret;
1010}
1011
1013 : nimg(0)
1014{
1015
1016}
1017
1018
1020{
1021 if (!image) {
1022 return;
1023 }
1024
1025
1026
1027 EMData *fft=image->do_fft();
1028
1029 if (nimg >= 1 && !EMUtil::is_same_size(fft, result)) {
1030 LOGERR("%s Averager can only process images of the same size", get_name().c_str());
1031 return;
1032 }
1033
1034 nimg++;
1035 if (nimg == 1) {
1036 result = fft->copy_head();
1037 result->to_zero();
1038 }
1039
1040 Ctf *ctf = (Ctf *)image->get_attr("ctf");
1041//string cc=ctf->to_string();
1042//FILE *out=fopen("ctf.txt","a");
1043//fprintf(out,"%s\n",cc.c_str());
1044//fclose(out);
1045 float b=ctf->bfactor;
1046 ctf->bfactor=100.0; // FIXME - this is a temporary fixed B-factor which does a (very) little sharpening
1047
1048// if (nimg==1) unlink("snr.hdf");
1049
1050 EMData *snr = result -> copy();
1052// snr->write_image("snr.hdf",-1);
1053 EMData *ctfi = result-> copy();
1055
1056 ctf->bfactor=b; // return to its original value
1057
1058 float *outd = result->get_data();
1059 float *ind = fft->get_data();
1060 float *snrd = snr->get_data();
1061 float *ctfd = ctfi->get_data();
1062
1063 size_t sz=snr->get_xsize()*snr->get_ysize();
1064 for (size_t i = 0; i < sz; i+=2) {
1065 if (snrd[i]<0) snrd[i]=0.001; // Used to be 0. See ctfcauto averager
1066 ctfd[i]=fabs(ctfd[i]);
1067 if (ctfd[i]<.05) ctfd[i]=0.05f;
1068// {
1069// if (snrd[i]<=0) ctfd[i]=.05f;
1070// else ctfd[i]=snrd[i]*10.0f;
1071// }
1072 outd[i]+=ind[i]*snrd[i]/ctfd[i];
1073 outd[i+1]+=ind[i+1]*snrd[i]/ctfd[i];
1074 }
1075
1076 if (nimg==1) {
1077 snrsum=snr->copy_head();
1078 float *ssnrd=snrsum->get_data();
1079 // we're only using the real component, and we need to start with 1.0
1080 for (size_t i = 0; i < sz; i+=2) { ssnrd[i]=1.0; ssnrd[i+1]=0.0; }
1081 }
1082// snr->write_image("snr.hdf",-1);
1083 snr->process_inplace("math.absvalue");
1084 snrsum->add(*snr);
1085
1086 delete ctf;
1087 delete fft;
1088 delete snr;
1089 delete ctfi;
1090}
1091
1093{
1094/* EMData *tmp=result->do_ift();
1095 tmp->write_image("ctfcw.hdf",0);
1096 delete tmp;
1097
1098 tmp=snrsum->do_ift();
1099 tmp->write_image("ctfcw.hdf",1);
1100 delete tmp;*/
1101
1102//snrsum->write_image("snrsum.hdf",-1);
1103 //size_t sz=result->get_xsize()*result->get_ysize();
1104 int nx=result->get_xsize();
1105 int ny=result->get_ysize();
1106 float *snrsd=snrsum->get_data();
1107 float *outd=result->get_data();
1108
1109 int rm=(ny-2)*(ny-2)/4;
1110 for (int j=0; j<ny; j++) {
1111 for (int i=0; i<nx; i+=2) {
1112 size_t ii=i+j*nx;
1113 if ((j<ny/2 && i*i/4+j*j>rm) ||(j>=ny/2 && i*i/4+(ny-j)*(ny-j)>rm) || snrsd[ii]==0) { outd[ii]=outd[ii+1]=0; continue; }
1114 outd[ii]/=snrsd[ii]; // snrsd contains total SNR
1115 outd[ii+1]/=snrsd[ii];
1116 }
1117 }
1118
1119 result->update();
1120 result->set_attr("ptcl_repr",nimg);
1121 result->set_attr("ctf_snr_total",snrsum->calc_radial_dist(snrsum->get_ysize()/2,0,1,false));
1122 result->set_attr("ctf_wiener_filtered",1);
1123
1124 delete snrsum;
1125 EMData *ret=result->do_ift();
1126 delete result;
1127 result=NULL;
1128 return ret;
1129}
1130
1132 : nimg(0)
1133{
1134
1135}
1136
1137
1139{
1140 if (!image) {
1141 return;
1142 }
1143
1144
1145
1146 EMData *fft=image->do_fft();
1147
1148 if (nimg >= 1 && !EMUtil::is_same_size(fft, result)) {
1149 LOGERR("%s Averager can only process images of the same size", get_name().c_str());
1150 return;
1151 }
1152
1153 nimg++;
1154 if (nimg == 1) {
1155 result = fft->copy_head();
1156 result->to_zero();
1157 }
1158
1159 Ctf *ctf = (Ctf *)image->get_attr("ctf");
1160 float b=ctf->bfactor;
1161 ctf->bfactor=0; // NO B-FACTOR CORRECTION !
1162
1163 EMData *snr = result -> copy();
1165 EMData *ctfi = result-> copy();
1167
1168 ctf->bfactor=b; // return to its original value
1169
1170 float *outd = result->get_data();
1171 float *ind = fft->get_data();
1172 float *snrd = snr->get_data();
1173 float *ctfd = ctfi->get_data();
1174
1175 size_t sz=snr->get_xsize()*snr->get_ysize();
1176 for (size_t i = 0; i < sz; i+=2) {
1177 if (snrd[i]<=0) snrd[i]=0.001; // used to be 0. Trying to insure that there is always at least a little signal used. In cases with dense particles, SNR may be dramatically underestimated
1178 ctfd[i]=fabs(ctfd[i]);
1179
1180 // This limits the maximum possible amplification in CTF correction to 10x
1181 if (ctfd[i]<.05) ctfd[i]=0.05f;
1182// {
1183// if (snrd[i]<=0) ctfd[i]=.05f;
1184// else ctfd[i]=snrd[i]*10.0f;
1185// }
1186
1187 // SNR weight and CTF correction
1188 outd[i]+=ind[i]*snrd[i]/ctfd[i];
1189 outd[i+1]+=ind[i+1]*snrd[i]/ctfd[i];
1190 }
1191
1192 if (nimg==1) {
1193 snrsum=snr->copy_head();
1194 float *ssnrd=snrsum->get_data();
1195 // we're only using the real component, for Wiener filter we put 1.0 in R, but for just SNR weight we use 0
1196 for (size_t i = 0; i < sz; i+=2) { ssnrd[i]=0.0; ssnrd[i+1]=0.0; }
1197 }
1198 snr->process_inplace("math.absvalue");
1199 snrsum->add(*snr);
1200
1201 delete ctf;
1202 delete fft;
1203 delete snr;
1204 delete ctfi;
1205}
1206
1208{
1209/* EMData *tmp=result->do_ift();
1210 tmp->write_image("ctfcw.hdf",0);
1211 delete tmp;
1212
1213 tmp=snrsum->do_ift();
1214 tmp->write_image("ctfcw.hdf",1);
1215 delete tmp;*/
1216
1217// snrsum->write_image("snrsum.hdf",-1);
1218 //size_t sz=result->get_xsize()*result->get_ysize();
1219 int nx=result->get_xsize();
1220 int ny=result->get_ysize();
1221 float *snrsd=snrsum->get_data();
1222 float *outd=result->get_data();
1223
1224 int rm=(ny-2)*(ny-2)/4;
1225 for (int j=0; j<ny; j++) {
1226 for (int i=0; i<nx; i+=2) {
1227 size_t ii=i+j*nx;
1228 if ((j<ny/2 && i*i/4+j*j>rm) ||(j>=ny/2 && i*i/4+(ny-j)*(ny-j)>rm) || snrsd[ii]==0) { outd[ii]=outd[ii+1]=0; continue; }
1229 // we aren't wiener filtering, but if the total SNR is too low, we don't want TOO much exaggeration of noise
1230 if (snrsd[ii]<.05) {
1231 outd[ii]*=20.0; // 1/0.05
1232 outd[ii+1]*=20.0;
1233 }
1234 else {
1235 outd[ii]/=snrsd[ii]; // snrsd contains total SNR
1236 outd[ii+1]/=snrsd[ii];
1237 }
1238 }
1239 }
1240 result->update();
1241 result->set_attr("ptcl_repr",nimg);
1242 result->set_attr("ctf_snr_total",snrsum->calc_radial_dist(snrsum->get_ysize()/2,0,1,false));
1243 result->set_attr("ctf_wiener_filtered",0);
1244
1245/* snrsum->write_image("snr.hdf",-1);
1246 result->write_image("avg.hdf",-1);*/
1247
1248 delete snrsum;
1249 EMData *ret=result->do_ift();
1250 delete result;
1251 result=NULL;
1252 return ret;
1253}
1254
1256 : nimg(0)
1257{
1258
1259}
1260
1261
1263{
1264 if (!image) {
1265 return;
1266 }
1267
1268
1269
1270 EMData *fft=image->do_fft();
1271
1272 if (nimg >= 1 && !EMUtil::is_same_size(fft, result)) {
1273 LOGERR("%s Averager can only process images of the same size", get_name().c_str());
1274 return;
1275 }
1276
1277 nimg++;
1278 if (nimg == 1) {
1279 result = fft->copy_head();
1280 result->to_zero();
1281 }
1282
1283 Ctf *ctf = (Ctf *)image->get_attr("ctf");
1284
1285 EMData *ctfi = result-> copy();
1286 float b=ctf->bfactor;
1287 ctf->bfactor=0; // no B-factor used in weight
1289 ctf->bfactor=b; // return to its original value
1290
1291 float *outd = result->get_data();
1292 float *ind = fft->get_data();
1293 float *ctfd = ctfi->get_data();
1294
1295 size_t sz=ctfi->get_xsize()*ctfi->get_ysize();
1296 for (size_t i = 0; i < sz; i+=2) {
1297
1298 // CTF weight
1299 outd[i]+=ind[i]*ctfd[i];
1300 outd[i+1]+=ind[i+1]*ctfd[i];
1301 }
1302
1303 if (nimg==1) {
1304 ctfsum=ctfi->copy_head();
1305 ctfsum->to_zero();
1306 }
1307 ctfsum->add(*ctfi);
1308
1309 delete ctf;
1310 delete fft;
1311 delete ctfi;
1312}
1313
1315{
1316 int nx=result->get_xsize();
1317 int ny=result->get_ysize();
1318 float *ctfsd=ctfsum->get_data();
1319 float *outd=result->get_data();
1320
1321 for (int j=0; j<ny; j++) {
1322 for (int i=0; i<nx; i+=2) {
1323 size_t ii=i+j*nx;
1324 outd[ii]/=ctfsd[ii]; // snrsd contains total SNR
1325 outd[ii+1]/=ctfsd[ii];
1326 }
1327 }
1328 result->update();
1329 result->set_attr("ptcl_repr",nimg);
1330// result->set_attr("ctf_total",ctfsum->calc_radial_dist(ctfsum->get_ysize()/2,0,1,false));
1331 result->set_attr("ctf_wiener_filtered",0);
1332
1333/* snrsum->write_image("snr.hdf",-1);
1334 result->write_image("avg.hdf",-1);*/
1335
1336 delete ctfsum;
1337 EMData *ret=result->do_ift();
1338 delete result;
1339 result=NULL;
1340 return ret;
1341}
1342
1344{
1345 nimg[0]=0;
1346 nimg[1]=0;
1347 eo=-1;
1348}
1349
1350
1352{
1353 if (!image) {
1354 return;
1355 }
1356
1357
1358
1359 EMData *fft=image->do_fft();
1360
1361 if (nimg[0] >= 1 && !EMUtil::is_same_size(fft, results[0])) {
1362 LOGERR("%s Averager can only process images of the same size", get_name().c_str());
1363 return;
1364 }
1365
1366 if (eo==-1) {
1367 results[0] = fft->copy_head();
1368 results[0]->to_zero();
1369 results[1] = fft->copy_head();
1370 results[1]->to_zero();
1371 eo=1;
1372 }
1373
1374 eo^=1;
1375 nimg[eo]++;
1376
1377
1378 EMData *ctfi = results[0]-> copy();
1379 if (image->has_attr("ctf")) {
1380 Ctf *ctf = (Ctf *)image->get_attr("ctf");
1381
1382 float b=ctf->bfactor;
1383 ctf->bfactor=0; // no B-factor used in weight, not strictly threadsafe, but shouldn't be a problem
1385 ctf->bfactor=b; // return to its original value
1386 delete ctf;
1387 }
1388 else {
1389 ctfi->to_one();
1390 }
1391
1392 float *outd = results[eo]->get_data();
1393 float *ind = fft->get_data();
1394 float *ctfd = ctfi->get_data();
1395
1396 size_t sz=ctfi->get_xsize()*ctfi->get_ysize();
1397 for (size_t i = 0; i < sz; i+=2) {
1398
1399 // CTF weight
1400 outd[i]+=ind[i]*ctfd[i];
1401 outd[i+1]+=ind[i+1]*ctfd[i];
1402 }
1403
1404 if (nimg[eo]==1) {
1405 ctfsum[eo]=ctfi->copy_head();
1406 ctfsum[eo]->to_zero();
1407 ctfsum[eo]->add(0.1); // we start with a value of 0.1 rather than zero to empirically help with situations where the data is incomplete
1408 }
1409 ctfsum[eo]->add(*ctfi);
1410
1411 delete fft;
1412 delete ctfi;
1413}
1414
1416{
1417 if (nimg[0]==0 && nimg[1]==0) return NULL; // no images
1418 // Only a single image, so we just return it. No way to filter
1419 if (nimg[1]==0) {
1420 EMData *ret=results[0]->do_ift();
1421 delete results[0];
1422 delete results[1];
1423 delete ctfsum[0];
1424 return ret;
1425 }
1426
1427 int nx=results[0]->get_xsize();
1428 int ny=results[0]->get_ysize();
1429
1430 for (int k=0; k<2; k++) {
1431 float *outd=results[k]->get_data();
1432 float *ctfsd=ctfsum[k]->get_data();
1433 for (int j=0; j<ny; j++) {
1434 for (int i=0; i<nx; i+=2) {
1435 size_t ii=i+j*nx;
1436 outd[ii]/=ctfsd[ii];
1437 outd[ii+1]/=ctfsd[ii];
1438 }
1439 }
1440 results[k]->update();
1441 // result->set_attr("ctf_total",ctfsum->calc_radial_dist(ctfsum->get_ysize()/2,0,1,false));
1442 results[0]->set_attr("ctf_wiener_filtered",1);
1443 }
1444
1445 // compute the Wiener filter from the FSC
1446 std::vector<float> fsc=results[0]->calc_fourier_shell_correlation(results[1]);
1447 int third=fsc.size()/3;
1448 for (int i=third; i<third*2; i++) {
1449 if (fsc[i]>=.9999) fsc[i]=.9999;
1450 if (fsc[i]<.001) fsc[i]=.001;
1451 float snr=2.0*fsc[i]/(1.0-fsc[i]); // convert the FSC to SNR and multiply by 2
1452 fsc[i]=snr/(snr+1.0); // to give us the FSC of the combined average, which is also a Wiener filter (depending on whether SNR is squared)
1453 }
1454
1455
1456 results[0]->add(*results[1]);
1457
1458 float c;
1459 for (int j=-ny/2; j<ny/2; j++) {
1460 for (int i=0; i<nx/2; i++) {
1461 int r=(int)Util::hypot_fast(i,j);
1462 if (r>=third) c=0.0;
1463 else c=fsc[third+r];
1464 results[0]->set_complex_at(i,j,results[0]->get_complex_at(i,j)*c);
1465 }
1466 }
1467
1468 EMData *ret=results[0]->do_ift();
1469 ret->set_attr("ptcl_repr",nimg[0]+nimg[1]);
1470
1471/* snrsum->write_image("snr.hdf",-1);
1472 result->write_image("avg.hdf",-1);*/
1473
1474 delete ctfsum[0];
1475 delete ctfsum[1];
1476 delete results[0];
1477 delete results[1];
1478 results[0]=results[1]=NULL;
1479 return ret;
1480}
1481
1482
1483#if 0
1484EMData *IterationAverager::average(const vector < EMData * >&image_list) const
1485{
1486 if (image_list.size() == 0) {
1487 return 0;
1488 }
1489
1490 EMData *image0 = image_list[0];
1491
1492 int nx = image0->get_xsize();
1493 int ny = image0->get_ysize();
1494 int nz = image0->get_zsize();
1495 size_t image_size = (size_t)nx * ny * nz;
1496
1497 EMData *result = new EMData();
1498 result->set_size(nx, ny, nz);
1499
1500 EMData *sigma_image = new EMData();
1501 sigma_image->set_size(nx, ny, nz);
1502
1503 float *image0_data = image0->get_data();
1504 float *result_data = result->get_data();
1505 float *sigma_image_data = sigma_image->get_data();
1506
1507 memcpy(result_data, image0_data, image_size * sizeof(float));
1508 memcpy(sigma_image_data, image0_data, image_size * sizeof(float));
1509
1510 for (size_t j = 0; j < image_size; ++j) {
1511 sigma_image_data[j] *= sigma_image_data[j];
1512 }
1513
1514 image0->update();
1515
1516 int nc = 1;
1517 for (size_t i = 1; i < image_list.size(); i++) {
1518 EMData *image = image_list[i];
1519
1520 if (EMUtil::is_same_size(image, result)) {
1521 float *image_data = image->get_data();
1522
1523 for (size_t j = 0; j < image_size; j++) {
1524 result_data[j] += image_data[j];
1525 sigma_image_data[j] += image_data[j] * image_data[j];
1526 }
1527
1528 image->update();
1529 nc++;
1530 }
1531 }
1532
1533 float c = static_cast < float >(nc);
1534 for (size_t j = 0; j < image_size; ++j) {
1535 float f1 = sigma_image_data[j] / c;
1536 float f2 = result_data[j] / c;
1537 sigma_image_data[j] = sqrt(f1 - f2 * f2) / sqrt(c);
1538 }
1539
1540
1541 for (size_t j = 0; j < image_size; ++j) {
1542 result_data[j] /= c;
1543 }
1544
1545 result->update();
1546 sigma_image->update();
1547
1548 result->append_image("iter.hed");
1549
1550 float sigma = sigma_image->get_attr("sigma");
1551 float *sigma_image_data2 = sigma_image->get_data();
1552 float *result_data2 = result->get_data();
1553 float *d2 = new float[nx * ny];
1554 size_t sec_size = nx * ny * sizeof(float);
1555
1556 memcpy(d2, result_data2, sec_size);
1557 memcpy(sigma_image_data2, result_data2, sec_size);
1558
1559 printf("Iter sigma=%f\n", sigma);
1560
1561 for (int k = 0; k < 1000; k++) {
1562 for (int i = 1; i < nx - 1; i++) {
1563 for (int j = 1; j < ny - 1; j++) {
1564 int l = i + j * nx;
1565 float c1 = (d2[l - 1] + d2[l + 1] + d2[l - nx] + d2[l + nx]) / 4.0f - d2[l];
1566 float c2 = fabs(result_data2[l] - sigma_image_data2[l]) / sigma;
1567 result_data2[l] += c1 * Util::eman_erfc(c2) / 100.0f;
1568 }
1569 }
1570
1571 memcpy(d2, result_data2, sec_size);
1572 }
1573
1574 if( d2 )
1575 {
1576 delete[]d2;
1577 d2 = 0;
1578 }
1579
1580 sigma_image->update();
1581 if( sigma_image )
1582 {
1583 delete sigma_image;
1584 sigma_image = 0;
1585 }
1586
1587 result->update();
1588 result->append_image("iter.hed");
1589
1590 return result;
1591}
1592#endif
1593
1594
1595
1596
1598{
1599 dump_factory < Averager > ();
1600}
1601
1602map<string, vector<string> > EMAN::dump_averagers_list()
1603{
1604 return dump_factory_list < Averager > ();
1605}
#define rdata(i)
Definition: analyzer.cpp:592
#define v0(i)
Definition: analyzer.cpp:698
virtual void add_image_list(const vector< EMData * > &images)
To add multiple images to the Averager.
Definition: averager.cpp:86
EMData * result
Definition: averager.h:158
virtual void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:81
string get_name() const
Get the Averager's name.
Definition: averager.h:573
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:1207
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:1138
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:1092
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:1019
string get_name() const
Get the Averager's name.
Definition: averager.h:612
string get_name() const
Get the Averager's name.
Definition: averager.h:494
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:1314
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:1262
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:1351
EMData * ctfsum[2]
Definition: averager.h:557
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:1415
string get_name() const
Get the Averager's name.
Definition: averager.h:532
EMData * results[2]
Definition: averager.h:556
Ctf is the base class for all CTF model.
Definition: ctf.h:60
virtual void compute_2d_complex(EMData *img, CtfType t, XYData *struct_factor=0)=0
float bfactor
Definition: ctf.h:85
@ CTF_AMP
Definition: ctf.h:65
@ CTF_INTEN
Definition: ctf.h:74
@ CTF_SNR
Definition: ctf.h:68
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
type set_default(const string &key, type val)
Default setting behavior This can be achieved using a template - d.woolford Jan 2008 (before there wa...
Definition: emobject.h:569
bool has_key(const string &key) const
Ask the Dictionary if it as a particular key.
Definition: emobject.h:511
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
vector< float > calc_radial_dist(int n, float x0, float dx, int inten)
calculates radial distribution.
Definition: emdata.cpp:2781
static bool is_same_size(const EMData *image1, const EMData *image2)
Check whether two EMData images are of the same size.
Definition: emutil.cpp:1224
Factory is used to store objects to create new instances.
Definition: emobject.h:725
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:516
string get_name() const
Get the Averager's name.
Definition: averager.h:309
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:468
EMData * sigma_image
Definition: averager.h:203
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:289
EMData * normimage
Definition: averager.h:203
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:227
string get_name() const
Get the Averager's name.
Definition: averager.h:174
std::vector< EMData * > images
Definition: averager.h:292
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:697
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:690
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:555
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:583
std::vector< EMData * > images
Definition: averager.h:250
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:914
std::vector< EMData * > imgs
Definition: averager.h:479
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:928
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:899
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:855
string get_name() const
Get the Averager's name.
Definition: averager.h:408
EMData * normimage
Definition: averager.h:683
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:352
string get_name() const
Get the Averager's name.
Definition: averager.h:654
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:411
EMData * mean_image
Definition: averager.h:683
EMData * finish()
Finish up the averaging and return the result.
Definition: averager.cpp:173
string get_name() const
Get the Averager's name.
Definition: averager.h:358
void add_image(EMData *image)
To add an image to the Averager.
Definition: averager.cpp:99
EMData * norm_image
Definition: averager.h:389
static float eman_erfc(float x)
complementary error function.
Definition: util.h:1184
static float hypot3(int x, int y, int z)
Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.
Definition: util.h:827
static float hypot_fast(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
Definition: util.cpp:742
static float hypot2(float x, float y)
Euclidean distance function in 2D: f(x,y) = sqrt(x*x + y*y);.
Definition: util.h:774
XYData defines a 1D (x,y) data set.
Definition: xydata.h:47
float get_yatx(float x, bool outzero=true)
Definition: xydata.cpp:172
EMData * copy() const
This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata....
void to_zero()
Set all the pixel value = 0.
EMData * log() const
return natural logarithm image for a image
void mult(int n)
multiply an integer number to each pixel value of the image.
Definition: emdata_core.h:97
EMData * sqrt() const
return square root of current image
std::complex< float > get_complex_at(const int &x, const int &y) const
Get complex<float> value at x,y.
#define ImageDimensionException(desc)
Definition: exception.h:166
#define NullPointerException(desc)
Definition: exception.h:241
#define LOGERR
Definition: log.h:51
E2Exception class.
Definition: aligner.h:40
map< string, vector< string > > dump_averagers_list()
Definition: averager.cpp:1602
void dump_averagers()
Definition: averager.cpp:1597
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517