3333
3434from __future__ import division , print_function , unicode_literals
3535
36+ import logging
3637import numpy
38+ from warnings import warn
39+
3740from .classifiers import Classifier
3841
39- from warnings import warn
4042
4143
4244def L1 (v1 , v2 ):
@@ -164,6 +166,8 @@ def kmeans(image, nclusters=10, max_iterations=20, **kwargs):
164166 import spectral
165167 import numpy
166168
169+ logger = logging .getLogger ('spectral' )
170+
167171 if isinstance (image , numpy .ndarray ):
168172 return kmeans_ndarray (* (image , nclusters , max_iterations ), ** kwargs )
169173
@@ -203,7 +207,7 @@ def kmeans(image, nclusters=10, max_iterations=20, **kwargs):
203207 nclusters clusters in the startCenters array.'
204208 centers = numpy .array (start_clusters )
205209 else :
206- print ('Initializing clusters along diagonal of N-dimensional bounding box.' )
210+ logging . debug ('Initializing clusters along diagonal of N-dimensional bounding box.' )
207211 centers = numpy .empty ((nclusters , nbands ), float )
208212 boxMin = image [0 , 0 ]
209213 boxMax = image [0 , 0 ]
@@ -272,8 +276,8 @@ def kmeans(image, nclusters=10, max_iterations=20, **kwargs):
272276 print ("KeyboardInterrupt: Returning clusters from previous iteration" )
273277 return (old_clusters , old_centers )
274278
275- print ('kmeans terminated with' , len ( set ( old_clusters . ravel ())), \
276- 'clusters after' , itnum - 1 , 'iterations.' , file = status )
279+ logger . info ('kmeans terminated with %d clusters after %d iterations' ,
280+ len ( set ( old_clusters . ravel ())), itnum - 1 )
277281 return (old_clusters , centers )
278282
279283
@@ -344,11 +348,11 @@ def kmeans_ndarray(image, nclusters=10, max_iterations=20, **kwargs):
344348 import numpy as np
345349 from spectral .algorithms .spymath import has_nan , NaNValueError
346350
351+ logger = logging .getLogger ('spectral' )
352+
347353 if has_nan (image ):
348354 raise NaNValueError ('Image data contains NaN values.' )
349355
350- status = spectral ._status
351-
352356 # defaults for kwargs
353357 start_clusters = None
354358 compare = None
@@ -384,7 +388,8 @@ def kmeans_ndarray(image, nclusters=10, max_iterations=20, **kwargs):
384388 nclusters clusters in the startCenters array.'
385389 centers = numpy .array (start_clusters )
386390 else :
387- print ('Initializing clusters along diagonal of N-dimensional bounding box.' )
391+ logger .debug ('Initializing clusters along diagonal of N-dimensional' \
392+ ' bounding box.' )
388393 boxMin = np .amin (image , 0 )
389394 boxMax = np .amax (image , 0 )
390395 delta = (boxMax - boxMin ) / (nclusters - 1 )
@@ -400,8 +405,6 @@ def kmeans_ndarray(image, nclusters=10, max_iterations=20, **kwargs):
400405 itnum = 1
401406 while (itnum <= max_iterations ):
402407 try :
403- status .display_percentage ('Iteration %d...' % itnum )
404-
405408 # Assign all pixels
406409 for i in range (nclusters ):
407410 diffs = np .subtract (image , centers [i ], out = diffs )
@@ -423,16 +426,13 @@ def kmeans_ndarray(image, nclusters=10, max_iterations=20, **kwargs):
423426 iterations .append (clusters .reshape (nrows , ncols ))
424427
425428 if compare and compare (old_clusters , clusters ):
426- status .end_percentage ('done.' )
427429 break
428430 else :
429431 nChanged = numpy .sum (clusters != old_clusters )
432+ logger .info ('k-means iteration {} - {} pixels reassigned.' \
433+ .format (itnum , nChanged ))
430434 if nChanged == 0 :
431- status .end_percentage ('0 pixels reassigned.' )
432435 break
433- else :
434- status .end_percentage ('%d pixels reassigned.' \
435- % (nChanged ))
436436
437437 old_clusters [:] = clusters
438438 old_centers [:] = centers
@@ -442,7 +442,7 @@ def kmeans_ndarray(image, nclusters=10, max_iterations=20, **kwargs):
442442 print ("KeyboardInterrupt: Returning clusters from previous iteration." )
443443 return (old_clusters .reshape (nrows , ncols ), old_centers )
444444
445- print ('kmeans terminated with' , len ( set ( old_clusters . ravel ())), \
446- 'clusters after' , itnum - 1 , 'iterations.' , file = status )
445+ logger . info ('kmeans terminated with %d clusters after %d iterations.' ,
446+ len ( set ( old_clusters . ravel ())), itnum - 1 )
447447 return (old_clusters .reshape (nrows , ncols ), centers )
448448
0 commit comments