File: cmeans.cc

package info (click to toggle)
mia 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,532 kB
  • ctags: 16,800
  • sloc: cpp: 137,909; python: 1,057; ansic: 998; sh: 146; xml: 127; csh: 24; makefile: 13
file content (443 lines) | stat: -rw-r--r-- 11,815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis 
 * Copyright (c) Leipzig, Madrid 1999-2014 Gert Wollny
 *
 * MIA is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdexcept>
#include <cmath>
#include <cassert>
#include <numeric>
#include <fstream>

#ifdef HAVE_BLAS
extern "C" {
#include <cblas.h>
}
#endif


#include <mia/core/msgstream.hh>
#include <mia/core/cmdlineparser.hh>
#include <mia/core/probmap.hh>

NS_MIA_USE; 
using namespace std; 


const SProgramDescription g_description = {
	{pdi_group,"Miscellaneous programs"}, 
	{pdi_short,"C-means classification of a histogram."}, 
	{pdi_description, "This Program reads a histogram from stdin and evaluates a "
	 "cmeans classification of the intensity values into the given number of classes."}
}; 

typedef pair<int, int> CClassRange; 
typedef map<double, CClassRange> CClassMap; 

struct CForwardTransform {
	CForwardTransform(float minh, float maxh):
		_M_minh(minh),  
		_M_div(maxh - minh + 1.0)
	{
	}

	float operator() (float x) const 
	{
		return (x - _M_minh) / _M_div;
	}
private: 
	float _M_minh;
	float _M_div; 

};

struct CBackTransform {
	CBackTransform(float minh, float maxh):
		_M_minh(minh), 
		_M_div(maxh - minh + 1.0)
	{
	}
	float operator() (float x) const 
	{
		return x *  _M_div + _M_minh;
	}
private: 
	float _M_minh;
	float _M_div; 
};

class CCMeans {
public: 
	CCMeans(double k, double epsilon, bool start_even); 
	CProbabilityVector operator()(double_vector const& histogram, 
				      double_vector& class_centers, bool initialise, bool auto_k) const; 
private:
	void initialise_cc(double_vector& class_centers, double_vector const& histogram) const; 
	void evaluate_probabilities(double_vector const& classes, size_t nvalues , 
				    CProbabilityVector& pv, double k)const;
	double update_class_centers(double_vector& center, double_vector const& histo, double_vector const& shisto, 
				    CProbabilityVector const& pv)const;

	double adjust_k(double_vector const& class_centers, double_vector const& histogram, CProbabilityVector const & pv)const; 
	
	double _M_k; 
	double _M_epsilon; 
	bool _M_even_start; 
};
	

CCMeans::CCMeans(double k, double epsilon, bool start_even):
	_M_k(k), 
	_M_epsilon(epsilon), 
	_M_even_start(start_even)
{
}
	

void CCMeans::evaluate_probabilities(double_vector const& classes, size_t nvalues , 
				     CProbabilityVector& pv, double k)const
{
	for (size_t i = 0; i < nvalues; ++i) {
		double const x = double(i)/ nvalues; 
		double sum = 0.0; 
		for (size_t j = 0; j < classes.size(); ++j) {
			double  val =  x - classes[j]; 
			val = exp(- (val * val) / k);
			pv[j][i] = val; 
			sum += val;
		}
		
		assert(sum != 0.0); 

		
		for (size_t j = 0; j < classes.size(); ++j)
			pv[j][i] /= sum; 

	}
}

double CCMeans::update_class_centers(double_vector& class_center, double_vector const& histo, 
				     double_vector const& shisto,CProbabilityVector const& pv)const
{
	double residuum = 0.0; 

	for (size_t i = 0; i < class_center.size(); ++i) {
		float cc = class_center[i]; 
		
#ifdef HAVE_BLAS
		double sum_prob   = cblas_ddot(histo.size(), &histo[0], 1, &pv[i][0], 1);
		double sum_weight = cblas_ddot(histo.size(), &shisto[0], 1, &pv[i][0], 1); 
#else		
		double_vector::const_iterator ihelp = histo.begin(); 
		double_vector::const_iterator shelp = shisto.begin(); 
		double_vector::const_iterator iprob = pv[i].begin(); 
		double_vector::const_iterator eprob = pv[i].end();
		
		double sum_prob = 0.0; 
		double sum_weight = 0.0; 
	
		for (size_t  ix = 0; ix < histo.size(); ++ix, ++iprob, ++ihelp, ++shelp) {
			if (*iprob > 0.0f) {
				sum_prob += *iprob * *ihelp; 
				sum_weight += *iprob * *shelp; 
			}
		}
		
#endif		
		if (sum_prob  != 0.0) // move slowly in the direction of new center
			cc += 0.5 * (sum_weight / sum_prob - cc); 
		else {
			cvwarn() << "class[" << i << "] has no probable members, keeping old value:" << 
				sum_prob << ":" <<sum_weight <<endl; 
			
		}
		double delta = cc - class_center[i]; 
		residuum += delta * delta; 
		class_center[i] =  cc; 
		
	}// end update class centers
	return sqrt(residuum); 

}

