Medical Imaging Interaction Toolkit  2023.12.00
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmitknnUNetFolderParser.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef QmitknnUNetFolderParser_h
14 #define QmitknnUNetFolderParser_h
15 
16 #include <QDirIterator>
17 #include <memory>
18 #include <functional>
19 #include <QString>
20 #include <vector>
21 
26 struct FolderNode
27 {
28  QString name;
29  QString path; // parent
30  std::vector<std::shared_ptr<FolderNode>> subFolders;
31 };
32 
40 {
41 public:
47  QmitknnUNetFolderParser(const QString parentFolder);
52  ~QmitknnUNetFolderParser() = default;
58  QString getResultsFolder();
59 
67  template <typename T>
69  {
70  auto models = GetSubFolderNamesFromNode<T>(m_RootNode);
71  return models;
72  }
73 
82  template <typename T>
83  T getTasksForModel(const QString &modelName)
84  {
85  std::shared_ptr<FolderNode> modelNode = GetSubNodeMatchingNameCrietria(modelName, m_RootNode);
86  auto tasks = GetSubFolderNamesFromNode<T>(modelNode);
87  return tasks;
88  }
89 
98  template <typename T>
99  T getModelsForTask(const QString &taskName)
100  {
101  T modelsForTask;
102  auto models = GetSubFolderNamesFromNode<T>(m_RootNode);
103  foreach (QString model, models)
104  {
105  QStringList taskList = getTasksForModel<QStringList>(model);
106  if (taskList.contains(taskName, Qt::CaseInsensitive))
107  {
108  modelsForTask << model;
109  }
110  }
111  return modelsForTask;
112  }
113 
121  template <typename T>
123  {
124  T allTasks;
125  auto models = GetSubFolderNamesFromNode<T>(m_RootNode);
126  foreach (QString model, models)
127  {
128  allTasks << getTasksForModel<QStringList>(model);
129  }
130  return allTasks;
131  }
132 
142  template <typename T>
143  T getTrainerPlannersForTask(const QString &taskName, const QString &modelName)
144  {
145  std::shared_ptr<FolderNode> modelNode = GetSubNodeMatchingNameCrietria(modelName, m_RootNode);
146  std::shared_ptr<FolderNode> taskNode = GetSubNodeMatchingNameCrietria(taskName, modelNode);
147  auto tps = GetSubFolderNamesFromNode<T>(taskNode);
148  return tps;
149  }
150 
162  template <typename T>
163  T getFoldsForTrainerPlanner(const QString &trainer,
164  const QString &planner,
165  const QString &taskName,
166  const QString &modelName)
167  {
168  std::shared_ptr<FolderNode> modelNode = GetSubNodeMatchingNameCrietria(modelName, m_RootNode);
169  std::shared_ptr<FolderNode> taskNode = GetSubNodeMatchingNameCrietria(taskName, modelNode);
170  QString trainerPlanner = trainer + QString("__") + planner;
171  std::shared_ptr<FolderNode> tpNode = GetSubNodeMatchingNameCrietria(trainerPlanner, taskNode);
172  auto folds = GetSubFolderNamesFromNode<T>(tpNode);
173  return folds;
174  }
175 
176 private:
177  const int m_LEVEL = 4;
178  std::shared_ptr<FolderNode> m_RootNode;
179 
186  std::function<bool(QString)> RuleEngine(int level);
187 
195  std::shared_ptr<FolderNode> GetSubNodeMatchingNameCrietria(const QString &queryName, std::shared_ptr<FolderNode> parentNode);
196 
205  template <typename T>
206  T GetSubFolderNamesFromNode(const std::shared_ptr<FolderNode> parent)
207  {
208  T folders;
209  std::vector<std::shared_ptr<FolderNode>> subNodes = parent->subFolders;
210  for (std::shared_ptr<FolderNode> folder : subNodes)
211  {
212  folders.push_back(folder->name);
213  }
214  return folders;
215  }
216 
224  void InitDirs(std::shared_ptr<FolderNode> parent, int level);
225 
233  void DeleteDirs(std::shared_ptr<FolderNode> parent, int level);
234 
243  template <typename T>
244  T FetchFoldersFromDir(const QString &path, std::function<bool(QString)> callback)
245  {
246  T folders;
247  for (QDirIterator it(path, QDir::AllDirs, QDirIterator::NoIteratorFlags); it.hasNext();)
248  {
249  it.next();
250  if (!it.fileName().startsWith('.') && callback(it.filePath()))
251  {
252  folders.push_back(it.fileName());
253  }
254  }
255  return folders;
256  }
257 };
258 #endif
QmitknnUNetFolderParser::QmitknnUNetFolderParser
QmitknnUNetFolderParser(const QString parentFolder)
Construct a new QmitknnUNetFolderParser object Initializes root folder node object pointer calls.
FolderNode
Struct to store each (Folder) Node of the hierarchy tree structure.
Definition: QmitknnUNetFolderParser.h:26
QmitknnUNetFolderParser
Class to store and retreive folder hierarchy information of RESULTS_FOLDER. Only Root node is explici...
Definition: QmitknnUNetFolderParser.h:39
FolderNode::subFolders
std::vector< std::shared_ptr< FolderNode > > subFolders
Definition: QmitknnUNetFolderParser.h:30
QmitknnUNetFolderParser::getTrainerPlannersForTask
T getTrainerPlannersForTask(const QString &taskName, const QString &modelName)
Returns the trainer / planner names for a given task & model. Template function, type can be any of s...
Definition: QmitknnUNetFolderParser.h:143
QmitknnUNetFolderParser::getTasksForModel
T getTasksForModel(const QString &modelName)
Returns the task names for a given model. Template function, type can be any of stl or Qt containers ...
Definition: QmitknnUNetFolderParser.h:83
QmitknnUNetFolderParser::getResultsFolder
QString getResultsFolder()
Returns the "Results Folder" string which is parent path of the root node.
QmitknnUNetFolderParser::~QmitknnUNetFolderParser
~QmitknnUNetFolderParser()=default
Destroy the QmitknnUNetFolderParser object.
QmitknnUNetFolderParser::getModelsForTask
T getModelsForTask(const QString &taskName)
Returns the models names for a given task. Template function, type can be any of stl or Qt containers...
Definition: QmitknnUNetFolderParser.h:99
QmitknnUNetFolderParser::getAllTasks
T getAllTasks()
Returns all the task names present in the root node with possible duplicates. Template function,...
Definition: QmitknnUNetFolderParser.h:122
QmitknnUNetFolderParser::getFoldsForTrainerPlanner
T getFoldsForTrainerPlanner(const QString &trainer, const QString &planner, const QString &taskName, const QString &modelName)
Returns the Folds names for a given trainer,planner,task & model name. Template function,...
Definition: QmitknnUNetFolderParser.h:163
FolderNode::path
QString path
Definition: QmitknnUNetFolderParser.h:29
QmitknnUNetFolderParser::getModelNames
T getModelNames()
Returns the Model Names from root node. Template function, type can be any of stl or Qt containers wh...
Definition: QmitknnUNetFolderParser.h:68
FolderNode::name
QString name
Definition: QmitknnUNetFolderParser.h:28