import numpy as np
import matplotlib.pyplot as plt
import pdb
import os
import cPickle
import numpy as np
import shutil
from photom import photom_inspect, photom_reduce, photom_absolute, photom_relative, photom_checks, photom_optimise

homestr = os.path.expanduser( '~' )

class photom():
    """
    """
    def __init__(self):
        """
        Initalise a default photom object.
        """
        self.analysis_dir = ''
        self.nstars = None
        self.image_list = None
        self.bias_list = None
        self.dark_list = None
        self.flat_list = None
        self.ccdproc_params = 'default'
        self.master_bias = None
        self.master_dark = None
        self.master_flat = None
        self.red_image_list = None
        self.nimages_total = None
        self.nimages_good = None
        self.goodbad_flags = None
        self.coords_input_files = None
        self.coords_input_type = None
        self.photpars = 'default'
        self.fitskypars = 'default'
        self.centerpars = 'default'
        self.datapars = 'default'
        self.dat_files = None
        self.absphot_file = None
        self.relphot_file = None
        return None

    def set_attributes( self, analysis_dir=None, image_list=None, bias_list=None, dark_list=None, \
                        flat_list=None, ccdproc_params=None, ap_params=None, master_bias=None, \
                        master_dark=None, master_flat=None, red_image_list=None, nimages_total=None, \
                        nimages_good=None, goodbad_flags=None, nstars=None, coords_input_files=None, \
                        coords_input_type=None, photpars=None, fitskypars=None, centerpars=None, \
                        datapars=None, dat_files=None, absphot_file=None, relphot_file=None ):
        """
        Set photom object parameters.
        """
        if analysis_dir!=None: self.analysis_dir = analysis_dir.replace( '~', homestr )
        if self.analysis_dir=='': self.analysis_dir = os.getcwd()
        if image_list!=None:
            if (os.path.dirname(image_list)==''):
                self.image_list = str(self.analysis_dir+'/'+image_list).replace('//','/')
            else:
                self.image_list = image_list
        if red_image_list!=None:
            if (os.path.dirname(red_image_list)==''):
                self.red_image_list = str(self.analysis_dir+'/'+red_image_list).replace('//','/')
            else:
                self.red_image_list = red_image_list
        if bias_list!=None:
            if (os.path.dirname(bias_list)==''):
                self.bias_list = str(self.analysis_dir+'/'+bias_list).replace('//','/')
            else:
                self.bias_list = bias_list
        if dark_list!=None:
            if (os.path.dirname(dark_list)==''):
                self.dark_list = str(self.analysis_dir+'/'+dark_list).replace('//','/')
            else:
                self.dark_list = dark_list
        if flat_list!=None:
            if (os.path.dirname(flat_list)==''):
                self.flat_list = str(self.analysis_dir+'/'+flat_list).replace('//','/')
            else:
                self.flat_list = flat_list
        if coords_input_files!=None:
            if np.rank(coords_input_files)==0:
                self.coords_input_files = [str(self.analysis_dir+'/'+coords_input_files).replace('//','/')]
            else:
                self.coords_input_files = []
                for coords_input in coords_input_files:
                    if os.path.dirname(coords_input)=='':
                        coords_input_full = str(self.analysis_dir+'/'+coords_input).replace('//','/')
                    else:
                        coords_input_full = coords_input
                    self.coords_input_files = self.coords_input_files+[coords_input_full]
        if coords_input_type!=None: self.coords_input_type = coords_input_type
        if ccdproc_params!=None: self.ccdproc_params = ccdproc_params
        if ap_params!=None: self.ap_params = ap_params
        if master_bias!=None: self.master_bias = master_bias
        if master_dark!=None: self.master_dark = master_dark
        if master_flat!=None: self.master_flat = master_flat
        if red_image_list!=None: self.red_image_list = red_image_list
        if goodbad_flags!=None: self.goodbad_flags = goodbad_flags
        if nimages_total!=None: self.nimages_total = nimages_total
        if nimages_good!=None: self.nimages_good = nimages_good
        if nstars!=None: self.nstars = nstars
        if photpars!=None: self.photpars = photpars
        if fitskypars!=None: self.fitskypars = fitskypars
        if centerpars!=None: self.centerpars = centerpars
        if datapars!=None: self.datapars = datapars
        if dat_files!=None: self.dat_files = dat_files
        if absphot_file!=None: self.absphot_file = absphot_file
        if relphot_file!=None: self.relphot_file = relphot_file
        self.pickle_obj()
        return None

    def inspect_images( self, obstime_kw=None, iraf_display_mode='display' ):
        """
        """
        photom_inspect.Main( self, obstime_kw=obstime_kw, iraf_display_mode=iraf_display_mode )
        self.pickle_obj()
        return None

    def reduce_images( self, use_previous=False, ccdproc_ccdtype='default', ccdproc_overscan='default', \
                       ccdproc_trim='default', ccdproc_fixpix='default', ccdproc_illumcor='default', \
                       ccdproc_fringecor='default', ccdproc_readcor='default', ccdproc_scancor='default', \
                       ccdproc_interactive='default', ccdproc_biassec='default', ccdproc_trimsec='default' ):
        """
        """
        if self.ccdproc_params=='custom':
            photom_reduce.custom_ccdproc_params( ccdproc_ccdtype=ccdproc_ccdtype, ccdproc_overscan=ccdproc_overscan, \
                                                 ccdproc_trim=ccdproc_trim, ccdproc_fixpix=ccdproc_fixpix, \
                                                 ccdproc_illumcor=ccdproc_illumcor, ccdproc_fringecor=ccdproc_fringecor, \
                                                 ccdproc_readcor=ccdproc_readcor, ccdproc_scancor=ccdproc_scancor, \
                                                 ccdproc_interactive=ccdproc_interactive, ccdproc_biassec=ccdproc_biassec, \
                                                 ccdproc_trimsec=ccdproc_trimsec )
        elif self.ccdproc_params=='default':
            photom_reduce.default_ccdproc_params(self)
        if use_previous==False:
            photom_reduce.Main(self)
        else:
            self.self_update()
        self.pickle_obj()
        return None

    def optimise_aperture( self, ap_size_trials, sky_annulus_trials, sky_dannulus, gain_kw=None, readnoise_kw=None, \
                           exptime_kw=None, obstime_kw=None, airmass_kw=None, ix_target=None, ix_comparisons=None ):
        """
        Searches a grid of aperture radii and sky annulus radii for the combination that
        minimises the scatter of the relative photometry.
        """
        scatter_array = photom_optimise.Main( self, ap_size_trials, sky_annulus_trials, sky_dannulus, datapars_gain=gain_kw, \
                                              datapars_readnoise=readnoise_kw, datapars_exposure=exptime_kw, \
                                              datapars_obstime=obstime_kw, datapars_airmass=airmass_kw, ix_target=ix_target, \
                                              ix_comparisons=ix_comparisons )
        return scatter_array

    def do_absphot( self, photpars_apertures='default', fitskypars_annulus='default', fitskypars_dannulus='default', \
                    fitskypars_salgorithm='default', centerpars_maxshift='default', centerpars_cbox='default', \
                    centerpars_minsnratio='default', datapars_gain='default', datapars_readnoise='default', \
                    datapars_exposure='default', datapars_obstime='default', datapars_airmass='default', make_plots=True ):
        """
        Does absolute photometry for one or more stars given a list of images.
        Output is generated in the form of two types of file:
          1. starX_absphot.dat for X=0,1,2,... files contain columns with the
             more detailed output for each of the stars, with each line
             corresponding to a different image.
          2. absolute.phot file containing the important numerical columns for
             each of the stars; it's supposed to be the most convenient output
             for use with numpy and for generating relative photometry.

       Summary plots are also generated by default:

        Figure 1:
          ** Top left = traces of xy drift for each of the stars
          ** Bottom left = airmass versus time
          ** Top right = absolute flux versus time for each star
          ** Bottom right = sky annulus value as a function of time for each star

        Figure 2:
          ?? Plots image number versus measured scatter divided by the calculated Poisson noise ??
        """

        if self.photpars=='custom':
            photom_absolute.custom_photpars( self, photpars_apertures=photpars_apertures )
        elif self.photpars=='default':
            photom_absolute.default_photpars( self )
        if self.fitskypars=='custom':
            photom_absolute.custom_fitskypars( self, fitskypars_annulus=fitskypars_annulus, fitskypars_dannulus=fitskypars_dannulus, \
                                               fitskypars_salgorithm=fitskypars_salgorithm )
        elif self.fitskypars=='default':
            photom_absolute.default_fitskypars( self )
        if self.centerpars=='custom':
            photom_absolute.custom_centerpars( self, centerpars_maxshift=centerpars_maxshift, centerpars_cbox=centerpars_cbox, \
                                               centerpars_minsnratio=centerpars_minsnratio )
        elif self.centerpars=='default':
            photom_absolute.default_centerpars( self )
        if self.datapars=='custom':
            photom_absolute.custom_datapars( self, datapars_gain=datapars_gain, datapars_readnoise=datapars_readnoise, \
                                             datapars_exposure=datapars_exposure, datapars_obstime=datapars_obstime, \
                                             datapars_airmass=datapars_airmass )
        elif self.datapars=='default':
            photom_absolute.default_datapars( self )
        photom_absolute.Main( self, make_plots=make_plots )
        self.pickle_obj()
        return None

    def do_relphot( self, ix_target=None, ix_comparisons=None, make_plots=True ):
        """
        Calculates relative fluxes using absolute photometry already stored in the photom object.
        Must specify indices for the target star and comparison stars to be used, using the format
        0,1,2,... etc where 0 is the first star.
        """
        photom_relative.Main( self, ix_target=ix_target, ix_comparisons=ix_comparisons, make_plots=make_plots )
        self.pickle_obj()
        return None

    def check_relphot( self ):
        """
        Does two basic checks of the relative photometry in an effort to identify
        variable comparison stars. Output is in the form of plots that must be
        visually inspected to identify variable stars.

        The two types of checks are:

          1. All possible pairs of stars that can be made up from the target
             and comparisons are checked.
          2. A leave-one-out approach is taken, where the relative photometry is
             repeated multiple times, with a different comparison star excluded
             each time.
        """
        photom_checks.comparisons( self )
        return None

    def update_auxvars( self, headerkws=None ):
        """
        Will update the auxiliary variables within the photom object. This is done
        using the photometry already contained in the object to calculate the total
        sum of all stellar fluxes, as well as extracting header information from
        images stored in the red_images_list variable.
        """
        photom_checks.auxiliary_variables( self, headerkws=headerkws )
        return None

    def self_update( self ):
        """
        Routine used to generate default values for a few variables, eg. if
        a certain analysis step, such as reduce_images(), has already been
        performed and so does not need to be repeated.

        !!NOTE: This routine is currently pretty ad-hoc and could possibly do with a
        rethink plus the addition of various features that have been added to the
        overall pipeline since I first wrote this particular routine a while back.
        """
        # Count the total number of images:
        try:
            red_images = np.loadtxt(self.red_image_list, dtype='str')
            self.nimages_total = len(red_images)
        except:
            pass
        # Set the goodbad_flags to all be good:
        if self.goodbad_flags==None:
            try:
                self.goodbad_flags = np.ones(self.nimages_total)
            except:
                pass
        # Count the number of good images:
        self.nimages_good = int(np.sum(self.goodbad_flags))
        self.pickle_obj()
        return None

    def pickle_obj( self, quiet=False ):
        """
        Pickle the photom object in its current state. Saves the output as photom_object.pkl in the
        analysis directory.
        """
        outfile_name = str( self.analysis_dir + '/photom_object.pkl' ).replace( '//', '/' )
        outfile_open = open( outfile_name, 'w' )
        cPickle.dump( self, outfile_open )
        outfile_open.close()
        if quiet==False:
            print '\nSaved %s\n' % outfile_name
        self.pickled_output = outfile_name
        return None

    def backup_the_pickle( self ):
        """
        Makes a backup of the current photom_object.pkl, saving the backed up version as photom_object.pkl.BACKUP
        in the analysis directory.
        """
        pkl_name = str( self.analysis_dir + '/photom_object.pkl' ).replace( '//', '/' )
        shutil.copyfile( pkl_name, pkl_name + '.BACKUP' )
        print '\nBacked up pickled photom object'
        return None


