-
Notifications
You must be signed in to change notification settings - Fork 193
/
healpix_sampling.cpp
2827 lines (2540 loc) · 99.4 KB
/
healpix_sampling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
*
* Author: "Sjors H.W. Scheres"
* MRC Laboratory of Molecular Biology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This complete copyright notice must be included in any revised version of the
* source code. Additional authorship citations may be added, but existing
* author citations must be preserved.
***************************************************************************/
#include "src/healpix_sampling.h"
//#define DEBUG_SAMPLING
//#define DEBUG_CHECKSIZES
//#define DEBUG_HELICAL_ORIENTATIONAL_SEARCH
void HealpixSampling::clear()
{
is_3D = false;
isRelax = false;
fn_sym = "C1";
fn_sym_relax = "C1";
limit_tilt = psi_step = offset_range = offset_step = helical_offset_step = psi_step_ori = offset_range_ori = offset_step_ori = 0.;
random_perturbation = perturbation_factor = 0.;
// Jun19,2015 - Shaoda, Helical refinement
helical_offset_step = -1.;
directions_ipix.clear();
rot_angles.clear();
tilt_angles.clear();
psi_angles.clear();
translations_x.clear();
translations_y.clear();
translations_z.clear();
L_repository.clear();
R_repository.clear();
L_repository_relax.clear();
R_repository_relax.clear();
pgGroup = pgOrder = 0;
pgGroupRelaxSym = pgOrderRelaxSym = 0;
}
void HealpixSampling::initialise(
int ref_dim,
bool do_3d_trans,
bool do_changepsi,
bool do_warnpsi,
bool do_local_searches_helical,
bool do_helical_refine,
RFLOAT rise_Angst,
RFLOAT twist_deg)
{
if (ref_dim != -1)
is_3D = (ref_dim == 3);
// Set the symmetry relaxation flag
isRelax = fn_sym_relax == "" ? false : true;
// Set flag for x,y,z-translations
is_3d_trans = do_3d_trans;
// By default psi_step is approximate sampling of rot,tilt in 3D; and 10 degrees in 2D
if (psi_step < 0)
{
if (is_3D)
psi_step = 360. / (6 * ROUND(std::pow(2., healpix_order)));
else
psi_step = 10.;
}
if (perturbation_factor < 0. || perturbation_factor > 1.)
REPORT_ERROR("HealpixSampling::initialise: random perturbation factor should be between 0 and 1.");
if (is_3D)
{
healpix_base.Set(healpix_order, NEST);
// Set up symmetry
R_repository.clear();
L_repository.clear();
initialiseSymMats(fn_sym, pgGroup, pgOrder, R_repository, L_repository);
// Set up symmetry matrices for symmetry relax
if (fn_sym_relax != "")
{
if (fn_sym_relax[0] != 'C' && fn_sym_relax[0] != 'c')
REPORT_ERROR("Sorry, symmetry relaxation is currently available only for cyclic (Cn) point groups. For other symmetries, please see https://github.com/3dem/relion/issues/796.");
R_repository_relax.clear();
L_repository_relax.clear();
initialiseSymMats(fn_sym_relax, pgGroupRelaxSym, pgOrderRelaxSym, R_repository_relax, L_repository_relax);
}
}
else
{
int t_nr_psi = CEIL(360./psi_step);
if(t_nr_psi%32!=0 && do_changepsi)
{
// Force-adjust psi_step to be multiples of 32 (for efficient GPU calculations)
t_nr_psi = CEIL((float)t_nr_psi / 32.0)*32;
if (do_warnpsi)
std::cout << " + WARNING: Changing psi sampling rate (before oversampling) to " << 360./(RFLOAT)t_nr_psi << " degrees, for more efficient GPU calculations" << std::endl;
}
psi_step = 360./(RFLOAT)t_nr_psi;
fn_sym = "C1"; // This may not be set yet if restarting a 2D run....
}
// Store the not-oversampled translations, and make sure oversampled sampling is 1 pixel
//setTranslations();
// May06,2015 - Shaoda & Sjors, Helical translational searches
setTranslations(-1, -1, do_local_searches_helical, do_helical_refine, -1, rise_Angst, twist_deg);
// Store the non-oversampled projection directions
setOrientations(-1, -1.);
// Random perturbation and filling of the directions, psi_angles and translations vectors
resetRandomlyPerturbedSampling();
// SHWS 27feb2020: Set original sampling rates to allow 2D/3D classifications using coarser ones in earlier iterations
healpix_order_ori = healpix_order;
psi_step_ori = psi_step;
offset_range_ori = offset_range;
offset_step_ori = offset_step;
}
void HealpixSampling::initialiseSymMats(FileName fn_sym_, int & pgGroup_,
int & pgOrder_, std::vector <Matrix2D<RFLOAT> > & R_repository_,
std::vector <Matrix2D<RFLOAT> > & L_repository_)
{
// Set up symmetry
SymList SL;
SL.isSymmetryGroup(fn_sym_, pgGroup_, pgOrder_);
SL.read_sym_file(fn_sym_);
// Precalculate (3x3) symmetry matrices
Matrix2D<RFLOAT> L(4, 4), R(4, 4);
Matrix2D<RFLOAT> Identity(3,3);
Identity.initIdentity();
R_repository_.clear();
L_repository_.clear();
R_repository_.push_back(Identity);
L_repository_.push_back(Identity);
for (int isym = 0; isym < SL.SymsNo(); isym++)
{
SL.get_matrices(isym, L, R);
R.resize(3, 3);
L.resize(3, 3);
R_repository_.push_back(R);
L_repository_.push_back(L);
}
}
void HealpixSampling::resetRandomlyPerturbedSampling()
{
// Actual instance of random perturbation
// Add to the random perturbation from the last iteration, so it keeps changing strongly...
random_perturbation += rnd_unif(0.5*perturbation_factor, perturbation_factor);
random_perturbation = realWRAP(random_perturbation, -perturbation_factor, perturbation_factor);
}
void HealpixSampling::read(FileName fn_in)
{
// Open input file
std::ifstream in(fn_in.data(), std::ios_base::in);
if (in.fail())
REPORT_ERROR( (std::string) "HealpixSampling::readStar: File " + fn_in + " cannot be read." );
MetaDataTable MD;
// Read general stuff
MD.readStar(in, "sampling_general");
in.close();
if (!MD.getValue(EMDL_SAMPLING_IS_3D, is_3D) ||
!MD.getValue(EMDL_SAMPLING_IS_3D_TRANS, is_3d_trans) ||
!MD.getValue(EMDL_SAMPLING_PSI_STEP, psi_step) ||
!MD.getValue(EMDL_SAMPLING_OFFSET_RANGE, offset_range) ||
!MD.getValue(EMDL_SAMPLING_OFFSET_STEP, offset_step) ||
!MD.getValue(EMDL_SAMPLING_PERTURBATION_FACTOR, perturbation_factor))
REPORT_ERROR("HealpixSampling::readStar: incorrect sampling_general table");
// Jun19,2015 - Shaoda, Helical translational searches, backward compatibility
if (!MD.getValue(EMDL_SAMPLING_HELICAL_OFFSET_STEP, helical_offset_step))
helical_offset_step = -1.;
// SHWS 27Feb2020: backwards compatibility: older star files will not yet have original sampling parameters, just use current ones
if (!MD.getValue(EMDL_SAMPLING_OFFSET_STEP_ORI, offset_step_ori)) offset_step_ori = offset_step;
if (!MD.getValue(EMDL_SAMPLING_OFFSET_RANGE_ORI, offset_range_ori)) offset_range_ori = offset_range;
if (!MD.getValue(EMDL_SAMPLING_PSI_STEP_ORI, psi_step_ori)) psi_step_ori = psi_step;
if (is_3D)
{
if (!MD.getValue(EMDL_SAMPLING_HEALPIX_ORDER, healpix_order) ||
!MD.getValue(EMDL_SAMPLING_SYMMETRY, fn_sym) ||
!MD.getValue(EMDL_SAMPLING_LIMIT_TILT, limit_tilt) )
REPORT_ERROR("HealpixSampling::readStar: incorrect sampling_general table for 3D sampling");
// For 3D samplings reset psi_step to -1:
// By default it will then be set to the healpix sampling
// Only if the --psi_step option is given on the command line it will be set to something different!
psi_step = -1.;
// SHWS 27Feb2020: backwards compatibility: older star files will not yet have original sampling parameters, just use current ones
if (!MD.getValue(EMDL_SAMPLING_HEALPIX_ORDER_ORI, healpix_order_ori)) healpix_order_ori = healpix_order;
}
else
{
fn_sym = "irrelevant";
limit_tilt = 0.;
healpix_order = 0;
}
}
void HealpixSampling::write(FileName fn_out)
{
MetaDataTable MD;
std::ofstream fh;
FileName fn_tmp;
fn_tmp = fn_out + "_sampling.star";
fh.open((fn_tmp).c_str(), std::ios::out);
if (!fh)
REPORT_ERROR( (std::string)"HealpixSampling::write: Cannot write file: " + fn_tmp);
MD.setIsList(true);
MD.addObject();
MD.setName("sampling_general");
MD.setValue(EMDL_SAMPLING_IS_3D, is_3D);
MD.setValue(EMDL_SAMPLING_IS_3D_TRANS, is_3d_trans);
if (is_3D)
{
MD.setValue(EMDL_SAMPLING_HEALPIX_ORDER, healpix_order);
MD.setValue(EMDL_SAMPLING_SYMMETRY, fn_sym);
MD.setValue(EMDL_SAMPLING_LIMIT_TILT, limit_tilt);
}
MD.setValue(EMDL_SAMPLING_PSI_STEP, psi_step);
MD.setValue(EMDL_SAMPLING_OFFSET_RANGE, offset_range);
MD.setValue(EMDL_SAMPLING_OFFSET_STEP, offset_step);
// Jun19,2015 - Shaoda, Helical translational searches
MD.setValue(EMDL_SAMPLING_HELICAL_OFFSET_STEP, helical_offset_step);
MD.setValue(EMDL_SAMPLING_PERTURB, random_perturbation);
MD.setValue(EMDL_SAMPLING_PERTURBATION_FACTOR, perturbation_factor);
//27Feb2020 SHWS: write original sampling rates to allow 2D/3D classifications to use coarser ones in initial iterations
MD.setValue(EMDL_SAMPLING_HEALPIX_ORDER_ORI, healpix_order_ori);
MD.setValue(EMDL_SAMPLING_PSI_STEP_ORI, psi_step_ori);
MD.setValue(EMDL_SAMPLING_OFFSET_RANGE_ORI, offset_range_ori);
MD.setValue(EMDL_SAMPLING_OFFSET_STEP_ORI, offset_step_ori);
MD.write(fh);
// In the 3D case, also write a table with the sampled rot, tilt angles
if (is_3D)
{
MD.clear();
MD.setIsList(false);
MD.setName("sampling_directions");
for (long int idir = 0; idir < NrDirections(); idir++)
{
RFLOAT rot, tilt;
getDirection(idir, rot, tilt);
MD.addObject();
MD.setValue(EMDL_ORIENT_ROT, rot);
MD.setValue(EMDL_ORIENT_TILT, tilt);
}
MD.write(fh);
}
// Close the file
fh.close();
}
void HealpixSampling::setTranslations(
RFLOAT new_offset_step,
RFLOAT new_offset_range,
bool do_local_searches_helical,
bool do_helical_refine,
RFLOAT new_helical_offset_step,
RFLOAT helical_rise_Angst,
RFLOAT helical_twist_deg)
{
// Ordinary single particles
int maxp; // Max half nr samplings in all directions
RFLOAT xoff, yoff, zoff, max2, old_offset_step, old_offset_range; // Offset lengths
// Helical refinement
int maxh; // Max half nr samplings in along helical axis
RFLOAT h_range, old_helical_offset_step; // Translations along helical axis
// Check old and new offsets
old_offset_step = offset_step; // can be < 0 ??????
old_offset_range = offset_range; // can be < 0 ??????
old_helical_offset_step = helical_offset_step; // can be < 0
if ( (new_offset_step > 0.) && (new_offset_range >= 0.) )
{
offset_step = new_offset_step;
offset_range = new_offset_range;
}
else
{
if (!(offset_step > 0.))
{
std::cerr << " offset_range= " << offset_range << " offset_step= " << offset_step << std::endl;
REPORT_ERROR("HealpixSampling::setTranslations BUG %% Trying to set translations with uninitialised offset_step!");
}
}
// Sometimes new offsets are set to -1, that means the old offsets remain unchanged.
new_offset_step = offset_step; // > 0
new_offset_range = offset_range; // >= 0
// Ordinary single particles
maxp = CEIL(offset_range / offset_step); // Perpendicular to helical axis (P1, P2)
maxh = maxp;
// Helical refinement
if (do_helical_refine)
{
// Assume all helical parameters are valid (this should be checked before in ml_optimiser.cpp)
helical_rise_Angst = fabs(helical_rise_Angst);
helical_twist_deg = fabs(helical_twist_deg);
// Search range (half) along helical axis = (-0.5 * rise, +0.5 * rise)
h_range = (helical_rise_Angst / 2.);
// If continue from old run or new offset is not applicable...
if (new_helical_offset_step < 0.)
new_helical_offset_step = old_helical_offset_step;
// New_helical_offset_step is not OK.
// (1) negative value
// (2) larger than before (if there is a valid offset before)
// (3) larger than new_offset_step
// (4) samplings along helical axis is less than 3
if ( (new_helical_offset_step < 0.)
|| ( (new_helical_offset_step > old_helical_offset_step) && (old_helical_offset_step > 0.) )
|| (new_helical_offset_step > new_offset_step)
|| ((helical_rise_Angst / new_helical_offset_step) < 3.) )
{
// First try 'new_offset_step'
new_helical_offset_step = new_offset_step;
// Change to 'old_helical_offset_step' if the old is smaller
if ( (old_helical_offset_step > 0.) && (new_helical_offset_step > old_helical_offset_step) )
new_helical_offset_step = old_helical_offset_step;
// New_helical_offset_step should be finer than 1/3 the helical rise
if ( (helical_rise_Angst / new_helical_offset_step) < 3.)
new_helical_offset_step = helical_rise_Angst / 3.;
}
maxh = CEIL(h_range / new_helical_offset_step); // Out of range samplings will be excluded next
if (do_local_searches_helical) // Local searches along helical axis
{
// Local searches (2*2+1=5 samplings)
if (maxh > 2)
maxh = 2;
// New helical offset step is smaller than 1/3 of the old one, samplings should be increased.
if ( (old_helical_offset_step > 0.) && ((old_helical_offset_step / new_helical_offset_step) > 3) )
maxh = FLOOR(old_helical_offset_step / new_helical_offset_step); // Use FLOOR here!
// Local searches should not be wider than 1/3 of the helical rise
if ( ((new_helical_offset_step * maxh) > (helical_rise_Angst / 6.)) )
{
maxh = FLOOR(helical_rise_Angst / 6. / new_helical_offset_step); // Use FLOOR here!
if (maxh < 1) // At least we should do some searches...
maxh = 1;
}
}
// DEBUG - this should not happen
if (maxh < 0)
maxh = 0;
helical_offset_step = new_helical_offset_step;
}
// DEBUG
if ( (maxh < 0) || (maxp < 0) )
{
std::cerr << "maxh= " << maxh << " maxp= " << maxp << std::endl;
REPORT_ERROR("HealpixSampling::setTranslations BUG %% No translations to set! ('maxh' or 'maxp' < 0)");
}
translations_x.clear();
translations_y.clear();
translations_z.clear();
for (long int ix = -maxh; ix <= maxh; ix++)
{
// For helices use a different step size along helical axis X
xoff = (do_helical_refine) ? (ix * helical_offset_step) : (ix * offset_step);
// For helical refinement, exclude xoff outside the range of (-0.5 * rise, +0.5 * rise)
if ( (do_helical_refine) && (ix != 0) && (fabs(xoff) > fabs(helical_rise_Angst / 2.)) )
continue;
for (long int iy = -maxp; iy <= maxp; iy++)
{
yoff = iy * offset_step;
// For helices do not limit translations along helical axis X
max2 = (do_helical_refine) ? (yoff * yoff) : (xoff * xoff + yoff * yoff);
if (is_3d_trans)
{
for (long int iz = -maxp; iz <= maxp; iz++)
{
zoff = iz * offset_step;
if ((max2 + zoff * zoff) <= (offset_range * offset_range))
{
translations_y.push_back(yoff);
if (do_helical_refine) // Z axis corresponds to the helical axis in 3D subtomogram averaging !!!
{
translations_x.push_back(zoff);
translations_z.push_back(xoff);
}
else
{
translations_x.push_back(xoff);
translations_z.push_back(zoff);
}
}
}
}
else
{
if (max2 < (offset_range * offset_range) + 0.001) // +0.001 prevent precision errors in relion-3.1
{
translations_x.push_back(xoff);
translations_y.push_back(yoff);
}
}
}
}
#ifdef DEBUG_SETTRANS
std::cerr << " is_3d_trans= " << is_3d_trans << std::endl;
for (int i = 0; i < translations_x.size(); i++)
std::cerr << " translations_x[i]= " << translations_x[i] << std::endl;
#endif
return;
}
/* Set only a single translation */
void HealpixSampling::addOneTranslation(
RFLOAT offset_x,
RFLOAT offset_y,
RFLOAT offset_z,
bool do_clear,
bool do_helical_refine,
RFLOAT rot_deg,
RFLOAT tilt_deg,
RFLOAT psi_deg)
{
if (do_clear)
{
translations_x.clear();
translations_y.clear();
translations_z.clear();
}
if (do_helical_refine)
transformCartesianAndHelicalCoords(offset_x, offset_y, offset_z, offset_x, offset_y, offset_z, rot_deg, tilt_deg, psi_deg, ((is_3d_trans) ? (3) : (2)), CART_TO_HELICAL_COORDS);
translations_x.push_back(offset_x);
translations_y.push_back(offset_y);
if (is_3d_trans)
translations_z.push_back(offset_z);
}
void HealpixSampling::setOrientations(int _order, RFLOAT _psi_step)
{
// Initialise
directions_ipix.clear();
rot_angles.clear();
tilt_angles.clear();
psi_angles.clear();
if (_order >= 0)
healpix_order = _order;
// Setup the HealPix object
// For adaptive oversampling only precalculate the COARSE sampling!
if (_order >= 0)
healpix_base.Set(_order, NEST);
// 3D directions
if (is_3D)
{
RFLOAT rot, tilt;
for (long int ipix = 0; ipix < healpix_base.Npix(); ipix++)
{
getDirectionFromHealPix(ipix, rot, tilt);
// Push back as Matrix1D's in the vectors
rot_angles.push_back(rot);
tilt_angles.push_back(tilt);
directions_ipix.push_back(ipix);
}
//#define DEBUG_SAMPLING
#ifdef DEBUG_SAMPLING
writeAllOrientationsToBild("orients_all.bild", "1 0 0 ", 0.020);
#endif
// Now remove symmetry-related pixels if not relaxing symmetry
// TODO check size of healpix_base.max_pixrad
if (!isRelax)
removeSymmetryEquivalentPoints(0.5 * RAD2DEG(healpix_base.max_pixrad()));
#ifdef DEBUG_SAMPLING
writeAllOrientationsToBild("orients_sym.bild", "0 1 0 ", 0.021);
#endif
// Also remove limited tilt angles
removePointsOutsideLimitedTiltAngles();
#ifdef DEBUG_SAMPLING
if (ABS(limit_tilt) < 90.)
writeAllOrientationsToBild("orients_tilt.bild", "1 1 0 ", 0.022);
#endif
}
else
{
rot_angles.push_back(0.);
tilt_angles.push_back(0.);
directions_ipix.push_back(-1);
}
// 2D in-plane angles
// By default in 3D case: use more-or-less same psi-sampling as the 3D healpix object
// By default in 2D case: use 5 degree
if (_psi_step > 0.)
psi_step = _psi_step;
int nr_psi = CEIL(360./psi_step);
RFLOAT psi;
psi_step = 360./(RFLOAT)nr_psi;
for (int ipsi = 0; ipsi < nr_psi; ipsi++)
{
psi = ipsi * psi_step;
psi_angles.push_back(psi);
}
//#define DEBUG_SAMPLING
#ifdef DEBUG_SAMPLING
writeAllOrientationsToBild("orients_final.bild", "1 0 0 ", 0.020);
#endif
}
/* Set only a single orientation */
void HealpixSampling::addOneOrientation(RFLOAT rot, RFLOAT tilt, RFLOAT psi, bool do_clear)
{
if (do_clear)
{
directions_ipix.clear();
rot_angles.clear();
tilt_angles.clear();
psi_angles.clear();
}
// 3D directions
if (is_3D)
{
rot_angles.push_back(rot);
tilt_angles.push_back(tilt);
directions_ipix.push_back(-1);
}
else
{
rot_angles.push_back(0.);
tilt_angles.push_back(0.);
directions_ipix.push_back(-1);
}
// in-plane rotation
psi_angles.push_back(psi);
}
void HealpixSampling::writeAllOrientationsToBild(FileName fn_bild, std::string rgb, RFLOAT size)
{
std::ofstream out;
out.open (fn_bild.c_str());
if (!out)
REPORT_ERROR( (std::string)"HealpixSampling::writeAllOrientationsToBild: Cannot write file: " + fn_bild);
out << ".color 1 0 0 \n";
out << ".arrow 0 0 0 1 0 0 0.01 \n";
out << ".color 0 1 0 \n";
out << ".arrow 0 0 0 0 1 0 0.01 \n";
out << ".color 0 0 1 \n";
out << ".arrow 0 0 0 0 0 1 0.01 \n";
Matrix1D<RFLOAT> v(3);
out << ".color " << rgb << std::endl;
for (unsigned long int ipix = 0; ipix < rot_angles.size(); ipix++)
{
Euler_angles2direction(rot_angles[ipix], tilt_angles[ipix], v);
out << ".sphere " << XX(v) << " " << YY(v) << " " << ZZ(v) << " " << floatToString(size) << std::endl;
}
out.close();
}
void HealpixSampling::writeNonZeroPriorOrientationsToBild(FileName fn_bild, RFLOAT rot_prior, RFLOAT tilt_prior,
std::vector<int> &pointer_dir_nonzeroprior, std::string rgb, RFLOAT size)
{
std::ofstream out;
out.open (fn_bild.c_str());
if (!out)
REPORT_ERROR( (std::string)"HealpixSampling::writeNonZeroOrientationsToBild: Cannot write file: " + fn_bild);
out << ".color 1 0 0 \n";
out << ".arrow 0 0 0 1 0 0 0.01 \n";
out << ".color 0 1 0 \n";
out << ".arrow 0 0 0 0 1 0 0.01 \n";
out << ".color 0 0 1 \n";
out << ".arrow 0 0 0 0 0 1 0.01 \n";
Matrix1D<RFLOAT> v(3);
Euler_angles2direction(rot_prior, tilt_prior, v);
out << ".color 1 0 0 \n";
out << ".sphere " << XX(v) << " " << YY(v) << " " << ZZ(v) << " " << floatToString(size) << std::endl;
out << ".color " << rgb << std::endl;
for (unsigned long int ipix = 0; ipix < pointer_dir_nonzeroprior.size(); ipix++)
{
long int idir = pointer_dir_nonzeroprior[ipix];
Euler_angles2direction(rot_angles[idir], tilt_angles[idir], v);
out << ".sphere " << XX(v) << " " << YY(v) << " " << ZZ(v) << " " << floatToString(size) << std::endl;
}
out.close();
}
RFLOAT HealpixSampling::calculateDeltaRot(Matrix1D<RFLOAT> my_direction, RFLOAT rot_prior)
{
// Rotate the x,y-components of the direction, according to rot-prior
Matrix1D< RFLOAT > my_rot_direction;
Matrix2D< RFLOAT > A;
rotation2DMatrix(rot_prior, A);
my_rot_direction = A.inv() * my_direction;
// Get component along the new Y-axis
return fabs(ASIND(my_rot_direction(1)));
}
void HealpixSampling::selectOrientationsWithNonZeroPriorProbability(
RFLOAT prior_rot, RFLOAT prior_tilt, RFLOAT prior_psi,
RFLOAT sigma_rot, RFLOAT sigma_tilt, RFLOAT sigma_psi,
std::vector<int> &pointer_dir_nonzeroprior, std::vector<RFLOAT> &directions_prior,
std::vector<int> &pointer_psi_nonzeroprior, std::vector<RFLOAT> &psi_prior,
bool do_bimodal_search_psi,
RFLOAT sigma_cutoff, RFLOAT sigma_tilt_from_ninety, RFLOAT sigma_psi_from_zero)
{
pointer_dir_nonzeroprior.clear();
directions_prior.clear();
// Do not check the mates again
std::vector<bool> idir_flag(rot_angles.size(), false);
if (is_3D)
{
//std::cerr<<"sigma_rot "<<sigma_rot<<" sigma_tilt "<<sigma_tilt<<std::endl;
Matrix1D<RFLOAT> prior90_direction;
if (sigma_tilt_from_ninety > 0.)
{
// pre-calculate original (0,90) direction
Euler_angles2direction(0., 90., prior90_direction);
}
// Loop over all directions
RFLOAT sumprior = 0.;
RFLOAT sumprior_withsigmafromzero = 0.;
// Keep track of the closest distance to prevent 0 orientations
RFLOAT best_ang = 9999.;
long int best_idir = -999;
for (long int idir = 0; idir < rot_angles.size(); idir++)
{
// Check if this direction was met before as symmetry mate
if (idir_flag[idir] == true)
continue;
bool is_nonzero_pdf = false;
// Any prior involving BOTH rot and tilt.
if ( (sigma_rot > 0.) && (sigma_tilt > 0.) )
{
// Get the direction of the prior
Matrix1D<RFLOAT> prior_direction, my_direction, sym_direction, best_direction;
Euler_angles2direction(prior_rot, prior_tilt, prior_direction);
// Get the current direction in the loop
Euler_angles2direction(rot_angles[idir], tilt_angles[idir], my_direction);
best_direction = my_direction;
// Loop over all symmetry operators to find the operator that brings this direction nearest to the prior if no symmetry relaxation
if (!isRelax)
{
RFLOAT best_dotProduct = dotProduct(prior_direction, my_direction);
for (int j = 0; j < R_repository.size(); j++)
{
sym_direction = L_repository[j] * (my_direction.transpose() * R_repository[j]).transpose();
RFLOAT my_dotProduct = dotProduct(prior_direction, sym_direction);
if (my_dotProduct > best_dotProduct)
{
best_direction = sym_direction;
best_dotProduct = my_dotProduct;
}
}
}
// Now that we have the best direction, find the corresponding prior probability
RFLOAT diffang = ACOSD( dotProduct(best_direction, prior_direction) );
if (diffang > 180.)
diffang = ABS(diffang - 360.);
if (do_bimodal_search_psi && (diffang > 90.)) // KThurber
diffang = ABS(diffang - 180.); // KThurber
// Only consider differences within sigma_cutoff * sigma_rot
// TODO: If sigma_rot and sigma_tilt are not the same (NOT for helices)?
RFLOAT biggest_sigma = XMIPP_MAX(sigma_rot, sigma_tilt);
if (diffang < sigma_cutoff * biggest_sigma)
{
// TODO!!! If tilt is zero then any rot will be OK!!!!!
//std::cerr<<"Best direction index: "<<idir<<std::endl;
pointer_dir_nonzeroprior.push_back(idir);
RFLOAT prior = gaussian1D(diffang, biggest_sigma, 0.);
sumprior += prior;
if (isRelax)
{
idir_flag[idir] = true;
RFLOAT my_prior = prior / R_repository_relax.size();
directions_prior.push_back(my_prior);
findSymmetryMate(idir, my_prior, pointer_dir_nonzeroprior, directions_prior, idir_flag);
}
else
directions_prior.push_back(prior);
is_nonzero_pdf = true;
}
// Keep track of the nearest direction
if (diffang < best_ang)
{
best_idir = idir;
best_ang = diffang;
}
}
else if (sigma_rot > 0.)
{
Matrix1D<RFLOAT> my_direction, sym_direction;
RFLOAT sym_rot, sym_tilt;
// Get the current direction in the loop
Euler_angles2direction(rot_angles[idir], tilt_angles[idir], my_direction);
RFLOAT diffang = calculateDeltaRot(my_direction, prior_rot);
RFLOAT best_diffang = diffang;
for (int j = 0; j < R_repository.size(); j++)
{
sym_direction = L_repository[j] * (my_direction.transpose() * R_repository[j]).transpose();
diffang = calculateDeltaRot(sym_direction, prior_rot);
if (diffang < best_diffang)
best_diffang = diffang;
}
// Only consider differences within sigma_cutoff * sigma_rot
if (best_diffang < sigma_cutoff * sigma_rot)
{
RFLOAT prior = gaussian1D(best_diffang, sigma_rot, 0.);
pointer_dir_nonzeroprior.push_back(idir);
directions_prior.push_back(prior);
sumprior += prior;
is_nonzero_pdf = true;
}
// Keep track of the nearest direction
if (best_diffang < best_ang)
{
best_idir = idir;
best_ang = diffang;
}
}
else if (sigma_tilt > 0.)
{
Matrix1D<RFLOAT> my_direction, sym_direction;
RFLOAT sym_rot, sym_tilt;
// Get the current direction in the loop
Euler_angles2direction(rot_angles[idir], tilt_angles[idir], my_direction);
// Loop over all symmetry operators to find the operator that brings this direction nearest to the prior
RFLOAT diffang = ABS(tilt_angles[idir] - prior_tilt);
if (diffang > 180.)
diffang = ABS(diffang - 360.);
RFLOAT best_diffang = diffang;
for (int j = 0; j < R_repository.size(); j++)
{
sym_direction = L_repository[j] * (my_direction.transpose() * R_repository[j]).transpose();
Euler_direction2angles(sym_direction, sym_rot, sym_tilt);
diffang = ABS(sym_tilt - prior_tilt);
if (diffang > 180.)
diffang = ABS(diffang - 360.);
if (diffang < best_diffang)
best_diffang = diffang;
}
// Only consider differences within sigma_cutoff * sigma_tilt
if (best_diffang < sigma_cutoff * sigma_tilt)
{
RFLOAT prior = gaussian1D(best_diffang, sigma_tilt, 0.);
pointer_dir_nonzeroprior.push_back(idir);
directions_prior.push_back(prior);
sumprior += prior;
is_nonzero_pdf = true;
}
// Keep track of the nearest direction
if (best_diffang < best_ang)
{
best_idir = idir;
best_ang = diffang;
}
} // end if any prior involving rot and/or tilt
else
{
// If no prior on the directions: just add all of them
pointer_dir_nonzeroprior.push_back(idir);
directions_prior.push_back(1.);
sumprior += 1.;
is_nonzero_pdf = true;
}
// For priors on deviations from (0,90)-degree (rot,tilt) angles in multi-body refinement
if (sigma_tilt_from_ninety > 0. && is_nonzero_pdf)
{
// Get the current direction in the loop (re-do, as sometimes sigma_rot and sigma_tilt are both zero!
Matrix1D<RFLOAT> my_direction, best_direction, sym_direction;
Euler_angles2direction(rot_angles[idir], tilt_angles[idir], my_direction);
// Loop over all symmetry operators to find the operator that brings this direction nearest to the prior
RFLOAT best_dotProduct = dotProduct(prior90_direction, my_direction);
best_direction = my_direction;
for (int j = 0; j < R_repository.size(); j++)
{
sym_direction = L_repository[j] * (my_direction.transpose() * R_repository[j]).transpose();
RFLOAT my_dotProduct = dotProduct(prior90_direction, sym_direction);
if (my_dotProduct > best_dotProduct)
{
best_direction = sym_direction;
best_dotProduct = my_dotProduct;
}
}
// Now that we have the best direction, find the corresponding prior probability
RFLOAT diffang = ACOSD( dotProduct(best_direction, prior90_direction) );
if (diffang < -180.)
diffang = ABS(diffang + 360.);
else if (diffang > 180.)
diffang = ABS(diffang - 360.);
diffang = ABS(diffang);
long int mypos = pointer_dir_nonzeroprior.size() - 1;
// Check tilt angle is within 3*sigma_tilt_from_ninety
if (diffang > sigma_cutoff * sigma_tilt_from_ninety)
{
pointer_dir_nonzeroprior.pop_back();
directions_prior.pop_back();
}
else
{
RFLOAT prior = gaussian1D(diffang, sigma_tilt_from_ninety, 0.);
directions_prior[mypos] *= prior;
sumprior_withsigmafromzero += directions_prior[mypos];
}
}
// Here add the code for relax symmetry to find the symmetry mates
} // end for idir
//Normalise the prior probability distribution to have sum 1 over all psi-angles
for (long int idir = 0; idir < directions_prior.size(); idir++)
{
if (sigma_tilt_from_ninety > 0.)
directions_prior[idir] /= sumprior_withsigmafromzero;
else
directions_prior[idir] /= sumprior;
}
// If there were no directions at all, just select the single nearest one:
if (directions_prior.size() == 0)
{
pointer_dir_nonzeroprior.push_back(best_idir);
//std::cerr<<"No direction has been found"<<std::endl;
if (best_idir < 0)
REPORT_ERROR("HealpixSampling::selectOrientationsWithNonZeroPriorProbability BUG: best_idir < 0");
if (isRelax)
{
idir_flag[best_idir] = true;
RFLOAT my_prior = 1./R_repository_relax.size();
directions_prior.push_back(my_prior);
findSymmetryMate(best_idir, my_prior, pointer_dir_nonzeroprior, directions_prior, idir_flag);
}
else
directions_prior.push_back(1.);
}
#ifdef DEBUG_SAMPLING
writeNonZeroPriorOrientationsToBild("orients_local.bild", prior_rot, prior_tilt, pointer_dir_nonzeroprior, "0 0 1", 0.023);
std::cerr << " directions_prior.size()= " << directions_prior.size() << " pointer_dir_nonzeroprior.size()= " << pointer_dir_nonzeroprior.size() << std::endl;
std::cerr << " sumprior= " << sumprior << std::endl;
char c;
std::cerr << "Written orients_local.bild for prior on angles ("<<prior_rot<<","<<prior_tilt<<") Press any key to continue.." << std::endl;
std::cin >> c;
#endif
}
else
{
pointer_dir_nonzeroprior.push_back(0);
directions_prior.push_back(1.);
}
// Psi-angles
pointer_psi_nonzeroprior.clear();
psi_prior.clear();
RFLOAT sumprior = 0.;
RFLOAT sumprior_withsigmafromzero = 0.;
RFLOAT best_diff = 9999.;
long int best_ipsi = -999;
for (long int ipsi = 0; ipsi < psi_angles.size(); ipsi++)
{
bool is_nonzero_pdf = false;
// Sjors 12jul2017: for small tilt-angles, rot-angle may become anything, psi-angle then follows that
// Therefore, psi-prior may be completely wrong.... The following line would however be a very expensive fix....
//if (sigma_psi > 0. && prior_tilt > 10.)
if (sigma_psi > 0.)
{
RFLOAT diffpsi = ABS(psi_angles[ipsi] - prior_psi);
if (diffpsi > 180.)
diffpsi = ABS(diffpsi - 360.);
if (do_bimodal_search_psi && (diffpsi > 90.))
diffpsi = ABS(diffpsi - 180.);
// Only consider differences within sigma_cutoff * sigma_psi
if (diffpsi < sigma_cutoff * sigma_psi)
{
RFLOAT prior = gaussian1D(diffpsi, sigma_psi, 0.);
pointer_psi_nonzeroprior.push_back(ipsi);
psi_prior.push_back(prior);
sumprior += prior;
is_nonzero_pdf = true;
// TMP DEBUGGING
if (prior == 0.)
{
std::cerr << " psi_angles[ipsi]= " << psi_angles[ipsi] << " prior_psi= " << prior_psi << std::endl;
std::cerr << " diffpsi= " << diffpsi << " sigma_cutoff= " << sigma_cutoff << " sigma_psi= " << sigma_psi << std::endl;
REPORT_ERROR("prior on psi is zero!");
}
}
// Keep track of the nearest sampling point
if (diffpsi < best_diff)
{
best_ipsi = ipsi;
best_diff = diffpsi;
}
}
else
{
pointer_psi_nonzeroprior.push_back(ipsi);
psi_prior.push_back(1.);
sumprior += 1.;
is_nonzero_pdf = true;
}
// For priors on deviations from 0 psi angles in multi-body refinement
if (sigma_psi_from_zero > 0. && is_nonzero_pdf)