Xmipp  v3.23.11-Nereus
Public Types | Public Member Functions | Public Attributes | List of all members
VariabilityClass Class Reference

#include <recons_misc.h>

Collaboration diagram for VariabilityClass:
Collaboration graph
[legend]

Public Types

enum  t_VAR_status { VAR_none, VAR_measuring, VAR_analyzing }
 

Public Member Functions

 VariabilityClass (BasicARTParameters *_prm, int _Zoutput_volume_size, int _Youtput_volume_size, int _Xoutput_volume_size)
 Constructor. More...
 
void newIteration ()
 
void newUpdateVolume (GridVolume *ptr_vol_out, Projection &read_proj)
 
void finishAnalysis ()
 

Public Attributes

t_VAR_status VAR_state
 
int Zoutput_volume_size
 
int Youtput_volume_size
 
int Xoutput_volume_size
 
BasicARTParametersprm
 
std::vector< MultidimArray< double > > VA
 Vector of training vectors. More...
 
int N
 Number of updates so far. More...
 

Detailed Description

Variability structure

Definition at line 77 of file recons_misc.h.

Member Enumeration Documentation

◆ t_VAR_status

Constructor & Destructor Documentation

◆ VariabilityClass()

VariabilityClass::VariabilityClass ( BasicARTParameters _prm,
int  _Zoutput_volume_size,
int  _Youtput_volume_size,
int  _Xoutput_volume_size 
)

Constructor.

Definition at line 396 of file recons_misc.cpp.

399 {
400  prm = _prm;
401  Zoutput_volume_size = _Zoutput_volume_size;
402  Youtput_volume_size = _Youtput_volume_size;
403  Xoutput_volume_size = _Xoutput_volume_size;
404  N = 0;
406 }
BasicARTParameters * prm
Definition: recons_misc.h:85
t_VAR_status VAR_state
Definition: recons_misc.h:81
int N
Number of updates so far.
Definition: recons_misc.h:91

Member Function Documentation

◆ finishAnalysis()

void VariabilityClass::finishAnalysis ( )

Finish analysis.

Definition at line 463 of file recons_misc.cpp.