import numpy as np
import cv2
import matplotlib.pyplot as plt

# load in image and add Salt and pepper noise
moon = cv2.imread('images/moon.png', 0)

######################################################## ADD SALT & PEPPER NOISE
# salt and peppering manually (randomly assign coords as either white or black)
rows, cols = moon.shape
salt_vs_pepper_ratio = 0.5
amount = 0.01
moon_salted_and_peppered = moon.copy()
num_salt = np.ceil(amount * moon.size * salt_vs_pepper_ratio)
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in moon.shape]
moon_salted_and_peppered[coords] = 255
num_pepper = np.ceil(amount * moon.size * (1 - salt_vs_pepper_ratio))
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in moon.shape]
moon_salted_and_peppered[coords] = 0

############################################ APPLY MEDIAN FILTER TO REMOVE NOISE
# The second argument is the aperture linear size; it must be odd and greater
# than 1, for example: 3, 5, 7
moon_median = cv2.medianBlur(moon, 3)

# show all three images using Matplotlib
plt.figure(figsize=(15, 6))
plt.subplot(1, 3, 1)
plt.imshow(moon, cmap='gray'), plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 2)
plt.imshow(moon_salted_and_peppered, cmap='gray')
plt.title('Salted & Peppered'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 3)
plt.imshow(moon_median, cmap='gray'), plt.title('Median Blur on S&P')
plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()


import numpy as np

from sklearn.cross_validation import cross_val_score, ShuffleSplit
from sklearn.svm import LinearSVC, SVC
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import precision_recall_fscore_support

import matplotlib.pyplot as plt

data = np.genfromtxt('dists_cleaned.csv', delimiter=',')

#samples = np.bitwise_or(np.bitwise_and(data[:,0] == -1, data[:,1] <= 2), data[:,0]==1)
#data = data[samples,:]
#data = data[data[:,7]>=5,:]
pos = data[data[:,0]==1,:]
neg = data[data[:,0]==-1,:]

a = 0.33974138
b = 0.47850904
c = -0.56307525

xrg = np.linspace(0,5,100)
yrg = np.linspace(0,5,100)

X,Y = np.meshgrid(xrg,yrg)
Z = a * X + b * Y + c
print Z

plt.scatter(neg[:,3], neg[:,4], color='r')
plt.scatter(pos[:,3], pos[:,4], color='b')
plt.contour(X,Y,Z)

plt.xlabel('medians')
plt.ylabel('heights')
plt.show()
#data[data[:,0]==1,0] = 2
#data[data[:,0]==-1,0] = 1
#data[data[:,0]==2,0] = -1

#print data.shape
#data = data[data[:,1]>0,:]
#data = data[data[:,2]>0,:]
#print data.shape
cv = ShuffleSplit(data.shape[0], n_iter=10, random_state=4)

min_dists = [1,2,3,4,5,100]
c1_grid = np.logspace(0,2,10)
c2_grid = np.logspace(0,4,15)
class_weights = {1:5, -1:1}
beta = 1.0

#best_params = {}
#best_fscore = 0
#for d in min_dists:
#    for C1 in c1_grid:
#        for C2 in c2_grid:
#            precisions = []
#            recalls = []
#            fscores = []
#            accs = []
#            for (train_idx, test_idx) in cv:
#                X = data[train_idx,3:5]
#                y = data[train_idx,0]
#
#                svm1 = LinearSVC(random_state=42, C=C1, class_weight=class_weights)
#                svm1.fit(X,y)
#
#                X_simple = data[train_idx, 4]
#                X_simple = X_simple.reshape((train_idx.shape[0], 1))
#                svm2 = LinearSVC(random_state=42, C=C2, class_weight=class_weights)
#                svm2.fit(X_simple, y)
#
#                #ys = svm2.predict(data[test_idx, 4].reshape((test_idx.shape[0], 1)))
#                #accs.append(svm2.score(data[test_idx, 4].reshape((test_idx.shape[0], 1)), data[test_idx,0]))
#
#                ys = np.zeros((test_idx.shape[0],))
#                for i,idx in enumerate(test_idx):
#                    if data[idx,-1] >= d:
#                        ys[i] = svm1.predict(data[idx, 3:5].reshape((1,2)))
#                    else:
#                        ys[i] = svm2.predict(data[idx, 4].reshape((1,1)))
#
#                acc = np.sum(data[test_idx,0] == ys) / float(test_idx.shape[0])
#                accs.append(acc)
#                ps, rs, fs, ss = precision_recall_fscore_support(data[test_idx,0], ys, beta=beta)
#                precisions.append(ps[1])
#                recalls.append(rs[1])
#                fscores.append(fs[1])
#            print 'C1: %f, C2: %f, d: %f, prec: %f, recall: %f, f-score: %f, acc: %f' % (C1, C2, d, np.mean(precisions), np.mean(recalls), np.mean(fscores), np.mean(accs))
#            if np.mean(fscores) > best_fscore:
#                print '*'
#                best_fscore = np.mean(fscores)
#                best_params = {
#                    'C1': C1,
#                    'C2': C2,
#                    'd': d
#                }
#

#best_params = {}
#best_fscore = 0
#print data.shape
#for C1 in c1_grid:
#    precisions = []
#    recalls = []
#    fscores = []
#    accs = []
#    for (train_idx, test_idx) in cv:
#        X = data[train_idx,3:5]
#        y = data[train_idx,0]
#
#        svm1 = LinearSVC(random_state=4,C=C1, class_weight=class_weights)
#        svm1.fit(X,y)
#
#        ys = np.zeros((test_idx.shape[0],))
#        for i,idx in enumerate(test_idx):
#            ys[i] = svm1.predict(data[idx, 3:5].reshape((1,2)))
#
#        acc = np.sum(data[test_idx,0] == ys) / float(test_idx.shape[0])
#        accs.append(acc)
#        ps, rs, fs, ss = precision_recall_fscore_support(data[test_idx,0], ys, beta=beta)
#        precisions.append(ps[1])
#        recalls.append(rs[1])
#        fscores.append(fs[1])
#    svm = LinearSVC(random_state=4,C=C1, class_weight=class_weights)
#    svm.fit(data[:,3:5], data[:,0])
#    print 'C1: %f, prec: %f, recall: %f, f-score: %f, acc: %f' % (C1, np.mean(precisions), np.mean(recalls), np.mean(fscores), np.mean(accs))
#    print svm.coef_
#    print svm.intercept_
#    if np.mean(fscores) > best_fscore:
#        print '*'
#        best_fscore = np.mean(fscores)
#        best_params = {
#            'C1': C1
#        }

#print 'best:'
#print best_params

#svm = SVC(kernel='linear', C=best_params['C1'], class_weight=class_weights)
#svm.fit(data[:,1:3], data[:,0])
#print svm.coef_
#print svm.intercept_
#svm = SVC(kernel='linear', C=best_params['C1'], class_weight=class_weights)
#svm.fit(data[:,3:5], data[:,0])
#print svm.coef_
#print svm.intercept_

#svm = SVC()
#search = GridSearchCV(svm, {
#    'kernel': ('rbf',), 'C': [1,10,100,1000],
#    'gamma': [0.05, 0.1, 0.25, 0.128, 0.5, 1.0, 1.5],
#    'class_weight': ({1:1,-1:1},),
#    }, cv=cv)
svm = LinearSVC()
search = GridSearchCV(svm, {
    'C': np.logspace(0,4,15).tolist(),
    'class_weight': (
        {1:1,1:1},
        {1:1,-1:2},
        {1:1,-1:3},
        {1:1,-1:4},
        {-1:1,1:2},
        {-1:1,1:3},
        {-1:1,1:5},{1:4,-1:1},{1:5,-1:1})
    }, cv=cv, refit=False)

search.fit(data[:,3:5], data[:,0],cv=cv)

print search
print search.best_score_
print search.best_params_

svm = LinearSVC(random_state=42,**search.best_params_)
svm.fit(data[:,1:3],data[:,0])
print svm.score(data[:,1:3],data[:,0])
print svm.coef_
print svm.intercept_


#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import pandas as pd
from textblob import TextBlob
from time import strptime
import numpy as np
import re
import time
import zipcode
import sys, errno
from nltk.corpus import stopwords
from itertools import combinations
from collections import Counter
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import nltk
nltk.download('stopwords')

