Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
mitkMonaiLabelTool.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 #ifndef mitkMonaiLabelTool_h
13 #define mitkMonaiLabelTool_h
14 
15 #include "mitkSegWithPreviewTool.h"
17 #include <memory>
18 #include <unordered_map>
19 #include <set>
20 #include <nlohmann/json.hpp>
21 #include <mitkPointSet.h>
23 
24 
25 namespace us
26 {
27  class ModuleResource;
28 }
29 
30 namespace mitk
31 {
37  {
38  std::string name;
39  std::string type;
40  std::unordered_map<std::string, int> labels;
41  int dimension;
42  std::string description;
43  std::unordered_map<bool, std::string> config;
44 
45  inline bool operator==(const MonaiModelInfo &rhs) const
46  {
47  return (this->name == rhs.name && this->type == rhs.type); // Comparing only name and type, for now.
48  }
49 
50  inline bool IsInteractive() const
51  {
52  return ("deepgrow" == type || "deepedit" == type);
53  }
54 
55  inline bool Is2D() const
56  {
57  return dimension == 2;
58  }
59  };
60 
66  {
67  std::string name;
68  std::string description;
69  std::vector<std::string> labels;
70  std::string version;
71  std::string hostName;
72  int port;
73  std::string origin;
74  std::vector<MonaiModelInfo> models;
75  };
76 
83  {
85  std::string hostName;
86  std::string requestLabel;
87  int port;
88 
89  inline bool operator==(const MonaiLabelRequest &rhs) const
90  {
91  return (this->model == rhs.model && this->hostName == rhs.hostName && this->port == rhs.port &&
92  this->requestLabel == rhs.requestLabel);
93  }
94  };
95 
105  {
106  public:
108  itkCloneMacro(Self);
109  void Activated() override;
110  void Deactivated() override;
111  void UpdatePrepare() override;
112 
117  void FetchOverallInfo(const std::string &hostName, const int &port);
118 
123  std::unique_ptr<MonaiLabelRequest> m_RequestParameters;
124 
129  const std::vector<MonaiModelInfo> GetAutoSegmentationModels(const int dim = -1) const;
130 
135  const std::vector<MonaiModelInfo> GetInteractiveSegmentationModels(const int dim = -1) const;
136 
141  const std::vector<MonaiModelInfo> GetScribbleSegmentationModels(const int dim = -1) const;
142 
146  MonaiModelInfo GetModelInfoFromName(const std::string) const;
147 
148  itkSetMacro(ModelName, std::string);
149  itkGetConstMacro(ModelName, std::string);
150  itkSetMacro(URL, std::string);
151  itkGetConstMacro(URL, std::string);
152  itkSetMacro(TempDir, std::string);
153  itkGetConstMacro(TempDir, std::string);
154  const MonaiAppMetadata *GetInfoParameters() const;
155 
156 
160  void ClearPicks();
161 
166  bool HasPicks() const;
167 
169 
170  protected:
171  MonaiLabelTool();
172  ~MonaiLabelTool();
173  void DoUpdatePreview(const Image* inputAtTimeStep, const Image* oldSegAtTimeStep, LabelSetImage* previewImage, TimeStepType timeStep) override;
174  void ConnectActionsAndFunctions() override;
175 
180  virtual void WriteImage(const Image *, const std::string &) const = 0;
181 
182  /*
183  * @brief Add positive seed point action of StateMachine pattern.
184  */
185  virtual void OnAddPositivePoint(StateMachineAction *, InteractionEvent *interactionEvent);
186 
187  /*
188  * @brief Add negative seed point action of StateMachine pattern
189  */
190  virtual void OnAddNegativePoint(StateMachineAction *, InteractionEvent *interactionEvent);
191 
192  /*
193  * @brief Delete action of StateMachine pattern
194  */
195  virtual void OnDelete(StateMachineAction *, InteractionEvent *);
196 
197  /*
198  * @brief Clear all seed points and call UpdatePreview to reset the segmentation Preview
199  */
200  void ClearSeeds();
201 
206  virtual std::string ConvertPointsAsListString(const mitk::BaseGeometry *baseGeometry,
207  const PointSet::Pointer pointSet) const;
208 
212  virtual void WriteBackResults(LabelSetImage *, LabelSetImage *, TimeStepType) const = 0;
213 
218  int m_PointSetCount = 0;
219 
220  private:
221 
226  std::unique_ptr<MonaiAppMetadata> m_InfoParameters;
227 
232  std::pair<std::string, std::string> CreateTempDirs(const std::string &filePattern) const;
233 
237  bool IsMonaiServerOn(const std::string &hostName, const int &port) const;
238 
243  mitk::Image::Pointer ApplyLevelWindowEffect(const Image *inputAtTimeStep) const;
244 
249  void PostInferRequest(const std::string &hostName, const int &port, const std::string &filePath, const std::string &outFile,
250  const mitk::BaseGeometry *baseGeometry);
251 
252  std::string m_TempDir;
253  std::string m_ModelName;
254  std::string m_URL;
255  nlohmann::json m_ResultMetadata;
256  const std::set<std::string> m_AUTO_SEG_TYPE_NAME = {"segmentation"};
257  const std::set<std::string> m_SCRIBBLE_SEG_TYPE_NAME = {"scribbles"};
258  const std::set<std::string> m_INTERACTIVE_SEG_TYPE_NAME = {"deepgrow"}; // deepedit not supported yet
259  const std::string m_TEMPLATE_FILENAME = "XXXXXX_000_0000.nii.gz";
260  const std::string m_SERVER_503_ERROR_TEXT = "A connection to MonaiLabel server cannot be established.";
261  const Label::PixelType MASK_VALUE = 1;
262  };
263 }
264 #endif
mitk::MonaiAppMetadata::models
std::vector< MonaiModelInfo > models
Definition: mitkMonaiLabelTool.h:74
mitk::SegWithPreviewTool
Base class for any auto segmentation tool that provides a preview of the new segmentation.
Definition: mitkSegWithPreviewTool.h:38
mitk::InteractionEvent
Definition: mitkInteractionEvent.h:26
mitk::MonaiAppMetadata
Struct to store MonaiLabel server metadata including all model infos.
Definition: mitkMonaiLabelTool.h:65
mitk::Message1< const bool >
mitk::MonaiModelInfo::description
std::string description
Definition: mitkMonaiLabelTool.h:42
mitk::MonaiModelInfo::type
std::string type
Definition: mitkMonaiLabelTool.h:39
mitk::MonaiLabelRequest::port
int port
Definition: mitkMonaiLabelTool.h:87
mitk::MonaiAppMetadata::version
std::string version
Definition: mitkMonaiLabelTool.h:70
us
Definition: mitkAbstractFileReader.h:29
mitk::MonaiLabelRequest::requestLabel
std::string requestLabel
Definition: mitkMonaiLabelTool.h:86
mitk::MonaiAppMetadata::description
std::string description
Definition: mitkMonaiLabelTool.h:68
mitkSegWithPreviewTool.h
mitk::MonaiModelInfo::IsInteractive
bool IsInteractive() const
Definition: mitkMonaiLabelTool.h:50
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
itk::SmartPointer< Self >
mitk::MonaiModelInfo::name
std::string name
Definition: mitkMonaiLabelTool.h:38
mitk::MonaiModelInfo::Is2D
bool Is2D() const
Definition: mitkMonaiLabelTool.h:55
mitk::MonaiLabelTool::m_RequestParameters
std::unique_ptr< MonaiLabelRequest > m_RequestParameters
Variable to set selected model's and other data needed for the POST call.
Definition: mitkMonaiLabelTool.h:123
mitk::MonaiLabelRequest::operator==
bool operator==(const MonaiLabelRequest &rhs) const
Definition: mitkMonaiLabelTool.h:89
mitk::MonaiLabelTool::MonaiStatusEvent
Message1< const bool > MonaiStatusEvent
Definition: mitkMonaiLabelTool.h:168
mitk::Label::PixelType
unsigned short PixelType
Definition: mitkLabel.h:34
mitk::MonaiLabelRequest
Request class to pack model and other necessary server information from GUI.
Definition: mitkMonaiLabelTool.h:82
mitk::MonaiAppMetadata::labels
std::vector< std::string > labels
Definition: mitkMonaiLabelTool.h:69
mitkPointSet.h
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::MonaiAppMetadata::origin
std::string origin
Definition: mitkMonaiLabelTool.h:73
mitk::EventStateMachine
‍**
Definition: mitkEventStateMachine.h:111
mitk::MonaiAppMetadata::port
int port
Definition: mitkMonaiLabelTool.h:72
mitk::MonaiLabelTool
MonaiLabel segmentation tool base class.
Definition: mitkMonaiLabelTool.h:104
mitk::MonaiLabelTool::m_PointSetNegative
PointSet::Pointer m_PointSetNegative
Definition: mitkMonaiLabelTool.h:215
mitk::MonaiAppMetadata::name
std::string name
Definition: mitkMonaiLabelTool.h:67
mitkInteractionPositionEvent.h
mitk::MonaiLabelTool::m_PointSetNodeNegative
DataNode::Pointer m_PointSetNodeNegative
Definition: mitkMonaiLabelTool.h:217
json
nlohmann::json json
Definition: mitkModelTestFixture.h:29
mitk::BaseGeometry
BaseGeometry Describes the geometry of a data object.
Definition: mitkBaseGeometry.h:94
mitk::MonaiModelInfo
Struct to hold featured models individual info.
Definition: mitkMonaiLabelTool.h:36
mitk::MonaiLabelTool::m_PointSetNodePositive
DataNode::Pointer m_PointSetNodePositive
Definition: mitkMonaiLabelTool.h:216
MITKSEGMENTATION_EXPORT
#define MITKSEGMENTATION_EXPORT
Definition: MitkSegmentationExports.h:15
mitk::MonaiModelInfo::labels
std::unordered_map< std::string, int > labels
Definition: mitkMonaiLabelTool.h:40
mitk::MonaiLabelRequest::hostName
std::string hostName
Definition: mitkMonaiLabelTool.h:85
mitk::MonaiAppMetadata::hostName
std::string hostName
Definition: mitkMonaiLabelTool.h:71
mitk::TimeStepType
std::size_t TimeStepType
Definition: mitkTimeGeometry.h:27
mitk::StateMachineAction
Represents an action, that is executed after a certain event (in statemachine-mechanism) TODO: implem...
Definition: mitkStateMachineAction.h:30
mitk::MonaiModelInfo::config
std::unordered_map< bool, std::string > config
Definition: mitkMonaiLabelTool.h:43
mitkClassMacro
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:36
mitk::MonaiModelInfo::dimension
int dimension
Definition: mitkMonaiLabelTool.h:41
MitkSegmentationExports.h
mitk::MonaiLabelTool::m_PointSetPositive
PointSet::Pointer m_PointSetPositive
Definition: mitkMonaiLabelTool.h:214
mitk::MonaiModelInfo::operator==
bool operator==(const MonaiModelInfo &rhs) const
Definition: mitkMonaiLabelTool.h:45
mitk::LabelSetImage
LabelSetImage class for handling labels and layers in a segmentation session.
Definition: mitkLabelSetImage.h:43
mitk::MonaiLabelRequest::model
MonaiModelInfo model
Definition: mitkMonaiLabelTool.h:84