Xmipp  v3.23.11-Nereus
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
MetaData Class Referenceabstract

#include <metadata_base.h>

Inheritance diagram for MetaData:
Inheritance graph
[legend]
Collaboration diagram for MetaData:
Collaboration graph
[legend]

Classes

struct  idIterator
 
struct  IdIteratorProxy
 
struct  rowIterator
 

Public Member Functions

virtual void fillConstant (MDLabel label, const String &value)=0
 
virtual void fillRandom (MDLabel label, const String &mode, double op1, double op2, double op3=0.)=0
 
virtual void fillLinear (MDLabel label, double initial, double step)=0
 
virtual void copyColumn (MDLabel labelDest, MDLabel labelSrc)=0
 
virtual void copyColumnTo (MetaData &md, MDLabel labelDest, MDLabel labelSrc)=0
 
virtual void renameColumn (MDLabel oldLabel, MDLabel newLabel)=0
 
virtual void renameColumn (const std::vector< MDLabel > &oldLabel, const std::vector< MDLabel > &newLabel)=0
 
virtual bool nextBlock (mdBuffer &buffer, mdBlock &block)
 
Constructors
 MetaData ()=default
 
virtual ~MetaData ()
 
virtual void clear ()
 
Getters and setters
virtual bool isColumnFormat () const
 
virtual void setMaxRows (size_t maxRows=0)
 
virtual size_t getParsedLines ()
 
virtual void setPrecission (int _precision)
 
virtual void setColumnFormat (bool column)
 
virtual void writeXML (const FileName fn, const FileName blockname, WriteModeMetaData mode) const =0
 
virtual void writeText (const FileName fn, const std::vector< MDLabel > *desiredLabels) const =0
 
virtual void writeStar (const FileName &outFile, const String &blockName, WriteModeMetaData mode) const
 
virtual String getComment () const
 
virtual void setComment (const String &newComment="No comment")
 
virtual FileName getFilename () const
 
virtual void setFilename (const FileName &_filename)
 
virtual std::vector< MDLabelgetActiveLabels () const =0
 
virtual int getMaxStringLength (const MDLabel thisLabel) const =0
 
MetaData Manipulation
virtual bool setValueCol (const MDObject &mdValueIn)=0
 
template<class T >
bool setValueCol (const MDLabel label, const T &valueIn)
 
template<class T >
bool setValue (const MDLabel label, const T &valueIn, size_t id)
 
virtual size_t addRow (const MDRow &row)=0
 
MetaDataoperator= (const MetaData &md)
 
virtual bool setValue (const MDObject &mdValueIn, size_t id)=0
 
virtual bool getValue (MDObject &mdValueOut, size_t id) const =0
 
template<class T >
getValue (const MDLabel label, size_t id) const
 
template<class T >
bool getValue (const MDLabel label, T &valueOut, size_t id) const
 
template<class T >
const T & getValueOrAbort (const MDLabel label, size_t id) const
 
template<class T >
T & getValueOrAbort (const MDLabel label, size_t id)
 
template<class T >
void getValueOrAbort (const MDLabel label, T &valueOut, size_t id) const
 
template<typename T >
const T getValueOrDefault (const MDLabel label, size_t id, const T &_default) const
 
template<typename T >
getValueOrDefault (const MDLabel label, size_t id, T &_default)
 
template<typename T , typename T1 >
void getValueOrDefault (const MDLabel label, T &valueOut, size_t id, const T1 &_default) const
 
template<class T >
std::vector< T > getColumnValues (const MDLabel label) const
 
template<class T >
void getColumnValues (const MDLabel label, std::vector< T > &out) const
 
virtual bool getRowValues (size_t id, std::vector< MDObject > &values) const =0
 
virtual void getColumnValues (const MDLabel label, std::vector< MDObject > &valuesOut) const =0
 
template<class T >
void setColumnValues (const MDLabel label, const std::vector< T > &valuesIn)
 
virtual void setColumnValues (const std::vector< MDObject > &valuesIn)=0
 
virtual std::unique_ptr< MDRowgetRow (size_t id)=0
 
virtual std::unique_ptr< const MDRowgetRow (size_t id) const =0
 
virtual bool setValueFromStr (const MDLabel label, const String &value, size_t id)
 
virtual bool getStrFromValue (const MDLabel label, String &strOut, size_t id) const
 
virtual bool isEmpty () const
 
virtual size_t size () const =0
 
virtual bool containsLabel (const MDLabel label) const =0
 
virtual bool addLabel (const MDLabel label, int pos=-1)=0
 
virtual bool removeLabel (const MDLabel label)=0
 
void keepLabels (const std::vector< MDLabel > &labels)
 
virtual size_t addObject ()=0
 
virtual void importObject (const MetaData &md, const size_t id, bool doClear=true)=0
 
virtual void importObjects (const MetaData &md, const std::vector< size_t > &objectsToAdd, bool doClear=true)=0
 
virtual void importObjects (const MetaData &md, const MDQuery &query, bool doClear=true)=0
 
virtual bool removeObject (size_t id)=0
 
virtual void removeObjects (const std::vector< size_t > &toRemove)=0
 
virtual int removeObjects (const MDQuery &)=0
 
virtual int removeObjects ()=0
 
Iteration functions
virtual size_t firstRowId () const =0
 
virtual size_t firstObject (const MDQuery &) const =0
 
virtual size_t lastRowId () const =0
 
Search operations
virtual void findObjects (std::vector< size_t > &objectsOut, const MDQuery &query) const =0
 
virtual void findObjects (std::vector< size_t > &objectsOut, int limit=-1) const =0
 
virtual size_t countObjects (const MDQuery &) const =0
 
virtual bool containsObject (size_t objectId) const =0
 
virtual bool containsObject (const MDQuery &) const =0
 
I/O functions
virtual void _writeRows (std::ostream &os) const =0
 
