35 if (collection->HasElement(origin))
41 if (!collection->HasElement(target) && itkOriginImage.IsNotNull())
44 image->SetRegions(itkOriginImage->GetLargestPossibleRegion());
45 image->SetSpacing(itkOriginImage->GetSpacing());
46 image->SetOrigin(itkOriginImage->GetOrigin());
47 image->SetDirection(itkOriginImage->GetDirection());
50 collection->AddData(dynamic_cast<itk::DataObject *>(image.GetPointer()), target,
"");
53 for (std::size_t i = 0; i < collection->Size(); ++i)
64 : m_TargetID(
"TARGET"),
66 m_MaskID(
"BRAINMASK"),
69 m_WeightSamples(false)
76 MITK_INFO <<
"LearnProgressionFeatures: Selecting training voxels.";
81 MITK_INFO <<
" Selection Mode " << mode <<
" use all tumor voxels, healthy: 50% vicinity / 50% far away";
93 unsigned int totalTumor = 0;
94 unsigned int totalHealthyClose = 0;
95 unsigned int totalHealthy = 0;
97 while (!brainMaskIter.
IsAtEnd())
120 unsigned int targetHealthy = totalTumor * m_ClassRatio;
125 ScalarType thHealthyClose =
std::min(1.0, (targetHealthy * ratioClose) / totalHealthyClose);
126 ScalarType thHealthyAny =
std::min(1.0, (targetHealthy * (1.0 - ratioClose)) / totalHealthy);
129 MITK_INFO <<
"Total # Tumor Voxels " << totalTumor;
130 MITK_INFO <<
"Total # Healthy Voxels" << totalHealthy;
131 MITK_INFO <<
"Target Ratio " << m_ClassRatio;
132 MITK_INFO <<
"Ratio for healthy close: " << thHealthyClose;
133 MITK_INFO <<
"Ratio for healthy any other: " << thHealthyAny;
137 unsigned int tumor = 0;
138 unsigned int healthy = 0;
139 while (!brainMaskIter.
IsAtEnd())
155 ScalarType rndVal = (float)rand() / (float)(RAND_MAX);
156 if (gtvIter.
GetVoxel() == 0 && ((targetDil.
GetVoxel() == 1 && rndVal <= thHealthyClose)))
161 else if (((targetDil.
GetVoxel() == 0 && rndVal <= thHealthyAny)))
178 MITK_INFO <<
"Training with Samples #Tumor " << tumor <<
" / healthy # " << healthy;
183 MITK_INFO <<
" Selection Mode " << mode <<
" Weighted with ratio";
201 double totalTumor = 0;
202 double totalHealthyClose = 0;
203 double totalHealthyNonClose = 0;
204 double totalHealthy = 0;
206 while (!brainMaskIter.
IsAtEnd())
217 ++totalHealthyNonClose;
226 ++excludeHealthyIter;
237 ScalarType wHealthyClose = 10000.0 / totalHealthyClose;
238 ScalarType wHealthyAny = 5000.0 / totalHealthyNonClose;
239 ScalarType wTumor = (m_ClassRatio * (10000.0 + 5000.0)) / totalTumor;
242 double potentialClose = 0;
243 double selectedClose = 0;
246 while (!brainMaskIter.
IsAtEnd())
270 weightsIter.
SetVoxel(wHealthyClose);
271 healthy += wHealthyClose;
273 else if (((targetDil.
GetVoxel() == 0 && excludeHealthyIter.
GetVoxel() == 0)))
277 healthy += wHealthyAny;
304 ++excludeHealthyIter;
308 MITK_INFO <<
"Training with Samples #Tumor " << tumor <<
" / healthy # " << healthy;
309 MITK_INFO <<
"Potential Close" << potentialClose;
310 MITK_INFO <<
"Selected Close" << selectedClose;
316 <<
" Exclude voxels in border regions, healthy: 50% vicinity / 50% far away";
339 double totalTumor = 0;
340 double totalHealthyClose = 0;
341 double totalHealthyNonClose = 0;
342 double totalHealthy = 0;
344 while (!brainMaskIter.
IsAtEnd())
355 ++totalHealthyNonClose;
364 ++excludeHealthyIter;
375 unsigned int targetHealthy = (tumorWeight / healthyTissue) * totalTumor * m_ClassRatio;
380 ScalarType thHealthyClose =
std::min(1.0, ((
double)targetHealthy * ratioClose) / totalHealthyClose);
381 ScalarType thHealthyAny =
std::min(1.0, ((
double)targetHealthy * (1.0 - ratioClose)) / totalHealthyNonClose);
384 MITK_INFO <<
"Total Tumor " << totalTumor;
385 MITK_INFO <<
"Total healthy " << totalHealthyNonClose;
386 MITK_INFO <<
"Total healthy close " << totalHealthyClose;
387 MITK_INFO <<
"Total healthy non-close " << totalHealthyNonClose;
388 MITK_INFO <<
"Target Ratio " << m_ClassRatio;
390 MITK_INFO <<
"Target Healthy " << targetHealthy;
391 MITK_INFO <<
"Probabilty close " << thHealthyClose;
392 MITK_INFO <<
"Probabilty any other " << thHealthyAny;
396 double potentialClose = 0;
397 double selectedClose = 0;
400 while (!brainMaskIter.
IsAtEnd())
423 if (!m_WeightSamples && ((
float)rand() / (float)(RAND_MAX) < thHealthyClose))
432 weightsIter.
SetVoxel(thHealthyClose);
433 healthy += thHealthyClose;
436 else if (((targetDil.
GetVoxel() == 0 && excludeHealthyIter.
GetVoxel() == 0)))
438 if (!m_WeightSamples && ((
float)rand() / (float)(RAND_MAX) < thHealthyAny))
448 healthy += thHealthyAny;
466 if ((
float)rand() / (float)(RAND_MAX) < thHealthyClose)
484 ++excludeHealthyIter;
488 MITK_INFO <<
"Training with Samples #Tumor " << tumor <<
" / healthy # " << healthy;
489 MITK_INFO <<
"Potential Close" << potentialClose;
490 MITK_INFO <<
"Selected Close" << selectedClose;
499 MITK_INFO <<
"PrepareResponseSamples: Selecting training voxels.";
511 double nonInvolved = 0;
512 double responsive = 0;
515 while (!brainMaskIter.
IsAtEnd())
552 double wNonInvolved = 10000.0 / nonInvolved;
553 double wResponsive = 10000.0 / responsive;
554 double wRezidiv = 10000.0 / rezidiv;
556 std::cout <<
"Weights are " << wNonInvolved <<
"/ " << wResponsive <<
" / " << wRezidiv << std::endl;
561 while (!brainMaskIter.
IsAtEnd())
583 std::vector<std::string> modalitiesList,
591 m_Forest.SetMaximumTreeDepth(treeDepth);
593 m_Forest.SetTreeCount(forestSize);
598 m_Forest.SetPointWiseWeight(trainDataW);
599 m_Forest.UsePointWiseWeight(
true);
605 m_Forest.Train(trainDataX, trainDataY);
607 catch (std::exception &e)
609 MITK_INFO <<
"Exception while training forest: " << e.what();
611 std::cout <<
"Training finished" << std::endl;
615 std::vector<std::string> modalitiesList)
617 if (collection != NULL)
622 auto testDataNewY = m_Forest.Predict(testDataX);
625 Eigen::MatrixXd Probs = m_Forest.GetPointWiseProbabilities();
627 Eigen::MatrixXd prob0 = Probs.col(0);
628 Eigen::MatrixXd prob1 = Probs.col(1);
632 if (Probs.cols() >= 3)
634 Eigen::MatrixXd prob2 = Probs.col(2);
639 MITK_ERROR <<
"TumorInvasionClassification::PredictInvasion - provided collection is NULL.";
649 std::ofstream fileStream;
650 fileStream.open(outputFile.c_str(), std::ios_base::app);
652 fileStream <<
"Configuration:" << std::endl;
653 for (
size_t patient = 0; patient < collection->
Size(); ++patient)
656 fileStream <<
"Name : " << dataPatient->
GetName() <<
" with time steps:" << std::endl;
657 for (
size_t timeStep = 0; timeStep < dataPatient->
Size(); ++timeStep)
659 fileStream << dataPatient->
IndexToName(timeStep) <<
"-" << std::endl;
661 for (
size_t ts = 0; ts < dataTS->
Size(); ++ts)
662 fileStream << dataTS->
IndexToName(ts) <<
"-" << std::endl;
TumorInvasionClassification()
static void ErodeBinaryByName(DataCollection *dataCollection, std::string name, unsigned int xy=5, unsigned int z=0, std::string suffix="ERODE")
ErodeBinaryByName - Erode all occurances of a modality within a mitk::DataCollection.
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
itk::Image< mitk::ScalarType, 3 > FeatureImage
itk::SmartPointer< Self > Pointer
void SaveRandomForest(std::string filename)
SaveRandomForest - Saves a trained random forest.
void SetVoxel(TDataType value)
vcl_size_t Size() const
Size - number of data items in collection.
std::string GetName() const
void DescriptionToLogFile(DataCollection *collection, std::string outputFile)
DescriptionToLogFile - Append to log file: patient ids and time steps contained in this data collecti...
void SelectTrainingSamples(DataCollection *collection, unsigned int mode=0)
SelectTrainingSamples.
void PrepareResponseSamples(DataCollection *collection)
PrepareResponseSamples.
void PredictInvasion(DataCollection *collection, std::vector< std::string > modalitiesList)
PredictGrowth - Classify voxels into remaining healthy / turning into tumor.
std::string IndexToName(vcl_size_t index) const
IndexToName - Get name from index.
static void PerformGrayOpening(mitk::DataCollection *dataCollection, std::string name, std::string suffix)
PerformGrayOpening - Opening operation on a specific modality type wihtin the DataCollection. Creates a new item.
static void MatrixToDC3d(const Eigen::MatrixXd &matrix, mitk::DataCollection::Pointer dc, const std::vector< std::string > &names, std::string mask)
static const std::string filename
static Eigen::MatrixXi DC3dDToMatrixXi(mitk::DataCollection::Pointer dc, std::string name, std::string mask)
Image class for storing images.
static Eigen::MatrixXd DC3dDToMatrixXd(mitk::DataCollection::Pointer dc, std::string names, std::string mask)
void LearnProgressionFeatures(DataCollection *collection, std::vector< std::string > modalitiesList, vcl_size_t forestSize=300, vcl_size_t treeDepth=10)
LearnProgressionFeatures.
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static void EnsureDataImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.
void SanitizeResults(DataCollection *collection, std::string resultID="RESULT")
SanitizeResults - Performs an Opening Operation on tha data to remove isolated misclassifications.
void LoadRandomForest(std::string)
LoadRandomForest - loads a random forest to be used for classification.
static void DilateBinaryByName(DataCollection *dataCollection, std::string name, unsigned int xy=5, unsigned int z=0, std::string suffix="DILATE")
DilateBinaryByName - Dilate all occurances of a modality within a mitk::DataCollection.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.