import sys

def inboard(board):
    for i in range(3):
        for j in range(3):
            if board[i*3+j] == -1:
                print('O',end=' ')
            elif board[i*3+j] == 1:
                print('X',end=' ')
            else:
                print('-',end=' ')
        print()
def premove(board):
    def check(board):
        for i in range(3):
            r = board[i*3 : i*3+3]
            c = board[i :: 3]
            if all(cell == 1 for cell in r):
                return 10
            if all(cell == -1 for cell in r):
                return -10
            if all(cell == 1 for cell in c):
                return 10
            if all(cell == -1 for cell in c):
                return -10
        dcheo1 = board[0] == board[4] == board[8]
        dcheo2 = board[2] == board[4] == board[6]
        if dcheo1 and board[0] != 0:
            return 10 if board[0] == 1 else -10
        if dcheo2 and board[2] != 0:
            return 10 if board[2] == 1 else -10
        if 0 not in board:
            return 0
        return None
    def minimax(board,d,who):
        score = check(board)
        if score is not None:
            return score
        if who:
            bestscore = -sys.maxsize
            for i,cell in enumerate(board):
                if cell == 0:
                    board[i] = 1
                    score = minimax(board,d+1,False)
                    board[i] = 0
                    bestscore = max(score,bestscore)
            return bestscore
        else:
            bestscore = sys.maxsize
            for i,cell in enumerate(board):
                if cell == 0:
                    board[i] = -1
                    score = minimax(board,d+1,True)
                    board[i] = 0
                    bestscore = min(score,bestscore)
            return bestscore
    #Main của hàm đoán nước đi
    bestmove = None
    bestscore = -sys.maxsize
    for i,cell in enumerate(board):
        if cell == 0:
            board[i] = 1
            score = minimax(board,0,False)
            board[i] = 0
            if score > bestscore:
                bestscore = score
                bestmove = i
    return bestmove + 1
#Main cua truong trinh
if __name__ == '__main__':
    board = list(map(int,input().split()))
    #inboard(board)
    ans = premove(board)
    print(ans)


from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import torch
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import os
from os.path import join
from os import listdir
import pandas as pd
from mask_functions import read_dicom, mask2rle, rle2mask
from collections import Counter
import torch.nn as nn
mean=(0.485, 0.456, 0.406)
std=(0.229, 0.224, 0.225)

transform = transforms.Compose([
	transforms.ToPILImage(),
	transforms.Resize((256,256)),
	transforms.ToTensor(),
	transforms.Normalize(mean, std)
	])

label_transform = transforms.Compose([
	transforms.ToPILImage(),
	transforms.Resize((256, 256)),
	transforms.ToTensor()
	])

class siim_dataset(Dataset):

	def __init__(self, img_path, label_csv):

		self.img_path = img_path
		self.label_csv = label_csv
		self.imgs_dir = listdir(join(os.getcwd(), self.img_path))
		###############################################################
		######		 			  [csv format]		  			 ######
		######		ImageId						EncodedPixels	 ######
		###############################################################
		self.label_csv = pd.read_csv(join(os.getcwd(), self.label_csv))
		self.image_id = self.label_csv['ImageId'].values
		self.image_mask = self.label_csv[' EncodedPixels'].values
		self.id2mask = {}
		for x, y in zip(self.image_id, self.image_mask):
			self.id2mask[x]=y

	def __len__(self):
		return len(self.imgs_dir)

	def __getitem__(self, idx):

		dir_name = self.imgs_dir[idx]
		sub_dir_name = [file for file in listdir(join(os.getcwd(), self.img_path, dir_name)) if file != '.DS_Store'][0]
		image_name = [file for file in listdir(join(os.getcwd(), self.img_path, dir_name, sub_dir_name)) if file != '.DS_Store'][0]

		#image_mask = self.label_csv[self.label_csv['ImageId'] == image_name.replace('.dcm', '')][' EncodedPixels'].values[0]
		image_mask = self.id2mask[image_name.replace('.dcm','')]

		image, width, height = read_dicom(join(os.getcwd(), self.img_path, dir_name, sub_dir_name, image_name))

		label = rle2mask(image_mask, width, height).T

		image = transform(image)
		label = label_transform(label)
		label = label.view(1, 256, 256)

		return image, label

if __name__ == '__main__':
	dataset = siim_dataset('dicom-images-train', 'train-rle.csv')
	dataset = DataLoader(dataset, batch_size=1)
	fn = nn.BCELoss()
	loss = 0
	for index, i in enumerate(dataset):
		x, y = i
		out = torch.ones(x.size())
		out /= 2
		c_loss = fn(out, y)
		print(c_loss)
		loss += c_loss.item()
	print(loss/len(dataset))


import os
from dotenv import load_dotenv
#from datetime import date

from app import APP_ENV
# web_app/routes/pokemon_routess.py

from flask import Blueprint, jsonify, request, render_template, redirect, flash

pokemon_routes = Blueprint("pokemon_routes", __name__)

@pokemon_routess.route("/api/pokemons")
@pokemon_routess.route("/api/pokemons.json")
def list_pokemons():
    print("pokemonS...")
    pokemons = [
        {"id": 1, "title": "pokemon 1", "year": 1957},
        {"id": 2, "title": "pokemon 2", "year": 1990},
        {"id": 3, "title": "pokemon 3", "year": 2031},
    ] # some dummy / placeholder data
    return jsonify(pokemons)

#if URL params are required, we will use this kind of approach:
@pokemon_routess.route("/api/pokemons/<int:pokemon_id>")
@pokemon_routess.route("/api/pokemons/<int:pokemon_id>.json")
def get_pokemon(pokemon_id):
    print("pokemon...", pokemon_id)
    pokemon = {"id": pokemon_id, "title": f"Example pokemon", "year": 2000} # some dummy / placeholder data
    return jsonify(pokemon)


"""
Problem
Marut is great warrior. Marut loves his girlfriend Shizuka very much. Being jealous from Marut's love, the Devil kidnaps his girlfriend. Hence, Marut declares a war against the Devil. The devil decides to send his army men one by one to fight with Marut. Marut being a smart person, he has a secret energy booster named "CodeRas". If Marut drinks "CodeRas" one time, his energy increases by V unit. Marut has an infinite amount of "CodeRas".

Each army man does some damage to Marut. If an army man has X amount energy and fights with Marut, energy of both of them will decrease by X unit. A person dies if his energy becomes zero unit.

Now, Marut knows the number of army men that Devil will send and their energy level. As poor Marut is bad in mathematics, he wants to know the minimum number of times he need to drink "CodeRas" in order to kill all army men and keep himself alive at the end of war. Help him !!!

Note: Marut cannot drink "CodeRas" in between the fight. He can only drink it before and after the fight.

Input:
First line contains an integer T, denoting the number of test cases.
Each testcase contains two lines.
First line of each testcase contains three integers N , E and V separated by single space. N denotes the size of army, E denotes the initial energy of Marut and V denotes the amount of energy which is increased after drinking "CodeRas".
Second line of each testcase contains N integers, separated by single space. ith integer of this line denotes the energy of ith army man.

Output:
For each test case, print the answer in a new line.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 105
1 ≤ E ≤ 105
1 ≤ V ≤ 105
1 ≤ Army man's energy ≤ 105
"""

def win_fight(army, energy, fill_energy):
    count = 0
    i = 0
    while i < len(army):
        a = army[i]
        if a < energy:
            energy -= a
            i = i + 1
        else:
            energy += fill_energy
            count += 1
    return count

# Write your code here
t = int(input())

for i in range(t):
    input1 = input().strip().split(" ")
    input1 = [int(inp.strip()) for inp in input1]
    army = input().strip().split(" ")
    army = [int(a) for a in army]
    army.sort()
    ans = win_fight(army, input1[1], input1[2])
    print(ans)


n = int(input())    # buildings
d = int(input())    # maxdistance between two humans
nums = [0]*n
for i in range(n):
    nums[i] = int(input())   # locations of buildings
res = 0
# print(n)
# print(d)
# print(nums)
# double links
nums.sort()
slow = 0
fast = 2
while fast < n:
    if (fast - slow < 2):
        fast+=1
    elif nums[fast] - nums[slow] <= d:
        res+= (fast-slow-1)*(fast-slow)/2
        fast+=1
    elif nums[fast] - nums[slow] > d:
        slow+=1
print(res%99997867)


import os
import sys
import numpy as np
import pandas as pd
from datetime import date
from collections import OrderedDict
from dateutil.relativedelta import *
import matplotlib.pyplot as plt
from IPython.core.pylabtools import figsize
from IPython.core.display import display, HTML
from IPython.display import Latex

sys.path = ["./"]+sys.path

import fingenerators as fingen

# ============================================================================
def convertcompounded(n,m,imc,returnstr=False):
    inc = n * ( (1 + imc/m) ** (m/n) -1 )
    if returnstr:
        inc = f'Effective x{n} rate of {imc} compounded x{m} is {inc}'
    return inc

# ============================================================================
def plot_amort_balance(schedules,scenarios):
    """Plot the remaining  balance of all amortisation scenarios
    """
    figsize(12,8)
    fig, ax = plt.subplots(1, 1)
    for scenario in scenarios.keys():
        schedules[scenario].plot(x='Month', y='End Balance', label=f'{scenario}', ax=ax)
    plt.title("Repayment Timelines");
    plt.ylabel("Balance");

# ============================================================================
def plot_amort_annual_interest(schedules,scenarios, stats):
    """Plot the annual interest of all amortisation scenarios
    """
    ys = {}
    labels = {}
    header = {}
    for scenario in scenarios.keys():
        ys[scenario],labels[scenario],header[scenario] = df_annual_amort_interest(schedules[scenario], stats[scenario])

    dfai = pd.concat([ys[scenario] for scenario in ys.keys()], axis=1)
    dfai.columns = header.keys()

    figsize(12,8)
    fig, ax = plt.subplots(1, 1)
    for header in header.keys():
        dfai.plot(y=header, label=f'{header}', ax=ax)
    plt.legend([labels[scenario] for scenario in labels.keys()], loc=1, prop={'size':10})
    plt.title("Interest Payments");

# ============================================================================
def df_annual_amort_interest(schedule, stats):
    """Create a dataframe with annual amortisation interest totals, and a descriptive label
    """
    annint = schedule.set_index('Month')['Interest'].resample("A").sum().reset_index()
    annint["Year"] = annint["Month"].dt.year
    annint.set_index('Year', inplace=True)
    annint.drop('Month', 1, inplace=True)
    label="{} years at {}% with additional payment of {:.0f}".format(stats['BondYears'], stats['Interest Rate']*100, stats['AddPayment'])
    header = f'{stats["AddPayment"]}'
    return annint, label, header

# ============================================================================
def calc_scenarios(scenarios,cyclesPerAnnum=12,paymentSign=1):
    """Given a scenario dictionary calculate bond schedules and statistics
    """
    schedules = {}
    stats = {}

    for scenario in scenarios.keys():
        if 'reqPayment' not in scenarios[scenario].keys():
            scenarios[scenario]['reqPayment'] = \
                paymentSign * round(np.pmt(scenarios[scenario]['intr'] / cyclesPerAnnum,
                              scenarios[scenario]['years'] * cyclesPerAnnum,
                              scenarios[scenario]['princ']), 2);

        schedules[scenario], stats[scenario] = fingen.amortisation_table(
                        scenarios[scenario]['princ'],
                        scenarios[scenario]['intr'],
                        scenarios[scenario]['years'],
                        scenarios[scenario]['reqPayment'],
                        scenarios[scenario]['addPayment'],
                        cyclesPerAnnum=12
                    );
    return schedules,stats