virtual void write (const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const =0
 
virtual void write (std::ostream &os, const String &blockName="", WriteModeMetaData mode=MD_OVERWRITE) const =0
 
virtual void print () const
 
void append (const FileName &outFile) const
 
virtual void read (const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true)=0
 
Set Operations
virtual void removeDisabled ()
 
virtual void selectRandomSubset (const MetaData &mdIn, size_t numberOfObjects, const MDLabel sortLabel=MDL_OBJID)=0
 
virtual void selectPart (const MetaData &mdIn, size_t startPosition, size_t numberOfObjects, const MDLabel sortLabel=MDL_OBJID)=0
 

Public Attributes

bool isMetadataFile
 

Protected Member Functions

void copyInfo (const MetaData &md)
 
double precision () const
 
virtual void readStar (const FileName &filename, const std::vector< MDLabel > *desiredLabels, const String &blockRegExp, bool decomposeStack)
 
virtual void _readColumns (std::istream &is, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels=nullptr)
 
virtual void _readColumnsStar (mdBlock &block, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels, bool addColumns=true, size_t id=BAD_OBJID)
 
virtual void _parseObject (std::istream &is, MDObject &object, size_t id=BAD_OBJID)
 
virtual void _parseObjects (std::istream &is, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels, bool firstTime)=0
 
virtual void _readRows (std::istream &is, std::vector< MDObject *> &columnValues, bool useCommentAsImage)
 
virtual void _readRowsStar (mdBlock &block, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels)
 

Protected Attributes

std::map< String, size_t > _fastStringSearch
 
MDLabel _fastStringSearchLabel
 
String _comment
 
int _precision = 1000
 
FileName _inFile
 
size_t _maxRows = 0
 
size_t _parsedLines = 0
 

Iterators

using iterator = rowIterator< false >
 
using const_iterator = rowIterator< true >
 
using id_iterator = idIterator< false >
 
using id_const_iterator = idIterator< true >
 
struct MDBaseRowIterator< false >
 
struct MDBaseRowIterator< true >
 
virtual iterator begin ()=0
 
virtual iterator end ()=0
 
virtual const_iterator begin () const =0
 
virtual const_iterator end () const =0
 
virtual id_iterator id_begin ()=0
 
virtual id_iterator id_end ()=0
 
virtual id_const_iterator id_begin () const =0
 
virtual id_const_iterator id_end () const =0
 
virtual IdIteratorProxy< false > ids ()
 
virtual IdIteratorProxy< true > ids () const
 

Detailed Description

Definition of API of all MetaDatas.

General information about metadata

MetaData* class(es) implement a way to store metadata in xmipp programs.

There was a single original database (MetaDataDb) implementation of MetaData till 2021, when MetaData were split into MetaDataDb & MetaDataVec with aim to achive higner speeds via saving metadata in std::vectors instead of sql database.

Current MetaData implementation:

  1. metadata_base.(h|cpp): common MetaData API definition
  2. metadata_vec.(h|cpp): vector MetaData implementation
  3. metadata_db.(h|cpp): old datababse MetaData implementation

MetaDataBase

MetaData class cannot be instantiated however it could be passed to functions as reference or pointer where only common MetaData functions are required:

This code doesn't compile: void roo(MetaData& md) { md.someDbSpeficicOperation(); }

This code compiles: void roo(MetaDataDb& md) { md.someDbSpeficicOperation(); }

Iterating over MetaData

You can iterate directly over abstract MetaData as well as over specific MetaData*. You can iterate over:

  1. Ids of rows (arbitrary size_t): for (size_t id : md.ids())
  2. Rows: for (const MDRow& row : md)

Useful information

Definition at line 176 of file metadata_base.h.

Member Typedef Documentation

◆ const_iterator

Definition at line 706 of file metadata_base.h.

◆ id_const_iterator

Definition at line 736 of file metadata_base.h.

◆ id_iterator

Definition at line 735 of file metadata_base.h.

◆ iterator

Definition at line 705 of file metadata_base.h.

Constructor & Destructor Documentation

◆ MetaData()

MetaData::MetaData ( )
default

Empty Constructor.

The MetaData is created with no data stored on it. You can fill in it programmatically or by a later reading from a MetaData file or old Xmipp formatted type. if labels vectors is passed this labels are created on metadata

◆ ~MetaData()

MetaData::~MetaData ( )
virtual

Definition at line 105 of file metadata_base.cpp.

105 {}

Member Function Documentation

◆ _parseObject()

void MetaData::_parseObject ( std::istream &  is,
MDObject object,
size_t  id = BAD_OBJID 
)
protectedvirtual

Definition at line 325 of file metadata_base.cpp.

325  {
326  object.fromStream(is);
327  if (is.fail()) {
328  String errorMsg = formatString("MetaData: Error parsing column '%s' value.", MDL::label2Str(object.label).c_str());
329  object.failed = true;
330  std::cerr << "WARNING: " << errorMsg << std::endl;
331  //REPORT_ERROR(ERR_MD_BADLABEL, (String)"read: Error parsing data column, expecting " + MDL::label2Str(object.label));
332  } else {
333  if (object.label != MDL_UNDEFINED)
334  setValue(object, id);
335  }
336 }
bool setValue(const MDLabel label, const T &valueIn, size_t id)
std::string String
Definition: xmipp_strings.h:34
String formatString(const char *format,...)
static String label2Str(const MDLabel &label)

◆ _parseObjects()

virtual void MetaData::_parseObjects ( std::istream &  is,
std::vector< MDObject *> &  columnValues,
const std::vector< MDLabel > *  desiredLabels,
bool  firstTime 
)
protectedpure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ _readColumns()

void MetaData::_readColumns ( std::istream &  is,
std::vector< MDObject *> &  columnValues,
const std::vector< MDLabel > *  desiredLabels = nullptr 
)
protectedvirtual

Definition at line 223 of file metadata_base.cpp.

224  {
225  String token;
226  MDLabel label;
227 
228  while (is >> token)
229  if (token.find('(') == String::npos)
230  {
231  //label is not recognized, the MDValue will be created
232  //with MDL_UNDEFINED, which will be ignored while reading data
233  label = MDL::str2Label(token);
234 
235  // Try to read undefined labels as String using the buffer approach
236  if (label == MDL_UNDEFINED)
237  label = MDL::getNewAlias(token);
238 
239  if (desiredLabels != NULL && !vectorContainsLabel(*desiredLabels, label))
240  label = MDL_UNDEFINED; //ignore if not present in desiredLabels
241  columnValues.emplace_back(new MDObject(label));
242  if (label != MDL_UNDEFINED)
243  addLabel(label);
244 
245  }
246 }
static MDLabel str2Label(const String &labelName)
static MDLabel getNewAlias(const String &alias, MDLabelType type=LABEL_NOTYPE)
bool vectorContainsLabel(const std::vector< MDLabel > &labelsVector, const MDLabel label)
std::string String
Definition: xmipp_strings.h:34
virtual bool addLabel(const MDLabel label, int pos=-1)=0
MDLabel

◆ _readColumnsStar()

void MetaData::_readColumnsStar ( mdBlock block,
std::vector< MDObject *> &  columnValues,
const std::vector< MDLabel > *  desiredLabels,
bool  addColumns = true,
size_t  id = BAD_OBJID 
)
protectedvirtual

Definition at line 253 of file metadata_base.cpp.

257  {
258  char * end = block.end;
259  char * newline = NULL;
260  bool found_column;
261  MDLabel label;
262  char * iter = block.loop;
263  if (!_isColumnFormat)
264  {
265  iter = block.begin;
266  iter = END_OF_LINE() + 1; //this should point at first label, after data_XXX
267  }
268 
269  do
270  {
271  found_column = false;
272  while (iter[0] == '#') //Skip comment
273  iter = END_OF_LINE() + 1;
274 
275  //trim spaces and newlines at the beginning
276  while ( isspace(iter[0]))
277  ++iter;
278 
279  if (iter < end && iter[0] == '_')
280  {
281  found_column = true;
282  ++iter; //shift _
283  std::stringstream ss;
284  newline = END_OF_LINE();
285  //Last label and no data needs this check
286  if (newline == NULL)
287  newline = end;
288  String s(iter, newline - iter);//get current line
289  ss.str(s);//set the string of the stream
290  //Take the first token which is the label
291  //if the label contain spaces will fail
292  ss >> s; //get the first token, the label
293  label = MDL::str2Label(s);
294  if (label == MDL_UNDEFINED)
295  label = MDL::getNewAlias(s);
296  if (desiredLabels != NULL && !vectorContainsLabel(*desiredLabels, label))
297  label = MDL_UNDEFINED; //ignore if not present in desiredLabels
298 
299  if (label != MDL_UNDEFINED)
300  addLabel(label);
301 
302  if (addColumns)
303  {
304  MDObject * _mdObject = new MDObject(label);
305  columnValues.emplace_back(_mdObject);//add the value here with a char
306  if(!_isColumnFormat)
307  _parseObject(ss, *_mdObject, id);
308  }
309  iter = newline + 1;//go to next line character
310  }
311  }
312  while (found_column)
313  ;
314 
315  // This condition fails for empty blocks
316  // if (iter < block.end)
317  if (iter <= block.end +1)
318  block.loop = iter; //Move loop pointer to position of last found column
319 }
static MDLabel str2Label(const String &labelName)
#define END_OF_LINE()
Definition: metadata_base.h:69
char * end
Definition: metadata_base.h:96
glob_prnt iter
static MDLabel getNewAlias(const String &alias, MDLabelType type=LABEL_NOTYPE)
bool vectorContainsLabel(const std::vector< MDLabel > &labelsVector, const MDLabel label)
char * loop
Definition: metadata_base.h:97
char * begin
Definition: metadata_base.h:94
std::string String
Definition: xmipp_strings.h:34
virtual bool addLabel(const MDLabel label, int pos=-1)=0
virtual iterator end()=0
MDLabel
virtual void _parseObject(std::istream &is, MDObject &object, size_t id=BAD_OBJID)

◆ _readRows()

void MetaData::_readRows ( std::istream &  is,
std::vector< MDObject *> &  columnValues,
bool  useCommentAsImage 
)
protectedvirtual

Definition at line 379 of file metadata_base.cpp.

379  {
380  String line = "";
381  while (!is.eof() && !is.fail()) {
382  // Move until the ';' or the first alphanumeric character
383  while (is.peek() != ';' && isspace(is.peek()) && !is.eof())
384  is.ignore(1);
385  if (!is.eof()) {
386  if (is.peek() == ';') { //is a comment
387  is.ignore(1); //ignore the ';'
388  getline(is, line);
389  trim(line);
390  } else if (!isspace(is.peek())) {
391  size_t id = addObject();
392  if (line != "") { //this is for old format files
393  if (!useCommentAsImage)
394  this->setValue(MDObject(MDL_STAR_COMMENT, line), id);
395  else
396  this->setValue(MDObject(MDL_IMAGE, line), id);
397  }
398  int nCol = columnValues.size();
399  for (int i = 0; i < nCol; ++i)
400  this->_parseObject(is, *(columnValues[i]), id);
401  }
402  }
403  }
404 }
virtual size_t addObject()=0
void trim(std::string &s)
Definition: text.cpp:205
#define i
bool setValue(const MDLabel label, const T &valueIn, size_t id)
std::string String
Definition: xmipp_strings.h:34
Name of an image (std::string)
virtual void _parseObject(std::istream &is, MDObject &object, size_t id=BAD_OBJID)
A comment for this object /*** NOTE THIS IS A SPECIAL CASE AND SO IS TREATED ***/.

◆ _readRowsStar()

void MetaData::_readRowsStar ( mdBlock block,
std::vector< MDObject *> &  columnValues,
const std::vector< MDLabel > *  desiredLabels 
)
protectedvirtual

This function will be used to parse the rows data in START format

Parameters
[out]columnValuesMDRow with values to fill in
pchStartpointer to the position of '_loop' in memory
pEndpointer to the position of the next '_data' in memory
maxRowsif this number if greater than 0, only this number of rows will be parsed.

Reimplemented in MetaDataDb.

Definition at line 340 of file metadata_base.cpp.

341  {
342  String line;
343  std::stringstream ss;
344  size_t n = block.end - block.loop;
345  bool firstTime = true;
346 
347  if (n == 0)
348  return;
349 
350  char * buffer = new char[n];
351  memcpy(buffer, block.loop, n);
352  char *iter = buffer, *end = iter + n, * newline = NULL;
353  _parsedLines = 0; //Check how many lines the md have
354 
355  while (iter < end) { //while there are data lines
356  //Adding \n position and check if NULL at the same time
357  if (!(newline = END_OF_LINE()))
358  newline = end;
359  line.assign(iter, newline - iter);
360  trim(line);
361 
362  if (!line.empty() && line[0] != '#') {
363  //_maxRows would be > 0 if we only want to read some
364  // rows from the md for performance reasons...
365  // anyway the number of lines will be counted in _parsedLines
366  if (_maxRows == 0 || _parsedLines < _maxRows) {
367  std::stringstream ss(line);
368  this->_parseObjects(ss, columnValues, desiredLabels, firstTime);
369  firstTime = false;
370  }
371  _parsedLines++;
372  }
373  iter = newline + 1; //go to next line
374  }
375 
376  delete[] buffer;
377 }
#define END_OF_LINE()
Definition: metadata_base.h:69
size_t _parsedLines
char * end
Definition: metadata_base.h:96
HBITMAP buffer
Definition: svm-toy.cpp:37
void trim(std::string &s)
Definition: text.cpp:205
glob_prnt iter
virtual void _parseObjects(std::istream &is, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels, bool firstTime)=0
size_t _maxRows
char * loop
Definition: metadata_base.h:97
std::string String
Definition: xmipp_strings.h:34
virtual iterator end()=0
int * n

◆ _writeRows()

virtual void MetaData::_writeRows ( std::ostream &  os) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ addLabel()

virtual bool MetaData::addLabel ( const MDLabel  label,
int  pos = -1 
)
pure virtual

Add a new label to the metadata. By default the label is added at the end, if the position is specified and is between 0 and n-1 the new label is inserted at that position.

Implemented in MetaDataDb, and MetaDataVec.

◆ addObject()

virtual size_t MetaData::addObject ( )
pure virtual

Adds a new, empty object to the objects map. If objectId == -1 the new ID will be that for the last object inserted + 1, else the given objectId is used. If there is already an object whose objectId == input objectId, just removes it and creates an empty one

Implemented in MetaDataDb, and MetaDataVec.

◆ addRow()

virtual size_t MetaData::addRow ( const MDRow row)
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ append()

void MetaData::append ( const FileName outFile) const

Append data lines to file. This function can be used to add new data to an existing metadata. Now should be used with files with only one metadata, maybe can be extended later. For now it will not check any compatibility beetween the existent metadata and the new data to append.

Definition at line 647 of file metadata_base.cpp.

648 {
649  if (outFile.exists())
650  {
651  std::ofstream ofs(outFile.c_str(), std::ios_base::app);
652  _writeRows(ofs);
653  ofs.close();
654  }
655  else
656  write(outFile);
657 }
virtual void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const =0
virtual void _writeRows(std::ostream &os) const =0
bool exists() const

◆ begin() [1/2]

virtual iterator MetaData::begin ( )
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ begin() [2/2]

virtual const_iterator MetaData::begin ( ) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ clear()

void MetaData::clear ( )
virtual

Clear all data

Reimplemented in MetaDataDb, and MetaDataVec.

Definition at line 152 of file metadata_base.cpp.

152  {
153  _comment.clear();
154  _fastStringSearch.clear();
156 
157  _isColumnFormat = true;
158  _inFile = FileName();
159  _precision = 1000;
160  _parsedLines = 0;
161 }
size_t _parsedLines
FileName _inFile
String _comment
std::map< String, size_t > _fastStringSearch
MDLabel _fastStringSearchLabel
int _precision

◆ containsLabel()

virtual bool MetaData::containsLabel ( const MDLabel  label) const
pure virtual

Check whether a label is contained in metadata.

Implemented in MetaDataDb, and MetaDataVec.

◆ containsObject() [1/2]

virtual bool MetaData::containsObject ( size_t  objectId) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ containsObject() [2/2]

virtual bool MetaData::containsObject ( const MDQuery ) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ copyColumn()

virtual void MetaData::copyColumn ( MDLabel  labelDest,
MDLabel  labelSrc 
)
pure virtual

Copy all values from one column to another. Source column should exist

Implemented in MetaDataDb, and MetaDataVec.

◆ copyColumnTo()

virtual void MetaData::copyColumnTo ( MetaData md,
MDLabel  labelDest,
MDLabel  labelSrc 
)
pure virtual

Same as previous, but copy to another metadata

Implemented in MetaDataDb, and MetaDataVec.

◆ copyInfo()

void MetaData::copyInfo ( const MetaData md)
protected

Definition at line 163 of file metadata_base.cpp.

163  {
164  _comment = md._comment;
167 
168  _isColumnFormat = md._isColumnFormat;
169  _inFile = md._inFile;
170 }
FileName _inFile
String _comment
std::map< String, size_t > _fastStringSearch
MDLabel _fastStringSearchLabel

◆ countObjects()

virtual size_t MetaData::countObjects ( const MDQuery ) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ end() [1/2]

virtual iterator MetaData::end ( )
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ end() [2/2]

virtual const_iterator MetaData::end ( ) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ fillConstant()

virtual void MetaData::fillConstant ( MDLabel  label,
const String value 
)
pure virtual

Fill column with constant value

Implemented in MetaDataDb, and MetaDataVec.

◆ fillLinear()

virtual void MetaData::fillLinear ( MDLabel  label,
double  initial,
double  step 
)
pure virtual

Fill lineal, starting at some value and with some step

Implemented in MetaDataDb, and MetaDataVec.

◆ fillRandom()

virtual void MetaData::fillRandom ( MDLabel  label,
const String mode,
double  op1,
double  op2,
double  op3 = 0. 
)
pure virtual

Fill column with random value mode should be: uniform, gaussian or student op1, op2 and op2 are interpreted for each mode: uniform: op1 and op2 are the limits of the interval gaussian: op1 and op2 are mean and std student: same as gaussian and use op3

Implemented in MetaDataDb, and MetaDataVec.

◆ findObjects() [1/2]

virtual void MetaData::findObjects ( std::vector< size_t > &  objectsOut,
const MDQuery query 
) const
pure virtual

Find all objects that match a query. if called without query, all objects are returned if limit is provided only return a maximun of 'limit'

Implemented in MetaDataDb, and MetaDataVec.

◆ findObjects() [2/2]

virtual void MetaData::findObjects ( std::vector< size_t > &  objectsOut,
int  limit = -1 
) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ firstObject()

virtual size_t MetaData::firstObject ( const MDQuery ) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ firstRowId()

virtual size_t MetaData::firstRowId ( ) const
pure virtual

Return the object id of the first element in metadata.

Implemented in MetaDataDb, and MetaDataVec.

◆ getActiveLabels()

virtual std::vector<MDLabel> MetaData::getActiveLabels ( ) const
pure virtual

Get safe access to active labels.

Implemented in MetaDataDb, and MetaDataVec.

◆ getColumnValues() [1/3]

template<class T >
std::vector<T> MetaData::getColumnValues ( const MDLabel  label) const
inline

Get all values of a column as a vector.

Definition at line 466 of file metadata_base.h.

466  {
467  std::vector<T> result;
468  MDObject mdValueOut(label);
469  std::vector<size_t> objectsId;
470  findObjects(objectsId);
471  size_t n = objectsId.size();
472  result.reserve(n);
473  for (size_t i = 0; i < n; ++i) {
474  if (!getValue(mdValueOut, objectsId[i]))
475  throw std::logic_error("Column does not exist!");
476  result.emplace_back(mdValueOut.getValue2(T()));
477  }
478  return result;
479  }
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0
#define i
virtual void findObjects(std::vector< size_t > &objectsOut, const MDQuery &query) const =0
int * n

◆ getColumnValues() [2/3]

template<class T >
void MetaData::getColumnValues ( const MDLabel  label,
std::vector< T > &  out 
) const
inline

Definition at line 483 of file metadata_base.h.

483  {
484  out = this->getColumnValues<T>(label);
485  }

◆ getColumnValues() [3/3]

virtual void MetaData::getColumnValues ( const MDLabel  label,
std::vector< MDObject > &  valuesOut 
) const
pure virtual

Get all values of a column as a vector.

Implemented in MetaDataDb, and MetaDataVec.

◆ getComment()

virtual String MetaData::getComment ( ) const
inlinevirtual

Get Header Comment. the comment will appear in second line.

Definition at line 310 of file metadata_base.h.

310 { return this->_comment; }
String _comment

◆ getFilename()

virtual FileName MetaData::getFilename ( ) const
inlinevirtual

Get metadata filename.

Definition at line 319 of file metadata_base.h.

319 { return this->_inFile; }
FileName _inFile

◆ getMaxStringLength()

virtual int MetaData::getMaxStringLength ( const MDLabel  thisLabel) const
pure virtual

Get maximum string length of column values.

Implemented in MetaDataDb, and MetaDataVec.

◆ getParsedLines()

virtual size_t MetaData::getParsedLines ( )
inlinevirtual

Return the number of lines in the metadata file. Serves to know the number of items even is read with maxRows != 0

Definition at line 283 of file metadata_base.h.

283 { return _parsedLines; }
size_t _parsedLines

◆ getRow() [1/2]

virtual std::unique_ptr<MDRow> MetaData::getRow ( size_t  id)
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ getRow() [2/2]

virtual std::unique_ptr<const MDRow> MetaData::getRow ( size_t  id) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ getRowValues()

virtual bool MetaData::getRowValues ( size_t  id,
std::vector< MDObject > &  values 
) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ getStrFromValue()

bool MetaData::getStrFromValue ( const MDLabel  label,
String strOut,
size_t  id 
) const
virtual

Get string representation from label value.

Definition at line 115 of file metadata_base.cpp.

115  {
116  MDObject mdValueOut(label);
117  if (!getValue(mdValueOut, id))
118  return false;
119  strOut = mdValueOut.toString();
120  return true;
121 }
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0

◆ getValue() [1/3]

virtual bool MetaData::getValue ( MDObject mdValueOut,
size_t  id 
) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ getValue() [2/3]

template<class T >
T MetaData::getValue ( const MDLabel  label,
size_t  id 
) const
inline

Get the value of some label. from the object that has id 'objectId' or from 'activeObject' if objectId=-1.

md.read("images.xmd");
FileName imageFn; *
{
md.getValue(MDL_IMAGE, imageFn);
std::out << "Image: " << imageFn);
}

