Skip to content

Commit 07d214a

Browse files
krupalbhathjmjohnson
authored andcommitted
STYLE: Centralize exception handling in Statistics examples
Refactored the Statistics examples by moving core logic into examplemain() function, try/catch handling is now done only in main(), which calls examplemain(). Original try blocks within the examples were removed.
1 parent 7b06022 commit 07d214a

11 files changed

+835
-655
lines changed

Examples/Statistics/BayesianClassifier.cxx

Lines changed: 79 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
#include "itkGradientAnisotropicDiffusionImageFilter.h"
5555
#include "itkRescaleIntensityImageFilter.h"
5656

57+
namespace
58+
{
5759
int
58-
main(int argc, char * argv[])
60+
ExampleMain(int argc, const char * const argv[])
5961
{
6062

6163
if (argc < 3)
@@ -67,70 +69,85 @@ main(int argc, char * argv[])
6769
return EXIT_FAILURE;
6870
}
6971

72+
constexpr unsigned int Dimension = 2;
73+
using InputPixelType = float;
74+
using InputImageType = itk::VectorImage<InputPixelType, Dimension>;
75+
76+
auto input = itk::ReadImage<InputImageType>(argv[1]);
77+
78+
using LabelType = unsigned char;
79+
using PriorType = float;
80+
using PosteriorType = float;
81+
82+
using ClassifierFilterType =
83+
itk::BayesianClassifierImageFilter<InputImageType,
84+
LabelType,
85+
PosteriorType,
86+
PriorType>;
87+
88+
auto filter = ClassifierFilterType::New();
89+
90+
filter->SetInput(input);
91+
92+
if (argc > 3)
93+
{
94+
filter->SetNumberOfSmoothingIterations(std::stoi(argv[3]));
95+
using ExtractedComponentImageType =
96+
ClassifierFilterType::ExtractedComponentImageType;
97+
using SmoothingFilterType = itk::GradientAnisotropicDiffusionImageFilter<
98+
ExtractedComponentImageType,
99+
ExtractedComponentImageType>;
100+
auto smoother = SmoothingFilterType::New();
101+
smoother->SetNumberOfIterations(1);
102+
smoother->SetTimeStep(0.125);
103+
smoother->SetConductanceParameter(3);
104+
filter->SetSmoothingFilter(smoother);
105+
}
106+
107+
// SET FILTER'S PRIOR PARAMETERS
108+
// do nothing here to default to uniform priors
109+
// otherwise set the priors to some user provided values
110+
111+
// Rescale the label map to the dynamic range of the datatype and write it
112+
using ClassifierOutputImageType = ClassifierFilterType::OutputImageType;
113+
using OutputImageType = itk::Image<unsigned char, Dimension>;
114+
using RescalerType =
115+
itk::RescaleIntensityImageFilter<ClassifierOutputImageType,
116+
OutputImageType>;
117+
auto rescaler = RescalerType::New();
118+
rescaler->SetInput(filter->GetOutput());
119+
rescaler->SetOutputMinimum(0);
120+
rescaler->SetOutputMaximum(255);
121+
122+
// Write labelmap to file
123+
itk::WriteImage(rescaler->GetOutput(), argv[2]);
124+
125+
// Testing print
126+
filter->Print(std::cout);
127+
std::cout << "Test passed." << std::endl;
128+
129+
return EXIT_SUCCESS;
130+
}
131+
} // namespace
132+
133+
int
134+
main(int argc, char * argv[])
135+
{
70136
try
71137
{
72-
constexpr unsigned int Dimension = 2;
73-
using InputPixelType = float;
74-
using InputImageType = itk::VectorImage<InputPixelType, Dimension>;
75-
76-
auto input = itk::ReadImage<InputImageType>(argv[1]);
77-
78-
using LabelType = unsigned char;
79-
using PriorType = float;
80-
using PosteriorType = float;
81-
82-
using ClassifierFilterType =
83-
itk::BayesianClassifierImageFilter<InputImageType,
84-
LabelType,
85-
PosteriorType,
86-
PriorType>;
87-
88-
auto filter = ClassifierFilterType::New();
89-
90-
filter->SetInput(input);
91-
92-
if (argc > 3)
93-
{
94-
filter->SetNumberOfSmoothingIterations(std::stoi(argv[3]));
95-
using ExtractedComponentImageType =
96-
ClassifierFilterType::ExtractedComponentImageType;
97-
using SmoothingFilterType =
98-
itk::GradientAnisotropicDiffusionImageFilter<
99-
ExtractedComponentImageType,
100-
ExtractedComponentImageType>;
101-
auto smoother = SmoothingFilterType::New();
102-
smoother->SetNumberOfIterations(1);
103-
smoother->SetTimeStep(0.125);
104-
smoother->SetConductanceParameter(3);
105-
filter->SetSmoothingFilter(smoother);
106-
}
107-
108-
// SET FILTER'S PRIOR PARAMETERS
109-
// do nothing here to default to uniform priors
110-
// otherwise set the priors to some user provided values
111-
112-
// Rescale the label map to the dynamic range of the datatype and write it
113-
using ClassifierOutputImageType = ClassifierFilterType::OutputImageType;
114-
using OutputImageType = itk::Image<unsigned char, Dimension>;
115-
using RescalerType =
116-
itk::RescaleIntensityImageFilter<ClassifierOutputImageType,
117-
OutputImageType>;
118-
auto rescaler = RescalerType::New();
119-
rescaler->SetInput(filter->GetOutput());
120-
rescaler->SetOutputMinimum(0);
121-
rescaler->SetOutputMaximum(255);
122-
123-
// Write labelmap to file
124-
itk::WriteImage(rescaler->GetOutput(), argv[2]);
125-
126-
// Testing print
127-
filter->Print(std::cout);
128-
std::cout << "Test passed." << std::endl;
138+
return ExampleMain(argc, argv);
129139
}
130-
catch (const itk::ExceptionObject & excp)
140+
catch (const itk::ExceptionObject & exceptionObject)
131141
{
132-
std::cerr << "ITK exception caught:\n" << excp << std::endl;
133-
return EXIT_FAILURE;
142+
std::cerr << "ITK exception caught:\n" << exceptionObject << '\n';
134143
}
135-
return EXIT_SUCCESS;
144+
catch (const std::exception & stdException)
145+
{
146+
std::cerr << "std exception caught:\n" << stdException.what() << '\n';
147+
}
148+
catch (...)
149+
{
150+
std::cerr << "Unhandled exception!\n";
151+
}
152+
return EXIT_FAILURE;
136153
}

