Xmipp  v3.23.11-Nereus
micrograph.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef _CORE_XMIPPMICROGRAPH_H
27 #define _CORE_XMIPPMICROGRAPH_H
28 
29 /* ************************************************************************* */
30 /* INCLUDES */
31 /* ************************************************************************* */
32 #include <vector>
33 #include "filters.h"
34 #include <core/xmipp_funcs.h>
35 #include <core/multidim_array.h>
36 #include <core/xmipp_image.h>
37 #include <core/xmipp_fftw.h>
38 #include <core/metadata_vec.h>
39 #include <core/xmipp_error.h>
40 
41 /* ************************************************************************* */
42 /* FORWARD DEFINITIONS */
43 /* ************************************************************************* */
44 // This forward definitions are needed for defining operators functions that
45 // use other clases type
46 
47 /* ************************************************************************* */
48 /* MICROGRAPHY */
49 /* ************************************************************************* */
52 
53 
56 {
58  int label;
60  int X;
62  int Y;
64  bool valid;
66  double cost;
68  double scoreVar;
70  double scoreGini;
71 };
72 
73 
79 {
80 public:
81  struct Point
82  {
83  double x;
84  double y;
85  };
86 private:
87  /* This image will contain a single particle from the micrograph,
88  this is done to avoid asking/freeing memory all time. */
89  Image<double> single_particle;
90 
91  FileName fn_coords;
92  FileName fn_micrograph;
93  MDRowVec ctfRow; //MetaData row with CTF params
94  FileName fn_inf;
95  int X_window_size;
96  int Y_window_size;
97  int datatype;
98  int swapbyte;
99  int __offset;
100  bool compute_transmitance;
101  bool compute_inverse;
102  int fh_micrograph;
103  std::vector<std::string> labels;
104  double stdevFilter = -1;
105  Image<unsigned char> IUChar = {};
106  Image<short int> IShort = {};
107  Image<unsigned short int> IUShort = {};
108  Image<int> IInt = {};
109  Image<unsigned int> IUInt = {};
110  Image<float> IFloat = {};
111 public:
112  size_t Xdim;
113  size_t Ydim;
114  size_t Zdim;
115  size_t Ndim;
118  std::vector<Particle_coords> coords;
119 
121  void clear();
122 
124  int getDatatype() const
125  {
126  return datatype;
127  }
129  int getDatatypeDetph() const;
130 
133  void open_micrograph(const FileName &fn_micrograph);
134 
137  void close_micrograph();
138 
141  {
142  return fn_micrograph;
143  }
144 
147  {
148  fn_micrograph = fn;
149  }
150 
152  // TODO: when needed, change this function to take MDRow&
153  void set_ctfparams(const MDRowVec &ctf)
154  {
155  ctfRow = ctf;
156  ctfRow.detach();
157  }
158 
161  {
162  return ctfRow;
163  }
164 
165  void setStdevFilter(double d)
166  {
167  stdevFilter=d;
168  }
169 
171  void write_coordinates(int label, double minCost, const FileName &fn_coords = "");
172 
177  void read_coordinates(int label, const FileName &fn_coords);
178 
181  void transform_coordinates(const Matrix2D<double> &M);
182 
184  void scale_coordinates(const double &c);
185 
188  int ParticleNo() const
189  {
190  return coords.size();
191  }
192 
195  std::vector<Particle_coords> & Particles()
196  {
197  return coords;
198  }
199 
201  void get_Particles(std::vector<Particle_coords> & _coords)
202  {
203  _coords = coords;
204  }
205 
209  void set_window_size(int _X_window_size, int _Y_window_size)
210  {
211  X_window_size = _X_window_size;
212  Y_window_size = _Y_window_size;
213  }
214 
220  void set_transmitance_flag(bool flag_value)
221  {
222  compute_transmitance =
223  flag_value;
224  }
225 
226  void setDataType(DataType _datatype)
227  {
228  datatype = _datatype;
229  }
236  {
237  return compute_transmitance;
238  }
239 
244  void set_inverse_flag(bool flag_value)
245  {
246  compute_inverse =
247  flag_value;
248  }
249 
254  bool read_inverse_flag(void)
255  {
256  return compute_inverse;
257  }
258 
262  template <typename T>
263  int templateScissor(const Image<T> &I,
264  const Particle_coords &P, MultidimArray<double> &result,
265  double Dmin, double Dmax, double scaleX, double scaleY,
266  bool only_check, bool fillBorders)
267  {
268  result.initZeros(Y_window_size, X_window_size);
269  int _i0 = ROUND(scaleY * P.Y) + FIRST_XMIPP_INDEX(Y_window_size);
270  int _iF = ROUND(scaleY * P.Y) + LAST_XMIPP_INDEX(Y_window_size);
271  int _j0 = ROUND(scaleX * P.X) + FIRST_XMIPP_INDEX(X_window_size);
272  int _jF = ROUND(scaleX * P.X) + LAST_XMIPP_INDEX(X_window_size);
273  int retval = 1;
274  double irange=1.0/(Dmax - Dmin);
275 
276  if (!fillBorders && (_i0 < 0 || _iF >= Ydim || _j0 < 0 || _jF >= Xdim))
277  retval = 0;
278  else
279  if (!only_check)
280  {
281 
282  for (int i = _i0, i_i0=0; i <= _iF; i++, i_i0++)
283  {
284  int ifrom=i;
285  if (fillBorders)
286  {
287  if (ifrom<0)
288  ifrom=0;
289  else if (ifrom>=Ydim)
290  ifrom=Ydim-1;
291  }
292  for (int j=_j0, j_j0=0; j<=_jF; j++, j_j0++)
293  {
294  int jfrom=j;
295  if (fillBorders)
296  {
297  if (jfrom<0)
298  jfrom=0;
299  else if (jfrom>=Xdim)
300  jfrom=Xdim-1;
301  }
302  double val=IMGPIXEL(I,ifrom,jfrom);
303  if (compute_transmitance)
304  {
305  double temp;
306  if (val < 1)
307  temp = val;
308  else
309  temp = log10(val);
310  if (compute_inverse)
311  A2D_ELEM(result,i_i0, j_j0) = (Dmax - temp) * irange;
312  else
313  A2D_ELEM(result,i_i0, j_j0) = (temp - Dmin) * irange;
314  }
315  else
316  {
317  if (compute_inverse)
318  A2D_ELEM(result, i_i0, j_j0) = (Dmax - val) * irange;
319  else
320  A2D_ELEM(result, i_i0, j_j0) = val;
321  }
322  }
323  }
324  }
325  return retval;
326  }
327 
346  int scissor(const Particle_coords &P, MultidimArray<double> &result,
347  double Dmin, double Dmax,
348  double scaleX = 1, double scaleY = 1, bool only_check = false,
349  bool fillBorders = false);
350 
354  double operator()(size_t y, size_t x) const
355  {
356  if (y < 0 || y >= Ydim || x < 0 || x >= Xdim)
357  // COSS: REPORT_ERROR(1, "Micrograph::(): index out of range");
358  return 0;
359  if (datatype == DT_UChar || datatype == DT_UHalfByte)
360  {
361  return IMGPIXEL(IUChar,y,x);
362  }
363  else if (datatype == DT_UShort)
364  {
365  return IMGPIXEL(IUShort,y,x);
366  }
367  else if (datatype == DT_Short)
368  {
369  return IMGPIXEL(IShort,y,x);
370  }
371  else if (datatype == DT_UInt)
372  {
373  return IMGPIXEL(IUInt,y,x);
374  }
375  else if (datatype == DT_Int)
376  {
377  return IMGPIXEL(IInt,y,x);
378  }
379  else if (datatype == DT_Float)
380  {
381  return IMGPIXEL(IFloat,y,x);
382  }
383  else
384  REPORT_ERROR(ERR_TYPE_INCORRECT, "Micrograph::(): unknown datatype");
385  }
386 
388  void computeDoubleMinMax(double &Dmin, double &Dmax) const
389  {
390  if (datatype == DT_UChar || datatype == DT_UHalfByte)
391  {
392  return IUChar().computeDoubleMinMax(Dmin,Dmax);
393  }
394  else if (datatype == DT_UShort)
395  {
396  return IUShort().computeDoubleMinMax(Dmin,Dmax);
397  }
398  else if (datatype == DT_Short)
399  {
400  return IShort().computeDoubleMinMax(Dmin,Dmax);
401  }
402  else if (datatype == DT_UInt)
403  {
404  return IUInt().computeDoubleMinMax(Dmin,Dmax);
405  }
406  else if (datatype == DT_Int)
407  {
408  return IInt().computeDoubleMinMax(Dmin,Dmax);
409  }
410  else if (datatype == DT_Float)
411  {
412  return IFloat().computeDoubleMinMax(Dmin,Dmax);
413  }
414 
415  else
416  REPORT_ERROR(ERR_TYPE_INCORRECT, "Micrograph::computeDoubleMinMax::(): unknown datatype");
417  }
418 
420  //Dangerous function indeed
421  //write in default endiam
422  void set_val(int y, int x, double new_val)
423  {
424  if (datatype == DT_UChar || datatype == DT_UHalfByte)
425  {
426  IMGPIXEL(IUChar,y,x) = (unsigned char) new_val;
427  }
428  else if (datatype == DT_UShort)
429  {
430  IMGPIXEL(IUShort,y,x) = (unsigned short) new_val;
431  }
432  else if (datatype == DT_Short)
433  {
434  IMGPIXEL(IShort,y,x) = (short) new_val;
435  }
436  else if (datatype == DT_UInt)
437  {
438  IMGPIXEL(IUInt,y,x) = (unsigned int) new_val;
439  }
440  else if (datatype == DT_Int)
441  {
442  IMGPIXEL(IInt,y,x) = (int) new_val;
443  }
444  else if (datatype == DT_Float)
445  {
446  IMGPIXEL(IFloat,y,x) = (float) new_val;
447  }
448 
449  else
450  REPORT_ERROR(ERR_TYPE_INCORRECT, "Micrograph::set_val::(): unknown datatype");
451 
452  }
453 
459  void produce_all_images(int label, double minCost, const FileName &fn_root,
460  const FileName &fn_image = "", double ang = 0,
461  //double gamma = 0., double psi = 0.,
462  bool rmStack=false,
463  bool fillBorders=false,
464  bool extractNoise=false,
465  int Nnoise=-1);
466 
470  int search_coord_near(int x, int y, int prec = 3) const;
471 
475  void invalidate_coord(int n);
476 
479  int add_coord(int x, int y, int label, double cost);
480 
482  void move_last_coord_to(int x, int y);
483 
487  {
488  if (n < 0 || n > ParticleNo())
489  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "Micrograph::coord(): index out of range");
490  return coords[n];
491  }
492 
494  void get_coord(int n, Particle_coords &_coords)
495  {
496  _coords = coord(n);
497  }
498 
501  int add_label(const std::string &label)
502  {
503  labels.push_back(label);
504  return labels.size() - 1;
505  }
506 
508  int LabelNo()
509  {
510  return labels.size();
511  }
512 
516  std::string & get_label(int n)
517  {
518  if (n < 0 || n > LabelNo())
519  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "Micrograph::get_label(): index out of range");
520  return labels[n];
521  }
522 
524  void get_label(int n, std::string &_label)
525  {
526  _label = get_label(n);
527  }
528 
530  void size(int &_Xdim, int &_Ydim) const
531  {
532  _Xdim = Xdim;
533  _Ydim = Ydim;
534  }
535 
537  void resize(int Xdim, int Ydim, const FileName &filename="");
538 
544  void write(const FileName &fileName,CastWriteMode castMode=CW_CAST);
545 };
546 
549 {
550 public:
552  std::vector<int> coordU;
554  std::vector<int> coordT;
555 public:
557  TiltPairAligner();
558 
560  void clear();
561 
563  void addCoordinatePair(int _muX, int _muY, int _mtX, int _mtY);
564 
566  void adjustPassingMatrix(int _muX, int _muY, int _mtX, int _mtY);
567 
569  void calculatePassingMatrix();
570 
572  void passToTilted(int _muX, int _muY, int &_mtX, int &_mtY);
573 
575  void passToUntilted(int _mtX, int _mtY, int &_muX, int &_muY);
576 
578  void computeGamma();
579 
583  void computeAngles(double &ualpha, double &talpha, double &ogamma);
584 public:
585  // For tilted-untilted correspondance
586  Matrix2D<double> Au; // Untilted "positions"
587  Matrix2D<double> Bt; // Tilted "positions"
588  Matrix2D<double> Put; // Passing matrix from untilted to tilted
589  Matrix2D<double> Ptu; // Passing matrix from tilted to untilted
590  int Nu; // Number of points in matrices
591  double gamma; // Tilting angle in radians
592  double alpha_u;// Angle of axis with X axis in radians
593  double alpha_t;// Anfle of axis with X axis in radians
597 };
598 
600 
601 #endif
Index out of bounds.
Definition: xmipp_error.h:132
int ParticleNo() const
Definition: micrograph.h:188
void set_ctfparams(const MDRowVec &ctf)
Definition: micrograph.h:153
int Y
Y position.
Definition: micrograph.h:62
#define A2D_ELEM(v, i, j)
void write(std::ostream &os, const datablock &db)
Definition: cif2pdb.cpp:3747
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Matrix1D< double > m
Auxiliary vector.
Definition: micrograph.h:595
doublereal * c
static double * y
bool read_transmitance_flag(void)
Definition: micrograph.h:235
Matrix2D< double > Put
Definition: micrograph.h:588
void setStdevFilter(double d)
Definition: micrograph.h:165
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int X
X position.
Definition: micrograph.h:60
bool valid
Valid.
Definition: micrograph.h:64
int getDatatype() const
Definition: micrograph.h:124
std::vector< Particle_coords > & Particles()
Definition: micrograph.h:195
bool read_inverse_flag(void)
Definition: micrograph.h:254
void set_window_size(int _X_window_size, int _Y_window_size)
Definition: micrograph.h:209
void set_micrograph_name(const FileName &fn)
Definition: micrograph.h:146
int add_label(const std::string &label)
Definition: micrograph.h:501
double scoreVar
Score by var and Gini.
Definition: micrograph.h:68
doublereal * x
#define i
doublereal * d
void get_coord(int n, Particle_coords &_coords)
Definition: micrograph.h:494
void computeDoubleMinMax(double &Dmin, double &Dmax) const
Definition: micrograph.h:388
double operator()(size_t y, size_t x) const
Definition: micrograph.h:354
void get_Particles(std::vector< Particle_coords > &_coords)
Definition: micrograph.h:201
const MDRow & get_ctfparams()
Definition: micrograph.h:160
void set_val(int y, int x, double new_val)
Definition: micrograph.h:422
int templateScissor(const Image< T > &I, const Particle_coords &P, MultidimArray< double > &result, double Dmin, double Dmax, double scaleX, double scaleY, bool only_check, bool fillBorders)
Definition: micrograph.h:263
Matrix2D< double > Bt
Definition: micrograph.h:587
double scoreGini
Score by Gini.
Definition: micrograph.h:70
std::vector< int > coordT
Tilted coordinates.
Definition: micrograph.h:554
size_t Zdim
Definition: micrograph.h:114
#define ROUND(x)
Definition: xmipp_macros.h:210
void get_label(int n, std::string &_label)
Definition: micrograph.h:524
size_t Xdim
Definition: micrograph.h:112
DataType
Matrix2D< double > Au
Definition: micrograph.h:586
void log10(Image< double > &op)
Matrix2D< double > pair_E
Definition: micrograph.h:596
#define j
int label
Label.
Definition: micrograph.h:58
Point point2
Definition: micrograph.h:117
Point point1
Definition: micrograph.h:116
std::vector< int > coordU
Untilted coordinates.
Definition: micrograph.h:552
int LabelNo()
Definition: micrograph.h:508
Matrix2D< double > Ptu
Definition: micrograph.h:589
void setDataType(DataType _datatype)
Definition: micrograph.h:226
#define FIRST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:439
size_t Ndim
Definition: micrograph.h:115
void set_transmitance_flag(bool flag_value)
Definition: micrograph.h:220
std::string & get_label(int n)
Definition: micrograph.h:516
Particle_coords & coord(int n)
Definition: micrograph.h:486
double cost
Cost, scaled between 0 and 1.
Definition: micrograph.h:66
const FileName & micrograph_name()
Definition: micrograph.h:140
size_t Ydim
Definition: micrograph.h:113
CastWriteMode
void initZeros(const MultidimArray< T1 > &op)
void detach() override
Incorrect type received.
Definition: xmipp_error.h:190
#define LAST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:448
void set_inverse_flag(bool flag_value)
Definition: micrograph.h:244
void size(int &_Xdim, int &_Ydim) const
Definition: micrograph.h:530
int * n
#define IMGPIXEL(I, i, j)