# -*- coding: utf-8 -*-
"""
Created on Tue Jan 17 14:54:24 2017

@author: RJovelin
"""

# use this script to test for enrichement of overlapping genes among disease genes
# save data as table and figure

# import modules
# use Agg backend on server without X server
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib import rc
import matplotlib.gridspec as gridspec
rc('mathtext', default='regular')
import json
import random
import copy
import sys
import os
import math
import numpy as np
from scipy import stats
from HsaNestedGenes import *

# load dictionaries of overlapping genes
JsonFiles = ['HumanOverlappingGenes.json', 'HumanNestedGenes.json',
             'HumanPiggyBackGenes.json', 'HumanConvergentGenes.json',
             'HumanDivergentGenes.json']
# make a list of dictionaries
Overlap = []
# loop over files
for i in range(len(JsonFiles)):
    # load dictionary of overlapping gene pairs
    json_data = open(JsonFiles[i])
    overlapping = json.load(json_data)
    json_data.close()
    Overlap.append(overlapping)

# get GFF file
GFF = 'Homo_sapiens.GRCh38.88.gff3'

# get the coordinates of genes on each chromo
# {chromo: {gene:[chromosome, start, end, sense]}}
GeneChromoCoord = ChromoGenesCoord(GFF)
# map each gene to its mRNA transcripts
MapGeneTranscript = GeneToTranscripts(GFF)
# remove genes that do not have a mRNA transcripts (may have abberant transcripts, NMD processed transcripts, etc)
GeneChromoCoord = FilterOutGenesWithoutValidTranscript(GeneChromoCoord, MapGeneTranscript)
# get the coordinates of each gene {gene:[chromosome, start, end, sense]}
GeneCoord = FromChromoCoordToGeneCoord(GeneChromoCoord)

# generate gene sets
GeneSets = []
for i in range(len(Overlap)):
    GeneSets.append(MakeFullPartialOverlapGeneSet(Overlap[i]))
# make a set of non-overlapping genes
NonOverlappingGenes = MakeNonOverlappingGeneSet(Overlap[0], GeneCoord)

# map ensembl gene IDs to gene names {gene_ID: Name}
GeneIDToNames = MapNametoID('Homo_sapiens.GRCh38.88.gff3')
# reverse dictionary {name: ID}
GeneNamesToID = {}
for ID in GeneIDToNames:
    GeneNamesToID[GeneIDToNames[ID]] = ID

# make a set of complex disease genes
GAD = ParseComplexDisease('GADCDC_data.tsv', GeneNamesToID)
# make a set of GWAS genes
GWAS = ParseGWASDisease('gwas_catalog_v1.0-associations_e87_r2017-01-09.tsv', 'TraitsToRemove.txt', GeneNamesToID)
# make a set of cancer driver genes
Drivers = ParseCosmicFile('Census_allMon_Mar27_2017.tsv', GeneNamesToID)
# mnake a set of mendelean disease genes
OMIM = ParseOMIMDisease('mimTitles.txt', 'morbidmap.txt', GeneNamesToID)

# create a set with all disease genes
DiseaseGenes = GAD.union(GWAS).union(Drivers).union(OMIM)

# create a list of gene sets
AllGenes = [NonOverlappingGenes]
for i in range(1, len(GeneSets)):
    AllGenes.append(GeneSets[i])
# create a parallel list of gene categories
GeneCats = ['Not', 'Nst', 'Pbk', 'Con', 'Div']

# make a list of counts for each disease and gene category[[[disease, non_disease],... ], ....]
GeneCounts = CountDiseaseGenes(AllGenes, [GAD, GWAS, Drivers, OMIM, DiseaseGenes])

# test significance by comparing each overlapping gene class to non-overlapping genes for each disease
Significance = []
for i in range(len(GeneCounts)):
    # access disease type
    PVals = []
    for j in range(1, len(GeneCounts[i])):
        # compare each gene class to non-overlapping genes for that disease class
        P = stats.fisher_exact([GeneCounts[i][0], GeneCounts[i][j]])[1]
        PVals.append(P)
    # convert p values to star significance
    Significance.append(ConvertPToStars(PVals))

# compute proportions of disease and non-disease for each class in each disease class
DisProp, NonDisProp = [], []
for i in range(len(GeneCounts)):
    # access disease type
    disease, nondisease = [], []
    for j in range(len(GeneCounts[i])):
        # access disease and non-disease counts for given gene class in this disease class
        disease.append(GeneCounts[i][j][0] / sum(GeneCounts[i][j]))
        nondisease.append(GeneCounts[i][j][1] / sum(GeneCounts[i][j]))
    DisProp.append(disease)
    NonDisProp.append(nondisease)

# create a function to format the subplots
def CreateAx(Columns, Rows, Position, figure, Data, XLabel, isYLabel):
    '''
    Returns a ax instance in figure
    '''

    # add a plot to figure (N row, N column, plot N)
    ax = figure.add_subplot(Rows, Columns, Position)
    # Create a horizontal bar plot for proportions of disease genes
    ax.bar([0, 0.2, 0.4, 0.6, 0.8], Data[0], width = 0.2, label = 'disease', color= ['#2b8cbe'] * len(Data[0]), edgecolor = 'white', linewidth = 0.7)
    # Create a horizontal bar plot for proportions of non-disease genes
    ax.bar([0, 0.2, 0.4, 0.6, 0.8], Data[1], width = 0.2, bottom = Data[0], label = 'non-disease', color= ['#88419d'] * len(Data[1]), edgecolor = 'white', linewidth = 0.7)

    # set font for all text in figure
    FigFont = {'fontname':'Arial'}
    # write y axis label
    if isYLabel == True:
        YLabel = 'Proportions'
        ax.set_ylabel(YLabel, color = 'black',  size = 6, ha = 'center', **FigFont)
        # edit y axis ticks
        plt.yticks(np.arange(0, 1.25, 0.25))
    # add ticks and lebels
    plt.xticks([0.1, 0.3, 0.5, 0.7, 0.9], ['Not', 'Nst', 'Pbk', 'Con', 'Div'], rotation = 0, size = 6, color = 'black', ha = 'center', **FigFont)
    # add x label
    ax.set_xlabel(XLabel, color = 'black',  size = 7, ha = 'center', **FigFont)
    # add a range for the Y axis
    plt.ylim([0, 1])
    # do not show lines around figure
    ax.spines["top"].set_visible(False)
    ax.spines["bottom"].set_visible(True)
    ax.spines["right"].set_visible(False)
    if isYLabel == True:
        ax.spines["left"].set_visible(True)
    else:
        ax.spines['left'].set_visible(False)
    # offset the spines
    for spine in ax.spines.values():
        spine.set_position(('outward', 3))
    # edit tick parameters
    if isYLabel == True:
        plt.tick_params(axis='both', which='both', bottom='on', top='off',
                        right = 'off', left = 'on', labelbottom='on',
                        colors = 'black', labelsize = 6, direction = 'out')
        # Set the tick labels font name
        for label in ax.get_yticklabels():
            label.set_fontname('Arial')
    else:
        plt.tick_params(axis='both', which='both', bottom='on', top='off',
                        right = 'off', left = 'off', labelbottom='on', labelleft = 'off',
                        colors = 'black', labelsize = 6, direction = 'out')
    # add margins
    plt.margins(0.1)
    return ax

# make a figure with proportion of disease and non-disease genes

# create figure
fig = plt.figure(1, figsize = (6.2, 1.8))
# plot data
ax1 = CreateAx(5, 1, 1, fig, [DisProp[0], NonDisProp[0]], 'complex', True)
ax2 = CreateAx(5, 1, 2, fig, [DisProp[1], NonDisProp[1]], 'GWAS', False)
ax3 = CreateAx(5, 1, 3, fig, [DisProp[2], NonDisProp[2]], 'tumors', False)
ax4 = CreateAx(5, 1, 4, fig, [DisProp[3], NonDisProp[3]], 'mendelian', False)
ax5 = CreateAx(5, 1, 5, fig, [DisProp[4], NonDisProp[4]], 'all', False)

