Medical Imaging Interaction Toolkit  2025.12.02
Medical Imaging Interaction Toolkit
mitkPythonContext.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 mitkPythonContext_h
14 #define mitkPythonContext_h
15 
16 #include <mitkImage.h>
17 #include <MitkPythonExports.h>
18 
19 #include <memory>
20 #include <optional>
21 
22 namespace mitk
23 {
24  class MITKPYTHON_EXPORT PythonContext : public itk::LightObject
25  {
26  public:
28 
29  itkFactorylessNewMacro(Self);
30  mitkNewMacro1Param(Self, const std::string&);
31 
36  void Activate();
37 
42  bool HasVariable(const std::string &varName);
43 
44  template <typename T>
45  std::optional<T> GetVariableAs(const std::string& varName);
46 
51  mitk::Image* LoadImageFromPython(const std::string &varName);
52 
57  void TransferBaseDataToPython(mitk::BaseData *mitkImage, const std::string &varName = "_mitk_image");
58 
63  std::string ExecuteString(const std::string &pyCommands);
64 
69  std::string ExecuteFile(const std::string &filePath);
70 
76 
81  std::string GetStdOut(const std::string &varName = "_mitk_stdout");
82 
83  protected:
84  explicit PythonContext(const std::string& venvName = {});
86 
87  private:
88  struct Impl;
89  std::unique_ptr<Impl> m_Impl;
90  };
91 
92  template <>
93  MITKPYTHON_EXPORT std::optional<bool> PythonContext::GetVariableAs<bool>(const std::string&);
94 
95  template <>
96  MITKPYTHON_EXPORT std::optional<int> PythonContext::GetVariableAs<int>(const std::string&);
97 
98  template <>
99  MITKPYTHON_EXPORT std::optional<double> PythonContext::GetVariableAs<double>(const std::string&);
100 
101  template <>
102  MITKPYTHON_EXPORT std::optional<std::string> PythonContext::GetVariableAs<std::string>(const std::string&);
103 }
104 
105 #endif
106 
#define MITKPYTHON_EXPORT
Base of all data objects.
Definition: mitkBaseData.h:44
Image class for storing images.
Definition: mitkImage.h:70
std::string ExecuteFile(const std::string &filePath)
Executes the given python file in the current context defined by globals (m_GlobalDictionary) & local...
PythonContext(const std::string &venvName={})
std::string GetStdOut(const std::string &varName="_mitk_stdout")
Returns value from the given string stream object. See: https://docs.python.org/3/library/io....
std::optional< T > GetVariableAs(const std::string &varName)
mitkClassMacroItkParent(PythonContext, itk::LightObject)
bool HasVariable(const std::string &varName)
Check if given variable exists in the current context defined by globals (m_GlobalDictionary) & local...
void TransferBaseDataToPython(mitk::BaseData *mitkImage, const std::string &varName="_mitk_image")
Creates view of mitk::BaseData pointer in MITK into corresponding SWIG proxy type object in Python.
mitkNewMacro1Param(Self, const std::string &)
void Activate()
Imports essential python packages: numpy, os, sys, io and pyMITK. Also adds current bin folder to pat...
std::string ExecuteString(const std::string &pyCommands)
Executes the given python syntax in the current context defined by globals (m_GlobalDictionary) & loc...
std::string GetPythonExceptionTraceback()
Returns any exception stacktrace occured in python as string back to MITK.
mitk::Image * LoadImageFromPython(const std::string &varName)
Provides view into mitk::Image type object in Python as mitk::Image* pointer in MITK.
Find image slices visible on a given plane.