Xmipp  v3.23.11-Nereus
metadata_vec.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Jan Horacek (xhorace4@fi.muni.cz)
4  *
5  * Institute of Computer Science MUNI
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 VEC_METADATA_H
27 #define VEC_METADATA_H
28 
29 #include <memory>
30 #include <exception>
31 #include <unordered_map>
32 #include "memory.h"
33 #include "metadata_base.h"
34 #include "metadata_base_it.h"
35 #include "utils/memory_utils.h"
36 
37 using MetaDataVecRow = std::vector<MDObject>;
38 
39 
46 class MetaDataVec: public MetaData {
47 protected:
48  std::vector<MetaDataVecRow> _rows;
49  std::array<int, MDL_LAST_LABEL> _label_to_col; // -1 = no mapping
50  std::vector<MDLabel> _col_to_label;
51  size_t _no_columns = 0;
52  std::unordered_map<size_t, size_t> _id_to_index;
53  size_t _next_id = 1;
54 
58  void init(const std::vector<MDLabel>& labelsVector);
59 
61  void _clear(bool onlyData=false);
62 
66  std::vector<MDObject> getObjectsForActiveLabels() const;
67 
68  int _labelIndex(MDLabel label) const;
69  const MDObject& _getObject(size_t i, MDLabel label) const;
70  MDObject& _getObject(size_t i, MDLabel label);
71  const MDObject& _getObject(const MetaDataVecRow&, MDLabel) const;
73  int _rowIndex(size_t id) const;
74  size_t _rowIndexSafe(size_t id) const;
75 
76  void _setRow(const MDRow &row, size_t index);
77  bool _match(const MetaDataVecRow&, const MDQuery&) const;
78  size_t getRowId(const MetaDataVecRow&) const;
79 
80  // Expand row to fit label in.
81  void _expand(MetaDataVecRow&, const MDLabel);
82  void _expand(MetaDataVecRow&, size_t labeli);
83 
84  void _parseObjects(std::istream &is, std::vector<MDObject*> & columnValues,
85  const std::vector<MDLabel> *desiredLabels, bool firstTime) override;
86 
87  void _recalc_id_to_index();
88 
89  bool _contains(const std::vector<MetaDataVecRow>&, const MetaDataVecRow&) const;
90  bool _rowsEq(const MetaDataVecRow& a, const MetaDataVecRow& b) const;
91 
92 public:
103  MetaDataVec();
104  MetaDataVec(const std::vector<MDLabel> &labelsVector);
105 
111  MetaDataVec(const FileName &fileName, const std::vector<MDLabel> &desiredLabels);
112  MetaDataVec(const FileName &fileName);
113 
118  MetaDataVec(const MetaData &md);
119  MetaDataVec(const MetaDataVec &md) = default;
120 
121  virtual ~MetaDataVec() {}
122 
125  void clear() override;
131  std::vector<MDLabel> getActiveLabels() const override;
132 
136  void writeXML(const FileName fn, const FileName blockname, WriteModeMetaData mode) const override;
137 
141  void writeText(const FileName fn, const std::vector<MDLabel>* desiredLabels) const override;
142 
149  size_t addRow(const MDRow &row) override;
150  void addRows(const std::vector<MDRowVec> &rows);
151 
152 
153  int getMaxStringLength(const MDLabel thisLabel) const override;
154 
157  bool setValueCol(const MDObject &mdValueIn) override;
158 
159  template<class T>
160  bool setValueCol(const MDLabel label, const T &valueIn) {
161  return MetaData::setValueCol(label, valueIn);
162  }
163 
170  bool setValue(const MDObject &mdValueIn, size_t id);
171 
172  template<class T>
173  bool setValue(const MDLabel label, const T &valueIn, size_t id) {
174  return MetaData::setValue(label, valueIn, id);
175  }
176 
177  bool getValue(MDObject &mdValueOut, size_t id) const override;
178  MDObject &getValue(MDLabel, size_t id);
179  const MDObject &getValue(MDLabel, size_t id) const;
180 
181  template<class T>
182  bool getValue(const MDLabel label, T &valueOut, size_t id) const {
183  return MetaData::getValue(label, valueOut, id);
184  }
185 
186  template<class T>
187  T getValue(const MDLabel label, size_t id) {
188  return this->getValue(label, id).getValue2(T());
189  }
190 
191  template<class T>
192  const T getValue(const MDLabel label, size_t id) const {
193  return this->getValue(label, id).getValue2(T());
194  }
195 
196  std::unique_ptr<MDRow> getRow(size_t id) override;
197  std::unique_ptr<const MDRow> getRow(size_t id) const override;
198 
199  MDRowVec getRowVec(size_t id);
200  const MDRowVec getRowVec(size_t id) const;
201 
202  void getRow(MDRowVec &row, size_t id); // FIXME: deprecated, use getRow above
203 
204  bool getRowValues(size_t id, std::vector<MDObject> &values) const override;
205  size_t getRowId(size_t i) const;
206  void getColumnValues(const MDLabel label, std::vector<MDObject> &valuesOut) const override;
207  void setColumnValues(const std::vector<MDObject> &valuesIn) override;
208 
209  template<class T>
210  void setColumnValues(const MDLabel label, const std::vector<T> &valuesIn) {
211  return MetaData::setColumnValues(label, valuesIn);
212  }
213 
214  bool setRow(const MDRow &row, size_t id);
215 
216  template<class T>
217  std::vector<T> getColumnValues(const MDLabel label) const {
218  return MetaData::getColumnValues<T>(label);
219  }
220 
221  template<class T>
222  void getColumnValues(const MDLabel label, std::vector<T> &valuesOut) const {
223  return MetaData::getColumnValues(label, valuesOut);
224  }
225 
228  bool isEmpty() const override;
229 
232  size_t size() const override;
233 
236  bool containsLabel(const MDLabel label) const override;
237 
238  size_t firstRowId() const override;
239  size_t firstObject(const MDQuery&) const override;
240  size_t lastRowId() const override;
241 
253  bool addLabel(const MDLabel label, int pos = -1) override;
254 
259  bool removeLabel(const MDLabel label) override;
260 
267  size_t addObject() override;
268 
279  void importObject(const MetaData &md, const size_t id, bool doClear=true) override;
280  void importObjects(const MetaData &md, const std::vector<size_t> &objectsToAdd, bool doClear=true) override;
281  void importObjects(const MetaData &md, const MDQuery &query, bool doClear=true) override;
282 
287  bool removeObject(size_t id) override;
288 
293  void removeObjects(const std::vector<size_t> &toRemove) override;
294 
301  int removeObjects() override;
302  int removeObjects(const MDQuery&) override;
303 
312  void findObjects(std::vector<size_t> &objectsOut, const MDQuery &query) const override;
313  void findObjects(std::vector<size_t> &objectsOut, int limit = -1) const override;
314 
315  size_t countObjects(const MDQuery&) const override;
316  bool containsObject(size_t objectId) const override;
317  bool containsObject(const MDQuery&) const override;
318 
321  bool containsObject(size_t objectId);
322 
330  void _writeRows(std::ostream &os) const override;
331 
337  void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const;
338 
339 
342  void write(std::ostream &os, const String & blockName="",WriteModeMetaData mode=MD_OVERWRITE) const;
343 
347  void readXML(const FileName &inFile,
348  const std::vector<MDLabel> *desiredLabels = nullptr,
349  const String & blockRegExp=DEFAULT_BLOCK_NAME,
350  bool decomposeStack=true);
351 
357  void read(const FileName &inFile, const std::vector<MDLabel> *desiredLabels = nullptr, bool decomposeStack = true) override;
364  void readPlain(const FileName &inFile, const String &labelsString, const String &separator = " ");
369  void addPlain(const FileName &inFile, const String &labelsString, const String &separator=" ");
370 
371 
375  double getColumnMax(MDLabel column);
376 
377  double getColumnMin(MDLabel column);
378 
379 
384  void replace(const MDLabel label, const String &oldStr, const String &newStr);
385 
390  void randomize(const MetaData &MDin);
391 
394  void removeDuplicates(MetaData &MDin, MDLabel label=MDL_UNDEFINED);
395 
396  /*
397  * Sort a Metadata by a label.
398  * Sort the content of MDin comparing
399  * the label supplied, the result will
400  * be in the "calling" MetaData.
401  * Limit fixes the maximum number of returned rows
402  * Offset skips the first N rows
403  */
404  void sort(const MetaDataVec &MDin,
405  const MDLabel sortLabel,
406  bool asc=true,
407  int limit=-1,
408  int offset=0);
409 
410 
411  /*
412  * Sort a Metadata by a label.
413  * Sort the content of MDin comparing
414  * the label supplied, the result will
415  * be in the "calling" MetaData.
416  * If the input label is a vector field,
417  * you may supply label:col, to sort by that column,
418  * e.g., NMADisplacements:0
419  * Limit fixes the maximum number of returned rows
420  * Offset skips the first N rows
421  *
422  */
423  void sort(MetaDataVec &MDin, const String &sortLabel, bool asc=true, int limit=-1, int offset=0);
424 
436  void split(size_t n, std::vector<MetaDataVec> &results,
437  const MDLabel sortLabel=MDL_OBJID) const;
438 
440  void selectRandomSubset(const MetaData &mdIn, size_t numberOfObjects, const MDLabel sortLabel=MDL_OBJID) override;
441 
448  void selectPart(const MetaData &mdIn, size_t startPosition, size_t numberOfObjects,
449  const MDLabel sortLabel=MDL_OBJID) override;
450 
454  void makeAbsPath(const MDLabel label=MDL_IMAGE);
455 
462  template <bool IsConst>
463  struct MDVecRowIterator : public MDBaseRowIterator<IsConst> {
464  private:
466  size_t _i;
468  std::unique_ptr<RowType> _row;
469 
470  public:
472  : _mdv(mdv), _i(i) {
473  if (_i < _mdv.size())
474  _row.reset(new RowType(mdv._rows.at(i), i, mdv._label_to_col, mdv._col_to_label, mdv._no_columns));
475  else
476  _row = nullptr;
477  }
478 
479  std::unique_ptr<MDBaseRowIterator<IsConst>> clone() override {
480  return memoryUtils::make_unique<MDVecRowIterator<IsConst>>(_mdv, _i);
481  }
482 
483  void increment() override {
484  _i++;
485  if (_i < _mdv.size())
486  _row.reset(new RowType(_mdv._rows.at(_i), _i, _mdv._label_to_col, _mdv._col_to_label, _mdv._no_columns));
487  else
488  _row = nullptr;
489  }
490 
491  bool operator==(const MDBaseRowIterator<IsConst>& other) const override {
492  const MDVecRowIterator* vri = dynamic_cast<const MDVecRowIterator<IsConst>*>(&other);
493  if (vri != nullptr)
494  return _i == vri->_i;
495  return false;
496  }
497 
499  };
500 
501  iterator begin() override {
502  return {memoryUtils::make_unique<MDVecRowIterator<false>>(*this, 0)};
503  }
504  iterator end() override {
505  return {memoryUtils::make_unique<MDVecRowIterator<false>>(*this, this->size())};
506  }
507 
508  const_iterator begin() const override {
509  return {memoryUtils::make_unique<MDVecRowIterator<true>>(*this, 0)};
510  }
511  const_iterator end() const override {
512  return {memoryUtils::make_unique<MDVecRowIterator<true>>(*this, this->size())};
513  }
514 
515 
516  template <bool IsConst>
517  struct MDVecIdIterator : public MDBaseIdIterator<IsConst> {
518  private:
519  const MetaDataVec& _mdv;
520  size_t _i;
521 
522  public:
523  MDVecIdIterator(const MetaDataVec& mdv, size_t i)
524  : _mdv(mdv), _i(i) {}
525 
526  bool operator==(const MDBaseIdIterator<IsConst>& other) const override {
527  const MDVecIdIterator<IsConst>* dri = dynamic_cast<const MDVecIdIterator<IsConst>*>(&other);
528  if (dri != nullptr)
529  return this->_i == dri->_i;
530  return false;
531  }
532 
533  size_t operator*() override { return _mdv.getRowId(_i); }
534 
535  void increment() override { this->_i++; }
536 
537  std::unique_ptr<MDBaseIdIterator<IsConst>> clone() override {
538  return memoryUtils::make_unique<MDVecIdIterator<IsConst>>(_mdv, _i);
539  }
540  };
541 
542  id_iterator id_begin() override {
543  return {memoryUtils::make_unique<MDVecIdIterator<false>>(*this, 0)};
544  }
545 
546  id_iterator id_end() override {
547  return {memoryUtils::make_unique<MDVecIdIterator<false>>(*this, this->size())};
548  }
549 
550  id_const_iterator id_begin() const override {
551  return {memoryUtils::make_unique<MDVecIdIterator<true>>(*this, 0)};
552  }
553 
554  id_const_iterator id_end() const override {
555  return {memoryUtils::make_unique<MDVecIdIterator<true>>(*this, this->size())};
556  }
557 
560  void fillConstant(MDLabel label, const String &value) override;
561  void fillRandom(MDLabel label, const String &mode, double op1, double op2, double op3=0.) override;
562  void fillLinear(MDLabel label, double initial, double step) override;
563 
564  void copyColumn(MDLabel labelDest, MDLabel labelSrc) override;
565  void copyColumnTo(MetaData& md, MDLabel labelDest, MDLabel labelSrc) override;
566 
567  void renameColumn(MDLabel oldLabel, MDLabel newLabel) override;
568  void renameColumn(const std::vector<MDLabel> &oldLabel,
569  const std::vector<MDLabel> &newLabel) override;
570 
571  bool operator==(const MetaDataVec& op) const;
572 };//class MetaDataVec
573 
574 std::ostream& operator<<(std::ostream& o, const MetaDataVec& md);
575 
576 #endif
object id (int), NOTE: This label is special and shouldn&#39;t be used
size_t lastRowId() const override
virtual bool setValueCol(const MDObject &mdValueIn)=0
void init(const std::vector< MDLabel > &labelsVector)
id_iterator id_begin() override
Definition: metadata_vec.h:542
std::vector< MDObject > MetaDataVecRow
Definition: metadata_vec.h:37
bool _match(const MetaDataVecRow &, const MDQuery &) const
id_const_iterator id_begin() const override
Definition: metadata_vec.h:550
bool operator==(const MDBaseRowIterator< IsConst > &other) const override
Definition: metadata_vec.h:491
void removeDuplicates(MetaData &MDin, MDLabel label=MDL_UNDEFINED)
void sort(const MetaDataVec &MDin, const MDLabel sortLabel, bool asc=true, int limit=-1, int offset=0)
std::ostream & operator<<(std::ostream &o, const MetaDataVec &md)
void setColumnValues(const std::vector< MDObject > &valuesIn) override
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void getColumnValues(const MDLabel label, std::vector< T > &valuesOut) const
Definition: metadata_vec.h:222
iterator begin() override
Definition: metadata_vec.h:501
iterator end() override
Definition: metadata_vec.h:504
size_t _no_columns
Definition: metadata_vec.h:51
void readXML(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, const String &blockRegExp=DEFAULT_BLOCK_NAME, bool decomposeStack=true)
void setColumnValues(const MDLabel label, const std::vector< T > &valuesIn)
Definition: metadata_vec.h:210
const_iterator begin() const override
Definition: metadata_vec.h:508
bool removeLabel(const MDLabel label) override
bool removeObject(size_t id) override
void writeText(const FileName fn, const std::vector< MDLabel > *desiredLabels) const override
double getColumnMax(MDLabel column)
int _rowIndex(size_t id) const
void replace(const MDLabel label, const String &oldStr, const String &newStr)
void selectPart(const MetaData &mdIn, size_t startPosition, size_t numberOfObjects, const MDLabel sortLabel=MDL_OBJID) override
void split(size_t n, std::vector< MetaDataVec > &results, const MDLabel sortLabel=MDL_OBJID) const
size_t firstObject(const MDQuery &) const override
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
void addPlain(const FileName &inFile, const String &labelsString, const String &separator=" ")
std::vector< T > getColumnValues(const MDLabel label) const
Definition: metadata_vec.h:217
std::unique_ptr< MDRow > getRow(size_t id) override
void randomize(const MetaData &MDin)
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0
size_t size() const override
#define i
size_t addRow(const MDRow &row) override
const_iterator end() const override
Definition: metadata_vec.h:511
void clear() override
void addRows(const std::vector< MDRowVec > &rows)
bool isEmpty() const override
double getColumnMin(MDLabel column)
virtual ~MetaDataVec()
Definition: metadata_vec.h:121
std::unordered_map< size_t, size_t > _id_to_index
Definition: metadata_vec.h:52
doublereal * b
void _recalc_id_to_index()
size_t _next_id
Definition: metadata_vec.h:53
viol index
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
void renameColumn(MDLabel oldLabel, MDLabel newLabel) override
void readPlain(const FileName &inFile, const String &labelsString, const String &separator=" ")
size_t firstRowId() const override
void importObjects(const MetaData &md, const std::vector< size_t > &objectsToAdd, bool doClear=true) override
std::array< int, MDL_LAST_LABEL > _label_to_col
Definition: metadata_vec.h:49
void _writeRows(std::ostream &os) const override
void findObjects(std::vector< size_t > &objectsOut, const MDQuery &query) const override
std::vector< MDObject > getObjectsForActiveLabels() const
id_const_iterator id_end() const override
Definition: metadata_vec.h:554
const T getValue(const MDLabel label, size_t id) const
Definition: metadata_vec.h:192
void _expand(MetaDataVecRow &, const MDLabel)
id_iterator id_end() override
Definition: metadata_vec.h:546
void _clear(bool onlyData=false)
bool getRowValues(size_t id, std::vector< MDObject > &values) const override
void mode
bool getValue(const MDLabel label, T &valueOut, size_t id) const
Definition: metadata_vec.h:182
void fillConstant(MDLabel label, const String &value) override
size_t operator*() override
Definition: metadata_vec.h:533
void makeAbsPath(const MDLabel label=MDL_IMAGE)
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
int getMaxStringLength(const MDLabel thisLabel) const override
void importObject(const MetaData &md, const size_t id, bool doClear=true) override
std::vector< T > getColumnValues(const MDLabel label) const
int removeObjects() override
std::vector< MetaDataVecRow > _rows
Definition: metadata_vec.h:48
bool setValueCol(const MDObject &mdValueIn) override
size_t getRowId(const MetaDataVecRow &) const
bool getValue(MDObject &mdValueOut, size_t id) const override
bool containsObject(size_t objectId) const override
void selectRandomSubset(const MetaData &mdIn, size_t numberOfObjects, const MDLabel sortLabel=MDL_OBJID) override
bool addLabel(const MDLabel label, int pos=-1) override
void copyColumnTo(MetaData &md, MDLabel labelDest, MDLabel labelSrc) override
bool setValue(const MDLabel label, const T &valueIn, size_t id)
void getColumnValues(const MDLabel label, std::vector< MDObject > &valuesOut) const override
const MDObject & _getObject(size_t i, MDLabel label) const
bool operator==(const MDBaseIdIterator< IsConst > &other) const override
Definition: metadata_vec.h:526
void _setRow(const MDRow &row, size_t index)
int _labelIndex(MDLabel label) const
MDRowVec getRowVec(size_t id)
std::string String
Definition: xmipp_strings.h:34
std::unique_ptr< MDBaseIdIterator< IsConst > > clone() override
Definition: metadata_vec.h:537
T getValue(const MDLabel label, size_t id)
Definition: metadata_vec.h:187
MDVecRowIterator(typename TypeHelpers::choose< IsConst, const MetaDataVec &, MetaDataVec &>::type &mdv, size_t i)
Definition: metadata_vec.h:471
void fillLinear(MDLabel label, double initial, double step) override
std::unique_ptr< MDBaseRowIterator< IsConst > > clone() override
Definition: metadata_vec.h:479
bool _contains(const std::vector< MetaDataVecRow > &, const MetaDataVecRow &) const
bool setValue(const MDLabel label, const T &valueIn, size_t id)
Definition: metadata_vec.h:173
MDVecIdIterator(const MetaDataVec &mdv, size_t i)
Definition: metadata_vec.h:523
void setColumnValues(const MDLabel label, const std::vector< T > &valuesIn)
void writeXML(const FileName fn, const FileName blockname, WriteModeMetaData mode) const override
bool _rowsEq(const MetaDataVecRow &a, const MetaDataVecRow &b) const
void _parseObjects(std::istream &is, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels, bool firstTime) override
bool containsLabel(const MDLabel label) const override
TypeHelpers::choose< IsConst, const MDRowVec &, MDRowVec & >::type operator*() override
Definition: metadata_vec.h:498
void fillRandom(MDLabel label, const String &mode, double op1, double op2, double op3=0.) override
size_t _rowIndexSafe(size_t id) const
void copyColumn(MDLabel labelDest, MDLabel labelSrc) override
bool setRow(const MDRow &row, size_t id)
#define DEFAULT_BLOCK_NAME
Definition: metadata_base.h:60
WriteModeMetaData
int * n
Name of an image (std::string)
std::vector< MDLabel > _col_to_label
Definition: metadata_vec.h:50
MDLabel
doublereal * a
bool setValueCol(const MDLabel label, const T &valueIn)
Definition: metadata_vec.h:160
size_t countObjects(const MDQuery &) const override
std::vector< MDLabel > getActiveLabels() const override