# annotate figure to add significance
# significant comparisons were already determined, add letters to show significance
xpos = [0.3, 0.5, 0.7, 0.9]
for i in range(len(Significance[0])):
    ax1.text(xpos[i], 1.05, Significance[0][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[1])):
    ax2.text(xpos[i], 1.05, Significance[1][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[2])):
    ax3.text(xpos[i], 1.05, Significance[2][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[3])):
    ax4.text(xpos[i], 1.05, Significance[3][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[4])):
    ax5.text(xpos[i], 1.05, Significance[4][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)

# add legend
D = mpatches.Patch(facecolor = '#2b8cbe', edgecolor = 'white', linewidth = 0.7, label= 'disease')
N = mpatches.Patch(facecolor = '#88419d', edgecolor = 'white', linewidth = 0.7, label= 'non-disease')
ax1.legend(handles = [D, N], loc = (0.05, 1.2), fontsize = 6, frameon = False, ncol = 2)

## add subplot labels
#ax1.text(-0.45, 1.1, 'A', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax2.text(0, 1.1, 'B', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax3.text(0, 1.1, 'C', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax4.text(0, 1.1, 'D', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax5.text(0, 1.1, 'E', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)

# make sure subplots do not overlap
plt.tight_layout()

# save figure to file
outputfile = 'ProportionDiseaseGenes'
for extension in ['.pdf', '.eps', '.png']:
    fig.savefig(outputfile + extension, bbox_inches = 'tight')


import sys
from sklearn.metrics import pairwise_distances
import numpy as np

def euclidian_distance_recognition(target_image_flattened, images_flattened,
                                   target_persone_i: int, target_sample_i: int, metric: str):
    closest_sample = None
    closest_persone = None
    closest_distance = sys.float_info.max

    for persone_i, samples in enumerate(images_flattened):
        for sample_i, sample in enumerate(samples):
            if(persone_i != target_persone_i or sample_i != target_sample_i):
                distance = pairwise_distances([sample-target_image_flattened],
                                              metric)
                if(distance < closest_distance):
                    closest_distance = distance
                    closest_persone = persone_i
                    closest_sample = sample_i

    return closest_persone, closest_sample

def euclidian_distance_recognition_2(target_image_flattened, images_flattened,
                                   target_persone_i: int,
                                   target_sample_i: int):
    closest_sample = None
    closest_persone = None
    closest_distance = sys.float_info.max

    for persone_i, samples in enumerate(images_flattened):
        for sample_i, sample in enumerate(samples):
            if(persone_i != target_persone_i or sample_i != target_sample_i):
                distance = np.linalg.norm(sample-target_image_flattened)
                if(distance < closest_distance):
                    closest_distance = distance
                    closest_persone = persone_i
                    closest_sample = sample_i

    return closest_persone, closest_sample

def euclidian_distance_metric_recognition(images_flattened, metric: str):
    images_flattened = np.array(images_flattened)
    images_flattened = images_flattened.reshape(-1,images_flattened.shape[-1])
    return pairwise_distances(images_flattened, metric=metric)

def euclidian_distance_metric_recognition_histogram(images_flattened):
    return pairwise_distances(images_flattened, metric="euclidean")

def TP_rate_from_distances(distance_mtx):
    TP = 0
    for i, row in enumerate(distance_mtx):
        closest_distance = closest_distance = sys.float_info.max
        closest_j = None
        for j, distance in enumerate(row):
            if(i != j and closest_distance>distance):
                closest_j = j
                closest_distance = distance

        if(int(i/10) == int(closest_j/10)):
            TP += 1

    return (TP/len(distance_mtx))


cat_VBF = 'n_jets>=2 && vbf_n_central==0 && vbf_mjj > 500 && abs(vbf_deta) > 3.5'
cat_VBF_Rel_30 = 'n_jets>=2 && n_bjets==0 && vbf_n_central==0 && vbf_mjj>200 && abs(vbf_deta) > 2.'
cat_VBF_Rel_20 = 'n_jets_20>=2 && n_bjets==0 && vbf_n_central==0 && vbf_mjj>200 && abs(vbf_deta) > 2.'

cat_J2 = 'n_jets>=2'
# cat_VH  = 'n_jets>=2 && VBF_mjj>60 && VBF_mjj<120 && VBF_dijetpt>150 && VBF_mva<0.5 && nBJets==0'
cat_J1  = 'n_jets>=1 && !({VBF}) && n_bjets==0'.format(VBF=cat_VBF)
#cat_J1  = 'n_jets>=1 && nBJets==0' #Jose
cat_J1B = 'nBJets>=1 && n_jets<2'
cat_J0 = 'n_jets==0 && n_bjets==0' # Jose
#cat_J0  = '!({VBF}) && !({J1}) && !({J1B}) && n_jets==0'.format(VBF=cat_VBF,
#                                                               J1=cat_J1,
#                                                               J1B=cat_J1B)

cat_J1B_Rel_CSVL = 'nCSVLJets>=1 && n_jets<2'

cat_1BInclusive = 'nBJets>=1'
cat_0B = 'nBJets==0'

cat_J0_high = cat_J0 + ' && l1_pt>45.'
cat_J0_medium = cat_J0 + ' && l1_pt>30. && l1_pt<=45.'
cat_J0_low = cat_J0 + ' && l1_pt>20. && l1_pt<=30.'

cat_J1_high_mediumhiggs = cat_J1 + ' && l1_pt>45. && pthiggs>100.'
cat_J1_high_lowhiggs = cat_J1 + ' && l1_pt>45. && pthiggs<100.'
cat_J1_medium = cat_J1 + ' && l1_pt>30. && l1_pt<=45.'

cat_J1_oldlow = cat_J1 + ' && l1_pt<=40.'
cat_J1_oldhigh = cat_J1 + ' && l1_pt>40.'

cat_J0_oldlow = cat_J0 + ' && l1_pt<=40.'
cat_J0_oldhigh = cat_J0 + ' && l1_pt>40.'

cat_VBF_tight = 'n_jets>=2 && l1_pt>30. && nBJets==0 && VBF_nCentral==0 && VBF_mjj>700 && abs(VBF_deta)>4. && pthiggs>100.'
cat_VBF_loose = cat_VBF + ' && l1_pt>30. && nBJets==0 && !({VBF_tight})'.format(VBF_tight=cat_VBF_tight)
cat_J2B0 = 'n_jets>=2 && l1_pt>30. && nBJets==0'

def replaceCategories(cutstr, categories):
    for catname, cat in categories.iteritems():
        cutstr = cutstr.replace( catname, cat )
    return cutstr

categories_common = {
    'Xcat_VBFX':cat_VBF,
    'Xcat_VBF_Rel_30X':cat_VBF_Rel_30,
    'Xcat_VBF_Rel_20X':cat_VBF_Rel_20,
    'Xcat_J2X':cat_J2,
    'Xcat_J2B0X':cat_J2B0,
    'Xcat_J1X':cat_J1,
    'Xcat_J1BX':cat_J1B,
    'Xcat_J0X':cat_J0,
    # The following are the Summer 13 categories
    'Xcat_J0_highX':cat_J0_high,
    'Xcat_J0_mediumX':cat_J0_medium,
    'Xcat_J0_lowX':cat_J0_low,
    'Xcat_J1_high_mediumhiggsX':cat_J1_high_mediumhiggs,
    'Xcat_J1_high_lowhiggsX':cat_J1_high_lowhiggs,
    'Xcat_J1_mediumX':cat_J1_medium,
    'Xcat_VBF_tightX':cat_VBF_tight,
    'Xcat_VBF_looseX':cat_VBF_loose,
    'Xcat_J0_oldlowX':cat_J0_oldlow,
    'Xcat_J0_oldhighX':cat_J0_oldhigh,
    'Xcat_J1_oldlowX':cat_J1_oldlow,
    'Xcat_J1_oldhighX':cat_J1_oldhigh,
    'Xcat_1BInclusiveX':cat_1BInclusive,
    'Xcat_0BX':cat_0B,
    'Xcat_J1B_Rel_CSVLX':cat_J1B_Rel_CSVL
    }


from django.shortcuts import render,redirect
from .models import Contact
from django.contrib.auth.forms import UserCreationForm
from ContactApp.forms import CreateUserForm
from django.http import HttpResponse
import csv
from django.urls import reverse_lazy

from django.contrib.auth.views import LoginView

from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required

from django.contrib.auth.forms import UserChangeForm
from django.views.generic.edit import FormView
from django.contrib.auth import login

class RegisterPage(FormView):
    template_name = 'ContactApp/register.html'
    form_class = UserCreationForm
    redirect_authenticated_user = True
    success_url = reverse_lazy('index')

    def form_valid(self, form):
        user = form.save()
        if user is not None:
            login(self.request, user)
        return super (RegisterPage, self).form_valid(form)

    def get(self, *args, **kwargs):
        if self.request.user.is_authenticated:
            return redirect('index')
        return super(RegisterPage, self).get(*args, **kwargs)

class CustomLoginView(LoginView):
    template_name='ContactApp/login.html'
    fields = '__all__'
    redirect_authenticated_user=True

    def get_success_url(self):
        return reverse_lazy('index')

@login_required()
def index(request):
    contacts=Contact.objects.filter(user=request.user)
    search_input=request.GET.get('search-area')
    if search_input:
       contacts=contacts.filter(full_name__icontains=search_input)
    else:
        contacts=contacts.all()
        search_input=''
    return render(request,'ContactApp/index.html', {'contacts':contacts,'search-input':search_input})

@login_required()
def viewContact(request,pk):
    contact=Contact.objects.get(id=pk)
    return render(request,'ContactApp/view-contact.html',{'contact':contact})

@login_required()
def editContact(request,pk):
    contact=Contact.objects.get(id=pk)
    if request.method=="POST":
        contact.full_name=request.POST["full_name"]
        contact.phone=request.POST['phone']
        contact.email=request.POST['email']
        contact.address=request.POST['address']
        contact.save()
        return redirect('/')
    return render(request,'ContactApp/edit-contact.html',{'contact':contact})

@login_required()
def addContact(request):
    if request.method=="POST":
        request.user.contact_set.create(
            full_name=request.POST["full_name"],
            phone=request.POST['phone'],
            email=request.POST['email'],
            address=request.POST['address'],)
        return redirect('/')
    return render(request,'ContactApp/add-contact.html')

@login_required()
def deleteContact(request,pk):
    contact=Contact.objects.get(id=pk)
    if request.method=="POST":
        contact.delete()
        return redirect('/')
    return render(request,'ContactApp/delete-contact.html',{'contact':contact})

@login_required
def settings(request):
    return render(request,'ContactApp/settings.html')

@login_required
def export_csv(request):
    response =  HttpResponse(content_type='text/csv')
    response['Content-Disposition']='attachment; filename=Contacts.csv'

    writer = csv.writer(response)
    writer.writerow(['Name','Phone','Email','Address'])

    contacts=Contact.objects.filter(user=request.user)
    for contact in contacts:
        writer.writerow([contact.full_name,contact.phone,contact.email,contact.address])
    return response


import json
import nbformat
import pathlib
import tempfile

from nbconvert import RSTExporter

def convert_rst(nb_path, tags_to_ignore=["hide"]):
    """
    Convert a notebook to rst
    """
    contents = nb_path.read_text()
    nb = json.loads(contents)

    error_cols = ['\x1b[0;31m', '\x1b[0m', '\x1b[0;32m', '\x1b[0;34m', '\x1b[0;36m']

    body = ''
    for cell in nb["cells"]:
        if cell['cell_type'] != 'code':
            for line in cell['source']:
                body += line.replace('$', '$$')
        if cell['cell_type'] == 'code':
            if "tags" not in cell["metadata"] or 'hide' not in cell["metadata"]["tags"]:
                body += "{% highlight python %}\n"
                for line in cell['source']:
                    if line[:4] == '    ':
                        body += '... '
                        body += line
                    elif line[0] in ['}', ')', ']']:
                        body += '... '
                        body += line
                    elif line == '\n':
                        body += '\n'
                    else:
                        body += '>>> '
                        body += line
                body += '\n'
                if len(cell['outputs']) == 1:
                    if 'data' in cell['outputs'][0]:
                        for l in cell['outputs'][0]['data']['text/plain']:
                            body += l
                    elif 'text' in cell['outputs'][0]:
                        for l in cell['outputs'][0]['text']:
                            body += l
                    else:
                        for l in cell['outputs'][0]['traceback']:
                            for col in error_cols:
                                l = l.replace(col, '')
                            body += l
                            body += '\n'
                    body += '\n'
                body += "{% endhighlight %}"
        body += '\n\n'

    with open('chapters/' + nb_path.name.split('.')[0] + '.md', 'w') as f:
        f.write(body)

    cells = []
    for cell in nb["cells"]:
        if "tags" not in cell["metadata"] or all(
            tag not in cell["metadata"]["tags"] for tag in tags_to_ignore
        ):
            if cell["cell_type"] == "code":
                cell['outputs'] = []
            cells.append(cell)
    nb["cells"] = cells

    with (nb_path.parent / 'student' / nb_path.name).open(mode='w') as f:
        f.write(json.dumps(nb))

if __name__ == "__main__":
    nb_dir = pathlib.Path("chapters/nbs")
    worksheet_paths = sorted(nb_dir.glob("*ipynb"))
    for worksheet in worksheet_paths:
        print(worksheet)
        convert_rst(worksheet)


# imports and graphics
import numpy as np
import csv
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
inv = np.linalg.inv
plt.style.use('ggplot')

# import data
with open('online_news_popularity.csv', 'r') as f:
    reader = csv.reader(f)
    a = list(reader)
    labels = a[0]
    data = np.array(a[1:])
data.shape

# convert the data into a convenient array
data = data[:,1:]
arrToFloat = np.vectorize(float)
data = arrToFloat(data)
shares = np.log(data[:,-1])

# split data into training and test data
dataRatio = .1 # note I reduced used data for runtime
trainingrows = int(data.shape[0]*dataRatio)
trdata = data[0:trainingrows, :]
trshares = shares[0:trainingrows]
tstdata = data[trainingrows:, :]
tstshares = shares[trainingrows:]

# compare gradient descent to closed form solution
trshares.reshape(trshares.shape[0],1)

# initialize weights randomly
b = np.random.randn(1)
weights = np.random.randn(trdata.shape[1])

# define learning parameters
wlrn = 2.5e-16
blrn = 2.5e-4
normconst = 100
iterations = 100

# norm constant matrix, Gamma^T Gamma
gamma = normconst * np.identity(trdata.shape[1])

# reduce redundant calculations
normterm = trdata.T @ trdata + gamma
onecol = np.ones((trdata.shape[0],1))
yterm = sum(trshares)

overNterm = trdata.T @ onecol @ onecol.T @ trdata / trdata.shape[0]

# calculate closed form solution
identTerm = trdata.T @ (np.eye(trdata.shape[0]) - 1.0/trdata.shape[0])
ident = np.identity(trdata.shape[0])

toinv = (trdata.T @ trdata  + gamma.T @ gamma - overNterm)

otherterm = (trdata.T @ trshares -
            trdata.T @ onecol @ onecol.T @ trshares / trdata.shape[0])

xstar = np.linalg.solve(toinv, otherterm)

bstar = trshares.T @ onecol -  (xstar.T
        @ trdata.T @ onecol)/trdata.shape[0]

def dist(w,b): # weights, b, real weights, real b
    wdif = w.T - xstar.T
    bdif = b - bstar
    return (wdif.T @ wdif  + bdif**2)**.5

# dists for plot
dists = []
iters = list(range(iterations))

# run gradient descent
for i in iters:
    # calculate gradient w.r.t. weights and deriv w.r.t b
    gradw = normterm @ weights + trdata.T @ (b *
            np.ones(trshares.shape[0]) - trshares)
    derivb = weights.T @ trdata.T @ onecol - yterm + b * trdata.shape[0]

    # update weights and b
    weights = weights - wlrn * gradw
    b = b - blrn * derivb

    # get distance from ideal solution
    dists += [dist(weights,b)]

# plot distance from optimal solution
plt.xlabel("Iterations")
plt.ylabel("L2 Distance from Closed Form Solution")

plt.plot(iters,dists)


from .dxgi1_5_h import *
from .d3d11_3_h import *
from ..utils import *

class D3D11_FEATURE_DATA_VIDEO_DECODER_HISTOGRAM(ctypes.Structure):
    pass

class D3D11_VIDEO_DECODER_BUFFER_DESC2(ctypes.Structure):
    pass

class D3D11_FEATURE_DATA_D3D11_OPTIONS4(ctypes.Structure):
    pass

class ID3D11Device4(ID3D11Device3):
    _case_insensitive_ = True
    _iid_ = None
    _idlflags_ = []

class ID3D11Device5(ID3D11Device4):
    _case_insensitive_ = True
    _iid_ = None
    _idlflags_ = []

class ID3D11Multithread(comtypes.IUnknown):
    _case_insensitive_ = True
    _iid_ = None
    _idlflags_ = []

class ID3D11VideoContext2(ID3D11VideoContext1):
    _case_insensitive_ = True
    _iid_ = None
    _idlflags_ = []

class ID3D11VideoDevice2(ID3D11VideoDevice1):
    _case_insensitive_ = True
    _iid_ = None
    _idlflags_ = []

class ID3D11VideoContext3(ID3D11VideoContext2):
    _case_insensitive_ = True
    _iid_ = None
    _idlflags_ = []

IID_ID3D11Device4 = MIDL_INTERFACE(
    "{8992AB71-02E6-4B8D-BA48-B056DCDA42C4}"
)
ID3D11Device4._iid_ = IID_ID3D11Device4

ID3D11Device4._methods_ = [
    COMMETHOD(
        [helpstring('Method RegisterDeviceRemovedEvent')],
        HRESULT,
        'RegisterDeviceRemovedEvent',
        (['in'], HANDLE, 'hEvent'),
        (['out'], POINTER(DWORD), 'pdwCookie'),
    ),
    COMMETHOD(
        [helpstring('Method UnregisterDeviceRemoved')],
        VOID,
        'UnregisterDeviceRemoved',
        (['in'], DWORD, 'dwCookie'),
    ),
]

IID_ID3D11Device5 = MIDL_INTERFACE(
    "{8FFDE202-A0E7-45DF-9E01-E837801B5EA0}"
)
ID3D11Device5._iid_ = IID_ID3D11Device5

ID3D11Device5._methods_ = [
    COMMETHOD(
        [helpstring('Method OpenSharedFence')],
        HRESULT,
        'OpenSharedFence',
        (['in'], HANDLE, 'hFence'),
        (['in'], REFIID, 'ReturnedInterface'),
        (['out'], POINTER(POINTER(VOID)), 'ppFence'),
    ),
    COMMETHOD(
        [helpstring('Method CreateFence')],
        HRESULT,
        'CreateFence',
        (['in'], UINT64, 'InitialValue'),
        (['in'], D3D11_FENCE_FLAG, 'Flags'),
        (['in'], REFIID, 'ReturnedInterface'),
        (['out'], POINTER(POINTER(VOID)), 'ppFence'),
    ),
]

IID_ID3D11Multithread = MIDL_INTERFACE(
    "{9B7E4E00-342C-4106-A19F-4F2704F689F0}"
)
ID3D11Multithread._iid_ = IID_ID3D11Multithread

ID3D11Multithread._methods_ = [
    COMMETHOD(
        [helpstring('Method Enter')],
        VOID,
        'Enter',
    ),
    COMMETHOD(
        [helpstring('Method Leave')],
        VOID,
        'Leave',
    ),
    COMMETHOD(
        [helpstring('Method SetMultithreadProtected')],
        BOOL,
        'SetMultithreadProtected',
        (['in'], BOOL, 'bMTProtect'),
    ),
    COMMETHOD(
        [helpstring('Method GetMultithreadProtected')],
        BOOL,
        'GetMultithreadProtected',
    ),
]

IID_ID3D11VideoContext2 = MIDL_INTERFACE(
    "{C4E7374C-6243-4D1B-AE87-52B4F740E261}"
)
ID3D11VideoContext2._iid_ = IID_ID3D11VideoContext2

ID3D11VideoContext2._methods_ = [
    COMMETHOD(
        [helpstring('Method VideoProcessorSetOutputHDRMetaData')],
        VOID,
        'VideoProcessorSetOutputHDRMetaData',
        (
            ['in'],
            POINTER(ID3D11VideoProcessor),
            'pVideoProcessor'
        ),
        (['in'], DXGI_HDR_METADATA_TYPE, 'Type'),
        (['in'], UINT, 'Size'),
        (['in'], POINTER(VOID), 'pHDRMetaData'),
    ),
    COMMETHOD(
        [helpstring('Method VideoProcessorGetOutputHDRMetaData')],
        VOID,
        'VideoProcessorGetOutputHDRMetaData',
        (
            ['in'],
            POINTER(ID3D11VideoProcessor),
            'pVideoProcessor'
        ),
        (['out'], POINTER(DXGI_HDR_METADATA_TYPE), 'pType'),
        (['in'], UINT, 'Size'),
        (['out'], POINTER(VOID), 'pMetaData'),
    ),
    COMMETHOD(
        [helpstring('Method VideoProcessorSetStreamHDRMetaData')],
        VOID,
        'VideoProcessorSetStreamHDRMetaData',
        (
            ['in'],
            POINTER(ID3D11VideoProcessor),
            'pVideoProcessor'
        ),
        (['in'], UINT, 'StreamIndex'),
        (['in'], DXGI_HDR_METADATA_TYPE, 'Type'),
        (['in'], UINT, 'Size'),
        (['in'], POINTER(VOID), 'pHDRMetaData'),
    ),
    COMMETHOD(
        [helpstring('Method VideoProcessorGetStreamHDRMetaData')],
        VOID,
        'VideoProcessorGetStreamHDRMetaData',
        (
            ['in'],
            POINTER(ID3D11VideoProcessor),
            'pVideoProcessor'
        ),
        (['in'], UINT, 'StreamIndex'),
        (['out'], POINTER(DXGI_HDR_METADATA_TYPE), 'pType'),
        (['in'], UINT, 'Size'),
        (['out'], POINTER(VOID), 'pMetaData'),
    ),
]

class D3D11_FEATURE_VIDEO(ENUM):
    D3D11_FEATURE_VIDEO_DECODER_HISTOGRAM = 0

class D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT(ENUM):
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_Y = 0
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_U = 1
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_V = 2
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_R = 0
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_G = 1
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_B = 2
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_A = 3

class D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAGS(ENUM):
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_NONE = 0
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_Y = (
        1 << D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT.D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_Y
    )
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_U = (
        1 << D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT.D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_U
    )
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_V = (
        1 << D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT.D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_V
    )
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_R = (
        1 << D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT.D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_R
    )
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_G = (
        1 << D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT.D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_G
    )
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_B = (
        1 << D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT.D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_B
    )
    D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAG_A = (
        1 << D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT.D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_A
    )

D3D11_FEATURE_DATA_VIDEO_DECODER_HISTOGRAM._fields_ = [
    ('DecoderDesc', D3D11_VIDEO_DECODER_DESC),
    ('Components', D3D11_VIDEO_DECODER_HISTOGRAM_COMPONENT_FLAGS),
    ('BinCount', UINT),
    ('CounterBitDepth', UINT),
]

class D3D11_CRYPTO_SESSION_KEY_EXCHANGE_FLAGS(ENUM):
    D3D11_CRYPTO_SESSION_KEY_EXCHANGE_FLAG_NONE = 0

IID_ID3D11VideoDevice2 = MIDL_INTERFACE(
    "{59C0CB01-35F0-4A70-8F67-87905C906A53}"
)
ID3D11VideoDevice2._iid_ = IID_ID3D11VideoDevice2

ID3D11VideoDevice2._methods_ = [
    COMMETHOD(
        [helpstring('Method CheckFeatureSupport')],
        HRESULT,
        'CheckFeatureSupport',
        ([], D3D11_FEATURE_VIDEO, 'Feature'),
        (['out'], POINTER(VOID), 'pFeatureSupportData'),
        ([], UINT, 'FeatureSupportDataSize'),
    ),
    COMMETHOD(
        [helpstring('Method NegotiateCryptoSessionKeyExchangeMT')],
        HRESULT,
        'NegotiateCryptoSessionKeyExchangeMT',
        (
            ['in'],
            POINTER(ID3D11CryptoSession),
            'pCryptoSession'
        ),
        (
            ['in'],
            D3D11_CRYPTO_SESSION_KEY_EXCHANGE_FLAGS,
            'flags'
        ),
        (['in'], UINT, 'DataSize'),
        (['out', 'in'], POINTER(VOID), 'pData'),
    ),
]

D3D11_VIDEO_DECODER_BUFFER_DESC2._fields_ = [
    ('BufferType', D3D11_VIDEO_DECODER_BUFFER_TYPE),
    ('DataOffset', UINT),
    ('DataSize', UINT),
    # [annotation]
    ('pIV', POINTER(VOID)),
    ('IVSize', UINT),
    # [annotation]
    ('pSubSampleMappingBlock', POINTER(D3D11_VIDEO_DECODER_SUB_SAMPLE_MAPPING_BLOCK)),
    ('SubSampleMappingCount', UINT),
    ('cBlocksStripeEncrypted', UINT),
    ('cBlocksStripeClear', UINT),
]

IID_ID3D11VideoContext3 = MIDL_INTERFACE(
    "{A9E2FAA0-CB39-418F-A0B7-D8AAD4DE672E}"
)
ID3D11VideoContext3._iid_ = IID_ID3D11VideoContext3

ID3D11VideoContext3._methods_ = [
    COMMETHOD(
        [helpstring('Method DecoderBeginFrame1')],
        HRESULT,
        'DecoderBeginFrame1',
        (['in'], POINTER(ID3D11VideoDecoder), 'pDecoder'),
        (
            ['in'],
            POINTER(ID3D11VideoDecoderOutputView),
            'pView'
        ),
        ([], UINT, 'ContentKeySize'),
        (['in'], POINTER(VOID), 'pContentKey'),
        (['in'], UINT, 'NumComponentHistograms'),
        (['in'], POINTER(UINT), 'pHistogramOffsets'),
        (['in'], POINTER(ID3D11Buffer), 'ppHistogramBuffers'),
    ),
    COMMETHOD(
        [helpstring('Method SubmitDecoderBuffers2')],
        HRESULT,
        'SubmitDecoderBuffers2',
        (['in'], POINTER(ID3D11VideoDecoder), 'pDecoder'),
        (['in'], UINT, 'NumBuffers'),
        (
            ['in'],
            POINTER(D3D11_VIDEO_DECODER_BUFFER_DESC2),
            'pBufferDesc'
        ),
    ),
]

D3D11_FEATURE_DATA_D3D11_OPTIONS4._fields_ = [
    ('ExtendedNV12SharedTextureSupported', BOOL),
]

IID_ID3D11Device4 = GUID('{8992AB71-2E6-4B8D-BA48-B056DCDA42C4}')

IID_ID3D11Device5 = GUID('{8FFDE202-A0E7-45DF-9E01-E837801B5EA0}')

IID_ID3D11Multithread = GUID('{9B7E4E00-342C-4106-A19F-4F2704F689F0}')

IID_ID3D11VideoContext2 = GUID('{C4E7374C-6243-4D1B-AE87-52B4F740E261}')

IID_ID3D11VideoDevice2 = GUID('{59C0CB01-35F0-4A70-8F67-87905C906A53}')

IID_ID3D11VideoContext3 = GUID('{A9E2FAA0-CB39-418F-A0B7-D8AAD4DE672E}')


capital_letter = ""
for i in range(65, 91):
    capital_letter += chr(i)

small_letters = ""
for i in range(97, 123):
    small_letters += chr(i)

def find_capital_letters(input_string):
    output_string = ""
    for letter in input_string:
        if letter in capital_letter:
            output_string += letter
    return output_string

game = [[1, 2, 1], [2, 1, 2], [2, 1, 0]]

def tic_tac_toe(game: list) -> int:
    """
    Find game winner.

    #4

    The 3x3 table is represented as a list of 3 rows, each row has 3 element (ints).
    The value can be 1 (player 1), 2 (player 2) or 0 (empty).
    The winner is the player who gets 3 of her pieces in a row, column or diagonal.

    There is only one winner or draw. You don't have to validate whether the game is in correct (possible) state.
    I.e the game could have four 1s and one 0 etc.

    tic_tac_toe([[1, 2, 1], [2, 1, 2], [2, 2, 1]]) => 1
    tic_tac_toe([[1, 0, 1], [2, 1, 2], [2, 2, 0]]) => 0
    tic_tac_toe([[2, 2, 2], [0, 2, 0], [0, 1, 0]]) => 2

    :param game
    :return: winning player id
    """
    for i in game:
        if 0 not in i and i[0] == i[1] == i[2]:
            return i[0]

    if game[0][0] == game[1][1] == game[2][2] != 0:
        return game[0][0]
    elif game[0][0] == game[1][0] == game[2][0] != 0:
        return game[0][0]
    elif game[2][0] == game[1][1] == game[0][2] != 0:
        return game[2][0]
    elif game[0][2] == game[1][2] == game[2][2] != 0:
        return game[0][2]
    elif game[0][1] == game[1][1] == game[2][1]:
        return game[0][1]
    else:
        return 0

def close_far(a: int, b: int, c: int) -> bool:
    """
    Return if one value is "close" and other is "far".

    #2

    Given three ints, a b c, return true if one of b or c is "close" (differing from a by at most 1),
    while the other is "far", differing from both other values by 2 or more.

    close_far(1, 2, 10) => True
    close_far(1, 2, 3) => False
    close_far(4, 1, 3) => True
    """
    if abs(b - c) >= 2:
        if (abs(a - b) <= 1 and abs(a - c) >= 2) or (abs(a - c) <= 1 and abs(a - b) >= 2):
            return True
    return False

if __name__ == '__main__':
    print(close_far(1, 2, 3))


# -*- coding: utf-8 -*-
"""
Created on 2020/11/16 9:48 下午
@Author  : Donghang He
@FileName: method.py
@Software: PyCharm
"""
import os
import pandas as pd
import numpy as np
import traceback

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn import svm, tree
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA

sc = StandardScaler()
le = LabelEncoder()

knn_classifier = KNeighborsClassifier(n_neighbors=7)
logistic_classifier = LogisticRegression()
nb_classifier = GaussianNB()
clf = tree.DecisionTreeClassifier(criterion='entropy')
model = RandomForestClassifier(n_estimators=9, max_depth=5, random_state=1, criterion='entropy')
lda_classifier = LDA()
qda_classifier = QDA()
svm_classifier = svm.SVC(kernel='linear')

ticker = 'nba'
input_dir = r'datasets'
input_file = os.path.join(input_dir, ticker + '.csv')
output_dir = r'plots'

score_col = ['FG', 'FGA', 'FG%', '3P', '3PA', '3P%', '2P', '2PA', '2P%', 'eFG%', 'FT', 'FTA', 'FT%', 'PTS']
other_col = ['G', 'MP', 'ORB', 'DRB', 'TRB', 'AST', 'STL', 'BLK', 'TOV', 'PF']
pred_score = ['FG%', '3P%', '2P%', 'eFG%', 'FT%']
pred_other = ['G', 'ORB', 'TRB', 'AST', 'BLK']

x_train = [0, 0]
x_train_sc = [0, 0]
y_train = [0, 0]

x_test = [0, 0]
x_test_sc = [0, 0]
y_test = [0, 0]

title_list = ['score', 'other']
method = ['kNN', 'Logistic', 'Naive', 'Decision Tree', 'Random Forest',
          'LDA', 'QDA', 'SVM']

try:
    df_new = pd.read_csv('datasets/NBA_label.csv')
    df_score = df_new[score_col]
    df_other = df_new[other_col]
    df_corr = [df_score, df_other]

    x1 = df_new[pred_score].values
    y = df_new['Label'].values
    x_train[0], x_test[0], y_train[0], y_test[0] = train_test_split(x1, y, test_size=0.5, random_state=0)

    x2 = df_new[pred_other].values
    x_train[1], x_test[1], y_train[1], y_test[1] = train_test_split(x2, y, test_size=0.5, random_state=0)

    x_train_sc[0] = sc.fit_transform(x_train[0])
    x_train_sc[1] = sc.fit_transform(x_train[1])
    x_test_sc[0] = sc.fit_transform(x_test[0])
    x_test_sc[1] = sc.fit_transform(x_test[1])

except Exception as e:
    print(e)
    print(traceback.format_exc())

def acc(predict, name, i):
    accuracy = np.mean(predict == y_test[i])
    print("Accuracy of", name, "for", title_list[i], "is", round(float(accuracy), 3))
    return round(float(accuracy), 3)

# use to get the label of age, bigger than 25 is 1 other is 0
# def generateLabel():
#     df = pd.read_csv(input_file, encoding='gbk')
#     age = df['Age'].tolist()
#     label = []
#     for i in range(len(age)):
#         if age[i] > 25:
#             label.append(1)
#         else:
#             label.append(0)
#     df['Label'] = label
#     df.to_csv('datasets/NBA_label.csv', index=False)
#
#
# def main():
#     # generateLabel()

# main()


# -*- coding: utf-8 -*-
""" My Aggregated bandit algorithm, similar to Exp4 but not exactly equivalent.

The algorithm is a master A, managing several "slave" algorithms, :math:`A_1, ..., A_N`.

- At every step, the prediction of every slave is gathered, and a vote is done to decide A's decision.
- The vote is simply a majority vote, weighted by a trust probability. If :math:`A_i` decides arm :math:`I_i`, then the probability of selecting :math:`k` is the sum of trust probabilities, :math:`P_i`, of every :math:`A_i` for which :math:`I_i = k`.
- The trust probabilities are first uniform, :math:`P_i = 1/N`, and then at every step, after receiving the feedback for *one* arm :math:`k` (the reward), the trust in each slave :math:`A_i` is updated: :math:`P_i` increases if :math:`A_i` advised :math:`k` (:math:`I_i = k`), or decreases if :math:`A_i` advised another arm.

- The detail about how to increase or decrease the probabilities are specified below.

- Reference: [[Aggregation of Multi-Armed Bandits Learning Algorithms for Opportunistic Spectrum Access, Lilian Besson and Emilie Kaufmann and Christophe Moy, 2017]](https://hal.inria.fr/hal-01705292)

.. note::

   Why call it *Aggregator* ?
   Because this algorithm is an efficient *aggregation* algorithm,
   and like The Terminator, he beats his opponents with an iron fist!
   (*OK, that's a stupid joke but a cool name, thanks Emilie!*)

   .. image::  https://media.giphy.com/media/YoB1eEFB6FZ1m/giphy.gif
      :target: https://en.wikipedia.org/wiki/Terminator_T-800_Model_101
      :alt:    https://en.wikipedia.org/wiki/Terminator_T-800_Model_101

.. note::

   I wanted to call it *Aggragorn*.
   Because this algorithm is like `Aragorn the ranger <https://en.wikipedia.org/wiki/Aragorn>`_,
   it starts like a simple bandit, but soon it will become king!!

   .. image::  https://media.giphy.com/media/12GSQqUY9CSmJO/giphy.gif
      :target: https://en.wikipedia.org/wiki/Aragorn
      :alt:    https://en.wikipedia.org/wiki/Aragorn
"""
from __future__ import division, print_function  # Python 2 compatibility

__author__ = "Lilian Besson"
__version__ = "0.6"

import numpy as np
import numpy.random as rn
try:
    from .BasePolicy import BasePolicy
except ImportError:
    from BasePolicy import BasePolicy

# Default values for the parameters

#: A flag to know if the rewards are used as biased estimator,
#: i.e., just :math:`r_t`, or unbiased estimators, :math:`r_t / p_t`, if :math:`p_t` is the probability of selecting that arm at time :math:`t`.
#: It seemed to work better with unbiased estimators (of course).
UNBIASED = False
UNBIASED = True    # Better

#: Flag to know if we should update the trusts proba like in Exp4 or like in my initial Aggregator proposal
#:
#: - First choice: like Exp4, trusts are fully recomputed, ``trusts^(t+1) = exp(rate_t * estimated mean rewards upto time t)``,
#: - Second choice: my proposal, trusts are just updated multiplicatively, ``trusts^(t+1) <-- trusts^t * exp(rate_t * estimate instant reward at time t)``.
#:
#: Both choices seem fine, and anyway the trusts are renormalized to be a probability distribution, so it doesn't matter much.
UPDATE_LIKE_EXP4 = True
UPDATE_LIKE_EXP4 = False  # Better

#: Non parametric flag to know if the Exp4-like update uses losses or rewards.
#: Losses are ``1 - reward``, in which case the ``rate_t`` is negative.
USE_LOSSES = True
USE_LOSSES = False

#: Should all trusts be updated, or only the trusts of slaves Ai who advised the decision ``Aggregator[A1..AN]`` followed.
UPDATE_ALL_CHILDREN = False

class Aggregator(BasePolicy):
    """ My Aggregated bandit algorithm, similar to Exp4 but not exactly equivalent."""

    def __init__(self, nbArms, children=None,
                 learningRate=None, decreaseRate=None, horizon=None,
                 update_all_children=UPDATE_ALL_CHILDREN, update_like_exp4=UPDATE_LIKE_EXP4,
                 unbiased=UNBIASED, prior='uniform',
                 lower=0., amplitude=1.,
                 extra_str=''
                ):
        # Attributes
        self.nbArms = nbArms  #: Number of arms
        self.lower = lower  #: Lower values for rewards
        self.amplitude = amplitude  #: Larger values for rewards
        self.learningRate = learningRate  #: Value of the learning rate (can be decreasing in time)
        self.decreaseRate = decreaseRate  #: Value of the constant used in the decreasing of the learning rate
        self.unbiased = unbiased or update_like_exp4  #: Flag, see above.
        # XXX If we use the Exp4 update rule, it's better to be unbiased
        # XXX If we use my update rule, it seems to be better to be "biased"
        self.horizon = int(horizon) if horizon is not None else None  #: Horizon T, if given and not None, can be used to compute a "good" constant learning rate, :math:`\sqrt{\frac{2 \log(N)}{T K}}` for N slaves, K arms (heuristic).
        self.extra_str = extra_str  #: A string to add at the end of the ``str(self)``, to specify which algorithms are aggregated for instance.
        self.update_all_children = update_all_children  #: Flag, see above.
        self.nbChildren = len(children)  #: Number N of slave algorithms.
        self.t = 0  #: Internal time
        self.update_like_exp4 = update_like_exp4  #: Flag, see above.
        # If possible, pre compute the learning rate
        if horizon is not None and decreaseRate == 'auto':
            self.learningRate = np.sqrt(2 * np.log(self.nbChildren) / (self.horizon * self.nbArms))
            self.decreaseRate = None
        elif learningRate is None:
            self.decreaseRate = 'auto'
        # Internal object memory
        self.children = []  #: List of slave algorithms.
        for i, child in enumerate(children):
            if isinstance(child, dict):
                print("  Creating this child player from a dictionnary 'children[{}]' = {} ...".format(i, child))  # DEBUG
                localparams = {'lower': lower, 'amplitude': amplitude}
                localparams.update(child['params'])
                self.children.append(child['archtype'](nbArms, **localparams))
            elif isinstance(child, type):
                print("  Using this not-yet created player 'children[{}]' = {} ...".format(i, child))  # DEBUG
                self.children.append(child(nbArms, lower=lower, amplitude=amplitude))  # Create it here!
            elif callable(child):
                print("  Using this delayed function to create player 'children[{}]' = {} ...".format(i, child))  # DEBUG
                self.children.append(child())
            else:
                print("  Using this already created player 'children[{}]' = {} ...".format(i, child))  # DEBUG
                self.children.append(child)
        # Initialize the arrays
        # Assume uniform prior if not given or if = 'uniform'
        self.trusts = np.full(self.nbChildren, 1. / self.nbChildren)  #: Initial trusts in the slaves. Default to uniform, but a prior can also be given.
        if prior is not None and prior != 'uniform':
            assert len(prior) == self.nbChildren, "Error: the 'prior' argument given to Aggregator has to be an array of the good size ({}).".format(self.nbChildren)  # DEBUG
            self.trusts = prior
        # Internal vectorial memory
        self.choices = np.full(self.nbChildren, -10000, dtype=int)  #: Keep track of the last choices of each slave, to know whom to update if update_all_children is false.
        if self.update_like_exp4:
            self.children_cumulated_losses = np.zeros(self.nbChildren)  #: Keep track of the cumulated loss (empirical mean)
        self.index = np.zeros(nbArms)  #: Numerical index for each arms

    # Print, different output according to the parameters
    def __str__(self):
        """ Nicely print the name of the algorithm with its relevant parameters."""
        name = "Exp4" if self.update_like_exp4 else "Aggregator"
        all_children = ", update all" if self.update_all_children else ""
        if self.decreaseRate == 'auto':
            if self.horizon:
                s = r"{}($T={}$, $N={}${})".format(name, self.horizon, self.nbChildren, all_children)
            else:
                s = r"{}($N={}${})".format(name, self.nbChildren, all_children)
        elif self.decreaseRate is not None:
            s = r"{}($N={}${}, $\eta={:.3g}$, $dRate={:.3g}$)".format(name, self.nbChildren, all_children, self.learningRate, self.decreaseRate)
        else:
            s = r"{}($N={}${}, $\eta={:.3g}$)".format(name, self.nbChildren, all_children, self.learningRate)
        # usually information in extra_str gives information about N=...
        if self.extra_str != "":
            return s.replace("($N={}$)".format(self.nbChildren), "") + self.extra_str
        else:
            return s + self.extra_str

    # This decorator @property makes this method an attribute, cf. https://docs.python.org/3/library/functions.html#property
    @property
    def rate(self):
        """ Learning rate, can be constant if self.decreaseRate is None, or decreasing.

        - if horizon is known, use the formula which uses it,
        - if horizon is not known, use the formula which uses current time :math:`t`,
        - else, if decreaseRate is a number, use an exponentionally decreasing learning rate, ``rate = learningRate * exp(- t / decreaseRate)``. Bad.
        """
        if self.decreaseRate is None:  # Constant learning rate
            return self.learningRate
        elif self.decreaseRate == 'auto':
            # DONE Implement the two smart values given in Theorem 4.2 from [Bubeck & Cesa-Bianchi, 2012](http://sbubeck.com/SurveyBCB12.pdf)
            if self.horizon is None:
                return np.sqrt(np.log(self.nbChildren) / (self.t * self.nbArms))
            else:
                return np.sqrt(2 * np.log(self.nbChildren) / (self.horizon * self.nbArms))
        else:
            # DONE I tried to reduce the learning rate (geometrically) when t increase: it does not improve much
            return self.learningRate * np.exp(- self.t / self.decreaseRate)

    # --- Start and get a reward

    def startGame(self):
        """ Start the game for each child."""
        self.t = 0
        # Start all children
        for i in range(self.nbChildren):
            self.children[i].startGame()
        self.choices.fill(-1)
        self.index.fill(0)

    def getReward(self, arm, reward):
        """ Give reward for each child, and then update the trust probabilities."""
        self.t += 1
        # First, give reward to all children
        for child in self.children:
            child.getReward(arm, reward)
        # Then compute the new learning rate
        trusts = self.trusts
        rate = self.rate
        reward = (reward - self.lower) / self.amplitude  # Normalize it to [0, 1]
        # DONE compute the proba that we observed this arm, p_t, if unbiased
        if self.unbiased:
            proba_of_observing_arm = np.sum(trusts[self.choices == arm])
            # print("  Observing arm", arm, "with reward", reward, "and the estimated proba of observing it was", proba_of_observing_arm)  # DEBUG
            reward /= proba_of_observing_arm
        # 3. Compute the new trust proba, like in Exp4
        if self.update_like_exp4:
            if USE_LOSSES:
                loss = 1 - reward
                # Update estimated cumulated rewards for each player
                self.children_cumulated_losses[self.choices == arm] += loss
                trusts = np.exp(-1.0 * rate * self.children_cumulated_losses)
            else:
                # Update estimated cumulated rewards for each player
                self.children_cumulated_losses[self.choices == arm] += reward
                trusts = np.exp(rate * self.children_cumulated_losses)
        # 3'. increase self.trusts for the children who were true
        else:
            scaling = np.exp(rate * reward)
            trusts[self.choices == arm] *= scaling
            # DONE test both, by changing the option self.update_all_children
            if self.update_all_children:
                trusts[self.choices != arm] /= scaling
        # 4. renormalize self.trusts to make it a proba dist
        # In practice, it also decreases the self.trusts for the children who were wrong
        self.trusts = trusts / np.sum(trusts)
        # print("  The most trusted child policy is the {}th with confidence {}.".format(1 + np.argmax(self.trusts), np.max(self.trusts)))  # DEBUG
        # print("self.trusts =", self.trusts)  # DEBUG

    # --- Internal method

    def _makeChildrenChoose(self):
        """ Convenience method to make every children chose their best arm, and store their decision in ``self.choices``."""
        for i, child in enumerate(self.children):
            self.choices[i] = child.choice()
            # Could we be faster here? Idea: first sample according to self.trusts, then make it decide
            # XXX No: in fact, we need to vector self.choices to update the self.trusts probabilities!
        # print("self.choices =", self.choices)  # DEBUG

    # --- Choice of arm methods

    def choice(self):
        """ Make each child vote, then sample the decision by `importance sampling <https://en.wikipedia.org/wiki/Importance_sampling>`_ on their votes with the trust probabilities."""
        # 1. make vote every child
        self._makeChildrenChoose()
        # 2. select the vote to trust, randomly
        return rn.choice(self.choices, p=self.trusts)

    def choiceWithRank(self, rank=1):
        """ Make each child vote, with rank, then sample the decision by `importance sampling <https://en.wikipedia.org/wiki/Importance_sampling>`_ on their votes with the trust probabilities."""
        if rank == 1:
            return self.choice()
        else:
            for i, child in enumerate(self.children):
                self.choices[i] = child.choiceWithRank(rank)
            return rn.choice(self.choices, p=self.trusts)

    def choiceFromSubSet(self, availableArms='all'):
        """ Make each child vote, on subsets of arms, then sample the decision by `importance sampling <https://en.wikipedia.org/wiki/Importance_sampling>`_ on their votes with the trust probabilities."""
        if (availableArms == 'all') or (len(availableArms) == self.nbArms):
            return self.choice()
        else:
            for i, child in enumerate(self.children):
                self.choices[i] = child.choiceFromSubSet(availableArms)
            return rn.choice(self.choices, p=self.trusts)

    def choiceMultiple(self, nb=1):
        """ Make each child vote, multiple times, then sample the decision by `importance sampling <https://en.wikipedia.org/wiki/Importance_sampling>`_ on their votes with the trust probabilities."""
        if nb == 1:
            return np.array([self.choice()])
        else:
            choices = [None] * self.nbChildren
            for i, child in enumerate(self.children):
                choices[i] = child.choiceMultiple(nb)
                self.choices[i] = choices[i][0]
            this_choices = choices[rn.choice(self.nbChildren, replace=False, p=self.trusts)]
            return this_choices

    def choiceIMP(self, nb=1, startWithChoiceMultiple=True):
        """ Make each child vote, multiple times (with IMP scheme), then sample the decision by `importance sampling <https://en.wikipedia.org/wiki/Importance_sampling>`_ on their votes with the trust probabilities."""
        if nb == 1:
            return np.array([self.choice()])
        else:
            choices = [None] * self.nbChildren
            for i, child in enumerate(self.children):
                choices[i] = child.choiceIMP(nb)
                self.choices[i] = choices[i][0]
            this_choices = choices[rn.choice(self.nbChildren, replace=False, p=self.trusts)]
            return this_choices

    def estimatedOrder(self):
        """ Make each child vote for their estimate order of the arms, then randomly select an ordering by `importance sampling <https://en.wikipedia.org/wiki/Importance_sampling>`_ with the trust probabilities.
        Return the estimate order of the arms, as a permutation on ``[0..K-1]`` that would order the arms by increasing means."""
        alltrusts = self.trusts
        orders = []
        trusts = []
        for i, child in enumerate(self.children):
            if hasattr(child, 'estimatedOrder'):
                orders.append(child.estimatedOrder())
                trusts.append(alltrusts[i])
        trusts = np.asarray(trusts)
        trusts /= np.sum(trusts)
        chosenOrder = int(rn.choice(len(orders), size=1, replace=False, p=trusts))
        return orders[chosenOrder]

    def estimatedBestArms(self, M=1):
        """ Return a (non-necessarily sorted) list of the indexes of the M-best arms. Identify the set M-best."""
        assert 1 <= M <= self.nbArms, "Error: the parameter 'M' has to be between 1 and K = {}, but it was {} ...".format(self.nbArms, M)  # DEBUG
        order = self.estimatedOrder()
        return order[-M:]

    def computeIndex(self, arm):
        """ Compute the current index of arm 'arm', by computing all the indexes of the children policies, and computing a convex combination using the trusts probabilities."""
        indexes = [None] * self.nbChildren
        for i, child in enumerate(self.children):
            indexes[i] = child.computeIndex(arm)
        index = np.dot(indexes, self.trusts)
        return index

    def computeAllIndex(self):
        """ Compute the current indexes for all arms. Possibly vectorized, by default it can *not* be vectorized automatically."""
        for arm in range(self.nbArms):
            self.index[arm] = self.computeIndex(arm)

    def handleCollision(self, arm, reward=None):
        """ Default to give a 0 reward (or ``self.lower``)."""
        # FIXME not clear why it should be like giving a zero reward to the master policy,
        # super(Aggregator, self).handleCollision(arm, reward=reward)
        # XXX maybe giving the collision information to all children players is enough...
        for child in self.children:
            child.handleCollision(arm, reward=reward)


import sys

sys.stdin = open('4843_특별한정렬_input.txt')

T = int(input())

for tc in range(1, T+1):
    N = int(input())
    new_list = [0] * N
    nums = list(map(int, input().split()))

    # 오름차순 정렬
    for i in range(N-1):
        for j in range(N-1):
            if nums[j] > nums[j+1]:
                nums[j], nums[j+1] = nums[j+1], nums[j]

    # 짝 홀 행에 하나씩 담기
    for k in range(N):
        a = int(N-(k/2)-1)
        b = int(k-(k+1)/2)
        if k % 2 == 0:
            new_list[k] = nums[a]
        else:
            new_list[k] = nums[b]

    print(f'#{tc}', end=' ')
    print(*new_list)


from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

def plot_distribution_times(data, times=[0, 7.5, 15, 30, 60, 120],
    xlim=(0, 100), ylim=(0, 1000), bins=30):
    """
    Plot the distribution of a given data set for each time point.
    """

    fig, axs = plt.subplots(2, 3, figsize=(8,4))
    fig.tight_layout(rect=[0.05, 0.03, 0.95, 0.95])
    plt.subplots_adjust(hspace=0.5, wspace=0.5)
    axs = np.concatenate([axs[0], axs[1]])
    for i in range(len(times)):
        time = times[i]
        ax = axs[i]
        current_data = data[time]
        current_data = current_data[(current_data > xlim[0]) &
                                    (current_data < xlim[1])]

        ax.hist(current_data.values, bins=bins, alpha=1., edgecolor='white', linewidth=0.5)
        ax.set_title("%.1f min" % time)
        ax.set_xlim(*xlim)
        ax.set_ylim(*ylim)


import requests
import bs4

url  = 'https://www.aporeto.com/job-listings/'
html = requests.get(url)

bs = bs4.BeautifulSoup(html.text,"html.parser")

jobs = bs.find_all('div',{'class':'job-listing__text'})
# print(jobs)
open('aporeto-jobs.txt','w').close()
file = open('aporeto-jobs.txt','a+')
for job in jobs:
    title = job.find('h2').get_text()
    location = job.find('h4').get_text()
    description = job.find('p').getText()

    file.write("Title:"+ title +"\n")
    file.write("Location:"+ location +"\n")
    file.write("Description:"+ description +"\n")
    file.write("\n\n")
file.close()


# https://www.acmicpc.net/problem/11724
import sys
from collections import deque, defaultdict

input = sys.stdin.readline

def bfs(x):
    q = deque()
    q.append(x)
    while q:
        vx = q.popleft()
        for nx in graph[vx]:
            if nx not in visited:
                visited.add(nx)
                q.append(nx)

if __name__ == "__main__":
    global graph
    graph = defaultdict(set)
    N, M = map(int, input().split())
    for _ in range(M):
        u, v = map(int, input().split())
        graph[u].add(v)
        graph[v].add(u)
    global visited
    visited = set()
    ans = 0
    for i in range(1,N+1):
        if i not in visited:
            visited.add(i)
            bfs(i)
            ans += 1
    print(ans)


def get_count(trees, cut_height):
	idx = 0
	cnt = 0
	while idx < len(trees):
		if trees[idx] <= cut_height:
			break
		cnt += trees[idx] - cut_height
		idx += 1
	return cnt

def cut(N, M, trees):
	start = 0
	end = trees[0]
	answer = 0
	while start < end:
		mid = (start + end) // 2
		cnt = get_count(trees, mid)
		if cnt == M or start == mid or end == mid:
			answer = mid
			break
		elif cnt < M:
			end = mid
		else:
			start = mid
	return answer

N, M = list(map(int, input().split()))
trees = list(map(int, input().split()))
trees = sorted(trees, reverse=True)
print(cut(N, M, trees))


import math
import tensorflow as tf
import tensorflow.contrib.eager as tfe

# Enable Eager Execution
tf.enable_eager_execution()

from svnh_loader import SvnhLoader
from tfrecord_loader import TfrecordLoader

from pi_model import PiModel, pi_model_loss, pi_model_gradients, ramp_up_function, ramp_down_function

def main():
    # Constants variables
    NUM_TRAIN_SAMPLES = 73257
    NUM_TEST_SAMPLES = 26032

    # Editable variables
    num_labeled_samples = 1000
    num_validation_samples = 200
    num_train_unlabeled_samples = NUM_TRAIN_SAMPLES - \
        num_labeled_samples - num_validation_samples
    batch_size = 25
    epochs = 300
    max_learning_rate = 0.001
    initial_beta1 = 0.9
    final_beta1 = 0.5
    checkpoint_directory = './checkpoints/PiModel'
    tensorboard_logs_directory = './logs/PiModel'

    # Assign it as tfe.variable since we will change it across epochs
    learning_rate = tfe.Variable(max_learning_rate)
    beta_1 = tfe.Variable(initial_beta1)

    # Download and Save Dataset in Tfrecords
    loader = SvnhLoader('./data', NUM_TRAIN_SAMPLES,
                        num_validation_samples, num_labeled_samples)
    loader.download_images_and_generate_tf_record()

    # Generate data loaders
    train_labeled_iterator, train_unlabeled_iterator, validation_iterator, test_iterator = loader.load_dataset(
        batch_size, epochs)

    batches_per_epoch = int(num_labeled_samples/batch_size)
    batches_per_epoch_val = int(num_validation_samples / batch_size)

    model = PiModel()
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate, beta1=beta_1, beta2=0.990)
    max_unsupervised_weight = 100 * num_labeled_samples / \
        (NUM_TRAIN_SAMPLES - num_validation_samples)

    best_val_accuracy = 0
    global_step = tf.train.get_or_create_global_step()
    writer = tf.contrib.summary.create_file_writer(tensorboard_logs_directory)
    writer.set_as_default()

    for epoch in range(epochs):

        rampdown_value = ramp_down_function(epoch, epochs)
        rampup_value = ramp_up_function(epoch)

        if epoch == 0:
            unsupervised_weight = 0
        else:
            unsupervised_weight = max_unsupervised_weight * \
                rampup_value

        learning_rate.assign(rampup_value * rampdown_value * max_learning_rate)
        beta_1.assign(rampdown_value * initial_beta1 +
                      (1.0 - rampdown_value) * final_beta1)

        epoch_loss_avg = tfe.metrics.Mean()
        epoch_accuracy = tfe.metrics.Accuracy()
        epoch_loss_avg_val = tfe.metrics.Mean()
        epoch_accuracy_val = tfe.metrics.Accuracy()
        for batch_nr in range(batches_per_epoch):
            X_labeled_train, y_labeled_train, _ = train_labeled_iterator.get_next()
            X_unlabeled_train, _, _ = train_unlabeled_iterator.get_next()

            loss_val, grads = pi_model_gradients(X_labeled_train, y_labeled_train, X_unlabeled_train,
                                                 model, unsupervised_weight)

            optimizer.apply_gradients(zip(grads, model.variables),
                                      global_step=global_step)
            epoch_loss_avg(loss_val)
            epoch_accuracy(
                tf.argmax(model(X_labeled_train), 1), tf.argmax(y_labeled_train, 1))
            if (batch_nr == batches_per_epoch - 1):
                for batch_val_nr in range(batches_per_epoch_val):
                    X_val, y_val, _ = validation_iterator.get_next()
                    y_val_predictions = model(X_val, training=False)

                    epoch_loss_avg_val(tf.losses.softmax_cross_entropy(
                        y_val, y_val_predictions))
                    epoch_accuracy_val(
                        tf.argmax(y_val_predictions, 1), tf.argmax(y_val, 1))

        print("Epoch {:03d}/{:03d}: Train Loss: {:9.7f}, Train Accuracy: {:02.6%}, Validation Loss: {:9.7f}, "
              "Validation Accuracy: {:02.6%}, lr={:.9f}, unsupervised weight={:5.3f}, beta1={:.9f}".format(epoch+1,
                                                                                epochs,
                                                                                epoch_loss_avg.result(),
                                                                                epoch_accuracy.result(),
                                                                                epoch_loss_avg_val.result(),
                                                                                epoch_accuracy_val.result(),
                                                                                learning_rate.numpy(),
                                                                                unsupervised_weight,
                                                                                beta_1.numpy()))

        # If the accuracy of validation improves save a checkpoint Best 85%
        if best_val_accuracy < epoch_accuracy_val.result():
            best_val_accuracy = epoch_accuracy_val.result()
            checkpoint = tfe.Checkpoint(optimizer=optimizer,
                                        model=model,
                                        optimizer_step=global_step)
            checkpoint.save(file_prefix=checkpoint_directory)

        # Record summaries
        with tf.contrib.summary.record_summaries_every_n_global_steps(1):
            tf.contrib.summary.scalar('Train Loss', epoch_loss_avg.result())
            tf.contrib.summary.scalar(
                'Train Accuracy', epoch_accuracy.result())
            tf.contrib.summary.scalar(
                'Validation Loss', epoch_loss_avg_val.result())
            tf.contrib.summary.scalar(
                'Validation Accuracy', epoch_accuracy_val.result())
            tf.contrib.summary.scalar(
                'Unsupervised Weight', unsupervised_weight)
            tf.contrib.summary.scalar('Learning Rate', learning_rate.numpy())
            tf.contrib.summary.scalar('Ramp Up Function', rampup_value)
            tf.contrib.summary.scalar('Ramp Down Function', rampdown_value)

    print('\nTrain Ended! Best Validation accuracy = {}\n'.format(best_val_accuracy))

    # Load the best model
    root = tfe.Checkpoint(optimizer=optimizer,
                          model=model,
                          optimizer_step=tf.train.get_or_create_global_step())
    root.restore(tf.train.latest_checkpoint(checkpoint_directory))

    # Evaluate on the final test set
    num_test_batches = math.ceil(NUM_TEST_SAMPLES/batch_size)
    test_accuracy = tfe.metrics.Accuracy()
    for test_batch in range(num_test_batches):
        X_test, y_test, _ = test_iterator.get_next()
        y_test_predictions = model(X_test, training=False)
        test_accuracy(tf.argmax(y_test_predictions, 1), tf.argmax(y_test, 1))

    print("Final Test Accuracy: {:.6%}".format(test_accuracy.result()))

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Navigator Node

Controls PID loops to control robot wheel motors

Subscribes To:
    /status: the status of the PS4 controller
    /trajectory: the destination to head towards
    /odom/ekf/enk_imu: the filtered odometry info of the robot
Publishes:
    /cmd_vel: motor commands to be interpreted by the motor driver
        coordinate system:
            x: +forward -backward
            y: +right - left
            z: +up -down

"""
import rospy
from object_tracking.msg import trajectory
from geometry_msgs.msg import Twist
from ds4_driver.msg import Status
from nav_msgs.msg import Odometry
from modules.PID import PID
import time

running = False

def process_error(angular_error, linear_error,
        angularPID, angular_deadband, max_angular,
        linearPID, linear_deadband, max_linear,
        dt, d_vel_err):
    '''
    Feeds the errors into PID loops

    Parameters:
    angular_error (float): how far angularly robot is from desired
    linear_error (float): how far linear robot is from desired
    angularPID (PID): pid controller for angular velocity
    angular_deadband (float): max allowable linear before correcting
    max_angular (float): cap on angular command
    linearPID (PID): pid controller for linear velocity
    linear_deadband (float): max allowable linear error before correcting
    max_linear (float): cap on linear command
    dt (float): time since last called
    d_vel_err (float): optional other estimate for velocity (potentially make use of better velocity estimation and feed it here for feedback control)

    Returns:
    vel (geometry_msgs.msg Twist)

    '''
    vel = Twist()
    #checks if angular error outside deadband
    if(abs(-angular_error) > angular_deadband):
        #if so compute the error using the pid and clamping it below max
        vel.angular.z = min(max_angular, max(-max_angular, angularPID.compute(-angular_error,dt)))
    else:
        angularPID.feed_sample(-angular_error, dt)
    #checks if linear error outside deadband
    if(abs(linear_error) > linear_deadband):
        #if so compute the error using the pid and clamping it below max
        vel.linear.x = min(max_linear, max(-max_linear, linearPID.compute(linear_error,dt, d_vel_err)))
    else:
        linearPID.feed_sample(linear_error, dt)
    return vel

def handle_trajectory(trajectory, container):
    '''
    Callback for reading trajectory info

    Parameters:
    trajectory (object_tracking.msg trajectory): current desired relative location of robot
    container (Dictionary): container to store info in

    '''
    container["angular_error"] = trajectory.angular_error
    container["linear_error"] = trajectory.linear_error
#checks controller state
def handle_controller(msg):
    '''
    Callback for reading the PS4 controller
    Changes global of whether or not the program is "running"

    Parameters:
    msg (ds4_driver.msg 'Status'): msg from the controlller
    '''
    global running
    if (msg.button_triangle == 1):
        running = True
    if (msg.button_circle == 1):
        running = False
def handle_odometry(msg, container):
    '''
    Callback for handlng odometry info

    Parameters:
    msg (sensor_msgs.msg Odometry): odemetry msg from the robot
    container (dictionary): container to store data in

    '''
    container["robot_linear_vel"] = msg.twist.twist.linear.x

def run():
    '''
    Main function controlling node
    '''
    global running

    #grabs parameters
    process_rate = rospy.get_param("/process_rate")
    running = rospy.get_param("/start_running")
    angular_p_gain  = rospy.get_param("/angular_p_gain")
    angular_d_gain  = rospy.get_param("/angular_d_gain")
    angular_deadband= rospy.get_param("/angular_deadband")
    max_angular = rospy.get_param("/max_angular")

    linear_p_gain   = rospy.get_param("/linear_p_gain")
    linear_d_gain   = rospy.get_param("/linear_d_gain")
    linear_deadband = rospy.get_param("/linear_deadband")
    const_vel = rospy.get_param("/const_vel")
    feed_forward_k = rospy.get_param("/feed_forward_k")
    max_linear = rospy.get_param("/max_linear")

    #creates pid controllers
    linearPID = PID(linear_deadband, linear_p_gain, 0, linear_d_gain, 0)
    angularPID = PID(angular_deadband, angular_p_gain, 0, angular_d_gain, 0)

    #initializes ros stuff
    rospy.init_node("navigator", anonymous=False)
    rate = rospy.Rate(process_rate)
    pub = rospy.Publisher("/cmd_vel", Twist, queue_size =process_rate)

    prev_time = time.time()
    #container for storing callback info
    container = {
        "angular_error": 0,
        "linear_error": 0,
        "robot_linear_vel":0,

    }
    #initialize subscriber
    rospy.Subscriber("/trajectory", trajectory, handle_trajectory,
                (container)
            )

    rospy.Subscriber("/status", Status, handle_controller)

    rospy.Subscriber("/odom/ekf/enc_imu", Odometry, handle_odometry, container)

    prev_time = time.time()

    while not rospy.is_shutdown():
        curr_time = time.time()
        #initilize d_vel_error to be none
        d_vel_error = None
        #checks if a const vel (exterior vel element is specified)
        if (const_vel != ""):
            #if so d_vel_error is configured to be the combination of the robots velocity and the velocity of the person (converted to mm because that is what the pid expects
            d_vel_error = 1000 * (container['robot_linear_vel'] + const_vel)
        #run all the info through the process error command
        vel = process_error(container['angular_error'], container['linear_error'], angularPID, angular_deadband, max_angular, linearPID, linear_deadband, max_linear, curr_time-prev_time, d_vel_error)
        #optionally apply the feed forward part if const vel exists (you can set feed_forward_k to zero to stop this from having an effect)
        if (const_vel != ""):
            vel.linear.x += feed_forward_k * const_vel

        #if running publish
        if (running):
            pub.publish(vel)
        prev_time = curr_time
        rate.sleep()

if __name__ == "__main__":
    try:
        run()
    except rospy.ROSInterruptException:
        pass


import folium

from django.shortcuts import render, get_object_or_404
from .models import PokemonEntity, Pokemon
from django.utils.timezone import localtime

MOSCOW_CENTER = [55.751244, 37.618423]
DEFAULT_IMAGE_URL = (
    'https://vignette.wikia.nocookie.net/pokemon/images/6/6e/%21.png/revision'
    '/latest/fixed-aspect-ratio-down/width/240/height/240?cb=20130525215832'
    '&fill=transparent'
)

def add_pokemon(folium_map, lat, lon, image_url=DEFAULT_IMAGE_URL):
    icon = folium.features.CustomIcon(
        image_url,
        icon_size=(50, 50),
    )
    folium.Marker(
        [lat, lon],
        # Warning! `tooltip` attribute is disabled intentionally
        # to fix strange folium cyrillic encoding bug
        icon=icon,
    ).add_to(folium_map)

def show_all_pokemons(request):
    folium_map = folium.Map(location=MOSCOW_CENTER, zoom_start=12)
    current_time = localtime()
    pokemon_entities = PokemonEntity.objects.filter(
        appeared_at__lt=current_time, disappeared_at__gt=current_time
        )
    for pokemon_entity in pokemon_entities:
        add_pokemon(
            folium_map, pokemon_entity.lat,
            pokemon_entity.lon,
            request.build_absolute_uri(pokemon_entity.pokemon.image.url)
        )

    pokemons_on_page = []
    pokemons = Pokemon.objects.all()
    for pokemon in pokemons:
        pokemons_on_page.append({
            'pokemon_id': pokemon.id,
            'img_url': request.build_absolute_uri(pokemon.image.url),
            'title_ru': pokemon.title,
        })

    return render(request, 'mainpage.html', context={
        'map': folium_map._repr_html_(),
        'pokemons': pokemons_on_page,
    })

def show_pokemon(request, pokemon_id):
    requested_pokemon = get_object_or_404(Pokemon, id=int(pokemon_id))
    requested_pokemon_entities = requested_pokemon.entities.all()

    folium_map = folium.Map(location=MOSCOW_CENTER, zoom_start=12)
    for pokemon_entity in requested_pokemon_entities:
        add_pokemon(
            folium_map, pokemon_entity.lat,
            pokemon_entity.lon,
            request.build_absolute_uri(requested_pokemon.image.url)
        )

    pokemon = {
            'pokemon_id': requested_pokemon.id,
            'img_url': request.build_absolute_uri(requested_pokemon.image.url),
            'title_ru': requested_pokemon.title,
            'title_en': requested_pokemon.title_en,
            'title_jp': requested_pokemon.title_jp,
            'description': requested_pokemon.description,
            'previous_evolution': {
                'title_ru': requested_pokemon.previous_evolution.title,
                'pokemon_id': requested_pokemon.previous_evolution.id,
                'img_url': request.build_absolute_uri(requested_pokemon.previous_evolution.image.url)
                } if requested_pokemon.previous_evolution else '',
            'next_evolution': {
                'title_ru': requested_pokemon.next_evolutions.first().title,
                'pokemon_id': requested_pokemon.next_evolutions.first().id,
                'img_url': request.build_absolute_uri(requested_pokemon.next_evolutions.first().image.url)
                } if requested_pokemon.next_evolutions.first() else '',
            }

    return render(request, 'pokemon.html', context={
        'map': folium_map._repr_html_(), 'pokemon': pokemon
    })


#!/bin/python3

import sys

def sockMerchant(n, ar):
    count = 0
    index = 0
    while index < len(ar)-1:
        i = index + 1
        while i < len(ar):
            if ar[index] == ar[i]:
                ar.remove(ar[index])
                ar.remove(ar[i-1])
                count += 1
                index = -1
                break
            i += 1
        index += 1
    return count

n = int(input().strip())
ar = list(map(int, input().strip().split(' ')))
result = sockMerchant(n, ar)
print(result)


# Caesar cipher - decrypting a message.
cipher = input('Enter your cryptogram: ')
text = ''
for char in cipher:

    if char.isalnum() or char.isspace():

        if char.isalpha():
            if char not in 'a':
                code = ord(char) - 1
            elif char in 'a':
                code = ord('z')
        if char.isspace():
            code = ord(char)

        text += chr(code)

        if char.isdigit():
            text += char

print(text)


"""
General utilities module.
"""
from game.settings import __version__

def get_perception(agent, cave, is_wall=None,):
    """
    Returns a tuple of perceptions over the area with the folowing
    structure:

    (stink, breeze, shine, impact, wumpus)
    """
    x, y = agent.current_position
    stink = cave._map[x][y].stink
    breeze = cave._map[x][y].breeze
    shine = cave._map[x][y].shine
    impact = is_wall
    wumpus = cave._map[x][y].wumpus

    return (stink, breeze, shine, impact, wumpus)

def ascii_banner():
    return r'''

     __      __
     \ \    / /  _ _ __  _ __ _  _ ___
      \ \/\/ / || | '  \| '_ \ || (_-<
     __\_/\_/_\_,_|_|_|_| .__/\_,_/__/
     \ \    / /__ _ _| ||_| |
      \ \/\/ / _ \ '_| / _` |
       \_/\_/\___/_| |_\__,_|
   ''' + f'\n\tversion: {__version__}'


from matplotlib.image import NonUniformImage
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

data =  np.loadtxt("/home/user/Desktop/lorenz_transient_terminal__test_full5.csv", delimiter = ",")

rho   =  20.00
sigma = 10.00
beta  = 8.0/3.0

dimension = 0

drho = data[dimension:,4]
dsigma = data[dimension:,5]
dbeta = data[dimension:,6]

drho_edges = np.linspace(-rho/10, rho/10, 10)
dsigma_edges = np.linspace(-sigma/10, sigma/10, 10)
dbeta_edges = np.linspace(-beta/10, beta/10, 10)

H, xedges, yedges = np.histogram2d(dsigma, drho, bins=(dsigma_edges, drho_edges))
H = H.T

fig = plt.figure(figsize=(7, 3))

ax = fig.add_subplot(111, title='P(sigma-rho)', aspect='equal', xlim=xedges[[0, -1]], ylim=yedges[[0, -1]])
im = NonUniformImage(ax, interpolation='bilinear', cmap = cm.Greys_r)
xcenters = (xedges[:-1] + xedges[1:]) / 2
ycenters = (yedges[:-1] + yedges[1:]) / 2
im.set_data(xcenters, ycenters, H)
ax.images.append(im)
plt.show()


import logging
import platform
import sys
import tempfile
from pathlib import Path
from unittest import mock
from unittest import TestCase

from click.testing import CliRunner

from tests import fixtures_dir
from xsdata import __version__
from xsdata.cli import cli
from xsdata.cli import resolve_source
from xsdata.codegen.transformer import SchemaTransformer
from xsdata.codegen.writer import CodeWriter
from xsdata.formats.dataclass.generator import DataclassGenerator
from xsdata.logger import logger
from xsdata.models.config import GeneratorConfig
from xsdata.models.config import StructureStyle
from xsdata.utils.downloader import Downloader

CodeWriter.register_generator("testing", DataclassGenerator)

class CliTests(TestCase):
    def setUp(self):
        self.runner = CliRunner()
        super().setUp()

    @classmethod
    def setUpClass(cls):
        CodeWriter.register_generator("testing", DataclassGenerator)

    @classmethod
    def tearDownClass(cls):
        CodeWriter.unregister_generator("testing")

    @mock.patch.object(SchemaTransformer, "process")
    @mock.patch.object(SchemaTransformer, "__init__", return_value=None)
    def test_generate(self, mock_init, mock_process):
        source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd")
        result = self.runner.invoke(cli, [str(source), "--package", "foo"])
        config = mock_init.call_args[1]["config"]

        self.assertIsNone(result.exception)
        self.assertFalse(mock_init.call_args[1]["print"])
        self.assertEqual("foo", config.output.package)
        self.assertEqual("dataclasses", config.output.format.value)
        self.assertFalse(config.output.relative_imports)
        self.assertEqual(StructureStyle.FILENAMES, config.output.structure_style)
        self.assertEqual([source.as_uri()], mock_process.call_args[0][0])

    @mock.patch.object(SchemaTransformer, "process")
    @mock.patch.object(SchemaTransformer, "__init__", return_value=None)
    def test_generate_with_configuration_file(self, mock_init, mock_process):
        file_path = Path(tempfile.mktemp())
        config = GeneratorConfig()
        config.output.package = "foo.bar"
        config.output.structure_style = StructureStyle.NAMESPACES
        with file_path.open("w") as fp:
            config.write(fp, config)

        source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd")
        result = self.runner.invoke(
            cli,
            [str(source), "--config", str(file_path), "--no-eq"],
            catch_exceptions=False,
        )
        config = mock_init.call_args[1]["config"]

        self.assertIsNone(result.exception)
        self.assertFalse(mock_init.call_args[1]["print"])
        self.assertEqual("foo.bar", config.output.package)
        self.assertEqual("dataclasses", config.output.format.value)
        self.assertFalse(config.output.format.eq)
        self.assertEqual(StructureStyle.NAMESPACES, config.output.structure_style)
        self.assertEqual([source.as_uri()], mock_process.call_args[0][0])
        file_path.unlink()

    @mock.patch.object(SchemaTransformer, "process")
    @mock.patch.object(SchemaTransformer, "__init__", return_value=None)
    def test_generate_with_print_mode(self, mock_init, mock_process):
        source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd")
        result = self.runner.invoke(cli, [str(source), "--package", "foo", "--print"])

        self.assertIsNone(result.exception)
        self.assertEqual([source.as_uri()], mock_process.call_args[0][0])
        self.assertTrue(mock_init.call_args[1]["print"])

    @mock.patch.object(SchemaTransformer, "process")
    @mock.patch.object(SchemaTransformer, "__init__", return_value=None)
    def test_generate_with_debug_mode(self, *args):
        self.runner.invoke(cli, ["foo.xsd", "--package", "foo", "--debug"])
        self.assertEqual(logging.DEBUG, logger.level)

    @mock.patch("xsdata.cli.logger.info")
    def test_init_config(self, mock_info):
        output = tempfile.mktemp()
        output_path = Path(output)
        result = self.runner.invoke(cli, ["init-config", str(output_path)])

        self.assertIsNone(result.exception)
        self.assertEqual(GeneratorConfig.create(), GeneratorConfig.read(output_path))
        mock_info.assert_has_calls(
            [
                mock.call(
                    "========= xsdata v%s / Python %s / Platform %s =========\n",
                    __version__,
                    platform.python_version(),
                    sys.platform,
                ),
                mock.call("Initializing configuration file %s", str(output_path)),
            ]
        )
        output_path.unlink()

    @mock.patch("xsdata.cli.logger.info")
    def test_init_config_when_file_exists(self, mock_info):
        output = tempfile.mktemp()
        output_path = Path(output).resolve()

        config = GeneratorConfig.create()
        config.version = "20.8"

        with output_path.open("w") as fp:
            config.write(fp, config)

        result = self.runner.invoke(cli, ["init-config", str(output_path)])

        self.assertIsNone(result.exception)
        self.assertNotEqual("20.8", GeneratorConfig.read(output_path))
        mock_info.assert_has_calls(
            [
                mock.call(
                    "========= xsdata v%s / Python %s / Platform %s =========\n",
                    __version__,
                    platform.python_version(),
                    sys.platform,
                ),
                mock.call("Updating configuration file %s", str(output_path)),
            ]
        )
        output_path.unlink()

    def test_init_config_with_print_mode(self):
        result = self.runner.invoke(cli, ["init-config", "--print"])

        self.assertIsNone(result.exception)
        self.assertIn('<Config xmlns="http://pypi.org/project/xsdata"', result.output)

    @mock.patch.object(Downloader, "wget")
    @mock.patch.object(Downloader, "__init__", return_value=None)
    def test_download(self, mock_init, mock_wget):
        uri = "http://www.w3.org/2009/01/xml.xsd"
        result = self.runner.invoke(cli, ["download", uri])

        self.assertIsNone(result.exception)
        mock_init.assert_called_once_with(output=Path.cwd())
        mock_wget.assert_called_once_with(uri)

    @mock.patch.object(Downloader, "wget")
    @mock.patch.object(Downloader, "__init__", return_value=None)
    def test_download_with_custom_output(self, mock_init, mock_wget):
        uri = "http://www.w3.org/2009/01/xml.xsd"

        result = self.runner.invoke(cli, ["download", uri, "--output", "here/schemas"])

        self.assertIsNone(result.exception)
        mock_init.assert_called_once_with(output=Path("here/schemas").resolve())
        mock_wget.assert_called_once_with(uri)

    def test_resolve_source(self):
        hello_path = fixtures_dir.joinpath("hello")

        file = hello_path.joinpath("hello.xsd")
        url = "http://www.xsdata/schema.xsd"

        self.assertEqual([file.as_uri()], list(resolve_source(str(file), False)))
        self.assertEqual([url], list(resolve_source(url, False)))
        self.assertEqual(5, len(list(resolve_source(str(hello_path), False))))

        def_xml_path = fixtures_dir.joinpath("calculator")
        self.assertEqual(3, len(list(resolve_source(str(def_xml_path), False))))

        actual = list(resolve_source(str(fixtures_dir), True))
        self.assertEqual(39, len(actual))


import argparse
import logging
import os
import random
import sys
import time
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from tensorboardX import SummaryWriter
from torch.nn.modules.loss import CrossEntropyLoss
from torch.utils.data import DataLoader
from tqdm import tqdm
from utils import DiceLoss
from torchvision import transforms

def trainer_synapse(args, model, snapshot_path):
    from datasets.dataset_synapse import Synapse_dataset, RandomGenerator
    logging.basicConfig(filename=snapshot_path + "/log.txt", level=logging.INFO,
                        format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
    logging.info(str(args))
    base_lr = args.base_lr
    num_classes = args.num_classes
    batch_size = args.batch_size * args.n_gpu
    # max_iterations = args.max_iterations
    db_train = Synapse_dataset(base_dir=args.root_path, list_dir=args.list_dir, split="train",
                               transform=transforms.Compose(
                                   [RandomGenerator(output_size=[args.img_size, args.img_size])]))
    print("The length of train set is: {}".format(len(db_train)))

    def worker_init_fn(worker_id):
        random.seed(args.seed + worker_id)

    trainloader = DataLoader(db_train, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True,
                             worker_init_fn=worker_init_fn)
    if args.n_gpu > 1:
        model = nn.DataParallel(model)
    model.train()
    ce_loss = CrossEntropyLoss()
    dice_loss = DiceLoss(num_classes)
    optimizer = optim.SGD(model.parameters(), lr=base_lr, momentum=0.9, weight_decay=0.0001)
    writer = SummaryWriter(snapshot_path + '/log')
    iter_num = 0
    max_epoch = args.max_epochs
    max_iterations = args.max_epochs * len(trainloader)  # max_epoch = max_iterations // len(trainloader) + 1
    logging.info("{} iterations per epoch. {} max iterations ".format(len(trainloader), max_iterations))
    best_performance = 0.0
    iterator = tqdm(range(max_epoch), ncols=70)
    for epoch_num in iterator:
        for i_batch, sampled_batch in enumerate(trainloader):
            image_batch, label_batch = sampled_batch['image'], sampled_batch['label']
            image_batch, label_batch = image_batch.cuda(), label_batch.cuda()
            outputs = model(image_batch)
            loss_ce = ce_loss(outputs, label_batch[:].long())
            loss_dice = dice_loss(outputs, label_batch, softmax=True)
            loss = 0.5 * loss_ce + 0.5 * loss_dice
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            lr_ = base_lr * (1.0 - iter_num / max_iterations) ** 0.9
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr_

            iter_num = iter_num + 1
            writer.add_scalar('info/lr', lr_, iter_num)
            writer.add_scalar('info/total_loss', loss, iter_num)
            writer.add_scalar('info/loss_ce', loss_ce, iter_num)

            logging.info('iteration %d : loss : %f, loss_ce: %f' % (iter_num, loss.item(), loss_ce.item()))

            if iter_num % 20 == 0:
                image = image_batch[1, 0:1, :, :]
                image = (image - image.min()) / (image.max() - image.min())
                writer.add_image('train/Image', image, iter_num)
                outputs = torch.argmax(torch.softmax(outputs, dim=1), dim=1, keepdim=True)
                writer.add_image('train/Prediction', outputs[1, ...] * 50, iter_num)
                labs = label_batch[1, ...].unsqueeze(0) * 50
                writer.add_image('train/GroundTruth', labs, iter_num)

        save_interval = 50  # int(max_epoch/6)
        if epoch_num > int(max_epoch / 2) and (epoch_num + 1) % save_interval == 0:
            save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')
            torch.save(model.state_dict(), save_mode_path)
            logging.info("save model to {}".format(save_mode_path))

        if epoch_num >= max_epoch - 1:
            save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')
            torch.save(model.state_dict(), save_mode_path)
            logging.info("save model to {}".format(save_mode_path))
            iterator.close()
            break

    writer.close()
    return "Training Finished!"


import requests

def test_calculate_bmr():
    url = 'http://127.0.0.1:8000/calculate_bmr'

    # Example input data, replace with appropriate values for testing
    input_data = {
        'weight': 70,
        'height': 180,
        'age': 30,
        'sex': 'male'
    }

    # Send a POST request with JSON data
    response = requests.post(url, json=input_data)

    # Check the response status code
    if response.status_code == 200:
        # Parse the JSON response
        data = response.json()
        print("BMR:", data.get('bmr'))
    else:
        print("Error:", response.json())

if __name__ == "__main__":
    test_calculate_bmr()


# -*- coding: utf-8 -*-
"""
Created on Fri Jun  4 12:16:50 2021