Definition at line 409 of file metadata_base.h.

409  {
410  MDObject mdValueOut(label);
411  if (!getValue(mdValueOut, id))
412  throw ObjectDoesNotExist(label, id);
413  return mdValueOut.getValue2(T());
414  }
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0

◆ getValue() [3/3]

template<class T >
bool MetaData::getValue ( const MDLabel  label,
T &  valueOut,
size_t  id 
) const
inline

Definition at line 417 of file metadata_base.h.

417  {
418  MDObject mdValueOut(label);
419  if (!getValue(mdValueOut, id))
420  return false;
421  valueOut = mdValueOut.getValue2(T());
422  return true;
423  }
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0

◆ getValueOrAbort() [1/3]

template<class T >
const T& MetaData::getValueOrAbort ( const MDLabel  label,
size_t  id 
) const
inline

Definition at line 426 of file metadata_base.h.

426  {
427  return getValue<T>(label, id);
428  }

◆ getValueOrAbort() [2/3]

template<class T >
T& MetaData::getValueOrAbort ( const MDLabel  label,
size_t  id 
)
inline

Definition at line 431 of file metadata_base.h.

431  {
432  return getValue<T>(label, id);
433  }

◆ getValueOrAbort() [3/3]

template<class T >
void MetaData::getValueOrAbort ( const MDLabel  label,
T &  valueOut,
size_t  id 
) const
inline

