1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19 // Software Guide : BeginLatex
20 //
21 // \index{itk::Statistics::List\-Sample\-To\-Histogram\-Filter}
22 // \index{itk::Statistics::List\-Sample\-To\-Histogram\-Generator}
23 // \index{itk::Statistics::Neighborhood\-Sampler}
24 // \index{itk::Statistics::Sample\-To\-Histogram\-Projection\-Filter}
25 // \index{itk::Statistics::Selective\-Subsample\-Generator}
26 // \index{itk::Statistics::Membership\-Sample\-Generator}
27 //
28 // To use, an \code{MembershipSample} object, we include the header files for
29 // the class itself and a \code{Sample} class. We will use the
30 // \code{ListSample} as the input sample.
31 //
32 // Software Guide : EndLatex
33
34
35 // Software Guide : BeginCodeSnippet
36 #include "itkListSample.h"
37 #include "itkMembershipSample.h"
38 // Software Guide : EndCodeSnippet
39
40 // Software Guide : BeginLatex
41 //
42 // We need another header for measurement vectors. We are going to use
43 // the \doxygen{Vector} class which is a subclass of the \doxygen{FixedArray}
44 // in this example.
45 //
46 // Software Guide : EndLatex
47
48 // Software Guide : BeginCodeSnippet
49 #include "itkVector.h"
50 // Software Guide : EndCodeSnippet
51
main()52 int main()
53 {
54 // Software Guide : BeginLatex
55 //
56 // The following code snippet will create a \code{ListSample} object
57 // with three-component float measurement vectors and put three
58 // measurement vectors in the \code{ListSample} object.
59 //
60 // Software Guide : EndLatex
61
62 // Software Guide : BeginCodeSnippet
63 using MeasurementVectorType = itk::Vector< float, 3 >;
64 using SampleType = itk::Statistics::ListSample< MeasurementVectorType >;
65 SampleType::Pointer sample = SampleType::New();
66 MeasurementVectorType mv;
67
68 mv[0] = 1.0;
69 mv[1] = 2.0;
70 mv[2] = 4.0;
71 sample->PushBack(mv);
72
73 mv[0] = 2.0;
74 mv[1] = 4.0;
75 mv[2] = 5.0;
76 sample->PushBack(mv);
77
78 mv[0] = 3.0;
79 mv[1] = 8.0;
80 mv[2] = 6.0;
81 sample->PushBack(mv);
82 // Software Guide : EndCodeSnippet
83
84 // Software Guide : BeginLatex
85 //
86 // To create a \code{MembershipSample} instance, we define the type of the
87 // \code{MembershipSample} with the source sample type, in this case,
88 // previously defined \code{SampleType}. As usual, after that, we call
89 // \code{New()} method to instantiate an instance. We must plug in the
90 // source sample, \code{sample} object using the
91 // \code{SetSample(source sample)} method. However, in regard to
92 // \textbf{class labels}, the \code{membershipSample} is empty. We
93 // provide class labels for data instances in the \code{sample} object
94 // using the \code{AddInstance(class label, instance identifier)}
95 // method. As the required initialization step for the
96 // \code{membershipSample}, we must call the
97 // \code{SetNumberOfClasses(number of classes)} method with the number
98 // of classes. We must add all instances in the source sample with
99 // their class labels. In the following code snippet, we set the first
100 // instance class label to 0, the second to 0, the third (last) to
101 // 1. After this, the \code{membershipSample} has two \code{Subclass}
102 // objects. And the class labels for these two \code{Subclass} are 0
103 // and 1. The \textbf{0} class \code{Subsample} object includes the
104 // first and second instances, and the \textbf{1} class includes the
105 // third instance.
106 //
107 // Software Guide : EndLatex
108
109 // Software Guide : BeginCodeSnippet
110 using MembershipSampleType =
111 itk::Statistics::MembershipSample< SampleType >;
112
113 MembershipSampleType::Pointer membershipSample =
114 MembershipSampleType::New();
115
116 membershipSample->SetSample(sample);
117 membershipSample->SetNumberOfClasses(2);
118
119 membershipSample->AddInstance(0U, 0UL );
120 membershipSample->AddInstance(0U, 1UL );
121 membershipSample->AddInstance(1U, 2UL );
122 // Software Guide : EndCodeSnippet
123
124
125 // Software Guide : BeginLatex
126 //
127 // The \code{Size()} and \code{GetTotalFrequency()} methods return the same
128 // values as the \code{sample}.
129 //
130 // Software Guide : EndLatex
131
132 // Software Guide : BeginCodeSnippet
133 std::cout << "Size = " << membershipSample->Size() << std::endl;
134 std::cout << "Total frequency = "
135 << membershipSample->GetTotalFrequency() << std::endl;
136 // Software Guide : EndCodeSnippet
137
138 // Software Guide : BeginLatex
139 //
140 // The \code{membershipSample} is ready for use. The following code snippet
141 // shows how to use \code{Iterator} interfaces. The
142 // \code{MembershipSample} \code{Iterator} has an additional method
143 // that returns the class label (\code{GetClassLabel()}).
144 //
145 // Software Guide : EndLatex
146
147 // Software Guide : BeginCodeSnippet
148 MembershipSampleType::Iterator iter = membershipSample->Begin();
149 while ( iter != membershipSample->End() )
150 {
151 std::cout << "instance identifier = " << iter.GetInstanceIdentifier()
152 << "\t measurement vector = "
153 << iter.GetMeasurementVector()
154 << "\t frequency = "
155 << iter.GetFrequency()
156 << "\t class label = "
157 << iter.GetClassLabel()
158 << std::endl;
159 ++iter;
160 }
161 // Software Guide : EndCodeSnippet
162
163
164 // Software Guide : BeginLatex
165 //
166 // To see the numbers of instances in each class subsample, we use
167 // the \code{GetClassSampleSize(class label)} method.
168 //
169 // Software Guide : EndLatex
170
171 // Software Guide : BeginCodeSnippet
172 std::cout << "class label = 0 sample size = "
173 << membershipSample->GetClassSampleSize(0) << std::endl;
174 std::cout << "class label = 1 sample size = "
175 << membershipSample->GetClassSampleSize(0) << std::endl;
176 // Software Guide : EndCodeSnippet
177
178
179 // Software Guide : BeginLatex
180 //
181 // We call the \code{GetClassSample(class label)} method to get the
182 // class subsample in the \code{membershipSample}. The
183 // \code{MembershipSampleType::ClassSampleType} is actually an
184 // specialization of the \subdoxygen{Statistics}{Subsample}. We print
185 // out the instance identifiers, measurement vectors, and frequency
186 // values that are part of the class. The output will be two lines for
187 // the two instances that belong to the class \textbf{0}.
188 //
189 // Software Guide : EndLatex
190
191 // Software Guide : BeginCodeSnippet
192 MembershipSampleType::ClassSampleType::Pointer classSample =
193 membershipSample->GetClassSample(0);
194 MembershipSampleType::ClassSampleType::Iterator c_iter =
195 classSample->Begin();
196 while ( c_iter != classSample->End() )
197 {
198 std::cout << "instance identifier = " << c_iter.GetInstanceIdentifier()
199 << "\t measurement vector = "
200 << c_iter.GetMeasurementVector()
201 << "\t frequency = "
202 << c_iter.GetFrequency() << std::endl;
203 ++c_iter;
204 }
205 // Software Guide : EndCodeSnippet
206
207 return EXIT_SUCCESS;
208 }
209