runCount=0
#Variables that contains the user credentials to access Twitter API
access_token = ""
access_token_secret = ""
consumer_key = ""
consumer_secret = ""

tweets_data = []

stop = stopwords.words('english') + ['and']
emoticons_str = r"""
	(?:
		[:=;] # Eyes
		[oO\-]? # Nose (optional)
		[D\)\]\(\]/\\OpP] # Mouth
	)"""

regex_str = [
	emoticons_str,
	r'<[^>]+>', # HTML tags
	r'(?:@[\w_]+)', # @-mentions
	r"(?:\#+[\w_]+[\w\'_\-]*[\w_]+)", # hash-tags
	r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+', # URLs

	r'(?:(?:\d+,?)+(?:\.?\d+)?)', # numbers
	r"(?:[a-z][a-z'\-_]+[a-z])", # words with - and '
	r'(?:[\w_]+)', # other words
	r'(?:\S)' # anything else
]

tokens_re = re.compile(r'('+'|'.join(regex_str)+')', re.VERBOSE | re.IGNORECASE)
emoticon_re = re.compile(r'^'+emoticons_str+'$', re.VERBOSE | re.IGNORECASE)
Count=0
stop = stopwords.words('english')

def create_dataframe(tweets_data):
	tweets = pd.DataFrame(index=range(len(tweets_data)),
	columns=['text','created_at','location','state','sentiment','sentiment_cat','country_code','hour'])

	for i in range(len(tweets_data)):
		try:
			tweets['text'][i] = tweets_data[i]['text']
		except:
			tweets['text'][i] = ""
		try:
			tweets['location'][i]=tweets_data[i]['user']['location']
		except:
			tweets['location'][i]='NA'
		try:
			tweets['country_code'][i]=tweets_data[i]['place']['country_code']
		except:
			tweets['country_code'][i]=''
		try:
			lon=tweets_data[i]['place']['bounding_box']['coordinates'][0][0][0]
		except:
			lon='NA'
		try:
			lat=tweets_data[i]['place']['bounding_box']['coordinates'][0][0][1]
		except:
			lat='NA'
		#print (lat,lon)
		try:
			tweets['created_at'][i]=tweets_data[i]['created_at']
		except:
			tweets['created_at'][i]='NA'
		try:
			tweets['hour'][i]=tweets['created_at'][i][11:13]
		except:
			tweets['hour'][i]='NA'
		try:
			stateFromData=tweets['location'][i].split(',')[1]
		except:
			stateFromData=''
		if len(stateFromData)==2:
			tweets['state'][i]=stateFromData
		else:
			if lat!='NA':
				radius=10
				incre=10
				zips=zipcode.isinradius((lat,lon),radius)
				while len(zips)==0:
					radius=radius+incre
					zips=zipcode.isinradius((lat,lon),radius)
					incre=incre+10
				myzip = zipcode.isequal(str(zips[0].zip))
				tweets['state'][i]=myzip.state
			else:
				tweets['state'][i]='NA'
		blob = TextBlob(tweets['text'][i])
		try:
			sentence=blob.sentences[0]
			tweets['sentiment'][i]=float(sentence.sentiment.polarity)
		except:
			tweets['sentiment'][i]=0
		if tweets['sentiment'][i] < 0:
			tweets['sentiment_cat'][i] = 'Neg'
		else:
			if tweets['sentiment'][i] > 0:
				tweets['sentiment_cat'][i] = 'Pos'
			else:
				tweets['sentiment_cat'][i] = 'Neu'
	print (tweets.head())
	return tweets

def state_senti(newFolder,usStateSentiOld,tweetsFinal):

    output2=pd.DataFrame({'value' : tweetsFinal.groupby( [ "State","sentiment_cat"] ).size()}).reset_index()

    outData=pd.pivot_table(output2,values='value', index=['State'], columns=['sentiment_cat'], aggfunc=np.sum)
    outData=outData.fillna(0)
    outData['State']=outData.index
    #outData.reset_index()
    print (outData.columns.values)
    outData = pd.merge(usStateSentiOld, outData,  how='left', left_on='State', right_on = 'State')
    outData=outData.fillna(0)
    outData['Pos']=outData['Pos_x']+outData['Pos_y']
    del outData['Pos_x']
    del outData['Pos_y']
    outData['Neg']=outData['Neg_x']+outData['Neg_y']
    del outData['Neg_x']
    del outData['Neg_y']
    outData['Neu']=outData['Neu_x']+outData['Neu_y']
    del outData['Neu_x']
    del outData['Neu_y']
    outData.to_csv(newFolder+"usStates-SentiCount.csv",index=False)
    #-------------------------------------------
    try:
        outData['sum']=outData[['Neg', 'Neu', 'Pos']].sum(axis=1)
        outData['max']=outData['maxFinal']=outData[['Neg', 'Neu', 'Pos']].idxmax(axis=1)
    except:
        outData['sum']=outData[['Neu', 'Pos']].sum(axis=1)
        outData['max']=outData['maxFinal']=outData[[ 'Neu', 'Pos']].idxmax(axis=1)
    #-------------------------------------------
    for i in range(len(outData)):
        if outData['max'][i] =="Pos":
            outData['maxFinal'][i] = '1'
        else:
            if outData['max'][i] =="Neu":
                outData['maxFinal'][i] = '-1'
            else:
                outData['maxFinal'][i] = '2'

    del outData['max']

    d="var data =[\n"
    for i in range(len(outData)):
        row=outData.ix[i]
        #print (row)
        d += "[\'"+row['State']+"\',"+",".join([str(i) for i in row[:5]])+"],\n"

    return d+']'

def create_timechart(newFolder,oldtimedata,tweets):
    td1 = pd.DataFrame({'value' : tweets.groupby( [ "created_at"] ).size()}).reset_index()
    td1['created_at'] = td1['created_at'].astype('str')
    mask = (td1['created_at'].str.len() > 2)
    td1=td1.loc[mask]
    timedata = td1[td1.created_at != 'NA']
    timedata=oldtimedata.append(timedata, ignore_index=True)
    timedata.to_csv(newFolder+"timeseries.csv",index=False)
    data1 ={}
    data = ["var data=["]
    for i in range(0,len(timedata)):

        year = timedata['created_at'][i][-4:]
        if (timedata['created_at'][i][4:7] == 'Jan'):
            mon = '1'
        else:
            if (timedata['created_at'][i][4:7] == 'Feb'):
                mon = '2'
            else:
                if (timedata['created_at'][i][4:7] == 'Mar'):
                    mon = '3'
                else:
                    if (timedata['created_at'][i][4:7] == 'Apr'):
                        mon = '4'
                    else:
                        if (timedata['created_at'][i][4:7] == 'May'):
                            mon = '5'
                        else:
                            if (timedata['created_at'][i][4:7] == 'Jun'):
                                mon = '6'
                            else:
                                if (timedata['created_at'][i][4:7] == 'Jul'):
                                    mon = '7'
                                else:
                                    if (timedata['created_at'][i][4:7] == 'Aug'):
                                        mon = '8'
                                    else:
                                        if (timedata['created_at'][i][4:7] == 'Sep'):
                                            mon = '9'
                                        else:
                                            if (timedata['created_at'][i][4:7] == 'Oct'):
                                                mon = '10'
                                            else:
                                                if (timedata['created_at'][i][4:7] == 'Nov'):
                                                    mon = '11'
                                                else:
                                                    mon = '12'
        date = timedata['created_at'][i][7:10]
        hour = timedata['created_at'][i][10:13]
        minu = timedata['created_at'][i][14:16]
        sec = timedata['created_at'][i][17:20]
        value = timedata['value'][i]
        data1 = ("[Date.UTC("+str(year)+","+str(mon)+","+str(date)+","+str(hour)+","+str(minu)+","+str(sec)+"),"+str(value)+"]")
        if (len(timedata)):
            data.append
        data.append(data1)
    data = ",\n".join(data)+"\n]"
    data = data.replace("[,","[")
    return data

def tokenize(s):
	tokens=tokens_re.findall(s)
	return [ x for x in tokens if 'http' not in x and len(x)>1 and x.lower() not in stop]

def preprocess(s, lowercase=True):
	tokens = tokenize(s)
	if lowercase:
		tokens = [token if emoticon_re.search(token) else token.lower() for token in tokens]
	return tokens

def collect_pairs(lines):
	pair_counter = Counter()
	for line in lines:
		unique_tokens = sorted(set(line))  # exclude duplicates in same line and sort to ensure one word is always before other
		combos = combinations(unique_tokens, 2)
		pair_counter += Counter(combos)
	return pair_counter
#Co-occurrence:
def co_occur(tweets):
	t2 = []
	t1 =tweets['text']
	for t in range(len(t1)):
		t2.append(preprocess(t1[t]))
	pairs = collect_pairs(t2)
	top_pairs = pairs.most_common(200)
	nodes={}
	links=["\"links\":["]
	count =0
	len_top=len(top_pairs)
	nptp = np.array(top_pairs)
	maxtp = np.max(nptp[:,1])
	for p in range(len(top_pairs)):
		for i in range(2):
			if top_pairs[p][0][i] not in nodes:
				nodes[top_pairs[p][0][i]] = count
				count+=1
		link="{ \"source\":"+str(nodes[top_pairs[p][0][0]])+",\"target\":"+str(nodes[top_pairs[p][0][1]])+",\"value\":"+str(round(top_pairs[p][1]*10/maxtp))+"}"
		links.append(link)
	links=",\n".join(links)+"\n]"
	links=links.replace("[,","[")
	nodes = sorted(nodes.items(), key=lambda x: x[1])
	nodes1=["\"nodes\":["]
	for p in range(len(nodes)):
		nodes1.append("{ \"name\":\""+nodes[p][0]+"\",\"group\":"+"0}")
	nodes1=",\n".join(nodes1)+"\n]"
	nodes1=nodes1.replace("[,","[")

	return nodes1,links