# ============================================================================
# to evaluate the tax benefits
def bondtaxsavingsanalysis(principal,interest_rate,bondyears,taxrate,rentpmonth,
                           increasepyear,cyclesPerAnnum=12,addpayment=0,addpayrate=0):
    """Calculate a monthly schedule and summary of tax benefit on bond loan
    """

    reqpayment = round(np.pmt(interest_rate/cyclesPerAnnum, bondyears*cyclesPerAnnum, principal), 2)

    # calculate the mortgage
    df, stats = fingen.amortisation_table(
        principal=principal,
        interest_rate=interest_rate,
        bondyears=bondyears,
        reqpayment = reqpayment,
        addpayment=addpayment,
        cyclesPerAnnum=cyclesPerAnnum,
        start_date=date(2000, 1,1),
        addpayrate=addpayrate)

    numcycles = stats['Num Payments']
    rischedule = fingen.annIncreaseTable(value=rentpmonth, increasepyear=increasepyear, numcycles=numcycles)
    # Now merge the bond table with the rent income table
    dfc = df.copy().drop(["ReqPayment","AddPayment","Begin Balance"],axis=1)
    dfc = dfc.merge(rischedule.drop(["Month"],axis=1), on='Period')

    # number of payments
    dfc['NumPay'] = stats['Num Payments']

    # save tax for later
    dfc['TaxRate'] = taxrate

    # tax and net income if **NO** bond present
    dfc['TaxNoInter'] = - taxrate * (dfc['Rent'])
    # can't get tax back
    dfc['TaxNoInter'][dfc['TaxNoInter'] > 0] = 0
    dfc['IncomeNoInter'] = dfc['Rent'] + dfc['TaxNoInter']

    # tax and net income if bond present
    dfc['TaxWithInter'] = - taxrate * (dfc['Rent'] + dfc['Interest'])
    # can't get tax back
    dfc['TaxWithInter'][dfc['TaxWithInter'] > 0] = 0
    dfc['IncomeWithInter'] = dfc['Rent'] + dfc['TaxWithInter']

    # net benefit
    dfc['BondBenefit'] = dfc['IncomeWithInter'] - dfc['IncomeNoInter']

    #Create a summary statistics table
    nistats = pd.Series([stats['Principal'],stats['Interest Rate'],
                         stats['ReqPayment'],stats['AddPayment'],dfc["Period"].count(),
                         rentpmonth,  increasepyear,
                         taxrate,
                         dfc["Rent"].sum(),
                         dfc["TaxNoInter"].sum(),
                         dfc["IncomeNoInter"].sum(),
                         dfc["TaxWithInter"].sum(),
                         dfc["IncomeWithInter"].sum(),
                         dfc["BondBenefit"].sum(),
                         100 * dfc["BondBenefit"].sum()/dfc["Rent"].sum(),
                        ],
                       index=["Bond","Interest Rate",
                              "ReqPayment","AddPayment","Num Payments",
                              "InitRent",  "RentIncrease",
                              "TaxRate",
                              "Total Rent",
                              "TaxNoInter",
                              "IncomeNoInter",
                              "TaxWithInter",
                              "IncomeWithInter",
                              "BondBenefit",
                              "Benefit/Rent %",
                             ])

    if False:
        figsize(12,8)
        fig, ax = plt.subplots(1, 1)
        # for scenario in scenarios.keys():
        #     schedules[scenario].plot(x='Month', y='End Balance', label=f'{scenario}', ax=ax)
        dfc.plot(x='Month',y='Rent', label='Rent', ax=ax)
        dfc.plot(x='Month',y='Interest', label='Interest', ax=ax)
        dfc.plot(x='Month',y='TaxNoInter', label='TaxNoInter', ax=ax)
        dfc.plot(x='Month',y='TaxWithInter', label='TaxWithInter', ax=ax)
        plt.title("Repayment Timelines");
        plt.ylabel("Value");
        plt.xlabel("Time");

    return dfc,nistats

# ============================================================================
def dispdfTable(dfi,doLaTeXdisplay,decimals=2,index=None, drops=[]):
    """Creates LaTeX or HTML display string
    """
#     print(type(dfi))
    extraVal = 1 if index==True else 0;
    if isinstance(dfi, pd.Series):
        if 'ID' in dfi.index:
            if len(dfi['ID']) == 0:
                drops = drops + ['ID']
        dfi = dfi.drop(drops)

        df = dfi.to_frame()
        tabstr = f'|l*{{{df.shape[1]}}}{{r}}|'
        index = True if index==None else index
        header = False

        df = df.round(decimals)

    else:
        df = dfi
        tabstr = f'*{{{df.shape[1]+extraVal}}}{{|r}}|'
        index = False if index==None else index
        if 'ID' in df.columns:
            if len(df.iloc[0]['ID']) == 0:
                drops = drops + ['ID']
        df = df.drop(drops,axis=1).round(decimals)
        header = True

    if doLaTeXdisplay:
        display(Latex(df.to_latex(index=index,column_format=tabstr,header=header)))
    else:
        display(HTML(df.to_html(index=index,header=header)))


from auth.login import Login

# Criar uma instância da classe Login
login = Login()

# Verificar se a sessão já existe em um arquivo
session_file = 'session.pkl'
try:
    session = login.load_session(session_file)
    print("Sessão carregada do arquivo.")
except FileNotFoundError:
    # Obter uma nova sessão
    status_code, session_id, session = login.get_session()
    login.save_session(session, session_file)
    print("Nova sessão criada e salva no arquivo.")

# Continuar usando a sessão para outras requisições...


def coord(v):
    y = v // 8
    x = v - 8 * y
    return x,y

def idx(c):
    return c[0] + 8 * c[1]

def next(v,dst):
    c = coord(v)
    c_d = coord(dst)

    if c[0] > c_d[0] and abs(c[0] - c_d[0]) > 1:
        c_move = [-1,-2]
    elif c[0] < c_d[0] and abs(c[0] - c_d[0]) > 1:
        c_move = [1,2]
    else:
        c_move = [-1,-2,1,2]

    moves = []
    if c[1] > c_d[1] and abs(c[1] - c_d[1]) > 1:
        for x in c_move:
            if abs(x) == 1:
                moves.append((x,-2))
            else:
                moves.append((x,-1))
    elif c[1] < c_d[1] and abs(c[1] - c_d[1]) > 1:
        for x in c_move:
            if abs(x) == 1:
                moves.append((x,2))
            else:
                moves.append((x,1))
    else:
        for x in c_move:
            if abs(x) == 1:
                moves.append((x,2))
                moves.append((x,-2))
            else:
                moves.append((x,1))
                moves.append((x,-1))

    ns = []
    for (x,y) in moves:
        n = (c[0] + x,c[1] + y)
        if (0 <= n[0] and n[0] < 8) and (0 <= n[1] and n[1] < 8):
            #valid
            ns.append(idx(n))
    return ns

def tr_deplicated(src, dest, trace):
    if len(trace) >= 8:
        return 64

    min = 64

    for v in next(src,dest):
        if v == dest:
            #print(trace + [v])
            return len(trace)
        else:
            if v in trace:
                continue
            trace.append(v)
            n_step = tr_deplicated(v, dest, trace)
            trace.pop()
            if n_step < min:
                min = n_step
    return min

def tr(src, dest):
    if src == dest:
        return 0
    step = 1
    visited = []
    queue = next(src, dest)
    while queue:
        n_queue = []
        for v in queue:
            if v == dest:
                return step
            else:
                if v in visited:
                    continue
                visited.append(v)
                n_queue.extend(next(v, dest))
        step += 1
        queue = n_queue

    return -1

def solution(src, dst):
    return tr(src, dst)
    #return tr_deplicated(src, dst, [src])

print(solution(0, 1))
print(solution(19,36))
print(solution(47,63))
print(solution(0, 63))

print(solution(49, 56))
print(solution(54, 63))
print(solution(0, 0))
print(solution(3, 0))


# Class for a logging 'server'
# In general, an event takes the form of:
#   event = {
#       'eventType': 'someType',
#           userCommand
#           accountTransaction
#           systemEvent
#           quoteServer
#           errorEvent
#       'timestamp': seconds since the epoch,
#       'server': 'where the server originated from',
#       'transactionNum': the transaction number the event is associated with,
#       'username': 'userId of the user who triggered the event'
#       SOME ADDITIONAL ARGS
#       userCommand
#           args: {
#               'command': {'name': ,
#                           args{}}'string representing the user's command',
#                   commit_buy
#                   cancel_buy
#                   commit_sell
#                   cancel_sell
#                   display_summary
#                       no additional args
#
#                   quote
#                   cancel_set_buy
#                   cancel_set_sell
#                       stockSymbol
#
#                   dumplog
#                       fileName
#
#                   add
#                       funds
#
#					buy
#					sell
#					set_buy_amount
#					set_sell_amount
#					set_buy_trigger
#					set_sell_trigger
#						stockSymbol
#						funds
#           }
#       accountTransaction
#           args: {
#               'action': string corresponding to type of account transaction
#                   add
#                   remove
#                   reserve
#                   free
#               'funds': amount of money being moved
#           }
#       systemEvent
#           args: {
#               'command': same as in userCommand
#           }
#       quoteServer
#           args: {
#               'quoteServerTime': time the quote was received from the quote server,
#               'stockSymbol': 'stcksymbl',
#               'price': price of the stock at the time the server quoted it,
#               'cryptokey': 'cryptographic key the server returns'
#           }
#       errorEvent
#           args: {
#               'command': same as in userCommand,
#               'errorMessage': message associated with the error
#           }
#   }

