Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
QmitknnUNetToolGUI.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 QmitknnUNetToolGUI_h
14 #define QmitknnUNetToolGUI_h
15 
18 #include "QmitknnUNetGPU.h"
19 #include "QmitknnUNetWorker.h"
20 #include "mitkProcessExecutor.h"
21 #include "mitknnUnetTool.h"
22 #include "ui_QmitknnUNetToolGUIControls.h"
24 #include <QCache>
25 #include <QMessageBox>
26 #include <QSettings>
27 #include <QThread>
30 #include <boost/functional/hash.hpp>
31 #include <unordered_map>
32 
34 {
35 public:
37  static size_t GetUniqueHash(std::vector<mitk::ModelParams> &requestQ)
38  {
39  size_t hashCode = 0;
40  for (mitk::ModelParams &request : requestQ)
41  {
42  boost::hash_combine(hashCode, request.generateHash());
43  }
44  return hashCode;
45  }
46 };
47 
49 {
50  Q_OBJECT
51 
52 public:
54  itkFactorylessNewMacro(Self);
55  itkCloneMacro(Self);
56 
57  QCache<size_t, nnUNetCache> m_Cache;
58 
63  std::unordered_map<std::string, mitk::ProcessExecutor::Pointer> m_Processes;
64 
65 protected slots:
66 
71  void OnPreviewRequested();
72 
77  void OnDirectoryChanged(const QString&);
78 
83  void OnModelChanged(const QString&);
84 
89  void OnTaskChanged(const QString &);
90 
95  void OnTrainerChanged(const QString&);
96 
101  void OnCheckBoxChanged(int);
102 
108  void SegmentationProcessFailed();
109 
114  void SegmentationResultHandler(mitk::nnUNetTool *, bool forceRender = false);
115 
120  void OnModalitiesNumberChanged(int);
121 
126  void OnPythonPathChanged(const QString&);
127 
132  void OnRefreshPresssed();
133 
138  void OnClearCachePressed();
139 
144  void OnDownloadModel();
145 
150  void OnDownloadWorkerExit(bool, const QString);
151 
156  void OnStopDownload();
157 
158 signals:
163 
164 protected:
167 
168  void ConnectNewTool(mitk::SegWithPreviewTool* newTool) override;
169  void InitializeUI(QBoxLayout* mainLayout) override;
170  void EnableWidgets(bool enabled) override;
171 
172 private:
176  void SetComboBoxToNone(ctkCheckableComboBox*);
177 
182  void FillAvailableModelsInfoFromJSON(const QString&);
183 
188  void ExportAvailableModelsAsJSON(const QString&);
189 
194  void ClearAllModalities();
195 
200  void DisplayMultiModalInfoFromJSON(const QString&);
201 
206  void ClearAllModalLabels();
207 
215  void DumpJSONfromPickle(const QString&);
216 
222  QString FetchResultsFolderFromEnv();
223 
229  unsigned int FetchSelectedGPUFromUI();
230 
236  void SetGPUInfo();
237 
242  void AddToCache(size_t&, mitk::LabelSetImage::ConstPointer);
243 
248  void CheckAllInCheckableComboBox(ctkCheckableComboBox*);
249 
256  std::pair<QStringList, QStringList> ExtractTrainerPlannerFromString(QStringList);
261  void ProcessEnsembleModelsParams(mitk::nnUNetTool::Pointer);
262 
267  void ProcessModelParams(mitk::nnUNetTool::Pointer);
268 
272  void ShowEnsembleLayout(bool visible = true);
273 
277  void ShowErrorMessage(const std::string&, QMessageBox::Icon = QMessageBox::Critical);
278 
282  void WriteStatusMessage(const QString&);
283 
287  void WriteErrorMessage(const QString&);
288 
293  void AutoParsePythonPaths();
294 
298  bool IsModelExists(const QString&, const QString&, const QString&);
299 
305  void ClearAllComboBoxes();
306 
311  void DisableEverything();
312 
318  bool IsNNUNetInstalled(const QString&);
319 
325  mitk::ModelParams MapToRequest(
326  const QString&, const QString&, const QString&, const QString&, const std::vector<std::string>&);
327 
333  std::vector<std::string> FetchSelectedFoldsFromUI(ctkCheckableComboBox*);
334 
340  std::vector<mitk::Image::ConstPointer> FetchMultiModalImagesFromUI();
341 
346  void UpdateCacheCountOnUI();
347 
348  Ui_QmitknnUNetToolGUIControls m_Controls;
349  QmitkGPULoader m_GpuLoader;
350 
355  std::vector<QmitkSingleNodeSelectionWidget*> m_Modalities;
356  std::vector<QLabel*> m_ModalLabels;
357 
358  std::vector<std::unique_ptr<QmitknnUNetTaskParamsUITemplate>> m_EnsembleParams;
359 
360  mitk::NodePredicateBase::Pointer m_MultiModalPredicate;
361 
362  QString m_PythonPath;
363 
370  int m_UI_ROWS;
371 
376  std::shared_ptr<QmitknnUNetFolderParser> m_ParentFolder = nullptr;
377 
382  const QStringList m_VALID_MODELS = {"2d", "3d_lowres", "3d_fullres", "3d_cascade_fullres", "ensembles"};
383 
384  const QString m_CACHE_COUNT_BASE_LABEL = "Cached Items: ";
385 
386  const QString m_MITK_EXPORT_JSON_FILENAME = "mitk_export.json";
387 
388  const QString m_AVAILABLE_MODELS_JSON_FILENAME = "available_models.json";
389 
390  const QString m_PICKLE_FILENAME = "plans.pkl";
391 
395  QSettings m_Settings;
396 
397  bool m_IsResultsFolderValid = false;
398 
399  QThread* m_nnUNetThread;
400  nnUNetDownloadWorker* m_Worker;
401 
402  bool m_FirstPreviewComputation = true;
403  EnableConfirmSegBtnFunctionType m_SuperclassEnableConfirmSegBtnFnc;
404 };
405 #endif
mitk::SegWithPreviewTool
Base class for any auto segmentation tool that provides a preview of the new segmentation.
Definition: mitkSegWithPreviewTool.h:38
MITKSEGMENTATIONUI_EXPORT
#define MITKSEGMENTATIONUI_EXPORT
Definition: MitkSegmentationUIExports.h:15
QmitkMultiLabelSegWithPreviewToolGUIBase.h
mitk::ModelParams
nnUNet parameter request object holding all model parameters for input. Also holds output temporary d...
Definition: mitknnUnetTool.h:35
mitk::ProcessExecutor::ArgumentListType
std::vector< std::string > ArgumentListType
Definition: mitkProcessExecutor.h:81
nnUNetCache
Definition: QmitknnUNetToolGUI.h:33
mitkProcessExecutor.h
QmitknnUNetGPU.h
QmitkGPULoader
Class to load and save GPU information for further validation.
Definition: QmitknnUNetGPU.h:35
QmitkSingleNodeSelectionWidget.h
QmitkMultiLabelSegWithPreviewToolGUIBase::InitializeUI
void InitializeUI(QBoxLayout *mainLayout) override
mitknnUnetTool.h
QmitknnUNetWorker.h
itk::SmartPointer< const Self >
mitk::nnUNetTool
nnUNet segmentation tool.
Definition: mitknnUnetTool.h:68
QmitknnUNetToolGUI::m_Processes
std::unordered_map< std::string, mitk::ProcessExecutor::Pointer > m_Processes
The hash map stores all bifurcating processes' ID.
Definition: QmitknnUNetToolGUI.h:63
nnUNetCache::m_SegCache
mitk::LabelSetImage::ConstPointer m_SegCache
Definition: QmitknnUNetToolGUI.h:36
nnUNetCache::GetUniqueHash
static size_t GetUniqueHash(std::vector< mitk::ModelParams > &requestQ)
Definition: QmitknnUNetToolGUI.h:37
QmitkMultiLabelSegWithPreviewToolGUIBase::EnableWidgets
void EnableWidgets(bool enabled) override
QmitknnUNetToolGUI
Definition: QmitknnUNetToolGUI.h:48
QmitkMultiLabelSegWithPreviewToolGUIBase::mitkClassMacro
mitkClassMacro(QmitkMultiLabelSegWithPreviewToolGUIBase, QmitkSegWithPreviewToolGUIBase)
QmitknnUNetFolderParser.h
MitkSegmentationUIExports.h
QmitknnUNetToolGUI::m_Cache
QCache< size_t, nnUNetCache > m_Cache
Definition: QmitknnUNetToolGUI.h:55
QmitknnUNetEnsembleLayout.h
nnUNetDownloadWorker
Class to execute some functions from the Segmentation Plugin in a separate thread.
Definition: QmitknnUNetWorker.h:26
QmitkMultiLabelSegWithPreviewToolGUIBase
GUI for tools based on mitk::AutoMLSegmentationWithPreviewTool.
Definition: QmitkMultiLabelSegWithPreviewToolGUIBase.h:28
QmitkSegWithPreviewToolGUIBase::ConnectNewTool
virtual void ConnectNewTool(mitk::SegWithPreviewTool *newTool)