def heatworldgrid(newFolder,worldOld,tweets):
    contdata=pd.read_csv("continents.txt")
    contdat=contdata.fillna("NA")
    tweets['sentiment']=tweets['sentiment'].apply(pd.to_numeric)
    #print (tweets.dtypes)

    Countryhour=pd.DataFrame({'sentiment' : tweets.groupby( ["country_code","hour"] )['sentiment'].mean()}).reset_index()
    final=pd.merge(Countryhour, contdata, how='left',left_on="country_code",right_on="country")
    print (final.columns.values)
    del final['country']
    #del final['Unnamed: 0']
    del final['country_code']

    Conthour=pd.DataFrame({'sentiment' : final.groupby( ["continent","hour"] )['sentiment'].mean()}).reset_index()
    Conthour = pd.merge(worldOld, Conthour,  how='left', left_on=["continent","hour"] , right_on = ["continent","hour"] )
    Conthour=Conthour.fillna(0)
    Conthour['sentiment']=(Conthour['sentiment_x']*1000000+Conthour['sentiment_y']*10000)/(1010000)

    del Conthour['sentiment_x']
    del Conthour['sentiment_y']
    Conthour.to_csv(newFolder+"Continent-hour-senti.csv",index=False)
    minVal=min(Conthour['sentiment'])
    maxVal=max(Conthour['sentiment'])
    outputStr=""
    uniqueCont= list(np.unique(Conthour['continent']))
    outputStr+="var continent =["+",".join(["'"+i+"'" for i in uniqueCont])+"];\n"
    numCont=len(uniqueCont)
    numHour=24

    outputStr+="var hour =["+",".join(["'"+str(i)+"'" for i in range(numHour)])+"];\n"
    outMatrix=np.zeros(shape=(numCont,numHour))
    outputStr+="var data=["
    datastr=[]
    for i in range(len(Conthour)):
        continent=Conthour['continent'][i]
        hour=Conthour['hour'][i]
        contIndex=uniqueCont.index(continent)
        outMatrix[contIndex][int(hour)]=Conthour['sentiment'][i]

    for i in range(numCont):
        for j in range(numHour):
            datastr.append("["+str(j)+","+str(i)+","+str(int(outMatrix[i][j]))+"]")

    outputStr+=",".join(datastr)+"]; var minval = "+str(minVal)+";\n var maxval = "+str(maxVal)+";"

    return outputStr

def createwordcloud(tweets):
	# Read the whole text.
	#text = open(path.join(d, 'constitution.txt')).read()
	textpos = tweets[tweets.sentiment_cat == 'Pos']
	textneg = tweets[tweets.sentiment_cat == 'Neg']

	postweets=""
	for i in textpos.index.values:
		postweets+=textpos['text'][i]+" "
	negtweets=""
	for i in textneg.index.values:
		negtweets+=textneg['text'][i]+" "

	textp = preprocess(postweets)
	textp=" ".join(textp)
	textn = preprocess(negtweets)
	textn=" ".join(textn)
	wordcloudp = WordCloud( stopwords=stop,background_color='white',width=1200,height=1000).generate(textp)
	wordcloudn = WordCloud( stopwords=stop,background_color='white', width=1200,height=1000).generate(textn)
	image1 = wordcloudp.to_image()
	image2= wordcloudn.to_image()
	image1.save("wordcloup.png")
	image2.save("wordcloudn.png")

def analyze(tweets_data):
	oldFolder="Data\\"
	outputFolder="OutputJS\\"
	newFolder="NewData\\"

	#Dataframe is created from the list of json tweets, sentiment is also calculated
	tweets=create_dataframe(tweets_data)
	statedata=pd.read_csv(oldFolder+"states.csv")
	tweetsFinal=pd.merge(tweets, statedata, how='left',left_on="state",right_on="Abbreviation")

	#UsStatewise Tweets
	usStateOld=pd.read_csv(oldFolder+"usStatesCount.csv")
	usState=pd.DataFrame({'value' : tweetsFinal.groupby( [ "State"] ).size()}).reset_index()
	usState_new = pd.merge(usStateOld, usState,  how='left', left_on='State', right_on = 'State')
	usState_new=usState_new.fillna(0)
	usState_new['value']=usState_new['value_x']+usState_new['value_y']
	del usState_new['value_x']
	del usState_new['value_y']
	usState_new.to_csv(newFolder+"usStatesCount.csv",index=False)
	print (usState_new.head())
	usStateJson=usState_new.to_json(orient = "records")
	usStateJsonfinalOutput=usStateJson[33:len(usStateJson)-1].upper().replace("\"STATE\"","ucName").replace("\"VALUE\"","value")
	with open('Final\\US_heat_count\\data.js', 'w') as outfile:
		outfile.write(usStateJsonfinalOutput)

	#UsStatewise Sentiment
	usStateSentiOld=pd.read_csv(oldFolder+"usStates-SentiCount.csv")
	statesentiout=state_senti(newFolder,usStateSentiOld,tweetsFinal)
	with open('Final\\map-pies\\data.js', 'w') as outfile:
		outfile.write(statesentiout)

	#TimeSeries Chart
	timeOld=pd.read_csv(oldFolder+"timeseries.csv")
	timedata=create_timechart(newFolder,timeOld,tweets)
	with open('Final\\dynamic-master-detail\\time_series.js', 'w') as outfile:
		outfile.write(timedata)

	#Co-occur Chart
	nodes1,links=co_occur(tweets)
	with open(outputFolder+'cooccur_word-1.json', 'w') as outfile:
		outfile.write("{\n"+nodes1+",\n"+links+"}\n")

	#Heat World Grid
	worldOld=pd.read_csv(oldFolder+"Continent-hour-senti.csv")
	heatjson=heatworldgrid(newFolder,worldOld,tweets)
	with open('Final\\heatmap\\heatchart_data-1.js', 'w') as outfile:
		outfile.write(heatjson)
	#WordCloud
	createwordcloud(tweets)

#This is a basic listener that just prints received tweets to stdout.

class StdOutListener(StreamListener):

	def on_data(self, data):
		global Count,tweets_data
		Count+=1
		#TweetCount+=1
		if Count%1000==0:
			print ("Analyze data started")
			x=time.time()
			analyze(tweets_data)
			print ("Analyze data Completed in ", time.time()-x)
			sys.exit(errno.EACCES)
			tweets_data=[]
			Count=0
		tweet = json.loads(data)
		tweets_data.append(tweet)
		return True

	def on_error(self, status):
		print (status)

if __name__ == '__main__':

	#This handles Twitter authetification and the connection to Twitter Streaming API
	l = StdOutListener()
	auth = OAuthHandler(consumer_key, consumer_secret)
	auth.set_access_token(access_token, access_token_secret)
	stream = Stream(auth, l)

	#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
	stream.filter(languages=["en"],track=['a', 'e', 'i','o','u','#'])


from __future__ import print_function

import os
import time
import json
import pickle
import sys
from itertools import product, combinations

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

from sklearn.preprocessing import LabelEncoder

from ruzicka.utilities import binarize
from ruzicka.vectorization import Vectorizer
from ruzicka.utilities import load_pan_dataset, train_dev_split, get_vocab_size
from sklearn.cross_validation import train_test_split
from ruzicka.score_shifting import ScoreShifter
from ruzicka.evaluation import pan_metrics
from ruzicka.Order2Verifier import Order2Verifier as Verifier
import ruzicka.art as art

# run script for top-5 metrics
ngram_type = 'word'
ngram_size = 1
base = 'profile'
vector_space = 'tf_std'
metric = 'cosine'
nb_bootstrap_iter = 100
rnd_prop = 0.5
nb_imposters = 30
mfi = sys.maxint
min_df = 2

# get imposter data:
train_data, _ = load_pan_dataset('../data/latin/dev') # ignore unknown documents
train_labels, train_documents = zip(*train_data)

# get test data:
test_data, _ = load_pan_dataset('../data/latin/test') # ignore unknown documents
test_labels, test_documents = zip(*test_data)

# fit encoder for author labels:
label_encoder = LabelEncoder()
label_encoder.fit(train_labels+test_labels)
train_ints = label_encoder.transform(train_labels)
test_ints = label_encoder.transform(test_labels)

# fit vectorizer:
vectorizer = Vectorizer(mfi = mfi,
                        vector_space = vector_space,
                        ngram_type = ngram_type,
                        ngram_size = ngram_size)
vectorizer.fit(train_documents+test_documents)
train_X = vectorizer.transform(train_documents).toarray()
test_X = vectorizer.transform(test_documents).toarray()

cols = ['label']
for test_author in sorted(set(test_ints)):
    auth_label = label_encoder.inverse_transform([test_author])[0]
    cols.append(auth_label)

proba_df = pd.DataFrame(columns=cols)

for idx in range(len(test_documents)):
    target_auth = test_ints[idx]
    target_docu = test_X[idx]
    non_target_test_ints = np.array([test_ints[i] for i in range(len(test_ints)) if i != idx])
    non_target_test_X = np.array([test_X[i] for i in range(len(test_ints)) if i != idx])
    tmp_train_X = np.vstack((train_X, non_target_test_X))
    tmp_train_y = np.hstack((train_ints, non_target_test_ints))

    tmp_test_X, tmp_test_y = [], []
    for t_auth in sorted(set(test_ints)):
        tmp_test_X.append(target_docu)
        tmp_test_y.append(t_auth)

    # fit the verifier:
    verifier = Verifier(metric = metric,
                        base = base,
                        nb_bootstrap_iter = nb_bootstrap_iter,
                        rnd_prop = rnd_prop)
    verifier.fit(tmp_train_X, tmp_train_y)
    probas = verifier.predict_proba(test_X = tmp_test_X,
                        test_y = tmp_test_y,
                        nb_imposters = nb_imposters)

    row = [label_encoder.inverse_transform([target_auth])[0]] # author label
    row += list(probas)
    print(row)
    proba_df.loc[len(proba_df)] = row

proba_df = proba_df.set_index('label')

# write away score tables:
table_dir = '../output/tables/'
if not os.path.isdir(table_dir):
    os.mkdir(table_dir)
proba_df.to_csv(table_dir+'lat_proba_'+metric+'_'+vector_space+'.csv')


# Author: Mathieu Blondel <mathieu@mblondel.org>
#         Arnaud Joly <a.joly@ulg.ac.be>
#         Maheshakya Wijewardena <maheshakya.10@cse.mrt.ac.lk>
# License: BSD 3 clause

import warnings
import numpy as np
import scipy.sparse as sp

from .base import BaseEstimator, ClassifierMixin, RegressorMixin
from .base import MultiOutputMixin
from .utils import check_random_state
from .utils.validation import _num_samples
from .utils.validation import check_array
from .utils.validation import check_consistent_length
from .utils.validation import check_is_fitted, _check_sample_weight
from .utils.random import _random_choice_csc
from .utils.stats import _weighted_percentile
from .utils.multiclass import class_distribution
from .utils.validation import _deprecate_positional_args