class Event(object):
	# possible event types
	eventTypes = {
		'userCommandEvent': 'userCommandEvent',
		'accountTransactionEvent': 'accountTransactionEvent',
		'systemEvent': 'systemEvent',
		'quoteServerEvent': 'quoteServerEvent',
		'errorEvent': 'errorEvent'
	}

	accountTransactionEvents = {
		'add': 'add',
		'remove': 'remove',
		'reserve': 'reserve',
		'free': 'free'
	}

	def __init__(self, **kwargs):
		# get base event kwargs
		self.eventType = kwargs['eventType']
		self.timeStamp = kwargs['timeStamp']
		self.server = kwargs['server']
		self.transactionNum = kwargs['transactionNum']
		self.userId = kwargs['userId']
		# handle the other kwargs
		self._handleKwargs(**kwargs)

	def _handleKwargs(self, **kwargs):
		# figure out what type of event we are dealing with
		if self.eventType == 'userCommandEvent':
			self._handleUserCommandEventKwargs(**kwargs)
		elif self.eventType == 'accountTransactionEvent':
			self._handleAccountTransactionEventKwargs(**kwargs)
		elif self.eventType == 'systemEvent':
			self._handleSystemEventKwargs(**kwargs)
		elif self.eventType == 'quoteServerEvent':
			self._handleQuoteServerEventKwargs(**kwargs)
		elif self.eventType == 'errorEvent':
			self._handleErrorEventKwargs(**kwargs)

	def _handleUserCommandEventKwargs(**kwargs):
		self._handleCommandKwargs(**kwargs)

	def _handleAccountTransactionEventKwargs(self, **kwargs):
		self.accountTransactionEventType = self.accountTransactionEvents[kwargs['AccountTransactionEventType']]
		self.funds = kwargs['funds']

	def _handleSystemEventKwargs(self, **kwargs):
		self._handleCommandKwargs(**kwargs)

	def _handleQuoteServerEventKwargs(self, **kwargs):
		self.quoteServerTime = kwargs['quoteServerTime']
		self.stockSymbol = kwargs['stockSymbol']
		self.price = kwargs['price']
		self.cryptoKey = kwargs['cryptoKey']

	def _handleErrorEventKwargs(self, **kwargs):
		self.errorMessage = kwargs['errorMessage']
		self._handleCommandKwargs(**kwargs)

	def _handleCommandKwargs(self, **kwargs):
		self.commandName = kwargs['commandName']
		if self.commandName in ['quote', 'cancel_set_buy', 'cancel_set_sell']:
			self.stockSymbol = kwargs['stockSymbol']
		elif self.commandName in ['dumplog']:
			self.fileName = kwargs['fileName']
		elif self.commandName in ['add']:
			self.amount = kwargs['amount']
		elif self.commandName in ['buy', 'sell', 'set_buy_amount', 'set_sell_amount', 'set_buy_trigger', 'set_sell_trigger']:
			self.amount = kwargs['amount']
			self.stockSymbol = kwargs['stockSymbol']
		elif self.commandName in ['commit_buy', 'cancel_buy', 'commit_sell', 'cancel_sell', 'display_summary']:
			pass

	def serialize(self):
		# for each diff type of eventType, extend the base serialization.
		return dict(self._handleKwargsSerialization(),
			eventType=self.eventType,
			timeStamp=self.timeStamp,
			server=self.server,
			transactionNum=self.transactionNum,
			userId=self.userId
		)

	def _handleKwargsSerialization(self):
		# for each type of event handle the serialization
		# figure out what type of event we are dealing with
		if self.eventType == 'userCommandEvent':
			return self._handleUserCommandEventKwargsSerialization()
		elif self.eventType == 'accountTransactionEvent':
			return self._handleAccountTransactionEventKwargsSerialization()
		elif self.eventType == 'systemEvent':
			return self._handleSystemEventKwargsSerialization()
		elif self.eventType == 'quoteServerEvent':
			return self._handleQuoteServerEventKwargsSerialization()
		elif self.eventType == 'errorEvent':
			return self._handleErrorEventKwargsSerialization()

	def _handleUserCommandEventKwargsSerialization(self):
		return self._handleCommandSerialization()

	def _handleAccountTransactionEventKwargsSerialization(self):
		return {
			'accountTransactionEventType': self.accountTransactionEventType,
			'funds': self.funds
		}

	def _handleSystemEventKwargsSerialization(self):
		return self._handleCommandSerialization()

	def _handleQuoteServerEventKwargsSerialization(self):
		return {
			'quoteServerTime': self.quoteServerTime,
			'stockSymbol': self.stockSymbol,
			'price': self.price,
			'cryptoKey': self.cryptoKey
		}

	def _handleErrorEventKwargsSerialization(self, **kwargs):
		return dict(self._handleCommandSerialization(),
			errorMessage=self.errorMessage
		)

	def _handleCommandSerialization(self):
		if self.commandName in ['quote', 'cancel_set_buy', 'cancel_set_sell']:
			return {
				'commandName': self.commandName,
				'stockSymbol': self.stockSymbol
			}
		elif self.commandName in ['dumplog']:
			return {
				'commandName': self.commandName,
				'fileName': self.fileName
			}
		elif self.commandName in ['add']:
			return {
				'commandName': self.commandName,
				'amount': self.amount
			}
		elif self.commandName in ['buy', 'sell', 'set_buy_amount', 'set_sell_amount', 'set_buy_trigger', 'set_sell_trigger']:
			return {
				'commandName': self.commandName,
				'amount': self.amount,
				'stockSymbol': self.stockSymbol
			}
		elif self.commandName in ['commit_buy', 'cancel_buy', 'commit_sell', 'cancel_sell', 'display_summary']:
			return {
				'commandName': self.commandName,
			}


import discord
from discord.ext import commands
from sys import platform
import os
import subprocess
import time
import threading

#import psutil

"""
def find_a(exe: str, return_arr):
    found = false;
    while not found:
        if return_arr[1] < 0:
            return
        for process in psutil.process_iter():
            if process.name() == exe:
                a_proc = process;
                found = True;
                break;
    a_proc_mem = a_proc.memory_full_info().uss;
    return_arr[0] = a_proc_mem;
    return
"""

def write_from_source(language:str, source:str):
    source_file = os.path.join(os.getcwd(), "compiled", "source.{0}".format(language));
    try:
        with open(source_file, 'w', encoding='utf-8') as f:
            f.write(source);
            f.close();
            return 0;
    except:
        return -1;

def compile_from_file(source):
    pass

def run_from_exe(source):
    pass

@commands.command()
async def run(ctx, language: str, *, code=None):
    output = discord.Embed();
    python = ["py", "python"];
    clang = ["c", "c++", "cpp"];
    in_cpp = language in clang;
    in_py = language in python;
    if (not in_cpp and not in_py):
        output.title = "Writing source failed";
        output.color = discord.Color.red();
        output.add_field(name="Reason :", value="Please specify a supported language");
        await ctx.send(embed=output);
        return -1

    # Strip everything other than the source code
    source = ctx.message.content[5:]
    source = source[len(language)+1:];
    first_tilde = source[:3];
    if (first_tilde == "```"):
        source = source[3:-4];
        char_index = 0;
        while (source[char_index] != "\n"):
            char_index = char_index + 1;
        else:
            source = source[char_index:];
    else:
        while source[0] == "\n":
            source = source[1:];

    # Check for sus codes and set language = filetype
    # I (Joe) run the code on termux, and it doesnt support gcc
    # but my pc does, so it is this way, yeah
    if (in_py):
        compiler = "python3"
        language = "py";
        sus_list = ["import os", "from os", "from pathlib", "import path", "import sys", "from sys"];
        for sus in sus_list:
            if sus in source:
                output.title = "Writing source failed";
                output.color = discord.Color.red();
                output.add_field(name="Reason :",value="Risky libraries are not allowed");
                await ctx.send(embed=output);
                return -1;

    elif (in_cpp):
        if (language == "c"):
            language = "c";
            if platform == "win32":
                compiler = "gcc";
            else:
                compiler = "clang";
        else:
            language = "cpp";
            if platform == "win32":
                compiler = "g++";
            else:
                compiler = "clang++";

    write_src_error = write_from_source(language, source);
    if (write_src_error == -1):
        output.title = "Writing source failed";
        output.color = discord.Color.dark_red();
        output.description = "Something unknown went wrong!";
        await ctx.send(embed=output);
        return -1;

    try:
        if platform == "win32":
            file = "a.exe";
        else:
            file = "a.out";
        os.remove(file);
    except:
        pass

    # compilation and running
    try:
        prev_time = time.time();
        source_file = os.path.join(os.getcwd(), "compiled", "source.{0}".format(language));
        compile_process = subprocess.run([compiler, source_file], stderr=subprocess.PIPE, text=True, timeout=15, stdout=subprocess.PIPE);
        compile_time = time.time() - prev_time;
        if (compile_process.returncode != 0):
            error = discord.Embed();
            error.title = "Compilation not successful";
            error.set_footer(text=f"Compile time : {compile_time}s");
            error.color = discord.Color.red();
            error.add_field(name="stderr :", value=f"```{compile_process.stderr} ```");
            stdout = compile_process.stdout;
            if stdout != None:
                error.add_field(name="stdout :", value=f"```{compile_process.stderr} ```");
            await ctx.send(embed=error);
        else:
            # Python is interpreted
            if language == "py":
                output.title = "Running completed";
                output.color = discord.Color.green();
                output.add_field(name="Output:", value=f"```{compile_process.stdout} ```");
                output.set_footer(text=f"Runtime : {compile_time}s\nMemory : KB");
                await ctx.send(embed=output);
                return 0;

            output.title = "Compilation completed";
            if platform == "win32":
                file = "a.exe";
                command = ".\\" + file;
            else:
                file = "a.out";
                command = "./" + file;
            memory = [0, 1];
            #pid = threading.Thread(target=find_a, args=[file, memory],daemon=True);
            try:
                #pid.start();
                prev_time = time.time();
                run_process = subprocess.run(command, stderr=subprocess.PIPE, text=True, timeout=45, stdout=subprocess.PIPE);
                run_time = time.time() - prev_time;
                # thread should stop
                memory[1] = -1;
                if (run_process.returncode != 0):
                    warning = discord.Embed();
                    warning.title = "Compilation completed";
                    warning.color = discord.Color.light_gray();
                    warning.description = f"Error while running : returncode ({run_process.returncode})";
                    warning.add_field(name="stderr :",value=f"```{run_process.stderr} ```");
                    warning.add_field(name="stdout :", value=f"```{run_process.stdout} ```");
                    warning.set_footer(text=f"Compile time : {compile_time}s\nRuntime : {run_time}s");
                    await ctx.send(embed=warning);
                else:
                    output.description = "Running completed";
                    output.color = discord.Color.green();
                    output.add_field(name="Output :",value=f"```{run_process.stdout} ```");
                    if memory[0] == 0:
                        membytes = "-";
                    else:
                        membytes = memory[0];
                    output.set_footer(text=f"Compile time : {compile_time}s\nRuntime : {run_time}s\nMemory : {membytes} bytes");
                    await ctx.send(embed=output);
                return 0;

            except subprocess.TimeoutExpired as e:
                error = discord.Embed();
                error.title = "Running failed!";
                error.color = discord.Color.red();
                error.add_field(name="Reason :", value="Runtime took to long! The limit is 45 seconds!");
                await ctx.send(embed=error);
                return -1;
            except Exception as e:
                error_str = str(e);
                error = discord.Embed();
                error.title = "Running failed!";
                error.add_field(name="Reason :", value=f"```{error_str} ```");
                error.color = discord.Color.red();
                await ctx.send(embed=error);
                return -1;

    except subprocess.TimeoutExpired as e:
        error = discord.Embed();
        error.title = "Compilation failed";
        error.color = discord.Color.red();
        error.add_field(name="Reason :", value="Compilation took to long! The limit is 15 seconds!");
        await ctx.send(embed=error);
    except Exception as e:
        error = discord.Embed();
        error.title = "Compilation failed";
        error_str = str(e);
        error.color = discord.Color.red();
        error.add_field(name="Reason :", value=f"```{error_str} ```");
        await ctx.send(embed=error);
    finally:
        return -1;

@run.error
async def run_error(ctx, error):
    if isinstance(error, commands.BadArgument):
        output = discord.Embed(title="Command Error", description="Wrong arguments, to run code, type `;run <language> <code>`", color=discord.Color.red());
        await ctx.send(embed=output);
        return
    print(error);

def setup(bot):
  bot.add_command(run);


import numpy as np
from neuron import h,gui
import signal
import pickle
from matplotlib import pyplot as plt
from glob import glob
import pandas as pd
import sys
from extra_function import load_ASC,SIGSEGV_signal_arises
from read_spine_properties import get_spine_xyz,get_n_spinese, get_spine_part
signal.signal(signal.SIGSEGV, SIGSEGV_signal_arises)

if len(sys.argv) != 7:
    print("sys.argv not running and with length",len(sys.argv))
    cells= ['2017_05_08_A_4-5','2017_05_08_A_5-4','2017_03_04_A_6-7']
    file_type='new.ASC'
    folder_='/ems/elsc-labs/segev-i/moria.fridman/project/analysis_groger_cells'
    with_plot=True
else:
    print("sys.argv is correct and running")
    cells = [sys.argv[1],sys.argv[2],sys.argv[3]]
    file_type=sys.argv[4]
    folder_=sys.argv[5]
    with_plot=eval(sys.argv[6])
folder_data=folder_+'/cells_initial_information/'
folder_save=folder_+'/cells_outputs_data/'