Examples/Statistics/BayesianClassifierInitializer.cxx

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@
5858
#include "itkImageFileWriter.h"
5959
#include "itkRescaleIntensityImageFilter.h"
6060
#include "itkImageRegionConstIterator.h"
61-
61+
namespace
62+
{
6263
int
63-
main(int argc, char * argv[])
64+
ExampleMain(int argc, const char * const argv[])
6465
{
6566

6667
constexpr unsigned int Dimension = 2;
@@ -80,74 +81,90 @@ main(int argc, char * argv[])
8081
return EXIT_FAILURE;
8182
}
8283

83-
try
84-
{
85-
using ImageType = itk::Image<unsigned char, Dimension>;
86-
using BayesianInitializerType =
87-
itk::BayesianClassifierInitializationImageFilter<ImageType>;
88-
auto bayesianInitializer = BayesianInitializerType::New();
8984

90-
auto input = itk::ReadImage<ImageType>(argv[1]);
85+
using ImageType = itk::Image<unsigned char, Dimension>;
86+
using BayesianInitializerType =
87+
itk::BayesianClassifierInitializationImageFilter<ImageType>;
88+
auto bayesianInitializer = BayesianInitializerType::New();
9189

92-
bayesianInitializer->SetInput(input);
93-
bayesianInitializer->SetNumberOfClasses(std::stoi(argv[3]));
90+
auto input = itk::ReadImage<ImageType>(argv[1]);
9491

95-
// TODO add test where we specify membership functions
92+
bayesianInitializer->SetInput(input);
93+
bayesianInitializer->SetNumberOfClasses(std::stoi(argv[3]));
9694

97-
bayesianInitializer->Update();
95+
// TODO add test where we specify membership functions
9896

99-
itk::WriteImage(bayesianInitializer->GetOutput(), argv[2]);
97+
bayesianInitializer->Update();
10098

101-
if (argv[4] && argv[5])
102-
{
103-
using MembershipImageType = BayesianInitializerType::OutputImageType;
104-
using ExtractedComponentImageType =
105-
itk::Image<MembershipImageType::InternalPixelType, Dimension>;
106-
auto extractedComponentImage = ExtractedComponentImageType::New();
107-
extractedComponentImage->CopyInformation(
108-
bayesianInitializer->GetOutput());
109-
extractedComponentImage->SetBufferedRegion(
110-
bayesianInitializer->GetOutput()->GetBufferedRegion());
111-
extractedComponentImage->SetRequestedRegion(
112-
bayesianInitializer->GetOutput()->GetRequestedRegion());
113-
extractedComponentImage->Allocate();
114-
using ConstIteratorType =
115-
itk::ImageRegionConstIterator<MembershipImageType>;
116-
using IteratorType =
117-
itk::ImageRegionIterator<ExtractedComponentImageType>;
118-
ConstIteratorType cit(
119-
bayesianInitializer->GetOutput(),
120-
bayesianInitializer->GetOutput()->GetBufferedRegion());
121-
IteratorType it(extractedComponentImage,
122-
extractedComponentImage->GetLargestPossibleRegion());
99+
itk::WriteImage(bayesianInitializer->GetOutput(), argv[2]);
123100

124-
const unsigned int componentToExtract = std::stoi(argv[4]);
125-
cit.GoToBegin();
126-
it.GoToBegin();
127-
while (!cit.IsAtEnd())
128-
{
129-
it.Set(cit.Get()[componentToExtract]);
130-
++it;
131-
++cit;
132-
}
101+
if (argv[4] && argv[5])
102+
{
103+
using MembershipImageType = BayesianInitializerType::OutputImageType;
104+
using ExtractedComponentImageType =
105+
itk::Image<MembershipImageType::InternalPixelType, Dimension>;
106+
auto extractedComponentImage = ExtractedComponentImageType::New();
107+
extractedComponentImage->CopyInformation(
108+
bayesianInitializer->GetOutput());
109+
extractedComponentImage->SetBufferedRegion(
110+
bayesianInitializer->GetOutput()->GetBufferedRegion());
111+
extractedComponentImage->SetRequestedRegion(
112+
bayesianInitializer->GetOutput()->GetRequestedRegion());
113+
extractedComponentImage->Allocate();
114+
using ConstIteratorType =
115+
itk::ImageRegionConstIterator<MembershipImageType>;
116+
using IteratorType =
117+
itk::ImageRegionIterator<ExtractedComponentImageType>;
118+
ConstIteratorType cit(
119+
bayesianInitializer->GetOutput(),
120+
bayesianInitializer->GetOutput()->GetBufferedRegion());
121+
IteratorType it(extractedComponentImage,
122+
extractedComponentImage->GetLargestPossibleRegion());
133123

134-
// Write out the rescaled extracted component
135-
using OutputImageType = itk::Image<unsigned char, Dimension>;
136-
using RescalerType =
137-
itk::RescaleIntensityImageFilter<ExtractedComponentImageType,
138-
OutputImageType>;
139-
auto rescaler = RescalerType::New();
140-
rescaler->SetInput(extractedComponentImage);
141-
rescaler->SetOutputMinimum(0);
142-
rescaler->SetOutputMaximum(255);
143-
itk::WriteImage(rescaler->GetOutput(), argv[5]);
124+
const unsigned int componentToExtract = std::stoi(argv[4]);
125+
cit.GoToBegin();
126+
it.GoToBegin();
127+
while (!cit.IsAtEnd())
128+
{
129+
it.Set(cit.Get()[componentToExtract]);
130+
++it;
131+
++cit;
144132
}
145-
}
146-
catch (const itk::ExceptionObject & excp)
147-
{
148-
std::cerr << "ITK exception caught:\n" << excp << std::endl;
149-
return EXIT_FAILURE;
133+
134+
// Write out the rescaled extracted component
135+
using OutputImageType = itk::Image<unsigned char, Dimension>;
136+
using RescalerType =
137+
itk::RescaleIntensityImageFilter<ExtractedComponentImageType,
138+
OutputImageType>;
139+
auto rescaler = RescalerType::New();
140+
rescaler->SetInput(extractedComponentImage);
141+
rescaler->SetOutputMinimum(0);
142+
rescaler->SetOutputMaximum(255);
143+
itk::WriteImage(rescaler->GetOutput(), argv[5]);
150144
}
151145

152146
return EXIT_SUCCESS;
153147
}
148+
} // namespace
149+
150+
int
151+
main(int argc, char * argv[])
152+
{
153+
try
154+
{
155+
return ExampleMain(argc, argv);
156+
}
157+
catch (const itk::ExceptionObject & exceptionObject)
158+
{
159+
std::cerr << "ITK exception caught:\n" << exceptionObject << '\n';
160+
}
161+
catch (const std::exception & stdException)
162+
{
163+
std::cerr << "std exception caught:\n" << stdException.what() << '\n';
164+
}
165+
catch (...)
166+
{
167+
std::cerr << "Unhandled exception!\n";
168+
}
169+
return EXIT_FAILURE;
170+
}

Examples/Statistics/ImageEntropy1.cxx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@
5757
// Software Guide : EndCodeSnippet
5858

5959
#include "itkImageFileReader.h"
60-
60+
namespace
61+
{
6162
int
62-
main(int argc, char * argv[])
63+
ExampleMain(int argc, const char * const argv[])
6364
{
6465

6566
if (argc < 3)
@@ -85,17 +86,7 @@ main(int argc, char * argv[])
8586
// Software Guide : EndCodeSnippet
8687

8788
ImageType::Pointer input;
88-
try
89-
{
90-
input = itk::ReadImage<ImageType>(argv[1]);
91-
}
92-
catch (const itk::ExceptionObject & excp)
93-
{
94-
std::cerr << "Problem encountered while reading image file : " << argv[1]
95-
<< std::endl;
96-
std::cerr << excp << std::endl;
97-
return EXIT_FAILURE;
98-
}
89+
input = itk::ReadImage<ImageType>(argv[1]);
9990

10091
// Software Guide : BeginLatex
10192
//
@@ -274,3 +265,26 @@ main(int argc, char * argv[])
274265

275266
return EXIT_SUCCESS;
276267
}
268+
} // namespace
269+
270+
int
271+
main(int argc, char * argv[])
272+
{
273+
try
274+
{
275+
return ExampleMain(argc, argv);
276+
}
277+
catch (const itk::ExceptionObject & exceptionObject)
278+
{
279+
std::cerr << "ITK exception caught:\n" << exceptionObject << '\n';
280+
}
281+
catch (const std::exception & stdException)
282+
{
283+
std::cerr << "std exception caught:\n" << stdException.what() << '\n';
284+
}
285+
catch (...)
286+
{
287+
std::cerr << "Unhandled exception!\n";
288+
}
289+
return EXIT_FAILURE;
290+
}

0 commit comments

Comments
 (0)