#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r"""
 #        .---.         .-----------
 #       /     \  __  /    ------
 #      / /     \(  )/    -----   (`-')  _ _(`-')              <-. (`-')_
 #     //////    '\/ `   ---      ( OO).-/( (OO ).->     .->      \( OO) )     .->
 #    //// / //  :   : ---      (,------. \    .'_ (`-')----. ,--./ ,--/  ,--.'  ,-.
 #   // /   /  / `\/ '--         |  .---' '`'-..__)( OO).-. ' |   \ |  | (`-')'.'  /
 #  //          //..\\          (|  '--.  |  |  ' |( _) | | | |  . '|  |)(OO \    /
 # ============UU====UU====      |  .--'  |  |  / : \|  |)| | |  |\    |  |  /   /)
 #             '//||\\`          |  `---. |  '-'  /  '  '-' ' |  | \   |  `-/   /`
 #               ''``            `------' `------'    `-----' `--'  `--'    `--'
 # ######################################################################################
 #
 # Author: edony - edonyzpc@gmail.com
 #
 # twitter : @edonyzpc
 #
 # Last modified: 2015-12-14 13:12
 #
 # Filename: pc_cluster.py
 #
 # Description: All Rights Are Reserved
 #
"""
#import scipy as sp
#import math as m
#import matplotlib as mpl
import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D as Ax3
#from scipy import stats as st
#from matplotlib import cm
import numpy as np

class PyColor(object):
    """ This class is for colored print in the python interpreter!
    "F3" call Addpy() function to add this class which is defined
    in the .vimrc for vim Editor."""
    def __init__(self):
        self.self_doc = r"""
        STYLE: \033['display model';'foreground';'background'm
        DETAILS:
        FOREGROUND        BACKGOUND       COLOR
        ---------------------------------------
        30                40              black
        31                41              red
        32                42              green
        33                43              yellow
        34                44              blue
        35                45              purple
        36                46              cyan
        37                47              white
        DISPLAY MODEL    DETAILS
        -------------------------
        0                default
        1                highlight
        4                underline
        5                flicker
        7                reverse
        8                non-visiable
        e.g:
        \033[1;31;40m   <!--1-highlight;31-foreground red;40-background black-->
        \033[0m         <!--set all into default-->
        """
        self.warningcolor = '\033[0;31m'
        self.tipcolor = '\033[0;32m'
        self.endcolor = '\033[0m'
        self._newcolor = ''
    @property
    def new(self):
        """
        Customized Python Print Color.
        """
        return self._newcolor
    @new.setter
    def new(self, color_str):
        """
        New Color.
        """
        self._newcolor = color_str
    def disable(self):
        """
        Disable Color Print.
        """
        self.warningcolor = ''
        self.endcolor = ''

from sklearn.cluster import MeanShift, estimate_bandwidth
from itertools import cycle

filebuf = open('point_cluster.xyz')
points = []
for line in filebuf.readlines():
    line = line.rstrip().split()
    tmp = [float(line[0]), float(line[1]), float(line[2])]
    points.append(tmp)
points = np.array(points)
print points.shape
bandwidth = estimate_bandwidth(points, quantile=0.1)
ms = MeanShift(bandwidth, bin_seeding=True)
ms.fit(points)
labels = ms.labels_
centers = ms.cluster_centers_

labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)
print(labels)

#plt.figure(1)
#plt.clf()
#colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
#for k, col in zip(range(n_clusters_), colors):
#    my_members = labels == k
#    cluster_center = centers[k]
#    plt.plot(points[my_members, 0], points[my_members, 1], col + '.')
#    plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
#            markeredgecolor='k', markersize=14)
#plt.title('Estimated number of clusters: %d' % n_clusters_)
#plt.show()

@@ -3346,27 +3346,8 @@
 ts)%0A
-print points.shape%0A
 band
@@ -3576,21 +3576,313 @@
 ue)%0A
-print(labels)
+%0Awfiles = %5B%5D%0Afor i in range(n_clusters_):%0A    filename = 'file'%0A    filename += str(i)%0A    filename += '.sp'%0A    wfiles.append(filename)%0A%0Afilebuf = %5B%5D%0Afor f in wfiles:%0A    file = open(f,'w+')%0A    filebuf.append(file)%0A%0Afor l, p in zip(labels, points):%0A    filebuf%5Bl%5D.write('%25d %25d %25d%5Cn'%25(p%5B0%5D, p%5B1%5D, p%5B2%5D))%0A
 %0A%0A#p

update pc_cluster