def synaptic_loc(cell_dir,syn_poses_list,with_plot=False, part='all', save_place=''):
    dict2={}
    cell=None
    cell=load_ASC(cell_dir)
    #syn_pose should be (x,y,z) coordinates
    # h.load_file("import3d.hoc")
    h.load_file("nrngui.hoc")
    secs,dends,dists,dends_name,dis_from_soma,sec_name,sec_num,seg_num=[],[],[],[],[],[],[],[]
    for i in range(len(syn_poses_list)):
        secs.append(None)
        dends.append(None)
        sec_num.append(None)
        sec_name.append(None)
        seg_num.append(None)
        dists.append(10000)
        dends_name.append(None)
        dis_from_soma.append(None)

    if part == 'all':
        relevant_sections = cell.all_sec()
    elif part == 'basal':
        relevant_sections = cell.dend
    elif part == 'apical':
        if len(cell.apic)==0:
            relevant_sections = cell.dend
        else:
            relevant_sections = cell.apic
    else:
        raise BaseException('the part is not good '+str(part))
    all_points = []
    for sec in relevant_sections:
        lens = []
        initial_point = np.array([sec.x3d(0), sec.y3d(0), sec.z3d(0)])
        for i in range(sec.n3d()):
            lens.append(np.linalg.norm(initial_point - np.array([sec.x3d(i), sec.y3d(i), sec.z3d(i)])))
            initial_point = np.array([sec.x3d(i), sec.y3d(i), sec.z3d(i)])
        total_len = np.sum(lens)
        accumalate_len = 0
        initial_point = np.array([sec.x3d(0), sec.y3d(0), sec.z3d(0)])
        points = [initial_point]
        for i in range(1,sec.n3d()):
            dend_pos = np.array([sec.x3d(i), sec.y3d(i), sec.z3d(i)])
            points_diffrance = dend_pos-initial_point
            distance = np.linalg.norm(initial_point - dend_pos)
            number_of_steps =int(np.ceil(distance))
            for step_number in range(1, number_of_steps, 1):
                intermideate_point = initial_point.copy() + points_diffrance*step_number/number_of_steps
                points.append(intermideate_point)
            points.append(dend_pos)
            initial_point = dend_pos
        initial_point = points[0]
        all_points+=points
        for point in points[1:]:
            dend_pos = point
            accumalate_len += np.linalg.norm(initial_point - dend_pos)
            initial_point = dend_pos
            for j, syn_pos in enumerate(syn_poses_list):
                if np.linalg.norm(syn_pos - dend_pos) < dists[j]:
                    dists[j] = np.linalg.norm(syn_pos - dend_pos)
                    secs[j] = [sec, accumalate_len / total_len]
                    dends[j]=[sec,round(accumalate_len / total_len,3)]
                    dends_name[j]=[str(sec)[str(sec).find('>')+2:],round(accumalate_len / total_len,3)]
                    sec_name[j]=str(sec)[str(sec).find('>')+2:]
                    seg_num[j]=round(accumalate_len / total_len,3)
                    sec_num[j]=int(str(sec)[str(sec).find('[')+1:-1])
    if with_plot:
        plt.figure()
        for p in all_points:
            plt.scatter(p[0], p[1], color='black',s=0.5)
    dend_pos_dict={}
    for j,dend in enumerate(dends_name):
        sec=eval('cell.'+dend[0])
        initial_point = np.array([sec.x3d(0), sec.y3d(0), sec.z3d(0)])
        points_dend = [initial_point]
        for i in range(1,sec.n3d()):
            dend_pos = np.array([sec.x3d(i), sec.y3d(i), sec.z3d(i)])
            # points_diffrance = dend_pos-initial_point
            # distance = np.linalg.norm(initial_point - dend_pos)
            # number_of_steps =int(np.ceil(distance))
            # for step_number in range(1, number_of_steps, 1):
            #     intermideate_point = initial_point.copy() + points_diffrance*step_number/number_of_steps
            #     points_dend.append(intermideate_point)
            points_dend.append(dend_pos)
            dend_pos_dict[j]=points_dend
            # initial_point = dend_pos
            if with_plot:
                plt.scatter(dend_pos[0], dend_pos[1], color='g',s=0.5)
        # for p in points_dend:
        #     plt.scatter(p[0], p[1], color='g',s=0.5)
    if with_plot:
        for j, syn_pos in enumerate(syn_poses_list):
            # if type(syn_pos)!=list:
            #     syn_pos=[syn_pos]
            dis_from_soma[j]=syn_dis_from_soma(cell,dends_name[j])
            plt.scatter(syn_pos[0], syn_pos[1],s=0.7, color='cyan')
            plt.text(-50,-50,str(syn_pos)+'dis from soma='+str(dis_from_soma[j]))
        color_code={'basal':'blue','apical':'red','axon':'green','soma':'purple','synapse':'cyan','syn_trunk':'green'}
        for i in range(cell.soma.n3d()):
            plt.scatter(cell.soma.x3d(i), cell.soma.y3d(i),lw=cell.soma.diam3d(i), color=color_code['soma'])

        legend_elements = [
        plt.scatter(syn_pos[0], syn_pos[1], color='black', s=0.5, label="all_point"),
        plt.scatter(syn_pos[0], syn_pos[1], color='green',s=0.5, label="syn_trunk"),
        plt.scatter(syn_pos[0], syn_pos[1], color=color_code['soma'],s=0.5, label="soma"),
        plt.scatter(syn_pos[0], syn_pos[1], color='cyan', s=0.5, label="synapse")
            ]
        plt.legend(handles=legend_elements, loc="best")
        plt.savefig(save_place+'.pdf')
        plt.savefig(save_place)
        plt.close()
    with open(save_place + '_neuron_morphology.p', 'wb') as f:
        pickle.dump({"all_point":all_points,"synaptic_dend":dend_pos_dict,"syn_pos":xyz,"syn_sec_pos":dends_name}, f)

    dict =  { 'sec_name':sec_name,'sec_num':sec_num,'seg_num':seg_num,'place_name':dends_name,'dist_from_soma':dis_from_soma,'dist':dists, 'part':part}
    print(dict)

    try_save_dict(dict,folder_save+cell_name+'/','synaptic_location'+file_type)
    for i in range(len(syn_poses_list)):
        dict2[str(i)] = {'sec_name':sec_name[i],'sec_num':sec_num[i],'seg_num':seg_num[i],'place_name':dends_name[i],'dist_from_soma':dis_from_soma[i],'dist':dists[i], 'part':part}
        print(dict2)

    try_save_dict(dict2,folder_save+cell_name+'/','synaptic_location_seperate'+file_type)
    return dict,dict2

def syn_dis_from_soma(cell,syn_loc):
    h.distance(0, 0.5, sec=cell.soma)
    synapses_dis_from_soma=h.distance(eval('cell.' + syn_loc[0])(syn_loc[1]))
    return synapses_dis_from_soma
def try_save_dict(dict,folder_save,name):
    try:
        with open(folder_save + name+'.p', 'wb') as f:
            pickle.dump(dict, f)
    except Exception as e:
        print("Error trying to save pickle: " + str(e))
        pass

    with open(folder_save+name+'.txt', 'w') as f:
        try:
            f.write('synaptic_location')
            f.write(dict)
        except Exception as e:
            print("Error trying to save txt: " + str(e))
            pass
    try:
        df1 = pd.DataFrame(dict)
        df1.to_excel(folder_save+name+".xlsx")
    except Exception as e:
        print("Error trying to save xlsx: " + str(e))
        pass
if __name__=='__main__':
    dict2={}
    dict3,dict4={},{}
    name2save=''
    for cell_name in cells:
        xyz,dend_part=[],[]
    # for cell_name in ['2017_05_08_A_5-4']:
        if len(glob(folder_data+cell_name+'/*'+file_type))<1:continue
        dir=glob(folder_data+cell_name+'/*'+file_type)[0]
        for i in range(get_n_spinese(cell_name)):
            print('one syn dict:',dict)
            xyz.append(list(get_spine_xyz(cell_name,i)))
            dend_part.append(get_spine_part(cell_name,i))
        dict1,dict2=synaptic_loc(dir,xyz, part='all', save_place=folder_save+cell_name+'/synapses'+file_type,with_plot=with_plot)
        dict3[cell_name]=dict1
        for key in dict2.keys():
            dict4[cell_name+key]=dict2[key]
        print('more then one syn dict',cell_name,[xyz])
    try_save_dict(dict3,folder_save,'synaptic_location'+file_type)
    try_save_dict(dict4,folder_save,'synaptic_location_seperate'+file_type)


from os import uname
import sys
import time
import requests
from rich.console import Console
from rich.table import Table
from rich.text import Text
from argparse import ArgumentParser, RawTextHelpFormatter
from collections import OrderedDict
from rdflib import ConjunctiveGraph, Namespace, URIRef
from rdflib.namespace import RDF, RDFS, OWL
import jellyfish

def download_EDAM():
    r = requests.get("http://edamontology.org/EDAM.owl")
    with open("EDAM.owl", "wb") as f:
        f.write(r.content)

# a single function to load EDAM and get the graph object as a result
def load_EDAM():

    g = ConjunctiveGraph()
    try:
        g.load("./EDAM.owl", format="xml")
    except FileNotFoundError:
        download_EDAM()
        g.load("./EDAM.owl", format="xml")
    g.bind("edam", Namespace("http://edamontology.org#"))
    g.bind("oboInOwl", Namespace("http://www.geneontology.org/formats/oboInOwl#"))
    return g

class LimitedSizeDict(OrderedDict):
    def __init__(self, *args, **kwds):
        self.size_limit = kwds.pop("size_limit", None)
        OrderedDict.__init__(self, *args, **kwds)
        self._check_size_limit()

    def __setitem__(self, key, value):
        OrderedDict.__setitem__(self, key, value)
        self._check_size_limit()

    def _check_size_limit(self):
        if self.size_limit is not None:
            while len(self) > self.size_limit:
                self.popitem(last=False)

def index_EDAM(edam_kg):
    index_label_to_uri = {}
    for subject, predicate, obj in edam_kg.triples((None, RDFS.label, None)):
        index_label_to_uri[str(obj)] = str(subject)

    for subject, predicate, obj in edam_kg.triples(
        (
            None,
            URIRef("http://www.geneontology.org/formats/oboInOwl#hasExactSynonym"),
            None,
        )
    ):
        index_label_to_uri[str(obj)] = str(subject)
    return index_label_to_uri

def get_edam_top_10_jaro(l, index):
    max_sim = 0
    min_dist = 100
    top_10 = LimitedSizeDict(size_limit=5)
    for label in index.keys():
        s = jellyfish.jaro_winkler(l, label)
        if s > max_sim:
            max_sim = s
            top_10[label] = s
            top_10.move_to_end(label, last=False)
    return top_10

def get_edam_top_10_jaro_ci(l, index):
    max_sim = 0
    min_dist = 100
    top_10 = LimitedSizeDict(size_limit=5)
    for label in index.keys():
        s = jellyfish.jaro_winkler(l.lower(), label.lower())
        if s > max_sim:
            max_sim = s
            top_10[label] = s
            top_10.move_to_end(label, last=False)
    return top_10

def get_edam_top_10(l, index):
    max_sim = 0
    min_dist = 100
    top = {}
    for label in index.keys():
        top[label] = jellyfish.jaro_winkler(l.lower(), label.lower())

    sorted_hits = dict(sorted(top.items(), key=lambda item: item[1], reverse=True))
    top_10 = {k: sorted_hits[k] for k in list(sorted_hits)[:10]}

    return top_10

parser = ArgumentParser(
    description="""
text2edam recommend EDAM term URIs based on syntactic similarity.

Usage examples :
    python text2edam.py -l

Please report any issue to alban.gaignard@univ-nantes.fr
""",
    formatter_class=RawTextHelpFormatter,
)

parser.add_argument(
    "-u",
    "--update",
    help="download or update the EDAM ontology",
)

parser.add_argument(
    "labels",
    metavar="label",
    type=str,
    nargs="+",
    help="input labels",
)

