PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/matlab_tools/Converted/kkkmeans.m

http://github.com/aludnam/MATLAB
Objective C | 320 lines | 317 code | 3 blank | 0 comment | 58 complexity | c98cdff4248c12d1eb38c88a3d3bd239 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %kkkmeans 'Perform K-Means Clustering'
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros kkmeans.pane file
  3. %
  4. % Parameters:
  5. % InputFile: i1 'Input data object', required: 'input data object'
  6. % InputFile: i2 'Cluster center input object', optional: 'cluster center input object'
  7. % Toggle: map 'Generate output map', default: 0: 'generate output map'
  8. % Toggle: spectrum 'SPECTRUM compatable map segment', default: 0: 'SPECTRUM compatable map segment'
  9. % Integer: n 'Max number of iterations', default: 50000: 'max number of iterations'
  10. % Integer: k 'Number of clusters', default: 2: 'number of clusters'
  11. % OutputFile: o1 'Cluster number output object', required: 'cluster number output object'
  12. % OutputFile: o2 'Cluster center output object', optional: 'cluster center output object'
  13. % OutputFile: o3 'Cluster variance output object', optional: 'cluster variance output object'
  14. % OutputFile: o4 'Cluster membership count output', optional: 'Cluster membership count output'
  15. % OutputFile: o5 'K-means statistics output (ASCII)', optional: 'K-means statistics output (ASCII)'
  16. %
  17. % Example: [o1, o2, o3, o4, o5] = kkkmeans({i1, i2}, {'i1','';'i2','';'map',0;'spectrum',0;'n',50000;'k',2;'o1','';'o2','';'o3','';'o4','';'o5',''})
  18. %
  19. % Khoros helpfile follows below:
  20. %
  21. % PROGRAM
  22. % kkmeans - Perform K-Means Clustering
  23. %
  24. % DESCRIPTION
  25. % .I kkmeans
  26. % accepts an input data object containing vectors of equal size and performs
  27. % the K-means clustering algorithm on the vectors. The length of each vector is
  28. % determined by the elements (E) dimension of the input data object.
  29. %
  30. % The K-means algorithm is based on minimization of the sum of
  31. % the squared distances from all points in a cluster to a cluster
  32. % center. The user chooses K initial cluster centers and the input
  33. % vectors are iteratively distributed among the K cluster domains.
  34. % New cluster centers are computed from these results, such that
  35. % the sum of the squared distances from all points in a cluster to
  36. % the new cluster center is minimized.
  37. %
  38. % Although the K-means algorithm does not really converge (in a continuous
  39. % space), it may converge in a discrete space or a
  40. % practical upper limit can be chosen for convergence. The user has
  41. % the option of specifying the maximum number of iterations using
  42. % the -n option. The default is 50000 iterations.
  43. %
  44. % There are two ways to specify the initial cluster centers. If the -i2
  45. % argument is supplied, then the cluster centers are read from the specified
  46. % object. The vectors are assumed to be stored along the E direction. Only the
  47. % first K centers (as specified by the -k argument) will be read. If the -i2
  48. % argument is "not\fR present, then the first K vectors in the -i1
  49. % object will be used as the initial cluster centers.
  50. %
  51. % It should be noted that it is possible to specify an initial cluster that
  52. % lies at a sufficient distance from all input vectors that it will have no
  53. % vectors assigned to it during a pass of the K-means algorithm. If this
  54. % happens, "kkmeans\fR will reinitialize the value of that cluster to
  55. % the mean value of a moving pair of the existing cluster centers, thus avoiding
  56. % degeneracy.
  57. %
  58. % If no options are selected, the output object specified by the -o1
  59. % argument will contain a value segment specifying the cluster number to
  60. % which each input vector was assigned. If the -map flag is also specified,
  61. % then a map segment will also be generated. The final cluster centers will
  62. % be stored row by row in the map. The values in the value segment can
  63. % be interpreted as "pointing" to a particular row in the map where the
  64. % associated cluster for that input vector can be found.
  65. %
  66. % If the -spectrum flag is specified, then the -o1 output object will
  67. % contain a special map segment (regardless of the -map flag) with additional
  68. % information required for use with the "spectrum\fR program in the most
  69. % general sense. Here, not only the cluster centers are stored, but so are the
  70. % number of vectors associated with each cluster and the packed upper triangle
  71. % of the covariance matrix for each cluster. See the "spectrum\fR manual for
  72. % additional information on how this data is used and the additional capabilities
  73. % that become available when the extra data supplied by the -spectrum flag
  74. % is present.
  75. %
  76. % The -o2 optional argument will generate an output data object containing the
  77. % cluster centers (mean vectors), stored row by row in the value segment.
  78. % The dimensions of the value segment will be WxHx1x1x1 where W is the number of
  79. % elements in each mean vector and W is the number of clusters.
  80. %
  81. % The -o3 optional argument will generate an output data object containing the
  82. % cluster variances, stored row by row in the value segment.
  83. % The dimensions of the value segment will be WxHx1x1x1 where W is the number of
  84. % elements in each vector of variances and W is the number of clusters.
  85. %
  86. % The -o4 optional argument will generate an output data object containing the
  87. % cluster membership counts, stored row by row in the value segment.
  88. % The dimensions of the value segment will be 1xHx1x1x1 where H is the number of
  89. % clusters. The membership counts simply state the number of vectors that
  90. % were present in the input object that were assigned to each of the final
  91. % cluster centers.
  92. %
  93. % The statistics file (-o5) contains statistics obtained during the execution of
  94. % "kkmeans\fR. This file includes the following information:
  95. %
  96. % Total Number of K-means Iterations
  97. % Total Number of Clusters
  98. % Number of Vectors Per Cluster
  99. % Cluster Center Values
  100. % Cluster Center Variance Values
  101. % Trace of Covariance Matrix
  102. %
  103. %
  104. % Results obtained by the K-means algorithm can be influenced by the
  105. % number and choice of initial cluster centers and the geometrical
  106. % properties of the data.
  107. %
  108. % For the -o2, and -o3, output objects, the data will be stored as type
  109. % KDOUBLE. For the -o4 output object, the data will be stored as type KINT.
  110. % For the -o1 output object, the value data will be stored as type
  111. % KSHORT and all map data as type KDOUBLE.
  112. %
  113. % "kkmeans\fR was converted from the K1.5 vkmeans program, which was written
  114. % by Tom Sauer and Charlie Gage, with assistance and ideas from
  115. % Dr. Don Hush, University of New Mexico, Dept. of EECE. Significant modifications
  116. % were made to the algorithm by Scott Wilson during conversion to K2.
  117. %
  118. %
  119. %
  120. % EXAMPLES
  121. % kkmeans -i1 image1 -n 10000 -k 6 -o1 image2 -o2 image3
  122. %
  123. % this will apply the K-means clustering algorithm to image1 using
  124. % the first K vectors as cluster centers. The number of iterations
  125. % selected is 10000, and the number of clusters selected is 6.
  126. % Image2 will contain a map linking each input vector to it's
  127. % respective cluster center, while image3 will contain the
  128. % actual cluster centers.
  129. %
  130. % kkmeans -i1 image1 -i2 file1 -k 8 -o1 image2 -o2 image3 -o5 file2 -map
  131. %
  132. % this will apply the K-means clustering algorithm to image1 using
  133. % the cluster centers specified in file1. The -k
  134. % option specifies 8 cluster centers. An ASCII file containing
  135. % the K-means statistics (file2) is created. The other output objects are
  136. % as specified above, except that image2 will also have a map attached containing
  137. % the cluster centers.
  138. %
  139. % kkmeans -i1 object1 -i2 object2 -k 8 -o1 object3 -spectrum
  140. %
  141. % this will apply the K-means clustering algorithm to object1 using
  142. % as input object2 to specify the initial cluster centers. The -k option
  143. % specifies 8 cluster centers. Object3 will contain not only the mapping
  144. % from vectors to clusters (in the value segment), but an extended map segment
  145. % containing the cluster centers, counts, and covariance matrices. This
  146. % object can be automatically classified using the AutoClassify utilities in
  147. % "spectrum\fR.
  148. %
  149. % "SEE ALSO"
  150. % spectrum(1)
  151. %
  152. % RESTRICTIONS
  153. % "kkmeans\fR will not operate on any form of COMPLEX data. Mask data is
  154. % currently ignored. If map data is present, then the value data is pulled through
  155. % the map prior to application of the K-means algorithm.
  156. %
  157. % A maximum of 32767 clusters can be requested due to the use of the KSHORT
  158. % output data type. If more clusters than this are desired, then the code can
  159. % be easily modified to change the output data type to KINT.
  160. %
  161. % REFERENCES
  162. %
  163. % The K-means algorithm is also called out as the Basic Isodata algorithm in
  164. % R. Duda and P. Hart, \fBPattern Classification and Scene Analysis\fR, Wiley,
  165. % N.Y., 1973, p. 201. ISBN 0-471-22361-1. This is a dated, by very useful
  166. % reference.
  167. %
  168. % COPYRIGHT
  169. % Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI") All rights reserved.
  170. %
  171. function varargout = kkkmeans(varargin)
  172. if nargin ==0
  173. Inputs={};arglist={'',''};
  174. elseif nargin ==1
  175. Inputs=varargin{1};arglist={'',''};
  176. elseif nargin ==2
  177. Inputs=varargin{1}; arglist=varargin{2};
  178. else error('Usage: [out1,..] = kkkmeans(Inputs,arglist).');
  179. end
  180. if size(arglist,2)~=2
  181. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  182. end
  183. narglist={'i1', '__input';'i2', '__input';'map', 0;'spectrum', 0;'n', 50000;'k', 2;'o1', '__output';'o2', '__output';'o3', '__output';'o4', '__output';'o5', '__output'};
  184. maxval={0,1,0,0,100000,2,0,1,1,1,1};
  185. minval={0,1,0,0,0,2,0,1,1,1,1};
  186. istoggle=[0,1,1,1,1,1,0,1,1,1,1];
  187. was_set=istoggle * 0;
  188. paramtype={'InputFile','InputFile','Toggle','Toggle','Integer','Integer','OutputFile','OutputFile','OutputFile','OutputFile','OutputFile'};
  189. % identify the input arrays and assign them to the arguments as stated by the user
  190. if ~iscell(Inputs)
  191. Inputs = {Inputs};
  192. end
  193. NumReqOutputs=1; nextinput=1; nextoutput=1;
  194. for ii=1:size(arglist,1)
  195. wasmatched=0;
  196. for jj=1:size(narglist,1)
  197. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  198. wasmatched = 1;
  199. was_set(jj) = 1;
  200. if strcmp(narglist{jj,2}, '__input')
  201. if (nextinput > length(Inputs))
  202. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  203. end
  204. narglist{jj,2} = 'OK_in';
  205. nextinput = nextinput + 1;
  206. elseif strcmp(narglist{jj,2}, '__output')
  207. if (nextoutput > nargout)
  208. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  209. end
  210. if (isempty(arglist{ii,2}))
  211. narglist{jj,2} = 'OK_out';
  212. else
  213. narglist{jj,2} = arglist{ii,2};
  214. end
  215. nextoutput = nextoutput + 1;
  216. if (minval{jj} == 0)
  217. NumReqOutputs = NumReqOutputs - 1;
  218. end
  219. elseif isstr(arglist{ii,2})
  220. narglist{jj,2} = arglist{ii,2};
  221. else
  222. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  223. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  224. end
  225. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  226. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  227. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  228. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  229. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  230. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  231. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  232. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  233. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  234. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  235. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  236. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  237. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  238. end
  239. end
  240. end
  241. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  242. narglist{jj,2} = arglist{ii,2};
  243. end
  244. end
  245. end
  246. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  247. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  248. end
  249. end
  250. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  251. for jj=1:size(narglist,1)
  252. if strcmp(paramtype{jj}, 'Toggle')
  253. if (narglist{jj,2} ==0)
  254. narglist{jj,1} = '';
  255. end;
  256. narglist{jj,2} = '';
  257. end;
  258. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  259. narglist{jj,1} = '';
  260. narglist{jj,2} = '';
  261. end;
  262. if strcmp(narglist{jj,2}, '__input')
  263. if (minval{jj} == 0) % meaning this input is required
  264. if (nextinput > size(Inputs))
  265. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  266. else
  267. narglist{jj,2} = 'OK_in';
  268. nextinput = nextinput + 1;
  269. end
  270. else % this is an optional input
  271. if (nextinput <= length(Inputs))
  272. narglist{jj,2} = 'OK_in';
  273. nextinput = nextinput + 1;
  274. else
  275. narglist{jj,1} = '';
  276. narglist{jj,2} = '';
  277. end;
  278. end;
  279. else
  280. if strcmp(narglist{jj,2}, '__output')
  281. if (minval{jj} == 0) % this is a required output
  282. if (nextoutput > nargout & nargout > 1)
  283. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  284. else
  285. narglist{jj,2} = 'OK_out';
  286. nextoutput = nextoutput + 1;
  287. NumReqOutputs = NumReqOutputs-1;
  288. end
  289. else % this is an optional output
  290. if (nargout - nextoutput >= NumReqOutputs)
  291. narglist{jj,2} = 'OK_out';
  292. nextoutput = nextoutput + 1;
  293. else
  294. narglist{jj,1} = '';
  295. narglist{jj,2} = '';
  296. end;
  297. end
  298. end
  299. end
  300. end
  301. if nargout
  302. varargout = cell(1,nargout);
  303. else
  304. varargout = cell(1,1);
  305. end
  306. global KhorosRoot
  307. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  308. w=['"' KhorosRoot];
  309. else
  310. if ispc
  311. w='"C:\Program Files\dip\khorosBin\';
  312. else
  313. [s,w] = system('which cantata');
  314. w=['"' w(1:end-8)];
  315. end
  316. end
  317. [varargout{:}]=callKhoros([w 'kkmeans" '],Inputs,narglist);