text:
<file_sep># Purfect-Match
The web page for matching cats with people

branch_name: refs/heads/master

repo_language: Markdown

repo_name: VasiMva/Purfect-Match


text: <file_sep># Ishanchaudhary.github.io

branch_name: refs/heads/main


text:
<file_sep>## Ad-hoc tau ID training with sklearn using ROOT trees as input
# Requires root_numpy https://github.com/rootpy/root_numpy
# <NAME> 27 Aug 2015
import numpy as np
import time

import xgboost as xgb
from root_numpy import root2array, root2rec

def trainVars():
    return [
    'mt',
    'n_jets',
    'met_pt',
    'pthiggs',
    'vbf_mjj',
    'vbf_deta',
    'vbf_n_central',
    'l2_pt',
    'l1_pt',
    'svfit_transverse_mass',
    'delta_phi_l1_l2',
    'delta_eta_l1_l2',
    'svfit_mass'
    ]

files_sig = [
    'data/inclusive_HiggsVBF125_weight.root',
    'data/inclusive_HiggsGGH125_weight.root'
]

files_ZTT = [
    # 'data/inclusive_HiggsGGH125_weight.root'
    'data/inclusive_ZTT_weight.root',
    'data/inclusive_ZTTM10_weight.root',
]

#files_signal = files_sig + files_ZTT

files_bg = [
'data/inclusive_TBarToLeptons_tch_powheg_weight.root',
'data/inclusive_TBar_tWch_weight.root',
'data/inclusive_TT_weight.root',
'data/inclusive_TToLeptons_tch_powheg_weight.root',
'data/inclusive_T_tWch_weight.root',
'data/inclusive_VVTo2L2Nu_weight.root',
'data/inclusive_W1Jets_weight.root',
'data/inclusive_W2Jets_weight.root',
'data/inclusive_W3Jets_weight.root',
'data/inclusive_W4Jets_weight.root',
'data/inclusive_WWTo1L1Nu2Q_weight.root',
'data/inclusive_WZTo1L1Nu2Q_weight.root',
'data/inclusive_WZTo1L3Nu_weight.root',
'data/inclusive_WZTo2L2Q_weight.root',
'data/inclusive_WZTo3L_weight.root',
'data/inclusive_W_weight.root',
'data/inclusive_ZJM10_weight.root',
'data/inclusive_ZJ_weight.root',
'data/inclusive_ZLM10_weight.root',
'data/inclusive_ZL_weight.root',
# 'data/inclusive_ZTTM10_weight.root',
# 'data/inclusive_ZTT_weight.root',
'data/inclusive_ZZTo2L2Q_weight.root',
'data/inclusive_ZZTo4L_weight.root',
# 'data/inclusive_data_obs_weight.root',
]

vals = np.zeros(4)

#print 'How many classes would you like?'
#num = input("Enter a value between 2 and 4 (inclusive): ")
num = 2

if num == 2:
    vals = [1, 0, 0, 0]

elif num == 3:
    vals = [2, 1, 0, 0]

elif num == 4:
    vals = [3, 2, 1, 0]

else:
    print 'Invalid entry. Please try again.'
    exit()

with open("XG_BoostTest1.1", "w") as text_file:
    text_file.write("XGBoost Optimization w/ 1000 Trees Unweighted\n\n")
    text_file.write("Learn Rate\tMax Depth\tN_Est.\tBest Cut\tBest S\tBest B\tMax AMS\n\n")

#selection = 'vbf_mjj > 400. && vbf_deta > 3.0'
#selection = '!(vbf_mjj > 400. && vbf_deta > 3.0)'
selection = '1'

def startClock():
    global startTime
    startTime=time.time()

def timer():

    RunTime=time.time()-startTime

    if RunTime >= 5:
        raise StopTraining()

    return

def sigmoid(x):
  return 1./(1. + np.exp(-x))

def AMS(s, b, b_r=10.):
    return np.sqrt(2.0*((s+b+b_r)*np.log(1.0+s/(b+b_r))-s))

def logregobj(preds, dtrain):
    y = dtrain.get_label()
    weight = dtrain.get_weight()
    # yhat = sigmoid(preds)

    max_ams = 0.
    best_cutoff = 0.
    for cutoff in np.arange(0., 0.1, 0.005):
        yhat = preds > cutoff    # approx ams using soft probability instead of hard predicted class
        # import pdb; pdb.set_trace()
        s = np.sum( weight * y * yhat )
        b = np.sum( weight * (1.-y) * yhat )
        # negative it since in xgboost, we minimize the loss
        ams = AMS(s, b)
        if ams > max_ams:
            max_ams = ams
            best_cutoff = cutoff
    # bReg = 10.0
    # tmp = 1.0 + s/(b + bReg)
    # # ds / dyhat
    # ds = np.log(tmp)/ams
    # # db / dyhat
    # db = (np.log(tmp) + 1 - tmp)/ams
    # # d(-ams) / dyhat
    # #grad = -(ds*(yhat-y)+db*(1-(yhat-y)))# @phunter's version
    # grad = (ds * y + db * (1.-y)) * weight * yhat * (1.-yhat) # @George's version
    # hess = np.ones(yhat.shape)/(10**3) #constant
    # import pdb; pdb.set_trace()
    return 'max_ams@{c}'.format(c=best_cutoff), max_ams #, 1.#grad, hess[0]