Definition at line 436 of file metadata_base.h.

436  { // FIXME: deprecated
437  if (!getValue(label, valueOut,id))
438  REPORT_ERROR(ERR_ARG_MISSING,(String)"Cannot find label: " + MDL::label2Str(label));
439  }
Argument missing.
Definition: xmipp_error.h:114
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0
std::string String
Definition: xmipp_strings.h:34
static String label2Str(const MDLabel &label)

◆ getValueOrDefault() [1/3]

template<typename T >
const T MetaData::getValueOrDefault ( const MDLabel  label,
size_t  id,
const T &  _default 
) const
inline

Definition at line 442 of file metadata_base.h.

442  {
443  MDObject mdValueOut(label);
444  if (!getValue(mdValueOut, id))
445  return _default;
446  return mdValueOut.getValue2(T());
447  }
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0

◆ getValueOrDefault() [2/3]

template<typename T >
T MetaData::getValueOrDefault ( const MDLabel  label,
size_t  id,
T &  _default 
)
inline

Definition at line 450 of file metadata_base.h.

450  {
451  MDObject mdValueOut(label);
452  if (!getValue(mdValueOut, id))
453  return _default;
454  return mdValueOut.getValue2(T());
455  }
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0

◆ getValueOrDefault() [3/3]

template<typename T , typename T1 >
void MetaData::getValueOrDefault ( const MDLabel  label,
T &  valueOut,
size_t  id,
const T1 &  _default 
) const
inline

