import pandas as pd
import numpy as np
from re import compile as rcompile
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.optimizers import Adam, Nadam
from sklearn.neighbors import NearestNeighbors
from joblib import load
from zipfile import ZipFile
from os.path import dirname

DIR = dirname(__file__)

rex = rcompile('[^a-zA-Z 0-9]')

tokenize = lambda x: rex.sub('', x.lower().replace(',', ' '))

MODELS_DIR = DIR + '/../../models/'
DATA_DIR = DIR + '/../../data/'

ENCODER = 'encoder.h5'
FG_ENCODER = 'fg_encoder.h5'

ENCODER_PATH = MODELS_DIR + ENCODER + '.zip'
ENCODED_DTM = MODELS_DIR + 'encoded_dtm.pkl'
TFIDF = MODELS_DIR + 'tfidf.pkl'

FG_ENCODER_PATH = MODELS_DIR + FG_ENCODER
FG_ENCODED_DF = MODELS_DIR + 'fg_encoded_df.pkl'
GENRES_TFIDF = MODELS_DIR + 'genres_tfidf.pkl'
SCALER = MODELS_DIR + 'scaler.pkl'

TRACKS = DATA_DIR + 'tracks_genres_lyrics_en.csv.zip'

class FindSongs(object):
    def __init__(self):

        with ZipFile(ENCODER_PATH, 'r') as zipObj:
            zipObj.extractall()

        self.encoder = load_model(ENCODER)
        self.tfidf = load(TFIDF)
        self.encoded_dtm = load(ENCODED_DTM)
        # Fit on DTM
        self.nn = NearestNeighbors(n_neighbors=5, algorithm='ball_tree')
        self.nn.fit(self.encoded_dtm)

        self.features = [
            'popularity', 'duration_ms', 'explicit', 'danceability',
            'energy', 'key', 'loudness', 'mode', 'speechiness',
            'acousticness', 'instrumentalness', 'liveness', 'valence',
            'tempo', 'time_signature'
        ]
        self.fg_encoder = load_model(FG_ENCODER_PATH)
        self.fg_encoded_df = load(FG_ENCODED_DF)
        self.genres_tfidf = load(GENRES_TFIDF)
        self.scaler = load(SCALER)
        self.fg_nn = NearestNeighbors(n_neighbors=5, algorithm='ball_tree')
        self.fg_nn.fit(self.fg_encoded_df)

        self.tracks_df = pd.read_csv(TRACKS)

        self.tracks_df.drop(columns=['Unnamed: 0'], inplace=True)
        self.tracks_df = self.tracks_df[self.tracks_df.genres.isna() == False]

    def find_song_entries(self, x):
        vec = self.tfidf.transform([tokenize(x)]).todense()
        encoded_vec = self.encoder.predict(vec)
        entries = self.nn.kneighbors(encoded_vec)[1][0].tolist()
        entries = self.tracks_df.iloc[entries].popularity.sort_values(ascending=False).index.tolist()

        return self.tracks_df.loc[entries]

    def find_song_entry(self, x, best_choice=True):
        df = self.find_song_entries(x)

        choice = df.index.tolist()
        if best_choice:
            choice = choice[0]

        return df.loc[choice]

    def get_recommendations(self, x):

        gvec = self.genres_tfidf.transform([tokenize(x.genres)]).todense()
        fvec = self.scaler.transform([x[self.features]])
        vec = [fvec.tolist()[0] + gvec.tolist()[0]]
        encoded_vec = self.fg_encoder.predict(vec)
        entries = self.fg_nn.kneighbors(encoded_vec)[1][0].tolist()
        entries = self.tracks_df.iloc[entries].popularity.\
            sort_values(ascending=False).index.tolist()

        return self.tracks_df.loc[entries]


#!/usr/bin/env python
# license removed for brevity
import rospy
import rostopic
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime

rospy.init_node('talker', anonymous=True)
#topics='/test'
topics='/zed/rgb/image_rect_color/compressed'
r = rostopic.ROSTopicHz(-1)
s = rospy.Subscriber(topics, rospy.AnyMsg, r.callback_hz, callback_args=topics)
rate_array=[]
times=[]
rate = rospy.Rate(10) # 10hz
init_time = datetime.now()
fig=plt.figure()
ax = fig.add_subplot(111)
i=0
while not rospy.is_shutdown():
    ret = r.get_hz(topics)
    i+=1
    if ret is None:
        print("no new messages")
    else:
        rates, min_delta, max_delta, std_dev, window = ret
        now = datetime.now()
        sec=(now-init_time).total_seconds()
        print('average rate:%.3f time:%.3f\n'%(rates,sec))
        rate_array.append(rates)
        #times.append(i)

        times.append(sec)

    #r.print_hz(['/test'])
    plt.plot(times,rate_array,'b',label='fps')
    #plot1, = ax.plot(times,rate_array,'b',label='fps')
    plt.xlabel('times(sec)')
    plt.ylabel('fps')
    rate.sleep()
fig.autofmt_xdate()
#ax.legend(['image'])
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Mon Oct  7 14:20:27 2019