def trainXGB(data, target, weight, UNweight, ntrees=1000, extraW=1.):

    nCrossVal = 2
    # kf = KFold(len(training_data), nCrossVal, shuffle=True, random_state=1)

    train_indices = np.where((data[:, 10]*1000.).astype(int)%2==1)
    test_indices = np.where((data[:, 10]*1000.).astype(int)%2==0)

    kf = [(train_indices, test_indices), (test_indices, train_indices)]

    # trainIndices = training_data[abs(int(training_data[10]*1000.))%2 == 0]
    # testIndices = training_data[abs(int(training_data[10]*1000.))%2 == 1]

    print 'Cross-validation:', nCrossVal, 'folds'

    for i_fold, (trainIndices, testIndices) in enumerate(kf):

        startClock()

        print 'Starting fold'

        staged_maxAMS = []
        staged_bestS = []
        staged_bestB = []
        staged_bestCutoffs = []
        staged_nTrees = []

        d_train = data[trainIndices]
        d_test = data[testIndices]

        t_train = target[trainIndices]
        global t_test
        t_test = target[testIndices]

        w_train = weight[trainIndices]
        w_test = weight[testIndices]

        w_trainUN = UNweights[trainIndices]
        global w_testUN
        w_testUN = UNweights[testIndices]

        # construct xgboost.DMatrix from numpy array, treat -999.0 as missing value
        xgmat = xgb.DMatrix(d_train, label=t_train, missing=-999.0, weight=w_trainUN)
        xgmat_test = xgb.DMatrix(d_test, label=t_test, missing=-999.0, weight=w_testUN)

        # setup parameters for xgboost
        param = {}
        # use logistic regression loss, use raw prediction before logistic transformation
        # since we only need the rank
        param['objective'] = 'binary:logistic'
        # scale weight of positive examples
        param['scale_pos_weight'] = extraW
        param['eta'] = eta
        param['max_depth'] = max_depth
        param['eval_metric'] = 'auc' # You may be able to use ams here
        param['silent'] = 1
        param['nthread'] = 1
        param['subsample'] = 0.5
        # param['colsample_bytree'] = 0.5
        param['min_child_weight'] = 0.1

        # you can directly throw param in, though we want to watch multiple metrics here
        plst = list(param.items())#+[('eval_metric', 'ams@0.145')]

        test_mat = xgb.DMatrix(d_test, missing=-999.0)
        watchlist = [(xgmat, 'train'), (xgmat_test, 'test')]
        # boost 120 tres
        # num_round = 10

        bst = xgb.train(plst, xgmat, ntrees, watchlist, feval=logregobj)

        scores = [bst.predict(test_mat)]

        for i in range(0,ntrees):
            if i%100 == 0:
                bst.update(xgmat, i)
                scores = [bst.predict(test_mat)]
                best_cutoff, best_S, best_B, max_AMS = calculateAMS(scores)

                print max_AMS
                staged_bestCutoffs.append(best_cutoff)
                staged_bestB.append(best_B)
                staged_bestS.append(best_S)
                staged_maxAMS.append(max_AMS)

                staged_nTrees.append(i+1)

        # save out model
        bst.save_model('higgs.model')

        max_AMS = max(staged_maxAMS)
        argmax_AMS = staged_maxAMS.index(max_AMS)
        best_cutoff = staged_bestCutoffs[argmax_AMS]
        best_n_for_AMS = staged_nTrees[argmax_AMS]
        best_B = staged_bestB[argmax_AMS]
        best_S = staged_bestS[argmax_AMS]

        print 'BEST N FOR AMS', best_n_for_AMS
        print 'BEST CUTOFF ', best_cutoff
        print 'MAX AMS ', max_AMS, '\n\n'

        with open("XG_BoostTest1.1", "a") as text_file:
            text_file.write("%f\t%f\t%f\t%f\t%f\t%f\t%f\n" % (eta, max_depth, best_n_for_AMS, best_cutoff, best_S,best_B, max_AMS))

    return bst

def calculateAMS(scores):

    n_cutoffs=100
    cutoffs=np.arange(n_cutoffs)
    AMS=np.zeros(n_cutoffs)
    S=np.zeros(n_cutoffs)
    B=np.zeros(n_cutoffs)
    cutoffs=np.multiply(cutoffs,1/float(n_cutoffs))
    j=0

    for cutoff in cutoffs:
        is_htt = np.zeros(len(t_test))
        is_htt[np.where(scores[0]>cutoff)]=1

        temp1=np.multiply(w_testUN, t_test)
        s=np.multiply(is_htt,temp1)

        temp2=np.multiply(w_testUN, (1-t_test))
        b=np.multiply(is_htt,temp2)

        S[j]=np.sum(s)
        B[j]=np.sum(b)
        AMS[j]=np.sqrt(2.0*((S[j]+B[j]+10.0)*np.log(1.0+S[j]/(B[j]+10.0))-S[j]))

        j=j+1

    best_cutoff=cutoffs[AMS.argmax()]
    best_S=S[AMS.argmax()]
    best_B=B[AMS.argmax()]
    max_AMS=np.amax(AMS)

    return best_cutoff, best_S, best_B, max_AMS

def readFiles():
    print 'Reading files...'

    # weightsS = root2rec(files_signal, treename='tree', branches=['weight'], selection=selection)
    weights_sig = root2rec(files_sig, treename='tree', branches=['full_weight'], selection=selection)['full_weight']
    weights_ZTT = root2rec(files_ZTT, treename='tree', branches=['full_weight'], selection=selection)['full_weight']
    weightsB = root2rec(files_bg, treename='tree', branches=['full_weight'], selection=selection)['full_weight']

    sum_weights_sig = np.sum(weights_sig)
    sum_weights_ZTT = np.sum(weights_ZTT)
    sum_weightsB = np.sum(weightsB)

    normWeights_ZTT = weights_ZTT * sum_weights_sig/sum_weights_ZTT
    normWeightsB = weightsB * sum_weights_sig/sum_weightsB

    # nS = len(weightsS)
    n_sig = len(weights_sig)
    n_ZTT = len(weights_ZTT)
    nB = len(weightsB)

    # fullWeight = np.concatenate((weightsS, weightsB))
    fullWeight = np.concatenate((weights_sig, normWeights_ZTT, normWeightsB))
    unNormFullWeight = np.concatenate((weights_sig, weights_ZTT, weightsB))
    # fullWeight = fullWeight['weight']

    # fullWeight = np.ones(len(fullWeight))

    # del weightsS, weightsB

    # arrSB = root2array(files_signal + files_bg, treename='tree', branches=trainVars(), selection=selection)
    arrSB = root2array(files_sig + files_ZTT + files_bg, treename='tree', branches=trainVars(), selection=selection)

    # Need a matrix-like array instead of a 1-D array of lists for sklearn
    arrSB = (np.asarray([arrSB[var] for var in trainVars()])).transpose()

    # targets = np.concatenate((np.ones(nS),np.zeros(nB)))
    # targets = np.concatenate((np.ones(n_sig)*2, np.ones(n_ZTT),np.zeros(nB)))
    targets = np.concatenate((np.ones(n_sig)*vals[0], np.ones(n_ZTT)*vals[1], np.ones(nB)*vals[2]))

    print 'Done reading files.'

    #import pdb; pdb.set_trace()

    return arrSB, fullWeight, unNormFullWeight, targets

etas = [0.025, 0.05, 0.1]
max_depths = [4,6,8]

global eta
global max_depth

for i in etas:
    for j in max_depths:

        eta=i
        max_depth=j

        if __name__ == '__main__':

            classifier = 'GBRT' # 'Ada' #'GBRT'
            doTrain = True

            print 'Read training and test files...'
            training, weights, UNweights, targets = readFiles()

            print 'Sizes'
            print training.nbytes, weights.nbytes, targets.nbytes

            if doTrain:
                print 'Start training'

                if classifier == 'GBRT':
                    trainXGB(training, targets, weights, UNweights)

<file_sep>## Ad-hoc tau ID training with sklearn using ROOT trees as input
# Requires root_numpy https://github.com/rootpy/root_numpy
# <NAME> 27 Aug 2015

import numpy as np

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier

# from sklearn.cross_validation import train_test_split #cross_val_score
from sklearn.cross_validation import KFold

from sklearn.metrics import roc_curve

# For model I/O
from sklearn.externals import joblib

from root_numpy import root2array, root2rec

def trainVars():
    return [
# 'mvis', 'mt2', 'l1_pt', 'l2_pt', 'delta_phi_l1_l2', 'delta_eta_l1_l2', 'met_pt', 'mt_total', 'mt_sum', 'pzeta_vis', 'pzeta_met', 'l2_mt', 'mt', 'pzeta_disc', 'pthiggs', 'jet1_pt', 'n_jets', 'pt_l1l2'
'mt2', 'delta_phi_l1_l2', 'delta_eta_l1_l2', 'mt', 'l2_mt', 'pzeta_disc', 'pt_l1l2'
    ]

files_signal = [
    'data_tt/susy_mtsum200_SMS_TStauStauMStau150MChi50.root',
    'data_tt/susy_mtsum200_SMS_TStauStauMStau150MChi20.root',
    'data_tt/susy_mtsum200_SMS_TStauStauMStau150MChi100.root',
    'data_tt/susy_mtsum200_SMS_TStauStauMStau150MChi10.root',
    'data_tt/susy_mtsum200_SMS_TStauStauMStau150MChi1.root',
    'data_tt/susy_mtsum200_SMS_TStauStauMStau100MChi1.root',
    'data_tt/susy_mtsum200_SMS_TStauStauMStau200MChi1.root',
]

files_bg = [
    'data_tt/susy_mtsum200_QCD.root',
    'data_tt/susy_mtsum200_TBar_tWch.root',
    'data_tt/susy_mtsum200_TBarToLeptons_tch_powheg.root',
    'data_tt/susy_mtsum200_VVTo2L2Nu.root',
    'data_tt/susy_mtsum200_T_tWch.root',
    'data_tt/susy_mtsum200_TToLeptons_tch_powheg.root',
    'data_tt/susy_mtsum200_TT.root',
    'data_tt/susy_mtsum200_WWTo1L1Nu2Q.root',
    'data_tt/susy_mtsum200_W4Jets.root',
    'data_tt/susy_mtsum200_W3Jets.root',
    'data_tt/susy_mtsum200_W2Jets.root',
    'data_tt/susy_mtsum200_W1Jets.root',
    'data_tt/susy_mtsum200_WZTo2L2Q.root',
    'data_tt/susy_mtsum200_WZTo1L3Nu.root',
    'data_tt/susy_mtsum200_WZTo1L1Nu2Q.root',
    'data_tt/susy_mtsum200_ZZTo4L.root',
    'data_tt/susy_mtsum200_ZZTo2L2Q.root',
    'data_tt/susy_mtsum200_ZTT.root',
    'data_tt/susy_mtsum200_ZL.root',
    'data_tt/susy_mtsum200_ZJ.root',
]

selection = '1'

def createGBRT(learning_rate=0.01, max_depth=4, n_estimators=1000, subSample=0.5):
    clf = GradientBoostingClassifier(n_estimators=n_estimators, learning_rate=learning_rate, max_depth=max_depth, random_state=1, loss='deviance', verbose=1, subsample=subSample, max_features=1.) #loss='exponential'/'deviance'
     # loss='deviance', verbose=1, subsample=subSample)
    return clf

