diff --git a/Modules/SegmentationUI/Qmitk/QmitknnUNetFolderParser.cpp b/Modules/SegmentationUI/Qmitk/QmitknnUNetFolderParser.cpp new file mode 100644 index 0000000000..d1b50b09df --- /dev/null +++ b/Modules/SegmentationUI/Qmitk/QmitknnUNetFolderParser.cpp @@ -0,0 +1,96 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file.s + +============================================================================*/ + +#include "QmitknnUNetFolderParser.h" + +QmitknnUNetFolderParser::QmitknnUNetFolderParser(const QString parentFolder) +{ + m_RootNode = std::make_shared(); + m_RootNode->path = parentFolder; + m_RootNode->name = QString("nnUNet"); + m_RootNode->subFolders.clear(); + InitDirs(m_RootNode, 0); +} + +QString QmitknnUNetFolderParser::getResultsFolder() +{ + return m_RootNode->path; +} + +std::function QmitknnUNetFolderParser::RuleEngine(int level) +{ + if (level == m_LEVEL - 1) + { + return [](QString path) + { + return (QFile::exists(path + QDir::separator() + QString("model_final_checkpoint.model")) && + QFile::exists(path + QDir::separator() + QString("model_final_checkpoint.model.pkl")) && + QFile::exists(path + QDir::separator() + QString("debug.json"))); + }; + } + if (level == m_LEVEL - 2) + { + return [](QString path) { return QFile::exists(path + QDir::separator() + QString("plans.pkl")); }; + } + else + { + return [](QString /*path*/) { return true; }; + } +} + +std::shared_ptr QmitknnUNetFolderParser::GetSubNodeMatchingNameCrietria( + const QString &queryName, std::shared_ptr parentNode) +{ + std::shared_ptr retNode; + std::vector> subNodes = parentNode->subFolders; + for (std::shared_ptr node : subNodes) + { + if (node->name == queryName) + { + retNode = node; + break; + } + } + return retNode; +} + +void QmitknnUNetFolderParser::InitDirs(std::shared_ptr parent, int level) +{ + QString searchFolder = parent->path + QDir::separator() + parent->name; + auto rules = RuleEngine(level); + auto subFolders = FetchFoldersFromDir(searchFolder, rules); + level++; + foreach (QString folder, subFolders) + { + std::shared_ptr fp = std::make_shared(); + fp->path = searchFolder; + fp->name = folder; + if (level < this->m_LEVEL) + { + InitDirs(fp, level); + } + parent->subFolders.push_back(fp); + } +} + +void QmitknnUNetFolderParser::DeleteDirs(std::shared_ptr parent, int level) +{ + level++; + for (std::shared_ptr subFolder : parent->subFolders) + { + if (level < m_LEVEL) + { + DeleteDirs(subFolder, level); + } + parent->subFolders.clear(); + } +} diff --git a/Modules/SegmentationUI/Qmitk/QmitknnUNetFolderParser.h b/Modules/SegmentationUI/Qmitk/QmitknnUNetFolderParser.h index 7edf2882fe..d37acf666a 100644 --- a/Modules/SegmentationUI/Qmitk/QmitknnUNetFolderParser.h +++ b/Modules/SegmentationUI/Qmitk/QmitknnUNetFolderParser.h @@ -1,328 +1,257 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file.s ============================================================================*/ #ifndef QmitknnUNetFolderParser_h_Included #define QmitknnUNetFolderParser_h_Included -#include "QmitknnUNetToolGUI.h" #include #include #include /** * @brief Struct to store each (Folder) Node of the hierarchy tree structure. * */ struct FolderNode { QString name; QString path; // parent std::vector> subFolders; }; /** * @brief Class to store and retreive folder hierarchy information * of RESULTS_FOLDER. Only Root node is explicitly stored in m_RootNode. * No. of sub levels in the hierachry is defined in the LEVEL constant. * */ -class MITKSEGMENTATIONUI_EXPORT QmitknnUNetFolderParser +class QmitknnUNetFolderParser { public: /** * @brief Construct a new QmitknnUNetFolderParser object * Initializes root folder node object pointer calls * @param parentFolder */ - QmitknnUNetFolderParser(const QString parentFolder) - { - m_RootNode = std::make_shared(); - m_RootNode->path = parentFolder; - m_RootNode->name = QString("nnUNet"); - m_RootNode->subFolders.clear(); - InitDirs(m_RootNode, 0); - } - + QmitknnUNetFolderParser(const QString parentFolder); /** * @brief Destroy the QmitknnUNetFolderParser object * */ - ~QmitknnUNetFolderParser() = default; /*{ DeleteDirs(m_RootNode, LEVEL); }*/ - + ~QmitknnUNetFolderParser() = default; /** * @brief Returns the "Results Folder" string which is parent path of the root node. * * @return QString */ - QString getResultsFolder() { return m_RootNode->path; } + QString getResultsFolder(); /** * @brief Returns the Model Names from root node. Template function, * type can be any of stl or Qt containers which supports push_back call. * * @tparam T * @return T (any of stl or Qt containers which supports push_back call) */ template T getModelNames() { auto models = GetSubFolderNamesFromNode(m_RootNode); return models; } /** * @brief Returns the task names for a given model. Template function, * type can be any of stl or Qt containers which supports push_back call. * * @tparam T * @param modelName * @return T (any of stl or Qt containers which supports push_back call) */ template T getTasksForModel(const QString &modelName) { std::shared_ptr modelNode = GetSubNodeMatchingNameCrietria(modelName, m_RootNode); auto tasks = GetSubFolderNamesFromNode(modelNode); return tasks; } /** * @brief Returns the models names for a given task. Template function, * type can be any of stl or Qt containers which supports push_back call. * * @tparam T * @param taskName * @return T (any of stl or Qt containers which supports push_back call) */ template T getModelsForTask(const QString &taskName) { T modelsForTask; auto models = GetSubFolderNamesFromNode(m_RootNode); foreach (QString model, models) { QStringList taskList = getTasksForModel(model); if (taskList.contains(taskName, Qt::CaseInsensitive)) { modelsForTask << model; } } return modelsForTask; } /** * @brief Returns all the task names present in the root node with possible duplicates. * Template function, type can be any of stl or Qt containers which supports push_back call. * * @param T * @param taskName * @return T (any of stl or Qt containers which supports push_back call) */ template T getAllTasks() { T allTasks; auto models = GetSubFolderNamesFromNode(m_RootNode); foreach (QString model, models) { allTasks << getTasksForModel(model); } return allTasks; } /** * @brief Returns the trainer / planner names for a given task & model. Template function, * type can be any of stl or Qt containers which supports push_back call. * * @tparam T * @param taskName * @param modelName * @return T (any of stl or Qt containers which supports push_back call) */ template T getTrainerPlannersForTask(const QString &taskName, const QString &modelName) { std::shared_ptr modelNode = GetSubNodeMatchingNameCrietria(modelName, m_RootNode); std::shared_ptr taskNode = GetSubNodeMatchingNameCrietria(taskName, modelNode); auto tps = GetSubFolderNamesFromNode(taskNode); return tps; } /** * @brief Returns the Folds names for a given trainer,planner,task & model name. Template function, * type can be any of stl or Qt containers which supports push_back call. * * @tparam T * @param trainer * @param planner * @param taskName * @param modelName * @return T (any of stl or Qt containers which supports push_back call) */ template T getFoldsForTrainerPlanner(const QString &trainer, const QString &planner, const QString &taskName, const QString &modelName) { std::shared_ptr modelNode = GetSubNodeMatchingNameCrietria(modelName, m_RootNode); std::shared_ptr taskNode = GetSubNodeMatchingNameCrietria(taskName, modelNode); QString trainerPlanner = trainer + QString("__") + planner; std::shared_ptr tpNode = GetSubNodeMatchingNameCrietria(trainerPlanner, taskNode); auto folds = GetSubFolderNamesFromNode(tpNode); return folds; } private: const int m_LEVEL = 4; std::shared_ptr m_RootNode; /** * @brief Returns rule function wrapper to check for specific files at given Result_Folder hierarchy level. * * @param level * @return std::function */ - std::function RuleEngine(int level) - { - if (level == m_LEVEL - 1) - { - return [](QString path) - { - return (QFile::exists(path + QDir::separator() + QString("model_final_checkpoint.model")) && - QFile::exists(path + QDir::separator() + QString("model_final_checkpoint.model.pkl")) && - QFile::exists(path + QDir::separator() + QString("debug.json"))); - }; - } - if (level == m_LEVEL - 2) - { - return [](QString path) { return QFile::exists(path + QDir::separator() + QString("plans.pkl")); }; - } - else - { - return [](QString /*path*/) { return true; }; - } - } + std::function RuleEngine(int level); /** * @brief Iterates through the root node and returns the sub FolderNode object Matching Name Crietria * * @param queryName * @param parentNode * @return std::shared_ptr */ - std::shared_ptr GetSubNodeMatchingNameCrietria(const QString &queryName, - std::shared_ptr parentNode) - { - std::shared_ptr retNode; - std::vector> subNodes = parentNode->subFolders; - for (std::shared_ptr node : subNodes) - { - if (node->name == queryName) - { - retNode = node; - break; - } - } - return retNode; - } + std::shared_ptr GetSubNodeMatchingNameCrietria(const QString &queryName, std::shared_ptr parentNode); /** * @brief Returns the sub folder names for a folder node object. Template function, * type can be any of stl or Qt containers which supports push_back call. * * @tparam T * @param std::shared_ptr * @return T (any of stl or Qt containers which supports push_back call) */ template T GetSubFolderNamesFromNode(const std::shared_ptr parent) { T folders; std::vector> subNodes = parent->subFolders; for (std::shared_ptr folder : subNodes) { folders.push_back(folder->name); } return folders; } /** * @brief Iterates through the sub folder hierarchy upto a level provided * and create a tree structure. * * @param parent * @param level */ - void InitDirs(std::shared_ptr parent, int level) - { - QString searchFolder = parent->path + QDir::separator() + parent->name; - auto rules = RuleEngine(level); - auto subFolders = FetchFoldersFromDir(searchFolder, rules); - level++; - foreach (QString folder, subFolders) - { - std::shared_ptr fp = std::make_shared(); - fp->path = searchFolder; - fp->name = folder; - if (level < this->m_LEVEL) - { - InitDirs(fp, level); - } - parent->subFolders.push_back(fp); - } - } + void InitDirs(std::shared_ptr parent, int level); /** * @brief Iterates through the sub folder hierarchy upto a level provided * and clears the sub folder std::vector from each node. * * @param parent * @param level */ - void DeleteDirs(std::shared_ptr parent, int level) - { - level++; - for (std::shared_ptr subFolder : parent->subFolders) - { - if (level < m_LEVEL) - { - DeleteDirs(subFolder, level); - } - parent->subFolders.clear(); - } - } + void DeleteDirs(std::shared_ptr parent, int level); /** * @brief Template function to fetch all folders inside a given path. * The type can be any of stl or Qt containers which supports push_back call. * * @tparam T * @param path * @return T */ template T FetchFoldersFromDir(const QString &path, std::function callback) { T folders; for (QDirIterator it(path, QDir::AllDirs, QDirIterator::NoIteratorFlags); it.hasNext();) { it.next(); if (!it.fileName().startsWith('.') && callback(it.filePath())) { folders.push_back(it.fileName()); } } return folders; } }; #endif diff --git a/Modules/SegmentationUI/Qmitk/QmitknnUNetGPU.cpp b/Modules/SegmentationUI/Qmitk/QmitknnUNetGPU.cpp new file mode 100644 index 0000000000..d0478d1332 --- /dev/null +++ b/Modules/SegmentationUI/Qmitk/QmitknnUNetGPU.cpp @@ -0,0 +1,52 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file.s + +============================================================================*/ + +#include "QmitknnUNetGPU.h" +#include + +QmitkGPULoader::QmitkGPULoader() +{ + QProcess process; + process.start("nvidia-smi --query-gpu=name,memory.total --format=csv"); + process.waitForFinished(-1); + QStringList infoStringList; + while (process.canReadLine()) + { + QString line = process.readLine(); + if (!line.startsWith("name")) + { + infoStringList << line; + } + } + unsigned int count = 0; + foreach (QString infoString, infoStringList) + { + QmitkGPUSpec spec; + QStringList gpuDetails; + gpuDetails = infoString.split(","); + spec.name = gpuDetails.at(0); + spec.id = count; + spec.memory = gpuDetails.at(1); + this->m_Gpus.push_back(spec); + ++count; + } +} + +int QmitkGPULoader::GetGPUCount() +{ + return static_cast(m_Gpus.size()); +} + +std::vector QmitkGPULoader::GetAllGPUSpecs() +{ + return m_Gpus; +} diff --git a/Modules/SegmentationUI/Qmitk/QmitknnUNetGPU.h b/Modules/SegmentationUI/Qmitk/QmitknnUNetGPU.h index cf92c9e028..b7f0ccbf7d 100644 --- a/Modules/SegmentationUI/Qmitk/QmitknnUNetGPU.h +++ b/Modules/SegmentationUI/Qmitk/QmitknnUNetGPU.h @@ -1,92 +1,63 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file.s ============================================================================*/ #ifndef QmitknnUNetToolGPU_h_Included #define QmitknnUNetToolGPU_h_Included -#include -#include -#include #include +#include +#include /** * @brief Struct to store GPU info. * */ struct QmitkGPUSpec { QString name; QString memory; unsigned int id; }; /** * @brief Class to load and save GPU information * for further validation */ -class QmitkGPULoader : public QObject +class MITKSEGMENTATIONUI_EXPORT QmitkGPULoader { - Q_OBJECT - private: std::vector m_Gpus; public: /** * @brief Construct a new Qmitk GPU Loader object. * Parses GPU info using `nvidia-smi` command and saves it as QmitkGPUSpec objects. */ - QmitkGPULoader() - { - QProcess process; - process.start("nvidia-smi --query-gpu=name,memory.total --format=csv"); - process.waitForFinished(-1); - QStringList infoStringList; - while (process.canReadLine()) - { - QString line = process.readLine(); - if (!line.startsWith("name")) - { - infoStringList << line; - } - } - unsigned int count = 0; - foreach (QString infoString, infoStringList) - { - QmitkGPUSpec spec; - QStringList gpuDetails; - gpuDetails = infoString.split(","); - spec.name = gpuDetails.at(0); - spec.id = count; - spec.memory = gpuDetails.at(1); - this->m_Gpus.push_back(spec); - ++count; - } - } + QmitkGPULoader(); ~QmitkGPULoader() = default; /** * @brief Returns the number of GPUs parsed and saved as QmitkGPUSpec objects. * * @return int */ - int GetGPUCount() { return static_cast(m_Gpus.size()); } + int GetGPUCount(); /** * @brief Returns all the parsed GPU information * * @return std::vector */ - std::vector GetAllGPUSpecs() { return m_Gpus; } + std::vector GetAllGPUSpecs(); }; #endif diff --git a/Modules/SegmentationUI/files.cmake b/Modules/SegmentationUI/files.cmake index 818830625e..e1bf1397ec 100644 --- a/Modules/SegmentationUI/files.cmake +++ b/Modules/SegmentationUI/files.cmake @@ -1,77 +1,79 @@ set( CPP_FILES Qmitk/QmitkSegWithPreviewToolGUIBase.cpp Qmitk/QmitkMultiLabelSegWithPreviewToolGUIBase.cpp Qmitk/QmitkBinaryThresholdToolGUIBase.cpp Qmitk/QmitkBinaryThresholdToolGUI.cpp Qmitk/QmitkBinaryThresholdULToolGUI.cpp Qmitk/QmitkCalculateGrayValueStatisticsToolGUI.cpp Qmitk/QmitkConfirmSegmentationDialog.cpp Qmitk/QmitkCopyToClipBoardDialog.cpp Qmitk/QmitkDrawPaintbrushToolGUI.cpp Qmitk/QmitkErasePaintbrushToolGUI.cpp Qmitk/QmitkLiveWireTool2DGUI.cpp Qmitk/QmitkOtsuTool3DGUI.cpp Qmitk/QmitkPaintbrushToolGUI.cpp Qmitk/QmitkPickingToolGUI.cpp Qmitk/QmitkPixelManipulationToolGUI.cpp Qmitk/QmitkSlicesInterpolator.cpp Qmitk/QmitkToolGUI.cpp Qmitk/QmitkToolGUIArea.cpp Qmitk/QmitkToolSelectionBox.cpp +Qmitk/QmitknnUNetFolderParser.cpp Qmitk/QmitknnUNetToolGUI.cpp Qmitk/QmitknnUNetWorker.cpp +Qmitk/QmitknnUNetGPU.cpp Qmitk/QmitkSurfaceStampWidget.cpp Qmitk/QmitkMaskStampWidget.cpp Qmitk/QmitkSliceBasedInterpolatorWidget.cpp Qmitk/QmitkStaticDynamicSegmentationDialog.cpp Qmitk/QmitkSurfaceBasedInterpolatorWidget.cpp Qmitk/QmitkSimpleLabelSetListWidget.cpp ) set(MOC_H_FILES Qmitk/QmitkSegWithPreviewToolGUIBase.h Qmitk/QmitkMultiLabelSegWithPreviewToolGUIBase.h Qmitk/QmitkBinaryThresholdToolGUIBase.h Qmitk/QmitkBinaryThresholdToolGUI.h Qmitk/QmitkBinaryThresholdULToolGUI.h Qmitk/QmitkCalculateGrayValueStatisticsToolGUI.h Qmitk/QmitkConfirmSegmentationDialog.h Qmitk/QmitkCopyToClipBoardDialog.h Qmitk/QmitkDrawPaintbrushToolGUI.h Qmitk/QmitkErasePaintbrushToolGUI.h Qmitk/QmitkLiveWireTool2DGUI.h Qmitk/QmitkOtsuTool3DGUI.h Qmitk/QmitkPaintbrushToolGUI.h Qmitk/QmitkPickingToolGUI.h Qmitk/QmitkPixelManipulationToolGUI.h Qmitk/QmitkSlicesInterpolator.h Qmitk/QmitkToolGUI.h Qmitk/QmitkToolGUIArea.h Qmitk/QmitkToolSelectionBox.h +Qmitk/QmitknnUNetFolderParser.h Qmitk/QmitknnUNetToolGUI.h Qmitk/QmitknnUNetGPU.h Qmitk/QmitknnUNetWorker.h Qmitk/QmitknnUNetEnsembleLayout.h -Qmitk/QmitknnUNetFolderParser.h Qmitk/QmitkSurfaceStampWidget.h Qmitk/QmitkMaskStampWidget.h Qmitk/QmitkSliceBasedInterpolatorWidget.h Qmitk/QmitkStaticDynamicSegmentationDialog.h Qmitk/QmitkSurfaceBasedInterpolatorWidget.h Qmitk/QmitkSimpleLabelSetListWidget.h ) set(UI_FILES Qmitk/QmitkConfirmSegmentationDialog.ui Qmitk/QmitkOtsuToolWidgetControls.ui Qmitk/QmitkLiveWireTool2DGUIControls.ui Qmitk/QmitkSurfaceStampWidgetGUIControls.ui Qmitk/QmitkMaskStampWidgetGUIControls.ui Qmitk/QmitkSliceBasedInterpolatorWidgetGUIControls.ui Qmitk/QmitkSurfaceBasedInterpolatorWidgetGUIControls.ui Qmitk/QmitknnUNetToolGUIControls.ui ) set(QRC_FILES resources/SegmentationUI.qrc )