class DummyClassifier(MultiOutputMixin, ClassifierMixin, BaseEstimator):
    """
    DummyClassifier is a classifier that makes predictions using simple rules.

    This classifier is useful as a simple baseline to compare with other
    (real) classifiers. Do not use it for real problems.

    Read more in the :ref:`User Guide <dummy_estimators>`.

    .. versionadded:: 0.13

    Parameters
    ----------
    strategy : {"stratified", "most_frequent", "prior", "uniform", \
            "constant"}, default="prior"
        Strategy to use to generate predictions.

        * "stratified": generates predictions by respecting the training
          set's class distribution.
        * "most_frequent": always predicts the most frequent label in the
          training set.
        * "prior": always predicts the class that maximizes the class prior
          (like "most_frequent") and ``predict_proba`` returns the class prior.
        * "uniform": generates predictions uniformly at random.
        * "constant": always predicts a constant label that is provided by
          the user. This is useful for metrics that evaluate a non-majority
          class

          .. versionchanged:: 0.24
             The default value of `strategy` has changed to "prior" in version
             0.24.

    random_state : int, RandomState instance or None, default=None
        Controls the randomness to generate the predictions when
        ``strategy='stratified'`` or ``strategy='uniform'``.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    constant : int or str or array-like of shape (n_outputs,)
        The explicit constant as predicted by the "constant" strategy. This
        parameter is useful only for the "constant" strategy.

    Attributes
    ----------
    classes_ : ndarray of shape (n_classes,) or list thereof
        Class labels for each output.

    n_classes_ : int or list of int
        Number of label for each output.

    class_prior_ : ndarray of shape (n_classes,) or list thereof
        Probability of each class for each output.

    n_outputs_ : int
        Number of outputs.

    sparse_output_ : bool
        True if the array returned from predict is to be in sparse CSC format.
        Is automatically set to True if the input y is passed in sparse format.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.dummy import DummyClassifier
    >>> X = np.array([-1, 1, 1, 1])
    >>> y = np.array([0, 1, 1, 1])
    >>> dummy_clf = DummyClassifier(strategy="most_frequent")
    >>> dummy_clf.fit(X, y)
    DummyClassifier(strategy='most_frequent')
    >>> dummy_clf.predict(X)
    array([1, 1, 1, 1])
    >>> dummy_clf.score(X, y)
    0.75
    """
    @_deprecate_positional_args
    def __init__(self, *, strategy="prior", random_state=None,
                 constant=None):
        self.strategy = strategy
        self.random_state = random_state
        self.constant = constant

    def fit(self, X, y, sample_weight=None):
        """Fit the random classifier.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Target values.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        self : object
        """
        allowed_strategies = ("most_frequent", "stratified", "uniform",
                              "constant", "prior")

        if self.strategy not in allowed_strategies:
            raise ValueError("Unknown strategy type: %s, expected one of %s."
                             % (self.strategy, allowed_strategies))

        self._strategy = self.strategy

        if self._strategy == "uniform" and sp.issparse(y):
            y = y.toarray()
            warnings.warn('A local copy of the target data has been converted '
                          'to a numpy array. Predicting on sparse target data '
                          'with the uniform strategy would not save memory '
                          'and would be slower.',
                          UserWarning)

        self.sparse_output_ = sp.issparse(y)

        if not self.sparse_output_:
            y = np.asarray(y)
            y = np.atleast_1d(y)

        if y.ndim == 1:
            y = np.reshape(y, (-1, 1))

        self.n_outputs_ = y.shape[1]

        self.n_features_in_ = None  # No input validation is done for X

        check_consistent_length(X, y)

        if sample_weight is not None:
            sample_weight = _check_sample_weight(sample_weight, X)

        if self._strategy == "constant":
            if self.constant is None:
                raise ValueError("Constant target value has to be specified "
                                 "when the constant strategy is used.")
            else:
                constant = np.reshape(np.atleast_1d(self.constant), (-1, 1))
                if constant.shape[0] != self.n_outputs_:
                    raise ValueError("Constant target value should have "
                                     "shape (%d, 1)." % self.n_outputs_)

        (self.classes_,
         self.n_classes_,
         self.class_prior_) = class_distribution(y, sample_weight)

        if self._strategy == "constant":
            for k in range(self.n_outputs_):
                if not any(constant[k][0] == c for c in self.classes_[k]):
                    # Checking in case of constant strategy if the constant
                    # provided by the user is in y.
                    err_msg = ("The constant target value must be present in "
                               "the training data. You provided constant={}. "
                               "Possible values are: {}."
                               .format(self.constant, list(self.classes_[k])))
                    raise ValueError(err_msg)

        if self.n_outputs_ == 1:
            self.n_classes_ = self.n_classes_[0]
            self.classes_ = self.classes_[0]
            self.class_prior_ = self.class_prior_[0]

        return self

    def predict(self, X):
        """Perform classification on test vectors X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Test data.

        Returns
        -------
        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Predicted target values for X.
        """
        check_is_fitted(self)

        # numpy random_state expects Python int and not long as size argument
        # under Windows
        n_samples = _num_samples(X)
        rs = check_random_state(self.random_state)

        n_classes_ = self.n_classes_
        classes_ = self.classes_
        class_prior_ = self.class_prior_
        constant = self.constant
        if self.n_outputs_ == 1:
            # Get same type even for self.n_outputs_ == 1
            n_classes_ = [n_classes_]
            classes_ = [classes_]
            class_prior_ = [class_prior_]
            constant = [constant]
        # Compute probability only once
        if self._strategy == "stratified":
            proba = self.predict_proba(X)
            if self.n_outputs_ == 1:
                proba = [proba]

        if self.sparse_output_:
            class_prob = None
            if self._strategy in ("most_frequent", "prior"):
                classes_ = [np.array([cp.argmax()]) for cp in class_prior_]

            elif self._strategy == "stratified":
                class_prob = class_prior_

            elif self._strategy == "uniform":
                raise ValueError("Sparse target prediction is not "
                                 "supported with the uniform strategy")

            elif self._strategy == "constant":
                classes_ = [np.array([c]) for c in constant]

            y = _random_choice_csc(n_samples, classes_, class_prob,
                                   self.random_state)
        else:
            if self._strategy in ("most_frequent", "prior"):
                y = np.tile([classes_[k][class_prior_[k].argmax()] for
                             k in range(self.n_outputs_)], [n_samples, 1])

            elif self._strategy == "stratified":
                y = np.vstack([classes_[k][proba[k].argmax(axis=1)] for
                               k in range(self.n_outputs_)]).T

            elif self._strategy == "uniform":
                ret = [classes_[k][rs.randint(n_classes_[k], size=n_samples)]
                       for k in range(self.n_outputs_)]
                y = np.vstack(ret).T

            elif self._strategy == "constant":
                y = np.tile(self.constant, (n_samples, 1))

            if self.n_outputs_ == 1:
                y = np.ravel(y)

        return y

    def predict_proba(self, X):
        """
        Return probability estimates for the test vectors X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Test data.

        Returns
        -------
        P : ndarray of shape (n_samples, n_classes) or list thereof
            Returns the probability of the sample for each class in
            the model, where classes are ordered arithmetically, for each
            output.
        """
        check_is_fitted(self)

        # numpy random_state expects Python int and not long as size argument
        # under Windows
        n_samples = _num_samples(X)
        rs = check_random_state(self.random_state)

        n_classes_ = self.n_classes_
        classes_ = self.classes_
        class_prior_ = self.class_prior_
        constant = self.constant
        if self.n_outputs_ == 1:
            # Get same type even for self.n_outputs_ == 1
            n_classes_ = [n_classes_]
            classes_ = [classes_]
            class_prior_ = [class_prior_]
            constant = [constant]

        P = []
        for k in range(self.n_outputs_):
            if self._strategy == "most_frequent":
                ind = class_prior_[k].argmax()
                out = np.zeros((n_samples, n_classes_[k]), dtype=np.float64)
                out[:, ind] = 1.0
            elif self._strategy == "prior":
                out = np.ones((n_samples, 1)) * class_prior_[k]

            elif self._strategy == "stratified":
                out = rs.multinomial(1, class_prior_[k], size=n_samples)
                out = out.astype(np.float64)

            elif self._strategy == "uniform":
                out = np.ones((n_samples, n_classes_[k]), dtype=np.float64)
                out /= n_classes_[k]

            elif self._strategy == "constant":
                ind = np.where(classes_[k] == constant[k])
                out = np.zeros((n_samples, n_classes_[k]), dtype=np.float64)
                out[:, ind] = 1.0

            P.append(out)

        if self.n_outputs_ == 1:
            P = P[0]

        return P

    def predict_log_proba(self, X):
        """
        Return log probability estimates for the test vectors X.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data, requires length = n_samples

        Returns
        -------
        P : ndarray of shape (n_samples, n_classes) or list thereof
            Returns the log probability of the sample for each class in
            the model, where classes are ordered arithmetically for each
            output.
        """
        proba = self.predict_proba(X)
        if self.n_outputs_ == 1:
            return np.log(proba)
        else:
            return [np.log(p) for p in proba]

    def _more_tags(self):
        return {
            'poor_score': True, 'no_validation': True,
            '_xfail_checks': {
                'check_methods_subset_invariance':
                'fails for the predict method',
                'check_methods_sample_order_invariance':
                'fails for the predict method'
            }
        }

    def score(self, X, y, sample_weight=None):
        """Returns the mean accuracy on the given test data and labels.

        In multi-label classification, this is the subset accuracy
        which is a harsh metric since you require for each sample that
        each label set be correctly predicted.

        Parameters
        ----------
        X : None or array-like of shape (n_samples, n_features)
            Test samples. Passing None as test samples gives the same result
            as passing real test samples, since DummyClassifier
            operates independently of the sampled observations.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            True labels for X.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        score : float
            Mean accuracy of self.predict(X) wrt. y.

        """
        if X is None:
            X = np.zeros(shape=(len(y), 1))
        return super().score(X, y, sample_weight)

class DummyRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
    """
    DummyRegressor is a regressor that makes predictions using
    simple rules.

    This regressor is useful as a simple baseline to compare with other
    (real) regressors. Do not use it for real problems.

    Read more in the :ref:`User Guide <dummy_estimators>`.

    .. versionadded:: 0.13

    Parameters
    ----------
    strategy : {"mean", "median", "quantile", "constant"}, default="mean"
        Strategy to use to generate predictions.

        * "mean": always predicts the mean of the training set
        * "median": always predicts the median of the training set
        * "quantile": always predicts a specified quantile of the training set,
          provided with the quantile parameter.
        * "constant": always predicts a constant value that is provided by
          the user.

    constant : int or float or array-like of shape (n_outputs,), default=None
        The explicit constant as predicted by the "constant" strategy. This
        parameter is useful only for the "constant" strategy.

    quantile : float in [0.0, 1.0], default=None
        The quantile to predict using the "quantile" strategy. A quantile of
        0.5 corresponds to the median, while 0.0 to the minimum and 1.0 to the
        maximum.

    Attributes
    ----------
    constant_ : ndarray of shape (1, n_outputs)
        Mean or median or quantile of the training targets or constant value
        given by the user.

    n_outputs_ : int
        Number of outputs.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.dummy import DummyRegressor
    >>> X = np.array([1.0, 2.0, 3.0, 4.0])
    >>> y = np.array([2.0, 3.0, 5.0, 10.0])
    >>> dummy_regr = DummyRegressor(strategy="mean")
    >>> dummy_regr.fit(X, y)
    DummyRegressor()
    >>> dummy_regr.predict(X)
    array([5., 5., 5., 5.])
    >>> dummy_regr.score(X, y)
    0.0
    """
    @_deprecate_positional_args
    def __init__(self, *, strategy="mean", constant=None, quantile=None):
        self.strategy = strategy
        self.constant = constant
        self.quantile = quantile

    def fit(self, X, y, sample_weight=None):
        """Fit the random regressor.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Target values.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        self : object
        """
        allowed_strategies = ("mean", "median", "quantile", "constant")
        if self.strategy not in allowed_strategies:
            raise ValueError("Unknown strategy type: %s, expected one of %s."
                             % (self.strategy, allowed_strategies))

        y = check_array(y, ensure_2d=False)
        self.n_features_in_ = None  # No input validation is done for X
        if len(y) == 0:
            raise ValueError("y must not be empty.")

        if y.ndim == 1:
            y = np.reshape(y, (-1, 1))
        self.n_outputs_ = y.shape[1]

        check_consistent_length(X, y, sample_weight)

        if sample_weight is not None:
            sample_weight = _check_sample_weight(sample_weight, X)

        if self.strategy == "mean":
            self.constant_ = np.average(y, axis=0, weights=sample_weight)

        elif self.strategy == "median":
            if sample_weight is None:
                self.constant_ = np.median(y, axis=0)
            else:
                self.constant_ = [_weighted_percentile(y[:, k], sample_weight,
                                                       percentile=50.)
                                  for k in range(self.n_outputs_)]

        elif self.strategy == "quantile":
            if self.quantile is None or not np.isscalar(self.quantile):
                raise ValueError("Quantile must be a scalar in the range "
                                 "[0.0, 1.0], but got %s." % self.quantile)

            percentile = self.quantile * 100.0
            if sample_weight is None:
                self.constant_ = np.percentile(y, axis=0, q=percentile)
            else:
                self.constant_ = [_weighted_percentile(y[:, k], sample_weight,
                                                       percentile=percentile)
                                  for k in range(self.n_outputs_)]

        elif self.strategy == "constant":
            if self.constant is None:
                raise TypeError("Constant target value has to be specified "
                                "when the constant strategy is used.")

            self.constant = check_array(self.constant,
                                        accept_sparse=['csr', 'csc', 'coo'],
                                        ensure_2d=False, ensure_min_samples=0)

            if self.n_outputs_ != 1 and self.constant.shape[0] != y.shape[1]:
                raise ValueError(
                    "Constant target value should have "
                    "shape (%d, 1)." % y.shape[1])

            self.constant_ = self.constant

        self.constant_ = np.reshape(self.constant_, (1, -1))
        return self

    def predict(self, X, return_std=False):
        """
        Perform classification on test vectors X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Test data.

        return_std : bool, default=False
            Whether to return the standard deviation of posterior prediction.
            All zeros in this case.

            .. versionadded:: 0.20

        Returns
        -------
        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Predicted target values for X.

        y_std : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Standard deviation of predictive distribution of query points.
        """
        check_is_fitted(self)
        n_samples = _num_samples(X)

        y = np.full((n_samples, self.n_outputs_), self.constant_,
                    dtype=np.array(self.constant_).dtype)
        y_std = np.zeros((n_samples, self.n_outputs_))

        if self.n_outputs_ == 1:
            y = np.ravel(y)
            y_std = np.ravel(y_std)

        return (y, y_std) if return_std else y

    def _more_tags(self):
        return {'poor_score': True, 'no_validation': True}

    def score(self, X, y, sample_weight=None):
        """Returns the coefficient of determination R^2 of the prediction.

        The coefficient R^2 is defined as (1 - u/v), where u is the residual
        sum of squares ((y_true - y_pred) ** 2).sum() and v is the total
        sum of squares ((y_true - y_true.mean()) ** 2).sum().
        The best possible score is 1.0 and it can be negative (because the
        model can be arbitrarily worse). A constant model that always
        predicts the expected value of y, disregarding the input features,
        would get a R^2 score of 0.0.

        Parameters
        ----------
        X : None or array-like of shape (n_samples, n_features)
            Test samples. Passing None as test samples gives the same result
            as passing real test samples, since DummyRegressor
            operates independently of the sampled observations.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            True values for X.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        score : float
            R^2 of self.predict(X) wrt. y.
        """
        if X is None:
            X = np.zeros(shape=(len(y), 1))
        return super().score(X, y, sample_weight)


"""
==========
Libsvm GUI
==========

A simple graphical frontend for Libsvm mainly intended for didactic
purposes. You can create data points by point and click and visualize
the decision region induced by different kernels and parameter settings.

To create positive examples click the left mouse button; to create
negative examples click the right button.

If all examples are from the same class, it uses a one-class SVM.

"""
from __future__ import division, print_function

print(__doc__)

# Author: Peter Prettenhoer <peter.prettenhofer@gmail.com>
#
# License: BSD 3 clause

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib.contour import ContourSet

import Tkinter as Tk
import sys
import numpy as np

from sklearn import svm
from sklearn.datasets import dump_svmlight_file
from sklearn.externals.six.moves import xrange

y_min, y_max = -50, 50
x_min, x_max = -50, 50

class Model(object):
    """The Model which hold the data. It implements the
    observable in the observer pattern and notifies the
    registered observers on change event.
    """

    def __init__(self):
        self.observers = []
        self.surface = None
        self.data = []
        self.cls = None
        self.surface_type = 0

    def changed(self, event):
        """Notify the observers. """
        for observer in self.observers:
            observer.update(event, self)

    def add_observer(self, observer):
        """Register an observer. """
        self.observers.append(observer)

    def set_surface(self, surface):
        self.surface = surface

    def dump_svmlight_file(self, file):
        data = np.array(self.data)
        X = data[:, 0:2]
        y = data[:, 2]
        dump_svmlight_file(X, y, file)

class Controller(object):
    def __init__(self, model):
        self.model = model
        self.kernel = Tk.IntVar()
        self.surface_type = Tk.IntVar()
        # Whether or not a model has been fitted
        self.fitted = False

    def fit(self):
        print("fit the model")
        train = np.array(self.model.data)
        X = train[:, 0:2]
        y = train[:, 2]

        C = float(self.complexity.get())
        gamma = float(self.gamma.get())
        coef0 = float(self.coef0.get())
        degree = int(self.degree.get())
        kernel_map = {0: "linear", 1: "rbf", 2: "poly"}
        if len(np.unique(y)) == 1:
            clf = svm.OneClassSVM(kernel=kernel_map[self.kernel.get()],
                                  gamma=gamma, coef0=coef0, degree=degree)
            clf.fit(X)
        else:
            clf = svm.SVC(kernel=kernel_map[self.kernel.get()], C=C,
                          gamma=gamma, coef0=coef0, degree=degree)
            clf.fit(X, y)
        if hasattr(clf, 'score'):
            print("Accuracy:", clf.score(X, y) * 100)
        X1, X2, Z = self.decision_surface(clf)
        self.model.clf = clf
        self.model.set_surface((X1, X2, Z))
        self.model.surface_type = self.surface_type.get()
        self.fitted = True
        self.model.changed("surface")

    def decision_surface(self, cls):
        delta = 1
        x = np.arange(x_min, x_max + delta, delta)
        y = np.arange(y_min, y_max + delta, delta)
        X1, X2 = np.meshgrid(x, y)
        Z = cls.decision_function(np.c_[X1.ravel(), X2.ravel()])
        Z = Z.reshape(X1.shape)
        return X1, X2, Z

    def clear_data(self):
        self.model.data = []
        self.fitted = False
        self.model.changed("clear")

    def add_example(self, x, y, label):
        self.model.data.append((x, y, label))
        self.model.changed("example_added")

        # update decision surface if already fitted.
        self.refit()

    def refit(self):
        """Refit the model if already fitted. """
        if self.fitted:
            self.fit()

