Skip to content

Commit

Permalink
Merge pull request #381 from fedorov/improve-error-handling
Browse files Browse the repository at this point in the history
ENH: improve error handling
  • Loading branch information
fedorov authored May 2, 2019
2 parents e074b95 + 2c9740f commit c3b40ac
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 59 deletions.
24 changes: 15 additions & 9 deletions apps/paramaps/itkimage2paramap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,29 @@ int main(int argc, char *argv[])
vector<DcmDataset*> dcmDatasets = helper::loadDatasets(dicomImageFileList);

if(dcmDatasets.empty()){
cerr << "Error: no DICOM could be loaded from the specified list/directory" << endl;
cerr << "ERROR: no DICOM could be loaded from the specified list/directory" << endl;
return EXIT_FAILURE;
}

ifstream metainfoStream(metaDataFileName.c_str(), ios_base::binary);
std::string metadata( (std::istreambuf_iterator<char>(metainfoStream) ),
(std::istreambuf_iterator<char>()));

DcmDataset* result = dcmqi::ParaMapConverter::itkimage2paramap(parametricMapImage, dcmDatasets, metadata);
try {
DcmDataset* result = dcmqi::ParaMapConverter::itkimage2paramap(parametricMapImage, dcmDatasets, metadata);

if (result == NULL) {
return EXIT_FAILURE;
} else {
DcmFileFormat segdocFF(result);
CHECK_COND(segdocFF.saveFile(outputParaMapFileName.c_str(), EXS_LittleEndianExplicit));
if (result == NULL) {
std::cerr << "ERROR: Conversion failed." << std::endl;
return EXIT_FAILURE;
} else {
DcmFileFormat segdocFF(result);
CHECK_COND(segdocFF.saveFile(outputParaMapFileName.c_str(), EXS_LittleEndianExplicit));

COUT << "Saved parametric map as " << outputParaMapFileName << endl;
return EXIT_SUCCESS;
COUT << "Saved parametric map as " << outputParaMapFileName << endl;
return EXIT_SUCCESS;
}
} catch (int e) {
std::cerr << "Fatal error encountered." << std::endl;
return EXIT_FAILURE;
}
}
51 changes: 28 additions & 23 deletions apps/paramaps/paramap2itkimage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,32 @@ int main(int argc, char *argv[])
CHECK_COND(sliceFF.loadFile(inputFileName.c_str()));
DcmDataset* dataset = sliceFF.getDataset();

pair <FloatImageType::Pointer, string> result = dcmqi::ParaMapConverter::paramap2itkimage(dataset);

string fileExtension = helper::getFileExtensionFromType(outputType);

typedef itk::ImageFileWriter<FloatImageType> WriterType;
string outputPrefix = prefix.empty() ? "" : prefix + "-";
WriterType::Pointer writer = WriterType::New();
stringstream imageFileNameSStream;
imageFileNameSStream << outputDirName << "/" << outputPrefix << "pmap" << fileExtension;
writer->SetFileName(imageFileNameSStream.str().c_str());
writer->SetInput(result.first);
writer->SetUseCompression(1);
writer->Update();

stringstream jsonOutput;
jsonOutput << outputDirName << "/" << outputPrefix << "meta.json";

ofstream outputFile;
outputFile.open(jsonOutput.str().c_str());
outputFile << result.second;
outputFile.close();

return EXIT_SUCCESS;
try {
pair <FloatImageType::Pointer, string> result = dcmqi::ParaMapConverter::paramap2itkimage(dataset);

string fileExtension = helper::getFileExtensionFromType(outputType);

typedef itk::ImageFileWriter<FloatImageType> WriterType;
string outputPrefix = prefix.empty() ? "" : prefix + "-";
WriterType::Pointer writer = WriterType::New();
stringstream imageFileNameSStream;
imageFileNameSStream << outputDirName << "/" << outputPrefix << "pmap" << fileExtension;
writer->SetFileName(imageFileNameSStream.str().c_str());
writer->SetInput(result.first);
writer->SetUseCompression(1);
writer->Update();

stringstream jsonOutput;
jsonOutput << outputDirName << "/" << outputPrefix << "meta.json";

ofstream outputFile;
outputFile.open(jsonOutput.str().c_str());
outputFile << result.second;
outputFile.close();

return EXIT_SUCCESS;
} catch (int e) {
std::cerr << "Fatal error encountered." << std::endl;
return EXIT_FAILURE;
}
}
39 changes: 22 additions & 17 deletions apps/seg/itkimage2segimage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,31 @@ int main(int argc, char *argv[])
segmentations = segmentationsReordered;
}