if __name__ == "__main__":

    if len(sys.argv) == 1:
        parser.print_help(sys.stderr)
        sys.exit(1)

    args = parser.parse_args()
    console = Console()

    table = Table(show_header=True, header_style="bold magenta")
    table.add_column("Label", justify="left")
    table.add_column("Matched term", justify="right")
    table.add_column("Confidence score", justify="right")
    table.add_column("Term URI", justify="right", style="green")

    if args.update:
        download_EDAM()
    elif args.labels:
        s1 = time.time()
        G = load_EDAM()
        ts1 = round((time.time() - s1), 2)
        console.print(f"Loaded {len(G)} triples from EDAM")
        console.print(f"EDAM ontology loaded in {ts1} s")

        s2 = time.time()
        index = index_EDAM(G)
        ts2 = round((time.time() - s2), 2)
        console.print(f"EDAM ontology indexed in {ts2} s")

        s3 = time.time()
        for label in args.labels:
            # top_10 = get_edam_top_10_jaro_ci(label, index=index)
            top_10 = get_edam_top_10(label, index=index)
            for hit in top_10.keys():
                table.add_row(
                    label,
                    hit,
                    str(top_10[hit]),
                    f"[link={index[hit]}]{index[hit]}[/link]",
                )
        ts3 = round((time.time() - s3), 2)
        console.print(f"Approximate term search in {ts3} s")
        console.print(table)


import pandas as pd
import numpy as np

from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold

import matplotlib.pyplot as plt
import timeit
import modeldata as md

starting_time = timeit.default_timer()

df = pd.read_csv('./train_set_2k_norm_pt.csv')
df.set_index('gene_id',drop=True, inplace=True)

genes_to_keep = []
with open ('xgbfeaimp_weight_top20.txt', 'r') as file:
    for line in file:
        genes_to_keep.append(line.strip())

df = df[df.index.isin(genes_to_keep)]

# transpose the df
df = df.T

df_y= pd.read_csv('./train_set_2k_metadata.csv')
df['label'] = list(df_y['label'])

x = df.drop(columns='label')
y = df['label']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=42, stratify=y)

scaler = MinMaxScaler()
x_train = pd.DataFrame(scaler.fit_transform(x_train), columns=x_train.columns)

scaler = MinMaxScaler()
x_test = pd.DataFrame(scaler.fit_transform(x_test), columns=x_test.columns)

# 10fold cv
cv = StratifiedKFold(n_splits=10, random_state=42, shuffle=True)
list_score = []
list_testsetscore = []

testset_y = pd.DataFrame(y_test).reset_index(drop=True)
testset= pd.concat([x_test, testset_y],axis=1)

# Run 10fold cv on each model
modelchoie = ['randomforest','xgboost','lgbm', 'svc']
for model in modelchoie:
    print("Currently running this model: ",model)
    for train_idx, valid_idx in cv.split(x_train, y_train):
        trainset = x_train.iloc[list(train_idx)]
        validset = x_train.iloc[list(valid_idx)]

        trainset_y = y_train.iloc[list(train_idx)]
        validset_y = y_train.iloc[list(valid_idx)]

        trainset.loc[:, 'label'] = list(trainset_y)
        validset.loc[:, 'label'] = list(validset_y)

        validscore = md.callmodel(trainset, model, separatetestset=True, testsetdf=validset, all=True)
        list_score.append(validscore)

        testsetscore = md.callmodel(trainset, model, separatetestset=True, testsetdf=testset, all=True)
        list_testsetscore.append(testsetscore)

outputdf = pd.DataFrame({'list_score': list_score, 'list_testsetscore': list_testsetscore })
outputdf.to_csv('modelselectionresult_2k_norm_pt_weight_gene_top20.csv', index=False)

print("Time taken :", timeit.default_timer() - starting_time)


import asyncio
import gc
import os
from joblib import Parallel, delayed
import pandas as pd
from prefect import flow, task
from prefect_ray.task_runners import RayTaskRunner
import numpy as np
from numpy import array, float32  # to eval strings as arrays

from sklearn.metrics import log_loss

import ray
from sklearn.model_selection import StratifiedKFold

from mlStack import (
    bootstrap,
    remove_all_flows,
    main as runMLstack,
    serializeBootstrapResults,
    serializeResultsDataframe,
)
from metaconfig import metaconfig
from config import config
from models import stack as modelStack
from tasks.input import processInputFiles

def relativePerplexity(y_true, y_pred, y_true_baseline, y_pred_baseline, epsilon=1e-15):
    samplePerplexity = perplexity(y_true, y_pred)
    baselineSamplePerplexity = perplexity(y_true_baseline, y_pred_baseline)

    return pd.Series(
        [
            samplePerplexity,
            baselineSamplePerplexity,
            np.divide(samplePerplexity, baselineSamplePerplexity + epsilon),
        ]
    )  # relative perplexity = perplexity / perplexity of model with single-most case correlated feature

def getBaselineFeatureResults(
    caseGenotypes,
    controlGenotypes,
    holdoutCaseGenotypes,
    holdoutControlGenotypes,
    clinicalData,
    config,
):
    selectedFeature = findBaselineFeature(caseGenotypes, controlGenotypes)
    outerCvIterator = StratifiedKFold(
        n_splits=config["sampling"]["crossValIterations"], shuffle=False
    )
    innerCvIterator = outerCvIterator

    bootstrap_args = [
        (
            caseGenotypes.loc[[selectedFeature]],
            controlGenotypes.loc[[selectedFeature]],
            holdoutCaseGenotypes.loc[[selectedFeature]],
            holdoutControlGenotypes.loc[[selectedFeature]],
            clinicalData,
            model,
            hyperParameterSpace,
            innerCvIterator,
            outerCvIterator,
            config,
            False,  # disable tracking
        )
        for model, hyperParameterSpace in list(modelStack.items())
    ]
    results = Parallel(n_jobs=-1)(delayed(bootstrap)(*args) for args in bootstrap_args)

    baselineFeatureResults = {}
    for i in range(len(modelStack)):
        modelResults = results[i]
        baselineFeatureResults = serializeBootstrapResults(
            modelResults, baselineFeatureResults
        )
    baselineFeatureResultsDataframe = serializeResultsDataframe(baselineFeatureResults)
    baselineFeatureResultsDataframe.index.name = "id"

    return baselineFeatureResultsDataframe, selectedFeature

def perplexity(y_true, y_pred):
    crossEntropy = log_loss(
        y_true, y_pred, labels=[0, 1], eps=1e-15
    )  # linear predictions (exactly 0 or 1) depend on offset of 1e-15 when log is applied to avoid NaN

    # perplexity = 2 ^ crossEntropy
    return np.power(2, crossEntropy)

def findBaselineFeature(caseGenotypes, controlGenotypes):
    # calculate the mean of each feature for cases and controls
    mean_cases = caseGenotypes.mean(axis=1)
    mean_controls = controlGenotypes.mean(axis=1)

    # calculate the absolute difference in means for each feature
    diff_means = abs(mean_cases - mean_controls)

    # get the feature with the largest difference in means
    selected_feature = diff_means.idxmax()

    print("Selected feature for baseline perplexity: ", selected_feature)
    return selected_feature

@task(retries=10)
def measureIterations(
    result,
    caseGenotypes,
    controlGenotypes,
    holdoutCaseGenotypes,
    holdoutControlGenotypes,
    clinicalData,
):
    baselineFeatureResults, selectedFeature = getBaselineFeatureResults(
        caseGenotypes,
        controlGenotypes,
        holdoutCaseGenotypes,
        holdoutControlGenotypes,
        clinicalData,
        config,
    )
    # serialize probability arrays from string
    result["probability"] = result["probability"].apply(lambda x: np.array(eval(x)))
    # take intersection of bootstrapped samples
    result = result.loc[baselineFeatureResults.index.intersection(result.index)]
    baselineFeatureResults = baselineFeatureResults.loc[result.index]
    result["baselineProbability"] = baselineFeatureResults["probability"]

    relativePerplexities = pd.DataFrame(index=result.index)
    new_cols = result.apply(
        lambda row: relativePerplexity(
            [row["label"]] * len(row["probability"]),
            row["probability"],
            [row["label"]] * len(row["baselineProbability"]),
            row["baselineProbability"],
        ),
        axis=1,
        result_type="expand",
    )

    (
        relativePerplexities["all features"],
        relativePerplexities[f"{selectedFeature}"],
        relativePerplexities["relative"],
    ) = (new_cols[0], new_cols[1], new_cols[2])

    # separate discordant & well-classified
    discordantSamples = result.loc[
        result["accuracy"] <= metaconfig["samples"]["discordantThreshold"],
    ]
    accurateSamples = result.loc[
        result["accuracy"] >= metaconfig["samples"]["accurateThreshold"],
    ]

    return (
        relativePerplexities,
        discordantSamples,
        accurateSamples,
        baselineFeatureResults,
    )

def readSampleIDs(table, label):
    sampleIDs = table.loc[table["label"] == label]
    return sampleIDs.index.tolist()

def sequesterOutlierSamples():
    samples = {
        "accurate": pd.read_csv(
            f"projects/{config['tracking']['project']}/accurateSamples.csv",
            index_col="id",
        ),
        "discordant": pd.read_csv(
            f"projects/{config['tracking']['project']}/discordantSamples.csv",
            index_col="id",
        ),
    }

    for sampleType, dataframe in samples.items():
        for labelType, label in {"case": 1, "control": 0}.items():
            if metaconfig["samples"]["sequester"][sampleType][labelType]:
                ids = readSampleIDs(dataframe, label=label)
                config["sampling"]["sequesteredIDs"].extend(ids)

@flow()
def main():
    newWellClassified = True
    countSuffix = 1

    baseProjectPath = config["tracking"]["project"]
    while newWellClassified:
        gc.collect()
        config["tracking"]["project"] = f"{baseProjectPath}__{str(countSuffix)}"
        if countSuffix <= metaconfig["tracking"]["lastIteration"]:
            sequesterOutlierSamples()
            countSuffix += 1
            continue
        else:
            (
                results,
                genotypeData,
                clinicalData,
            ) = runMLstack(config)
            config["sampling"]["lastIteration"] = 0
        currentResults = pd.read_csv(
            f"projects/{config['tracking']['project']}/sampleResults.csv",
            index_col="id",
        )
        (
            samplePerplexities,
            discordantSamples,
            accurateSamples,
            baselineFeatureResults,
        ) = measureIterations(
            currentResults,
            genotypeData.case.genotype,
            genotypeData.control.genotype,
            genotypeData.holdout_case.genotype,
            genotypeData.holdout_control.genotype,
            clinicalData,
        )

        samplePerplexities["accuracy"] = currentResults.loc[
            currentResults.index.intersection(samplePerplexities.index)
        ]["accuracy"]
        samplePerplexities["baselineAccuracy"] = baselineFeatureResults.loc[
            baselineFeatureResults.index.intersection(samplePerplexities.index)
        ]["accuracy"]
        samplePerplexities["label"] = currentResults.loc[
            currentResults.index.intersection(samplePerplexities.index)
        ]["label"]

        np.set_printoptions(threshold=np.inf)
        samplePerplexities.to_csv(
            f"projects/{config['tracking']['project']}/samplePerplexities.csv"
        )
        accurateSamples.to_csv(
            f"projects/{config['tracking']['project']}/accurateSamples.csv"
        )
        discordantSamples.to_csv(
            f"projects/{config['tracking']['project']}/discordantSamples.csv"
        )
        baselineFeatureResults["probability"] = baselineFeatureResults[
            "probability"
        ].map(lambda x: np.array2string(np.array(x), separator=","))
        baselineFeatureResults.to_csv(
            f"projects/{config['tracking']['project']}/baselineFeatureSampleResults.csv"
        )
        if (
            len(
                currentResults.loc[
                    (currentResults["label"] == 1)
                    & (
                        currentResults["accuracy"]
                        >= metaconfig["samples"]["accurateThreshold"]
                    )
                ]
            )
            == 0
        ):
            newWellClassified = False
            break
        sequesterOutlierSamples()
        countSuffix += 1

if __name__ == "__main__":
    # TODO replace notebook code with src imports
    ray.shutdown()
    main()
    clearHistory = True
    if clearHistory:
        asyncio.run(remove_all_flows())


def GeraEmail():
    nome = input('Digite seu nome ')
    sobrenome = input('Digite seu sobrenome ')
    RU = input('Informe seu RU')

    email = nome[0] + sobrenome + RU[0] + RU[1] + '@algoritmos.com.br'
    print(f'{nome}, seu email é {email}')

GeraEmail()


import numpy as np
import matplotlib.pyplot as plt
from data.pca import perform_pca

def read_data(file_path):
    '''
    read file path and return the dataset
    input(s):
      file_path (string): path of txt file
    output(s):
      dataset (ndarray): the dataset
    '''
    dataset = []
    # Open the file in read mode
    file = open(file_path, 'r')
    for f in file:
        # Split the content using commas
        split_content = f.split(',')
        split_content[-1] = split_content[-1][0]
        dataset.append(split_content)
    dataset = np.array(dataset).astype('float64')
    return dataset

def plot_features(dataset , label_names=[]):
    length = len(label_names)
    labels_column = dataset[:,-1]
    number_of_features = dataset.shape[1]
    labels = np.unique(labels_column).astype('int')
    if length != 0 and length !=len(labels):
        raise ValueError('the number of label names and features should be the same')
    if length == 0: label_names = labels
    for n in range(number_of_features-1):
        for l in labels:
            condition = np.where(labels_column == l)
            plt.hist(dataset[:, n][condition], density = True, bins=50, alpha=0.5, label=label_names[l], edgecolor='gray')
        plt.title('featre number' + str(n+1), alpha=0.7)
        plt.legend()
        plt.savefig('results/feature figures/'+ 'featre number' + str(n+1))
        plt.show()

def LDA(dataset, label_names=[]):
    X = dataset[:,:-1]
    y = dataset[:,-1]
    length = len(label_names)
    labels = np.unique(y).astype('int')
    if length != 0 and length !=len(labels):
        raise ValueError('the number of label names and features should be the same')
    if length == 0: label_names = labels
    condition1 = np.where(y == 0)
    condition2 = np.where(y == 1)
    data1 = X[condition1]
    data2 = X[condition2]
    # Compute class means
    mean_class1 = np.mean(data1, axis=0)
    mean_class2 = np.mean(data2, axis=0)
    # Compute the within-class scatter matrix
    scatter_within = np.dot((data1 - mean_class1).T, (data1 - mean_class1)) + \
                    np.dot((data2 - mean_class2).T, (data2 - mean_class2))
    # Compute the between-class scatter matrix
    scatter_between = np.outer(mean_class1 - mean_class2, mean_class1 - mean_class2)
    # Solve the generalized eigenvalue problem
    eigenvalues, eigenvectors = np.linalg.eig(np.linalg.inv(scatter_within).dot(scatter_between))
    # Sort eigenvectors by eigenvalues
    sorted_indices = np.argsort(eigenvalues)[::-1]
    eigenvalues = eigenvalues[sorted_indices]
    eigenvectors = eigenvectors[:, sorted_indices]
    # Choose the top k eigenvectors (k is the desired dimensionality)
    k = 1
    transformation_matrix = eigenvectors[:, :k]
    # Project the data onto the new subspace
    lda_data = np.dot(X, transformation_matrix)
    # Plot the LDA-transformed data
    plt.hist(lda_data[y == 0], bins=50, alpha=0.5, label=label_names[0], edgecolor='gray')
    plt.hist(lda_data[y == 1], bins=50, alpha=0.5, label=label_names[1], edgecolor='gray')
    plt.xlabel('LDA Component')
    plt.ylabel('Frequency')
    plt.legend()
    plt.title('Histogram of LDA-Transformed Data')
    plt.savefig('results/LDA figure/'+ 'LDA')
    plt.show()

def plot_pca2(dataset, label_names=[]):
    X = dataset[:,:-1]
    y = dataset[:,-1]
    length = len(label_names)
    labels = np.unique(y).astype('int')
    if length != 0 and length !=len(labels):
        raise ValueError('the number of label names and features should be the same')
    if length == 0: label_names = labels
    k = 2
    pca_data = perform_pca(X, k)
    # Plot the PCA-transformed data
    condition1 = np.where(y == 0)
    condition2 = np.where(y == 1)
    data1 = pca_data[condition1]
    data2 = pca_data[condition2]
    plt.scatter(data1[:,0], data1[:,1], alpha=0.7, label=label_names[0])
    plt.scatter(data2[:,0], data2[:,1], alpha=0.7, label=label_names[1])
    plt.xlabel('PCA Component')
    plt.ylabel('y')
    plt.title('PCA-Transformed Data')
    plt.savefig('results/pca2 figure/'+ 'pca2')
    plt.show()

def correlation_heatmap(hitmap_data, labels=None, title="Correlation Heatmap", cmap="gray_r", name='correlation heatmap'):
    correlation_matrix = np.corrcoef(hitmap_data, rowvar=False)
    plt.figure(figsize=(10, 8))
    plt.imshow(correlation_matrix, cmap=cmap)
    plt.colorbar()
    plt.xticks(np.arange(len(labels)), labels)
    plt.yticks(np.arange(len(labels)), labels)
    plt.title(title)
    plt.savefig('results/correlation heatmaps/'+ name)
    plt.show()

def cross_features(dataset, n_feature1, n_feature2, label_names=[]):
    X = dataset[:,:-1]
    y = dataset[:,-1]
    length = len(label_names)
    labels = np.unique(y).astype('int')
    if length != 0 and length !=len(labels):
        raise ValueError('the number of label names and features should be the same')
    if length == 0: label_names = labels
    # Plot the cross features
    condition1 = np.where(y == 0)
    condition2 = np.where(y == 1)
    data1 = X[condition1]
    data2 = X[condition2]
    plt.scatter(data1[:,n_feature1], data1[:,n_feature2], alpha=0.7, label=label_names[0])
    plt.scatter(data2[:,n_feature1], data2[:,n_feature2], alpha=0.7, label=label_names[1])
    plt.xlabel('feature ' + str(n_feature1))
    plt.ylabel('feature ' + str(n_feature2))
    plt.title('cross features')
    plt.savefig('results/cross features/'+ str(n_feature1) + '_and_' + str(n_feature2))
    plt.show()


import random

import pygame

SCREEN_SIZE = [1000, 800]
WHITE = (255, 255, 255)

CELL_RADIUS = 20
SNAKE_COLOR = (0, 0, 0)

FOOD_COLOR = (100, 100, 100)
FOOD_RADIUS = 20

UPDATE = pygame.USEREVENT + 1
Food = pygame.USEREVENT + 2

LIGHT_GREY = (100, 100, 100)
MSG_POSITION = (300, 500)

def init_game():
    pygame.init()
    pygame.display.set_caption("snake 贪吃蛇")
    pygame.time.set_timer(UPDATE, 500)
    pygame.time.set_timer(Food, 3000)

class Game:
    def __init__(self):
        self.running = True
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        self.snake = Snake()
        self.food = None
        self.message = None

class Cell:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def to_tuple(self):
        return self.x, self.y

    def copy(self):
        return Cell(self.x, self.y)

    def update(self, direction):
        if direction == "U":
            self.y -= CELL_RADIUS * 2
        elif direction == "D":
            self.y += CELL_RADIUS * 2
        elif direction == "L":
            self.x -= CELL_RADIUS * 2
        elif direction == "R":
            self.x += CELL_RADIUS * 2