@author: lukako
"""
int1 = 1
int2 = 1
int3 = 1
count = 2
def fibonacci (int1, int2, stev):
    global int3
    global count
    if count >= stev:
        print(int3)
        return
    else:
        int3 = int1 + int2
        int1 = int2
        int2 = int3
        count += 1
        #print (stev, int3)
        fibonacci(int1, int2, stev)
stev = input("Vnesi željeno številko zaporedja: ")
fibonacci(1, 1, int(stev))


# compare two topics by first creating a superset of all words in the two topics
# then create a vector for each topic with the word counts, filling in 0 for words that are not in the topic
# then compute the cosine similarity between the two vectors

from typing import List, Tuple, Dict, NamedTuple, Callable, Optional, Set
from collections import defaultdict

import numpy as np
from scipy.spatial.distance import cosine

Representation = List[Tuple[str, float]]

def make_comparable(representations: List[Representation]) -> List[Representation]:
    # clone the representations so we can modify them
    representations = [r[:] for r in representations]

    # create a superset of all words in all the representations
    words = set()
    for representation in representations:
        words.update([w for w, _ in representation])

    for representation in representations:
        for word in words:
            if word not in [word for word, score in representation]:
                representation.append((word, 0))
    for representation in representations:
        representation.sort(key=lambda x: x[0])

    assert all(len(representation) == len(words) for representation in representations)
    return representations

def compare_topics(topic1: Representation, topic2: Representation) -> float:
    topic1, topic2 = make_comparable([topic1, topic2])
    count_vector1 = np.array([c for _, c in topic1])
    count_vector2 = np.array([c for _, c in topic2])
    return float(1 - cosine(count_vector1, count_vector2))

class ScoredMatch(NamedTuple):
    topic_id: int
    similarity: float
Strategy = Callable[[List[Representation]], Representation]

latest_strategy: Strategy = lambda representations: representations[-1]
first_strategy: Strategy = lambda representations: representations[0]
def average_representations(representations: List[Representation]) -> Representation:
    assert len(representations) > 0

    if len(representations) == 1:
        return representations[0]

    representations = make_comparable(representations)

    # average the scores for each word
    words = [word for word, _ in representations[0]]
    scores = np.array([0.0 for _ in words])
    for representation in representations:
        scores += np.array([score for _, score in representation])
    scores = scores / len(representations)

    return list(zip(words, scores))
average_strategy: Strategy = average_representations

class KnownTopics:
    def __init__(self,
                 strategy: Strategy = latest_strategy,
                 threshold: float = 0.5):
        self.topic_dict: Dict[int, List[Representation]] = {}
        self.strategy = strategy
        self.threshold = threshold

    def topics(self) -> List[List[Representation]]:
        return list(self.topic_dict.values())

    # TODO: Currently this can match multiple new topics to the same old topic
    #       Think about whether this is a problem
    def add_topics(self, topics: List[Representation]) -> Tuple[Dict[int, Representation], List[int]]:
        new_overall_topic_dict = self.topic_dict.copy()
        these_topics = {}

        # this can match multiple new topics to the same old topic
        # record when that happens

        new_matches = defaultdict(list)

        for topic in topics:
            best_match = self.find_best_match(topic)
            if best_match is None or best_match.similarity < self.threshold:
                # new global topic
                topic_id = len(new_overall_topic_dict)
                new_overall_topic_dict[topic_id] = [topic]
            else:
                # It's a match!
                topic_id = best_match.topic_id
                new_overall_topic_dict[topic_id].append(topic)
                new_matches[topic_id].append((best_match.similarity, topic))
            these_topics[topic_id] = topic

        # find the topics that were matched to multiple new topics
        group_matches = {id:matches for id, matches in new_matches.items() if len(matches) > 1}
        if len(group_matches) > 0:
            print("!!!!!!MULTI-MATCH WARNING!!!!!!!")
        for id, matches in group_matches.items():
            print(f"Multiple new topics matched to old topic {id}:")
            for match in matches:
                print(f"  {match[0]}: {match[1]}")

        matched_topic_ids = [id for id in these_topics.keys() if id in self.topic_dict.keys()]

        self.topic_dict = new_overall_topic_dict

        return these_topics, matched_topic_ids

    def find_best_match(self,
                        new_topic: Representation,
                        strategy: Optional[Strategy] = None) -> Optional[ScoredMatch]:
        if strategy is None:
            strategy = self.strategy
        best_match: Optional[ScoredMatch] = None
        for topic_id, representations in self.topic_dict.items():
            similarity = compare_topics(strategy(representations), new_topic)
            if best_match is None or similarity > best_match.similarity:
                best_match = ScoredMatch(topic_id, similarity)
        return best_match


from setuptools import setup, find_packages

with open("requirements.txt") as f:
	install_requires = f.read().strip().split("\n")

# get version from __version__ variable in demo/__init__.py
from demo import __version__ as version

setup(
	name="demo",
	version=version,
	description="it is my first demo frappe app",
	author="Sritarun",
	author_email="lakkarajusri17@gmail.com",
	packages=find_packages(),
	zip_safe=False,
	include_package_data=True,
	install_requires=install_requires
)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
自适应阈值
前面看到简单阈值是一种全局性的阈值，只需要规定一个阈值值，整个图像都和这个阈值比较。
而自适应阈值可以看成一种局部性的阈值，通过规定一个区域大小，
比较这个点与区域大小里面像素点的平均值（或者其他特征）的大小关系确定这个像素点是属于黑或者白（如果是二值情况）。
使用的函数为：cv2.adaptiveThreshold（）
该函数需要填6个参数：
    第一个原始图像
    第二个像素值上限
    第三个自适应方法Adaptive Method:
        — cv2.ADAPTIVE_THRESH_MEAN_C ：领域内均值
        — cv2.ADAPTIVE_THRESH_GAUSSIAN_C ：领域内像素点加权和，权重为一个高斯窗口
    第四个值的赋值方法：只有cv2.THRESH_BINARY 和cv2.THRESH_BINARY_INV
    第五个Block size:规定领域大小（一个正方形的领域）
    第六个常数C，阈值等于均值或者加权值减去这个常数（为0相当于阈值 就是求得领域内均值或者加权值）
这种方法理论上得到的效果更好，相当于在动态自适应的调整属于自己像素点的阈值，而不是整幅图像都用一个阈值。
"""