@author: Christian Post
"""
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, KFold
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans

# Breast cancer dataset description
# https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic)
cancer = load_breast_cancer()

features = cancer.feature_names

cancer_df = pd.DataFrame(data=cancer.data, columns=features)
cancer_df['target'] = cancer.target

# scale the data for KNN.
scaler = StandardScaler()
scaler.fit(cancer_df[features])
cancer_df[features] = scaler.transform(cancer_df[features])

# Create decision tree classifer object for feature selection
clf_params = {
        'random_state': 1,
        'n_jobs': -1,
        'n_estimators': 200,
        'max_depth': None
        }
clf = RandomForestClassifier(**clf_params)
# Train the model
clf.fit(cancer_df[features], cancer_df['target'])
# Calculate feature importances
importances = clf.feature_importances_
# Sort feature importances in descending order
indices = np.argsort(importances)[::-1]
# Rearrange feature names so they match the sorted feature importances
names = [features[i] for i in indices]

# Create plot
plt.figure(figsize=(12, 6))
# Create plot title
plt.title("Feature Importance")
# Add bars
plt.barh(range(cancer_df[features].shape[1])[::-1], importances[indices])
# Add feature names as x-axis labels
plt.yticks(range(cancer_df[features].shape[1])[::-1], names)
plt.show()

# K Means
df = pd.DataFrame(data=cancer.data, columns=cancer.feature_names)
df['target'] = cancer.target

X = df[names]
# select 3 best features
#X = df[names[:3]]
y = df['target']

# Number of clusters
kmeans = KMeans(n_clusters=2)
# Fitting the input data
kmeans = kmeans.fit(X)
# Getting the cluster labels
labels = kmeans.predict(X)
# Centroid values
C = kmeans.cluster_centers_

plt.rcParams['figure.figsize'] = (12, 8)
fig = plt.figure()
ax = Axes3D(fig)
ax.view_init(azim=60, elev=60)
ax.set_xlabel(names[0])
ax.set_ylabel(names[2])
ax.set_zlabel(names[3])
ax.scatter(X[names[0]], X[names[1]], X[names[2]], s=36, c=cancer.target)
ax.scatter(C[:, 0], C[:, 1], C[:, 2], marker='*', c='#050505', s=1000)
plt.savefig('Breast_cancer_clustering.png', bbox_inches='tight')
plt.show()

# KNN with 10 most important features
accuracies = []
for i in range(100):
    KNN = KNeighborsClassifier()
    X_train, X_valid, y_train, y_valid = train_test_split(cancer_df[names[:10]],
                                                          cancer_df['target'],
                                                          test_size=0.3,
                                                          random_state=None)
    KNN.fit(X_train, y_train)
    acc = KNN.score(X_valid, y_valid)
    accuracies.append(acc)
print(f'\nMean accuracy with KNN for validation set: {round(np.mean(accuracies), 2)}')


import re
import os

import requests
from bs4 import BeautifulSoup
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')

engine = create_engine(SQLALCHEMY_DATABASE_URI, convert_unicode=True, echo=False)
Base = declarative_base()
Base.metadata.reflect(engine)

from sqlalchemy.orm import relationship

class Item(Base):
    __table__ = Base.metadata.tables['items']

countries = ['Australia', 'Canada', 'China', 'Ireland',
                  'New Zealand', 'United States', 'United Kingdom']
country_base_url = 'http://www.numbeo.com/cost-of-living/country_result.jsp?country='

def get_city_urls():
    # example: {city:{country:country, url:url}}
    cities = {}
    for country in countries:
        country_url = country_base_url + country.replace(' ', '+')
        r = requests.get(country_url)
        soup = BeautifulSoup(r.text)
        full_data_urls(country, cities, soup)
        partial_data_urls(country, cities, soup)
    return cities

def full_data_urls(country, cities, soup):
        tds = soup.findAll('td', { 'class' : 'cityOrCountryInIndicesTable' })
        soup = BeautifulSoup(str(tds))
        anchors = soup.findAll('a')
        for a in anchors:
            cities[a.contents[0]] = {'country':country, 'url':a['href']}

def partial_data_urls(country, cities, soup):
        base_url = 'http://www.numbeo.com/cost-of-living/'
        first_sibling = soup.find(
                text = re.compile(
                "Cost of Living in  this country for some cities:"
                ))
        anchors = first_sibling.findAllNext('a',
                attrs={"href":re.compile("^city_result")})
        for a in anchors:
            cities[a.contents[0]] = {'country' : country,
                                    'url' : base_url + a['href']}

def get_cities_price_data(cities, db_session):
    for city in cities:
        data = cities[city]
        url = data['url']
        r = requests.get(url)
        soup = BeautifulSoup(r.text)
        last_updated = soup.find(text = re.compile("Last update"))
        trs = soup.findAll('tr', { 'class' : ['tr_standard', 'tr_highlighted'] })
        soup = BeautifulSoup(str(trs))
        tds = soup.findAll('td')
        for i in range(len(tds)):
            if tds[i].has_attr('class') and 'priceValue' in tds[i].attrs['class']:
                name = tds[i-1].contents[0]
                price = tds[i].contents[0]
                country = data['country']
                item = Item(name=name, price=price, city=city,
                            country=country, url=url,last_updated=last_updated)
                db_session.add(item)
        db_session.commit()

def delete_database(db_session):
    for item in db_session.query(Item).all():
        db_session.delete(item)
        print(item)
    db_session.commit()

if __name__ == '__main__':
    from sqlalchemy.orm import scoped_session, sessionmaker, Query
    db_session = scoped_session(sessionmaker(bind=engine))
    delete_database(db_session)
    get_cities_price_data(get_city_urls(), db_session)
    for item in db_session.query(Item).all():
        print((item.name, item.price, item.city))


import unittest
from src.MyInventoryAllocator import *

class TestInventory(unittest.TestCase):

    #Test cases provided by the examiner
    def testGivenExamples(self):
        inputs = [({ 'apple': 1 }, [{ 'name': 'owd', 'inventory': { 'apple': 1 } }]),
                          ({ 'apple': 1 }, [{ 'name': 'owd', 'inventory': { 'apple': 0 } }]),
                          ({ 'apple': 10 }, [{ 'name': 'owd', 'inventory': { 'apple': 5 } }])]
        outputs = [[{ 'owd': { 'apple': 1 } }],[],[]]

        for i in range(len(inputs)):
            self.assertEqual(Inventory(inputs[i][0], inputs[i][1]).OrderShipment(), outputs[i])

    #Test contain empty warehouses and orders. Should return empty lists
    def testEmptyShipment(self):
        inputs = [({},[]),({ 'apple': 1, 'orange':2, 'berry': 4},[]),({},[{ 'name': 'owd', 'inventory': { 'apple': 1 } },{ 'name': 'dm', 'inventory': { 'orange':2 }},]),]
        outputs = [[],[],[]]

        for i in range(len(inputs)):
            self.assertEqual(Inventory(inputs[i][0],inputs[i][1]).OrderShipment(),outputs[i])

if __name__ == '__main__':
    unittest.main()


neighborhood=list(map(int,input().split('@')))

position=0
count=0

command=input()
while not command=='Love!':
    word,value=command.split()
    value=int(value)

    if position+value>len(neighborhood)-1:
        position=0
        value=0

    if neighborhood[position+value]==0:
        print(f"Place {position+value} already had Valentine's day.")
    else:
        neighborhood[position+value]-=2

        if neighborhood[position+value]==0:
            print(f"Place {position+value} has Valentine's day.")

    position+=value
    command = input()

for el in neighborhood:
    if el>0:
        count+=1

print(f"Cupid's last position was {position}.")
if sum(neighborhood)==0:
    print(f'Mission was successful.')
else:
    print(f"Cupid has failed {count} places.")

Exercise Lists Advanced/Heart Delivery.py


class Utils:
  def __init__(self):
    self.__something = 0

  def validSequences(self, length):
    sequences = []
    for i in range(length):
      tempSequence = []
      for j in range(i,length):
        tempSequence.append(j)
        newTemp = []
        for k in tempSequence:
            if k not in newTemp:
                newTemp.append(k)
        newTemp.sort()
        sequences.append(newTemp)
    return sequences

  def makeCSSentence(self, l1Sent, l1Sequence, l2Sent, align):
    #print l2Sent;#print sequence;#print align;#print l1Sent;#print sequence;
    csSent = []
    addedFlag = 0
    l2Sequence = self.l2Sequence(l1Sequence, align)
    #print l2Sequence
    for i in range(len(l1Sent)):
        if i < l1Sequence[0] or i> l1Sequence[-1]:
            csSent.append(l1Sent[i])
        elif addedFlag == 0:
            for j in l2Sequence:
                csSent.append(l2Sent[j])
            addedFlag = 1
    return csSent

  def wordTags(self, sentence):
    ## Note the delimiter. It's _# and not _ :P
    wordTags = []
    sentence = sentence.strip()
    spaceSplitSent = sentence.split()
    for i in range(len(spaceSplitSent)):
      if spaceSplitSent[i][-1] == ")":
        wordTags.append([spaceSplitSent[i].strip(")"), spaceSplitSent[i-1].strip("(")])
    return wordTags

  def l2Sequence(self, l1Sequence, align):
    l2Sequence = []
    for i in l1Sequence:
      if i in align.keys():  # What about else? Don't need to worry because the sequence in use is already checked for contiguity on L2 side!!
        for j in align[i]:
          if j not in l2Sequence:
            l2Sequence.append(j)
    l2Sequence.sort()
    return l2Sequence


#-*- encoding: utf-8 -*-
import sys
r=sys.stdin.readline

area = {}        # 평면

for i in range(1, 101):
    area[i] = [0] * 101

result = 0

for i in range(4):          # 4개의 직사각형
    x1, y1, x2, y2 = map(int, r().split())
    for y in range(y1, y2):
        for x in range(x1, x2):
            if area[y][x] == 0:
                area[y][x] = 1
                result +=1

print(result)

Algorithm/Baekjoon/02669 직사각형 네개의 합집합의 면적 구하기/2669.py


#-------------------------------------------------------------------------------
# Name:        Chip CPU Module
# Purpose:
#
# Author:      pixman
#
# Created:     01/10/2012
# Copyright:   (c) pixman 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import memory
import pygame
import sys

class CPU:
    def __init__(self,memory,VirtualMachine=None,Debug=None):
        print ("Initiating CPU instance")
        self.Debugging = Debug
        self.Memory = memory
        self.VM = VirtualMachine
        self.CreateRegisters()
    def CreateRegisters(self):
        print ("Creating Registers")
        self.Registers = [0 for x in range(0x0,0x10)]
        self.I = int(0x00)
        self.ProgramCounter= int (0x200)
        self.StackPointer = int(0)
        self.Stack = []
        self.DelayTimer = int(0x0)
        self.SoundTimer = int(0x0)
        self.RawOpcode = ""
    def Push(self,value):
        if self.StackPointer>15:
            if self.Debugging:
                print ("Stack is full, couldn't process PUSH()")
            return (-1)
        else:
            if self.Debugging:
                print ("StackPointer plus 1")
            self.Stack.append(value)
            self.StackPointer=+1
            return value
    def Pop(self):
        if self.StackPointer < 0:
            print ("Stack Pointer points to below zero, can't process POP()")
            sys.exit()
        else:
            if self.Debugging:
                print ("StackPointer minus 1")
            self.StackPointer -= 1
            return self.Stack.pop()

    def UpdateTimer(self):
        if self.DelayTimer != 0:
            self.DelayTimer -= 1
    def NextOpcode(self):
        self.OpcodePart =  ord(str(self.Memory.Mem[self.ProgramCounter]))
        if self.Debugging:
            print ("Opcode-Daten:", hex(self.OpcodePart), hex(ord(str(self.Memory.Mem[self.ProgramCounter+1]))))
        self.OpcodePart <<= 8
        self.OpcodePart |= ord(str(self.Memory.Mem[self.ProgramCounter+1]))
        self.OpcodePart = "0x%0.4X" % self.OpcodePart
        if self.Debugging:
            print ("Current Program Counter:",self.ProgramCounter)
        self.VM.Cpu.ProgramCounter += 2
        if self.Debugging:
            print ("Program Counter prognosis:", self.VM.Cpu.ProgramCounter)

        return self.OpcodePart
    def DecodeOpcode(self,opCode):
        rawOpCode = opCode[2:]
        Found = False
        switch = None
        x = 0
        OpcodeTable = { "0NNN" : 1,
                        "1NNN" : 2,
                        "8NN3" : 3,
                        "2NNN" : 4,
                        "ANNN" : 5,
                        "3NNN" : 6,
                        "7NNN" : 7,
                        "DNNN" : 8,
                        "6NNN" : 9,
                        "4NNN" : 10,
                        "FN33" : 11,
                        "FN55" : 12,
                        "FN65" : 13,
                        "FN29" : 14,
                        "00EE" : 15,
                        "FN07" : 16,
                        "FN15" : 17,
                        "CNNN" : 18,
                        "FN1E" : 19,
                        "ENA1" : 20,
                        "EN9E" : 21,
                        "8NN0" : 22,
                        "9NN0" : 23,
                        }
        print ("Opcode: ", opCode[0:])
        firstNumber = str(opCode)[2] + "NNN"
        secondNumber = "N" + str(opCode)[3] + "NN"
        secondAndLast = "N" + str(opCode)[3] + "N" + str(opCode)[5]
        lastTwoNumbers = "NN" + str(opCode)[4:6]
        firstAndLast = str(opCode)[2] + "NN" + str(opCode)[5:6]
        firstAndLastTwo = str(opCode)[2] + "N" + str(opCode)[4:6]
        possibleOpCodes = [firstNumber, secondNumber, secondAndLast, lastTwoNumbers, firstAndLast, firstAndLastTwo, str(rawOpCode)]
        for i in possibleOpCodes:
            if i in OpcodeTable:
                switch = OpcodeTable[i]
                Found = True
        if Found == False:
            if self.Debugging:
                print ("Opcode",opCode, "not found")
        else:
            print ("Opcode",switch,"found")
        if switch == 1:
            self.Opcode_0NNN(rawOpCode)
        if switch == 2:
            self.Opcode_1NNN(rawOpCode)
        if switch == 4:
            self.Opcode_2NNN(rawOpCode)
        if switch == 5:
            self.Opcode_ANNN(rawOpCode)
        if switch == 6:
            self.Opcode_3NNN(rawOpCode)
        if switch == 7:
            self.Opcode_7NNN(rawOpCode)
        if switch == 8:
            self.Opcode_DNNN(rawOpCode)
        if switch == 9:
            self.Opcode_6NNN(rawOpCode)
        if switch == 15:
            self.Opcode_00EE()

        if self.Debugging:
            print ("Decoding", possibleOpCodes)
    def Reset(self):
        self.Memory = memory.Memory(self)
        self.CreateRegisters(self)

    def Opcode_00E0(self):
        self.VM.Screen.fill((0,0,0))
    def Opcode_00EE(self):
        self.ProgramCounter = self.Pop()

        if self.Debugging:
            print ("Used",self.RawOpcode)
    def Opcode_0NNN(self,rawopc):
        if self.Debugging:
            print ("Used", self.RawOpcode)
    def Opcode_1NNN(self,rawopc):
        self.RawOpcode = rawopc
        if self.Debugging:
            print ("Used", self.RawOpcode)
        self.ProgramCounter = int(str( self.RawOpcode[1:]),16)
    def Opcode_2NNN(self, rawopc):
        self.RawOpcode = rawopc
        self.Push(self.ProgramCounter)
        if self.Debugging:
            print("Used", self.RawOpcode)
        self.ProgramCounter =  int(str(self.RawOpcode[1:]),16)
    def Opcode_ANNN(self,rawopc):
        self.RawOpcode = rawopc
        self.I = int(str(self.RawOpcode[1:]),16)
        if self.Debugging:
            print ("Used", self.RawOpcode)
    def Opcode_3NNN(self,rawopc):
        self.RawOpcode = rawopc
        if self.Registers[int(self.RawOpcode[1:2],16)] == int(self.RawOpcode[2:],16):
            self.ProgramCounter +=2
        # Function goes on and skips the next instruction (adding 2 to the PC)
    def Opcode_6NNN(self,rawopc):
        self.RawOpcode = rawopc
        self.Registers[int(self.RawOpcode[1],16)] == int(self.RawOpcode[2:],16)
    def Opcode_7NNN(self,rawopc):
        self.RawOpcode = rawopc
        self.Registers[1] = self.Registers[1] +  int(str(self.RawOpcode[2:]))
        #print (self.Registers)
       # print ("Register 2: ", int(self.Registers[1]))
        #print ("Byte from Opcode:",str(self.RawOpcode[2:]))
        # sys.exit("ENDE!") # LOESCHEN!
        if self.Debugging:
            print ("Used", self.RawOpcode)
    def Opcode_DNNN(self,rawopc):
        print ("SPrites are NEXT!")


from func import check, square
import numpy as np

def work():
    while 1:
        try:
            file = open('text.txt', 'r')
        except Exception:
            print("Невозможно найти путь к файлу")
            continue

        n = int(file.readline())
        print(n)
        x = []
        y = []
        z = []
        while n > 0:
            x.append(float(file.readline()))
            y.append(float(file.readline()))
            print(x, y, sep='\n')
            n -= 1
        file.close()
        res = 0
        k = np.size(x)
        for i in range(0, k, 1):
            if i == (k - 1):
                z.append(square(x[0], x[k - 1], y[1], y[k - 1]))
            else:
                z.append(square(x[i], x[i + 1], y[i], y[i + 1]))
            res += z[i]
        print("Площадь н-угольника ровняется: " + str(res))
        f = open('ans.txt', 'w')
        f.write("Остаточный ответ:  " + str(res))
        f.close()

        break

if __name__ == "__main__":
    work()


from graph import graph, RDF, OWL, FOAF, VIVO, STUDYBOT
from data import universities
from util import to_snake_case

from rdflib import URIRef, Literal

def convert():
    for i, row in universities.iterrows():
        name = row['Name']
        sameAs = row['Same As']
        uni_resource = STUDYBOT[to_snake_case(name)]
        same_as_resource = URIRef(sameAs)

        graph.add((
            uni_resource, RDF.type, VIVO.University
        ))
        graph.add((
            uni_resource, FOAF.name, Literal(name, lang='en')
        ))
        graph.add((
            uni_resource, OWL.sameAs, same_as_resource
        ))

if __name__ == "__main__":
    convert()

    print(
        graph.serialize(format='turtle').decode('utf-8')
    )


from loda.acting import ActorBase
import sys

class Actor(ActorBase):  # pragma: no cover
    receive = ['(.*)']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def perform(self, *args):
        sys.stdout.write(
            '%s\n' % ', '.join(
                str(a) for a in args
            )
        )


"""
This module contains wrapper code to the DEPRECATED paid thumbnail API v2.
Please use Image.get_thumbnail_url instead, as it uses the newer, complementary thumbnail service.
"""
import logging
try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode

from piplapis.data import Image
from piplapis.data.utils import to_utf8

logger = logging.getLogger(__name__)
logger.warning("The thumbnail API in piplapis.thumbnail is depracated.")

BASE_URL = 'http://api.pipl.com/thumbnail/v2/?'
# HTTPS is also supported:
#BASE_URL = 'https://api.pipl.com/thumbnail/v2/?'

MAX_PIXELS = 500

# Default API key value, you can set your key globally in this variable instead
# of passing it in each call to generate_thumbnail_url().
# >>> import piplapis.thumbnail
# >>> piplapis.thumbnail.default_api_key = '<your_key>'
default_api_key = None

def generate_thumbnail_url(image_url, height, width, favicon_domain=None,
                           zoom_face=True, api_key=None):
    """Take an image URL and generate a thumbnail URL for that image.

    Args:

    image_url -- unicode (or utf8 encoded str), URL of the image you want to
                 thumbnail.
    height -- int, requested thumbnail height in pixels, maximum 500.
    width -- int, requested thumbnail width in pixels, maximum 500.
    favicon_domain -- unicode (or utf8 encoded str), optional, the domain of
                      the website where the image came from, the favicon will
                      be added to the corner of the thumbnail, recommended for
                      copyright reasones.
                      IMPORTANT: Don't assume that the domain of the website is
                      the domain from `image_url`, it's possible that
                      domain1.com hosts its images on domain2.com.
    zoom_face -- bool, indicates whether you want the thumbnail to zoom on the
                 face in the image (in case there is a face) or not.
    api_key -- str, a valid API key (use "samplekey" for experimenting).
               Note that you can set a default API key
               (piplapis.thumbnail.default_api_key = '<your_key>') instead of
               passing your key in each call.

    ValueError is raised in case of illegal parameters.

    Example (thumbnail URL from an image URL):

    >>> from piplapis.thumbnail import generate_thumbnail_url
    >>> image_url = 'http://a7.twimg.com/a/ab76f.jpg'
    >>> generate_thumbnail_url(image_url, 100, 100,
                               favicon_domain='twitter.com',
                               api_key='samplekey')
    'http://api.pipl.com/thumbnail/v2/?key=samplekey&
    favicon_domain=twitter.com&height=100&width=100&zoom_face=True&
    image_url=http%3A%2F%2Fa7.twimg.com%2Fa%2Fab76f.jpg'

    Example (thumbnail URL from a record that came in the response of our
    Search API):

    >>> from piplapis.thumbnail import generate_thumbnail_url
    >>> generate_thumbnail_url(record.images[0].url, 100, 100,
                               favicon_domain=record.source.domain,
                               api_key='samplekey')
    'http://api.pipl.com/thumbnail/v2/?key=samplekey&
    favicon_domain=twitter.com&height=100&width=100&zoom_face=True&
    image_url=http%3A%2F%2Fa7.twimg.com%2Fa%2Fab76f.jpg'

    """
    if not (api_key or default_api_key):
        raise ValueError('A valid API key is required')
    if not Image(url=image_url).is_valid_url:
        raise ValueError('image_url is not a valid URL')
    if not (0 < height <= MAX_PIXELS and 0 < width <= MAX_PIXELS):
        raise ValueError('height/width must be between 0 and %d' % MAX_PIXELS)
    query = {
        'key': to_utf8(api_key or default_api_key),
        'image_url': urllib.unquote(to_utf8(image_url)),
        'height': height,
        'width': width,
        'favicon_domain': to_utf8(favicon_domain or ''),
        'zoom_face': zoom_face,
    }
    return BASE_URL + urlencode(query)


from api.api import store, SchemaUtils
import logging.config

log = logging.getLogger(__name__)

class BasicAction(object):

    @staticmethod
    def push(request):
        request_type = SchemaUtils.get_type(request)
        print(request_type)
        savable_data = SchemaUtils.get_data(request, request_type)
        if not store.index_exists(request_type):
            store.create_index(request_type)
        store.insert(request_type, SchemaUtils.get_id(savable_data), savable_data)
        return True

    @staticmethod
    def search(request):
        return store.multi_search(SchemaUtils.get_type(request), request.get('text'), 1, 10, request.get('fields'))


# codeing=utf-8
import urllib.request
import re
import random

# iplist=['5.98.86.141:8080','24.172.34.114:8181','27.145.145.105:8080']

list1 = ['百度', '清明', '锄禾', '期待', '钓鱼岛', 'django']
for i in list1:
    # ip=random.choice(iplist)

    gjc = urllib.request.quote(i)
    url = 'http://suggestion.baidu.com/su?wd=' + gjc + \
        '&json=1&p=3&sid=&cb=jQuery110205425511478908079_1396251136074&_=1396251136078'
    headers1 = {'GET': url,
                'Host': "suggestion.baidu.com",
                'User-Agent': "Mozilla/5.0 (Windows NT 6.2; rv:28.0) Gecko/20100101 Firefox/28.0",
                'Referer': "http://www.baidu.com/"}

    # proxy_support=urllib.request.ProxyHandler({'http':'http://'+ip})
    # opener=urllib.request.build_opener(proxy_support)
    # urllib.request.install_opener(opener)

    req = urllib.request.Request(url, headers=headers1)
    html = urllib.request.urlopen(req).read().decode('gbk')
    ss = re.findall('\[.*\]', html)
    print(ss[0].strip('[').strip(']').replace('"', '').split(','))


import namegenerator
import names
import numpy as np

class PreferenceDataset(object):
    """Preference dataset abstract class."""

    def __init__(self, objects, users, fields, ratings=None,
                 n_ratings=10):
        self.objects = objects
        self.users = users
        self.fields = fields
        if ratings is not None:
            self.ratings = ratings
        else:
            self._generate_many(n_ratings=n_ratings)

    def _generate_many(self, n_ratings=10):
        self.ratings = [self._generate_one() for _ in range(n_ratings)]

    def _generate_one(self):
        r = self._generate_one_impl()
        for x in ['o1', 'o2', 'ratings', 'user']:
            assert x in r, "Missing field %s" % x
        assert r['o1'] in self.objects, "Object not found %s" % r['o1']
        assert r['o2'] in self.objects, "Object not found %s" % r['o2']
        assert r['user'] in self.users, "Object not found %s" % r['user']
        for f in self.fields:
            assert f in r['ratings'], "Field not found %s" % f
            assert 0 <= r['ratings'][f] <= 100, "Value must be in [0, 100] %s %s" \
                                                % (f, str(r['ratings'][f]))
        return r

    def _generate_one_impl(self):
        raise NotImplementedError("This class is abstract")

    def __repr__(self):
        return "<PrefDataset %s~objects[%d] %s~fields[%d] %s~users[%d] %s~ratings[%d]" % \
               (np.random.choice(self.objects), len(self.objects),
                np.random.choice(self.fields), len(self.fields),
                np.random.choice(self.users), len(self.users),
                np.random.choice(self.ratings), len(self.ratings))

class ToyRandomDataset(PreferenceDataset):
    """Random dataset."""

    def __init__(self, n_objects=10, n_users=10):
        # list of objects to consider
        objects = [namegenerator.gen() for _ in range(n_objects)]

        # fields to consider (features)
        fields = [
            "unicorniness",
            "magic_skills",
            "number_of_trump_appearances"]

        # some users
        users = [names.get_full_name() for _ in range(n_users)]

        super(ToyRandomDataset, self).__init__(objects=objects,
                                               fields=fields,
                                               users=users)

    def _generate_one_impl(self):
        """Sample one rating."""
        o1, o2 = None, None
        while o1 == o2:
            o1 = np.random.choice(self.objects)
            o2 = np.random.choice(self.objects)

        def get_rating():
            return np.random.uniform(low=0, high=100)

        user = np.random.choice(self.users)
        ratings = {f: get_rating() for f in self.fields}
        weights = {f: np.random.rand() + 1 for f in self.fields}
        return {'o1': o1, 'o2': o2, 'ratings': ratings,
                'weights': weights,
                'user': user}

class ToyHardcodedDataset(PreferenceDataset):
    """Random dataset."""

    def __init__(self):
        users = ['sergei', 'le']
        objects = ['trump_video', 'science4all_video', 'bbc_video']
        fields = ['accuracy', 'pedagogy']

        self.scores_dict = \
            {'sergei':
                {
                    'accuracy':
                        {('trump_video', 'science4all_video'): 90,
                         ('trump_video', 'bbc_video'): 85,
                         ('science4all_video', 'bbc_video'): 0
                         },
                    'pedagogy':
                        {('trump_video', 'science4all_video'): 100,
                         ('trump_video', 'bbc_video'): 71,
                         ('science4all_video', 'bbc_video'): 1
                         },
                },
                'le':
                    {
                        'accuracy':
                            {('trump_video', 'science4all_video'): 99,
                             ('trump_video', 'bbc_video'): 80,
                             ('science4all_video', 'bbc_video'): 10
                             },
                        'pedagogy':
                            {('trump_video', 'science4all_video'): 100,
                             ('trump_video', 'bbc_video'): 70,
                             ('science4all_video', 'bbc_video'): 0
                             },
                }
             }

        super(ToyHardcodedDataset, self).__init__(objects=objects,
                                                  fields=fields,
                                                  users=users)

    def _generate_one_impl(self):
        """Sample one rating."""
        o1, o2 = None, None
        user = np.random.choice(self.users)
        while o1 == o2:
            o1 = np.random.choice(self.objects)
            o2 = np.random.choice(self.objects)

        def get_rating(f):
            scores = self.scores_dict[user]

            if (o1, o2) in scores[f]:
                return scores[f][(o1, o2)]
            elif (o2, o1) in scores[f]:
                return 100 - scores[f][(o2, o1)]
            else:
                raise Exception("score not found")

        ratings = {f: get_rating(f) for f in self.fields}
        return {'o1': o1, 'o2': o2, 'ratings': ratings,
                'user': user}


"""
Módulo relacionado à preparação de comandos para execução no servidor de acesso
"""
import os
import sys

from jinja2 import Environment, FileSystemLoader, exceptions

class Comando:
    __TEMPLATES_DIR = '../includes/ps_templates'
    __TEMPLATES_EXTENSAO = '.j2'

    def __init__(self, comando, descricao=None, **kwargs):
        try:
            self.comando = comando
            self.descricao = descricao if descricao else self.comando
            self.args = kwargs
            j2_env = Environment(
                loader=FileSystemLoader(os.path.join(
                    os.path.dirname(__file__), Comando.__TEMPLATES_DIR)),
                trim_blocks=True)
            self.template = j2_env.get_template(
                self.comando + Comando.__TEMPLATES_EXTENSAO)
        except exceptions.TemplateNotFound as ex:
            print(
                f"Template '{ex}' não encontrado para o comando '{self.comando}'.")
            sys.exit(1)

    def imprimir(self):
        print('\n' + self.template.render(self.args) + '\n')

    def executar(self, servidor_acesso):
        conteudo_script = self.template.render(self.args)
        return servidor_acesso.executar_script(
            self.comando, conteudo_script)


import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

class CNN_Sent(nn.Module):
    def __init__(self, voc, embedding_size, kernel_num, pretrained_weights, dropout=0.5):
        super(CNN_Sent, self).__init__()

        self.voc = voc
        self.embedding_size = embedding_size
        self.Ci = 1 # input channel
        self.Co = kernel_num    # 100个filter

        self.embed = nn.Embedding(voc.num_words, embedding_size, padding_idx=0)
        self.embed.weight.data.copy_(torch.from_numpy(pretrained_weights))

        self.conv1 = nn.Conv2d(self.Ci, self.Co, kernel_size=(3, embedding_size))
        self.conv2 = nn.Conv2d(self.Ci, self.Co, kernel_size=(4, embedding_size))
        self.conv3 = nn.Conv2d(self.Ci, self.Co, kernel_size=(5, embedding_size))

        self.dropout = nn.Dropout(dropout)
        self.fc = nn.Linear(self.Co*3, 2)

    def conv_and_pool(self, x, conv):
        x = F.relu(conv(x)).squeeze(3)  # (Batch, Co, conv_len) 将最后一维挤压掉
        x = F.max_pool1d(x, x.size(2)).squeeze(2)   # (Batch, Co)
        return x

    def forward(self, sent):
        x_embed = self.embed(sent)  # (Batch, seq_len, embed_size)
        x_embed = x_embed.unsqueeze(1)  # (Batch, Ci, seq_len, embed_size)

        x1 = self.conv_and_pool(x_embed, self.conv1)
        x2 = self.conv_and_pool(x_embed, self.conv2)
        x3 = self.conv_and_pool(x_embed, self.conv3)

        x = torch.cat((x1, x2, x3), dim=1)  # (Batch, Co*3)
        x = self.dropout(x)
        logit = self.fc(x)  # (Batch, Class_num)
        return logit


import telebot
from  INPUT import *

@BOT.message_handler(commands = ['start'])
def send_text(bot, chat_id, file):
	f = open(file).read()
	if f[14] == "1":
		bot.send_message(chat_id, f[15:])


import heapq
import sys
sys.path.append("..")

import lybSort.SortHelper as SortHelper
from lybCollections.BinaryHeap import MaxBinaryHeap , MinBinaryHeap
from lybCollections.MinHeapFunctions import *
from lybCollections.MaxHeapFunctions import *

################################################################################################################
## Catalogue for Heap Sort
## - 1.heapSorted( arr )
## ---- Using Python built-in heapq module to implement the heapsort
## ---- The reference is : https://docs.python.org/3.4/library/heapq.html
## ---- The heapq is min heap by default
## ---- It is much faster than mannually implementation
## ---- Return the sorted list and keep the original arr
## - 2.heapSortedByMinBinaryHeap( arr )
## ---- Using Min Binary Heap to sort an arr to increasing order
## ---- Return the sorted list and keep the original arr
## - 3.heapSortedByMaxBinaryHeap( arr )
## ---- Using Max Binary Heap to sort an arr to decreasing order
## ---- Return the sorted list and keep the original arr
## - 4.heapSortedByMinHeapFunctions( arr )
## ---- Using Min Binary Heap Functions ( without an Min Binary Heap instance )to sort an arr to increasing order
## ---- Return the sorted list and keep the original arr
## - 5.heapSortedByMaxHeapFunctions( arr )
## ---- Using Max Binary Heap Functions ( without an Max Binary Heap instance )to sort an arr to decreasing order
## ---- Return the sorted list and keep the original arr
#################################################################################################################
## The property for heap sort
## - O( nlogn ) at worsted case
## - Can be sort in place, but for implement convenience, the following implementation are all not sort in place
## - The sort in place algorithm can be refered in <Algorithms, 4th> by Princeton
## - Reference : http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Heap.java.html
## - Not stable
#################################################################################################################

##---------------------------------------------------------------------------------------------------------------

######################################################################
## Heap Sorted by built-in heapq module
## - Using Python built-in heapq module to implement the heapsort
## - The reference is : https://docs.python.org/3.4/library/heapq.html
## - The heapq is min heap by default
## - It is much faster than mannually implementation
## - Return the sorted list and keep the original arr
######################################################################
def heapSorted( arr ):
    heap = []
    for value in arr:
        heapq.heappush( heap , value)
    return [ heapq.heappop(heap) for i in range(len(arr))]

#############################################################
## Heap Sorted by MinBinaryHeap
## - Using Min Binary Heap to sort an arr to increasing order
## - Return the sorted list and keep the original arr
#############################################################
def heapSortedByMinBinaryHeap( arr ):

    minHeap = MinBinaryHeap( arr )
    return [ minHeap.extractMin() for i in range(len(arr))]

#############################################################
## Heap Sorted by MaxBinaryHeap
## - Using Max Binary Heap to sort an arr to decreasing order
## - Return the sorted list and keep the original arr
#############################################################
def heapSortedByMaxBinaryHeap( arr ):

    maxHeap = MaxBinaryHeap( arr )
    return [ maxHeap.extractMax() for i in range(len(arr))]

##############################################################################################################
## Heap Sorted by MinHeapFunctions
## - Using Min Binary Heap Functions ( without an Min Binary Heap instance )to sort an arr to increasing order
## - Return the sorted list and keep the original arr
##############################################################################################################
def heapSortedByMinHeapFunctions( arr ):
    minHeap = buildMinHeap( arr )
    return [extractMin( minHeap ) for i in range(len(arr))]

##############################################################################################################
## Heap Sorted by MaxHeapFunctions
## - Using Max Binary Heap Functions ( without an Max Binary Heap instance )to sort an arr to decreasing order
## - Return the sorted list and keep the original arr
##############################################################################################################
def heapSortedByMaxHeapFunctions( arr ):
    maxHeap = buildMaxHeap( arr )
    return [extractMax( maxHeap ) for i in range(len(arr))]

if __name__ == "__main__":

    # Rightness Check
    LEN = 10**5
    MAX_NUM = 10**9
    randArr = SortHelper.generateRandomIntsList( LEN , maxNum = MAX_NUM )

    arr1 = randArr[:]
    SortHelper.testSortCorrection( heapSorted , arr1 , sortname = "heapSorted" , inplace = False )

    arr2 = randArr[:]
    SortHelper.testSortCorrection( heapSortedByMinBinaryHeap , arr2 , sortname = "heapSortedByMinBinaryHeap" , inplace = False )

    arr3 = randArr[:]
    SortHelper.testSortCorrection( heapSortedByMaxBinaryHeap , arr3 , sortname = "heapSortedByMaxBinaryHeap" , inplace = False , key = lambda E : -E )

    arr4 = randArr[:]
    SortHelper.testSortCorrection( heapSortedByMinHeapFunctions , arr4 , sortname = "heapSortedByMinHeapFunctions" , inplace = False )

    arr5 = randArr[:]
    SortHelper.testSortCorrection( heapSortedByMaxHeapFunctions , arr5 , sortname = "heapSortedByMaxHeapFunctions" , inplace = False , key = lambda E : -E )


"""Functional wrapper around the pygmo, nlopt and scipy libraries."""
import numpy as np
from joblib import delayed
from joblib import Parallel

from estimagic.config import DEFAULT_DATABASE_NAME
from estimagic.dashboard.run_dashboard import run_dashboard_in_separate_process
from estimagic.decorators import negative_gradient
from estimagic.logging.update_database import update_scalar_field
from estimagic.optimization.broadcast_arguments import broadcast_arguments
from estimagic.optimization.check_arguments import check_arguments
from estimagic.optimization.pounders import minimize_pounders_np
from estimagic.optimization.pygmo import minimize_pygmo_np
from estimagic.optimization.reparametrize import reparametrize_from_internal
from estimagic.optimization.scipy import minimize_scipy_np
from estimagic.optimization.transform_problem import transform_problem

def maximize(
    criterion,
    params,
    algorithm,
    criterion_kwargs=None,
    constraints=None,
    general_options=None,
    algo_options=None,
    gradient=None,
    gradient_kwargs=None,
    gradient_options=None,
    logging=DEFAULT_DATABASE_NAME,
    log_options=None,
    dashboard=False,
    dash_options=None,
):
    """Maximize criterion using algorithm subject to constraints and bounds.
    Each argument except for general_options can also be replaced by a list of
    arguments in which case several optimizations are run in parallel. For this, either
    all arguments must be lists of the same length, or some arguments can be provided
    as single arguments in which case they are automatically broadcasted.
    Args:
        criterion (callable or list of callables):
            Python callable that takes a pandas DataFrame with parameters as the first
            argument. Supported outputs are:
                - scalar floating point
                - np.ndarray: contributions for the tao Pounders algorithm.
                - tuple of a scalar floating point and a pd.DataFrame:
                    In this case the first output is the criterion value.
                    The second output are the comparison_plot_data.
        params (pd.DataFrame or list of pd.DataFrames):
            See :ref:`params`.
        algorithm (str or list of strings): Specifies the optimization algorithm.
            See :ref:`list_of_algorithms`.
        criterion_kwargs (dict or list of dicts): Additional keyword arguments for
            criterion.
        constraints (list or list of lists): List with constraint dictionaries.
            See :ref:`constraints`.
        general_options (dict): Additional configurations for the optimization.
            Keys can include:
                - keep_dashboard_alive (bool): Do not terminate the dashboard process
                    after the optimization(s) finish(es).
        algo_options (dict or list of dicts): Algorithm specific configurations for the
            optimization.
        gradient (callable): Gradient of the criterion function. Takes params as first
            argument and returns the gradient as numpy array or pandas Series.
        gradient_kwargs (dict): Additional keyword arguments for the gradient.
        gradient_options (dict): Options for the gradient function.
        logging (str or pathlib.Path or list): Path(s) to (an) sqlite3 file(s) which
            typically has the file extension ``.db``. If the file does not exist,
            it will be created. See :ref:`logging` for details.
        log_options (dict or list of dict): Keyword arguments to influence the logging.
            See :ref:`logging` for details.
        dashboard (bool): Whether to create and show a dashboard, default is False.
            See :ref:`dashboard` for details.
        dash_options (dict or list of dict, optional): Options passed to the dashboard.
            Supported keys are:
                - port (int): port where to display the dashboard.
                - no_browser (bool): whether to display the dashboard in a browser.
                - rollover (int): how many iterations to keep in the convergence plots.
    Returns:
        results (tuple or list of tuples): Each tuple consists of the harmonized result
        info dictionary and the params DataFrame with the minimizing parameter values
        of the untransformed problem as specified of the user.
    """
    # Set a flag for a maximization problem.
    general_options = {} if general_options is None else general_options
    general_options["_maximization"] = True

    if isinstance(gradient, list):
        gradient = [negative_gradient(grad) for grad in gradient]
    else:
        gradient = negative_gradient(gradient)

    results = minimize(
        criterion=criterion,
        params=params,
        algorithm=algorithm,
        criterion_kwargs=criterion_kwargs,
        constraints=constraints,
        general_options=general_options,
        algo_options=algo_options,
        gradient=gradient,
        gradient_kwargs=gradient_kwargs,
        gradient_options=gradient_options,
        logging=logging,
        log_options=log_options,
        dashboard=dashboard,
        dash_options=dash_options,
    )

    # Change the fitness value. ``results`` is either a tuple of results and params or a
    # list of tuples.
    if not isinstance(results, list):
        results = [results]

    results = [_undo_sign_switch(res) for res in results]

    results = results[0] if len(results) == 1 else results

    return results

def _undo_sign_switch(res):
    info, params = res
    info = info.copy()
    info["fitness"] = -info["fitness"]
    if "jacobian" in info and info["jacobian"] is not None:
        info["jacobian"] = (-np.array(info["jacobian"])).tolist()
    if "hessian" in info and info["hessian"] is not None:
        info["hessian"] = (-np.array(info["hessian"])).tolist()
    return info, params

def minimize(
    criterion,
    params,
    algorithm,
    criterion_kwargs=None,
    constraints=None,
    general_options=None,
    algo_options=None,
    gradient=None,
    gradient_kwargs=None,
    gradient_options=None,
    logging=DEFAULT_DATABASE_NAME,
    log_options=None,
    dashboard=False,
    dash_options=None,
):
    """Minimize *criterion* using *algorithm* subject to *constraints* and bounds.
    Each argument except for ``general_options`` can also be replaced by a list of
    arguments in which case several optimizations are run in parallel. For this, either
    all arguments must be lists of the same length, or some arguments can be provided
    as single arguments in which case they are automatically broadcasted.
    Args:
        criterion (callable or list of callables):
            Python callable that takes a pandas DataFrame with parameters as the first
            argument. Supported outputs are:
                - scalar floating point
                - np.ndarray: contributions for the tao Pounders algorithm.
                - tuple of a scalar floating point and a pd.DataFrame:
                    In this case the first output is the criterion value.
                    The second output are the comparison_plot_data.
        params (pd.DataFrame or list of pd.DataFrames):
            See :ref:`params`.
        algorithm (str or list of strings): Specifies the optimization algorithm.
            See :ref:`list_of_algorithms`.
        criterion_kwargs (dict or list of dicts): Additional keyword arguments for
            criterion.
        constraints (list or list of lists): List with constraint dictionaries.
            See :ref:`constraints`.
        general_options (dict): Additional configurations for the optimization.
            Keys can include:
                - keep_dashboard_alive (bool): Do not terminate the dashboard process
                    after the optimization(s) finish(es).
        algo_options (dict or list of dicts): Algorithm specific configurations for the
            optimization.
        gradient (callable): Gradient of the criterion function. Takes params as first
            argument and returns the gradient as numpy array or pandas Series.
        gradient_kwargs (dict): Additional keyword arguments for the gradient.
        gradient_options (dict): Options for the gradient function.
        logging (str or pathlib.Path or list): Path(s) to (an) sqlite3 file(s) which
            typically has the file extension ``.db``. If the file does not exist,
            it will be created. See :ref:`logging` for details.
        log_options (dict or list of dict): Keyword arguments to influence the logging.
            See :ref:`logging` for details.
        dashboard (bool): Whether to create and show a dashboard, default is False.
            See :ref:`dashboard` for details.
        dash_options (dict or list of dict, optional): Options passed to the dashboard.
            Supported keys are:
                - port (int): port where to display the dashboard.
                - no_browser (bool): whether to display the dashboard in a browser.
                - rollover (int): how many iterations to keep in the convergence plots.
    Returns:
        results (tuple or list of tuples): Each tuple consists of the harmonized result
        info dictionary and the params DataFrame with the minimizing parameter values
        of the untransformed problem as specified of the user.
    """
    arguments = broadcast_arguments(
        criterion=criterion,
        params=params,
        algorithm=algorithm,
        criterion_kwargs=criterion_kwargs,
        constraints=constraints,
        general_options=general_options,
        algo_options=algo_options,
        gradient=gradient,
        gradient_kwargs=gradient_kwargs,
        gradient_options=gradient_options,
        logging=logging,
        log_options=log_options,
        dashboard=dashboard,
        dash_options=dash_options,
    )

    check_arguments(arguments)

    optim_arguments = []
    results_arguments = []
    database_paths_for_dashboard = []
    for single_arg in arguments:
        optim_kwargs, database_path, result_kwargs = transform_problem(**single_arg)
        optim_arguments.append(optim_kwargs)
        results_arguments.append(result_kwargs)
        if database_path is not None:
            database_paths_for_dashboard.append(database_path)

    if dashboard:
        dashboard_process = run_dashboard_in_separate_process(
            database_paths=database_paths_for_dashboard
        )

    if len(arguments) == 1:
        # Run only one optimization
        results = [_internal_minimize(**optim_arguments[0])]
    else:
        # Run multiple optimizations
        if "n_cores" not in optim_arguments[0]["general_options"]:
            raise ValueError(
                "n_cores need to be specified in general_options"
                + " if multiple optimizations should be run."
            )
        n_cores = optim_arguments[0]["general_options"]["n_cores"]

        results = Parallel(n_jobs=n_cores)(
            delayed(_internal_minimize)(**optim_kwargs)
            for optim_kwargs in optim_arguments
        )

    if dashboard and dashboard_process is not None:
        if not results_arguments[0]["keep_dashboard_alive"]:
            dashboard_process.terminate()

    results = _process_optimization_results(results, results_arguments)

    return results

def _internal_minimize(
    internal_criterion,
    internal_params,
    bounds,
    origin,
    algo_name,
    algo_options,
    internal_gradient,
    database,
    general_options,
):
    """Run one optimization of the transformed optimization problem.
    The transformed optimization problem is converted from the original problem
    which consists of the user supplied criterion, params DataFrame, criterion_kwargs,
    constraints and gradient (if supplied).
    In addition, the transformed optimization problem provides sophisticated logging
    tools if activated by the user.
    The transformed problem can be solved by almost any optimizer package:
        1. The only constraints are bounds on the parameters.
        2. The internal_criterion function takes an one dimensional np.array as input.
        3. The internal criterion function returns a scalar value
            (except for the case of the tao_pounders algorithm).
    Note that because of the reparametrizations done by estimagic to implement
    constraints on behalf of the user the internal params cannot be interpreted without
    reparametrizing it to the full params DataFrame.
    Args:
        internal_criterion (func): The transformed criterion function.
            It takes the internal_params numpy array as only argument, automatically
            enforcing constraints specified by the user. It calls the original
            criterion function after the necessary reparametrizations.
            If logging is activated it protocols every call automatically to the
            specified database.
        internal_params (np.array): One-dimenisonal array with the values of
            the free parameters.
        bounds (tuple): tuple of the length of internal_params. Every entry contains
            the lower and upper bound of the respective internal parameter.
        origin (str): Name of the package to which the algorithm belongs.
        algo_name (str): Name of the algorithm.
        algo_options (dict): Algorithm specific configurations.
        internal_gradient (func): The internal gradient
        database (sqlalchemy.MetaData or False). The engine that connects to the
            database can be accessed via ``database.bind``. This is only used to record
            the start and end of the optimization
        general_options (dict): Only used to pass the start_criterion_value in case
            the tao pounders algorithm is used.
    Returns:
        results (tuple): Tuple of the harmonized result info dictionary and the params
            DataFrame with the minimizing parameter values of the untransformed problem
            as specified of the user.
    """
    if database:
        update_scalar_field(database, "optimization_status", "running")

    if origin in ["nlopt", "pygmo"]:
        results = minimize_pygmo_np(
            internal_criterion,
            internal_params,
            bounds,
            origin,
            algo_name,
            algo_options,
            internal_gradient,
        )
    elif origin == "scipy":
        results = minimize_scipy_np(
            internal_criterion,
            internal_params,
            bounds=bounds,
            algo_name=algo_name,
            algo_options=algo_options,
            gradient=internal_gradient,
        )
    elif origin == "tao":
        crit_val = general_options["_start_criterion_value"]
        len_criterion_value = 1 if np.isscalar(crit_val) else len(crit_val)
        results = minimize_pounders_np(
            internal_criterion,
            internal_params,
            bounds,
            n_errors=len_criterion_value,
            **algo_options,
        )
    else:
        raise NotImplementedError("Invalid algorithm requested.")

    if database:
        update_scalar_field(database, "optimization_status", results["status"])

    return results

def _process_optimization_results(results, results_arguments):
    """Expand the solutions back to the original problems.
    Args:
        results (list):
            list of dictionaries with the harmonized results objects.
        results_arguments (list):
            each element is a dictionary supplying the start params DataFrame
            and the constraints to the original problem.
            The keys are "params", "constraints" and "keep_dashboard_alive".
    Returns:
        results (tuple): Tuple of the harmonized result info dictionary and the params
            DataFrame with the minimizing parameter values of the untransformed problem
            as specified of the user.
    """
    new_results = []
    for res, args in zip(results, results_arguments):
        res["x"] = list(res["x"])
        start_params = args["params"]
        params = reparametrize_from_internal(
            internal=np.array(res["x"]),
            fixed_values=start_params["_internal_fixed_value"].to_numpy(),
            pre_replacements=start_params["_pre_replacements"].to_numpy(dtype="int"),
            processed_constraints=args["constraints"],
            post_replacements=start_params["_post_replacements"].to_numpy(dtype="int"),
            processed_params=start_params,
        )
        new_results.append((res, params))

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


import os
import sys

import matplotlib.pyplot as plt
import numpy as np

from .csitools import get_CSI
from .filters import bandpass, hampel, running_mean
from .matlab import db
from .reader import get_reader

DEFAULT_PATH = "./data/intel/misc/log.all_csi.6.7.6.dat"
# DEFAULT_PATH = "./data/pi/walk_1597159475.pcap"

class BatchGraph:

    def __init__(self, path=DEFAULT_PATH):
        self.reader = get_reader(path)

    def prepostfilter(self):

        reader = self.reader

        csi_trace = reader.csi_trace
        finalEntry, no_frames, _ = get_CSI(csi_trace)

        finalEntry = finalEntry[15]

        hampelData = hampel(finalEntry, 10)
        smoothedData = running_mean(hampelData.copy(), 10)

        y = finalEntry
        y2 = hampelData
        y3 = smoothedData

        x = list([x["timestamp"] for x in csi_trace])

        if sum(x) == 0:
            x = np.arange(0, no_frames, 1)

        plt.plot(x, y, label="Raw")
        plt.plot(x, y2, label="Hampel")
        plt.plot(x, y3, "r", label="Hampel + Running Mean")

        plt.xlabel("Time (s)")
        plt.ylabel("Amplitude (dBm)")
        plt.legend(loc="upper right")

        plt.show()

    def plotAllSubcarriers(self):

        reader = self.reader

        scaled_csi = reader.csi_trace
        finalEntry, no_frames, _ = get_CSI(scaled_csi)

        for x in finalEntry:
            plt.plot(np.arange(no_frames)/20, x)

        plt.xlabel("Time (s)")
        plt.ylabel("Amplitude (dBm)")
        plt.legend(loc="upper right")

        plt.show()

    def heatmap(self):
        reader = self.reader

        csi_trace = reader.csi_trace
        finalEntry, no_frames, no_subcarriers = get_CSI(csi_trace, metric="amplitude")

        x_label = "Time (s)"
        x = list([x["timestamp"] for x in csi_trace])

        if sum(x) == 0:
            #Some files have invalid timestamp_low values which means we can't plot based on timestamps.
            #Instead we'll just plot by frame count.

            xlim = no_frames

            x_label = "Frame No."
        else:
            xlim = max(x)

        limits = [0, xlim, 1, no_subcarriers]
        # limits = [0, xlim, -64, 63]

        #TODO Add options for filtering.
        # for x in range(no_subcarriers):
        #     hampelData = hampel(finalEntry[x], 5, 3)
        #     runningMeanData = running_mean(hampelData, 20)
        #     # smoothedData = dynamic_detrend(runningMeanData, 5, 3, 1.2, 10)
        #     # doubleHampel = hampel(smoothedData, 10, 3)

        #     finalEntry[x] = bandpass(9, 1, 2, Fs, runningMeanData)
        #     # for i in range(0, 140):
        #     #     finalEntry[x][i] = 0

        _, ax = plt.subplots()
        im = ax.imshow(finalEntry, cmap="jet", extent=limits, aspect="auto")

        cbar = ax.figure.colorbar(im, ax=ax)
        cbar.ax.set_ylabel("Amplitude (dBm)")

        plt.xlabel(x_label)
        plt.ylabel("Subcarrier Index")

        plt.title(reader.filename.split("/")[-1])

        plt.show()

if __name__ == "__main__":
    bg = BatchGraph()
    bg.heatmap()


from django.conf.urls import url
from .views import PostListAPIView,PostCreateAPIView,DetailAPIView,PostUpdateAPIView, PostDeleteAPIView

urlpatterns = [
    url(r'^$',PostListAPIView.as_view(),name='list' ),
    url(r'^create/',PostCreateAPIView.as_view(),name="create"),
    url(r'^(?P<pk>\d+)/$',DetailAPIView.as_view() , name='detail'),
    url(r'^(?P<pk>\d+)/edit/$',PostUpdateAPIView.as_view() , name='update'),
    url(r'^(?P<pk>\d+)/delete/$',PostDeleteAPIView.as_view() , name='delete'),
    ]


import csv, time
import pandas as pd

def papoulate_data():
    # csv_reader_ticker=[]
    with open('/home/fintech/Ahsan/NEW_stocks_BETA_industries_values_with_dereived_industries.csv', 'r') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        # print(csv_reader)
        # for rows in csv_reader:
        #     print(rows["Ticker"])

    # csv_file.close()
        with open('/home/fintech/Ahsan/new_1_stocks_industries_values_with_dereived_industries.csv', 'w') as new_file:
            fieldnames = ['Ticker','Industry','Derived Industry','Environment score','Social score','Governance score','Percentile','Controversy','Adult entertainment - negative','Firearms','Animal welfare','Nuclear power','Alcohol','Tobacco','Gambling','Palm oil','Controversial weapons','Fur leather','Gmo','Catholic','Coal','Pesticides','Military Contract','Vegan','Climate change','Alignment with UN SDGs','Labor rights','Diversity','Civil liberties','Water','Wind power - positive','Clean energy','Carbon emissions','Child labor','Stem cell research','Interest bearing instruments','Minority owned','Millennial owned','Gender equity','LGBTQ','Immigration','Family values','Maternity leave (rights)','Abortion','Contraceptives','Beta', "Total Men","Total Women","Women Percentage"]

            csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames)

            csv_writer.writeheader()
            # for rows in csv_reader:
            #     print("------------------------------------------")
            #     print(rows)
            for rows in csv_reader:
                try:
                    # print("------------------------------------------")
                    # time.sleep(7)
                    ticker = rows["Ticker"]
                    # print(f'Downloading Estimates for {ticker}')
                    link='https://finance.yahoo.com/quote/{}/profile?p={}'.format(ticker, ticker)
                    df_est = pd.read_html(str(link))
                    res = df_est[0]
                    # print(res['Name'])
                    man = 0
                    women = 0
                    for name in res['Name']:
                        if "Mr." in name:
                            man += 1
                        elif "Ms." in name:
                            women += 1
                        else:
                            man += 1

                    if str(rows["Beta"]) == "NaN":
                        rows["Beta"] = 0

                    if str(rows["Controversy"]) == "FALSE":
                        rows["Controversy"] = 0

                    rows["Total Men"] = man
                    rows["Total Women"] = women
                    women_percentage = (women)/(man + women)
                    women_percentage = round(float(women_percentage*100),2)
                    rows["Women Percentage"] = women_percentage
                    csv_writer.writerow(rows)
                except:
                    if str(rows["Beta"]) == "NaN":
                        rows["Beta"] = 0
                    rows["Total Men"] = 0
                    rows["Total Women"] = 0
                    rows["Women Percentage"] = 0.0
                    # print(rows["Beta"])
                    csv_writer.writerow(rows)
                    pass

if __name__ == '__main__':
    papoulate_data()


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

def get_resolution(capture):
    width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

    return width, height

def camera_calibration(image_np, width, height):
    # Calibration dictionary (ret=ret, mtx=mtx, dist=dist, rvecs=rvecs, tvecs=tvecs)
    calibration_data = np.load('calibration.npz')

    # Camera parameters ([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
    mtx = calibration_data['mtx']
    # mtx = np.array([[477.59130484, 0., 332.65514887],
    #               [0., 480.76513178, 239.42255985],
    #               [0., 0., 1.]])

    # Distortion coefficients = ([k1, k2, p1, p2, k3)
    # dist = calibration_data['dist']
    dist = np.array([-0.30237751, 0.06247485, 0, 0, 0])

    objpoints = calibration_data['objpoints']
    imgpoints = calibration_data['imgpoints']
    rvecs = calibration_data['rvecs']
    tvecs = calibration_data['tvecs']

    new_camera_mtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (width, height), 0)

    # 1. Using cv2.undistort()
    # dst = cv2.undistort(image_np, mtx, dist, None, new_camera_mtx)
    # x, y, w, h = roi
    # dst = dst[y:y+h, x:x+w]

    # 2. Using remapping
    map_x, map_y = cv2.initUndistortRectifyMap(mtx, dist, None, new_camera_mtx, (width, height), 5)
    dst = cv2.remap(image_np, map_x, map_y, cv2.INTER_LINEAR)
    x, y, w, h = roi
    dst = dst[y:y + h, x:x + w]

    '''
    mean_error = 0
    for i in range(len(objpoints)):
        imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
        error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2) / len(imgpoints2)
        mean_error += error
    print("total error: {}".format(mean_error / len(objpoints)))
    '''

    return dst

def draw_lines(image_np, image_np2, lines, pts1, pts2):
    ''' image_np - image on which we draw the epilines for the points in image_np2
        lines - corresponding epilines '''
    r, c = image_np.shape[:2]
    # image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2BGR)
    # image_np2 = cv2.cvtColor(image_np2, cv2.COLOR_GRAY2BGR)
    for r,pt1,pt2 in zip(lines, pts1, pts2):
        color = tuple(np.random.randint(0, 255, 3).tolist())
        x0,y0 = map(int, [0, -r[2]/r[1] ])
        x1,y1 = map(int, [c, -(r[2]+r[0]*c)/r[1] ])
        image_np = cv2.line(image_np, (x0,y0), (x1,y1), color, 1)
        image_np = cv2.circle(image_np, tuple(pt1), 5, color, -1)
        image_np2 = cv2.circle(image_np2, tuple(pt2), 5, color, -1)

    return image_np, image_np2

def epipolar_geometry(image_np, image_np2):
    # sift = cv2.SIFT()
    # If Error=="AttributeError: module 'cv2.cv2' has no attribute 'SIFT')"
    sift = cv2.xfeatures2d.SIFT_create()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(image_np, None)
    kp2, des2 = sift.detectAndCompute(image_np2, None)

    # FLANN parameters
    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)

    flann = cv2.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)

    good = []
    pts1 = []
    pts2 = []

    # ratio test as per Lowe's paper
    for i, (m, n) in enumerate(matches):
        if m.distance < 0.8 * n.distance:
            good.append(m)
            pts2.append(kp2[m.trainIdx].pt)
            pts1.append(kp1[m.queryIdx].pt)

    pts1 = np.int32(pts1)
    pts2 = np.int32(pts2)
    F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_LMEDS)

    # We select only inlier points
    pts1 = pts1[mask.ravel() == 1]
    pts2 = pts2[mask.ravel() == 1]

    # Find epilines corresponding to points in right image (second image) and
    # drawing its lines on left image
    lines1 = cv2.computeCorrespondEpilines(pts2.reshape(-1, 1, 2), 2, F)
    lines1 = lines1.reshape(-1, 3)
    img5, img6 = draw_lines(image_np, image_np2, lines1, pts1, pts2)

    # Find epilines corresponding to points in left image (first image) and
    # drawing its lines on right image
    lines2 = cv2.computeCorrespondEpilines(pts1.reshape(-1, 1, 2), 1, F)
    lines2 = lines2.reshape(-1, 3)
    img3, img4 = draw_lines(image_np2, image_np, lines2, pts2, pts1)

    return img5, img3

cap = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)

width, height = get_resolution(cap)
width2, height2 = get_resolution(cap2)

while True:
    ret, image_np = cap.read()
    ret2, image_np2 = cap2.read()

    image_np = camera_calibration(image_np=image_np,
                                  width=width,
                                  height=height)

    image_np2 = camera_calibration(image_np=image_np2,
                                   width=width2,
                                   height=height2)

    # image_np, image_np2 = epipolar_geometry(image_np=image_np,
    #                                         image_np2=image_np2)

    image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
    image_np2 = cv2.cvtColor(image_np2, cv2.COLOR_BGR2GRAY)

    stereoMatcher = cv2.StereoSGBM_create()
    stereoMatcher.setMinDisparity(4)
    stereoMatcher.setNumDisparities(128)
    stereoMatcher.setBlockSize(21)
    stereoMatcher.setSpeckleRange(16)
    stereoMatcher.setSpeckleWindowSize(45)
    disparity = stereoMatcher.compute(image_np, image_np2)

    # compute disparity
    disparity = stereoMatcher.compute(image_np, image_np2).astype(np.float32) / 16.0
    disparity = (disparity - 4) / 128
    # disparity = cv2.threshold(disparity, 0.6, 1.0, cv2.THRESH_BINARY)[1]

    cv2.imshow('Calibration', disparity)

    # final_image_np = cv2.hconcat([image_np2, image_np])
    # cv2.imshow('Epipolar Geometry', cv2.resize(final_image_np, (1280, 480)))

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()


p = {}
names = {}

for line in open('input13.txt').readlines():
	line = line.split()
	a = line[0]
	y = (-1 if line[2] == 'lose' else 1) * int(line[3])
	b = line[10][:-1]
	if a not in names:
		names[a] = len(names)
	a = names[a]
	if b not in names:
		names[b] = len(names)
	b = names[b]
	p[(a, b)] = y

# add myself
me = len(names)
names['myself'] = me
for i in range(len(names) - 1):
	p[(i, me)] = 0
	p[(me, i)] = 0

n = len(names)
v = [False] * n
f = [-1] * n

mx = 0

def go(cur, dep, tot):
	global mx
	f[dep] = cur
	if dep != 0:
		tot += p[(f[dep-1], cur)]
		tot += p[(cur, f[dep-1])]
	if dep == n-1:
		tot += p[(f[0], cur)]
		tot += p[(cur, f[0])]
		print(f, tot)
		mx = max(mx, tot)
		return
	for nxt in range(n):
		if not v[nxt]:
			v[nxt] = True
			go(nxt, dep+1, tot)
			v[nxt] = False

for s in range(n):
	v[s] = True
	go(s, 0, 0)
	v[s] = False

print(mx)


import os, sys, argparse
sys.path.insert(0, '/home/grigoryanlab/home/fzheng/modules_py')
import General, PDB, Analyze
from mymodules import designscore

par = argparse.ArgumentParser()
par.add_argument('--p', required = True, help = 'a whole pdb file')
par.add_argument('--n', default = 50, type = int, help = 'topn sequence')
par.add_argument('--h', required = True, help = 'header of .seq file')
par.add_argument('--uniq', action = 'store_true', help = 'if make uniq of the .seq files')
par.add_argument('--homo', nargs = '+', help = 'if not none, here it contains a list of hits to be excluded')
par.add_argument('--natok', action = 'store_true', help =
                 'if true, dont have to remove hits whose sequences are identical to query \
                 (if query is a designed protein, can set it to be true)')
par.add_argument('--o', required = True, help = 'output file')
par.add_argument('--raw', action = 'store_true', help = 'if it is true, output raw score')
par.add_argument('--excluded', nargs = '*', default = [], help = 'a list, which include residues whose results not added to the final score')

args = par.parse_args()
pdbf, head, outf, topNseq = args.p, args.h, args.o, args.n
nhead = 'uniq_' + args.h

base = General.getBase( General.removePath(pdbf) )
conres = PDB.ConRes(pdbf)

# hash to store design score
dsc = {}
nn = {}

# dictionary of all residues need to be declared first
for res in conres:
    cid, resnum = res.getChid(), res.getResnum()
    if cid + ',' + str(resnum) in args.excluded:
        continue
    if not args.raw:
        dsc[ cid + ',' + str(resnum) ] = 0
        nn[ cid + ',' + str(resnum) ] = 0 # number of neighbors

out = open(outf, 'w')

for res in conres:
    cid, resnum, resname = res.getChid(), res.getResnum(), res.getResname()
    if cid + ',' + str(resnum) in args.excluded:
        continue
    if cid == ' ':
        fragpdb = base + '_-' + str(resnum) + '.pdb'
    else:
        fragpdb = base + '_' + cid + str(resnum) + '.pdb'
    seqf = head + '_' + General.changeExt(fragpdb, 'seq')

    if not os.path.isfile(seqf): # sometimes such query doesn't exist, then skip
        continue

    if args.uniq:
        if not args.natok:
            # parse native sequence of fragment
            natres = PDB.ConRes(fragpdb)
            natseq = []
            for r in natres:
                natseq.append(r.getResname())
            nseq = ' '.join(natseq)
            # take non-native unique sequences
            ok = Analyze.trimByUniqSeq(seqf, head, nhead, homo = args.homo, nativeSeq = nseq)
        else:
            ok = Analyze.trimByUniqSeq(seqf, head, nhead, homo = args.homo)
        if ok == -1:
            raise General.myerror('error in ' + seqf)
        seqf = seqf.replace(head, nhead)

    # calculate wild type frequency in top uniq seqs
    a_line = open(seqf).readline()
    if not a_line == '':
        pos = PDB.findPositionInPDB(fragpdb, resnum, cid)
        col = Analyze.readColumn(seqf, pos + Analyze.firstNonumCol(a_line.split()) -1, top = topNseq)
        if len(col) == 0:
            freq = 0
        else:
            count = Analyze.readAACount([resname], [col])
            freq = float(count) / len(col)
    else:
        freq = 0
    if args.raw:
        out.write(cid +','+str(resnum) + ':' +str('%.3f'%freq) + ' ')

    # add this score to all residue which has this residue as neighbor
    neighbors = designscore.neighborList(pdbf, cid + ',' + str(resnum), '.')

    for nb in neighbors:
        if nb in args.excluded:
            continue
        if args.raw:
            out.write(nb + ' ')
        else:
            dsc[nb] += freq
            nn[nb] += 1
    if args.raw:
        out.write('\n')

if not args.raw:
    for res in conres:
        k = res.getChid()+','+str(res.getResnum())
        if k in args.excluded:
            continue
        if nn[k] != 0:
            dsc[k] /= float(nn[k])
            out.write( k + ' : ' + str('%.3f' %dsc[k]) + '\n' )


#!/usr/bin/env python3
""" Python program to implement a single neuron neural network """
import numpy as np

class DeepNeuralNetwork():
    """
        class DeepNeuralNetwork that defines a deep neural network
        performing binary classification.
        nx : is the number of input features
        layers : is a list representing the number of nodes
        in each layer of the network
        L: The number of layers in the neural network.
        cache: A dictionary to hold all intermediary values of the network.
        weights: A dictionary to hold all weights and biased of the network.

    """

    def __init__(self, nx, layers):

        if not isinstance(nx, int):
            raise TypeError('nx must be an integer')

        if nx < 1:
            raise ValueError('nx must be a positive integer')

        if not isinstance(layers, list) or len(layers) == 0:
            raise TypeError("layers must be a list of positive integers")

        self.__L = len(layers)
        self.__cache = {}
        self.__weights = {}

        for lay in range(self.L):
            if not isinstance(layers[lay], int) or layers[lay] <= 0:
                raise TypeError("layers must be a list of positive integers")
            if lay == 0:
                He = (np.random.randn(layers[lay], nx)
                      * np.sqrt(2 / nx))
                self.__weights["W{}".format(lay + 1)] = He
            else:
                He = (np.random.randn(layers[lay], layers[lay - 1])
                      * np.sqrt(2 / layers[lay - 1]))
                self.__weights["W{}".format(lay + 1)] = He
            self.__weights["b{}".format(lay + 1)] = np.zeros((layers[lay], 1))

    @property
    def L(self):
        return self.__L

    @property
    def cache(self):
        return self.__cache

    @property
    def weights(self):
        return self.__weights

    def forward_prop(self, X):
        """
            Add the public method def forward_prop with
            He et al. Matrix Mult. of Weights
            and X values (input data).
        """
        self.cache.update({'A0': X})
        for i in range(self.L):
            A = self.cache.get('A' + str(i))
            biases = self.weights.get('b' + str(i + 1))
            weights = self.weights.get('W' + str(i + 1))
            Z = np.matmul(weights, A) + biases
            self.cache.update({'A' + str(i + 1): 1 / (1 + np.exp(-Z))})

        return self.cache.get('A' + str(i + 1)), self.cache

    def cost(self, Y, A):
        """
            Method to calculate the cost of the model
            using logistic regression
            Y : contains the correct labels for the input data.
            A : contains the activated output of the neuron for each.
        """

        cost = - (1 / Y.shape[1]) * np.sum(np.multiply(Y, np.log(A)) +
                                           np.multiply(1 - Y,
                                                       np.log(1.0000001 - A)))
        return cost

    def evaluate(self, X, Y):
        """
            Method to Evaluates the neuron’s predictions.
            X : is a numpy.ndarray with shape (nx, m)
                that contains the input data.
            Y : is a numpy.ndarray with shape (1, m)
                that contains the correct labels for the input data.
        """

        A, _ = self.forward_prop(X)
        NeuronEvaluation = np.where(
            A <= 0.5, 0, 1), self.cost(
            Y, A)

        return NeuronEvaluation

    def gradient_descent(self, Y, cache, alpha=0.05):
        """
            Y is a numpy.ndarray with shape (1, m) that contains
            the correct labels for the input data
            cache is a dictionary containing all the intermediary
            values of the network
            alpha is the learning rate
            Updates the private attribute __weights
        """

        NumberOfLayers = range(self.L, 0, -1)
        NumberOfElements = Y.shape[1]
        DifferenceStorage = 0
        weights = self.weights.copy()

        for i in NumberOfLayers:

            A = cache.get('A' + str(i))
            A_prev = cache.get('A' + str(i - 1))
            weights_i = weights.get('W' + str(i))
            weights_n = weights.get('W' + str(i + 1))
            biases = weights.get('b' + str(i))

            if i == self.L:
                Difference = A - Y
            else:
                Difference = np.matmul(
                    weights_n.T, DifferenceStorage) * (A * (1 - A))

            GradientW = np.matmul(Difference, A_prev.T) / NumberOfElements
            biasAverage = np.sum(
                Difference, axis=1, keepdims=True) / NumberOfElements
            self.__weights['W' + str(i)] = weights_i - (GradientW * alpha)
            self.__weights['b' + str(i)] = biases - (biasAverage * alpha)
            DifferenceStorage = Difference


# coding: utf-8

# In[ ]:

import numpy as np

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

def sigmoid_prime(x):
    return sigmoid(x) * (1 - sigmoid(x))

def KL_divergence(x, y):
    return x * np.log(x / y) + (1 - x)*np.log((1 - x) / (1 - y))

##------------------------------------------------------------##

def initialize(hidden_size, visible_size):
    """
    initialization of the weights and b
    """
    # choose weights uniformly randomly within the interval [-r, r]
    r = np.sqrt(6) / np.sqrt(hidden_size + visible_size + 1)

    W1 = np.random.random((hidden_size, visible_size))*2*r - r
    W2 = np.random.random((visible_size, hidden_size))*2*r - r

    b1 = np.zeros(hidden_size, dtype=np.float64)
    b2 = np.zeros(visible_size, dtype=np.float64)

    # Wrap weights and b to a vector
    theta = np.concatenate((W1.reshape(hidden_size*visible_size),
                            W2.reshape(visible_size*hidden_size),
                            b1.reshape(hidden_size),
                            b2.reshape(visible_size)))

    return theta

# variable list
# visible_size: number of inputs (could be 64)
# hidden_size: number of hidden units (could be 25)
# lambda_: decay parameter
# sparsity_param: average desired activation
# beta: weight of the sparsity penalty term

def sparse_autoencoder_cost(theta, visible_size, hidden_size,
                            lambda_, sparsity_param, beta, data):
    """
    Returns (cost, gradient) tuple
    """

    # first convert theta, which is a vector, to the (W1, W2, b1, b2) format
    W1 = theta[0 : hidden_size*visible_size].reshape(hidden_size, visible_size)
    W2 = theta[hidden_size*visible_size : 2*hidden_size*visible_size].reshape(visible_size, hidden_size)
    b1 = theta[2*hidden_size*visible_size : 2*hidden_size*visible_size + hidden_size]
    b2 = theta[2*hidden_size*visible_size + hidden_size : ]

    # number of training examples
    m = data.shape[1] # each column is an image(or part of an image)

    # Forward Propagation
    z2 = W1.dot(data) + np.tile(b1, (m, 1)).transpose()
    a2 = sigmoid(z2)
    z3 = W2.dot(a2) + np.tile(b2, (m, 1)).transpose()
    h = sigmoid(z3)

    # Sparsity
    rho_hat = np.sum(a2, axis=1) / m  # sum of each row
    rho = np.tile(sparsity_param, hidden_size)

    # Cost function
    cost = np.sum((h - data)**2) / (2*m) + (lambda_ / 2) * (np.sum(W1**2) + np.sum(W2**2)) + beta * np.sum(KL_divergence(rho, rho_hat))

    # Back Propagation
    sparsity_delta = np.tile(-rho/rho_hat + (1-rho)/(1-rho_hat), (m, 1)).transpose()
    delta3 = -(data-h) * sigmoid_prime(z3)
    delta2 = (W2.transpose().dot(delta3) + beta*sparsity_delta) * sigmoid_prime(z2)

    # Gradients
    W1grad = delta2.dot(data.transpose()) / m + lambda_*W1
    W2grad = delta3.dot(a2.transpose()) / m + lambda_*W2
    b1grad = np.sum(delta2, axis=1) / m
    b2grad = np.sum(delta3, axis=1) / m

    # Wrap the gradients to a vector
    grad = np.concatenate((W1grad.reshape(hidden_size*visible_size),
                         W2grad.reshape(visible_size*hidden_size),
                         b1grad.reshape(hidden_size),
                         b2grad.reshape(visible_size)))

    return cost, grad

def sparse_autoencoder(theta, hidden_size, visible_size, data):
    """
    returns the autoencoder of the input
    """

    # Unroll the trained parameters
    W1 = theta[0 : hidden_size*visible_size].reshape(hidden_size, visible_size)
    b1 = theta[2*hidden_size*visible_size : 2*hidden_size*visible_size + hidden_size]

    m = data.shape[1]

    # Forward propagation
    z2 = W1.dot(data) + np.tile(b1, (m, 1)).transpose()
    a2 = sigmoid(z2)

    return a2

def sparse_autoencoder_linear_cost(theta, visible_size, hidden_size,
                                   lambda_, sparsity_param, beta, data):
    # The input theta is a vector (because minFunc expects the parameters to be a vector).
    # We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this
    # follows the notation convention of the lecture notes.

    W1 = theta[0:hidden_size * visible_size].reshape(hidden_size, visible_size)
    W2 = theta[hidden_size * visible_size:2 * hidden_size * visible_size].reshape(visible_size, hidden_size)
    b1 = theta[2 * hidden_size * visible_size:2 * hidden_size * visible_size + hidden_size]
    b2 = theta[2 * hidden_size * visible_size + hidden_size:]

    # Number of training examples
    m = data.shape[1]

    # Forward propagation
    z2 = W1.dot(data) + np.tile(b1, (m, 1)).transpose()
    a2 = sigmoid(z2)
    z3 = W2.dot(a2) + np.tile(b2, (m, 1)).transpose()
    h = z3

    # Sparsity
    rho_hat = np.sum(a2, axis=1) / m
    rho = np.tile(sparsity_param, hidden_size)

    # Cost function
    cost = np.sum((h - data) ** 2) / (2 * m) + \
           (lambda_ / 2) * (np.sum(W1 ** 2) + np.sum(W2 ** 2)) + \
           beta * np.sum(KL_divergence(rho, rho_hat))

    # Backprop
    sparsity_delta = np.tile(- rho / rho_hat + (1 - rho) / (1 - rho_hat), (m, 1)).transpose()

    delta3 = -(data - h)
    delta2 = (W2.transpose().dot(delta3) + beta * sparsity_delta) * sigmoid_prime(z2)
    W1grad = delta2.dot(data.transpose()) / m + lambda_ * W1
    W2grad = delta3.dot(a2.transpose()) / m + lambda_ * W2
    b1grad = np.sum(delta2, axis=1) / m
    b2grad = np.sum(delta3, axis=1) / m

    # After computing the cost and gradient, we will convert the gradients back
    # to a vector format (suitable for minFunc).  Specifically, we will unroll
    # your gradient matrices into a vector.
    grad = np.concatenate((W1grad.reshape(hidden_size * visible_size),
                           W2grad.reshape(hidden_size * visible_size),
                           b1grad.reshape(hidden_size),
                           b2grad.reshape(visible_size)))

    return cost, grad


#!/usr/bin/python3
"""
Module has one class:
Student
"""

class Student:
    """Information that defines a student"""
    def __init__(self, first_name, last_name, age):
        """Initializing variables"""
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def to_json(self, attrs=None):
        """
        retrieves a dictionary representation of a student instance
        """
        if attrs is None:
            return self.__dict__
        else:
            new_dict = {}
            if isinstance(attrs, list) is True:
                for elem in attrs:
                    for key, val in self.__dict__.items():
                        if elem == key:
                            new_dict[elem] = val
            return new_dict


"""
        __________________________________

        Game's classes : Level, Personnage
        __________________________________