464 {
465  if (VA.size() == 0)
466  return;
467 
468  // Coocurrence matrix
469  int nmax = VA.size();
470  MultidimArray<int> coocurrence(nmax, nmax);
471 
472  // Study each voxel
473  Image<double> SignificantT2, SignificantMaxRatio, SignificantMinRatio;
474  int zsize = ZSIZE(VA[0]) / 2;
475  int ysize = YSIZE(VA[0]) / 2;
476  int xsize = XSIZE(VA[0]) / 2;
477  SignificantT2().initZeros(zsize, ysize, xsize);
478  SignificantMaxRatio().initZeros(zsize, ysize, xsize);
479  SignificantMinRatio().initZeros(zsize, ysize, xsize);
480  std::cout << "Classifying voxels ...\n";
481  init_progress_bar(MULTIDIM_SIZE(SignificantT2()));
482  int counter = 0, nxny = ysize * zsize;
483 #ifdef MODE7
484 #define NFEATURES 7
485 #endif
486 #ifdef MODE8
487 #define NFEATURES 8
488 #endif
489 #ifdef MODE15
490 #define NFEATURES 15
491 #endif
492 
493  FOR_ALL_ELEMENTS_IN_ARRAY3D(SignificantT2())
494  {
495  // Save the data for this voxel
496  std::ofstream fh_dat;
497  fh_dat.open("PPP.dat");
498  if (!fh_dat)
499  REPORT_ERROR(ERR_IO_NOTOPEN, "VariabilityClass::finishAnalysis: "
500  "Cannot open PPP.dat for output");
501  fh_dat << NFEATURES << " " << nmax << std::endl;
502  std::vector< Matrix1D<double> > v;
503  v.clear();
504  for (int n = 0; n < nmax; n++)
505  {
507 
508 #ifdef MODE7
509 
510  v_aux(0) = VA[n](k, i, j + xsize);
511  v_aux(1) = VA[n](k, i + ysize, j);
512  v_aux(2) = VA[n](k, i + ysize, j + xsize);
513  v_aux(3) = VA[n](k + zsize, i, j);
514  v_aux(4) = VA[n](k + zsize, i, j + xsize);
515  v_aux(5) = VA[n](k + zsize, i + ysize, j);
516  v_aux(6) = VA[n](k + zsize, i + ysize, j + xsize);
517 #endif
518 #ifdef MODE8
519 
520  v_aux(0) = VA[n](k, i, j);
521  v_aux(1) = VA[n](k, i, j + xsize);
522  v_aux(2) = VA[n](k, i + ysize, j);
523  v_aux(3) = VA[n](k, i + ysize, j + xsize);
524  v_aux(4) = VA[n](k + zsize, i, j);
525  v_aux(5) = VA[n](k + zsize, i, j + xsize);
526  v_aux(6) = VA[n](k + zsize, i + ysize, j);
527  v_aux(7) = VA[n](k + zsize, i + ysize, j + xsize);
528 #endif
529 #ifdef MODE15
530 
531  v_aux(0) = VA[n](k / 2, i / 2, j / 2);
532  v_aux(1) = VA[n](k / 2, i / 2, j / 2 + xsize2);
533  v_aux(2) = VA[n](k / 2, i / 2 + ysize2, j / 2);
534  v_aux(3) = VA[n](k / 2, i / 2 + ysize2, j / 2 + xsize2);
535  v_aux(4) = VA[n](k / 2 + zsize2, i / 2, j / 2);
536  v_aux(5) = VA[n](k / 2 + zsize2, i / 2, j / 2 + xsize2);
537  v_aux(6) = VA[n](k / 2 + zsize2, i / 2 + ysize2, j / 2);
538  v_aux(7) = VA[n](k / 2 + zsize2, i / 2 + ysize2, j / 2 + xsize2);
539  v_aux(8) = VA[n](k, i, j + xsize);
540  v_aux(9) = VA[n](k, i + ysize, j);
541  v_aux(10) = VA[n](k, i + ysize, j + xsize);
542  v_aux(11) = VA[n](k + zsize, i, j);
543  v_aux(12) = VA[n](k + zsize, i, j + xsize);
544  v_aux(13) = VA[n](k + zsize, i + ysize, j);
545  v_aux(14) = VA[n](k + zsize, i + ysize, j + xsize);
546 #endif
547 
548  v_aux = v_aux * v_aux;
549  // COSS: Doesn't work: v_aux=v_aux.sort();
550 
551  fh_dat << v_aux.transpose() << std::endl;
552  v.push_back(v_aux);
553  }
554  fh_dat.close();
555 
556  // Classify
557  if (system("xmipp_fcmeans -din PPP.dat -std::cout PPP -c 2 -saveclusters > /dev/null")==-1)
558  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
559 
560  // Pick up results
561  Matrix2D<double> aux;
562  int n_previous;
563 #define GET_RESULTS(fh,fn,avg,cov,N,idx) \
564  fh.open(fn);\
565  if (!fh) \
566  REPORT_ERROR(ERR_IO_NOTOPEN,(std::string)"VariabilityClass::finishAnalysis: " \
567  "Cannot open "+fn+" for input"); \
568  n_previous=-1; \
569  while (!fh.eof()) { \
570  int n; fh >> n; \
571  if (n!=n_previous) { \
572  n_previous=n; N++; \
573  idx(n)=1; \
574  avg+=v[n]; \
575  aux.fromVector(v[n]); \
576  cov+=aux*aux.transpose(); \
577  }\
578  } \
579  avg/=N; \
580  cov/=N; \
581  aux.fromVector(avg); \
582  cov-=aux*aux.transpose();
583 
584  std::ifstream fh_0;
586  Matrix1D<int> idx0(nmax);
587  Matrix2D<double> covariance0(NFEATURES, NFEATURES);
588  int N0 = 0;
589  GET_RESULTS(fh_0, "PPP.0", avg0, covariance0, N0, idx0);
590 #ifdef DEBUG
591 
592  std::cout << "Class 0 is:\n";
593  for (size_t n = 0; n < idx0.size(); n++)
594  {
595  if (idx0(n))
596  {
597  int iact_proj = prm->ordered_list(n);
598  std::cout << prm->IMG_Inf[iact_proj].fn_proj << std::endl;
599  }
600  }
601 #endif
602 
603  std::ifstream fh_1;
605  Matrix1D<int> idx1(nmax);
606  Matrix2D<double> covariance1(NFEATURES, NFEATURES);
607  int N1 = 0;
608  GET_RESULTS(fh_1, "PPP.1", avg1, covariance1, N1, idx1);
609 #ifdef DEBUG
610 
611  std::cout << "Class 1 is:\n";
612  for (size_t n = 0; n < idx1.size(); n++)
613  {
614  if (idx1(n))
615  {
616  int iact_proj = prm->ordered_list(n);
617  std::cout << prm->IMG_Inf[iact_proj].fn_proj << std::endl;
618  }
619  }
620 #endif
621 
622  Matrix2D<double> T2, covariance;
623  if (NFEATURES > 1)
624  {
625  // Perform T2-Hotelling test
626  Matrix1D<double> avg_diff = avg1 - avg0;
627  covariance = 1.0 / (N0 + N1 - 2) *
628  ((N0 - 1.0) * covariance0 + (N1 - 1.0) * covariance1);
629  covariance *= (1.0 / N0 + 1.0 / N1);
630  aux.fromVector(avg_diff);
631  T2 = (double)(N0 + N1 - avg_diff.size() - 1) /
632  ((N0 + N1 - 2) * avg_diff.size()) *
633  aux.transpose() * covariance.inv() * aux;
634  }
635  else
636  {
637  // Perform t-test
638  double variance = ((N0 - 1) * covariance0(0, 0) + (N1 - 1) * covariance1(0, 0)) /
639  (N0 + N1 - 2);
640  double t = (avg1(0) - avg0(0)) / sqrt(variance * (1.0 / N0 + 1.0 / N1));
641  T2.initZeros(1, 1);
642  T2(0, 0) = t;
643  }
644 
645  // Analysis of the covariance structure
646  Matrix1D<double> eigenvalues;
647  if (NFEATURES > 1)
648  {
649  Matrix2D<double> U, V;
650  svdcmp(covariance, U, eigenvalues, V);
651  }
652 
653  // Analysis of the coocurrences
654  for (size_t n = 0; n < idx0.size(); n++)
655  for (size_t np = n + 1; np < idx0.size(); np++)
656  if (idx0(n) && idx0(np))
657  coocurrence(n, np)++;
658 
659  for (size_t n = 0; n < idx1.size(); n++)
660  for (size_t np = n + 1; np < idx1.size(); np++)
661  if (idx1(n) && idx1(np))
662  coocurrence(n, np)++;
663 
664  // Keep results
665  SignificantT2(k, i, j) = T2(0, 0);
666  if (NFEATURES > 1)
667  {
668  SignificantMinRatio(k, i, j) = eigenvalues(1) / eigenvalues(0);
669  SignificantMaxRatio(k, i, j) = eigenvalues(NFEATURES - 1) / eigenvalues(0);
670  }
671 #ifdef DEBUG
672  std::cout << "T2 for this classification is " << T2(0, 0) << std::endl;
673  std::cout << "Eigenvalues are " << eigenvalues.transpose() << std::endl;
674 #endif
675 
676  if (++counter % nxny == 0)
677  progress_bar(counter);
678  }
679  progress_bar(MULTIDIM_SIZE(SignificantT2()));
680  SignificantT2.write(prm->fn_root + "_variability.vol");
681  SignificantMinRatio.write(prm->fn_root + "_minratio.vol");
682  SignificantMaxRatio.write(prm->fn_root + "_maxratio.vol");
683  if (system("rm PPP.dat PPP.cod PPP.vs PPP.err PPP.his PPP.inf PPP.0 PPP.1")==-1)
684  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
685 
686  for (int n = 0; n < nmax; n++)
687  for (int np = n + 1; np < nmax; np++)
688  coocurrence(np, n) = coocurrence(n, np);
689  Image<double> save;
690  typeCast(coocurrence, save());
691  save.write(prm->fn_root + "_coocurrence.xmp");
692 }
Just to locate unclassified errors.
Definition: xmipp_error.h:192
void init_progress_bar(long total)
int * nmax
#define YSIZE(v)
#define GET_RESULTS(fh, fn, avg, cov, N, idx)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
size_t size() const
Definition: matrix1d.h:508
void fromVector(const Matrix1D< T > &op1)
Definition: matrix2d.cpp:803
#define MULTIDIM_SIZE(v)
void sqrt(Image< double > &op)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
void inv(Matrix2D< T > &result) const
Definition: matrix2d.cpp:663
Matrix2D< T > transpose() const
Definition: matrix2d.cpp:1314
std::vector< MultidimArray< double > > VA
Vector of training vectors.
Definition: recons_misc.h:88
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
Matrix1D< T > transpose() const
Definition: matrix1d.cpp:644
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
ReconsInfo * IMG_Inf
Array with all the sorting information for each projection.
Definition: basic_art.h:342
#define XSIZE(v)
void progress_bar(long rlen)
#define ZSIZE(v)
FileName fn_root
Definition: basic_art.h:217
FileName fn_proj
Projection filename.
Definition: basic_art.h:61
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
Definition: matrix2d.cpp:125
#define j
BasicARTParameters * prm
Definition: recons_misc.h:85
File cannot be open.
Definition: xmipp_error.h:137
MultidimArray< int > ordered_list
Order in which projections will be presented to algorithm.
Definition: basic_art.h:345
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
Definition: matrix1d.h:1227
void initZeros()
Definition: matrix2d.h:626
int * n
#define NFEATURES