def train(clf, training_data, target, weights, set_neg_to_zero=True):
    print clf

    sumWeightsSignal = np.sum(weights * target)
    sumWeightsBackground = sum(weights * (1 - target))

    print 'Sum weights signal', sumWeightsSignal
    print 'Sum weights background', sumWeightsBackground

    aveWeightSignal = sumWeightsSignal/np.sum(target)
    print 'Average weight signal', aveWeightSignal
    aveWeightBG = sumWeightsSignal/np.sum(1-target)
    print 'Average weight background', aveWeightBG

    nCrossVal = 2
    # kf = KFold(len(training_data), nCrossVal, shuffle=True, random_state=1)

    train_indices = np.where((training_data[:, 0]*1000.).astype(int)%2==1)
    test_indices = np.where((training_data[:, 0]*1000.).astype(int)%2==0)

    kf = [(train_indices, test_indices), (test_indices, train_indices)]

    # trainIndices = training_data[abs(int(training_data[10]*1000.))%2 == 0]
    # testIndices = training_data[abs(int(training_data[10]*1000.))%2 == 1]

    print 'Cross-validation:', nCrossVal, 'folds'

    for i_fold, (trainIndices, testIndices) in enumerate(kf):
        print 'Starting fold'

        d_train = training_data[trainIndices]
        d_test = training_data[testIndices]

        t_train = target[trainIndices]
        t_test = target[testIndices]

        w_train = weights[trainIndices]
        w_test = weights[testIndices]

        if set_neg_to_zero:
            # import pdb; pdb.set_trace()
            # w_train = np.apply_along_axis(lambda x: x if x > 0. else 0., 1, w_train)
            w_train = (w_train>0) * w_train

        # del training_data, target, weights, trainIndices, testIndices, kf

        clf.fit(d_train, t_train, w_train)

        print 'Produce scores'
        # scores = clf.decision_function(d_test)
        scores = clf.predict_proba(d_test)

        # import pdb; pdb.set_trace()

        effs = [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

        # Classic ROC curve

        fpr, tpr, tresholds = roc_curve(t_test, scores[:,1], sample_weight=w_test)
        joblib.dump((fpr, tpr, tresholds), 'roc_vals.pkl')

        for eff in effs:
            print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        # Can save with different features if necessary
        joblib.dump(clf, 'train/{name}_clf_stau_mtsum200_{i_fold}.pkl'.format(name=clf.__class__.__name__, i_fold=i_fold), compress=9)

    # if doCrossVal:
    print 'Feature importances:'
    print clf.feature_importances_

    varList = trainVars()
    for i, imp in enumerate(clf.feature_importances_):
        print imp, varList[i] if i<len(varList) else 'N/A'

    return clf

def readFiles():
    print 'Reading files...'

    weightsS = root2rec(files_signal, treename='tree', branches=['full_weight'], selection=selection)['full_weight']
    weightsB = root2rec(files_bg, treename='tree', branches=['full_weight'], selection=selection)['full_weight']

    sum_weightsS = np.sum(weightsS)
    sum_weightsB = np.sum(weightsB)

    weightsB = weightsB * sum_weightsS/sum_weightsB

    nS = len(weightsS)
    nB = len(weightsB)

    fullWeight = np.concatenate((weightsS, weightsB))
    # fullWeight = fullWeight['weight']

    # fullWeight = np.ones(len(fullWeight))

    # del weightsS, weightsB

    arrSB = root2array(files_signal + files_bg, treename='tree', branches=trainVars(), selection=selection)

    # Need a matrix-like array instead of a 1-D array of lists for sklearn
    arrSB = (np.asarray([arrSB[var] for var in trainVars()])).transpose()

    targets = np.concatenate((np.ones(nS),np.zeros(nB)))

    print 'Done reading files.'

    return arrSB, fullWeight, targets

if __name__ == '__main__':

    classifier = 'GBRT' # 'Ada' #'GBRT'
    doTrain = True

    print 'Read training and test files...'
    training, weights, targets = readFiles()

    print 'Sizes'
    print training.nbytes, weights.nbytes, targets.nbytes

    if doTrain:
        print 'Start training'

        if classifier == 'GBRT':
            clf = createGBRT()
            train(clf, training, targets, weights)
<file_sep>## Ad-hoc tau ID training with sklearn using ROOT trees as input
# Requires root_numpy https://github.com/rootpy/root_numpy
# <NAME> 27 Aug 2015

import numpy as np

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier

# from sklearn.cross_validation import train_test_split #cross_val_score
from sklearn.cross_validation import KFold

from sklearn.metrics import roc_curve

# For model I/O
from sklearn.externals import joblib

from root_numpy import root2array, root2rec
def trainVars():
    return [
        'mt', 'l2_mt', 'n_jets', 'met_pt', 'pthiggs', 'vbf_mjj', 'vbf_deta', 'vbf_n_central', 'l2_pt', 'l1_pt','mvis', 'l1_eta', 'l2_eta', 'delta_phi_l1_l2', 'delta_eta_l1_l2', 'pt_l1l2', 'delta_phi_j1_met', 'pzeta_disc', 'jet1_pt', 'jet1_eta'
    ]

files_vbf = [
    'data/inclusive_HiggsVBF125.root',
    'data/inclusive_HiggsGGH125.root'
]

files_ztt = [
    'data/inclusive_ZTT.root',
]

files_signal = files_vbf + files_ztt

files_bg = [
'data/inclusive_TBarToLeptons_tch_powheg.root',
'data/inclusive_TToLeptons_tch_powheg.root',
'data/inclusive_TBar_tWch.root',
'data/inclusive_T_tWch.root',
'data/inclusive_TT.root',
'data/inclusive_VVTo2L2Nu.root',
'data/inclusive_WWTo1L1Nu2Q.root',
'data/inclusive_WZTo1L1Nu2Q.root',
'data/inclusive_WZTo1L3Nu.root',
'data/inclusive_WZTo2L2Q.root',
# 'data/inclusive_WZTo3L.root',
'data/inclusive_W.root',
# 'data/inclusive_ZJM10.root',
'data/inclusive_ZJ.root',
# 'data/inclusive_ZLM10.root',
'data/inclusive_ZL.root',
# 'data/inclusive_ZTTM10.root',
# 'data/inclusive_ZTT.root',
'data/inclusive_ZZTo2L2Q.root',
'data/inclusive_ZZTo4L.root',
'data/inclusive_QCD.root',
# 'data/inclusive_data_obs.root',
]

selection = 'vbf_mjj>500. && abs(vbf_deta)>3.5 '
selection = 'n_jets>0.5 && !(vbf_mjj>500. && abs(vbf_deta)>3.5)'
selection = 'n_jets < 0.5'
selection = '1.'

def createGBRT(learning_rate=0.01, max_depth=4, n_estimators=1000, subSample=0.5):
    clf = GradientBoostingClassifier(n_estimators=n_estimators, learning_rate=learning_rate, max_depth=max_depth, random_state=1, loss='deviance', verbose=1, subsample=subSample, max_features=0.5) #loss='exponential'/'deviance'
     # loss='deviance', verbose=1, subsample=subSample)
    return clf

def train(clf, training_data, target, weights, set_neg_to_zero=True):
    print clf

    sumWeightsSignal = np.sum(weights[np.where(target == 1)])
    sumWeightsBackground = sum(weights[np.where(target == 0)])

    print 'Sum weights signal', sumWeightsSignal
    print 'Sum weights background', sumWeightsBackground

    aveWeightSignal = sumWeightsSignal/np.sum(target)
    print 'Average weight signal', aveWeightSignal
    aveWeightBG = sumWeightsSignal/np.sum(1-target)
    print 'Average weight background', aveWeightBG

    nCrossVal = 2
    # kf = KFold(len(training_data), nCrossVal, shuffle=True, random_state=1)

    train_indices = np.where((training_data[:, 10]*1000.).astype(int)%2==1)
    test_indices = np.where((training_data[:, 10]*1000.).astype(int)%2==0)

    kf = [(train_indices, test_indices), (test_indices, train_indices)]

    # trainIndices = training_data[abs(int(training_data[10]*1000.))%2 == 0]
    # testIndices = training_data[abs(int(training_data[10]*1000.))%2 == 1]

    print 'Cross-validation:', nCrossVal, 'folds'

    for i_fold, (trainIndices, testIndices) in enumerate(kf):
        print 'Starting fold'

        d_train = training_data[trainIndices]
        d_test = training_data[testIndices]

        t_train = target[trainIndices]
        t_test = target[testIndices]

        w_train = weights[trainIndices]
        w_test = weights[testIndices]

        if set_neg_to_zero:
            # import pdb; pdb.set_trace()
            # w_train = np.apply_along_axis(lambda x: x if x > 0. else 0., 1, w_train)
            w_train = (w_train>0) * w_train

        # del training_data, target, weights, trainIndices, testIndices, kf

        clf.fit(d_train, t_train, w_train)

        print 'Produce scores'
        # scores = clf.decision_function(d_test)
        scores = clf.predict_proba(d_test)

        # import pdb; pdb.set_trace()

        effs = [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

        print 'ZTT vs background'

        fpr, tpr, tresholds = roc_curve(t_test[np.where(t_test > 0)], scores[np.where(t_test > 0)][:,1:2], sample_weight=w_test[np.where(t_test > 0)], pos_label=1)

        for eff in effs:
            print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        print 'VBF+ggH vs background'

        fpr, tpr, tresholds = roc_curve(t_test[np.where(t_test != 1)], scores[np.where(t_test != 1)][:,0:1], sample_weight=w_test[np.where(t_test != 1)], pos_label=0)

        for eff in effs:
            print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        print 'VBF+ggh vs ZTT'

        fpr, tpr, tresholds = roc_curve(t_test[np.where(t_test != 2)], scores[np.where(t_test != 2)][:,0:1],
            sample_weight=w_test[np.where(t_test != 2)], pos_label=0)

        for eff in effs:
            print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        # Classic ROC curve

        # fpr, tpr, tresholds = roc_curve(t_test, scores, sample_weight=w_test)
        # joblib.dump((fpr, tpr, tresholds), 'roc_vals.pkl')

        # for eff in effs:
        #     print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        # Can save with different features if necessary
        # joblib.dump(clf, 'train/{name}_clf_{i_fold}_vbf.pkl'.format(name=clf.__class__.__name__, i_fold=i_fold), compress=9)
        # joblib.dump(clf, 'train/{name}_clf_{i_fold}_1jet.pkl'.format(name=clf.__class__.__name__, i_fold=i_fold), compress=9)
        joblib.dump(clf, 'train/{name}_clf_{i_fold}_0jet.pkl'.format(name=clf.__class__.__name__, i_fold=i_fold), compress=9)

    # if doCrossVal:
    print 'Feature importances:'
    print clf.feature_importances_

    varList = trainVars()
    for i, imp in enumerate(clf.feature_importances_):
        print imp, varList[i] if i<len(varList) else 'N/A'

    return clf

def readFiles():
    print 'Reading files...'

    # weightsS = root2rec(files_signal, treename='tree', branches=['full_weight'], selection=selection)
    weights_vbf = root2rec(files_vbf, treename='tree', branches=['full_weight'], selection=selection)['full_weight']
    weights_ztt = root2rec(files_ztt, treename='tree', branches=['full_weight'], selection=selection)['full_weight']
    weightsB = root2rec(files_bg, treename='tree', branches=['full_weight'], selection=selection)['full_weight']

    sum_weights_vbf = np.sum(weights_vbf)
    sum_weights_ztt = np.sum(weights_ztt)
    sum_weightsB = np.sum(weightsB)

    weights_ztt = weights_ztt * sum_weights_vbf/sum_weights_ztt
    weightsB = weightsB * sum_weights_vbf/sum_weightsB

    # nS = len(weightsS)
    n_vbf = len(weights_vbf)
    n_ztt = len(weights_ztt)
    nB = len(weightsB)

    # fullWeight = np.concatenate((weightsS, weightsB))
    fullWeight = np.concatenate((weights_vbf, weights_ztt, weightsB))
    # fullWeight = fullWeight['full_weight']

    # fullWeight = np.ones(len(fullWeight))

    # del weightsS, weightsB

    # arrSB = root2array(files_signal + files_bg, treename='tree', branches=trainVars(), selection=selection)
    arrSB = root2array(files_vbf + files_ztt + files_bg, treename='tree', branches=trainVars(), selection=selection)

    # Need a matrix-like array instead of a 1-D array of lists for sklearn
    arrSB = (np.asarray([arrSB[var] for var in trainVars()])).transpose()

    # targets = np.concatenate((np.ones(nS),np.zeros(nB)))
    targets = np.concatenate((np.ones(n_vbf)*2, np.ones(n_ztt),np.zeros(nB)))

    print 'Done reading files.'

    return arrSB, fullWeight, targets

if __name__ == '__main__':

    classifier = 'GBRT' # 'Ada' #'GBRT'
    doTrain = True

    print 'Read training and test files...'
    training, weights, targets = readFiles()

    print 'Sizes'
    print training.nbytes, weights.nbytes, targets.nbytes

    if doTrain:
        print 'Start training'

        if classifier == 'GBRT':
            clf = createGBRT()
            train(clf, training, targets, weights)
<file_sep>## Ad-hoc tau ID training with sklearn using ROOT trees as input
# Requires root_numpy https://github.com/rootpy/root_numpy
# <NAME> 27 Aug 2015

import numpy as np

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier

# from sklearn.cross_validation import train_test_split #cross_val_score
from sklearn.cross_validation import KFold

from sklearn.metrics import roc_curve

# For model I/O
from sklearn.externals import joblib

from root_numpy import root2array, root2rec

def trainVars():
    return [
    'mt',
    'mt_leg2',
    'n_jets',
    'met_pt',
    'pthiggs',
    'vbf_mjj',
    'vbf_deta',
    'vbf_n_central',
    'l2_pt',
    'l1_pt',
    # 'svfit_transverse_mass',
    # 'delta_phi_l1_l2',
    # 'delta_eta_l1_l2',
    'dil_pt',
    'dil_eta',
    'jet1_pt',
    'mt_total',
    'delta_r_l1_l2',
    'mvis'
    ]

files_vbf = [
    'data/inclusive_HiggsVBF125_weight.root',
    'data/inclusive_HiggsGGH125_weight.root'
]

files_ggh = [
    'data/inclusive_ZTT_weight.root',
]

files_signal = files_vbf + files_ggh

files_bg = [
'data/inclusive_TBarToLeptons_tch_powheg_weight.root',
'data/inclusive_TToLeptons_tch_powheg_weight.root',
'data/inclusive_TBar_tWch_weight.root',
'data/inclusive_T_tWch_weight.root',
'data/inclusive_TT_weight.root',
'data/inclusive_VVTo2L2Nu_weight.root',
'data/inclusive_W1Jets_weight.root',
'data/inclusive_W2Jets_weight.root',
'data/inclusive_W3Jets_weight.root',
'data/inclusive_W4Jets_weight.root',
'data/inclusive_WWTo1L1Nu2Q_weight.root',
'data/inclusive_WZTo1L1Nu2Q_weight.root',
'data/inclusive_WZTo1L3Nu_weight.root',
'data/inclusive_WZTo2L2Q_weight.root',
# 'data/inclusive_WZTo3L_weight.root',
# 'data/inclusive_W_weight.root',
# 'data/inclusive_ZJM10_weight.root',
'data/inclusive_ZJ_weight.root',
# 'data/inclusive_ZLM10_weight.root',
'data/inclusive_ZL_weight.root',
# 'data/inclusive_ZTTM10_weight.root',
# 'data/inclusive_ZTT_weight.root',
'data/inclusive_ZZTo2L2Q_weight.root',
'data/inclusive_ZZTo4L_weight.root',
# 'data/inclusive_data_obs_weight.root',
]

selection = '1'

def createGBRT(learning_rate=0.01, max_depth=4, n_estimators=1000, subSample=0.5):
    clf = GradientBoostingClassifier(n_estimators=n_estimators, learning_rate=learning_rate, max_depth=max_depth, random_state=1, loss='deviance', verbose=1, subsample=subSample, max_features=0.5) #loss='exponential'/'deviance'
     # loss='deviance', verbose=1, subsample=subSample)
    return clf

def train(clf, training_data, target, weights, set_neg_to_zero=True):
    print clf

    sumWeightsSignal = np.sum(weights * target)
    sumWeightsBackground = sum(weights * (1 - target))

    print 'Sum weights signal', sumWeightsSignal
    print 'Sum weights background', sumWeightsBackground

    aveWeightSignal = sumWeightsSignal/np.sum(target)
    print 'Average weight signal', aveWeightSignal
    aveWeightBG = sumWeightsSignal/np.sum(1-target)
    print 'Average weight background', aveWeightBG

    nCrossVal = 2
    # kf = KFold(len(training_data), nCrossVal, shuffle=True, random_state=1)

    train_indices = np.where((training_data[:, 10]*1000.).astype(int)%2==1)
    test_indices = np.where((training_data[:, 10]*1000.).astype(int)%2==0)

    kf = [(train_indices, test_indices), (test_indices, train_indices)]

    # trainIndices = training_data[abs(int(training_data[10]*1000.))%2 == 0]
    # testIndices = training_data[abs(int(training_data[10]*1000.))%2 == 1]

    print 'Cross-validation:', nCrossVal, 'folds'

    for i_fold, (trainIndices, testIndices) in enumerate(kf):
        print 'Starting fold'

        d_train = training_data[trainIndices]
        d_test = training_data[testIndices]

        t_train = target[trainIndices]
        t_test = target[testIndices]

        w_train = weights[trainIndices]
        w_test = weights[testIndices]

        if set_neg_to_zero:
            # import pdb; pdb.set_trace()
            # w_train = np.apply_along_axis(lambda x: x if x > 0. else 0., 1, w_train)
            w_train = (w_train>0) * w_train

        # del training_data, target, weights, trainIndices, testIndices, kf

        clf.fit(d_train, t_train, w_train)

        print 'Produce scores'
        # scores = clf.decision_function(d_test)
        scores = clf.predict_proba(d_test)

        # import pdb; pdb.set_trace()

        effs = [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

        print 'ggh vs background'

        fpr, tpr, tresholds = roc_curve(t_test[np.where(t_test > 0)], scores[np.where(t_test > 0)][:,1:2], sample_weight=w_test[np.where(t_test > 0)], pos_label=1)

        for eff in effs:
            print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        print 'VBF vs background'

        fpr, tpr, tresholds = roc_curve(t_test[np.where(t_test != 1)], scores[np.where(t_test != 1)][:,0:1], sample_weight=w_test[np.where(t_test != 1)], pos_label=0)

        for eff in effs:
            print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        print 'VBF vs ggH'

        fpr, tpr, tresholds = roc_curve(t_test[np.where(t_test != 2)], scores[np.where(t_test != 2)][:,0:1],
            sample_weight=w_test[np.where(t_test != 2)], pos_label=0)

        for eff in effs:
            print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        # Classic ROC curve

        # fpr, tpr, tresholds = roc_curve(t_test, scores, sample_weight=w_test)
        # joblib.dump((fpr, tpr, tresholds), 'roc_vals.pkl')

        # for eff in effs:
        #     print 'Fake rate at signal eff', eff, fpr[np.argmax(tpr>eff)]

        # Can save with different features if necessary
        joblib.dump(clf, 'train/{name}_clf_dyh_{i_fold}.pkl'.format(name=clf.__class__.__name__, i_fold=i_fold), compress=9)

    # if doCrossVal:
    print 'Feature importances:'
    print clf.feature_importances_

    varList = trainVars()
    for i, imp in enumerate(clf.feature_importances_):
        print imp, varList[i] if i<len(varList) else 'N/A'

    return clf

def readFiles():
    print 'Reading files...'

    # weightsS = root2rec(files_signal, treename='tree', branches=['weight'], selection=selection)
    weights_vbf = root2rec(files_vbf, treename='tree', branches=['weight'], selection=selection)['weight']
    weights_ggh = root2rec(files_ggh, treename='tree', branches=['weight'], selection=selection)['weight']
    weightsB = root2rec(files_bg, treename='tree', branches=['weight'], selection=selection)['weight']

    sum_weights_vbf = np.sum(weights_vbf)
    sum_weights_ggh = np.sum(weights_ggh)
    sum_weightsB = np.sum(weightsB)

    weights_ggh = weights_ggh * sum_weights_vbf/sum_weights_ggh
    weightsB = weightsB * sum_weights_vbf/sum_weightsB

    # nS = len(weightsS)
    n_vbf = len(weights_vbf)
    n_ggh = len(weights_ggh)
    nB = len(weightsB)

    # fullWeight = np.concatenate((weightsS, weightsB))
    fullWeight = np.concatenate((weights_vbf, weights_ggh, weightsB))
    # fullWeight = fullWeight['weight']

    # fullWeight = np.ones(len(fullWeight))

    # del weightsS, weightsB

    # arrSB = root2array(files_signal + files_bg, treename='tree', branches=trainVars(), selection=selection)
    arrSB = root2array(files_vbf + files_ggh + files_bg, treename='tree', branches=trainVars(), selection=selection)

    # Need a matrix-like array instead of a 1-D array of lists for sklearn
    arrSB = (np.asarray([arrSB[var] for var in trainVars()])).transpose()

    # targets = np.concatenate((np.ones(nS),np.zeros(nB)))
    targets = np.concatenate((np.ones(n_vbf), np.ones(n_ggh),np.zeros(nB)))

    print 'Done reading files.'

    return arrSB, fullWeight, targets

if __name__ == '__main__':

    classifier = 'GBRT' # 'Ada' #'GBRT'
    doTrain = True

    print 'Read training and test files...'
    training, weights, targets = readFiles()

    print 'Sizes'
    print training.nbytes, weights.nbytes, targets.nbytes

    if doTrain:
        print 'Start training'

        if classifier == 'GBRT':
            clf = createGBRT()
            train(clf, training, targets, weights)

repo_language: Python

repo_name: steggema/htt-sm


text:
<repo_name>Ostralyan/visual-sorting<file_sep>/README.md
# Sorting algorithms visualized<file_sep>/src/BogoSort/BogoSort.tsx
import React from 'react';
import ArrayGenerator from '../utility/ArrayGenerator';

interface State {
  listOfNumbers: number[];
  isSorted: boolean;
};
interface Props {};

const arrSize = 4;

export default class BogoSort extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props);
    const arr = ArrayGenerator.create(arrSize);
    this.state = {
      listOfNumbers: arr,
      isSorted: this.isSorted(arr),
    }
  }

  public isSorted(arr: number[]): boolean {
    for(let i = 0; i < arr.length - 1; i++) {
      if (arr[i] > arr[i + 1]) {
        return false
      }
    }

    return true;
  }

  public nextRandomArray(): void {
    if (this.state.isSorted) return;

    const listOfNumbers = ArrayGenerator.create(arrSize);
    const isSorted = this.isSorted(listOfNumbers)
    const state = {
      listOfNumbers,
      isSorted
    }
    this.setState(state);
  }

  private createTable(): JSX.Element[] {
    let table: JSX.Element[] = [];

    for(let i = 0; i < this.state.listOfNumbers.length; i++) {
      const cellClasses = ["row-cell"];

      if (this.state.isSorted) {
        cellClasses.push("sorted");
      }

      table.push(

            {this.state.listOfNumbers[i]}

      );
    }

    return table;
  }

  public render(): React.ReactNode {
    return(

 this.nextRandomArray()}>

          Bogo Sort

        {this.createTable()}

    )
  }
}
<file_sep>/src/SelectionSort/SelectionSort.tsx
import React from 'react';
import ArrayGenerator from '../utility/ArrayGenerator';

interface Props {

}

interface State {
  listOfNumbers: number[];
  currentCursor: number;
  currentIteration: number;
  currentMax: number;
  currentMaxIndex: number;
}

const arrSize = 10;

export default class SelectionSort extends React.Component<Props, State> {

  constructor(props: Props) {
    super(props);

    const randomArray = ArrayGenerator.create(arrSize)
    this.state = {
      listOfNumbers: randomArray,
      currentCursor: 1,
      currentIteration: 0,
      currentMax: randomArray[0],
      currentMaxIndex: 0
    }
  }

  private createTable(): JSX.Element[] {
    let table: JSX.Element[] = [];

    for(let i = 0; i < this.state.listOfNumbers.length; i++) {
      const cellClasses = ["row-cell"];

      table.push(

            {this.state.listOfNumbers[i]}

      );
    }

    return table;
  }

  public nextSelection(): void {
    const nextPass = [...this.state.listOfNumbers];
    let nextMax = this.state.currentMax;
    let nextMaxIndex = this.state.currentMaxIndex;

    if (this.state.listOfNumbers[this.state.currentCursor] > this.state.currentMax) {
      nextMax = this.state.listOfNumbers[this.state.currentCursor];
      nextMaxIndex = this.state.currentCursor;
    }

    let nextCursor = 0;
    let nextIteration = this.state.currentIteration;

    if (this.state.currentCursor < arrSize - this.state.currentIteration) {
      nextCursor = this.state.currentCursor + 1;
    } else {
      nextCursor = 0;
      nextIteration++;

      const lastNonSortedIndex = arrSize - this.state.currentIteration - 1;
      nextPass[nextMaxIndex] = nextPass[nextMaxIndex] ^ nextPass[lastNonSortedIndex];
      nextPass[lastNonSortedIndex] = nextPass[lastNonSortedIndex] ^ nextPass[nextMaxIndex];
      nextPass[nextMaxIndex] = nextPass[nextMaxIndex] ^ nextPass[lastNonSortedIndex];

      nextMax = this.state.listOfNumbers[this.state.currentCursor];
      nextMaxIndex = this.state.currentCursor;
    }

    this.setState({
      listOfNumbers: nextPass,
      currentCursor: nextCursor,
      currentIteration: nextIteration,
      currentMax: nextMax
    });
  }