DcmDataset* result = dcmqi::ImageSEGConverter::itkimage2dcmSegmentation(dcmDatasets, segmentations, metadata, skipEmptySlices);
try {
DcmDataset* result = dcmqi::ImageSEGConverter::itkimage2dcmSegmentation(dcmDatasets, segmentations, metadata, skipEmptySlices);

if (result == NULL){
return EXIT_FAILURE;
} else {
DcmFileFormat segdocFF(result);
bool compress = false;
if(compress){
CHECK_COND(segdocFF.saveFile(outputSEGFileName.c_str(), EXS_DeflatedLittleEndianExplicit));
if (result == NULL){
std::cerr << "ERROR: Conversion failed." << std::endl;
return EXIT_FAILURE;
} else {
CHECK_COND(segdocFF.saveFile(outputSEGFileName.c_str(), EXS_LittleEndianExplicit));
}
DcmFileFormat segdocFF(result);
bool compress = false;
if(compress){
CHECK_COND(segdocFF.saveFile(outputSEGFileName.c_str(), EXS_DeflatedLittleEndianExplicit));
} else {
CHECK_COND(segdocFF.saveFile(outputSEGFileName.c_str(), EXS_LittleEndianExplicit));
}

COUT << "Saved segmentation as " << outputSEGFileName << endl;
}
COUT << "Saved segmentation as " << outputSEGFileName << endl;
}

for(size_t i=0;i<dcmDatasets.size();i++) {
delete dcmDatasets[i];
for(size_t i=0;i<dcmDatasets.size();i++) {
delete dcmDatasets[i];
}
if (result != NULL)
delete result;
return EXIT_SUCCESS;
} catch (int e) {
std::cerr << "Fatal error encountered." << std::endl;
}
if (result != NULL)
delete result;
return EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions apps/seg/segimage2itkimage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS;
} catch (int e) {
std::cerr << "Fatal error encountered." << std::endl;
return EXIT_FAILURE;
}
}
5 changes: 4 additions & 1 deletion libsrc/JSONParametricMapMetaInformationHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ namespace dcmqi {
try {
istringstream metainfoStream(this->jsonInput);
metainfoStream >> this->metaInfoRoot;
//std::cout << this->metaInfoRoot.asString() << std::endl;
this->seriesDescription = this->metaInfoRoot.get("SeriesDescription", "Segmentation").asString();
this->seriesNumber = this->metaInfoRoot.get("SeriesNumber", "300").asString();
this->instanceNumber = this->metaInfoRoot.get("InstanceNumber", "1").asString();
Expand Down Expand Up @@ -158,7 +159,9 @@ namespace dcmqi {
}

} catch (exception& e) {
cout << e.what() << endl;
cerr << "ERROR: JSON parameter file could not be parsed!" << std::endl;
cerr << "You can validate the JSON file here: http://qiicr.org/dcmqi/#/validators" << std::endl;
cerr << "Exception details (probably not very useful): " << e.what() << endl;
throw JSONReadErrorException();
}
}
Expand Down
6 changes: 4 additions & 2 deletions libsrc/JSONSegmentationMetaInformationHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ namespace dcmqi {
this->bodyPartExamined = this->metaInfoRoot.get("BodyPartExamined", "").asString();

this->readSegmentAttributes();
} catch (exception &e) {
cout << e.what() << '\n';
} catch (exception& e) {
cerr << "ERROR: JSON parameter file could not be parsed!" << std::endl;
cerr << "You can validate the JSON file here: http://qiicr.org/dcmqi/#/validators" << std::endl;
cerr << "Exception details (probably not very useful): " << e.what() << endl;
throw JSONReadErrorException();
}
}
Expand Down
14 changes: 7 additions & 7 deletions libsrc/ParaMapConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace dcmqi {
bval->getEntireConceptNameCodeSequence().push_back(qCodeName);
bval->getEntireMeasurementUnitsCodeSequence().push_back(bvalUnits);
if(bval->setNumericValue(metaInfo.metaInfoRoot["SourceImageDiffusionBValues"][static_cast<int>(bvalId)].asCString()).bad())
cout << "Failed to insert the value!" << endl;;
cout << "ERROR: Failed to insert the value!" << endl;;
realWorldValueMappingItem->getEntireQuantityDefinitionSequence().push_back(bval);
cout << bval->toString() << endl;
}
Expand Down Expand Up @@ -301,8 +301,8 @@ namespace dcmqi {
metaInfo.getDerivationDescription().c_str(),
derimgItem));
} else {
cerr << "DerivationCode must be specified in the input metadata!" << endl;
return NULL;
cerr << "ERROR: DerivationCode must be specified in the input metadata!" << endl;
throw -1;
}

//cout << "Total of " << siVector.size() << " source image items will be added" << endl;
Expand Down Expand Up @@ -453,7 +453,7 @@ namespace dcmqi {
FGInterface &fgInterface = pMapDoc->getFunctionalGroups();
FloatImageType::DirectionType direction;
if(getImageDirections(fgInterface, direction)){
cerr << "Failed to get image directions" << endl;
cerr << "ERROR: Failed to get image directions" << endl;
throw -1;
}

Expand All @@ -466,14 +466,14 @@ namespace dcmqi {

FloatImageType::PointType imageOrigin;
if(computeVolumeExtent(fgInterface, sliceDirection, imageOrigin, computedSliceSpacing, computedVolumeExtent)){
cerr << "Failed to compute origin and/or slice spacing!" << endl;
cerr << "ERROR: Failed to compute origin and/or slice spacing!" << endl;
throw -1;
}

FloatImageType::SpacingType imageSpacing;
imageSpacing.Fill(0);
if(getDeclaredImageSpacing(fgInterface, imageSpacing)){
cerr << "Failed to get image spacing from DICOM!" << endl;
cerr << "ERROR: Failed to get image spacing from DICOM!" << endl;
throw -1;
}

Expand Down Expand Up @@ -616,7 +616,7 @@ namespace dcmqi {

OFvariant<OFCondition,DPMParametricMapIOD*> result = DPMParametricMapIOD::loadDataset(*pmapDataset);
if (OFCondition* pCondition = OFget<OFCondition>(&result)) {
cerr << "Failed to load parametric map! " << pCondition->text() << endl;
cerr << "ERROR: Failed to load parametric map! " << pCondition->text() << endl;
throw -1;
}
DPMParametricMapIOD* pMapDoc = *OFget<DPMParametricMapIOD*>(&result);
Expand Down

0 comments on commit c3b40ac

Please sign in to comment.