class View(object):
    """Test docstring. """
    def __init__(self, root, controller):
        f = Figure()
        ax = f.add_subplot(111)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_xlim((x_min, x_max))
        ax.set_ylim((y_min, y_max))
        canvas = FigureCanvasTkAgg(f, master=root)
        canvas.show()
        canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        canvas.mpl_connect('key_press_event', self.onkeypress)
        canvas.mpl_connect('key_release_event', self.onkeyrelease)
        canvas.mpl_connect('button_press_event', self.onclick)
        toolbar = NavigationToolbar2TkAgg(canvas, root)
        toolbar.update()
        self.shift_down = False
        self.controllbar = ControllBar(root, controller)
        self.f = f
        self.ax = ax
        self.canvas = canvas
        self.controller = controller
        self.contours = []
        self.c_labels = None
        self.plot_kernels()

    def plot_kernels(self):
        self.ax.text(-50, -60, "Linear: $u^T v$")
        self.ax.text(-20, -60, "RBF: $\exp (-\gamma \| u-v \|^2)$")
        self.ax.text(10, -60, "Poly: $(\gamma \, u^T v + r)^d$")

    def onkeypress(self, event):
        if event.key == "shift":
            self.shift_down = True

    def onkeyrelease(self, event):
        if event.key == "shift":
            self.shift_down = False

    def onclick(self, event):
        if event.xdata and event.ydata:
            if self.shift_down or event.button == 3:
                self.controller.add_example(event.xdata, event.ydata, -1)
            elif event.button == 1:
                self.controller.add_example(event.xdata, event.ydata, 1)

    def update_example(self, model, idx):
        x, y, l = model.data[idx]
        if l == 1:
            color = 'w'
        elif l == -1:
            color = 'k'
        self.ax.plot([x], [y], "%so" % color, scalex=0.0, scaley=0.0)

    def update(self, event, model):
        if event == "examples_loaded":
            for i in xrange(len(model.data)):
                self.update_example(model, i)

        if event == "example_added":
            self.update_example(model, -1)

        if event == "clear":
            self.ax.clear()
            self.ax.set_xticks([])
            self.ax.set_yticks([])
            self.contours = []
            self.c_labels = None
            self.plot_kernels()

        if event == "surface":
            self.remove_surface()
            self.plot_support_vectors(model.clf.support_vectors_)
            self.plot_decision_surface(model.surface, model.surface_type)

        self.canvas.draw()

    def remove_surface(self):
        """Remove old decision surface."""
        if len(self.contours) > 0:
            for contour in self.contours:
                if isinstance(contour, ContourSet):
                    for lineset in contour.collections:
                        lineset.remove()
                else:
                    contour.remove()
            self.contours = []

    def plot_support_vectors(self, support_vectors):
        """Plot the support vectors by placing circles over the
        corresponding data points and adds the circle collection
        to the contours list."""
        cs = self.ax.scatter(support_vectors[:, 0], support_vectors[:, 1],
                             s=80, edgecolors="k", facecolors="none")
        self.contours.append(cs)

    def plot_decision_surface(self, surface, type):
        X1, X2, Z = surface
        if type == 0:
            levels = [-1.0, 0.0, 1.0]
            linestyles = ['dashed', 'solid', 'dashed']
            colors = 'k'
            self.contours.append(self.ax.contour(X1, X2, Z, levels,
                                                 colors=colors,
                                                 linestyles=linestyles))
        elif type == 1:
            self.contours.append(self.ax.contourf(X1, X2, Z, 10,
                                                  cmap=matplotlib.cm.bone,
                                                  origin='lower', alpha=0.85))
            self.contours.append(self.ax.contour(X1, X2, Z, [0.0], colors='k',
                                                 linestyles=['solid']))
        else:
            raise ValueError("surface type unknown")

class ControllBar(object):
    def __init__(self, root, controller):
        fm = Tk.Frame(root)
        kernel_group = Tk.Frame(fm)
        Tk.Radiobutton(kernel_group, text="Linear", variable=controller.kernel,
                       value=0, command=controller.refit).pack(anchor=Tk.W)
        Tk.Radiobutton(kernel_group, text="RBF", variable=controller.kernel,
                       value=1, command=controller.refit).pack(anchor=Tk.W)
        Tk.Radiobutton(kernel_group, text="Poly", variable=controller.kernel,
                       value=2, command=controller.refit).pack(anchor=Tk.W)
        kernel_group.pack(side=Tk.LEFT)

        valbox = Tk.Frame(fm)
        controller.complexity = Tk.StringVar()
        controller.complexity.set("1.0")
        c = Tk.Frame(valbox)
        Tk.Label(c, text="C:", anchor="e", width=7).pack(side=Tk.LEFT)
        Tk.Entry(c, width=6, textvariable=controller.complexity).pack(
            side=Tk.LEFT)
        c.pack()

        controller.gamma = Tk.StringVar()
        controller.gamma.set("0.01")
        g = Tk.Frame(valbox)
        Tk.Label(g, text="gamma:", anchor="e", width=7).pack(side=Tk.LEFT)
        Tk.Entry(g, width=6, textvariable=controller.gamma).pack(side=Tk.LEFT)
        g.pack()

        controller.degree = Tk.StringVar()
        controller.degree.set("3")
        d = Tk.Frame(valbox)
        Tk.Label(d, text="degree:", anchor="e", width=7).pack(side=Tk.LEFT)
        Tk.Entry(d, width=6, textvariable=controller.degree).pack(side=Tk.LEFT)
        d.pack()

        controller.coef0 = Tk.StringVar()
        controller.coef0.set("0")
        r = Tk.Frame(valbox)
        Tk.Label(r, text="coef0:", anchor="e", width=7).pack(side=Tk.LEFT)
        Tk.Entry(r, width=6, textvariable=controller.coef0).pack(side=Tk.LEFT)
        r.pack()
        valbox.pack(side=Tk.LEFT)

        cmap_group = Tk.Frame(fm)
        Tk.Radiobutton(cmap_group, text="Hyperplanes",
                       variable=controller.surface_type, value=0,
                       command=controller.refit).pack(anchor=Tk.W)
        Tk.Radiobutton(cmap_group, text="Surface",
                       variable=controller.surface_type, value=1,
                       command=controller.refit).pack(anchor=Tk.W)

        cmap_group.pack(side=Tk.LEFT)

        train_button = Tk.Button(fm, text='Fit', width=5,
                                 command=controller.fit)
        train_button.pack()
        fm.pack(side=Tk.LEFT)
        Tk.Button(fm, text='Clear', width=5,
                  command=controller.clear_data).pack(side=Tk.LEFT)

def get_parser():
    from optparse import OptionParser
    op = OptionParser()
    op.add_option("--output",
                  action="store", type="str", dest="output",
                  help="Path where to dump data.")
    return op

def main(argv):
    op = get_parser()
    opts, args = op.parse_args(argv[1:])
    root = Tk.Tk()
    model = Model()
    controller = Controller(model)
    root.wm_title("Scikit-learn Libsvm GUI")
    view = View(root, controller)
    model.add_observer(view)
    Tk.mainloop()

    if opts.output:
        model.dump_svmlight_file(opts.output)

if __name__ == "__main__":
    main(sys.argv)


"""
Testing for the partial dependence module.
"""

import numpy as np
from numpy.testing import assert_array_equal

from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import if_matplotlib
from sklearn.ensemble.partial_dependence import partial_dependence
from sklearn.ensemble.partial_dependence import plot_partial_dependence
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import GradientBoostingRegressor
from sklearn import datasets

# toy sample
X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]
y = [-1, -1, -1, 1, 1, 1]
T = [[-1, -1], [2, 2], [3, 2]]
true_result = [-1, 1, 1]

# also load the boston dataset
boston = datasets.load_boston()

# also load the iris dataset
iris = datasets.load_iris()

def test_partial_dependence_classifier():
    """Test partial dependence for classifier """
    clf = GradientBoostingClassifier(n_estimators=10, random_state=1)
    clf.fit(X, y)

    pdp, axes = partial_dependence(clf, [0], X=X, grid_resolution=5)

    # only 4 grid points instead of 5 because only 4 unique X[:,0] vals
    assert pdp.shape == (1, 4)
    assert axes[0].shape[0] == 4

    # now with our own grid
    X_ = np.asarray(X)
    grid = np.unique(X_[:, 0])
    pdp_2, axes = partial_dependence(clf, [0], grid=grid)

    assert axes is None
    assert_array_equal(pdp, pdp_2)

def test_partial_dependence_multiclass():
    """Test partial dependence for multi-class classifier """
    clf = GradientBoostingClassifier(n_estimators=10, random_state=1)
    clf.fit(iris.data, iris.target)

    grid_resolution = 25
    n_classes = clf.n_classes_
    pdp, axes = partial_dependence(
        clf, [0], X=iris.data, grid_resolution=grid_resolution)

    assert pdp.shape == (n_classes, grid_resolution)
    assert len(axes) == 1
    assert axes[0].shape[0] == grid_resolution

def test_partial_dependence_regressor():
    """Test partial dependence for regressor """
    clf = GradientBoostingRegressor(n_estimators=10, random_state=1)
    clf.fit(boston.data, boston.target)

    grid_resolution = 25
    pdp, axes = partial_dependence(
        clf, [0], X=boston.data, grid_resolution=grid_resolution)

    assert pdp.shape == (1, grid_resolution)
    assert axes[0].shape[0] == grid_resolution

def test_partial_dependecy_input():
    """Test input validation of partial dependence. """
    clf = GradientBoostingClassifier(n_estimators=10, random_state=1)
    clf.fit(X, y)

    assert_raises(ValueError, partial_dependence,
                  clf, [0], grid=None, X=None)

    assert_raises(ValueError, partial_dependence,
                  clf, [0], grid=[0, 1], X=X)

    # first argument must be an instance of BaseGradientBoosting
    assert_raises(ValueError, partial_dependence,
                  {}, [0], X=X)

    # Gradient boosting estimator must be fit
    assert_raises(ValueError, partial_dependence,
                  GradientBoostingClassifier(), [0], X=X)

    assert_raises(ValueError, partial_dependence, clf, [-1], X=X)

    assert_raises(ValueError, partial_dependence, clf, [100], X=X)

    # wrong ndim for grid
    grid = np.random.rand(10, 2, 1)
    assert_raises(ValueError, partial_dependence, clf, [0], grid=grid)

@if_matplotlib
def test_plot_partial_dependence():
    """Test partial dependence plot function. """
    clf = GradientBoostingRegressor(n_estimators=10, random_state=1)
    clf.fit(boston.data, boston.target)

    grid_resolution = 25
    fig, axs = plot_partial_dependence(clf, boston.data, [0, 1, (0, 1)],
                                       grid_resolution=grid_resolution,
                                       feature_names=boston.feature_names)
    assert len(axs) == 3
    assert all(ax.has_data for ax in axs)

    # check with str features and array feature names
    fig, axs = plot_partial_dependence(clf, boston.data, ['CRIM', 'ZN',
                                                          ('CRIM', 'ZN')],
                                       grid_resolution=grid_resolution,
                                       feature_names=boston.feature_names)

    assert len(axs) == 3
    assert all(ax.has_data for ax in axs)

    # check with list feature_names
    feature_names = boston.feature_names.tolist()
    fig, axs = plot_partial_dependence(clf, boston.data, ['CRIM', 'ZN',
                                                          ('CRIM', 'ZN')],
                                       grid_resolution=grid_resolution,
                                       feature_names=feature_names)
    assert len(axs) == 3
    assert all(ax.has_data for ax in axs)