  public render(): React.ReactNode {
    return(

 this.nextSelection()}>

          Selection Sort

        {this.createTable()}

    );
  }
}<file_sep>/src/App.tsx
import React from 'react';
import BubbleSort from './BubbleSort/BubbleSort';
import SelectionSort from './SelectionSort/SelectionSort';
import BogoSort from './BogoSort/BogoSort';
export default class App extends React.Component {
  render() {
    return(

        <BubbleSort />
        <SelectionSort />

        <BogoSort />

    )
  }
}<file_sep>/src/utility/ArrayGenerator.ts
export default class ArrayGenerator {
  public static create(length: number, minValue: number = 0, maxValue: number = 100) {
    const resultArray = [];
    for(let i = 0; i < length; i++) {
      const randomNumber = Math.floor((Math.random() * maxValue - minValue)) + minValue;
      resultArray.push(randomNumber);
    }
    return resultArray;
  }
}

languages: Markdown TypeScript TSX

repo_name: Ostralyan/visual-sorting


text: <file_sep># pelatihan-desain-grafis-smk


text:
<file_sep>angular.module('productAppController',[]).controller('Productadditioncontroller',function($scope,$state,$window){

    console.log("Product additon controller called");

     var value=$window.localStorage.getItem('data');
     console.log(JSON.parse(value));

    $scope.imageuploaded=0;
    $scope.imagefile="";

    $scope.productinfo=function(){  //function when data is submitted.

    	console.log($scope.productname);
    	//console.log($scope.productdesc);
    	//console.log($scope.stepsModel[0]);
       var value=$window.localStorage.getItem('data');

        if(value===null)
      {
        var obj=new Object();
        obj.productname=$scope.productname;
        obj.productimage=$scope.imagefile;
        obj.upvotes=0;
        obj.downvotes=0;
        obj.stars="";
        var x=[];
        x.push(obj);
        /*
        $cookies.putObject('data',x);
        var value=$cookies.getObject('data');
        console.log(value);
        */

        $window.localStorage.setItem('data',JSON.stringify(x));  //using local storage to store the object.
        var value=$window.localStorage.getItem('data');
        console.log(JSON.parse(value));

       }

       else
       {

          console.log("Entered else ");

           var obj=new Object();
           obj.productname=$scope.productname;
           obj.productimage=$scope.imagefile;
           obj.upvotes=0;
           obj.downvotes=0;
           obj.stars="";
           //var value=$cookies.getObject('data');
           //value.push(obj);
           //$cookies.putObject('data',value);

           var value=$window.localStorage.getItem('data');
           value=(JSON.parse(value));
           value.push(obj);                                                //pushiing the entire object into data.
           $window.localStorage.setItem('data',JSON.stringify(value));
            var value=$window.localStorage.getItem('data');
            console.log(JSON.parse(value));

        }

    }

    $scope.imageUpload = function(event){   //functiion called when image is uploaded and canvas is used to reduce the size of image thus indirectly reducing the lenght of base64 string.

         $scope.stepsModel=[];

         var files = event.target.files; //FileList object

         for (var i = 0; i < files.length; i++) {
             var file = files[i];
                 var reader = new FileReader();
                 reader.onload = $scope.imageIsLoaded;
                 reader.readAsDataURL(file);
         }
    }

    $scope.imageIsLoaded = function(e){
    	console.log("Entered here");
        $scope.$apply(function() {
            console.log(typeof(e.target.result));
            console.log((e.target.result).length);

            var img=new Image();
            img.src=e.target.result;

             img.onload = function () {

                            // CREATE A CANVAS ELEMENT AND ASSIGN THE IMAGES TO IT.
                            var canvas = document.createElement("canvas");

                            //var value = $('#size').val();

                            // RESIZE THE IMAGES ONE BY ONE.
                            img.width = (img.width * 10) / 100
                            img.height = (img.height * 10) / 100

                            var ctx = canvas.getContext("2d");
                            ctx.clearRect(0, 0, canvas.width, canvas.height);
                            canvas.width = img.width;
                            canvas.height = img.height;
                            ctx.drawImage(img, 0, 0, img.width, img.height);

                            var url=canvas.toDataURL("image/png");

                            console.log(typeof(url));

                            console.log(url.length);

                            $scope.imagefile=url;  //image url is collected over here.
                      };

        });
    }

});<file_sep>
 Single Product details would be Present over Here

