#!/usr/bin/env python
"""
NumPy implementation of the classic 'four regions' benchmark.
By David Warde-Farley -- user AT cs dot toronto dot edu (user = dwf)

Redistributable under the terms of the 3-clause BSD license 
(see source file).
"""

"""
Copyright (c) 2008 David Warde-Farley
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright 
     notice, this list of conditions and the following disclaimer in 
     the documentation and/or other materials provided with the distribution.

  3. The names of the authors may not be used to endorse or promote products
     derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import numpy as N

def fourregions_labels(points):
    """
    Returns labels for points in [-1,1]^2 from the "four regions" benchmark
    classification task first described by Singhal and Wu.
    
    'points' is a Nx2 rank-2 array (or numpy.matrix)
    
    For more information see:
        S. Singhal and L. Wu, "Training multilayer perceptrons with the 
        extended Kalman algorithm". Advances in Neural Information 
        Processing Systems, Proceedings of the 1988 Conference, pp133-140.
        http://books.nips.cc/papers/files/nips01/0133.pdf
    """
    region = N.zeros(points.shape[0])
    tophalf = points[:,1] > 0
    righthalf = points[:,0] > 0
    dists = N.sqrt(N.sum(points**2,axis=1))
    
    # The easy ones -- the outer shelf.
    region[dists > N.sqrt(2)] = N.nan
    outer = dists > 5./6.
    region[N.logical_and(tophalf, outer)] = 3
    region[N.logical_and(N.logical_not(tophalf), outer)] = 4
    
    firstring = N.logical_and(dists > 1./6., dists <= 1./2.)
    secondring = N.logical_and(dists > 1./2., dists <= 5./6.)
    
    # Region 2 -- right inner and left outer, excluding center nut
    region[N.logical_and(firstring, righthalf)] = 2
    region[N.logical_and(secondring,N.logical_not(righthalf))] = 2
    
    # Region 1 -- left inner and right outer, including center nut
    region[N.logical_and(secondring, righthalf)] = 1
    region[N.logical_and(N.logical_not(righthalf),dists < 1./2.)] = 1
    region[N.logical_and(righthalf,dists < 1./6.)] = 1
    assert(N.all(region > 0))
    return region


def demo():
    """Run a little demo, if matplotlib is available."""
    x = rand(90000,2) * 2 - 1
    scatter(x[:,0],x[:,1],10,fourregions_labels(x),cmap=gray())
    axis('equal')
    title('90000 samples from the four regions benchmark')
    show()

if __name__ == "__main__":
    try:
        from pylab import *
        demo()
    except:
        pass