From fdabe8c9ba68e1b6267cccf221bd8e21ea6ab561 Mon Sep 17 00:00:00 2001 From: sagebei Date: Fri, 16 Aug 2024 16:14:12 +0100 Subject: [PATCH] connection_to_ccta --- .../CemrgAppModule/include/CemrgCommandLine.h | 2 + .../CemrgAppModule/src/CemrgCommandLine.cpp | 29 ++++ .../src/internal/AtrialStrainMotionView.cpp | 146 ++++++++++++++++-- .../src/internal/AtrialStrainMotionView.h | 14 +- .../AtrialStrainMotionViewControls.ui | 14 +- 5 files changed, 185 insertions(+), 20 deletions(-) diff --git a/CemrgApp/Modules/CemrgAppModule/include/CemrgCommandLine.h b/CemrgApp/Modules/CemrgAppModule/include/CemrgCommandLine.h index 54c7aee..38f5d5a 100644 --- a/CemrgApp/Modules/CemrgAppModule/include/CemrgCommandLine.h +++ b/CemrgApp/Modules/CemrgAppModule/include/CemrgCommandLine.h @@ -110,6 +110,8 @@ class MITKCEMRGAPPMODULE_EXPORT CemrgCommandLine: public QObject { std::string PrintFullCommand(QString command, QStringList arguments); bool ExecuteCommand(QString executableName, QStringList arguments, QString outputPath, bool isOutputFile = true); + QString DockerCctaMultilabelSegmentation(QString dir, QString path_to_f, bool saveas_nifti); + protected slots: void UpdateStdText(); diff --git a/CemrgApp/Modules/CemrgAppModule/src/CemrgCommandLine.cpp b/CemrgApp/Modules/CemrgAppModule/src/CemrgCommandLine.cpp index 48d5740..87cdde5 100644 --- a/CemrgApp/Modules/CemrgAppModule/src/CemrgCommandLine.cpp +++ b/CemrgApp/Modules/CemrgAppModule/src/CemrgCommandLine.cpp @@ -1413,3 +1413,32 @@ void CemrgCommandLine::FinishedAlert() { QString data = process->program() + " Completed!"; panel->append(data); } + +QString CemrgCommandLine::DockerCctaMultilabelSegmentation(QString dir, QString path_to_f, bool saveas_nifti) { + SetDockerImage("cemrg/ccta:latest"); + QString executablePath = ""; +#if defined(__APPLE__) + executablePath = "/usr/local/bin/"; +#endif + QString executableName = executablePath + "docker"; + QDir home(dir); + QFileInfo fi(path_to_f); + QString outname = fi.baseName(); + outname += "_label_maps.nii.gz"; + QStringList arguments = GetDockerArguments(home.absolutePath()); + arguments << "--filename" << home.relativeFilePath(path_to_f); + arguments << "--device" << "cpu"; + if (saveas_nifti) { + arguments << "--saveas-nifti"; + } + QString outPath = home.absolutePath() + "/" + outname; + bool successful = ExecuteCommand(executableName, arguments, outPath); + QString res=""; + if (successful) { + MITK_INFO << "Successful mutilabel segmentation"; + res = outPath; + } else { + MITK_WARN << "Unsuccessful multilabel segmentation"; + } + return res; +} diff --git a/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.cpp b/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.cpp index c5d2b05..9531eb4 100644 --- a/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.cpp +++ b/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.cpp @@ -41,34 +41,95 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * =========================================================================*/ - +#include "kcl_cemrgapp_atrialstrainmotion_Activator.h" +#include "AtrialStrainMotionView.h" // Blueberry #include #include -// Qmitk -#include "kcl_cemrgapp_atrialstrainmotion_Activator.h" -#include "AtrialStrainMotionView.h" +//Micro services +#include +#ifdef _WIN32 +// _WIN32 = we're in windows +#include +#else +// or linux/mac +#include +#endif + +//VTK +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//ITK +#include +#include +#include +#include +#include "itkLabelObject.h" +#include "itkLabelMap.h" +#include "itkLabelImageToLabelMapFilter.h" +#include "itkLabelMapToLabelImageFilter.h" +#include "itkLabelSelectionLabelMapFilter.h" // Qt #include +#include +#include +#include +#include +#include -// mitk image +// mitk +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//CemrgAppModule +#include +#include +#include +#include + const std::string AtrialStrainMotionView::VIEW_ID = "org.mitk.views.atrialstrainmotionview"; void AtrialStrainMotionView::SetFocus() { - m_Controls.button_step_1->setFocus(); + m_Controls.segment_extract->setFocus(); } void AtrialStrainMotionView::CreateQtPartControl(QWidget *parent) { // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi(parent); - connect(m_Controls.button_step_1, &QPushButton::clicked, this, &AtrialStrainMotionView::DoImageProcessing); + // Step 1: Segment and Extract + connect(m_Controls.segment_extract, SIGNAL(clicked()), this, SLOT(SegmentExtract())); + + + // Set default variables + atrium = std::unique_ptr(new CemrgAtrialTools()); } void AtrialStrainMotionView::OnSelectionChanged(berry::IWorkbenchPart::Pointer /*source*/, @@ -80,13 +141,13 @@ void AtrialStrainMotionView::OnSelectionChanged(berry::IWorkbenchPart::Pointer / if (node.IsNotNull() && dynamic_cast(node->GetData())) { m_Controls.labelWarning->setVisible(false); - m_Controls.button_step_1->setEnabled(true); + m_Controls.segment_extract->setEnabled(true); return; } } m_Controls.labelWarning->setVisible(true); - m_Controls.button_step_1->setEnabled(false); + m_Controls.segment_extract->setEnabled(false); } void AtrialStrainMotionView::DoImageProcessing() @@ -129,3 +190,70 @@ void AtrialStrainMotionView::DoImageProcessing() } } } + +bool AtrialStrainMotionView::RequestProjectDirectoryFromUser() { + + bool succesfulAssignment = true; + + //Ask the user for a dir to store data + if (directory.isEmpty()) { + + MITK_INFO << "Directory is empty. Requesting user for directory."; + directory = QFileDialog::getExistingDirectory( NULL, "Open Project Directory", + mitk::IOUtil::GetProgramPath().c_str(),QFileDialog::ShowDirsOnly|QFileDialog::DontUseNativeDialog); + + MITK_INFO << ("Directory selected:" + directory).toStdString(); + atrium->SetWorkingDirectory(directory); + + if (directory.isEmpty() || directory.simplified().contains(" ")) { + MITK_WARN << "Please select a project directory with no spaces in the path!"; + QMessageBox::warning(NULL, "Attention", "Please select a project directory with no spaces in the path!"); + directory = QString(); + succesfulAssignment = false; + }//_if + + if (succesfulAssignment){ + QString now = QDate::currentDate().toString(Qt::ISODate); + QString logfilename = directory + "/afib_log" + now + ".log"; + std::string logfname_str = logfilename.toStdString(); + + mitk::LoggingBackend::SetLogFile(logfname_str.c_str()); + MITK_INFO << ("Changed logfile location to: " + logfilename).toStdString(); + } + + } else { + MITK_INFO << ("Project directory already set: " + directory).toStdString(); + }//_if + + + return succesfulAssignment; +} + +void AtrialStrainMotionView::SegmentExtract() { + + MITK_INFO << "[AnalysisChoice]"; + bool testRpdfu = RequestProjectDirectoryFromUser(); + MITK_INFO(testRpdfu) << "Should continue"; + if (!RequestProjectDirectoryFromUser()) return; // if the path was chosen incorrectly -> returns. + MITK_INFO << "After directory selection checkup"; + + // TODO: select the first image and segment it + QString nifti_dir = directory + "/nifti/"; + QString input_file = nifti_dir + "dcm-0.nii"; + std::unique_ptr cmd(new CemrgCommandLine()); + + MITK_INFO(QFile::copy(input_file, directory + "/dcm-0.nii")) << "QFile::Copy successful"; + // MITK_INFO << successful; + + // get pathToNifti file! + QFileInfo file(input_file); + + cmd->SetUseDockerContainers(true); + QString pathToSegmentation = cmd->DockerCctaMultilabelSegmentation(directory, directory + "/dcm-0.nii", false); + MITK_INFO << pathToSegmentation; + // TODO: build a docker image for extracting LA + + + + +} \ No newline at end of file diff --git a/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.h b/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.h index 9547586..d5829f6 100644 --- a/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.h +++ b/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionView.h @@ -52,6 +52,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ui_AtrialStrainMotionViewControls.h" + +#include "CemrgAtrialTools.h" /** \brief AtrialStrainMotionView @@ -67,6 +69,8 @@ class AtrialStrainMotionView : public QmitkAbstractView { public: static const std::string VIEW_ID; + bool RequestProjectDirectoryFromUser(); + protected: virtual void CreateQtPartControl(QWidget *parent) override; @@ -77,11 +81,19 @@ class AtrialStrainMotionView : public QmitkAbstractView { virtual void OnSelectionChanged(berry::IWorkbenchPart::Pointer source, const QList &nodes) override; + Ui::AtrialStrainMotionViewControls m_Controls; + /// \brief Called when the user clicks the GUI button + + protected slots: void DoImageProcessing(); + void SegmentExtract(); - Ui::AtrialStrainMotionViewControls m_Controls; + +private: + QString directory; + std::unique_ptr atrium; }; #endif // AtrialStrainMotionView_h diff --git a/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionViewControls.ui b/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionViewControls.ui index ea747f5..8e8279e 100644 --- a/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionViewControls.ui +++ b/CemrgApp/Plugins/kcl.cemrgapp.atrialstrainmotion/src/internal/AtrialStrainMotionViewControls.ui @@ -7,7 +7,7 @@ 0 0 222 - 161 + 555 @@ -26,20 +26,14 @@ QLabel { color: rgb(255, 0, 0) } - Please select an image! + Model Creation Pipeline - - - Do image processing - - - text-align: left; - + - Step 1: Do Something + Segment and Extract