double CCMeans::adjust_k(double_vector const& class_centers, double_vector const& histogram, CProbabilityVector const & pv)const
{
	double_vector cc(class_centers.size()); 
	size_t hsize = histogram.size(); 
	transform(class_centers.begin(), class_centers.end(), cc.begin(), 
		  [hsize](double x){return x * hsize;}); 
	
	// evaluate best mapping of classes based on maximum probability
	vector<int> classmap(histogram.size(), 0); 
	vector<double> classprob(pv[0]);
	for (size_t i = 1; i < cc.size(); ++i) {
		double_vector::const_iterator iprob = pv[i].begin(); 
		double_vector::const_iterator eprob = pv[i].end();
		vector<int>::iterator cmi = classmap.begin(); 
		vector<double>::iterator cpi = classprob.begin(); 

		while (iprob != eprob) {
			if (*cpi < *iprob) {
				*cpi = *iprob; 
				*cmi = i; 
			}
			++iprob; 
			++cpi; 
			++cmi; 
		}
	}
	
	double sum = 0.0; 
	double sum2 = 0.0; 
	double n = 0.0; 
	
	for (size_t i = 1; i < histogram.size(); ++i) {
		double delta = i - cc[classmap[i]]; 
		sum += delta * histogram[i]; 
		sum2 += delta * delta * histogram[i]; 
		n += histogram[i];
	}
	
	const double avg = sum / n; 
	float hist_size = histogram.size() - 1; 
	double new_k = 2 * ((sum2 - avg * sum) / (n-1)) /  (hist_size * hist_size); 
	
	return new_k > 0.001 ? new_k : 0.001; 
}
	
CProbabilityVector CCMeans::operator()(double_vector const& histogram, double_vector& class_centers, 
				       bool initialise, bool auto_k) const
{
	if (initialise)
		initialise_cc(class_centers, histogram); 
	
	CProbabilityVector pv(class_centers.size(), histogram.size()); 

	double_vector scale_histo(histogram); 
	const double dx = 1.0 / histogram.size(); 
	double x = 0.0; 
	
	for (size_t i = 0; i < histogram.size(); ++i, x+=dx)
		scale_histo[i] *= x; 
	
	

	for (size_t i = 0; i < class_centers.size(); ++i)
		if (class_centers[i] > 1.0) 
			class_centers[i] /= histogram.size(); 
	
	const size_t size = histogram.size(); 
	double k = _M_k; 
	bool cont = true; 
	
	while (cont) {
		evaluate_probabilities(class_centers, size, pv, k);
		
		cvmsg() << "Class centers: ";  
		for (double_vector::const_iterator i = class_centers.begin(), e = class_centers.end(); 
		     i != e; ++i) {
			cverb << *i << ", "; 
		}
		cont = update_class_centers(class_centers, histogram, scale_histo, pv) > _M_epsilon;
		if (auto_k) {
			k = adjust_k(class_centers, histogram, pv); 
			cvmsg() << "k = " << k; 
		}
		cvmsg() << '\n';
	};
	
	for (double_vector::iterator i = class_centers.begin(), e = class_centers.end(); i != e; ++i)
		*i *= size; 
	
	return pv; 
}

void CCMeans::initialise_cc(double_vector& class_centers, double_vector const& histogram)const
{
	double const size = histogram.size(); 
	double const step = size / double(class_centers.size() + 1); 
	
	if (_M_even_start) {
		
		for (size_t i = 0; i < class_centers.size(); ++i)
			class_centers[i] = i * step; 
	}else{
		class_centers[0] = 0.0; 
		
		double_vector::const_iterator hi = histogram.begin(); 
		double_vector::const_iterator const he = histogram.end(); 
		++hi; 

		double const thresh = accumulate(hi, he, 0.0) / size;
		float hit = 0.0; 
		size_t i = 1; 
		float val = step; 
		
		while (i < class_centers.size() && hi != he) {
			hit += *hi; 
			if (hit > thresh) {
				class_centers[i++] = val;
				hit -= thresh; 
			}
			++hi; 
			val += step; 
		}
	}
}

void test(double k, bool auto_k)
{
	const size_t Nh = 1024; 
	const size_t Nc = 3; 
	
	cvdebug() << "k = " << k << "\n"; 
	
	double_vector class_centers(Nc);
	float cstep = 1.0 / float(Nc); 

	cvdebug() << "class centers: "; 
	for (size_t i =0; i < Nc; ++i) {
		class_centers[i] = i * cstep + cstep / 4.0;
		cverb << class_centers[i] << ", "; 
	}
	cverb << "\n"; 
	
	double_vector histogram(Nh);
	
	CProbabilityVector pv(class_centers.size(), histogram.size());
	
	for (size_t i = 0; i < Nh; ++i) {
		float h = 0; 
		float x = float(i)/Nh; 
		for (size_t c = 0; c < Nc; ++c) {
			float delta = class_centers[c] - x; 
			delta *= delta; 
			delta /= k;
			float val = exp(-delta);
			pv[c][i] = val; 
			h += val; 
			histogram[i] += 1024 * val; 
		}
		cout << i << ":" << histogram[i] << " "; 
		for (size_t c = 0; c < Nc; ++c) {
			pv[c][i] /= h; 
			cout << pv[c][i] << " "; 
		}
		cout << "\n"; 
	}

	double_vector eval_class_centers(Nc);
	for (size_t i = 0; i < Nc; ++i)
		eval_class_centers[i] = i * cstep;
	
	
	CCMeans cmeans(k, 0.00001, false);
	CProbabilityVector eval_pv = cmeans(histogram, eval_class_centers, false, auto_k);
	for (size_t i = 0; i < Nc; ++i) {
		if (fabs(eval_class_centers[i] - Nh * class_centers[i]) > 0.5) {
			cverr() << i << ": " << eval_class_centers[i] << " vs." <<  Nh * class_centers[i] << "\n"; 
		}
	}
}


int do_main(int argc, char *argv[])
{
	int nclasses = 3; 
	int max_iter = 100; 
	bool even_start = false; 
	bool auto_k = false; 
	bool cut_histo = false; 
	double epsilon = 0.00001; 
	double k = 1.0; 
	bool self_test = false; 
	
	string in_filename; 
	string out_filename; 
	double_vector class_centers; 

	CCmdOptionList options(g_description);

	options.add(make_opt( in_filename, "in-file", 'i', "input file name containing the histogram", 
			      CCmdOptionFlags::required_input)); 
	options.add(make_opt( out_filename, "out-file", 'o', "output file name to store probabilities", 
			      CCmdOptionFlags::required_output)); 
	
	options.add(make_opt( nclasses, "nclasses", 'n', "number of classes to partition into")); 
	options.add(make_opt( max_iter, "max-iter", 'm', "maximum number of iterations")); 
	options.add(make_opt( even_start, "even-start", 'e', "start with centers evenly distributed over the histogram")); 
	options.add(make_opt( class_centers, "class-centers", 'c', "initial class centers")); 
	options.add(make_opt( auto_k, "auto", 'a', "atomatic adaption of variance (experimental)")); 
	options.add(make_opt( cut_histo,"cut-histo", 't', "cut empty histogram at the end "));
	options.add(make_opt( k, "variance", 'k', "variance parameter")); 
	options.add(make_opt( self_test, "self-test", 0, "run self test"));
	

	if (options.parse(argc, argv) != CCmdOptionList::hr_no)
		return EXIT_SUCCESS; 

		
	if (self_test) {
		test(0.002, false); 
		return 0; 
	}
		
	ifstream ifs( in_filename, ifstream::in ); 
			
	vector<double> histo; 
	// read input data
	size_t sig_size_c = 0; 
	size_t sig_size = 0; 
	while (ifs.good()) {
		float val, cnt;
		ifs >> val >> cnt; 
		histo.push_back(cnt); 
		++sig_size_c; 
		if (val > 0)
			sig_size = sig_size_c; 
	}
	ifs.close(); 
		
	if (sig_size < sig_size_c && cut_histo)
		histo.resize(sig_size); 

	cvmsg() << "got a histogram with " << histo.size() << " values\n"; 
	bool initialise = false; 
		
	if (class_centers.empty()) {
		class_centers.resize(nclasses); 
		initialise = true; 
	}
		
	CCMeans cmeans(k, epsilon, even_start); 
			
	CProbabilityVector pv = cmeans(histo, class_centers, initialise, auto_k); 
		
	if (!pv.save(out_filename)) {
		cverr() << "runtime: unable to save probability map\n"; 
		return EXIT_FAILURE; 
	}
	return EXIT_SUCCESS;
}

#include <mia/internal/main.hh>
MIA_MAIN(do_main)