13 # pragma warning (disable : 4996) 21 #include "itkNrrdImageIO.h" 22 #include "itkImageFileWriter.h" 35 const std::string
DATA =
"data";
36 const std::string
ITEM =
"item";
38 const std::string
NAME =
"name";
39 const std::string
ID =
"id";
41 const std::string
LINK =
"link";
44 static std::string
GetName(std::string fileName,std::string suffix,
bool longName =
false)
46 fileName = QFileInfo(QString::fromStdString(fileName)).fileName().toStdString();
48 return fileName.substr(0,fileName.length() -suffix.length()-11);
50 return fileName.substr(0,fileName.length() -suffix.length()-9);
53 static std::string
GetDate(std::string fileName,std::string suffix,
bool longName =
false)
55 fileName = QFileInfo(QString::fromStdString(fileName)).fileName().toStdString();
57 fileName = fileName.substr(fileName.length() - suffix.length()-10,10);
59 fileName = fileName.substr(fileName.length() - suffix.length()-8,8);
62 fileName.insert(6,
"-");
63 fileName.insert(4,
"-");
74 QDir fileName = QFileInfo(xmlFile.c_str()).absoluteDir();
75 std::string outputFolder = fileName.path().toStdString() + QDir::separator().toLatin1();
76 QDir baseFolder(outputFolder.c_str());
77 baseFolder.mkpath(outputFolder.c_str());
79 std::ofstream xmlFileStream;
80 xmlFileStream.open (xmlFile.c_str());
81 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
83 unsigned int subColId = 0;
85 unsigned int dataId = 0;
87 QDir dir(QString::fromStdString(outputFolder));
88 for (
size_t i = 0 ; i < dataCollection->
Size(); ++i)
93 dir.mkpath(QString::fromStdString(dataCollection->
IndexToName(i)));
97 if (subCollections ==
nullptr)
99 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
103 for (
size_t d = 0; d < subCollections->
Size(); d++ )
106 QString subPath = QString::fromStdString(dataCollection->
IndexToName(i))+
"/"+QString::fromStdString(subCollections->
IndexToName(d));
111 if (itemCollections ==
nullptr)
113 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
117 for (
size_t s = 0; s < itemCollections->
Size(); s++)
119 if (filter.size() > 0)
121 bool isSelected =
false;
122 for (
size_t f = 0; f < filter.size(); f++)
124 if (filter.at(f) == itemCollections->
IndexToName(s) )
130 if (isSelected ==
false)
134 QString fileName = dir.path() + dir.separator() + subPath + dir.separator() + QString::fromStdString(dataCollection->
IndexToName(i)) +
"_" + QString::fromStdString(subCollections->
IndexToName(d)) +
"_" + QString::fromStdString(itemCollections->
IndexToName(s));
141 catch (
const std::exception& e )
143 MITK_ERROR <<
"Caught exception: " << e.what();
146 std::string relativeFilename = baseFolder.relativeFilePath(fileName).toStdString();
147 xmlFileStream <<
" <" <<
ITEM <<
" " <<
NAME <<
"=\"" <<itemCollections->
IndexToName(s) <<
"\" " <<
FILEPATH <<
"=\"" <<
"\" " <<
LINK <<
"=\"" << relativeFilename <<
"\" />\n";
149 xmlFileStream <<
" </" <<
DATA <<
">\n";
157 xmlFileStream.flush();
158 xmlFileStream.close();
164 std::vector<std::string> mods;
170 QDir origFilename = QFileInfo(dataCollection->
GetXMLFile().c_str()).absoluteDir();
171 QString originalFolder = origFilename.path() + QDir::separator();
176 QDir fileName = QFileInfo(xmlFile.c_str()).absoluteDir();
177 std::string outputFolder = fileName.path().toStdString() + QDir::separator().toLatin1();
178 QDir baseFolder(outputFolder.c_str());
180 std::ofstream xmlFileStream;
181 xmlFileStream.open (xmlFile.c_str());
182 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
184 unsigned int subColId = 0;
186 unsigned int dataId = 0;
188 QDir dir(QString::fromStdString(outputFolder));
189 for (
size_t i = 0 ; i < dataCollection->
Size(); ++i)
194 dir.mkpath(QString::fromStdString(dataCollection->
IndexToName(i)));
198 if (subCollections ==
nullptr)
200 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
204 for (
size_t d = 0; d < subCollections->
Size(); d++ )
207 QString subPath = QString::fromStdString(dataCollection->
IndexToName(i))+
"/"+QString::fromStdString(subCollections->
IndexToName(d));
212 if (itemCollections ==
nullptr)
214 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
218 for (
size_t s = 0; s < itemCollections->
Size(); s++)
220 if (filter.size() > 0)
222 bool isSelected =
false;
223 for (
size_t f = 0; f < filter.size(); f++)
225 if (filter.at(f) == itemCollections->
IndexToName(s) )
231 if (isSelected ==
false)
236 bool fullName =
false;
239 fileName = originalFolder + QString::fromStdString(itemCollections->
GetDataFilePath(s));
244 fileName = dir.path() + dir.separator() + subPath + dir.separator() + QString::fromStdString(dataCollection->
IndexToName(i)) +
"_" + QString::fromStdString(subCollections->
IndexToName(d)) +
"_" + QString::fromStdString(itemCollections->
IndexToName(s));
253 catch (
const std::exception& e )
255 MITK_ERROR <<
"Caught exception: " << e.what();
258 std::string relativeFilename =baseFolder.relativeFilePath(fileName).toStdString();
259 xmlFileStream <<
" <" <<
ITEM <<
" " <<
NAME <<
"=\"" <<itemCollections->
IndexToName(s) <<
"\" " <<
FILEPATH <<
"=\"" <<
"\" " <<
LINK <<
"=\"" << relativeFilename <<
"\" />\n";
261 xmlFileStream <<
" </" <<
DATA <<
">\n";
269 xmlFileStream.flush();
270 xmlFileStream.close();
279 parseDir.setFilter( QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
280 parseDir.setPath(QString::fromStdString(folder));
282 QFileInfoList qFileList = parseDir.entryInfoList();
285 std::ofstream xmlFileStream;
286 xmlFileStream.open (xmlFile.c_str());
287 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
288 xmlFileStream <<
"<" <<
COLLECTION <<
" " <<
NAME <<
"=\"" <<
"GEN" <<
"\" >\n";
290 unsigned int dataId = 0;
293 for (
int i = 0; i < qFileList.size(); ++i)
296 std::string baseFolder = qFileList.at(i).absoluteFilePath().toStdString() + QDir::separator().toLatin1() + collectionType;
298 MITK_INFO <<
"Processing : " << baseFolder;
299 if (!QFileInfo(QString::fromStdString(baseFolder)).isDir())
301 MITK_WARN <<
"Not a valid folder, skipping.";
310 if (fileList.size() <= 0 || fileList.at(0).size() <= 0)
315 std::string name =
GetName(fileList.at(0).at(0),filter.at(0));
316 xmlFileStream <<
" <" <<
SUBCOLLECTION <<
" " << NAME <<
"=\"" << name <<
"\" " <<
FILEPATH <<
"=\"\" id=\"Col" << i <<
"\" >\n";
319 for (
unsigned int k=0;
k < fileList.at(0).size(); ++
k)
321 std::string strDate =
GetDate(fileList.at(0).at(
k),filter.at(0));
322 xmlFileStream <<
" <" <<
DATA <<
" " << NAME <<
"=\"" << strDate <<
"\" " <<
" id=\"Data" << dataId <<
"\" >\n";
324 for (
unsigned int i=0; i< filter.size(); ++i)
326 std::string fileName = fileList.at(i).at(
k);
327 xmlFileStream <<
" <" <<
ITEM <<
" " << NAME <<
"=\"" << seriesNames.at(i) <<
"\" " <<
LINK <<
"=\"" << fileName <<
"\" />\n";
329 xmlFileStream <<
" </" <<
DATA <<
">\n" ;
335 xmlFileStream.flush();
336 xmlFileStream.close();
343 std::ofstream xmlFileStream;
344 xmlFileStream.open (xmlFile.c_str());
345 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
346 xmlFileStream <<
"<" <<
COLLECTION <<
" " <<
NAME <<
"=\"" <<
"GEN" <<
"\" >\n";
348 unsigned int dataId = 0;
358 std::string name =
GetName(fileList.at(0).at(0),filter.at(0),longDate);
359 xmlFileStream <<
" <" <<
SUBCOLLECTION <<
" " << NAME <<
"=\"" << name <<
"\" " <<
FILEPATH <<
"=\"\" id=\"Col" << 0 <<
"\" >\n";
362 for (
unsigned int k=skipUntil;
k < fileList.at(0).size(); ++
k)
364 std::string strDate =
GetDate(fileList.at(0).at(
k),filter.at(0),
true);
365 xmlFileStream <<
" <" <<
DATA <<
" " << NAME <<
"=\"" << strDate <<
"\" " <<
" id=\"Data" << dataId <<
"\" >\n";
367 for (
unsigned int i=0; i< filter.size(); ++i)
369 std::string fileName = fileList.at(i).at(
k);
370 xmlFileStream <<
" <" <<
ITEM <<
" " << NAME <<
"=\"" << seriesNames.at(i) <<
"\" " <<
LINK <<
"=\"" << fileName <<
"\" />\n";
376 xmlFileStream <<
" </" <<
DATA <<
">\n" ;
378 if (GetIndexForinXMonths(fileList,months,
k+1,filter)== 0)
384 xmlFileStream.flush();
385 xmlFileStream.close();
392 std::string strDate0 =
GetDate(fileList.at(0).at(curIndex),filter.at(0),
true);
394 int year0 =std::atoi(strDate0.substr(0,4).c_str());
395 int month0 =std::atoi(strDate0.substr(5,2).c_str());
396 int day0 = std::atoi(strDate0.substr(8,2).c_str());
398 size_t bestIndex = 0;
401 for (
size_t i=curIndex+1; i < fileList.at(0).size(); ++i)
403 std::string strDate =
GetDate(fileList.at(0).at(i),filter.at(0),
true);
404 int year =std::atoi(strDate.substr(0,4).c_str());
405 int month =std::atoi(strDate.substr(5,2).c_str());
406 int day = std::atoi(strDate.substr(8,2).c_str());
408 int fit = std::fabs((months * 30 ) - (((year-year0)*360) +((month-month0)*30) + (day-day0)));
vcl_size_t Size() const
Size - number of data items in collection.
static FileListType GenerateFileLists(std::string folder, std::vector< std::string > suffixes, bool allowGaps=false)
GenerateFileLists Returns a collection of lists with valid files names in a folder.
static bool SaveCollection(DataCollection *dataCollection, std::vector< std::string > filter, std::string xmlFile="")
SaveCollection - Stores data collection at original location.
const std::string SUBCOLLECTION
static FileListType SanitizeFileList(FileListType list)
SanitizeFileList Removes all entries that are lacking at least one modality.
const std::string COLLECTION
mitk::Image::Pointer GetMitkImage(vcl_size_t index)
GetMitkImage - casts data to mitk::Image and returns it.
std::string GetDataFilePath(vcl_size_t index) const
static std::string GetDate(std::string fileName, std::string suffix, bool longName=false)
std::vector< std::vector< std::string > > FileListType
static bool SingleFolderToXml(std::string folder, std::string xmlFile, std::vector< std::string > filter, std::vector< std::string > seriesNames, bool longDate=true, int skipUntil=0, float months=0)
mitk::Image::Pointer image
static bool ExportCollectionToFolder(DataCollection *dataCollection, std::string xmlFile, std::vector< std::string > filter)
ExportCollectionToFolder.
const std::string FILEPATH
std::string IndexToName(vcl_size_t index) const
IndexToName - Get name from index.
static bool FolderToXml(std::string folder, std::string collectionType, std::string xmlFile, std::vector< std::string > filter, std::vector< std::string > seriesNames)
std::string GetName() const
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
std::string GetXMLFile()
SetXMLFile - gets xml file to which data collection is supposed to be saved.
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.
static std::string GetName(std::string fileName, std::string suffix, bool longName=false)