"""

"""
    Class for creating the levels
"""

from constants import *

class Level:

    def __init__(self):
        self.file = LEVEL_MAP
        self.structure = []

    def generate(self):
        """ method used to generate level depending on the file."""

        if self.structure == []:
            with open(self.file, "r") as file:

                level_structure = []

                for line in file:
                    lines = []
                    for sprite in line:
                        if sprite != '\n':
                            lines.append(sprite)
                    level_structure.append(lines)
                self.structure = level_structure

    def display(self, window):
        """Method for displaying the level depending on
        the structure of list returned by generate() method"""

        self.generate()
        wall = pygame.image.load(WALL_IMG).convert()
        way = pygame.image.load(WAY_IMG).convert()
        point = pygame.transform.scale(pygame.image.load(POINT_IMG), (5, 5))

        # browse throught the level lines
        line_num = 0
        for line in self.structure:
            square_num = 0
            for sprite in line:
                x = square_num * SPRITE_SIZE
                y = line_num * SPRITE_SIZE
                if sprite == 'w':  # w = wall
                    window.blit(wall, (x, y))
                elif sprite == 'p':
                    window.blit(way, (x, y))
                    window.blit(point, (x + 12, y + 12))
                else:
                    window.blit(way, (x, y))
                square_num += 1
            line_num += 1

class PacMan:

    def __init__(self):
        self.right = pygame.image.load(PACMAN_RIGHT).convert_alpha()
        self.left = pygame.image.load(PACMAN_LEFT).convert_alpha()
        self.up = pygame.image.load(PACMAN_UP).convert_alpha()
        self.down = pygame.image.load(PACMAN_DOWN).convert_alpha()
        #  Pacman position
        self.case_x = 9
        self.case_y = 8
        self.x = self.case_x * SPRITE_SIZE
        self.y = self.case_y * SPRITE_SIZE

        # default direction
        self.direction = self.right
        # selected level
        level_class = Level()
        level_class.generate()
        self.map = level_class.structure
        self.x_change = 0
        self.y_change = 0

    def change_direction(self, direction):

        self.x_change = 0
        self.y_change = 0

        if direction == "right":
            self.x_change = 1
            self.y_change = 0
            self.direction = self.right

        elif direction == "left":
            self.x_change = -1  # moving by one case
            self.y_change = 0  # moving by one case
            self.direction = self.left

        elif direction == "up":
            self.x_change = 0
            self.y_change = -1
            self.direction = self.up

        elif direction == "down":
            self.x_change = 0  # moving by one case
            self.y_change = 1  # moving by one case
            self.direction = self.down

    def move(self):

        if self.case_x < (SPRITE_WIDTH + self.x_change) and self.case_y < (SPRITE_HEIGHT + self.y_change):
            # check if there is not a wall
            if self.map[self.case_y + self.y_change][self.case_x + self.x_change] != 'w':
                self.case_x = self.case_x + self.x_change  # moving by one case
                self.x = self.case_x * SPRITE_SIZE
                self.case_y = self.case_y + self.y_change  # moving by one case
                self.y = self.case_y * SPRITE_SIZE

class Ghosts:  #  inherit from PacMan class

    def __init__(self, x_pos: int, y_pos: int, color: str):

        if color == "red": img = GHOST_RED
        if color == "yellow": img = GHOST_YELLOW
        if color == "green": img = GHOST_GREEN
        self.img = pygame.transform.scale(pygame.image.load(img),(20, 20)).convert_alpha()

        # self.right = pygame.transform.scale(pygame.image.load(img), (20, 20)).convert_alpha()
        # self.left = pygame.transform.scale(pygame.image.load(img), (20, 20)).convert_alpha()
        # self.up = pygame.transform.scale(pygame.image.load(img), (20, 20)).convert_alpha()
        # self.down = pygame.transform.scale(pygame.image.load(img), (20, 20)).convert_alpha()
        #  Pacman position
        # self.direction = self.right
        self.case_x = x_pos
        self.case_y = y_pos
        self.x = self.case_x * SPRITE_SIZE
        self.y = self.case_y * SPRITE_SIZE

        # selected level
        level_class = Level()
        level_class.generate()
        self.map = level_class.structure
        self.x_change = 0
        self.y_change = 0

        self.path = []

    def find_path(self, end_x: int, end_y: int):
        current_position = (0,)  # initialize tuple (y,x)
        frontier_list, visited_list = [], []
        backtracking_list = {}
        shortest_path = []
        dict_index = 0
        end = (end_y, end_x)

        frontier_list.append((self.case_y, self.case_x))

        while frontier_list != []:
            current_position = frontier_list[0]
            self.case_y, self.case_x = frontier_list[0][0], frontier_list[0][1]

            # check if there is wall or a visited cell at each side
            left_cell = (current_position[0], current_position[1] - 1)
            right_cell = (current_position[0], current_position[1] + 1)
            up_cell = (current_position[0] - 1, current_position[1])
            down_cell = (current_position[0] + 1, current_position[1])

            # left side
            if self.map[left_cell[0]][left_cell[1]] != 'w' \
                    and left_cell not in visited_list:
                frontier_list.append(left_cell)
                backtracking_list[dict_index] = (left_cell, current_position)  # (current), (previous)
                dict_index += 1

            # right side
            if self.map[right_cell[0]][right_cell[1]] != 'w' \
                    and right_cell not in visited_list:
                frontier_list.append(right_cell)
                backtracking_list[dict_index] = (right_cell, current_position)  # (current), (previous)
                dict_index += 1

            # up side
            if self.map[up_cell[0]][up_cell[1]] != 'w' \
                    and up_cell not in visited_list:
                frontier_list.append(up_cell)
                backtracking_list[dict_index] = (up_cell, current_position)  # (current), (previous)
                dict_index += 1

            # down side
            if self.map[down_cell[0]][down_cell[1]] != 'w' \
                    and down_cell not in visited_list:
                frontier_list.append(down_cell)
                backtracking_list[dict_index] = (down_cell, current_position)  # (current), (previous)
                dict_index += 1

            # place the current cell in the visited list
            visited_list.append(current_position)
            #  de-queue frontier list
            frontier_list.remove(current_position)

        shortest_path.append(end)
        temporary_path = end

        for i in range(len(backtracking_list) - 1, -1, -1):
            if backtracking_list[i][0] == temporary_path:
                temporary_path = backtracking_list[i][1]
                shortest_path.append(temporary_path)

        self.path = shortest_path

    def move_ghost(self):

        if len(self.path) > 1:
            # print(len(self.path))
            move = self.path[-2]  # path is an reversed list [end, n-1, ... start]
            self.y_change = move[0] - self.case_y
            self.x_change = move[1] - self.case_x

            self.case_x = self.case_x + self.x_change  # moving by one case
            self.x = self.case_x * SPRITE_SIZE
            self.case_y = self.case_y + self.y_change  # moving by one case
            self.y = self.case_y * SPRITE_SIZE

    def move_Blinky(self, pacman_case_x: int, pacman_case_y: int):
        self.find_path(pacman_case_x, pacman_case_y)
        self.move_ghost()

    def move_Clyde(self, pacman_case_x: int, pacman_case_y: int, pacman_direction: str):
        x_gap, y_gap = 0, 0
        if pacman_direction == "up":
            y_gap = -1
        elif pacman_direction == "down":
            y_gap = 1
        elif pacman_direction == "left":
            x_gap = -1
        elif pacman_direction == "right":
            x_gap = 1

        move_gap = 3

        for gap in range(move_gap, 0, -1):

            case_plus_gap_y = pacman_case_y + (gap * y_gap)
            case_plus_gap_x = pacman_case_x + (gap * x_gap)

            if 0 < case_plus_gap_y < SPRITE_HEIGHT and 0 < case_plus_gap_x < SPRITE_WIDTH \
                    and self.map[case_plus_gap_y][case_plus_gap_x] != 'w':
                self.find_path(case_plus_gap_x, case_plus_gap_y)
                break

        self.move_ghost()