Definition at line 458 of file metadata_base.h.

458  { // FIXME: deprecated
459  if (!getValue(label, valueOut,id))
460  valueOut = (T) _default;
461  }
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0

◆ id_begin() [1/2]

virtual id_iterator MetaData::id_begin ( )
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ id_begin() [2/2]

virtual id_const_iterator MetaData::id_begin ( ) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ id_end() [1/2]

virtual id_iterator MetaData::id_end ( )
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ id_end() [2/2]

virtual id_const_iterator MetaData::id_end ( ) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ ids() [1/2]

virtual IdIteratorProxy<false> MetaData::ids ( )
inlinevirtual

Definition at line 754 of file metadata_base.h.

754 { return IdIteratorProxy<false>(*this); };

◆ ids() [2/2]

virtual IdIteratorProxy<true> MetaData::ids ( ) const
inlinevirtual

Definition at line 755 of file metadata_base.h.

755 { return IdIteratorProxy<true>(*this); };

◆ importObject()

virtual void MetaData::importObject ( const MetaData md,
const size_t  id,
bool  doClear = true 
)
pure virtual

Import objects from another metadata.

//Import object 1000 from metadata B into metadata A
A.importObject(B, 1000);
//Import all objects with rotational angle greater that 60
A.importObjects(B, MDValuesGT(MDL_ANGLE_ROT, 60));
//Import all objects
A.importObjects(B);

Implemented in MetaDataDb, and MetaDataVec.

◆ importObjects() [1/2]

virtual void MetaData::importObjects ( const MetaData md,
const std::vector< size_t > &  objectsToAdd,
bool  doClear = true 
)
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ importObjects() [2/2]

virtual void MetaData::importObjects ( const MetaData md,
const MDQuery query,
bool  doClear = true 
)
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ isColumnFormat()

virtual bool MetaData::isColumnFormat ( ) const
inlinevirtual

Return true if the metadata is in column format.

Definition at line 272 of file metadata_base.h.

272 { return _isColumnFormat; }

◆ isEmpty()

virtual bool MetaData::isEmpty ( ) const
inlinevirtual

Check whether the metadata is empty.

Reimplemented in MetaDataVec.

Definition at line 522 of file metadata_base.h.

522 { return size() == 0; }
virtual size_t size() const =0

◆ keepLabels()

void MetaData::keepLabels ( const std::vector< MDLabel > &  labels)

Remove all the labels from the metadata but the ones given in labels vector.

Definition at line 143 of file metadata_base.cpp.

143  {
144  auto active = this->getActiveLabels();
145  for (const auto &l : active) {
146  if (!vectorContainsLabel(labels, l)) {
147  this->removeLabel(l);
148  }
149  }
150 }
virtual bool removeLabel(const MDLabel label)=0
bool vectorContainsLabel(const std::vector< MDLabel > &labelsVector, const MDLabel label)
virtual std::vector< MDLabel > getActiveLabels() const =0

◆ lastRowId()

virtual size_t MetaData::lastRowId ( ) const
pure virtual

Goto last metadata object.

Implemented in MetaDataDb, and MetaDataVec.

◆ nextBlock()

bool MetaData::nextBlock ( mdBuffer buffer,
mdBlock block 
)
virtual

Definition at line 176 of file metadata_base.cpp.

176  {
177  BLOCK_INIT(block);
178  if (buffer.size == 0)
179  return false;
180  // Search for data_ after a newline
181  block.begin = BUFFER_FIND(buffer, "data_", 5);
182 
183  if (block.begin) // data_ FOUND!!!
184  {
185  block.begin += 5; //Shift data_
186  size_t n = block.begin - buffer.begin;
187  BUFFER_MOVE(buffer, n);
188  //Search for the end of line
189  char *newLine = BUFFER_FIND(buffer, "\n", 1);
190  //Calculate length of block name, counting after data_
191  block.nameSize = newLine - buffer.begin;
192  //Search for next block if exists one
193  //use assign and check if not NULL at same time
194  if (!(block.end = BUFFER_FIND(buffer, "\ndata_", 6))) {
195  block.end = block.begin + buffer.size; }
196  else {
197  block.end += 1; // to include terminal \n
198  }
199  block.loop = BUFFER_FIND(buffer, "\nloop_", 6);
200  //If loop_ is not found or is found outside block
201  //scope, the block is in column format
202  if (block.loop)
203  {
204  if (block.loop < block.end)
205  block.loop += 6; // Shift \nloop_
206  else
207  block.loop = NULL;
208  }
209  //Move buffer to end of block
210  n = block.end - buffer.begin;
211  BUFFER_MOVE(buffer, n);
212  return true;
213  }
214 
215  return false;
216 }
#define BUFFER_FIND(b, str, n)
Definition: metadata_base.h:91
char * begin
Definition: metadata_base.h:82
char * end
Definition: metadata_base.h:96
#define BLOCK_INIT(b)
size_t nameSize
Definition: metadata_base.h:95
char * loop
Definition: metadata_base.h:97
#define BUFFER_MOVE(b, n)
Definition: metadata_base.h:90
char * begin
Definition: metadata_base.h:94
size_t size
Definition: metadata_base.h:83
int * n

◆ operator=()

MetaData& MetaData::operator= ( const MetaData md)
inline

Definition at line 373 of file metadata_base.h.

373  {
374  if (&md == this) {
375  return *this;
376  }
377  this->clear();
378  this->copyInfo(md);
379  for (const auto& row : md)
380  this->addRow(row);
381  return *this;
382  };
virtual void clear()
virtual size_t addRow(const MDRow &row)=0
void copyInfo(const MetaData &md)

◆ precision()

double MetaData::precision ( ) const
protected

Definition at line 172 of file metadata_base.cpp.

172  {
173  return 1./this->_precision;
174 }
int _precision

◆ print()

virtual void MetaData::print ( ) const
inlinevirtual

Definition at line 631 of file metadata_base.h.

631 { this->write(std::cout); }
virtual void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const =0

◆ read()

virtual void MetaData::read ( const FileName inFile,
const std::vector< MDLabel > *  desiredLabels = nullptr,
bool  decomposeStack = true 
)
pure virtual