import cv2
import matplotlib.pyplot as plt

# 读取图像
img = cv2.imread('./images/cv_flower2.jpg')

# 灰度转换
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 阈值列表
size_list = range(5, 126, 4)
# 参数列表
param_list = [
    [cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY],
    [cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV],
    [cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY],
    [cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV]
]
# 标题列表
title_list = ['MEAN-BINARY', 'MEAN-INV', 'GAUSSIAN-BINARY', 'GAUSSIAN-INV']

# 多子图绘制
fig, axes = plt.subplots(len(size_list), len(param_list) + 2, sharey=False, sharex=False)
# 设置图片尺寸
fig.set_size_inches(12, len(size_list) * 12 // 7)
# 总标题
fig.suptitle('')
# 设置标题(中文字体)
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

for i in range(len(size_list)):
    for j in range(len(param_list) + 2):
        if j == 0:
            show = img
            title = '原图'
        elif j == 1:
            show = gray
            title = '灰度图'
        else:
            param = param_list[j - 2]
            # 简单阈值
            show = cv2.adaptiveThreshold(gray, 255, param[0], param[1], size_list[i], 0)
            title = "%s-Block:%d" % (title_list[j - 2], size_list[i])

        ax = axes[i][j]
        # 显示图片
        ax.imshow(show, 'gray')
        # 显示标题
        ax.set_title(title)
        # 隐藏坐标轴
        ax.set_xticks([])
        ax.set_yticks([])

# 调整每隔子图之间的距离
plt.tight_layout()
# 保存图片
plt.savefig('./images/cv_img_threshold_adpt_result.jpg')
# 显示绘制后的图片
plt.show()