Xmipp  v3.23.11-Nereus
extract_subset.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "extract_subset.h"
27 #include <ostream>
28 
30  s.check();
31  auto it = s.md.ids().begin();
32  // get to proper position
33  for (size_t i = 0; i < s.first; ++i)
34  ++it;
35 
36  auto destMD = MetaDataVec();
37  auto img = Image<float>();
38  // iterate through all items
39  for (size_t i = 0; i < s.count; ++it, i++) {
40  // orig name
41  FileName origName;
42  s.md.getValue(MDL_IMAGE, origName, *it);
43  // new name
44  FileName newName;
45  newName.compose(i + 1, s.outStk); // within stk file, index images from one (1)
46  // copy row, replace name
47  MDRowVec row = s.md.getRowVec(*it);
48  row.setValue(MDL_IMAGE, newName);
49  row.setValue(MDL_ENABLED, 1);
50  destMD.addRow(row);
51  // copy image
52  // FIXME DS maybe we can do this more efficiently
53  img.read(origName);
54  img.write(newName, i, true, WRITE_APPEND);
55  }
56  // store metadata
57  destMD.write(s.outXmd);
58 }
59 
61  // target directories must exist, so that we can write there
62  if ( ! outXmd.getDir().exists()) {
63  REPORT_ERROR(ERR_IO_NOTEXIST, "Directory " + outXmd.getDir() + " does not exist");
64  }
65  if ( ! outStk.getDir().exists()) {
66  REPORT_ERROR(ERR_IO_NOTEXIST, "Directory " + outStk.getDir() + " does not exist");
67  }
68  // target files must not exist, otherwise we will append to them
69  if (outXmd.exists()) {
70  REPORT_ERROR(ERR_IO_NOTEXIST, "File " + outXmd + " already exists.");
71  }
72  if (outStk.exists()) {
73  REPORT_ERROR(ERR_IO_NOTEXIST, "File " + outStk + " already exists");
74  }
75  const size_t n = md.size();
76  if (first >= n) {
77  REPORT_ERROR(ERR_LOGIC_ERROR, "Zero-based index of the first item ("
78  + std::to_string(first) + ") is bigger than size of the metadata ("
79  + std::to_string(n) + ")");
80  }
81  if (first + count > n) {
82  REPORT_ERROR(ERR_LOGIC_ERROR, "Out of range ("
83  + std::to_string(first) + " + "
84  + std::to_string(count) + " >= "
85  + std::to_string(n) + ")");
86  }
87 }
88 
89 std::ostream& operator<<(std::ostream &os, const ExtractSubset::Settings &s) {
90  os << "Input metadata : " << s.md.getFilename() << "\n";
91  os << "Output metadata : " << s.outXmd << "\n";
92  os << "Matching items : " << s.count << "\n";
93  os << "Skip disabled : " << (s.skipDisabled ? "yes" : "no") << "\n";
94  os.flush();
95  return os;
96 }
97 
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void setValue(const MDObject &object) override
void compose(const String &str, const size_t no, const String &ext="")
virtual IdIteratorProxy< false > ids()
#define i
Is this image enabled? (int [-1 or 1])
glob_log first
friend std::ostream & operator<<(std::ostream &os, const Settings &s)
File or directory does not exist.
Definition: xmipp_error.h:136
static void createSubset(const Settings &s)
bool getValue(MDObject &mdValueOut, size_t id) const override
MDRowVec getRowVec(size_t id)
std::string to_string(bond_type bondType)
Definition: compound.cpp:43
int * n
Name of an image (std::string)
virtual FileName getFilename() const
Some logical error in the pipeline.
Definition: xmipp_error.h:147