Read data from file. Guess the blockname from the filename

inFilename="first@md1.doc" -> filename = md1.doc, blockname = first

Implemented in MetaDataDb, and MetaDataVec.

◆ readStar()

void MetaData::readStar ( const FileName filename,
const std::vector< MDLabel > *  desiredLabels,
const String blockRegExp,
bool  decomposeStack 
)
protectedvirtual

Definition at line 406 of file metadata_base.cpp.

407  {
408  // First try to open the file as a metadata
409  size_t id;
410  FileName inFile = filename.removeBlockName();
411 
412  if (!(isMetadataFile = inFile.isMetaData())) { //if not a metadata, try to read as image or stack
413  Image<char> image;
414  if (decomposeStack) // If not decomposeStack it is no necessary to read the image header
415  image.read(filename, HEADER);
416  if (!decomposeStack || image().ndim == 1) { // single image; !decomposeStack must be first
417  id = this->addObject();
418  MetaData::setValue(MDL_IMAGE, filename, id);
420  } else { // stack
421  FileName fnTemp;
422  for (size_t i = 1; i <= image().ndim; ++i) {
423  fnTemp.compose(i, filename);
424  id = addObject();
425  MetaData::setValue(MDL_IMAGE, fnTemp, id);
427  }
428  }
429  return;
430  }
431 
432  std::ifstream is(inFile.c_str(), std::ios_base::in);
433  std::stringstream ss;
434  String line, token,_comment;
435  std::vector<MDObject*> columnValues;
436 
437  getline(is, line); //get first line to identify the type of file
438 
439  if (is.fail())
440  REPORT_ERROR(ERR_IO_NOTEXIST, formatString("MetaDataDb::read: File doesn't exists: %s", inFile.c_str()) );
441 
442  bool useCommentAsImage = false;
443  this->_inFile = inFile;
444  bool oldFormat = true;
445 
446  is.seekg(0, std::ios::beg);//reset the stream position to the beginning to start parsing
447 
448  if (line.find(FileNameVersion) != String::npos ||
449  filename.getExtension() == "xmd" || filename.getExtension() == "star") {
450  oldFormat = false;
451  _comment.clear();
452 
453  // Skip comment parsing if we found the data key in the first line
454  if (line.find("data_") != 0) {
455  // Read comment
456  // is.ignore(256,'#');//format line
457  is.ignore(256, '\n');//skip first line
458  bool addspace = false;
459  while (1) {
460  getline(is, line);
461  trim(line);
462  if (line[0] == '#')
463  {
464  line[0] = ' ';
465  trim(line);
466  if (addspace)
467  _comment += " " + line;
468  else
469  _comment += line;
470  addspace = true;
471  } else
472  break;
473  }
474  this->setComment(_comment);
475  }
476 
477  //map file
478  int fd;
479  BUFFER_CREATE(bufferMap);
480  mapFile(inFile, bufferMap.begin, bufferMap.size, fd);
481 
482  BLOCK_CREATE(block);
483  regex_t re;
484  int rc = regcomp(&re, (blockRegExp+"$").c_str(), REG_EXTENDED|REG_NOSUB);
485  if (blockRegExp.size() && rc != 0)
486  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("Pattern '%s' cannot be parsed: %s",
487  blockRegExp.c_str(), inFile.c_str()));
488  BUFFER_COPY(bufferMap, buffer);
489  bool firstBlock = true;
490  bool singleBlock = blockRegExp.find_first_of(".[*+")==String::npos;
491 
492  String blockName;
493 
494  while (nextBlock(buffer, block)) {
495  //startingPoint, remainingSize, firstData, secondData, firstloop))
496  BLOCK_NAME(block, blockName);
497  if (blockRegExp.size() == 0 || regexec(&re, blockName.c_str(), (size_t) 0, NULL, 0) == 0) {
498  //Read column labels from the datablock that starts at firstData
499  //Label ends at firstloop
500  if ((_isColumnFormat = (block.loop != NULL))) {
501  _readColumnsStar(block, columnValues, desiredLabels, firstBlock);
502  // If block is empty, makes block.loop and block.end equal
503  if (block.loop == (block.end + 1))
504  block.loop--;
505  _readRowsStar(block, columnValues, desiredLabels);
506  } else {
507  id = this->addObject();
508  _parsedLines = 1;
509  this->_readColumnsStar(block, columnValues, desiredLabels, firstBlock, id);
510  }
511  firstBlock = false;
512 
513  if (singleBlock)
514  break;
515  }
516  }
517 
518  unmapFile(bufferMap.begin, bufferMap.size, fd);
519  regfree(&re);
520  if (firstBlock)
521  REPORT_ERROR(ERR_MD_BADBLOCK, formatString("Block: '%s': %s",
522  blockRegExp.c_str(), inFile.c_str()));
523 
524  } else if (line.find("Headerinfo columns:") != String::npos) {
525  // This looks like an old DocFile, parse header
526  std::cerr << "WARNING: ** You are using an old file format (DOCFILE) which is going "
527  << "to be deprecated in next Xmipp release **" << std::endl;
528  is.ignore(256, ':'); //ignore all until ':' to start parsing column labels
529  getline(is, line);
530  ss.str(line);
531  columnValues.emplace_back(new MDObject(MDL_UNDEFINED));
532  columnValues.emplace_back(new MDObject(MDL_UNDEFINED));
533 
534  this->addLabel(MDL_IMAGE);
535  this->_readColumns(ss, columnValues, desiredLabels);
536  useCommentAsImage = true;
537 
538  } else {
539  std::cerr << "WARNING: ** You are using an old file format (SELFILE) which is going "
540  << "to be deprecated in next Xmipp release **" << std::endl;
541  // I will assume that is an old SelFile, so only need to add two columns
542  columnValues.emplace_back(new MDObject(MDL_IMAGE));//addLabel(MDL_IMAGE);
543  columnValues.emplace_back(new MDObject(MDL_ENABLED));//addLabel(MDL_ENABLED);
544  }
545 
546  if (oldFormat)
547  this->_readRows(is, columnValues, useCommentAsImage);
548 
549  //free memory of column values
550  int nCols = columnValues.size();
551  for (int i = 0; i < nCols; ++i)
552  delete columnValues[i];
553 
554  is.close();
555 }
virtual size_t addObject()=0
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
size_t _parsedLines
virtual bool nextBlock(mdBuffer &buffer, mdBlock &block)
HBITMAP buffer
Definition: svm-toy.cpp:37
void trim(std::string &s)
Definition: text.cpp:205
#define BUFFER_CREATE(b)
Some macros to use the buffer.
Definition: metadata_base.h:88
void compose(const String &str, const size_t no, const String &ext="")
#define BUFFER_COPY(b1, b2)
Definition: metadata_base.h:89
virtual void _readRows(std::istream &is, std::vector< MDObject *> &columnValues, bool useCommentAsImage)
bool isMetadataFile
String FileNameVersion
FileName _inFile
String _comment
#define BLOCK_CREATE(b)
Some macros to use the block pointers.
#define i
Is this image enabled? (int [-1 or 1])
virtual void setComment(const String &newComment="No comment")
String getExtension() const
#define BLOCK_NAME(b, s)
virtual void _readRowsStar(mdBlock &block, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels)
void unmapFile(char *&map, size_t &size, int &fileDescriptor)
int in
Incorrect argument received.
Definition: xmipp_error.h:113
File or directory does not exist.
Definition: xmipp_error.h:136
This block does not exist.
Definition: xmipp_error.h:163
bool setValue(const MDLabel label, const T &valueIn, size_t id)
void mapFile(const FileName &filename, char *&map, size_t &size, int &fileDescriptor, bool readOnly)
bool isMetaData(bool failIfNotExists=true) const
FileName removeBlockName() const
virtual void _readColumns(std::istream &is, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels=nullptr)
std::string String
Definition: xmipp_strings.h:34
virtual bool addLabel(const MDLabel label, int pos=-1)=0
String formatString(const char *format,...)
virtual void _readColumnsStar(mdBlock &block, std::vector< MDObject *> &columnValues, const std::vector< MDLabel > *desiredLabels, bool addColumns=true, size_t id=BAD_OBJID)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
Name of an image (std::string)

