23 #include "PythonPath.h"
24 #include <vtkPolyData.h>
28 #include <numpy/arrayobject.h>
35 const QString mitk::PythonService::m_TmpDataFileName(
"temp_mitk_data_file");
36 #ifdef USE_MITK_BUILTIN_PYTHON
37 static char* pHome = NULL;
41 : m_ItkWrappingAvailable( true ), m_OpenCVWrappingAvailable( true ), m_VtkWrappingAvailable( true ), m_ErrorOccured( false )
46 bool pythonInitialized =
static_cast<bool>( Py_IsInitialized() );
48 MITK_DEBUG <<
"pythonInitialized " << pythonInitialized;
49 MITK_DEBUG <<
"m_PythonManager.isPythonInitialized() " << m_PythonManager.isPythonInitialized();
54 if( !m_PythonManager.isPythonInitialized() )
60 #if defined (__APPLE__) || defined(MACOSX)
61 const char* library =
"libpython2.7.dylib";
63 const char* library =
"libpython2.7.so";
66 if(dlopen(library, RTLD_NOW | RTLD_GLOBAL) == 0 )
68 mitkThrow() <<
"Python runtime could not be loaded: " << dlerror();
73 QDir programmDir( QString( programPath.c_str() ).append(
"/Python") );
74 QString pythonCommand;
79 if ( programmDir.exists() ) {
81 pythonCommand.append( QString(
"import site, sys\n") );
82 pythonCommand.append( QString(
"sys.path.append('')\n") );
83 pythonCommand.append( QString(
"sys.path.append('%1')\n").arg(programPath.c_str()) );
84 pythonCommand.append( QString(
"sys.path.append('%1/Python')").arg(programPath.c_str()) );
89 pythonCommand.append( QString(
"import site, sys\n") );
90 pythonCommand.append( QString(
"sys.path.append('')\n") );
91 pythonCommand.append( QString(
"sys.path.append('%1')\n").arg(EXTERNAL_DIST_PACKAGES) );
92 pythonCommand.append( QString(
"\nsite.addsitedir('%1')").arg(EXTERNAL_SITE_PACKAGES) );
95 if( pythonInitialized )
96 m_PythonManager.setInitializationFlags(PythonQt::RedirectStdOut|PythonQt::PythonAlreadyInitialized);
98 m_PythonManager.setInitializationFlags(PythonQt::RedirectStdOut);
101 #ifdef USE_MITK_BUILTIN_PYTHON
103 if ( programmDir.exists() )
104 pythonHome.append(QString(
"%1/Python").arg(programPath.c_str()));
106 pythonHome.append(PYTHONHOME);
108 if(pHome)
delete[] pHome;
109 pHome =
new char[pythonHome.toStdString().length() + 1];
111 strcpy(pHome,pythonHome.toStdString().c_str());
112 Py_SetPythonHome(pHome);
113 MITK_DEBUG(
"PythonService") <<
"PythonHome: " << pHome;
116 MITK_DEBUG(
"PythonService") <<
"initalizing python";
118 m_PythonManager.initialize();
120 #ifdef USE_MITK_BUILTIN_PYTHON
121 PyObject* dict = PyDict_New();
123 if (PyDict_GetItemString(dict,
"__builtins__") == NULL)
125 PyObject* builtinMod = PyImport_ImportModule(
"__builtin__");
126 if (builtinMod == NULL ||
127 PyDict_SetItemString(dict,
"__builtins__", builtinMod) != 0)
133 Py_DECREF(builtinMod);
137 MITK_DEBUG(
"PythonService")<<
"Python Search paths: " << Py_GetPath();
138 MITK_DEBUG(
"PythonService") <<
"python initalized";
141 m_PythonManager.executeString( pythonCommand, ctkAbstractPythonManager::FileInput );
145 MITK_DEBUG(
"PythonService") <<
"exception initalizing python";
152 MITK_DEBUG(
"mitk::PythonService") <<
"destructing PythonService";
154 #ifdef USE_MITK_BUILTIN_PYTHON
162 QString pythonCommand = QString::fromStdString(stdpythonCommand);
164 MITK_DEBUG(
"mitk::PythonService") <<
"pythonCommand = " << pythonCommand.toStdString();
165 MITK_DEBUG(
"mitk::PythonService") <<
"commandType = " << commandType;
169 bool commandIssued =
true;
172 result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::SingleInput );
174 result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::FileInput );
176 result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::EvalInput );
178 commandIssued =
false;
182 this->NotifyObserver(pythonCommand.toStdString());
183 m_ErrorOccured = PythonQt::self()->hadError();
186 return result.toString().toStdString();
191 m_PythonManager.executeFile(QString::fromStdString(pythonScript));
196 std::vector<mitk::PythonVariable> list;
198 PyObject* dict = PyImport_GetModuleDict();
199 PyObject*
object = PyDict_GetItemString(dict,
"__main__");
200 PyObject* dirMain = PyObject_Dir(
object);
201 PyObject* tempObject = 0;
202 PyObject* strTempObject = 0;
206 std::string name, attrValue, attrType;
208 for(
int i = 0; i<PyList_Size(dirMain); i++)
210 tempObject = PyList_GetItem(dirMain, i);
211 name = PyString_AsString(tempObject);
212 tempObject = PyObject_GetAttrString(
object, name.c_str() );
213 attrType = tempObject->ob_type->tp_name;
215 strTempObject = PyObject_Repr(tempObject);
216 if(strTempObject && ( PyUnicode_Check(strTempObject) || PyString_Check(strTempObject) ) )
217 attrValue = PyString_AsString(strTempObject);
234 bool varExists =
false;
236 std::vector<mitk::PythonVariable> allVars = this->GetVariableStack();
237 for(
unsigned int i = 0; i< allVars.size(); i++)
239 if( allVars.at(i).m_Name == name )
251 if(!m_Observer.contains(observer))
252 m_Observer.append(observer);
257 m_Observer.removeOne(observer);
262 MITK_DEBUG(
"mitk::PythonService") <<
"number of observer " << m_Observer.size();
263 for(
int i=0; i< m_Observer.size(); ++i )
265 m_Observer.at(i)->CommandExecuted(command);
271 QString tmpFolder = QDir::tempPath();
272 QString fileName = tmpFolder + QDir::separator() + m_TmpDataFileName + QString::fromStdString(ext);
278 QString varName = QString::fromStdString( stdvarName );
284 PyObject *pyMod = PyImport_AddModule((
char*)
"__main__");
286 PyObject *pyDict = PyModule_GetDict(pyMod);
291 PyObject* npyArray = NULL;
293 void* array = (
void*) racc.
GetData();
298 const vnl_matrix_fixed<ScalarType, 3, 3> &transform =
307 xDirection.Normalize();
308 yDirection.Normalize();
309 zDirection.Normalize();
312 npy_intp* npy_dims =
new npy_intp[1];
313 npy_dims[0] = imgDim[0];
320 QString dimensionString;
321 dimensionString.append(QString(
"["));
322 dimensionString.append(QString::number(imgDim[0]));
323 for (
unsigned i = 1; i < 3; ++i)
327 dimensionString.append(QString(
","));
328 dimensionString.append(QString::number(imgDim[i]));
329 npy_dims[0] *= imgDim[i];
331 dimensionString.append(
"]");
338 NPY_TYPES npy_type = NPY_USHORT;
339 std::string sitk_type =
"sitkUInt8";
340 if( ioPixelType == itk::ImageIOBase::SCALAR )
343 npy_type = NPY_DOUBLE;
344 sitk_type =
"sitkFloat64";
346 npy_type = NPY_FLOAT;
347 sitk_type =
"sitkFloat32";
349 npy_type = NPY_SHORT;
350 sitk_type =
"sitkInt16";
353 sitk_type =
"sitkInt8";
356 sitk_type =
"sitkInt32";
359 sitk_type =
"sitkInt64";
361 npy_type = NPY_UBYTE;
362 sitk_type =
"sitkUInt8";
365 sitk_type =
"sitkUInt32";
368 sitk_type =
"sitkUInt64";
370 npy_type = NPY_USHORT;
371 sitk_type =
"sitkUInt16";
374 else if ( ioPixelType == itk::ImageIOBase::VECTOR ||
375 ioPixelType == itk::ImageIOBase::RGB ||
376 ioPixelType == itk::ImageIOBase::RGBA
380 npy_type = NPY_DOUBLE;
381 sitk_type =
"sitkVectorFloat64";
383 npy_type = NPY_FLOAT;
384 sitk_type =
"sitkVectorFloat32";
386 npy_type = NPY_SHORT;
387 sitk_type =
"sitkVectorInt16";
390 sitk_type =
"sitkVectorInt8";
393 sitk_type =
"sitkVectorInt32";
396 sitk_type =
"sitkVectorInt64";
398 npy_type = NPY_UBYTE;
399 sitk_type =
"sitkVectorUInt8";
402 sitk_type =
"sitkVectorUInt32";
405 sitk_type =
"sitkVectorUInt64";
407 npy_type = NPY_USHORT;
408 sitk_type =
"sitkVectorUInt16";
412 MITK_WARN <<
"not a recognized pixeltype";
417 import_array1 (
true);
418 npyArray = PyArray_SimpleNewFromData(npy_nd,npy_dims,npy_type,array);
421 const int status = PyDict_SetItemString( pyDict,QString(
"%1_numpy_array")
422 .arg(varName).toStdString().c_str(),
431 command.append( QString(
"%1 = sitk.Image(%2,sitk.%3,%4)\n").arg(varName)
432 .arg(dimensionString)
434 command.append( QString(
"%1.SetSpacing([%2,%3,%4])\n").arg(varName)
435 .arg(QString::number(spacing[0]))
436 .arg(QString::number(spacing[1]))
437 .arg(QString::number(spacing[2])) );
438 command.append( QString(
"%1.SetOrigin([%2,%3,%4])\n").arg(varName)
439 .arg(QString::number(origin[0]))
440 .arg(QString::number(origin[1]))
441 .arg(QString::number(origin[2])) );
442 command.append( QString(
"%1.SetDirection([%2,%3,%4,%5,%6,%7,%8,%9,%10])\n").arg(varName)
443 .arg(QString::number(xDirection[0]))
444 .arg(QString::number(xDirection[1]))
445 .arg(QString::number(xDirection[2]))
446 .arg(QString::number(yDirection[0]))
447 .arg(QString::number(yDirection[1]))
448 .arg(QString::number(yDirection[2]))
449 .arg(QString::number(zDirection[0]))
450 .arg(QString::number(zDirection[1]))
451 .arg(QString::number(zDirection[2]))
454 command.append( QString(
"_SimpleITK._SetImageFromArray(%1_numpy_array,%1)\n").arg(varName) );
455 command.append( QString(
"del %1_numpy_array").arg(varName) );
457 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
468 typedef itk::RGBPixel< unsigned char > UCRGBPixelType;
469 typedef itk::RGBPixel< unsigned short > USRGBPixelType;
470 typedef itk::RGBPixel< float > FloatRGBPixelType;
471 typedef itk::RGBPixel< double > DoubleRGBPixelType;
472 typedef itk::Image< UCRGBPixelType > UCRGBImageType;
473 typedef itk::Image< USRGBPixelType > USRGBImageType;
474 typedef itk::Image< FloatRGBPixelType > FloatRGBImageType;
475 typedef itk::Image< DoubleRGBPixelType > DoubleRGBImageType;
476 typedef itk::RGBAPixel< unsigned char > UCRGBAPixelType;
477 typedef itk::RGBAPixel< unsigned short > USRGBAPixelType;
478 typedef itk::RGBAPixel< float > FloatRGBAPixelType;
479 typedef itk::RGBAPixel< double > DoubleRGBAPixelType;
480 typedef itk::Image< UCRGBAPixelType > UCRGBAImageType;
481 typedef itk::Image< USRGBAPixelType > USRGBAImageType;
482 typedef itk::Image< FloatRGBAPixelType > FloatRGBAImageType;
483 typedef itk::Image< DoubleRGBAPixelType > DoubleRGBAImageType;
485 mitk::PixelType pixelType = mitk::MakePixelType<char, char >(nrComponents);
487 if (nrComponents == 1)
489 if( pythonPixeltype.compare(
"float64") == 0 ) {
490 pixelType = mitk::MakePixelType<double, double >(nrComponents);
491 }
else if( pythonPixeltype.compare(
"float32") == 0 ) {
492 pixelType = mitk::MakePixelType<float, float >(nrComponents);
493 }
else if( pythonPixeltype.compare(
"int16") == 0) {
494 pixelType = mitk::MakePixelType<short, short >(nrComponents);
495 }
else if( pythonPixeltype.compare(
"int8") == 0 ) {
496 pixelType = mitk::MakePixelType<char, char >(nrComponents);
497 }
else if( pythonPixeltype.compare(
"int32") == 0 ) {
498 pixelType = mitk::MakePixelType<int, int >(nrComponents);
499 }
else if( pythonPixeltype.compare(
"int64") == 0 ) {
500 pixelType = mitk::MakePixelType<long, long >(nrComponents);
501 }
else if( pythonPixeltype.compare(
"uint8") == 0 ) {
502 pixelType = mitk::MakePixelType<unsigned char, unsigned char >(nrComponents);
503 }
else if( pythonPixeltype.compare(
"uint32") == 0 ) {
504 pixelType = mitk::MakePixelType<unsigned int, unsigned int >(nrComponents);
505 }
else if( pythonPixeltype.compare(
"uint64") == 0 ) {
506 pixelType = mitk::MakePixelType<unsigned long, unsigned long >(nrComponents);
507 }
else if( pythonPixeltype.compare(
"uint16") == 0 ) {
508 pixelType = mitk::MakePixelType<unsigned short, unsigned short >(nrComponents);
512 mitkThrow()<<
"unknown scalar PixelType";
514 }
else if(nrComponents == 3 && dimensions == 2) {
515 if( pythonPixeltype.compare(
"float64") == 0 ) {
516 pixelType = mitk::MakePixelType<DoubleRGBImageType>();
517 }
else if( pythonPixeltype.compare(
"float32") == 0 ) {
518 pixelType = mitk::MakePixelType<FloatRGBImageType>();
519 }
else if( pythonPixeltype.compare(
"uint8") == 0 ) {
520 pixelType = mitk::MakePixelType<UCRGBImageType>();
521 }
else if( pythonPixeltype.compare(
"uint16") == 0 ) {
522 pixelType = mitk::MakePixelType<USRGBImageType>();
524 }
else if( (nrComponents == 4) && dimensions == 2 ) {
525 if( pythonPixeltype.compare(
"float64") == 0 ) {
526 pixelType = mitk::MakePixelType<DoubleRGBAImageType>();
527 }
else if( pythonPixeltype.compare(
"float32") == 0 ) {
528 pixelType = mitk::MakePixelType<FloatRGBAImageType>();
529 }
else if( pythonPixeltype.compare(
"uint8") == 0 ) {
530 pixelType = mitk::MakePixelType<UCRGBAImageType>();
531 }
else if( pythonPixeltype.compare(
"uint16") == 0 ) {
532 pixelType = mitk::MakePixelType<USRGBAImageType>();
536 if( pythonPixeltype.compare(
"float64") == 0 ) {
537 pixelType = mitk::MakePixelType<double, itk::Vector<double,3> >(nrComponents);
538 }
else if( pythonPixeltype.compare(
"float32") == 0 ) {
539 pixelType = mitk::MakePixelType<float, itk::Vector<float,3> >(nrComponents);
540 }
else if( pythonPixeltype.compare(
"int16") == 0) {
541 pixelType = mitk::MakePixelType<short, itk::Vector<short,3> >(nrComponents);
542 }
else if( pythonPixeltype.compare(
"int8") == 0 ) {
543 pixelType = mitk::MakePixelType<char, itk::Vector<char,3> >(nrComponents);
544 }
else if( pythonPixeltype.compare(
"int32") == 0 ) {
545 pixelType = mitk::MakePixelType<int, itk::Vector<int,3> >(nrComponents);
546 }
else if( pythonPixeltype.compare(
"int64") == 0 ) {
547 pixelType = mitk::MakePixelType<long, itk::Vector<long,3> >(nrComponents);
548 }
else if( pythonPixeltype.compare(
"uint8") == 0 ) {
549 pixelType = mitk::MakePixelType<unsigned char, itk::Vector<unsigned char,3> >(nrComponents);
550 }
else if( pythonPixeltype.compare(
"uint16") == 0 ) {
551 pixelType = mitk::MakePixelType<unsigned short, itk::Vector<unsigned short,3> >(nrComponents);
552 }
else if( pythonPixeltype.compare(
"uint32") == 0 ) {
553 pixelType = mitk::MakePixelType<unsigned int, itk::Vector<unsigned int,3> >(nrComponents);
554 }
else if( pythonPixeltype.compare(
"uint64") == 0 ) {
555 pixelType = mitk::MakePixelType<unsigned long, itk::Vector<unsigned long,3> >(nrComponents);
557 mitkThrow()<<
"unknown vectorial PixelType";
568 PyObject *pyMod = PyImport_AddModule((
char*)
"__main__");
570 PyObject *pyDict = PyModule_GetDict(pyMod);
575 QString varName = QString::fromStdString( stdvarName );
577 command.append( QString(
"%1_numpy_array = sitk.GetArrayFromImage(%1)\n").arg(varName) );
578 command.append( QString(
"%1_spacing = numpy.asarray(%1.GetSpacing())\n").arg(varName) );
579 command.append( QString(
"%1_origin = numpy.asarray(%1.GetOrigin())\n").arg(varName) );
580 command.append( QString(
"%1_dtype = %1_numpy_array.dtype.name\n").arg(varName) );
581 command.append( QString(
"%1_direction = numpy.asarray(%1.GetDirection())\n").arg(varName) );
582 command.append( QString(
"%1_nrComponents = numpy.asarray(%1.GetNumberOfComponentsPerPixel())\n").arg(varName));
583 command.append( QString(
"%1_dtype = %1_numpy_array.dtype.name\n").arg(varName) );
586 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
589 PyObject* py_dtype = PyDict_GetItemString(pyDict,QString(
"%1_dtype").arg(varName).toStdString().c_str() );
590 std::string dtype = PyString_AsString(py_dtype);
591 PyArrayObject* py_data = (PyArrayObject*) PyDict_GetItemString(pyDict,QString(
"%1_numpy_array").arg(varName).toStdString().c_str() );
592 PyArrayObject* py_spacing = (PyArrayObject*) PyDict_GetItemString(pyDict,QString(
"%1_spacing").arg(varName).toStdString().c_str() );
593 PyArrayObject* py_origin = (PyArrayObject*) PyDict_GetItemString(pyDict,QString(
"%1_origin").arg(varName).toStdString().c_str() );
594 PyArrayObject* py_direction = (PyArrayObject*) PyDict_GetItemString(pyDict,QString(
"%1_direction").arg(varName).toStdString().c_str() );
596 PyArrayObject* py_nrComponents = (PyArrayObject*) PyDict_GetItemString(pyDict,QString(
"%1_nrComponents").arg(varName).toStdString().c_str() );
598 unsigned int nr_Components = *((
unsigned int*) py_nrComponents->data);
600 unsigned int nr_dimensions = py_data->nd;
601 if (nr_Components > 1)
608 unsigned int* dimensions =
new unsigned int[nr_dimensions];
610 for(
unsigned i = 0; i < nr_dimensions; ++i )
612 dimensions[i] = py_data->dimensions[nr_dimensions - 1 - i];
615 mitkImage->Initialize(pixelType, nr_dimensions, dimensions);
618 mitkImage->SetChannel(py_data->data);
621 ds = (
double*)py_spacing->data;
626 mitkImage->GetGeometry()->SetSpacing(spacing);
629 ds = (
double*)py_origin->data;
633 mitkImage->GetGeometry()->SetOrigin(origin);
636 itk::Matrix<double,3,3> py_transform;
638 ds = (
double*)py_direction->data;
639 py_transform[0][0] = ds[0];
640 py_transform[0][1] = ds[1];
641 py_transform[0][2] = ds[2];
643 py_transform[1][0] = ds[3];
644 py_transform[1][1] = ds[4];
645 py_transform[1][2] = ds[5];
647 py_transform[2][0] = ds[6];
648 py_transform[2][1] = ds[7];
649 py_transform[2][2] = ds[8];
653 itk::Matrix<double,3,3> transform = py_transform * affineTransform->GetMatrix();
655 affineTransform->SetMatrix(transform);
657 mitkImage->GetGeometry()->SetIndexToWorldTransform(affineTransform);
664 command.append( QString(
"del %1_numpy_array\n").arg(varName) );
665 command.append( QString(
"del %1_dtype\n").arg(varName) );
666 command.append( QString(
"del %1_spacing\n").arg(varName) );
667 command.append( QString(
"del %1_origin\n").arg(varName) );
668 command.append( QString(
"del %1_direction\n").arg(varName) );
669 command.append( QString(
"del %1_nrComponents\n").arg(varName) );
670 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
681 QString varName = QString::fromStdString( stdvarName );
687 PyObject *pyMod = PyImport_AddModule((
char*)
"__main__");
689 PyObject *pyDict = PyModule_GetDict(pyMod);
691 PyObject* npyArray = NULL;
693 void* array = (
void*) racc.
GetData();
696 npy_intp* npy_dims =
new npy_intp[1];
697 npy_dims[0] = imgDim[0];
704 QString dimensionString;
705 dimensionString.append(QString(
"["));
706 dimensionString.append(QString::number(imgDim[0]));
708 for (
unsigned i = 1; i < 3; ++i)
712 dimensionString.append(QString(
","));
713 dimensionString.append(QString::number(imgDim[i]));
714 npy_dims[0] *= imgDim[i];
716 dimensionString.append(
"]");
723 NPY_TYPES npy_type = NPY_USHORT;
725 npy_type = NPY_DOUBLE;
727 npy_type = NPY_FLOAT;
729 npy_type = NPY_SHORT;
737 npy_type = NPY_UBYTE;
743 npy_type = NPY_USHORT;
746 MITK_WARN <<
"not a recognized pixeltype";
751 import_array1 (
true);
752 npyArray = PyArray_SimpleNewFromData(npy_nd,npy_dims,npy_type,array);
755 const int status = PyDict_SetItemString( pyDict,QString(
"%1_numpy_array")
756 .arg(varName).toStdString().c_str(),
762 command.append( QString(
"import numpy as np\n"));
765 command.append( QString(
"%1_array_tmp=%1_numpy_array.copy()\n").arg(varName));
766 command.append( QString(
"%1_array_tmp=%1_array_tmp.reshape(%2,%3,%4)\n").arg( varName,
767 QString::number(imgDim[1]),
768 QString::number(imgDim[0]),
771 command.append( QString(
"%1 = %1_array_tmp[:,...,::-1]\n").arg(varName));
772 command.append( QString(
"del %1_numpy_array\n").arg(varName) );
773 command.append( QString(
"del %1_array_tmp").arg(varName) );
775 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
787 PyObject *pyMod = PyImport_AddModule((
char*)
"__main__");
789 PyObject *pyDict = PyModule_GetDict(pyMod);
792 QString varName = QString::fromStdString( stdvarName );
794 command.append( QString(
"import numpy as np\n"));
795 command.append( QString(
"%1_dtype=%1.dtype.name\n").arg(varName) );
796 command.append( QString(
"%1_shape=np.asarray(%1.shape)\n").arg(varName) );
797 command.append( QString(
"%1_np_array=%1[:,...,::-1]\n").arg(varName));
798 command.append( QString(
"%1_np_array=np.reshape(%1_np_array,%1.shape[0] * %1.shape[1] * %1.shape[2])").arg(varName) );
800 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
803 PyObject* py_dtype = PyDict_GetItemString(pyDict,QString(
"%1_dtype").arg(varName).toStdString().c_str() );
804 std::string dtype = PyString_AsString(py_dtype);
805 PyArrayObject* py_data = (PyArrayObject*) PyDict_GetItemString(pyDict,QString(
"%1_np_array").arg(varName).toStdString().c_str() );
806 PyArrayObject* shape = (PyArrayObject*) PyDict_GetItemString(pyDict,QString(
"%1_shape").arg(varName).toStdString().c_str() );
808 size_t* d = (
size_t*)shape->data;
810 unsigned int dimensions[3];
811 dimensions[0] = d[1];
812 dimensions[1] = d[0];
813 dimensions[2] = d[2];
815 unsigned int nr_dimensions = 2;
818 unsigned int nr_Components = (
unsigned int) d[2];
822 mitkImage->Initialize(pixelType, nr_dimensions, dimensions);
827 char* data = (
char*)(ra.
GetData());
828 memcpy(data, (
void*)py_data->data, dimensions[0] * dimensions[1] * pixelType.
GetSize());
833 command.append( QString(
"del %1_shape\n").arg(varName) );
834 command.append( QString(
"del %1_dtype\n").arg(varName) );
835 command.append( QString(
"del %1_np_array").arg(varName));
837 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
845 return &m_PythonManager;
851 PyObject *pyMod = PyImport_AddModule((
char*)
"__main__");
853 PyObject *pyDict = PyModule_GetDict(pyMod);
855 PyObject *pyAddr = NULL;
860 QString varName = QString::fromStdString( stdvarName );
862 command.append( QString(
"%1_addr_str = %1.GetAddressAsString(\"vtkPolyData\")\n").arg(varName) );
864 command.append( QString(
"%1_addr = int(%1_addr_str[5:],16)").arg(varName) );
866 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
870 pyAddr = PyDict_GetItemString(pyDict,QString(
"%1_addr").arg(varName).toStdString().c_str());
873 addr = PyInt_AsLong(pyAddr);
875 MITK_DEBUG <<
"Python object address: " << addr;
878 vtkPolyData* poly = (vtkPolyData*)((
void*)addr);
879 surface->SetVtkPolyData(poly);
883 command.append( QString(
"del %1_addr_str\n").arg(varName) );
884 command.append( QString(
"del %1_addr").arg(varName) );
886 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
894 QString varName = QString::fromStdString( stdvarName );
895 std::ostringstream oss;
896 std::string addr =
"";
906 address = QString::fromStdString(addr.substr(2));
908 command.append( QString(
"%1 = vtk.vtkPolyData(\"%2\")\n").arg(varName).arg(address) );
910 MITK_DEBUG(
"PythonService") <<
"Issuing python command " << command.toStdString();
921 m_ItkWrappingAvailable = !this->PythonErrorOccured();
926 if ( this->PythonErrorOccured() )
929 m_ItkWrappingAvailable = !this->PythonErrorOccured();
931 return m_ItkWrappingAvailable;
937 m_OpenCVWrappingAvailable = !this->PythonErrorOccured();
939 return m_OpenCVWrappingAvailable;
946 m_VtkWrappingAvailable = !this->PythonErrorOccured();
948 return m_VtkWrappingAvailable;
953 return m_ErrorOccured;
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
Class for storing surfaces (vtkPolyData).
bool DoesVariableExist(const std::string &name) const
itk::SmartPointer< Self > Pointer
bool CopyToPythonAsVtkPolyData(mitk::Surface *surface, const std::string &varName)
virtual vtkPolyData * GetVtkPolyData(unsigned int t=0) const
ctkAbstractPythonManager * GetPythonManager()
mitk::Surface::Pointer CopyVtkPolyDataFromPython(const std::string &varName)
const void * GetData() const
Gives const access to the data.
bool IsOpenCvPythonWrappingAvailable()
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
itk::ImageIOBase::IOPixelType GetPixelType() const
void * GetData()
Gives full data access.
static const int MULTI_LINE_COMMAND
void RemovePythonCommandObserver(PythonCommandObserver *observer)
bool IsSimpleItkPythonWrappingAvailable()
void NotifyObserver(const std::string &command)
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
bool PythonErrorOccured() const
vcl_size_t GetSize() const
Get size of the PixelType in bytes.
std::string Execute(const std::string &pythonCommand, int commandType=SINGLE_LINE_COMMAND)
unsigned int * GetDimensions() const
Get the sizes of all dimensions as an integer-array.
std::vector< PythonVariable > GetVariableStack() const
static const int SINGLE_LINE_COMMAND
bool CopyToPythonAsSimpleItkImage(mitk::Image *image, const std::string &varName)
Image class for storing images.
~PythonService()
empty implementation...
mitk::Image::Pointer CopyCvImageFromPython(const std::string &varName)
bool CopyToPythonAsCvImage(mitk::Image *image, const std::string &varName)
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
QString GetTempDataFileName(const std::string &ext) const
bool IsVtkPythonWrappingAvailable()
mitk::PixelType DeterminePixelType(const std::string &pythonPixeltype, int nrComponents, int dimensions)
static std::string GetProgramPath()
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
ImageWriteAccessor class to get locked write-access for a particular image part.
static const int EVAL_COMMAND
ImageReadAccessor class to get locked read access for a particular image part.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
void ExecuteScript(const std::string &pathToPythonScript)
mitk::Image::Pointer CopySimpleItkImageFromPython(const std::string &varName)
PythonService()
instantiate python manager here
Class for defining the data type of pixels.
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
void AddPythonCommandObserver(PythonCommandObserver *observer)