# -*- coding: utf-8 -*-

"""Scatter view."""


# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------

import logging

import numpy as np

from phy.utils._color import _colormap
from .base import ManualClusteringView

logger = logging.getLogger(__name__)


# -----------------------------------------------------------------------------
# Scatter view
# -----------------------------------------------------------------------------

class ScatterView(ManualClusteringView):
    _default_marker_size = 5.

    def __init__(self,
                 coords=None,  # function clusters: Bunch(x, y)
                 **kwargs):

        assert coords
        self.coords = coords

        # Initialize the view.
        super(ScatterView, self).__init__(**kwargs)

    def on_select(self, cluster_ids=None):
        super(ScatterView, self).on_select(cluster_ids)
        cluster_ids = self.cluster_ids
        n_clusters = len(cluster_ids)
        if n_clusters == 0:
            return

        # Retrieve the data.
        ds = [self.coords(cluster_id) for cluster_id in cluster_ids]

        # Compute the data bounds.
        data_bounds = ds[0].get('data_bounds', None)
        if data_bounds is None:
            xmin = np.min([d.x.min() for d in ds])
            ymin = np.min([d.y.min() for d in ds])
            xmax = np.max([d.x.max() for d in ds])
            ymax = np.max([d.y.max() for d in ds])
            data_bounds = (xmin, ymin, xmax, ymax)

        # Plot the points.
        with self.building():
            for i, cluster_id in enumerate(ds):
                d = ds[i]
                x = d.x
                y = d.y
                assert x.ndim == y.ndim == 1
                assert x.shape == y.shape

                self.scatter(x=x, y=y,
                             color=tuple(_colormap(i)) + (.5,),
                             size=self._default_marker_size,
                             data_bounds=data_bounds,
                             )

@@ -324,16 +324,41 @@
 ringView
+%0Afrom phy.plot import NDC
 %0A%0Alogger
@@ -898,25 +898,25 @@
     def 
-on_select
+_get_data
 (self, c
@@ -925,21 +925,16 @@
 ster_ids
-=None
 ):%0A     
@@ -940,42 +940,27 @@
     
-super(ScatterView, self).on_select
+return %5Bself.coords
 (clu
@@ -966,26 +966,21 @@
 uster_id
-s)%0A       
+) for
  cluster
@@ -986,17 +986,12 @@
 r_id
-s = self.
+ in 
 clus
@@ -1001,46 +1001,50 @@
 _ids
+%5D%0A
 %0A    
-    n_clusters = len(cluster_id
+def _get_data_bounds(self, bunch
 s)
+:
 %0A   
@@ -1056,22 +1056,17 @@
 if n
-_clusters == 0
+ot bunchs
 :%0A  
@@ -1081,17 +1081,20 @@
   return
-%0A
+ NDC
 %0A       
@@ -1098,157 +1098,27 @@
     
-# Retrieve the data.%0A        ds = %5Bself.coords(cluster_id) for cluster_id in cluster_ids%5D%0A%0A        # Compute the data bounds.%0A        data_bounds = d
+data_bounds = bunch
 s%5B0%5D
@@ -1213,33 +1213,37 @@
 .min() for d in 
-d
+bunch
 s%5D)%0A            
@@ -1268,33 +1268,37 @@
 .min() for d in 
-d
+bunch
 s%5D)%0A            
@@ -1323,33 +1323,37 @@
 .max() for d in 
-d
+bunch
 s%5D)%0A            
@@ -1386,17 +1386,21 @@
 or d in 
-d
+bunch
 s%5D)%0A    
@@ -1446,25 +1446,24 @@
 , ymax)%0A
-%0A
         
 # Plot t
@@ -1458,54 +1458,74 @@
     
-# Plot the points.%0A        with self.building(
+return data_bounds%0A%0A    def _plot_points(self, bunchs, data_bounds
 ):%0A 
@@ -1535,28 +1535,15 @@
     
-  
-  
 for i, 
-cluster_i
 d in
@@ -1553,17 +1553,21 @@
 umerate(
-d
+bunch
 s):%0A    
@@ -1578,74 +1578,24 @@
     
-    d = ds%5Bi%5D%0A                x = d.x%0A                y =
+x, y = d.x,
  d.y%0A
-    
     
@@ -1635,36 +1635,32 @@
 = 1%0A            
-    
 assert x.shape =
@@ -1670,20 +1670,16 @@
 .shape%0A%0A
-    
         
@@ -1730,20 +1730,16 @@
         
-    
 color=tu
@@ -1790,20 +1790,16 @@
         
-   
- 
 size=sel
@@ -1851,64 +1851,552 @@
     
-    data_bounds=data_bounds,%0A                             
+data_bounds=data_bounds,%0A                         )%0A%0A    def on_select(self, cluster_ids=None):%0A        super(ScatterView, self).on_select(cluster_ids)%0A        cluster_ids = self.cluster_ids%0A        n_clusters = len(cluster_ids)%0A        if n_clusters == 0:%0A            return%0A%0A        # Retrieve the data.%0A        bunchs = self._get_data(cluster_ids)%0A%0A        # Compute the data bounds.%0A        data_bounds = self._get_data_bounds(bunchs)%0A%0A        # Plot the points.%0A        with self.building():%0A            self._plot_points(bunchs, data_bounds
 )%0A

Refactor scatter view