◆ removeDisabled()

void MetaData::removeDisabled ( )
virtual

Remove rows with MDL_ENABLED = -1 if this label is present

Definition at line 123 of file metadata_base.cpp.

123  {
125  removeObjects(MDValueLE(MDL_ENABLED, 0)); // Remove values -1 and 0 on MDL_ENABLED label
126 }
virtual bool containsLabel(const MDLabel label) const =0
Is this image enabled? (int [-1 or 1])
virtual int removeObjects()=0

◆ removeLabel()

virtual bool MetaData::removeLabel ( const MDLabel  label)
pure virtual

Remove a label from the metadata. The data is still in the table. If you want to remove the data, make a copy of the MetaData.

Implemented in MetaDataDb, and MetaDataVec.

◆ removeObject()

virtual bool MetaData::removeObject ( size_t  id)
pure virtual

Remove the object with this id. Returns true if the object was removed or false if the object did not exist

Implemented in MetaDataDb, and MetaDataVec.

◆ removeObjects() [1/3]

virtual void MetaData::removeObjects ( const std::vector< size_t > &  toRemove)
pure virtual

Removes the collection of objects of given vector id's NOTE: The iterator will point to the first object after any of these operations

Implemented in MetaDataDb, and MetaDataVec.

◆ removeObjects() [2/3]

virtual int MetaData::removeObjects ( const MDQuery )
pure virtual

Removes objects from metadata. return the number of deleted rows if not query, all objectes are removed Queries can be used in the same way as in the importObjects function

Implemented in MetaDataDb, and MetaDataVec.

◆ removeObjects() [3/3]

virtual int MetaData::removeObjects ( )
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ renameColumn() [1/2]

virtual void MetaData::renameColumn ( MDLabel  oldLabel,
MDLabel  newLabel 
)
pure virtual

Rename column.

Implemented in MetaDataDb, and MetaDataVec.

◆ renameColumn() [2/2]

virtual void MetaData::renameColumn ( const std::vector< MDLabel > &  oldLabel,
const std::vector< MDLabel > &  newLabel 
)
pure virtual

Rename several columns. This is an expensive operations so if several columns need to be changed do it using this function instead one by one

Implemented in MetaDataDb, and MetaDataVec.

◆ selectPart()

virtual void MetaData::selectPart ( const MetaData mdIn,
size_t  startPosition,
size_t  numberOfObjects,
const MDLabel  sortLabel = MDL_OBJID 
)
pure virtual

Select some part from Metadata. Select elements from input Metadata at some starting position if the numberOfObjects is -1, all objects will be returned from startPosition to the end.

Implemented in MetaDataDb, and MetaDataVec.

◆ selectRandomSubset()

virtual void MetaData::selectRandomSubset ( const MetaData mdIn,
size_t  numberOfObjects,
const MDLabel  sortLabel = MDL_OBJID 
)
pure virtual

Select random subset

Implemented in MetaDataDb, and MetaDataVec.

◆ setColumnFormat()

virtual void MetaData::setColumnFormat ( bool  column)
inlinevirtual

Set to false for row format (parameter files). set to true for column format (this is the default) (docfiles)

Definition at line 293 of file metadata_base.h.

293 { _isColumnFormat = column; }

◆ setColumnValues() [1/2]

template<class T >
void MetaData::setColumnValues ( const MDLabel  label,
const std::vector< T > &  valuesIn 
)
inline

Set all values of a column as a vector. The input vector must have the same size as the Metadata.

Definition at line 499 of file metadata_base.h.

499  {
500  if (valuesIn.size() != size())
501  REPORT_ERROR(ERR_MD_OBJECTNUMBER, "Input vector must be of the same size as the metadata");
502  size_t n = 0;
503  for (size_t objId : this->ids())
504  this->setValue(label, valuesIn[n++], objId);
505  }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual IdIteratorProxy< false > ids()
Incorrect number of objects in Metadata.
Definition: xmipp_error.h:160
virtual size_t size() const =0
bool setValue(const MDLabel label, const T &valueIn, size_t id)
int * n

◆ setColumnValues() [2/2]

virtual void MetaData::setColumnValues ( const std::vector< MDObject > &  valuesIn)
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ setComment()

virtual void MetaData::setComment ( const String newComment = "No comment")
inlinevirtual

Set Header Comment. the comment will appear in second line

Definition at line 315 of file metadata_base.h.

315 { this->_comment = newComment; }
String _comment

◆ setFilename()

virtual void MetaData::setFilename ( const FileName _filename)
inlinevirtual

Set metadata filename.

Definition at line 323 of file metadata_base.h.

323 { this->_inFile = _filename; }
FileName _inFile

◆ setMaxRows()

virtual void MetaData::setMaxRows ( size_t  maxRows = 0)
inlinevirtual

Prevent from parsing all rows from the metadata. When reading from file, only maxRows will be read.

Definition at line 277 of file metadata_base.h.

277 { _maxRows = maxRows; }
size_t _maxRows

◆ setPrecission()

virtual void MetaData::setPrecission ( int  _precision)
inlinevirtual

Set precision (number of decimal digits) use by operator == when comparing metadatas with double data. "2" is a good value for angles

Definition at line 288 of file metadata_base.h.

288 { this->_precision = (int)pow (10,_precision); }
int _precision

◆ setValue() [1/2]

template<class T >
bool MetaData::setValue ( const MDLabel  label,
const T &  valueIn,
size_t  id 
)
inline

Set the value for some label. to the object that has id 'objectId' or to 'activeObject' if is objectId=-1. This is one of the most used functions to programatically fill a metadata.

size_t id = md.addObject();
md.setValue(MDL_IMAGE, "images/image00011.xmp",id);

Definition at line 367 of file metadata_base.h.

367  {
368  return setValue(MDObject(label, valueIn), id);
369  }
bool setValue(const MDLabel label, const T &valueIn, size_t id)

◆ setValue() [2/2]

virtual bool MetaData::setValue ( const MDObject mdValueIn,
size_t  id 
)
pure virtual

This functions are using MDObject for set real values there is an explicit function signature foreach type supported in Metadata. This is done for some type checking of Metadata labels and values

Implemented in MetaDataDb, and MetaDataVec.

◆ setValueCol() [1/2]

virtual bool MetaData::setValueCol ( const MDObject mdValueIn)
pure virtual

Set the value of all objects in an specified column (both value and column are specified in mdValueIn)

Implemented in MetaDataDb, and MetaDataVec.

◆ setValueCol() [2/2]

template<class T >
bool MetaData::setValueCol ( const MDLabel  label,
const T &  valueIn 
)
inline

Set the value of all objects in an specified column.

md.setValueCol(MDL_IMAGE, "images/image00011.xmp");

Definition at line 350 of file metadata_base.h.

350  {
351  return setValueCol(MDObject(label, valueIn));
352  }
virtual bool setValueCol(const MDObject &mdValueIn)=0