@if_matplotlib
def test_plot_partial_dependence_input():
    """Test partial dependence plot function input checks. """
    clf = GradientBoostingClassifier(n_estimators=10, random_state=1)

    # not fitted yet
    assert_raises(ValueError, plot_partial_dependence,
                  clf, X, [0])

    clf.fit(X, y)

    assert_raises(ValueError, plot_partial_dependence,
                  clf, np.array(X)[:, :0], [0])

    # first argument must be an instance of BaseGradientBoosting
    assert_raises(ValueError, plot_partial_dependence,
                  {}, X, [0])

    # must be larger than -1
    assert_raises(ValueError, plot_partial_dependence,
                  clf, X, [-1])

    # too large feature value
    assert_raises(ValueError, plot_partial_dependence,
                  clf, X, [100])

    # str feature but no feature_names
    assert_raises(ValueError, plot_partial_dependence,
                  clf, X, ['foobar'])

    # not valid features value
    assert_raises(ValueError, plot_partial_dependence,
                  clf, X, [{'foo': 'bar'}])

@if_matplotlib
def test_plot_partial_dependence_multiclass():
    """Test partial dependence plot function on multi-class input. """
    clf = GradientBoostingClassifier(n_estimators=10, random_state=1)
    clf.fit(iris.data, iris.target)

    grid_resolution = 25
    fig, axs = plot_partial_dependence(clf, iris.data, [0, 1],
                                       label=0,
                                       grid_resolution=grid_resolution)
    assert len(axs) == 2
    assert all(ax.has_data for ax in axs)

    # now with symbol labels
    target = iris.target_names[iris.target]
    clf = GradientBoostingClassifier(n_estimators=10, random_state=1)
    clf.fit(iris.data, target)

    grid_resolution = 25
    fig, axs = plot_partial_dependence(clf, iris.data, [0, 1],
                                       label='setosa',
                                       grid_resolution=grid_resolution)
    assert len(axs) == 2
    assert all(ax.has_data for ax in axs)

    # label not in gbrt.classes_
    assert_raises(ValueError, plot_partial_dependence,
                  clf, iris.data, [0, 1], label='foobar',
                  grid_resolution=grid_resolution)

    # label not provided
    assert_raises(ValueError, plot_partial_dependence,
                  clf, iris.data, [0, 1],
                  grid_resolution=grid_resolution)


"""
===========================================
Comparison of F-test and mutual information
===========================================

This example illustrates the differences between univariate F-test statistics
and mutual information.

We consider 3 features x_1, x_2, x_3 distributed uniformly over [0, 1], the
target depends on them as follows:

y = x_1 + sin(6 * pi * x_2) + 0.1 * N(0, 1), that is the third features is completely irrelevant.

The code below plots the dependency of y against individual x_i and normalized
values of univariate F-tests statistics and mutual information.

As F-test captures only linear dependency, it rates x_1 as the most
discriminative feature. On the other hand, mutual information can capture any
kind of dependency between variables and it rates x_2 as the most
discriminative feature, which probably agrees better with our intuitive
perception for this example. Both methods correctly marks x_3 as irrelevant.
"""
print(__doc__)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.feature_selection import f_regression, mutual_info_regression

np.random.seed(0)
X = np.random.rand(1000, 3)
y = X[:, 0] + np.sin(6 * np.pi * X[:, 1]) + 0.1 * np.random.randn(1000)

f_test, _ = f_regression(X, y)
f_test /= np.max(f_test)

mi = mutual_info_regression(X, y)
mi /= np.max(mi)

plt.figure(figsize=(15, 5))
for i in range(3):
    plt.subplot(1, 3, i + 1)
    plt.scatter(X[:, i], y)
    plt.xlabel("$x_{}$".format(i + 1), fontsize=14)
    if i == 0:
        plt.ylabel("$y$", fontsize=14)
    plt.title("F-test={:.2f}, MI={:.2f}".format(f_test[i], mi[i]),
              fontsize=16)
plt.show()


import numpy as np
import pytest

from pandas.compat import PYPY

import pandas as pd
from pandas import MultiIndex
import pandas.util.testing as tm

def test_contains_top_level():
    midx = MultiIndex.from_product([["A", "B"], [1, 2]])
    assert "A" in midx
    assert "A" not in midx._engine

def test_contains_with_nat():
    # MI with a NaT
    mi = MultiIndex(
        levels=[["C"], pd.date_range("2012-01-01", periods=5)],
        codes=[[0, 0, 0, 0, 0, 0], [-1, 0, 1, 2, 3, 4]],
        names=[None, "B"],
    )
    assert ("C", pd.Timestamp("2012-01-01")) in mi
    for val in mi.values:
        assert val in mi

def test_contains(idx):
    assert ("foo", "two") in idx
    assert ("bar", "two") not in idx
    assert None not in idx

@pytest.mark.skipif(not PYPY, reason="tuples cmp recursively on PyPy")
def test_isin_nan_pypy():
    idx = MultiIndex.from_arrays([["foo", "bar"], [1.0, np.nan]])
    tm.assert_numpy_array_equal(idx.isin([("bar", np.nan)]), np.array([False, True]))
    tm.assert_numpy_array_equal(
        idx.isin([("bar", float("nan"))]), np.array([False, True])
    )

def test_isin():
    values = [("foo", 2), ("bar", 3), ("quux", 4)]

    idx = MultiIndex.from_arrays([["qux", "baz", "foo", "bar"], np.arange(4)])
    result = idx.isin(values)
    expected = np.array([False, False, True, True])
    tm.assert_numpy_array_equal(result, expected)

    # empty, return dtype bool
    idx = MultiIndex.from_arrays([[], []])
    result = idx.isin(values)
    assert len(result) == 0
    assert result.dtype == np.bool_

@pytest.mark.skipif(PYPY, reason="tuples cmp recursively on PyPy")
def test_isin_nan_not_pypy():
    idx = MultiIndex.from_arrays([["foo", "bar"], [1.0, np.nan]])
    tm.assert_numpy_array_equal(idx.isin([("bar", np.nan)]), np.array([False, False]))
    tm.assert_numpy_array_equal(
        idx.isin([("bar", float("nan"))]), np.array([False, False])
    )

def test_isin_level_kwarg():
    idx = MultiIndex.from_arrays([["qux", "baz", "foo", "bar"], np.arange(4)])

    vals_0 = ["foo", "bar", "quux"]
    vals_1 = [2, 3, 10]

    expected = np.array([False, False, True, True])
    tm.assert_numpy_array_equal(expected, idx.isin(vals_0, level=0))
    tm.assert_numpy_array_equal(expected, idx.isin(vals_0, level=-2))

    tm.assert_numpy_array_equal(expected, idx.isin(vals_1, level=1))
    tm.assert_numpy_array_equal(expected, idx.isin(vals_1, level=-1))

    msg = "Too many levels: Index has only 2 levels, not 6"
    with pytest.raises(IndexError, match=msg):
        idx.isin(vals_0, level=5)
    msg = "Too many levels: Index has only 2 levels, -5 is not a valid level number"
    with pytest.raises(IndexError, match=msg):
        idx.isin(vals_0, level=-5)

    with pytest.raises(KeyError, match=r"'Level 1\.0 not found'"):
        idx.isin(vals_0, level=1.0)
    with pytest.raises(KeyError, match=r"'Level -1\.0 not found'"):
        idx.isin(vals_1, level=-1.0)
    with pytest.raises(KeyError, match="'Level A not found'"):
        idx.isin(vals_1, level="A")

    idx.names = ["A", "B"]
    tm.assert_numpy_array_equal(expected, idx.isin(vals_0, level="A"))
    tm.assert_numpy_array_equal(expected, idx.isin(vals_1, level="B"))

    with pytest.raises(KeyError, match="'Level C not found'"):
        idx.isin(vals_1, level="C")


#!/usr/bin/env python

import codecs
import os
import sys
import glob

from setuptools import setup, find_packages
try:
# http://stackoverflow.com/questions/21698004/python-behave-integration-in-setuptools-setup-py
    from setuptools_behave import behave_test
except ImportError:
    behave_test = None

dirname = os.path.dirname(__file__)

long_description = (
    codecs.open(os.path.join(dirname, "README.creole"), encoding="utf-8").read() + "\n"
    )

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["zmq"], "excludes": ["tkinter"]}

setup(
    name="OpenTrader",
    description="OpenTrader",
    long_description=long_description,
    author="Open Trading",
    license="LGPL2 license",
    url="https://www.github.com/OpenTrading/OpenTrader",
    version='1.0',
    classifiers=[
        "Development Status :: 2 - Pre-Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: LGPL2 License",
        "Operating System :: POSIX",
        "Operating System :: Microsoft :: Windows",
        "Operating System :: MacOS :: MacOS X",
        "Topic :: Office/Business :: Financial :: Investment",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Programming Language :: Python :: 2",
    ] + [("Programming Language :: Python :: %s" % x) for x in "2.6 2.7".split()],
    install_requires=[
        "configobj",
        "pandas",
        "pyparsing",
        # we'll make zmq default now
        "zmq",
        ],
    extras_require={'plotting': ["matplotlib"],
                    'pybacktest': ["pybacktest"],
                    'rabbit': ["pyrabbit"],
                    'doc': ["python-creole", "invoke"],
                    # we'll make zmq default now
                    # 'zmq': ["zmq"],
                    'amqp': ["pika"],
                    },
    data_files=[('', ['README.creole']),
                ('OpenTrader', glob.glob('OpenTrader/*.ini')),
                ('OpenTrader/Omlettes', glob.glob('OpenTrader/Omlettes/*.ini'))],

    options = {"build_exe": build_exe_options},
    entry_points={
        "console_scripts": [
            "OTCmd2 = OpenTrader.OTCmd2:iMain",
            "OTBackTest = OpenTrader.OTBackTest:iMain",
            "OTPpnAmgc = OpenTrader.OTPpnAmgc:iMain",
        ]
    },
    tests_require=["behave>=1.2.5"],
    cmdclass=behave_test and {"behave_test": behave_test,} or {},
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
)