Xmipp  v3.23.11-Nereus
selfile.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (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 #ifndef SELFILE_H
26 #define SELFILE_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include <core/xmipp_funcs.h>
32 #include <core/xmipp_image.h>
33 
34 // Forward declaration
35 class SelFile;
36 
39 
40 
50 class SelLine
51 {
52 public:
53  typedef enum
54  {
55  DISCARDED = -1,
56  ACTIVE = 1
57  } Label;
58 
59  typedef enum
60  {
63  DATALINE = 1,
64  COMMENT = 2
65  } Line_Type;
66 
67 private:
68  Line_Type line_type;
69  std::string text;
70  Label label;
71  int number;
72 
73  friend class SelFile;
74 
75 public:
81  SelLine() : line_type(NOT_ASSIGNED)
82  {}
83 
86  void assign(const SelLine& line);
87 
90  const std::string& get_text()
91  {
92  return text;
93  }
94 
97  short get_label()
98  {
99  return label;
100  }
101 
105  {
106  return number;
107  }
108 
112  {
113  return line_type == COMMENT;
114  }
115 
118  int Is_data()
119  {
120  return line_type == DATALINE;
121  }
122 
129  {
130  line_type = type;
131  }
132 
135  void set_number(int n)
136  {
137  number = n;
138  }
139 
145  friend bool operator<(const SelLine& l1, const SelLine& l2);
146 
147  friend SelFile compare(SelFile& SF1, SelFile& SF2, const int mode);
148  friend std::vector< SelLine >::iterator find(std::vector< SelLine >& text,
149  const std::string& img_name);
150 
153  friend std::ostream& operator<<(std::ostream& o, const SelLine& line);
154 
160  friend std::istream& operator>>(std::istream& i, SelLine& lin);
161 };
162 
168 class SelFile
169 {
170  // Filename of the .sel
171  FileName fn_sel;
172 
173  // The whole file is inside
174  std::vector< SelLine > text_line;
175 
176  // Number of valid images
177  int no_imgs;
178 
179  // Pointer to current line
180  std::vector< SelLine >::iterator current_line;
181 
182  // Find a specific image name in a list of sellines
183  std::vector< SelLine >::iterator find(const std::string& img_name);
184 
185  // Move the current_line to the next line with this label
186  void adjust_to_label(SelLine::Label label);
187 public:
189 
190 
199  SelFile();
200 
209  SelFile(const FileName &name)
210  {
211  read(name);
212  }
213 
219  void reserve(int n)
220  {
221  text_line.reserve(n);
222  current_line = text_line.begin();
223  }
224 
231  void clear();
233 
235 
236 
238  void assign(const SelFile& sel);
239 
249  friend std::ostream& operator<<(std::ostream& o, const SelFile& sel);
251 
253 
254 
267  void read(const FileName& name, int overrinding = 1);
268 
280  void append(const FileName& name)
281  {
282  read(name, 0);
283  }
284 
300  void merge(const FileName& name);
301 
304  void merge(SelFile& sel);
305 
314  SelFile operator+(SelFile& sel);
315 
318  void split_in_two(SelFile& sel1, SelFile& sel2);
319 
322  void split_in_N(int N, std::vector< SelFile >& parts);
323 
327  void mpi_select_part(int rank, int size, int& num_img_tot);
328 
333  void mpi_select_part2(int rank, int size, int& num_img_tot,int mpi_job_size);
334 
337  void chooseSubset(int firstImage, int lastImage, SelFile &SFsubset);
338 
349  void write(const FileName& sel_name = "");
351 
353 
354 
365  {
366  current_line = text_line.begin();
367  }
368 
378  {
379  go_beginning();
380  adjust_to_label(SelLine::ACTIVE);
381  }
382 
393  const std::string& operator()(int i)
394  {
395  return text_line[i].text;
396  }
397 
413  const std::string& NextImg(SelLine::Label label = SelLine::ACTIVE);
414 
429  void next()
430  {
431  current_line++;
432  }
433 
449  void jump(int count, SelLine::Label label = SelLine::ACTIVE);
450 
456  bool jump_lines(int count);
457 
473  void search(const std::string& img_name)
474  {
475  current_line = find(img_name);
476  }
477 
485  int eof()
486  {
487  return current_line == text_line.end();
488  }
490 
492 
493 
496  FileName name() const
497  {
498  return fn_sel;
499  }
500 
503  int Is_ACTIVE() const
504  {
505  return current_line->Is_data() &&
506  current_line->get_label() == SelLine::ACTIVE;
507  }
508 
511  int Is_DISCARDED() const
512  {
513  return current_line->Is_data() &&
514  current_line->get_label() == SelLine::DISCARDED;
515  }
516 
519  int Is_COMMENT() const
520  {
521  return current_line->Is_comment();
522  }
523 
526  const SelLine& current()
527  {
528  return *current_line;
529  }
530 
533  void get_current(SelLine& _SL)
534  {
535  _SL = current();
536  }
537 
549  int exists(const std::string& img_name)
550  {
551  return find(img_name) != text_line.end();
552  }
553 
567  int ImgNo(SelLine::Label label = SelLine::ACTIVE) const;
568 
578  int LineNo();
579 
594  void ImgSize(int& Ydim, int& Xdim);
595 
600  FileName FileExtension();
601 
606  int MaxFileNameLength();
607 
617  const std::string get_current_file();
618 
622  {
623  return *current_line;
624  }
625 
635  const std::string get_file_number(int i);
637 
639 
640 
652  void remove(const std::string& img_name);
653 
664  void remove_current();
665 
677  void set(const std::string& img_name, SelLine::Label label);
678 
684  void set_current(SelLine::Label label);
685 
691  void set_current_filename(const FileName& fn_new);
692 
704  void insert(const std::string& img_name, SelLine::Label label = SelLine::ACTIVE);
705 
716  void insert(const SelLine& _selline);
717 
728  void insert_comment(const std::string& comment);
729 
738  void clean();
739 
748  void clean_comments();
750 
752 
753 
765  SelFile sort_by_filenames();
766 
779  SelFile randomize();
780 
786  SelFile randomSubset(int subsetN, bool withReplacement=true);
787 
800  SelFile random_discard(int N);
801 
825  friend SelFile compare(SelFile& SF1, SelFile& SF2, const int mode);
827 };
829 #endif
friend std::ostream & operator<<(std::ostream &o, const SelLine &line)
Definition: selfile.cpp:59
FileName name() const
Definition: selfile.h:496
int Is_data()
Definition: selfile.h:118
void write(std::ostream &os, const datablock &db)
Definition: cif2pdb.cpp:3747
void search(const std::string &img_name)
Definition: selfile.h:473
int Is_COMMENT() const
Definition: selfile.h:519
void set_number(int n)
Definition: selfile.h:135
const SelLine & current()
Definition: selfile.h:526
int exists(const std::string &img_name)
Definition: selfile.h:549
void go_first_ACTIVE()
Definition: selfile.h:377
int eof()
Definition: selfile.h:485
std::vector< T > operator+(const std::vector< T > &v, const T &a)
Definition: vector_ops.h:51
int Is_DISCARDED() const
Definition: selfile.h:511
friend SelFile compare(SelFile &SF1, SelFile &SF2, const int mode)
Definition: selfile.cpp:872
#define i
int Is_comment()
Definition: selfile.h:111
Line_Type
Definition: selfile.h:59
const std::string & get_text()
Definition: selfile.h:90
viol type
void get_current(SelLine &_SL)
Definition: selfile.h:533
void append(const FileName &name)
Definition: selfile.h:280
void go_beginning()
Definition: selfile.h:364
void mode
SelFile(const FileName &name)
Definition: selfile.h:209
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
int Is_ACTIVE() const
Definition: selfile.h:503
SelLine get_current_line()
Definition: selfile.h:621
friend std::vector< SelLine >::iterator find(std::vector< SelLine > &text, const std::string &img_name)
Definition: selfile.cpp:553
void set_type(Line_Type type)
Definition: selfile.h:128
void reserve(int n)
Definition: selfile.h:219
void assign(const SelLine &line)
Definition: selfile.cpp:41
void next()
Definition: selfile.h:429
friend class SelFile
Definition: selfile.h:73
SelLine()
Definition: selfile.h:81
int get_number()
Definition: selfile.h:104
const std::string & operator()(int i)
Definition: selfile.h:393
file read(std::istream &is)
Definition: pdb2cif.cpp:6200
friend std::istream & operator>>(std::istream &i, SelLine &lin)
Definition: selfile.cpp:75
int * n
friend bool operator<(const SelLine &l1, const SelLine &l2)
Definition: selfile.cpp:49
short get_label()
Definition: selfile.h:97