 |
Bsoft
2.1.0
Bernard's software package
|
Go to the documentation of this file.
41 if ( refset < 0 ) refset = 0;
44 long nmg, npart, nmg_max = 1000;
57 cout <<
"Aligning micrograph particle coordinates for series of micrographs." << endl;
59 cout <<
"Particles\tX-shift\tY-shift\tX-scale\tY-scale\tAngle" << endl;
62 for ( i=1, mg=field->
mg; mg->
next && i < refset; mg=mg->next ) i++;
66 for ( i=0, mg=field->
mg; mg; mg=mg->
next, i++ ) {
67 for ( part=mg->
part; part; part=part->
next ) {
69 if ( !set[i] ) set[i] = m;
75 for ( i=0, mg=field->
mg->
next; mg; mg=mg->
next, i++ )
if ( i != refset ) {
84 for ( i=0; i<nmg; i++ )
109 double res_lo,
double res_hi,
double max_shift,
int filter_flag,
int refine_flag)
111 long i, iter, maxiter(5);
120 common_size = common_size.
max(1);
123 tile_size = tile_size.
max(1);
124 tile_size = tile_size.
min(common_size);
125 if ( tile_size[0] >= common_size[0] && common_size[0] > 1 ) {
126 tile_size[0] = (int) (0.4*common_size[0]);
129 if ( tile_size[1] >= common_size[1] && common_size[1] > 1 ) {
130 tile_size[1] = (int) (0.4*common_size[1]);
133 if ( tile_size[2] >= common_size[2] && common_size[2] > 1 ) {
134 tile_size[2] = (int) (0.4*common_size[2]);
138 if ( max_shift < 1 ) max_shift = tile_size[0]/4;
141 half_tile[0] = (tile_size[0] - 1)/2;
142 half_tile[1] = (tile_size[1] - 1)/2;
143 half_tile[2] = (tile_size[2] - 1)/2;
153 Bstring filename(fn1.base() +
"_" + fn2.
base() +
".ps");
156 cout <<
"Aligning images ";
157 if ( refine_flag ) cout <<
"with ";
158 else cout <<
"without ";
159 cout <<
"shift refinement." << endl << endl;
162 if ( common_size[0] > 2*tile_size[0] )
163 start1[0] = (common_size[0]
164 - (int) (tile_size[0]*(common_size[0]/tile_size[0])))/2;
165 if ( common_size[1] > 2*tile_size[1] )
166 start1[1] = (common_size[1]
167 - (int) (tile_size[1]*(common_size[1]/tile_size[1])))/2;
168 if ( common_size[2] > 2*tile_size[2] )
169 start1[2] = (common_size[2]
170 - (int) (tile_size[2]*(common_size[2]/tile_size[2])))/2;
173 cout <<
"Common size: " << common_size << endl;
174 cout <<
"Tile size: " << tile_size << endl;
175 cout <<
"Maximum shift: " << max_shift << endl;
176 cout <<
"Reference start: " << start1 << endl << endl;
179 if ( res_hi < 0.1 || res_hi > tile_size[0]*p1->
sampling(0)[0] )
187 pex1 = p1->
extract_tiles(0, start1, common_size, tile_size, step_size, 0);
190 vector<Vector3<long>> tile_start(pex1->
images());
192 for ( i=0; i<pex1->
images(); i++ ) {
195 if ( !set1 ) set1 = m1;
196 if ( !set2 ) set2 = m2;
203 for ( iter=0; iter<maxiter && best_t.
fom>0.1; iter++ ) {
206 pex1->
find_shift(pex2, NULL, res_hi, res_lo, max_shift, 0, refine_flag);
207 for ( i=0, m2=set2; i<pex1->
images(); i++, m2=m2->
next ) {
209 if ( shift[0] > half_tile[0] ) shift[0] -= tile_size[0];
210 if ( shift[1] > half_tile[1] ) shift[1] -= tile_size[1];
211 if ( shift[2] > half_tile[2] ) shift[2] -= tile_size[2];
213 m2->loc = tile_start[i] + half_tile - shift;
221 if ( best_t.
fom > t.
fom ) best_t = t;
223 for ( i=0, m1=set1, m2=set2; i<pex1->
images(); i++, m1=m1->
next, m2=m2->
next ) {
224 m2->loc[0] = best_t.
scale[0]*(m1->loc[0]*cos(best_t.
angle) -
225 m1->loc[1]*sin(best_t.
angle)) + t.
trans[0] + 0.5;
226 m2->loc[1] = best_t.
scale[1]*(m1->loc[0]*sin(best_t.
angle) +
227 m1->loc[1]*cos(best_t.
angle)) + t.
trans[1] + 0.5;
228 m2->loc[2] = best_t.
scale[2]*m1->loc[2] + t.
trans[2];
229 tile_start[i] = m2->loc - half_tile;
235 cout <<
"Best fit:\t" <<
263 for ( n=0; n<p->
images(); n++ ) {
264 dose += dose_per_frame;
267 cout <<
"Adding frame " << n << endl;
271 (*pa)[
"dose"] = dose;
280 if ( var < ns*ns*var1 ) snr = (var - ns*var1)/(ns*var1 - var/ns);
321 double max_shift,
int filter_flag,
int refine_flag)
334 cout <<
"Aligning micrographs for micrograph series ";
335 if ( refine_flag ) cout <<
"with ";
336 else cout <<
"without ";
337 cout <<
"shift refinement." << endl << endl;
341 for ( i=1, mg = mg_ref = field->
mg; mg->
next && i < refset; mg=mg->next, i++ ) ;
342 if ( mg ) mg_ref = mg;
345 cerr <<
"Error in mg_align_micrographs: File " << mg_ref->
fmg <<
" not read!" << endl;
353 for ( mg=field->
mg; mg; mg=mg->
next ) {
354 if ( mg_ref != mg ) {
357 cerr <<
"Error in mg_align_micrographs: File " << mg->
fmg <<
" not read!" << endl;
365 t =
img_align(p, p2, tile_size, res_lo, res_hi, max_shift, filter_flag, refine_flag);
407 int target_num,
double threshold)
411 if ( threshold < p->average() ) sign = -1;
413 long i, j, x, y, z, n;
419 cout <<
"Using the ";
420 switch ( extract_method ) {
421 case 1: cout <<
"center of mass ";
break;
422 case 2: cout <<
"center of volume ";
break;
423 case 3: cout <<
"highest pixel density ";
break;
424 default: cout <<
"center of mass ";
break;
426 cout <<
"to determine the feature coordinates in " << p->
file_name() << endl;
431 count = (long) pmask->
maximum();
434 cout <<
"Features extracted: " << count << endl << endl;
445 for ( i=0; i<count; i++ ) {
453 for ( i=n=0; n<p->
images(); n++ ) {
454 for ( z=0; z<p->
sizeZ(); z++ ) {
456 for ( y=0; y<p->
sizeY(); y++ ) {
458 for ( x=0; x<p->
sizeX(); x++, i++ ) {
460 if ( (*pmask)[i] > 0 ) {
461 j = (long) ((*pmask)[i]-1);
464 temp = fabs((*p)[i]);
465 if ( extract_method == 1 ) {
466 m->
loc += coor * temp;
468 }
else if ( extract_method == 2 ) {
472 if ( sign*(*p)[i] > sign*m->
fom ) {
474 if ( extract_method == 3 ) {
488 cout <<
"Feature marker list generated" << endl;
493 cout <<
"Feature\tx\ty\tz\tex\tey\tez\tFOM" << endl;
495 for ( m=set; m; m=m->
next ) {
496 if ( extract_method == 1 || extract_method == 2 )
507 cout <<
"\nDEBUG img_extract_features: The Feature set" << endl;
508 for ( m=set; m; m=m->
next )
509 cout <<
"Feature " << m->
id <<
": " << m->
loc <<
tab << m->
fom << endl;
512 filename = filename.
base() +
"_features.pif";
546 double res_low,
double res_high,
double thresh,
int extract_method)
548 long i, refset(-1), nmg;
559 if ( nmg_max < 5 ) nmg_max = 5;
566 cout <<
"Aligning micrographs for micrograph series using features." << endl;
570 if ( refset < 0 ) refset = 0;
571 for ( i=0, mg=field->
mg; mg; mg=mg->
next, i++ ) {
572 if ( mg->
part ) refset = i;
573 if ( i == refset ) mg_ref = mg;
579 width = 0.05*p->
sizeX();
581 origin = p->
size() * 0.1;
582 if ( origin[0] > p->
sizeX() - 1 ) origin[0] = p->
sizeX() - 1;
583 if ( origin[1] > p->
sizeY() - 1 ) origin[1] = p->
sizeY() - 1;
584 if ( origin[2] > p->
sizeZ() - 1 ) origin[2] = p->
sizeZ() - 1;
585 size[0] = (int) (p->
sizeX() - 2*origin[0]);
586 size[1] = (int) (p->
sizeY() - 2*origin[1]);
587 size[2] = (int) (p->
sizeZ() - 2*origin[2]);
588 if ( size[0] > p->
sizeX() ) size[0] = p->
sizeX();
589 if ( size[1] > p->
sizeY() ) size[1] = p->
sizeY();
590 if ( size[2] > p->
sizeZ() ) size[2] = p->
sizeZ();
594 if ( set[i] == NULL) {
595 error_show(
"Error in mg_align_feature_extraction", __FILE__, __LINE__);
596 cerr <<
"Unable to extract any features from " << p->
file_name() << endl;
604 error_show(
"Error in mg_align_feature_extraction", __FILE__, __LINE__);
605 cerr <<
"Transformation parameters not found!" << endl;
608 for ( i=0, mg=field->
mg, t=tlist; mg; mg=mg->
next, t=t->
next, i++ )
if ( i != refset ) {
614 for ( i=0, mg=field->
mg; mg; mg=mg->
next, i++ )
636 if ( mg_ref == mg_apply )
return 0;
654 double cos_ang = cos(angle), sin_ang = sin(angle);
665 cout <<
"Applying transform parameters to micrographs:" << endl;
666 cout <<
"Reference micrograph: " << mg_ref->
id << endl;
667 cout <<
"Transform micrograph: " << mg_apply->
id << endl;
668 cout <<
"Shift: " << shift << endl;
669 cout <<
"Scale: " << scale << endl;
670 cout <<
"Rotation angle: " << angle*180.0/
M_PI << endl << endl;
672 cout <<
"Applying transform parameters to micrographs" << endl << endl;
680 if ( npapp != npref ) {
682 mg_apply->
part = part_app = NULL;
683 for ( part_ref=mg_ref->
part; part_ref; part_ref=part_ref->
next ) {
685 if ( !mg_apply->
part ) mg_apply->
part = part_app;
689 if ( mg_ref->
part ) {
690 for ( part_ref=mg_ref->
part, part_app=mg_apply->
part; part_ref;
691 part_ref=part_ref->
next, part_app=part_app->
next ) {
692 part_app->
id = part_ref->
id;
693 loc = scale*(part_ref->
loc + shift);
694 part_app->
loc[0] = loc[0] * cos_ang - loc[1] * sin_ang;
695 part_app->
loc[1] = loc[0] * sin_ang + loc[1] * cos_ang;
699 if ( mg_apply->
bad ) {
701 mg_apply->
bad = NULL;
705 for ( bad_ref=mg_ref->
bad; bad_ref; bad_ref=bad_ref->
next ) {
707 loc = scale*(bad_ref->
loc + shift);
708 bad_app->
loc[0] = loc[0] * cos_ang - loc[1] * sin_ang;
709 bad_app->
loc[1] = loc[0] * sin_ang + loc[1] * cos_ang;
713 if ( nfapp != nfref ) {
715 mg_apply->
fil = fil_app = NULL;
716 for ( fil_ref=mg_ref->
fil; fil_ref; fil_ref=fil_ref->
next ) {
718 if ( !mg_apply->
fil ) mg_apply->
fil = fil_app;
719 fil_app->
node = fn_app = NULL;
720 for ( fn_ref = fil_ref->
node; fn_ref; fn_ref = fn_ref->
next ) {
722 if ( !fil_app->
node ) fil_app->
node = fn_app;
728 for ( fil_ref=mg_ref->
fil, fil_app=mg_apply->
fil; fil_ref;
729 fil_ref=fil_ref->
next, fil_app=fil_app->
next ) {
730 fil_app->
id = fil_ref->
id;
731 for ( fn_ref=fil_ref->
node, fn_app=fil_app->
node; fn_ref;
732 fn_ref=fn_ref->
next, fn_app=fn_app->
next ) {
733 fn_app->
id = fn_ref->
id;
734 loc = scale*(fn_ref->
loc + shift);
735 fn_app->
loc[0] = loc[0] * cos_ang - loc[1] * sin_ang;
736 fn_app->
loc[1] = loc[0] * sin_ang + loc[1] * cos_ang;
741 if ( mg_apply->
mark ) {
743 mg_apply->
mark = NULL;
746 if ( mg_ref->
mark ) {
747 for ( mark_ref=mg_ref->
mark; mark_ref; mark_ref=mark_ref->
next ) {
749 loc = scale*(mark_ref->
loc + shift);
750 mark_app->
loc[0] = loc[0] * cos_ang - loc[1] * sin_ang;
751 mark_app->
loc[1] = loc[0] * sin_ang + loc[1] * cos_ang;
752 mark_app->
sel = mark_ref->
sel;
753 mark_app->
fom = mark_ref->
fom;
788 for ( ifield = 0, field=
project->
field; field; field=field->
next, ifield++ ) {
790 for ( mg_ref = mg = field->
mg; mg; mg = mg->
next )
791 if ( mg_ref->
ctf && mg->
ctf )
795 return error_show(
"Error in mg_merge_focal_series", __FILE__, __LINE__);
799 for ( mg = field->
mg; mg; mg = mg->
next )
if ( mg != mg_ref ) {
802 return error_show(
"Error in mg_merge_focal_series", __FILE__, __LINE__);
827 for ( i=0, part = mg->
part, part_ref = mg_ref->
part; part;
828 i++, part = part->
next, part_ref = part_ref->
next ) {
830 if ( use_old_origins ) {
831 shift = part_ref->
ori - part->
ori;
846 filename = mg_ref->
fpart;
849 cout <<
"DEBUG mg_merge_focal_series: mg=" << mg_ref->
id <<
" filename=" << filename << endl;
852 for ( mg = field->
mg; mg; ) {
859 mg_ref->
block = ifield;
860 mg_ref->
fpart = filename;
879 for ( n=0, mg = field->
mg; mg; mg = mg->
next, n++ ) ;
884 for ( i=n=0, mg = field->
mg; mg; mg = mg->
next, i++ )
if ( numsel[i] ){
893 if ( !pavg ) pavg = p->
copy();
896 if ( !pstd ) pstd = p->
copy();
923 if ( imgfile.
length() && pavg ) {
966 for ( nmg=0, mg = field->
mg; mg; mg = mg->
next ) nmg++;
982 for ( i=0, mg = field->
mg; mg; mg = mg->
next, i++ ) {
987 origin = pone->
size()/2;
988 if ( origin != mg->
origin ) {
1004 if ( imgfile.
length() && paln )
1032 cout <<
"Writing micrograph average to " << imgfile << endl;
1034 cout <<
"Subset: " << subset << endl;
1047 dispatch_apply(nfield, dispatch_get_global_queue(0, 0), ^(
size_t i){
1057 #pragma omp parallel for
1058 for (
long i=0; i<nfield; i++ ) {
1072 cout <<
"Field\tSNR" << endl;
1075 snr_avg += field->
fom;
1078 cout << field->
id <<
tab << field->
fom << endl;
1081 if ( n ) snr_avg /= n;
1086 Bimage* pmask,
double hi_res,
double lo_res,
1087 double shift_limit,
double edge_width,
double gauss_width,
1101 double exposure(0), dose_per_frame(0);
1102 if ( dose_frac.
exists(
"exposure") )
1103 exposure = dose_frac[
"exposure"].
real();
1104 else if ( dose_frac.
exists(
"frame_rate") )
1105 exposure = p->
images()/dose_frac[
"frame_rate"].
real();
1107 if ( dose_frac.
exists(
"dose_per_frame") )
1108 dose_per_frame = dose_frac[
"dose_per_frame"].
real();
1112 cout <<
"Multiplying with " << pgr->
file_name() << endl;
1117 cout <<
"Aligning frames from micrograph " << mg->
id << endl;
1125 if ( bin.
volume() > 1 ) {
1133 JSvalue job = p->
align(ref_num, pmask, hi_res, lo_res, shift_limit,
1134 edge_width, gauss_width, aln_bin);
1139 double shift(job[
"shift_average"].real()*mg->
pixel_size[0]);
1146 for (
long i = 0; i<p->
images(); ++i ) {
1151 mg->
dose += dose_per_frame;
1156 cout <<
"Average shift per frame: " << shift <<
" A" << endl;
1158 cout <<
"Average shift rate: " <<
1159 (shift/exposure)*p->
images() <<
" A/s" << endl << endl;
1160 cout << job << endl;
1164 if ( ext.
contains(
"dm") ) ext =
"mrc";
1165 if ( ext.
contains(
"tif") ) ext =
"mrc";
1170 cout <<
"Writing aligned frames: " << mg->
fframe << endl;
1171 (*p)[
"dose"] = mg->
dose;
1181 mg->
dose = (*p1)[
"dose"].real();
1183 cout <<
"Writing aligned frame sum: " << mg->
fmg << endl;
1222 double shift_limit,
double edge_width,
double gauss_width,
1234 cout <<
"Aligning micrograph frames by cross-correlation:" << endl;
1235 cout <<
"Number of micrographs: " << nmg << endl;
1236 cout <<
"Reference frame: " << ref_img << endl;
1237 cout <<
"Resolution limits: " << hi_res <<
" - " << lo_res <<
" A" << endl;
1238 cout <<
"Shift limit: " << shift_limit << endl;
1239 cout <<
"Edge masking width & smoothing: " << edge_width <<
" " << gauss_width << endl;
1240 cout <<
"Binning: " << bin << endl;
1242 cout <<
"Gain reference file: " << pgr->
file_name() << endl;
1244 cout <<
"Using mask file: " << pmask->
file_name() << endl;
1245 if ( dose_frac.
exists(
"exposure") )
1246 cout <<
"Exposure time: " << dose_frac[
"exposure"].
real() <<
" s" << endl;
1247 if ( dose_frac.
exists(
"frame_rate") )
1248 cout <<
"Frame rate: " << dose_frac[
"frame_rate"].
real() <<
" frames/s" << endl;
1253 for ( mg = field->
mg; mg; mg = mg->
next, nmg++ ) {
1255 edge_width, gauss_width, bin, dose_frac, subset, flag);
1263 cout <<
"Overall average shift per frame: " << d << endl << endl;
1270 Bimage* pgr,
Bimage* pmask,
double hi_res,
double lo_res,
double shift_limit,
1271 double edge_width,
double gauss_width,
1274 if ( mg == mg_ref )
return 0;
1276 long bv(bin[0]*bin[1]*bin[2]);
1281 edge_size = edge_size.
max(1);
1289 if ( edge_width > 0 )
1296 if ( bv > 1 ) p->bin(bin);
1299 if ( bv > 1 ) shift *= obin;
1315 double shift_limit,
double edge_width,
double gauss_width,
1318 long i, j, nmg, nsel(0);
1321 for ( i=0, mg_ref = field->
mg; mg_ref && ( i<ref_img ); mg_ref = mg_ref->
next, i++ ) ;
1327 for ( nmg=0, mg = field->
mg; mg; mg = mg->
next ) nmg++;
1332 for ( i=nsel=0; i<nmg; i++ )
if ( numsel[i] ) nsel++;
1336 for ( i=j=0, mg = field->
mg; mg; mg = mg->
next, i++ )
1337 if ( numsel[i] ) mg_arr[j++] = mg;
1340 if ( origin.
length() < 1 ) origin = pref->
size()/2;
1348 edge_size = edge_size.
max(1);
1352 if ( edge_width > 0 )
1355 if ( bin[0]*bin[1]*bin[2] > 1 ) pref->bin(bin);
1357 if ( shift_limit < 0 ) shift_limit = pref->
sizeX()/10;
1360 cout <<
"Field " << field->
id << endl <<
"Micrograph\tdx\tdy\tdz\tCC" << endl;
1366 dispatch_apply(nsel, dispatch_get_global_queue(0, 0), ^(
size_t i){
1368 shift_limit, edge_width, gauss_width, bin,
planf,
planb);
1371 #pragma omp parallel for
1372 for (
long i=0; i<nsel; i++ )
1374 shift_limit, edge_width, gauss_width, bin,
planf,
planb);
1385 for ( nsel=i=0, mg = field->
mg; mg; mg = mg->
next, i++ )
if ( numsel[i] ) {
1387 cout << mg->
id <<
tab <<
1390 if ( mg != mg_ref ) {
1397 if ( nsel ) da /= nsel;
1400 cout <<
"Average shift per micrograph: " << da << endl;
1407 if ( ext.
contains(
"dm") ) ext =
"mrc";
1408 if ( ext.
contains(
"tif") ) ext =
"mrc";
1412 Bstring label(
"Written by bseries");
1448 double shift_limit,
double edge_width,
double gauss_width,
1460 cout <<
"Aligning micrographs in series by cross-correlation:" << endl;
1461 cout <<
"Number of images: " << nmg << endl;
1462 cout <<
"Resolution limits: " << hi_res <<
" - " << lo_res <<
" A" << endl;
1463 cout <<
"Shift limit: " << shift_limit << endl;
1464 cout <<
"Edge masking width & smoothing: " << edge_width <<
" " << gauss_width << endl;
1465 cout <<
"Binning: " << bin << endl;
1467 cout <<
"Using mask file: " << pmask->
file_name() << endl;
1469 cout <<
"Subset: " << subset << endl;
1476 shift_limit, edge_width, gauss_width, bin, subset, flag);
1483 cout <<
"Overall average shift per frame: " << d << endl << endl;
1499 error_show(
"Error in mg_tomo_reconstruct", __FILE__, __LINE__);
1500 cerr <<
"Micrograph number " << mg_id <<
" not found!" << endl << endl;
1506 cout <<
"Skipping micrograph number " << mg_id << endl << endl;
1510 if ( mg_max < mg_min || ( mg_max == mg_id && mg_min == mg_id ) ) {
1511 error_show(
"Error in mg_tomo_reconstruct", __FILE__, __LINE__);
1512 cerr <<
"Micrograph range is incorrect: " << mg_min <<
" - " << mg_max << endl << endl;
1517 error_show(
"Error in mg_tomo_reconstruct", __FILE__, __LINE__);
1518 cerr <<
"No micrograph image file name given for micrograph " << mg_res->
id << endl;
1522 if ( hi_res < 2*mg_res->pixel_size[0] ) hi_res = 2*mg_res->
pixel_size[0];
1525 cout <<
"DEBUG mg_tomo_reconstruct: size=" << size <<
" ft_size=" << ft_size << endl;
1534 long data_size = (long) prec->
size().
volume();
1536 long nrec(0), nmg(mg_max-mg_min), nover(0);
1541 for ( i=mg_min; i<=mg_max; i++ )
if ( mgarr[i]->select && i != mg_id ) {
1544 cout <<
"Reading image " << mg->
img_num <<
" (micrograph " << mg->
id <<
")" << endl;
1546 error_show(
"mg_tomo_reconstruct", __FILE__, __LINE__);
1568 cout <<
"Complete: " << nrec*100.0/nmg <<
"%\r" << flush;
1574 error_show(
"Error in mg_tomo_reconstruct: No micrographs used in this reconstruction!", __FILE__, __LINE__);
1581 cout <<
"Contributions: " << nover <<
" (" << nover*100.0/data_size <<
"%)" << endl;
1582 cout <<
"Weighing reconstruction. " << endl;
1585 for ( i=0; i<data_size; i++ ) {
1586 if ( fom[i] ) prec->
set(i, prec->
complex(i) / fom[i]);
1587 fom[i] = fom[i]/(fom[i] + 1);
1612 if ( scale < 0.1 ) scale = 1;
1619 if ( hi_res < 2*mg0->pixel_size[0] )
1623 for ( i=0; i<nmg; ++i )
1624 if ( fabs(mgarr[i]->tilt_angle) <= 15*
M_PI/180.0 )
1630 if ( size.
volume() < 1 ) size = p->
size()/scale;
1633 long ft_size = p->
sizeX();
1634 if ( ft_size < p->sizeY() ) ft_size = p->
sizeY();
1640 cout <<
"Reconstructing a 2D image:" << endl;
1641 cout <<
"Fourier transform size: " << ft_size <<
" x " << ft_size << endl;
1642 cout <<
"Reconstruction size: " << size << endl;
1643 cout <<
"Reconstruction scale: " << scale << endl;
1644 cout <<
"Number of adjacent images: " << dimg << endl;
1645 cout <<
"Resolution limit: " << hi_res <<
" A" << endl;
1664 long mg_id,
long dimg,
double resolution,
double shift_limit,
1672 if ( shift_limit < 0 ) shift_limit = size[0]/4;
1674 for ( imin=(mg_id>dimg)? mg_id-dimg: 0; mgarr[imin]->
fom < -0.5 && imin<=mg_id; imin++ ) ;
1676 for ( imax=(mg_id+dimg<nmg)? mg_id+dimg: nmg-1; mgarr[imax]->
fom < -0.5 && imax>=mg_id; imax-- ) ;
1684 cerr <<
"Error: Reconstruction for " << mg_id <<
" failed!" << endl;
1709 if ( edge_width > 0 ) {
1724 shift = mg_res->
origin - oldori;
1727 cout << mg_id <<
": " << imin <<
" - " << imax <<
tab <<
1765 long dimg,
double resolution,
double shift_limit,
double edge_width,
double gauss_width)
1770 error_show(
"Error in project_tomo_align ", __FILE__, __LINE__);
1771 cerr <<
"No micrograph image or transform file name given for micrograph "
1772 << mg0->
id << endl << endl;
1778 long ft_size = p->
sizeX();
1779 if ( ft_size < p->sizeY() ) ft_size = p->
sizeY();
1782 if ( thickness < 10 ) thickness = ft_size/5;
1786 if ( resolution < 2*mg0->pixel_size[0] ) resolution = 4*mg0->
pixel_size[0];
1788 long it, nmg(0), i, j, n, mg_id0(0);
1792 if ( shift_limit < 0 ) shift_limit = ft_size/4;
1795 for ( i=0; i<nmg; i++ ) {
1797 if ( mg0 == mgarr[i] ) mg_id0 = i;
1804 cout <<
"Aligning images in a tilt series by cross-correlation:" << endl;
1805 cout <<
"Maximum number of iterations: " << iter << endl;
1806 cout <<
"Number of images: " << nmg << endl;
1807 cout <<
"Fourier transform size: " << ft_size <<
" x " << ft_size << endl;
1808 cout <<
"Reconstruction size: " << size << endl;
1809 cout <<
"Number of adjacent images: " << dimg << endl;
1810 cout <<
"Resolution limit: " << resolution <<
" A" << endl;
1811 cout <<
"Shift limit: " << shift_limit <<
" pixels" << endl;
1812 cout <<
"Smoothing edge and width: " << edge_width <<
" " << gauss_width <<
" pixels" << endl;
1819 for ( it = 0; it < iter && dori > dchange; it++ ) {
1821 cout <<
"Iteration " << it+1 <<
":" << endl;
1822 cout <<
"Micrograph: range" <<
tab <<
"Origin" <<
tab <<
"Shift" <<
tab <<
"FOM" << endl;
1826 for ( i=mg_id0-1, j=mg_id0+1; i>=0 || j<nmg; i--, j++ ) {
1829 shift_limit, size, edge_width, gauss_width, ft_size,
planf,
planb);
1834 shift_limit, size, edge_width, gauss_width, ft_size,
planf,
planb);
1840 cout <<
"Iteration:\t" << it+1 <<
tab << dori << endl;
1849 cout <<
"\nImage\tdx\tdy\tdz" << endl;
1873 for ( i=0, mg = field->
mg; mg; mg = mg->
next, i++ ) {
1876 origin = p->
size()/2;
1877 if ( size.
volume() < 1 ) {
1879 if ( size[0] < size[1] ) size[1] = size[0];
1880 else size[0] = size[1];
1884 if ( !pavg ) pavg = ps->
copy();
1887 if ( !pstd ) pstd = ps->
copy();
1901 pavg->
divide(pstd, 1, 0);
1905 Bstring imgfile = field->
id +
"_snr.map";
1908 double rad_step(10), maxrad(size[0]/2);
1910 imgfile = field->
id +
"_snr.txt";
1932 cout <<
"Calculating the SSNR for each field:" << endl;
1946 for (
long i=0; i<nfield; i++ ) {
1955 cout <<
"Field\tSNR" << endl;
1957 cout << field->
id <<
tab << field->
fom << endl;
Bimage * regions(double threshold, int sign)
Segments an image into contiguous regions.
fft_plan fft_setup(fft_direction dir, int opt=0)
Sets up a plan for fast Fourier transforms.
void complex_to_intensities()
The intensities from a complex image is written to a simple image.
Header file for postscript tools dealing with Bmarkers.
long field_count_micrographs(Bfield *field)
Counts all the micrographs in the field-of-view structure.
double field_write_aligned_average(Bfield *field, Bimage *pgr, Bstring &imgfile, DataType datatype, Bstring &subset)
JSvalue align(long ref_num, Bimage *pmask, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin)
Aligns and sums a set of sub-images, first progressively and then iteratively.
long project_tomo_align(Bproject *project, long thickness, long iter, double dchange, long dimg, double resolution, double shift_limit, double edge_width, double gauss_width)
Aligns a series of micrographs by cross-correlation sequentially.
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
long filament_node_count(Bfilament *fil)
Counts the number of filament nodes in a linked list of filaments.
void set(long j, double v)
Sets a single value at the given index.
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Vector3< double > micrograph_get_nominal_origin(Bmicrograph *mg)
Gets the nominal origin for a micrograph.
double project_ssnr(Bproject *project)
int micrograph_kill(Bmicrograph *mg)
Frees a whole micrograph parameter structure.
double project_write_aligned_averages(Bproject *project, Bimage *pgr, Bstring &imgfile, DataType datatype, Bstring &subset)
Calculates aligned micrograph images and write them to a file.
Bimage * copy()
Copies the header information and data of an image into a new image structure.
int bexit(int value)
Exit function for cleanup and debugging.
int fspace_weigh_dose(long nn, double dose_per_frame, vector< double > critdose)
Bplot * histogram_counts(int flags=0)
Finds the peaks in a quantized image from the histogram.
Bfilament * filament_add(Bfilament **fil, int pid)
Adds a filament parameter structure to a linked list.
long micrograph_count_particles(Bmicrograph *mg)
Counts all the particles in the micrograph structure.
Header file for micrograph processing.
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Bimage * radial(long minrad, long maxrad, double rad_step=1, int wrap=0)
Calculates the radial average of an image.
Vector3< long > size() const
double project_align_frames(Bproject *project, int ref_img, Bimage *pgr, Bimage *pmask, Vector3< double > origin, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin, JSvalue &dose_frac, Bstring &subset, int flag)
Aligns a series of micrographs by cross-correlation.
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Bmicrograph ** project_micrograph_array(Bproject *project, long &nmg)
Sets up an array of selected micrograph pointers.
void view(double x, double y, double z, double a)
int find_shift(Bimage *pref, Bimage *pmask, double hires, double lores, double radius, double sigma, int refine_flag)
Calculates a cross-correlation map to find the shift for the pair of images.
double standard_deviation()
Bimage * mg_tomo_reconstruct2D(Bproject *project, long dimg, Vector3< long > size, double scale, double hi_res)
Reconstructs the zero-tilt image of a tilt-series.
Header file for image parameter functions.
double distance(const Vector3 &v)
double project_align_series(Bproject *project, int ref_img, Bimage *pgr, Bimage *pmask, Vector3< double > origin, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin, Bstring &subset, int flag)
Aligns a series of micrographs by cross-correlation.
int ps_marker_errors(Bstring &filename, Bmarker *set1, Bmarker *set2, Transform t, Vector3< long > size, double err_scale)
Output a postscript file that graphs the deviations of points in a Bmarker from their predicted locat...
double field_align_series(Bfield *field, int ref_img, Bimage *pgr, Bimage *pmask, Vector3< double > origin, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin, Bstring &subset, int flag)
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
void origin(double x, double y, double z)
void multiply(long j, Complex< double > cv)
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
void add(long j, double v)
string insert(string &filename, string ins)
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Bimage * mask_by_threshold(double threshold)
Generates a mask based on an image at a given threshold.
void divide(Bimage *p, double scale=1, double shift=0)
Divides the first image by the second.
General image parameter class.
double mg_reconstruct_align(Bmicrograph **mgarr, long nmg, long mg_id, long dimg, double resolution, double shift_limit, Vector3< long > size, double edge_width, double gauss_width, long ft_size, fft_plan planf, fft_plan planb)
CompoundType compound_type()
int shape(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Creates a shape in an image and fills it with a constant value.
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Bimage * extract_tiles(long nn, vector< Vector3< long >> &coords, Vector3< long > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
int replace(Bimage *img)
Replaces the data with that from the given image.
double mg_align_series(Bmicrograph *mg, Bmicrograph *mg_ref, Bimage *pref, Bimage *pgr, Bimage *pmask, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin, fft_plan planf, fft_plan planb)
Header file for image processing from micrograph structures.
Complex< double > complex(long j)
Returns a complex value at the given index.
Header file for image class.
int truncate_to_min_max(double minim, double maxim)
Truncates image data to a given minimum and maximum.
unsigned char * data_alloc_and_clear()
int mg_align_micrographs(Bproject *project, int refset, Vector3< long > tile_size, double res_lo, double res_hi, double max_shift, int filter_flag, int refine_flag)
Aligns a focal series of micrographs.
Bimage * img_sum_subset(Bimage *p, Bstring &subset, double dose_per_frame)
void subtract(Bimage *p)
Subtracts another image from an image.
int fspace_bandpass(double res_hi, double res_lo=0, double width=0)
Applies a bandpass filter to an image.
Header file for general utilities.
ostream & tab(ostream &out)
Transform * markers_map_and_find_transform(Bmarker **set, int nseries, int refset, Vector3< long > size)
This function finds the transform parameters between sets of coordinates. These sets are expected to ...
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
void origin(double ox, double oy, double oz)
Bmicrograph * field_find_zero_tilt_mg(Bfield *field)
Finds the micrograph closest to a zero degree tilt in a series.
long fspace_pack_2D_into_central_section(Bimage *p, long ft_size, double scale, double hi_res, double lo_res, Matrix3 matr, Matrix3 mat)
Packs a 2D Fourier transform into a central section of a 3D reciprocal space volume.
void change_type(char letter)
Get the data type indicated by a single letter code.
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
Header file for 2D and 3D image I/O.
Transform markers_find_rottrans(Bmarker *set1, Bmarker *set2, double tolerance)
Finds the rotation and shift between two sets of vectors.
void square()
Calculates the square of an image.
Header file for micrograph processing.
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
double project_write_aligned_images(Bproject *project, Bimage *pgr, Bstring &imgfile, DataType datatype)
Calculates aligned micrograph images and write them to a file.
void sampling(double x, double y, double z)
Header file for generalized linked list functions.
Bimage * mg_tomo_reconstruct(Bmicrograph **mgarr, long mg_id, long mg_min, long mg_max, double hi_res, double scale, Vector3< long > size, long ft_size, fft_plan planf, fft_plan planb)
unsigned char * data_pointer()
Bframe * frame_add(Bframe **frame, int pid)
Adds a movie frame parameter structure to a linked list.
int filament_kill(Bfilament *fil)
Frees a list of filaments.
Vector3< double > pixel_size
void sampling(long nn, Vector3< double > u)
DataType
Base data type specifier.
int phase_shift_to_origin()
Phase shifts a set of reflections to the image origin.
bool contains(const Bstring &s)
int mg_merge_focal_series(Bproject *project, int use_old_origins)
Merges corresponding particle images in each focal series.
int particle_kill(Bparticle *part)
Frees a list of particles.
long markers_limit(int target_num, int sign, Bmarker **set)
This method constricts a marker set to target_num points.
int mg_align_feature_extraction(Bproject *project, int max_features, double res_low, double res_high, double thresh, int extract_method)
Aligns images by feature extraction.
int edge(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Smooths the image edge with a soft rectangular function.
Bfilnode * filament_node_add(Bfilnode **fnode, int pid)
Adds a filament node parameter structure to a linked list.
Header file for functions to align micrographs or coordinates from micrographs and apply the resultan...
Transform img_align(Bimage *p1, Bimage *p2, Vector3< long > tile_size, double res_lo, double res_hi, double max_shift, int filter_flag, int refine_flag)
double field_ssnr(Bfield *field, fft_plan plan)
int phase_shift_to_center()
Phase shifts a set of reflections to the nominal center of the image origin.
Bmarker * img_extract_features(Bimage *p, int extract_method, int target_num, double threshold)
int select_numbers(Bstring &string, int n, int *numsel)
Converts a string with a selection specification into an integer array.
int findNextPowerOf(int startNumber, int powerOf)
Finds the next greatest number that is a power of a given number.
int mg_align_coordinates(Bproject *project, int refset)
Aligns the particle coordinates of a series of micrographs.
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
double mg_align_frames(Bmicrograph *mg, int ref_num, Bimage *pgr, Bimage *pmask, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin, JSvalue &dose_frac, Bstring &subset, int flag)
long statistics()
Calculates the statistics for an image.
int mg_apply_transform(Bmicrograph *mg_ref, Bmicrograph *mg_apply)
Applies a transformation to particle coordinates in a reference micrograph and writes them into an ap...
int filter_extremes()
Filters the extremes out of a micrograph image.
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.