Xmipp  v3.23.11-Nereus
metadata_static.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: J.M. De la Rosa Trevin (jmdelarosa@cnb.csic.es)
4  * Jan Horacek (xhorace4@fi.muni.cz)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
27 #include <sstream>
28 #include "metadata_static.h"
29 #include "xmipp_error.h"
30 
31 //This is needed for static memory allocation
32 MDLabelData * MDL::data[MDL_LAST_LABEL+1];
33 std::map<std::string, MDLabel> MDL::names;
34 MDLabelStaticInit MDL::initialization; //Just for initialization
35 MDLabel MDL::bufferIndex;
36 
37 void MDL::addLabel(const MDLabel label, const MDLabelType type, const String &name, int tags)
38 {
39  if (names.find(name) != names.end())
40  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("MDL::addLabel, label '%s' already exists.", name.c_str()));
41 
42  data[label] = new MDLabelData(type, name, tags);
43  names[name] = label;
44 }
45 
53 void MDL::addExtraAliases()
54 {
55  const char * extra_aliases = getenv("XMIPP_EXTRA_ALIASES");
56 
57  if (extra_aliases)
58  {
59  StringVector sv, pair;
60  String eq = "=", co = ":";
61  tokenize(extra_aliases, sv, ";");
62  MDLabel label;
63  bool replace;
64 
65  for (std::vector<String>::iterator it = sv.begin(); it != sv.end(); ++it)
66  {
67  if (it->find(eq) != it->npos)
68  {
69  tokenize(*it, pair, "=");
70  replace = true;
71  }
72  else if (it->find(co) != it->npos)
73  {
74  tokenize(*it, pair, co);
75  replace = false;
76  }
77  else
78  REPORT_ERROR(ERR_ARG_INCORRECT, "Invalid pair separator, use = or :");
79  label = MDL::str2Label(pair[0]);
80  // Add the label alias
81  if (label != MDL_UNDEFINED)
82  addLabelAlias(label, pair[1], replace);
83  else
85  formatString("Invalid label name: %s found in environment var XMIPP_EXTRA_ALIASES", pair[0].c_str()));
86  }
87  }
88 }
89 
90 void MDL::addLabelAlias(const MDLabel label, const String &alias, bool replace,
91  MDLabelType type)
92 {
93  names[alias] = label;
94  if (replace)
95  {
96  data[label]->str = alias;
97  if (type != LABEL_NOTYPE)
98  data[label]->type = type;
99  }
100 }
101 
103 {
104  MDLabel newLabel = MDL::bufferIndex;
105 
106  if (newLabel == MDL_LAST_LABEL)
107  REPORT_ERROR(ERR_ARG_INCORRECT, "Not more buffer labels to use!!!");
108 
109  addLabelAlias(newLabel, alias, true, type);
110  MDL::bufferIndex = (MDLabel)((int)newLabel + 1);
111 
112  return newLabel;
113 }
114 
116 {
117  MDL::bufferIndex = BUFFER_01;
118 }
119 
120 void MDL::str2LabelVector(const String &labelsStr, std::vector<MDLabel> &labels)
121 {
122  labels.clear();
123  StringVector parts;
124  splitString(labelsStr, " ", parts);
125  for (size_t i = 0; i < parts.size(); ++i)
126  if (MDL::isValidLabel(parts[i]))
127  labels.push_back(MDL::str2Label(parts[i]));
128  else
129  REPORT_ERROR(ERR_PARAM_INCORRECT, formatString("Unknown label '%s' received.", parts[i].c_str()));
130 }
131 
132 MDLabel MDL::str2Label(const String &labelName)
133 {
134  if (names.find(labelName) == names.end())
135  return MDL_UNDEFINED;
136  return names[labelName];
137 }
138 
140 {
141  return (isValidLabel(label)) ? data[(int)label]->str : "";
142 }
143 
145 {
146  String labelSqlite = "\"" + label2Str(label) + "\"";
147  return labelSqlite;
148 }
149 
151 {
152  std::stringstream ss;
153  ss << MDL::label2StrSql(label) << " ";
154 
155  switch (MDL::labelType(label))
156  {
157  case LABEL_BOOL: //bools are int in sqlite3
158  case LABEL_INT:
159  case LABEL_SIZET:
160  ss << "INTEGER";
161  break;
162  case LABEL_DOUBLE:
163  ss << "REAL";
164  break;
165  case LABEL_STRING:
166  ss << "TEXT";
167  break;
168  case LABEL_VECTOR_DOUBLE:
169  case LABEL_VECTOR_SIZET:
170  ss << "TEXT";
171  break;
172  case LABEL_NOTYPE:
173  ss << "NO_TYPE";
174  break;
175  }
176  return ss.str();
177 }
178 
180 {
181  switch (type)
182  {
183  case LABEL_STRING:
184  return "STRING";
185  case LABEL_DOUBLE:
186  return "DOUBLE";
187  case LABEL_INT:
188  return "INT";
189  case LABEL_BOOL:
190  return "BOOL";
191  case LABEL_VECTOR_DOUBLE:
192  return "VECTOR(DOUBLE)";
193  case LABEL_SIZET:
194  return "SIZE_T";
195  case LABEL_VECTOR_SIZET:
196  return "VECTOR(SIZE_T)";
197  case LABEL_NOTYPE:
198  return "NO_TYPE";
199  }
200  return "UNKNOWN";
201 }
202 
203 bool MDL::isInt(const MDLabel label)
204 {
205  return (data[label]->type == LABEL_INT);
206 }
207 
208 bool MDL::isLong(const MDLabel label)
209 {
210  return (data[label]->type == LABEL_SIZET);
211 }
212 
213 bool MDL::isBool(const MDLabel label)
214 {
215  return (data[label]->type == LABEL_BOOL);
216 }
217 
218 bool MDL::isString(const MDLabel label)
219 {
220  return (data[label]->type == LABEL_STRING);
221 }
222 
223 bool MDL::isDouble(const MDLabel label)
224 {
225  return (data[label]->type == LABEL_DOUBLE);
226 }
227 
228 bool MDL::isVector(const MDLabel label)
229 {
230  return (data[label]->type == LABEL_VECTOR_DOUBLE);
231 }
232 
233 bool MDL::isVectorLong(const MDLabel label)
234 {
235  return (data[label]->type == LABEL_VECTOR_SIZET);
236 }
237 
238 bool MDL::isValidLabel(const MDLabel &label)
239 {
240  return label > MDL_UNDEFINED &&
241  label < MDL_LAST_LABEL &&
242  data[label] != NULL;
243 }
244 
245 bool MDL::isValidLabel(const String &labelName)
246 {
247  return isValidLabel(str2Label(labelName));
248 }
249 
251 {
252  return data[label]->type;
253 }
254 
256 {
257  return data[str2Label(labelName)]->type;
258 }
259 
260 std::map<String, MDLabel>& MDL::getLabelDict()
261 {
262  return names;
263 }
264 
265 bool MDL::hasTag(const MDLabel label, const int tags)
266 {
267  return data[label]->tags & tags;
268 }
269 
270 bool MDL::isTextFile(const MDLabel label)
271 {
272  return data[label]->tags & TAGLABEL_TEXTFILE;
273 }
274 
275 bool MDL::isMetadata(const MDLabel label)
276 {
277  return data[label]->tags & TAGLABEL_METADATA;
278 }
279 
280 bool MDL::isCtfParam(const MDLabel label)
281 {
282  return data[label]->tags & TAGLABEL_CTFPARAM;
283 }
284 
285 bool MDL::isImage(const MDLabel label)
286 {
287  return data[label]->tags & TAGLABEL_IMAGE;
288 }
289 
290 bool MDL::isStack(const MDLabel label)
291 {
292  return data[label]->tags & TAGLABEL_STACK;
293 }
294 
295 bool MDL::isMicrograph(const MDLabel label)
296 {
297  return data[label]->tags & TAGLABEL_MICROGRAPH;
298 }
299 
300 bool MDL::isPSD(const MDLabel label)
301 {
302  return data[label]->tags & TAGLABEL_PSD;
303 }
304 
306  MDRowSql row;
307  row.resetGeo();
308  row.setValue(MDL_ANGLE_ROT, 0.);
309  row.setValue(MDL_ANGLE_TILT,0.);
310  return row;
311 }
312 
314  MDRowVec row;
315  row.resetGeo();
316  row.setValue(MDL_ANGLE_ROT, 0.);
317  row.setValue(MDL_ANGLE_TILT,0.);
318  return row;
319 }
320 
322  row.resetGeo();
323  row.setValue(MDL_ANGLE_ROT, 0.);
324  row.setValue(MDL_ANGLE_TILT,0.);
325 }
MDLabelType type
static bool isMicrograph(const MDLabel label)
Rotation angle of an image (double,degrees)
Parameter incorrect.
Definition: xmipp_error.h:181
static MDLabel str2Label(const String &labelName)
static bool isImage(const MDLabel label)
static std::map< String, MDLabel > & getLabelDict()
MDLabelType
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
static bool isInt(const MDLabel label)
static bool isCtfParam(const MDLabel label)
Tilting angle of an image (double,degrees)
void setValue(const MDObject &object) override
static bool isValidLabel(const MDLabel &label)
static void emptifyHeader(MDRow &)
static bool isBool(const MDLabel label)
static String labelType2Str(MDLabelType type)
static bool isVector(const MDLabel label)
static String label2StrSql(const MDLabel label)
std::vector< String > StringVector
Definition: xmipp_strings.h:35
#define i
static MDLabelType labelType(const MDLabel label)
static void resetBufferIndex()
viol type
int splitString(const String &input, const String &delimiter, StringVector &results, bool includeEmpties)
static bool isStack(const MDLabel label)
void setValue(const MDObject &object) override
Incorrect argument received.
Definition: xmipp_error.h:113
static bool isMetadata(const MDLabel label)
static bool isTextFile(const MDLabel label)
static bool isLong(const MDLabel label)
void eq(Image< double > &op1, const Image< double > &op2)
Be careful with integer images for relational operations...due to double comparisons.
static MDLabel getNewAlias(const String &alias, MDLabelType type=LABEL_NOTYPE)
void tokenize(const String &str, StringVector &tokens, const String &delimiters)
static MDRowSql emptyHeaderSql()
static MDRowVec emptyHeaderVec()
virtual void resetGeo(bool addLabels=true)
static bool isPSD(const MDLabel label)
static bool isString(const MDLabel label)
void setValue(MDLabel label, const T &d, bool addLabel=true)
static String label2SqlColumn(const MDLabel label)
std::string String
Definition: xmipp_strings.h:34
static bool isDouble(const MDLabel label)
String formatString(const char *format,...)
static bool hasTag(const MDLabel label, const int tags)
static void str2LabelVector(const String &labelsStr, std::vector< MDLabel > &labels)
static String label2Str(const MDLabel &label)
static void addLabelAlias(MDLabel label, const String &alias, bool replace=false, MDLabelType type=LABEL_NOTYPE)
MDLabel
static bool isVectorLong(const MDLabel label)