◆ newIteration()

void VariabilityClass::newIteration ( )

Start a new ART iteration.

Definition at line 408 of file recons_misc.cpp.

409 {}

◆ newUpdateVolume()

void VariabilityClass::newUpdateVolume ( GridVolume ptr_vol_out,
Projection read_proj 
)

Update data with a new volume. The update volume is set to zeros after this function

Definition at line 415 of file recons_misc.cpp.

417 {
418  Image<double> vol_voxels;
419 
420  // Convert from basis to voxels
421  prm->basis.changeToVoxels(*ptr_vol_out, &(vol_voxels()),
423  (*ptr_vol_out).initZeros();
424  N++;
425 
426  // Make the DWT
428 #ifdef MODE7
429 
430  int DWT_iterations = 2;
431  int keep_from_iteration = 0;
432 #endif
433 #ifdef MODE8
434 
435  int DWT_iterations = 2;
436  int keep_from_iteration = 0;
437 #endif
438 #ifdef MODE15
439 
440  int DWT_iterations = 2;
441  int keep_from_iteration = 0;
442 #endif
443 
444  Bilib_DWT(vol_voxels(), DWTV, DWT_iterations);
445 #ifdef DEBUG
446 
447  vol_voxels.write("PPPVariability.vol");
448  char c;
449  std::cout << "Press any key\n";
450  std::cin >> c;
451 #endif
452 
453  // Select the LLL block and keep it
454  int x1, x2, y1, y2, z1, z2;
455  SelectDWTBlock(keep_from_iteration, DWTV, "000", x1, x2, y1, y2, z1, z2);
456  DWTV.selfWindow(z1, y1, x1, z2, y2, x2);
457  STARTINGZ(DWTV) = STARTINGY(DWTV) = STARTINGX(DWTV) = 0;
458  VA.push_back(DWTV);
459 }
void selfWindow(int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T init_value=0)
void Bilib_DWT(const MultidimArray< double > &input, MultidimArray< double > &result, int iterations, int isign)
Definition: wavelet.cpp:43
Basis basis
Basis function. By default, blobs.
Definition: basic_art.h:97
doublereal * c
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
std::vector< MultidimArray< double > > VA
Vector of training vectors.
Definition: recons_misc.h:88
#define STARTINGX(v)
void changeToVoxels(GridVolume &vol_basis, MultidimArray< double > *vol_voxels, int Zdim, int Ydim, int Xdim, int threads=1) const
Definition: basis.cpp:261
#define STARTINGY(v)
BasicARTParameters * prm
Definition: recons_misc.h:85
void SelectDWTBlock(int scale, const MultidimArray< T > &I, const std::string &quadrant, int &x1, int &x2)
Definition: wavelet.h:134
int N
Number of updates so far.
Definition: recons_misc.h:91
#define STARTINGZ(v)

Member Data Documentation

◆ N

int VariabilityClass::N

Number of updates so far.

Definition at line 91 of file recons_misc.h.

◆ prm

BasicARTParameters* VariabilityClass::prm

Definition at line 85 of file recons_misc.h.

◆ VA

std::vector< MultidimArray<double> > VariabilityClass::VA

Vector of training vectors.

Definition at line 88 of file recons_misc.h.

◆ VAR_state

t_VAR_status VariabilityClass::VAR_state

Definition at line 81 of file recons_misc.h.

◆ Xoutput_volume_size

int VariabilityClass::Xoutput_volume_size

Definition at line 84 of file recons_misc.h.

◆ Youtput_volume_size

int VariabilityClass::Youtput_volume_size

Definition at line 83 of file recons_misc.h.

◆ Zoutput_volume_size

int VariabilityClass::Zoutput_volume_size

Definition at line 82 of file recons_misc.h.


The documentation for this class was generated from the following files: