40 std::vector<MDLabel> labels;
47 addUsageLine(
"Perform several operations on metadata files. ");
48 addUsageLine(
"If the -o option is not used the original metadata will be modified.");
49 addUsageLine(
"+ Also you can use the --print option just to print out the result metadata to screen.");
50 addUsageLine(
"+ The combination of -i and -o without other operations can serve to extract data blocks");
51 addUsageLine(
"+ inside a metadata and write to an independent one.");
55 addParamsLine(
" [-o <metadata>] : Output metadata file, if not provided result will overwrite input file");
57 addParamsLine(
" [--set <set_operation> <md2_file> <label=image> <label2=image2>] : Set operations");
59 addParamsLine(
" union : Union with metadata md2, duplicated values only will appear once");
60 addParamsLine(
" union_all : Union with metadata md2, will repeat duplicated values");
61 addParamsLine(
" intersection : Intersection with metadata md2");
62 addParamsLine(
" subtraction : Subtraction with metadata md2");
64 addParamsLine(
" natural_join : Natural join with md2 using all common labels");
65 addParamsLine(
" inner_join : Inner join with label=label2. No label may be repeated");
66 addParamsLine(
" merge : Merge columns with md2, label is ignored");
67 addParamsLine(
" : Both metadatas should have same size, and elements should be in same order,");
68 addParamsLine(
" : if not, you should use 'join' instead, but this constrain having a common label");
71 addParamsLine(
"or --operate <operation> : Operations on the metadata structure");
73 addParamsLine(
" sort <label=image> <order=asc>: Sort metadata using a label as identifier");
74 addParamsLine(
" : for sorting according to a component of a vector label");
77 addParamsLine(
" : order can be asc (ascending) or desc (descending)");
78 addParamsLine(
" percentile <labelIn> <labelOut> : Fill a column with the percentile (between 0 and 1) of another column");
80 addParamsLine(
" random_subset <size> : Extract a random subset without replacement of this metadata");
81 addParamsLine(
" bootstrap : Extract a bootstrap subset (with replacement) of this metadata");
83 addParamsLine(
" keep_column <labels> : Keep some columns(label list) from metadata");
84 addParamsLine(
" drop_column <labels> : Drop some columns(label list) from metadata");
85 addParamsLine(
" remove_duplicates <label> : Remove duplicates on a given label");
87 addParamsLine(
" modify_values <expression> : Use an SQLite expression to modify the metadata");
88 addParamsLine(
" : This option requires knowledge of basic SQL syntax(more specific SQLite");
89 addParamsLine(
" expand <factor> : Expand the metadata content by union with himself ");
92 addParamsLine(
":+ acos, asin, atan, atn2, atan2, acosh, asinh, atanh,");
94 addParamsLine(
":+ degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh,");
96 addParamsLine(
":+ log, log10, power, sign, sqrt, square, ceil, floor, pi.");
97 addParamsLine(
":+ String: replicate, charindex, leftstr, rightstr, ltrim, rtrim, trim,");
98 addParamsLine(
":+ replace, reverse, proper, padl, padr, padc, strfilter.");
100 addParamsLine(
":+ Aggregate: max, min, avg, sum, mstdev, variance, mode, median,");
105 addParamsLine(
"or --file <file_operation> : File operations");
107 addParamsLine(
" copy <directory> <label=image> : Copy files in metadata md1 to directory path (file names at label column)");
108 addParamsLine(
" move <directory> <label=image> : Move files in metadata md1 to directory path (file names at label column)");
109 addParamsLine(
" delete <label=image> : Delete files in metadata md1 (file names at label column)");
112 addParamsLine(
" import_txt <labels> : Import a text file specifying its columns");
115 addParamsLine(
"or --query <query_operation> : Query operations");
117 addParamsLine(
" select <expression> : Create new metadata with those entries that satisfy the expression");
118 addParamsLine(
" count <label> : for each value of a given label create new metadata with the number of times the value appears");
119 addParamsLine(
" sum <label1> <label2> : group metadata by label1 and add quantities in label2");
125 addParamsLine(
"or --fill <labels> <fill_mode> : Fill a column values(should be of same type)");
127 addParamsLine(
" constant <value> : Fill with a constant value");
128 addParamsLine(
" lineal <init_value> <step> : Fill with a lineal serie starting at init_value with an step");
129 addParamsLine(
" rand_uniform <a=0.> <b=1.> : Follow a uniform distribution between a and b");
130 addParamsLine(
" rand_gaussian <mean=0.> <stddev=1.> : Follow a gaussian distribution with mean and stddev");
131 addParamsLine(
" rand_student <mean=0.> <stddev=1.> <df=3.> : Follow a student distribution with mean, stddev and df degrees of freedom.");
132 addParamsLine(
" expand : Treat the column as the filename of an row metadata and expand values");
135 addParamsLine(
" [--print ] : Just print medata to stdout, or if -o is specified written to disk.");
136 addParamsLine(
" : this option is useful for extrating data blocks inside a metadata.");
139 addParamsLine(
" [--mode <mode=overwrite>] : Metadata writing mode.");
141 addParamsLine(
" overwrite : Replace the content of the file with the Metadata");
142 addParamsLine(
" append : Write the Metadata as a new block, removing the old one");
144 addExampleLine(
" Concatenate two metadatas. If label is not provided, by default is 'image'",
false);
145 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --set union mD2.doc -o out.doc");
146 addExampleLine(
" Intersect two metadatas using label 'order_'",
false);
147 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --set intersection mD2.doc order_ -o out.doc");
148 addExampleLine(
" Combine columns from two metadatas. Be sure of both have same number of rows and also",
false);
149 addExampleLine(
" there aren't common columns, in that case second metadata columns will be used",
false);
150 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --set merge mD2.doc -o out.doc");
151 addExampleLine(
" Sort the elements in metadata (using default label 'image').",
false);
152 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --operate sort -o out.doc");
153 addExampleLine(
" You can also add columns and 'filling' its values with different options",
false);
154 addExampleLine(
"By example, to add the column 'shiftX' with uniform random value between 0 and 10",
false);
155 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --fill shiftX rand_uniform 0 10 -o out.doc");
156 addExampleLine(
"Or for initialize metadata columns 'shiftX' and 'shiftY' with a constant value of 5",
false);
157 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc -l \"shiftX shiftY\" constant 5 -o out.doc");
158 addExampleLine(
"If you have columns that represent the filename of a metadata with other data (ex CTFParams)",
false);
159 addExampleLine(
"you cannot 'expand' the column with the values in that metadata",
false);
160 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --fill CTFParams expand -o outExpanded.doc");
161 addExampleLine(
"For check all options availables for 'filling' mode, use: ",
false);
163 addExampleLine(
" write metadata as table in Sqlite3 database. (use xmipp_sqlite3 to visualize results)",
false);
164 addExampleLine (
" xmipp_metadata_utilities -i blocknameIn@mD1.doc -o blocknameOut@mD1.sqlite");
166 addExampleLine (
" xmipp_metadata_utilities -i blocknameIn@mD1.doc -o blocknameOut@mD1.xml");
167 addExampleLine(
" Copy files in metadata to a location. The metadata will be also copied to new location",
false);
168 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --file copy /home/pepe/newLocation");
170 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --file delete");
171 addExampleLine(
" Select elements in metadata that satisfy a given constrain.",
false);
172 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --query select \"angleRot > 10 AND anglePsi < 0.5\" -o out.doc");
173 addExampleLine(
" You can also modify your data using SQLite syntax expression",
false);
174 addExampleLine(
" xmipp_metadata_utilities -i a.doc --operate modify_values \"angleRot=2.*angleRot\" -o b.doc");
175 addExampleLine(
" xmipp_metadata_utilities -i a.doc --operate modify_values \"angleRot=radians(angleRot)\" -o b.doc");
176 addExampleLine(
" xmipp_metadata_utilities -i a.doc --operate modify_values \"angleRot=sin(radians(angleRot))\" -o b.doc");
177 addExampleLine(
" xmipp_metadata_utilities -i a.doc --operate modify_values \"angleRot=sqrt(angleRot)\" -o b.doc");
178 addExampleLine(
" xmipp_metadata_utilities -i a.doc --operate modify_values \"image=replace(image, 'xmp','spi')\" -o b.doc");
179 addExampleLine(
" xmipp_metadata_utilities -i a.doc --operate modify_values \"image='new_prefix_dir/'||image\" -o b.doc");
181 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc -q count CTFModel -o out.doc");
183 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc -q sum defocusGroup count -o out.doc");
185 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --query size");
187 addExampleLine (
" xmipp_metadata_utilities -i mD1.doc --operate rename_column \"weight wRobust\"");
222 if (operation ==
"union")
229 else if (operation ==
"union_all")
234 else if (operation ==
"intersection")
236 else if (operation ==
"subtraction")
238 else if (operation ==
"join")
241 md.
join1(mdIn, md2, label);
244 else if (operation ==
"natural_join")
250 else if (operation ==
"inner_join")
256 else if (operation ==
"merge")
262 operation =
getParam(
"--operate", 0);
264 if ( operation ==
"keep_column")
271 else if ( operation ==
"drop_column")
274 for (
size_t i = 0;
i < labels.size(); ++
i)
277 else if ( operation ==
"remove_duplicates")
283 else if ( operation ==
"rename_column")
288 else if (operation ==
"modify_values")
292 else if (operation ==
"expand")
296 for (
int i = 0;
i < factor;
i++)
303 if (operation ==
"sort")
308 else if (operation ==
"percentile")
312 double iN=1.0/md.
size();
314 for (
size_t objId : mdIn.
ids())
315 mdIn.
setValue(labelOut,(n++)*iN,objId);
317 else if (operation ==
"randomize")
319 else if (operation ==
"bootstrap")
321 std::vector<size_t> objId;
322 objId.resize(md.
size());
324 for (
size_t id : md.
ids())
327 int N_1=((int)objId.size())-1;
330 for (
size_t _ : md.
ids())
337 else if (operation ==
"random_subset")
361 if (operation ==
"expand")
367 else if (operation ==
"constant")
369 else if (operation.find(
"rand_") == 0)
375 if (type ==
"student")
379 else if (operation ==
"lineal")
382 std::string msg =
"Unsupported operation (" + operation +
383 "). If you believe this is an error, please contact developers.";
388 for (
size_t i = 0;
i < labels.size(); ++
i)
390 generator->
label = labels[
i];
391 generator->
fill(mdIn);
408 if (operation ==
"count")
414 else if (operation ==
"sum")
422 else if (operation ==
"select")
424 expression =
getParam(
"--query", 1);
428 else if (operation ==
"size")
431 std::cout << fn_in +
" size is: " << mdIn.
getParsedLines() << std::endl;
433 else if (operation ==
"labels")
436 std::cout << fn_in +
" has labels: " << std::endl;
438 for (
size_t i = 0;
i < labels.size(); ++
i)
441 else if (operation ==
"blocks")
445 std::cout <<
"Blocks in " << fn_in <<
": " << std::endl;
447 for (
size_t i = 0;
i < blocks.size(); ++
i)
448 std::cout << blocks[
i] << std::endl;
467 if (operation ==
"import_txt")
474 FileName path, inFnImg, outFnImg, oldOutFnImg=
"";
475 if (!(doDelete = operation ==
"delete"))
486 for (
size_t objId : mdIn.
ids())
488 mdIn.
getValue(label, inFnImg, objId);
495 remove(inFnImg.c_str());
501 if (operation ==
"copy" && isStack)
503 if(oldOutFnImg != outFnImg && oldOutFnImg !=
"")
507 oldOutFnImg = outFnImg;
508 outFnImg.
compose(counter, outFnImg);
510 mdIn.
setValue(label, outFnImg, objId);
512 if (operation ==
"copy" && isStack)
515 outFnImg = path +
"/" + outFnImg;
519 outFnImg = path +
"/" + outFnImg;
520 if (operation ==
"copy")
525 else if (operation ==
"move")
530 rename(inFnImg.c_str(), outFnImg.c_str());
543 else if (mdIn.
size()==0)
555 mdIn.
write(std::cout);
557 mdIn.
write(fn_out, mode);
Case or algorithm not implemented yet.
double getDoubleParam(const char *param, int arg=0)
static MDLabel str2Label(const String &labelName)
#define REPORT_ERROR(nerr, ErrormMsg)
void copyImage(const FileName &source, const FileName &target)
FileName removePrefixNumber() const
void compose(const String &str, const size_t no, const String &ext="")
FileName removeDirectories(int keep=0) const
String integerToString(int I, int _width, char fill_with)
std::vector< String > StringVector
void addSeeAlsoLine(const char *seeAlso)
const char * getParam(const char *param, int arg=0)
String findAndReplace(const String &tInput, const String &tFind, const String &tReplace)
int makePath(mode_t mode=0755) const
void addExampleLine(const char *example, bool verbatim=true)
#define STR_EQUAL(str1, str2)
bool checkParam(const char *param)
static void str2LabelVector(const String &labelsStr, std::vector< MDLabel > &labels)
static String label2Str(const MDLabel &label)
void addUsageLine(const char *line, bool verbatim=false)
Insufficient permissions to perform operation.
int getIntParam(const char *param, int arg=0)
void addParamsLine(const String &line)