◆ setValueFromStr()

bool MetaData::setValueFromStr ( const MDLabel  label,
const String value,
size_t  id 
)
virtual

Set label values from string representation.

Definition at line 107 of file metadata_base.cpp.

107  {
108  addLabel(label);
109  MDObject mdValue(label);
110  mdValue.fromString(value);
111  this->setValue(mdValue, id);
112  return true;
113 }
bool setValue(const MDLabel label, const T &valueIn, size_t id)
virtual bool addLabel(const MDLabel label, int pos=-1)=0

◆ size()

virtual size_t MetaData::size ( ) const
pure virtual

Number of objects contained in the metadata.

Implemented in MetaDataDb, and MetaDataVec.

◆ write() [1/2]

virtual void MetaData::write ( const FileName outFile,
WriteModeMetaData  mode = MD_OVERWRITE 
) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ write() [2/2]

virtual void MetaData::write ( std::ostream &  os,
const String blockName = "",
WriteModeMetaData  mode = MD_OVERWRITE 
) const
pure virtual

Implemented in MetaDataDb, and MetaDataVec.

◆ writeStar()

void MetaData::writeStar ( const FileName outFile,
const String blockName,
WriteModeMetaData  mode 
) const
virtual

Definition at line 557 of file metadata_base.cpp.

557  {
558  // Move to MetaData?
559 #ifdef XMIPP_MMAP
560  if (outFile.hasImageExtension())
561  REPORT_ERROR(ERR_IO,"MetaData:writeStar Trying to write metadata with image extension");
562 
563  struct stat file_status;
564  int fd;
565  char *map = NULL;
566  char *tailMetadataFile = NULL; // auxiliary variable to keep metadata file tail in memory
567  size_t size=-1;
568  char *target, * target2 = NULL;
569 
570  //check if file exists or not block name has been given
571  //in our format no two identical data_xxx strings may exists
572  if (mode == MD_APPEND) {
573  if (blockName.empty() || !outFile.exists())
574  mode = MD_OVERWRITE;
575  else {
576  //does blockname exists?
577  //remove it from file in this case
578  // get length of file:
579  if(stat(outFile.c_str(), &file_status) != 0)
580  REPORT_ERROR(ERR_IO_NOPATH,"MetaData:writeStar can not get filesize for file "+outFile);
581  size = file_status.st_size;
582  if (size == 0)
583  mode = MD_OVERWRITE;
584  }
585 
586  if (mode == MD_APPEND) { //size=0 for /dev/stderr
587  fd = open(outFile.c_str(), O_RDWR, S_IREAD | S_IWRITE);
588  if (fd == -1)
589  REPORT_ERROR(ERR_IO_NOPATH,"MetaData:writeStar can not read file named "+outFile);
590 
591  map = (char *) mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
592  if (map == MAP_FAILED)
593  REPORT_ERROR(ERR_MEM_BADREQUEST,"MetaData:writeStar can not map memory ");
594 
595  // Is this a metadata formatted FILE
596  if(strncmp(map,FileNameVersion.c_str(),FileNameVersion.length()) != 0) {
597  mode = MD_OVERWRITE;
598  } else {
599  //block name
600  String _szBlockName = formatString("\ndata_%s\n", blockName.c_str());
601  size_t blockNameSize = _szBlockName.size();
602 
603  //search for the string
604  target = (char *) _memmem(map, size, _szBlockName.c_str(), blockNameSize);
605 
606  if (target != NULL)
607  {
608  target2 = (char *) _memmem(target+1, size - (target - map), "\ndata_", 6);
609 
610  if (target2 != NULL)
611  {
612  //target block is not the last one, so we need to
613  //copy file from target2 to auxiliary memory and truncate
614  tailMetadataFile = (char *) malloc( ((map + size) - target2));
615  memmove(tailMetadataFile,target2, (map + size) - target2);
616  }
617  if (ftruncate(fd, target - map+1)==-1) //truncate rest of the file
618  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot truncate file");
619  }
620  }
621  close(fd);
622 
623  if (munmap(map, size) == -1)
624  REPORT_ERROR(ERR_MEM_NOTDEALLOC, "MetaData:writeStar, Can not unmap memory");
625  }
626  }
627 
628  std::ios_base::openmode openMode = (mode == MD_OVERWRITE) ? std::ios_base::out : std::ios_base::app;
629  std::ofstream ofs(outFile.c_str(), openMode);
630 
631  write(ofs, blockName, mode);
632 
633  if (tailMetadataFile != NULL) {
634  //append memory buffer to file
635  //may a cat a buffer to a ofstream
636  ofs.write(tailMetadataFile,(map + size) - target2);
637  free(tailMetadataFile);
638  }
639  ofs.close();
640 
641 #else
642 
643  REPORT_ERROR(ERR_MMAP,"Mapping not supported in Windows");
644 #endif
645 }
Just to locate unclassified errors.
Definition: xmipp_error.h:192
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Global mmap error.
Definition: xmipp_error.h:170
virtual void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const =0
Input/Output general error.
Definition: xmipp_error.h:134
bool hasImageExtension() const
Memory has not been deallocated.
Definition: xmipp_error.h:167
String FileNameVersion
Bad amount of memory requested.
Definition: xmipp_error.h:165
void * _memmem(const void *haystack, size_t haystack_len, const void *needle, size_t needle_len)
free((char *) ob)
void mode
virtual size_t size() const =0
bool exists() const
std::string String
Definition: xmipp_strings.h:34
String formatString(const char *format,...)
Environment PATH cannot be read.
Definition: xmipp_error.h:143

◆ writeText()

virtual void MetaData::writeText ( const FileName  fn,
const std::vector< MDLabel > *  desiredLabels 
) const
pure virtual

Write metadata in text file as plain data without header.

Implemented in MetaDataDb, and MetaDataVec.

◆ writeXML()

virtual void MetaData::writeXML ( const FileName  fn,
const FileName  blockname,
WriteModeMetaData  mode 
) const
pure virtual

Export medatada to xml file.

Implemented in MetaDataDb, and MetaDataVec.

Friends And Related Function Documentation

◆ MDBaseRowIterator< false >

friend struct MDBaseRowIterator< false >
friend

Definition at line 676 of file metadata_base.h.

◆ MDBaseRowIterator< true >

friend struct MDBaseRowIterator< true >
friend

Definition at line 677 of file metadata_base.h.

Member Data Documentation

◆ _comment

String MetaData::_comment
protected

A general comment for the MetaData file

Definition at line 185 of file metadata_base.h.

◆ _fastStringSearch

std::map<String, size_t> MetaData::_fastStringSearch
protected

Definition at line 183 of file metadata_base.h.

◆ _fastStringSearchLabel

MDLabel MetaData::_fastStringSearchLabel
protected

Definition at line 184 of file metadata_base.h.

◆ _inFile

FileName MetaData::_inFile
protected

Input file name Where does this MetaData come from/go to be stored?

Definition at line 195 of file metadata_base.h.

◆ _maxRows

size_t MetaData::_maxRows = 0
protected

This two variables will be used to read the metadata information (labels and size) or maybe a few rows only

Definition at line 200 of file metadata_base.h.

◆ _parsedLines

size_t MetaData::_parsedLines = 0
protected

Definition at line 200 of file metadata_base.h.

◆ _precision

int MetaData::_precision = 1000
protected

Definition at line 190 of file metadata_base.h.

◆ isMetadataFile

bool MetaData::isMetadataFile

Filename used in the read command, useful to write Error messages

Definition at line 246 of file metadata_base.h.


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