class Snake:
    def __init__(self):
        cell_diameter = CELL_RADIUS * 2
        x = (SCREEN_SIZE[0] / cell_diameter // 2) * cell_diameter
        y = (SCREEN_SIZE[1] / cell_diameter // 2) * cell_diameter

        self.body = [Cell(x, y)]
        self.cell_size = CELL_RADIUS
        self.color = SNAKE_COLOR
        self.direction = "L"

    def update(self):
        head = self.body[0].copy()
        self.body.pop()
        head.update(self.direction)
        self.body.insert(0, head)

def is_snake_food_collide():
    head = game.snake.body[0].copy()
    head.update(game.snake.direction)
    return (game.food[0] - head.x) == 0 and (game.food[1] - head.y) == 0

def update():
    check_snake_dir()
    check_food()
    check_head_body_collision()
    check_out_boundary()
    check_win()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game.running = False
        if event.type == UPDATE:
            game.snake.update()
        if event.type == Food:
            generate_food()

def check_win():
    length = len(game.snake.body)
    cell_diameter = CELL_RADIUS * 2
    max_cell_x = SCREEN_SIZE[0] // cell_diameter
    max_cell_y = SCREEN_SIZE[1] // cell_diameter
    if length == 15:  # max_cell_x * max_cell_y:
        game.running = False
        game.message = "You WIN the game. Should restart? y/n"

def check_out_boundary():
    cell_diameter = 2 * CELL_RADIUS
    out_boundary_x_cells = [cell for cell in game.snake.body if
                            cell.x < cell_diameter or cell.x > SCREEN_SIZE[0] - cell_diameter]
    out_boundary_y_cells = [cell for cell in game.snake.body if
                            cell.y < cell_diameter or cell.y > SCREEN_SIZE[1] - cell_diameter]
    if len(out_boundary_x_cells) > 0 or len(out_boundary_y_cells) > 0:
        game.running = False
        game.message = "Snake is out of boundary. Should restart? y/n"

def check_head_body_collision():
    head = game.snake.body[0]
    body = game.snake.body
    try:
        index = [cell.to_tuple() for cell in body[1:]].index(head.to_tuple())
    except ValueError:
        index = -1
    if len(game.snake.body) > 1 and index > -1:
        game.snake.body = game.snake.body[:index]

def check_food():
    if game.food is not None and is_snake_food_collide():
        cell = Cell(game.food[0], game.food[1])
        game.snake.body.insert(0, cell)
        game.food = None

def check_snake_dir():
    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[pygame.K_UP] and not game.snake.direction == "D":
        game.snake.direction = "U"
    if pressed_keys[pygame.K_DOWN] and not game.snake.direction == "U":
        game.snake.direction = "D"
    if pressed_keys[pygame.K_LEFT] and not game.snake.direction == "R":
        game.snake.direction = "L"
    if pressed_keys[pygame.K_RIGHT] and not game.snake.direction == "L":
        game.snake.direction = "R"

def generate_food():
    while game.food is None:
        cell_diameter = CELL_RADIUS * 2
        rand_x = random.randint(1, (SCREEN_SIZE[0] / cell_diameter) - 1) * cell_diameter
        rand_y = random.randint(1, (SCREEN_SIZE[1] / cell_diameter) - 1) * cell_diameter
        if (rand_x, rand_y) not in [cell.to_tuple() for cell in game.snake.body]:
            game.food = (rand_x, rand_y)

def draw():
    game.screen.fill(WHITE)
    if game.running:
        draw_snake()
        draw_food()
    else:
        draw_restart()
    pygame.display.flip()

def draw_restart():
    font = pygame.font.Font(None, 30)
    restart_msg = font.render(game.message, True, LIGHT_GREY)
    game.screen.blit(restart_msg, MSG_POSITION)

def draw_food():
    if game.food is not None:
        pygame.draw.circle(game.screen, FOOD_COLOR, game.food, FOOD_RADIUS)

def draw_snake():
    for cell in game.snake.body:
        pygame.draw.circle(game.screen, game.snake.color, cell.to_tuple(), game.snake.cell_size)

def check_reatart():
    while not game.running:
        for event in pygame.event.get():
            if event.type == pygame.KEYUP and event.key == pygame.K_y:
                game.snake = Snake()
                game.running = True
            if event.type == pygame.KEYUP and event.key == pygame.K_n:
                return

if __name__ == "__main__":
    init_game()
    game = Game()
    while game.running:
        update()
        draw()
        check_reatart()

    pygame.quit()


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 20 15:29:19 2019

@author: Ham

Task from HackerRank
Given a string , which is the company name in lowercase letters,
your task is to find the top three most common characters in the string.

Print the three most common characters along with their occurrence count.
Sort in descending order of occurrence count.
If the occurrence count is the same, sort the characters in alphabetical order.
For example, according to the conditions described above,

 would have it's logo with the letters .

Input Format

A single line of input containing the string .

Constraints

Output Format

Print the three most common characters along with their occurrence count each on a separate line.
Sort output in descending order of occurrence count.
If the occurrence count is the same, sort the characters in alphabetical order.

Sample Input 0

aabbbccde
Sample Output 0

b 3
a 2
c 2
Explanation 0

Here, b occurs  times. It is printed first.
Both a and c occur  times. So, a is printed in the second line
and c in the third line because a comes before c in the alphabet.

Note: The string  has at least  distinct characters.
"""
import collections

if __name__ == '__main__':
    s = "szrmtbttyyaymadobvwniwmozojggfbtswdiocewnqsjrkimhovimghixqryqgzhgba"\
        "kpncwupcadwvglmupbexijimonxdowqsjinqzytkooacwkchatuwpsoxwvgrrejkukc"\
        "vyzbkfnzfvrthmtfvmbppkdebswfpspxnelhqnjlgntqzsprmhcnuomrvuyolvzlni"
    #s = input()
    #occ = {}
    #[occ.update({c: occ.get(c, 0)+1}) for c in s]
    #for c in s:
    #    occ.update({c: occ.get(c, 0) + 1})
    occ = collections.Counter(s)
    print(s)
    print(occ)
    occ_sorted = sorted(occ.items(), key=lambda x: x[1] * 1000 - ord(x[0]),
                        reverse=True)
    print(occ_sorted)
    [print(x[0], x[1]) for x in occ_sorted[:3]]


# flappyBall2.py
# Clinton Peterson
# 18 May 2017

from graphics import *
import random

#Define Constants
WIN_WIDTH = 800 #Pixels
WIN_HEIGHT = 800 #Pixels
UPDATE_RATE = 60 #frames per second
BALL_RAD = 30 #ball Size in Pixels
MOVEX = 2 #Pipe's moving left speed
MOVEY = 2 #Falling speed in pixels per frame
GAP_HEIGHT = 250 #in pixels
PIPE_WIDTH = 50 #in pixels

#Setup graphics window object
win = GraphWin("Flappy Ball", WIN_WIDTH, WIN_HEIGHT, autoflush=False)
win.setBackground("grey")

#Setup up the ball
ball01 = Circle(Point(200 ,WIN_HEIGHT/2), BALL_RAD)
ball01.setFill("blue")
ball01.setOutline("black")
ball01.setWidth(5)
ball01.draw(win)
def resetBall():
    ball01 = Circle(Point(200 ,WIN_HEIGHT/2), BALL_RAD)
    ball01.setFill("blue")

#Setup the pipe lists
topPipeList = []
bottomPipeList = []

#Setup the pipe function
def makePipes(lvl):
    global topPipeHeight
    topPipeHeight = random.randint(100,WIN_HEIGHT - GAP_HEIGHT - 100)
    topPipeList.append(Rectangle(Point(WIN_WIDTH-PIPE_WIDTH,0), Point(WIN_WIDTH,topPipeHeight)))
    topPipeList[lvl].setFill("green")
    topPipeList[lvl].setOutline("black")
    topPipeList[lvl].draw(win)
    bottomPipeList.append(Rectangle(Point(WIN_WIDTH-PIPE_WIDTH,WIN_HEIGHT), Point(WIN_WIDTH,topPipeHeight + GAP_HEIGHT)))
    bottomPipeList[lvl].setFill("green")
    bottomPipeList[lvl].setOutline("black")
    bottomPipeList[lvl].draw(win)

#Setup variable to be changed when you lose or win a level
gameOver = 0
level = 0

#Setup the text on the screen
message1 = Text(Point(WIN_WIDTH//2,100), "-Spacebar to flap-")
message1.draw(win)
message2 = Text(Point(WIN_WIDTH//2,150), "-q to quit-")
message2.draw(win)
message3 = Text(Point(WIN_WIDTH//2,200), "-Pipes passed-")
message3.draw(win)
message4 = Text(Point(WIN_WIDTH//2,240), level)
message4.draw(win)

###Main Program Loop
##playAgain = 1
##while playAgain == 1:
resetBall()
#Setup variable to be changed when you lose or win a level
gameOver = 0
level = 0
while True:
    #if level loop was broken becuase you lost, stop
    if gameOver == 1:
        break
    #Draw the pipes
    makePipes(level)
    #Sets a variable to be the same as the x cord of the leading edge of the pipe
    xPipe = WIN_WIDTH - PIPE_WIDTH
    #resets the level win condition counter
    winner = 0
    #This loops during each level until you win or lose
    while True:
        #Setting a variable for key presses
        key = win.checkKey()
        #For every space bar it flaps up 20 times what it falls each frame
        if key == "space":
            ball01.move(0, -20 * MOVEY)
        #quiting
        if key == "q":
            gameOver = 1
        #If the ball hits the top bottom, gameover
        if ball01.getCenter().getY() + BALL_RAD >= WIN_HEIGHT or ball01.getCenter().getY() - BALL_RAD <= 0 :
            gameOver = 1
        #If the pipe reaches past the left side, you win the level
        if xPipe < -PIPE_WIDTH:
            winner = 1
        #If the ball's xcord (+- the radius of the ball) is within the pipe's range check the nested if
        if ball01.getCenter().getX() + BALL_RAD >= xPipe and ball01.getCenter().getX() - BALL_RAD <= xPipe + PIPE_WIDTH :
            #if the ball's ycord (+- the radius of the ball) is within the pipe's range, gameover
            if ball01.getCenter().getY() - BALL_RAD <= topPipeHeight or ball01.getCenter().getY() + BALL_RAD >= topPipeHeight + GAP_HEIGHT:
                gameOver = 1
        #If gameover has been triggered, turn the ball red and break the loop
        if gameOver == 1 :
            ball01.setFill("red")
            break
        #If winner has been triggered, iterate the level counter, updates the display and breaks the loop
        if winner == 1:
            topPipeList[level].undraw()
            bottomPipeList[level].undraw()
            level = level +1
            message4.setText(level)
            break
        #Make the ball fall
        ball01.move(0, MOVEY)
        #Make both pipes move left
        topPipeList[level].move(-MOVEX, 0)
        bottomPipeList[level].move(-MOVEX, 0)
        #advance the counter that is equal the x cord of the leading edge of the pipe
        xPipe = xPipe - MOVEX
        #refresh the screen
        update(UPDATE_RATE)
#A loop that waits for a command to restart or quit
message5 = Text(Point(WIN_WIDTH//2,WIN_HEIGHT//2), "GAME OVER")
message5.draw(win)
message6 = Text(Point(WIN_WIDTH//2,WIN_HEIGHT//2+100), "Press y to play again or n to quit")
message6.draw(win)
update(UPDATE_RATE)
##    while True:
##        key = win.checkKey()
##        if key == "y":
##            message5.undraw()
##            message6.undraw()
##            topPipeList[level].undraw()
##            bottomPipeList[level].undraw()
##            ball01.undraw()
##            break
##        if key == "n":
##            playAgain = 0
##            break


import logging
import pytest

from mindarmour.utils.logger import LogUtil

@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_card
@pytest.mark.component_mindarmour
def test_logger():
    logger = LogUtil.get_instance()
    tag = 'Test'
    msg = 'i am %s, work in %s'
    args = ('tom', 'china')
    logger.debug(tag, msg, *args)
    logger.info(tag, msg, *args)
    logger.error(tag, msg, *args)
    logger.warn(tag, msg, *args)

    logger.set_level(logging.DEBUG)

    logger.debug(tag, msg, *args)
    logger.info(tag, msg, *args)
    logger.error(tag, msg, *args)
    logger.warn(tag, msg, *args)

    msg = 'i am tom, work in china'
    logger.info(tag, msg)

    logger.info(tag, 'accuracy is %f.', 0.995)
    logger.debug(tag, 'accuracy is %s.', 0.995)

@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_card
@pytest.mark.component_mindarmour
def test_value_error():
    with pytest.raises(SyntaxError) as e:
        assert LogUtil()
    assert str(e.value) == 'can not instance, please use get_instance.'
    logger = LogUtil.get_instance()
    handler = 'logging.Handler(level=1)'
    with pytest.raises(ValueError):
        logger.add_handler(handler)


'''
Created on Aug 8, 2016
Processing datasets.

@author: Xiangnan He (xiangnanhe@gmail.com)

Modified  on Nov 10, 2017, by Lianhai Miao
'''

import scipy.sparse as sp
import numpy as np
import torch
from torch.utils.data import TensorDataset, DataLoader
from collections import defaultdict
import math

class Data_loader:
    def __init__(self,dataset,batch_size):
        self.dataset = dataset
        self.batch_size = batch_size
        self.datasize = dataset.shape[0]

    def get_batch(self):
        index = torch.randint(self.datasize,[self.batch_size])
        return self.dataset[index]

class GDataset(object):

    def __init__(self, dataname, user_path, group_path, user_in_group_path, num_negatives, test_pos_neg_num):
        '''
        Constructor
        '''
        self.dataname = dataname
        self.user_in_group_path = user_in_group_path
        self.num_negatives = num_negatives
        self.test_pos_neg_num = test_pos_neg_num
        # user data
        self.user_trainMatrix, self.user_explicit_trainMatrix = self.load_rating_file_as_matrix(user_path + "Train.txt")
        self.num_users, self.num_items = self.user_trainMatrix.shape

        self.usertest_input, self.item_test_input = self.get_test_instances(user_path + "Negative.txt")

        # self.u_items_dict, self.i_users_dict = self.get_ui_dict(user_path + "Train.txt")
        # group data
        self.group_trainMatrix, self.gro_explicit_trainMatrix = self.load_rating_file_as_matrix(group_path + "Train.txt")

        self.grouptest_input, self.groupitem_test = self.get_test_instances(group_path + "Negative.txt")
        # self.gro_items_dict, self.i_groups_dict = self.get_ui_dict(group_path + "Train.txt")
        self.gro_members_dict, self.user_groups_dict = self.get_group_member_dict(user_in_group_path)
        self.num_groups = len(self.gro_members_dict)

        self.gro_user = self.get_group_member_input(user_in_group_path)
        self.userTrain = self.get_ug_train_instances(self.user_trainMatrix)
        self.groTrain = self.get_ug_train_instances(self.group_trainMatrix)

    def get_group_member_input(self, user_in_group_path):
        gro_input = []
        member_input = []
        with open(user_in_group_path, 'r') as f:
            line = f.readline().strip()
            while line != None and line != "":
                a = line.split(' ')
                g = int(a[0])
                # g_m_d[g] = []
                for m in a[1].split(','):
                    gro_input.append(g)
                    member_input.append(int(m))
                line = f.readline().strip()
        return gro_input, member_input

    def get_ug_train_instances(self, train):
        user_input, pos_item_input, neg_item_input = [], [], []
        num_users = train.shape[0]
        num_items = train.shape[1]
        for (u, i) in train.keys():
            # positive instance
            for _ in range(self.num_negatives):
                pos_item_input.append(i)
            # negative instances
            for _ in range(self.num_negatives):
                j = np.random.randint(num_items)
                while (u, j) in train:
                    j = np.random.randint(num_items)
                user_input.append(u)
                neg_item_input.append(j)
        u_pi_ni = [[u, pi, ni] for u, pi, ni in zip(user_input, pos_item_input, neg_item_input)]
        return np.array(u_pi_ni)

    def get_group_member_dict(self, user_in_group_path):
        g_m_d = defaultdict(list)
        u_g_d = defaultdict(list)
        with open(user_in_group_path, 'r') as f:
            line = f.readline().strip()
            while line != None and line != "":
                if self.dataname == 'MaFengWo' or self.dataname == 'CAMRa2011':
                    a = line.split(' ')
                    g = int(a[0])
                    # g_m_d[g] = []
                    for m in a[1].split(','):
                        g_m_d[g].append(int(m))
                        u_g_d[int(m)].append(g)
                elif self.dataname == 'ml-latest-small':
                    a = line.split(' ')
                    g = int(a[0])
                    # g_m_d[g] = []
                    for m in a[1:]:
                        g_m_d[g].append(int(m))
                        u_g_d[int(m)].append(g)
                line = f.readline().strip()
        return g_m_d, u_g_d

    # def get_user_groups_dict(self, user_in_group_path):
    #     u_g_d = defaultdict(list)
    #     with open(user_in_group_path, 'r') as f:
    #         line = f.readline().strip()
    #         while line != None and line != "":
    #             a = line.split(' ')
    #             g = int(a[0])
    #             for m in a[1].split(','):
    #                 u_g_d[int(m)].append(g)
    #             line = f.readline().strip()
    #     return u_g_d

    def load_rating_file_as_list(self, filename):
        ratingList = []
        with open(filename, "r") as f:
            line = f.readline()
            while line != None and line != "":
                arr = line.split(" ")
                user, item = int(arr[0]), int(arr[1])
                ratingList.append([user, item])
                line = f.readline()
        return ratingList

    def get_ui_dict(self, filename):
        # user_i -> items: 1: [10, 23]
        with open(filename, 'r') as reader:
            u_i_dict = defaultdict(list)
            i_u_dict = defaultdict(list)
            for line in reader:
                # 162540,32,4,...
                user_id, item_id = map(int, line.split(' ')[:2]) # ',' or '\t'
                u_i_dict[user_id].append(item_id)
                i_u_dict[item_id].append(user_id)

        return u_i_dict, i_u_dict

    def load_negative_file(self, filename):
        testRatingList, negativeList = [], []
        with open(filename, "r") as f:
            line = f.readline()
            while line != None and line != "":
                if line == "\n":
                    line = f.readline()
                    continue
                arr = line.split(" ")
                user, item = eval(arr[0])[0], eval(arr[0])[1]
                testRatingList.append([user, item])
                negatives = []
                for x in arr[1:]:
                    negatives.append(int(x))
                negativeList.append(negatives)
                line = f.readline()
        return testRatingList, negativeList

    def load_rating_file_as_matrix(self, filename):
        # Get number of users and items
        num_users, num_items = 0, 0
        with open(filename, "r") as f:
            line = f.readline()
            while line != None and line != "":
                arr = line.split(" ")
                u, i = int(arr[0]), int(arr[1])
                num_users = max(num_users, u)
                num_items = max(num_items, i)
                line = f.readline()
        # Construct matrix
        mat = sp.dok_matrix((num_users + 1, num_items + 1), dtype=np.float32)
        mat_explicit = sp.dok_matrix((num_users + 1, num_items + 1), dtype=np.float32)
        with open(filename, "r") as f:
            line = f.readline()
            while line != None and line != "":
                arr = line.split(" ")
                if len(arr) > 2:
                    user, item, rating = int(arr[0]), int(arr[1]), eval(arr[2])
                    if (rating > 0):
                        mat[user, item] = 1.0
                        mat_explicit[user, item] = rating
                else:
                    user, item = int(arr[0]), int(arr[1])
                    mat[user, item] = 1.0
                line = f.readline()
        return mat, mat_explicit

    def get_explicit_train_instances(self, train):
        user_input, item_input, rating = [], [], []
        num_users = train.shape[0]
        num_items = train.shape[1]
        max_rating = max(train.values())
        for (u, i) in train.keys():
            # positive instance
            user_input.append(u)
            item_input.append(i)
            rating.append(train[u, i]/max_rating)
            for _ in range(self.num_negatives):
                # negative instances
                j = np.random.randint(num_items)
                while (u, j) in train:
                    j = np.random.randint(num_items)
                user_input.append(u)
                item_input.append(j)
                rating.append(0)
        # pi_ni = [[pi, ni] for pi, ni in zip(pos_item_input, neg_item_input)]
        return user_input, item_input, rating

    def get_train_instances(self, train):
        user_input, pos_item_input, neg_item_input = [], [], []
        num_users = train.shape[0]
        num_items = train.shape[1]
        for (u, i) in train.keys():
            # positive instance
            for _ in range(self.num_negatives):
                pos_item_input.append(i)
            # negative instances
            for _ in range(self.num_negatives):
                j = np.random.randint(num_items)
                while (u, j) in train:
                    j = np.random.randint(num_items)
                user_input.append(u)
                neg_item_input.append(j)
        pi_ni = [[pi, ni] for pi, ni in zip(pos_item_input, neg_item_input)]
        return user_input, pi_ni

    def get_test_instances(self, filename):
        user_input, item_input = [], []
        with open(filename, "r") as f:
            line = f.readline()
            while line != None and line != "":
                if line == "\n":
                    line = f.readline()
                    continue
                if self.dataname == 'MaFengWo' or self.dataname == 'CAMRa2011':
                    arr = line.split(" ")
                    user, pos_item = eval(arr[0])[0], eval(arr[0])[1]
                    user_input.append(user)
                    item_input.append(pos_item)
                    for x in arr[1:]:
                        user_input.append(user)
                        item_input.append(int(x))
                elif self.dataname == 'ml-latest-small':
                    arr = line.split(' ')
                    user = int(arr[0])
                    pos_item = int(arr[1])
                    user_input.append(user)
                    item_input.append(pos_item)
                    for x in arr[2:]:
                        user_input.append(user)
                        item_input.append(int(x))
                line = f.readline()
        return user_input, item_input

    def get_user_dataloader(self, batch_size):
        user, positem_negitem_at_u = self.get_train_instances(self.user_trainMatrix)
        train_data = TensorDataset(torch.LongTensor(user), torch.LongTensor(positem_negitem_at_u))
        user_train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
        return user_train_loader

    def get_group_dataloader(self, batch_size):
        # group and positem_negitem_at_g are two lists
        group, positem_negitem_at_g = self.get_train_instances(self.group_trainMatrix)
        train_data = TensorDataset(torch.LongTensor(group), torch.LongTensor(positem_negitem_at_g))
        group_train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)

        return group_train_loader

    def get_ug_dataloader(self, batch_size):
        gro_input, member_input = self.get_group_member_input(self.user_in_group_path)
        train_data = TensorDataset(torch.LongTensor(gro_input), torch.LongTensor(member_input))
        train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)

        return train_loader

    def get_user_explicit_dataloader(self, batch_size):
        user_input, item_input, rating = self.get_explicit_train_instances(self.user_explicit_trainMatrix)
        train_data = TensorDataset(torch.LongTensor(user_input), torch.LongTensor(item_input), torch.Tensor(rating))
        user_explicit_train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)

        return user_explicit_train_loader

    def get_gro_explicit_dataloader(self, batch_size):
        gro_input, item_input, rating = self.get_explicit_train_instances(self.gro_explicit_trainMatrix)
        train_data = TensorDataset(torch.LongTensor(gro_input), torch.LongTensor(item_input), torch.Tensor(rating))
        gro_explicit_train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)

        return gro_explicit_train_loader

    def get_user_test_dataloader(self):
        test_tensor_data = TensorDataset(torch.LongTensor(self.usertest_input), torch.LongTensor(self.item_test_input))
        test_loader = DataLoader(test_tensor_data,batch_size=self.test_pos_neg_num + 1, shuffle=False)
        return test_loader

    def get_gro_test_dataloader(self):
        test_tensor_data = TensorDataset(torch.LongTensor(self.grouptest_input), torch.LongTensor(self.groupitem_test))
        test_loader = DataLoader(test_tensor_data, batch_size=self.test_pos_neg_num + 1, shuffle=False)
        return test_loader


from sklearn import datasets
import numpy as np
import collections
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier

irirs = datasets.load_iris()
irirs_data = irirs.data
irirs_label = irirs.target

size = len(irirs_data)

random_irirs_data = np.empty(irirs_data.shape, dtype=irirs_data.dtype)
random_irirs_label = np.empty(irirs_label.shape, dtype=irirs_label.dtype)

index = np.random.permutation(np.arange(size))

# random shuffle the origin dataset to cross-validation
for i in range(len(index)):
    random_irirs_data[i] = irirs_data[index[i]]
    random_irirs_label[i] = irirs_label[index[i]]

k = 5
intervals = int(size/k)
knn_accuracy, svm_accuracy, DecisionTree_accuracy = 0, 0, 0

for i in range(1, k+1):
    # print("now intervals:", (i-1)*intervals, "to",  i*intervals)
    x_test, y_test = random_irirs_data[(i-1)*intervals:i*intervals], random_irirs_label[(i-1)*intervals:i*intervals]
    x_train = np.concatenate((random_irirs_data[0:(i-1)*intervals], random_irirs_data[i*intervals:size]), axis=0)
    y_train = np.concatenate((random_irirs_label[0:(i-1)*intervals], random_irirs_label[i*intervals:size]), axis=0)

    knn = KNeighborsClassifier(n_neighbors=3, weights = 'uniform', algorithm='auto', leaf_size = 30, p=2,
                               metric = 'minkowski', metric_params=None)
    knn.fit(x_train, y_train)

    knn_result = collections.Counter(knn.predict(x_test) - y_test)
    knn_accuracy += knn_result[0]/intervals

    svm = SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto',
              kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True,tol=0.001, verbose=False)
    svm.fit(x_train, y_train)

    svm_result = collections.Counter(svm.predict(x_test) - y_test)
    svm_accuracy += svm_result[0]/intervals

    DecisionTree = DecisionTreeClassifier()
    DecisionTree.fit(x_train, y_train)

    DecisionTree_result = collections.Counter(DecisionTree.predict(x_test) - y_test)
    DecisionTree_accuracy += DecisionTree_result[0]/intervals

print(knn_accuracy/k)
print(svm_accuracy/k)
print(DecisionTree_accuracy/k)


"""
You are given an m x n integer matrix matrix with the following two properties:

    Each row is sorted in non-decreasing order.
    The first integer of each row is greater than the last integer of the previous row.

Given an integer target, return true if target is in matrix or false otherwise.

You must write a solution in O(log(m * n)) time complexity.

Example 1:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false

Constraints:

    m == matrix.length
    n == matrix[i].length
    1 <= m, n <= 100
    -104 <= matrix[i][j], target <= 104
"""

####################################################################
def searchMatrix(matrix, target: int) -> bool:
    ROWS, COLS = len(matrix), len(matrix[0])
    top, bot = 0, ROWS-1

    while top <= bot:
        row = (top+bot) //2
        if matrix[row][COLS-1] < target:
            top = row + 1
        elif matrix[row][0] > target:
            bot = row - 1
        else:
            break

    if not (top <= bot):
        return False
    row = (top + bot) //2
    l, r = 0, COLS-1
    while l <= r:
        m = (l+r) //2
        if matrix[row][m] < target:
            l = m + 1
        elif matrix[row][m] > target:
            r = m - 1
        else:
            return True
    return False
####################################################################
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]]
target1 = 3     # Output: true
target2 = 13    # Output: false
print(searchMatrix(matrix, target2))