<ratings>

{{x.productname}}

Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
           sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

                            Downvote
                             {{x.downvotes}}

                            Upvote
                             {{x.upvotes}}

                        	Rating:{{x.stars}}

</ratings>

<file_sep>angular.module('productAppController').controller('singleproductcontroller',function($scope,$stateParams,$window){

          console.log("Single product controller called");
          console.log($stateParams.id);
          $scope.value=$window.localStorage.getItem('data');
          $scope.value=JSON.parse($scope.value);
          $scope.x=$scope.value[$stateParams.id];
          console.log($scope.x);
 });<file_sep>angular.module('productAppController',[]);
 /*
 .controller('Productadditioncontroller',function($scope,$state,$window,$cookies,$localStorage){

    console.log("Product additon controller called");

     var value=$window.localStorage.getItem('data');
     console.log(JSON.parse(value));

    $scope.imageuploaded=0;
    $scope.imagefile="";

    $scope.productinfo=function(){

    	console.log($scope.productname);
    	//console.log($scope.productdesc);
    	//console.log($scope.stepsModel[0]);
       var value=$window.localStorage.getItem('data');

        if(value===null)
      {
        var obj=new Object();
        obj.productname=$scope.productname;
        obj.productimage=$scope.imagefile;
        obj.upvotes=0;
        obj.downvotes=0;
        obj.stars="";
        var x=[];
        x.push(obj);
        /*
        $cookies.putObject('data',x);
        var value=$cookies.getObject('data');
        console.log(value);

        $window.localStorage.setItem('data',JSON.stringify(x));
        var value=$window.localStorage.getItem('data');
        console.log(JSON.parse(value));

       }

       else
       {

          console.log("Entered else ");

           var obj=new Object();
           obj.productname=$scope.productname;
           obj.productimage=$scope.imagefile;
           obj.upvotes=0;
           obj.downvotes=0;
           obj.stars="";
           //var value=$cookies.getObject('data');
           //value.push(obj);
           //$cookies.putObject('data',value);

           var value=$window.localStorage.getItem('data');
           value=(JSON.parse(value));
           value.push(obj);
           $window.localStorage.setItem('data',JSON.stringify(value));
            var value=$window.localStorage.getItem('data');
            console.log(JSON.parse(value));

        }

    }

    $scope.imageUpload = function(event){

         $scope.stepsModel=[];

         var files = event.target.files; //FileList object

         for (var i = 0; i < files.length; i++) {
             var file = files[i];
                 var reader = new FileReader();
                 reader.onload = $scope.imageIsLoaded;
                 reader.readAsDataURL(file);
         }
    }

    $scope.imageIsLoaded = function(e){
    	console.log("Entered here");
        $scope.$apply(function() {
            console.log(typeof(e.target.result));
            console.log((e.target.result).length);

            var img=new Image();
            img.src=e.target.result;

             img.onload = function () {

                            // CREATE A CANVAS ELEMENT AND ASSIGN THE IMAGES TO IT.
                            var canvas = document.createElement("canvas");

                            //var value = $('#size').val();

                            // RESIZE THE IMAGES ONE BY ONE.
                            img.width = (img.width * 10) / 100
                            img.height = (img.height * 10) / 100

                            var ctx = canvas.getContext("2d");
                            ctx.clearRect(0, 0, canvas.width, canvas.height);
                            canvas.width = img.width;
                            canvas.height = img.height;
                            ctx.drawImage(img, 0, 0, img.width, img.height);

                            var url=canvas.toDataURL("image/png");

                            console.log(typeof(url));

                            console.log(url.length);

                            $scope.imagefile=url;
                      };

        });
    }

})
 .controller('productlistcontroller',function($scope,$cookies,$window,$localStorage){

     console.log('productlist controller called');
     $scope.value=$window.localStorage.getItem('data');
     //console.log(($scope.value));
     $scope.value=JSON.parse($scope.value);

 })
  .controller('singleproductcontroller',function($scope,$stateParams,$window,$cookies,$localStorage){

          console.log("Single product controller called");
          console.log($stateParams.id);
          $scope.value=$window.localStorage.getItem('data');
          $scope.value=JSON.parse($scope.value);
          $scope.x=$scope.value[$stateParams.id];
          console.log($scope.x);
  });
  */<file_sep># product_dashboard

Initial state is set to productslist

Hyperlinks are provided to get redirected to adding a image

Localstorage was used to store objects and HTML5 canvas API Is used to reduce the size of image uploaded and therby indirectly decreasing the uRL64base encoded string.
<file_sep>angular.module('productAppController').directive ('ratings',function($window){

   console.log("entered here in directive");

    return  {
     restrict:'E',

     link:function(scope,element,attrs){

        scope.calculate_rating=function(outerindex){

                var x=scope.x.upvotes;
                var y=scope.x.downvotes;
                var z=x+y;
                var star=x/z;
                star=star*5;
                star=Math.floor(star);
                scope.x.stars="";
                for(var i=0;i<star;i++)
                {
                    scope.x.stars+="#";
                }

                var value=$window.localStorage.getItem('data');
                value=JSON.parse(value);
                value[outerindex]=scope.x;
                $window.localStorage.setItem('data',JSON.stringify(value));

               var list=element.find('a');
                 console.log(list);
                for(var i=0;i<2;i++)
              {
                 $(list[i]).addClass("inactive")
             }

        };
        scope.upvote=function(outerindex){
        	if(scope.x.upvotes!=1 && scope.x.downvotes!=1)
        	{
            console.log(scope.x.upvotes);
            scope.x.upvotes+=1;
            scope.calculate_rating(outerindex);
            }
        };

        scope.downvote=function(outerindex)
        {
        	if(scope.x.downvotes!=1 && scope.x.downvotes!=1)
        	{
            scope.x.downvotes+=1;
            scope.calculate_rating(outerindex);
            }
        };
     },

     controller:'productlistcontroller'

    };

  });<file_sep>angular.module('productApp', ['ui.router','ngCookies','productAppController'])

 .config(function($stateProvider){
  $stateProvider.state('productview', {
    url: '/product/:id/view',
    templateUrl: 'partials/product-view.html',
    controller:'singleproductcontroller'
  }).state('products', {
    url: '/products',
    templateUrl: 'partials/productlist.html',
    controller:'productlistcontroller'
  }).state('newproduct', {
    url: '/newproduct',
    templateUrl: 'partials/productadd.html',
    controller:'Productadditioncontroller'
  });

 })
  .run(function($state) {
  $state.go('products');
});

languages: Markdown HTML JavaScript