diccionario_espanol_ingles = {}
diccionario_ingles_espanol = {}

def agregar_palabra(diccionario_espanol_ingles, diccionario_ingles_espanol):
    palabra_espanol = input("Ingrese la palabra en español: ")
    palabra_ingles = input("Ingrese la palabra en inglés: ")
    diccionario_espanol_ingles[palabra_espanol] = palabra_ingles
    diccionario_ingles_espanol[palabra_ingles] = palabra_espanol
    print("Palabras agregadas exitosamente.")

def traducir_palabra(diccionario_espanol_ingles, diccionario_ingles_espanol):
    palabra = input("Ingrese la palabra a traducir: ")
    if palabra in diccionario_espanol_ingles:
        traduccion = diccionario_espanol_ingles[palabra]
        print("Traducción:", traduccion)
    elif palabra in diccionario_ingles_espanol:
        traduccion = diccionario_ingles_espanol[palabra]
        print("Traducción:", traduccion)
    else:
        print("La palabra no se encuentra en el diccionario.")

def obtener_todas_las_palabras(diccionario):
    palabras = tuple(diccionario.keys())
    return palabras

def mostrar_menu():
    print("1. Agregar palabra al diccionario")
    print("2. Traducir palabra")
    print("3. Obtener todas las palabras en español")
    print("4. Obtener todas las palabras en inglés")
    print("5. Salir")

def ejecutar_menu():
    while True:
        mostrar_menu()
        opcion = input("Seleccione una opción: ")

        if opcion == "1":
            agregar_palabra(diccionario_espanol_ingles, diccionario_ingles_espanol)
        elif opcion == "2":
            traducir_palabra(diccionario_espanol_ingles, diccionario_ingles_espanol)
        elif opcion == "3":
            palabras_espanol = obtener_todas_las_palabras(diccionario_espanol_ingles)
            print("Palabras en español:", palabras_espanol)
        elif opcion == "4":
            palabras_ingles = obtener_todas_las_palabras(diccionario_ingles_espanol)
            print("Palabras en inglés:", palabras_ingles)
        elif opcion == "5":
            print("¡Hasta luego!")
            break
        else:
            print("Opción inválida. Por favor, seleccione una opción válida.")

ejecutar_menu()


import streamlit as st
from api import get_all_query_engines
from components.chat_history import chat_history_panel
from common.utils.logging_handler import Logger
import utils
from config import APP_BASE_PATH

Logger = Logger.get_logger(__file__)
params = st.experimental_get_query_params()
st.session_state.auth_token = params.get("auth_token", [None])[0]
auth_token = st.session_state.auth_token

def landing_page():
  st.title("Welcome.")
  chat_history_panel()

  start_chat, start_query, build_query = st.columns((1, 1, 1))

  with start_chat:
    with st.container():
      "Start a Chat with"
      agent_name = st.selectbox(
          "Agent:",
          ("Chat", "Plan"))
      chat_button = st.button("Start", key=2)
      if chat_button:
        utils.navigate_to(
          f"{APP_BASE_PATH}/Chat?agent_name={agent_name}"
          f"&auth_token={auth_token}")

  with start_query:
    # Get all query engines as a list
    query_engine_list = get_all_query_engines(auth_token=auth_token)

    if len(query_engine_list) == 0:
      with st.container():
        "No Query Engines"
    else:
      query_engines = {}
      for item in query_engine_list:
        query_engines[item["name"]] = item

      Logger.info(query_engines)
      with st.container():
        "Start a Query with"
        qe_name = st.selectbox(
            "Query Engine:",
            tuple(query_engines.keys()))
        query_button = st.button("Start", key=3)
        query_engine_id = query_engines[qe_name]["id"]
        if query_button:
          utils.navigate_to(
            f"{APP_BASE_PATH}/Query?query_engine_id={query_engine_id}"
            f"&auth_token={auth_token}")

  with build_query:
    with st.container():
      "Managing Query Engines"
      build_button = st.button("Start", key=4)
      if build_button:
        utils.navigate_to(
          f"{APP_BASE_PATH}/Query_Engines?auth_token={auth_token}")

if __name__ == "__main__":
  utils.init_api_base_url()
  landing_page()


from aiogram import F, Router
from aiogram.types import CallbackQuery

from templates import keyboards, texts

faq_router = Router()

@faq_router.callback_query(F.data == "faq")
async def faq_handler(call: CallbackQuery):
    await call.message.edit_text(
        text=texts.faq_greeting,
        reply_markup=keyboards.faq_kb(),
    )

@faq_router.callback_query(F.data.startswith("faq_question"))
async def faq_question_handler(call: CallbackQuery, callback_data: str):
    await call.message.edit_text(
        text=texts.faq_descriptions_mapper[callback_data],
        reply_markup=keyboards.build_back_button(callback_data="faq"),
    )


from django.shortcuts import render
from django.http import JsonResponse
from .blockchain import Blockchain

def mine_block(request):
    if request.method == 'POST':
        sender = request.POST.get('sender')
        recipient = request.POST.get('recipient')
        amount = float(request.POST.get('amount'))

        blockchain = Blockchain()
        transaction = blockchain.create_transaction(sender, recipient, amount)
        block = blockchain.mine_block()

        response_data = {
            'message': 'New block mined!',
            'block': {
                'id': block.id,
                'hash': block.hash,
                'timestamp': block.timestamp,
                'previous_hash': block.previous_hash,
                'transactions': [t.__str__() for t in block.transactions.all()]
            },
            'transaction': transaction.__str__()
        }
        return JsonResponse(response_data)

    return render(request, 'mine_block.html')

from django.shortcuts import render
from .models import Block, Transaction

def blockchain_explorer(request):
    blocks = Block.objects.all()
    transactions = Transaction.objects.all()
    context = {
        'blocks': blocks,
        'transactions': transactions,
    }
    return render(request, 'index.html', context)


"""

"""

import sys

def main():
    test_cases = int(input())
    for _ in range(test_cases):
        n = int(input())
        speeds = [int(x) for x in input().split()]
        max_speed = max(speeds)
        print(speeds.count(max_speed))

main()


import numpy as np
import pandas as pd
import h5py
import glob
import argparse
import yaml
import sys
import tqdm
from typing import List, Any, Dict, Tuple

sys.path.append('/mlodata1/lugeon/eeg_dream/scripts')
from interaction.interaction import ask_for_config

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', '-c', type=str, required=True)
    args = parser.parse_args()

    with open(args.config) as file:
        config = yaml.load(file, Loader=yaml.FullLoader)

    if ask_for_config(config): pass
    else: return

    transform_by_subject(**config)

def transform_by_subject(root_dir: str,
                         output_file: str,
                         metadata: str,
                         subject_pattern: str) -> None:
    """ Create an HDF5 dataset from the images of the archives contained in
    the root directory

    Args:
        root_dir (str): root directory with the subject-specific numpy archives
        output_file (str): where to write the HDF5 dataset
        metadata (str): metadata file used for creating the dataset
        subject_pattern (str): pattern for filtering subjects
    """

    print('\nCreate an HDF5 dataset with images...')

    # mapping between trials and labels
    df_metadata = pd.read_csv(metadata,
                              sep=' ',
                              header=0,
                              names=['trial', 'label', 'sleep_cycle', 'sleep_stage', 'elapsed_time'])

    labels_map = dict(zip(df_metadata.trial, df_metadata.label))
    cycle_map = dict(zip(df_metadata.trial, df_metadata.sleep_cycle))
    stage_map = dict(zip(df_metadata.trial, df_metadata.sleep_stage))
    etime_map = dict(zip(df_metadata.trial, df_metadata.elapsed_time))

    print('Compute size of the dataset...')

    # retrieve numpy archive containing the images, and the size of the dataset
    subjects = glob.glob(f'{root_dir}/{subject_pattern}')
    n_frames, n_gridpoints, n_channels = _get_dataset_shape(subjects, labels_map)

    # create HDF5 dataset
    f = h5py.File(output_file, 'w')
    dset_images = f.create_dataset('images', (n_frames, n_channels, n_gridpoints, n_gridpoints))
    dset_sid = f.create_dataset('subject_id', (n_frames,))
    dset_tid = f.create_dataset('trial_id', (n_frames,))
    dset_ss = f.create_dataset('sleep_stage', (n_frames,))
    dset_sc = f.create_dataset('sleep_cycle', (n_frames,))
    dset_fid = f.create_dataset('frame_id', (n_frames,))
    dset_labels = f.create_dataset('labels', (n_frames,))
    dset_etime = f.create_dataset('elapsed_time', (n_frames,))
    dset_snames = f.create_dataset('subject_name', (len(subjects),), dtype='S04')

    print('Fill the dataset...')

    # for each subject numpy archive
    sequence_counter = 0
    for subject_id, subject in tqdm.tqdm(enumerate(subjects), total=len(subjects), ncols=70,):

        subject_name = subject.split('/')[-1].split('.')[0]
        dset_snames[subject_id] = subject_name

        with np.load(subject) as images:

            # keep only trials in the labels mapping
            trials = [k for k in images.keys() if k.split('.')[0] in labels_map]

            # for each trial, fill the dataset
            for trial_id, trial in enumerate(trials):

                sequence = images[trial]
                sequence_length = sequence.shape[0]

                trial_key = trial.split('.')[0]
                label = labels_map[trial_key]
                sleep_stage = stage_map[trial_key]
                sleep_cycle = cycle_map[trial_key]
                etime = etime_map[trial_key]

                slice_ = slice(sequence_counter, sequence_counter + sequence_length)

                dset_images[slice_] = sequence
                dset_labels[slice_] = sequence_length * [label]
                dset_sid[slice_] = sequence_length * [subject_id]
                dset_tid[slice_] = sequence_length * [trial_id]
                dset_fid[slice_] = np.arange(sequence_length)
                dset_ss[slice_] = sequence_length * [sleep_stage]
                dset_sc[slice_] = sequence_length * [sleep_cycle]
                dset_etime[slice_] = sequence_length * [etime]

                sequence_counter += sequence.shape[0]

    f.close()

    print('Done.\n')
    print(f'Dataset {output_file} created with {n_frames} frames.\n')

def _get_dataset_shape(subjects: List[str],
                       labels_map: Dict[str, Any]) -> Tuple[int, int, int]:
    """ Compute the shape of the dataset, prior to its creation

    Args:
        subjects (List[str]): list of subject numpy archives
        labels_map (Dict[str, Any]): mapping from the trials to the labels

    Returns:
        Tuple[int, int, int]: number of frames, pixels per frame (one dim) and channels
    """

    n_frames, n_gridpoints, n_channels = 0, 0, 0
    for subject_id, subject in tqdm.tqdm(enumerate(subjects), total=len(subjects), ncols=70):

        with np.load(subject) as images:

            trials = [k for k in images.keys() if k.split('.')[0] in labels_map]

            n_frames += sum([images[trial].shape[0] for trial in trials])
            if subject_id == 0:
                _, n_channels, n_gridpoints, _ = next(iter(images.values())).shape

    return n_frames, n_gridpoints, n_channels

if __name__ == '__main__':
    main()


import turtle as t
from snake import Snake
import time
from food import Food
from scoreboard import Scoreboar

screen = t.Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake Game")
screen.tracer(0)

sucuri = Snake()
food = Food()
scoreboard = Scoreboar()

screen.listen()
screen.onkey(sucuri.up, "Up")
screen.onkey(sucuri.down, "Down")
screen.onkey(sucuri.left, "Left")
screen.onkey(sucuri.right, "Right")

game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(0.1)
    sucuri.move()

    #Detect Collision with food
    if sucuri.head.distance(food) < 15:
        food.refresh()
        sucuri.extend()
        scoreboard.increase_score()

    #Detect collision with wall
    if sucuri.head.xcor() > 280 or sucuri.head.xcor() < -280 or sucuri.head.ycor() > 280 or sucuri.head.ycor() < -280:
        game_is_on = False
        scoreboard.game_over()

    #Detect collision with tall
    for segment in sucuri.segments[1:]:
        if sucuri.head.distance(segment) < 10:
            game_is_on = False
            scoreboard.game_over()

screen.exitonclick()


import numpy as np
import pandas as pd
from scipy.special import digamma, gamma
import logging
import json
import os
# from tqdm import trange
import tensorflow as tf

from .kr_plus import *

class HPFtf(object):
    """
    """

    def __init__(self, u_max, g_max, k = 30, a = 0.3, a_prime = 0.3, b_prime = 1.0, c = 0.3, c_prime = 0.3, d_prime = 1.0, log_f = None):
        self.k = k
        self.a = a
        self.a_prime = a_prime
        self.b_prime = b_prime
        self.c = c
        self.c_prime = c_prime
        self.d_prime = d_prime
        self._log_f = log_f
        self.last_llk = 1e-30
        self.total_llk = 0
        self.total_rmse = 0
        self.total = 0
        self.u_max = u_max
        self.g_max = g_max
        self.u_mapper = IdxMap('data/user_mapper.pkl')
        self.g_mapper = IdxMap('data/item_mapper.pkl')
        self._build_graph()
        self.folder = 'ckpt'
        gpu_config = tf.ConfigProto()
        gpu_config.gpu_options.per_process_gpu_memory_fraction = 0.6
        gpu_config.gpu_options.allow_growth = True
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
        self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        # self.sess = tf.Session(config=gpu_config)
        self.sess.run(tf.global_variables_initializer())
        self.sess.run(tf.local_variables_initializer())
        # self.saver = tf.train.Saver([self.Theta, self.Beta])
        try:
            self.load()
        except Exception as e:
            self._log_f(e)

    def _remove_zeros(self, df):
        return df[lambda x: x.ratings > 0]

    def _generate_item_scores(self, df):
        return np.array(df.ratings.tolist(), dtype=np.float32)

    def _initialize_parameters(self):
        n_user = self.u_max
        n_item = self.g_max
        self.input_u = tf.placeholder(tf.int32, [None], name='input_u')
        self.input_g = tf.placeholder(tf.int32, [None], name='input_g')
        self.input_r = tf.placeholder(tf.float32, [None], name='input_r')
        # # n_user * 1
        # ksi = np.random.gamma(self.a_prime, self.b_prime / self.a_prime, size=(n_user, 1))
        # # # nu * k
        # self.Theta = np.random.gamma(self.a, 1 / ksi, size=(n_user, self.k))
        # # # ni * 1
        # eta = np.random.gamma(self.c_prime, self.d_prime / self.c_prime, size=(n_item, 1))
        # # # ni * k
        # self.Beta = np.random.gamma(self.c, 1 / eta, size=(n_item, self.k))
        self.Theta = np.random.gamma(self.a, 1 / self.b_prime, size=(n_user, self.k))
        self.Beta = np.random.gamma(self.c, 1 / self.d_prime, size=(n_item, self.k))
        # 1
        self.k_shp = self.a_prime + self.k * self.a
        # 1
        self.t_shp = self.c_prime + self.k * self.c
        # nu * 1
        self.k_rte = self.a_prime / self.b_prime + self.Theta.sum(axis=1, keepdims=True)
        # ni * 1
        self.t_rte = self.c_prime / self.d_prime + self.Beta.sum(axis=1, keepdims=True)
        # nu * k
        self.Gamma_rte = np.random.gamma(self.a_prime, self.b_prime / self.a_prime, size=(n_user, 1)) + self.Beta.sum(axis=0, keepdims=True)
        # nu * k
        self.Gamma_shp = self.Gamma_rte * self.Theta * np.random.uniform(low=0.85, high=1.15, size=(n_user, self.k))

        self.gamma_shp_tmp_init = np.zeros((n_user, self.k)) + self.a
        # ni * k
        self.Lambda_rte = np.random.gamma(self.c_prime, self.d_prime / self.c_prime, size=(n_item, 1)) + self.Theta.sum(axis=0, keepdims=True)
        # ni * k
        self.Lambda_shp = self.Lambda_rte * self.Beta * np.random.uniform(low=0.85, high=1.15, size=(n_item, self.k))
        self.lambda_shp_tmp_init = np.zeros((n_item, self.k)) + self.c

        # 1
        self.add_k_rte = self.a_prime / self.b_prime
        self.add_t_rte = self.c_prime / self.d_prime
        # 转为tf变量，不然无法assign
        dt = tf.float32
        self.Theta = tf.Variable(self.Theta, dtype=dt)
        self.Beta = tf.Variable(self.Beta, dtype=dt)
        self.k_rte = tf.Variable(self.k_rte, dtype=dt)
        self.t_rte = tf.Variable(self.t_rte, dtype=dt)
        self.Gamma_rte = tf.Variable(self.Gamma_rte, dtype=dt)
        self.Gamma_shp = tf.Variable(self.Gamma_shp, dtype=dt)
        self.Lambda_rte = tf.Variable(self.Lambda_rte, dtype=dt)
        self.Lambda_shp = tf.Variable(self.Lambda_shp, dtype=dt)
        # 用于分批更新psi
        self.gamma_shp_tmp = tf.Variable(self.gamma_shp_tmp_init, dtype=dt)
        self.lambda_shp_tmp = tf.Variable(self.lambda_shp_tmp_init, dtype=dt)

    def _update_phi(self):
        self._log_f("Update phi ...")
        gamma_shp = tf.nn.embedding_lookup(self.Gamma_shp, self.input_u)
        gamma_rte = tf.nn.embedding_lookup(self.Gamma_rte, self.input_u)
        lambda_shp = tf.nn.embedding_lookup(self.Lambda_shp, self.input_g)
        lambda_rte = tf.nn.embedding_lookup(self.Lambda_rte, self.input_g)
        self.phi = tf.exp(tf.digamma(gamma_shp) - tf.log(gamma_rte) + tf.digamma(lambda_shp) - tf.log(lambda_rte))
        sumphi = tf.reduce_sum(self.phi, axis=1, keepdims=True)
        self.phi = self.phi * tf.expand_dims(self.input_r, axis=-1) / sumphi

    # 暂不支持，batch训练，需输入全部数据
    def _build_graph(self):
        self._initialize_parameters()
        self._update_phi()
        self.gamma_shp_tmp2 = tf.scatter_add(self.gamma_shp_tmp, self.input_u, self.phi)
        self.lambda_shp_tmp2 = tf.scatter_add(self.lambda_shp_tmp, self.input_g, self.phi)
        self.batch_val = [self.gamma_shp_tmp2, self.lambda_shp_tmp2]
        self.batch_op = [tf.assign(self.gamma_shp_tmp, self.gamma_shp_tmp2), tf.assign(self.lambda_shp_tmp, self.lambda_shp_tmp2)]
        self.gamma_rte_tmp = tf.divide(tf.ones((self.u_max, self.k)) * self.k_shp, self.k_rte) + tf.reduce_sum(self.Beta, axis=0, keepdims=True)
        self.lambda_rte_tmp = tf.divide(tf.ones((self.g_max, self.k)) * self.t_shp, self.t_rte) + tf.reduce_sum(self.Theta, axis=0, keepdims=True)
        self.theta_tmp = self.gamma_shp_tmp / self.gamma_rte_tmp
        self.beta_tmp = self.lambda_shp_tmp / self.lambda_rte_tmp
        # nu * 1
        self.k_rte_tmp = self.add_k_rte + tf.reduce_sum(self.Theta, axis=1, keepdims=True)
        # ni * 1
        self.t_rte_tmp = self.add_t_rte + tf.reduce_sum(self.Beta, axis=1, keepdims=True)
        self.tmp_val = [self.gamma_shp_tmp, self.gamma_rte_tmp, self.lambda_shp_tmp, self.lambda_rte_tmp, self.theta_tmp, self.beta_tmp, self.k_rte_tmp, self.t_rte_tmp]
        self.assign_op = [tf.assign(self.Gamma_shp, self.gamma_shp_tmp), tf.assign(self.Gamma_rte, self.gamma_rte_tmp), tf.assign(self.Lambda_shp, self.lambda_shp_tmp), tf.assign(self.Lambda_rte, self.lambda_rte_tmp), tf.assign(self.Theta, self.theta_tmp), tf.assign(self.Beta, self.beta_tmp), tf.assign(self.k_rte, self.k_rte_tmp), tf.assign(self.t_rte, self.t_rte_tmp),
            tf.assign(self.gamma_shp_tmp, self.gamma_shp_tmp_init), tf.assign(self.lambda_shp_tmp, self.lambda_shp_tmp_init)]
        self.llk, self.rmse = self._build_llk_rmse()

    def _check_convergence(self, stop_thr, llk):
        return (1 - llk / self.last_llk) <= stop_thr

    # llk 和 rmse 貌似会跳越，所以最短也需要每2个step计算一次
    def _build_llk_rmse(self):
        self._log_f('Calculate log-likelihood and RMSE ...')
        theta = tf.nn.embedding_lookup(self.Theta, self.input_u)
        beta = tf.nn.embedding_lookup(self.Beta, self.input_g)
        y = tf.reduce_sum(theta * beta, axis=1)
        self.y = y
        r = self.input_r
        rmse = tf.sqrt(tf.reduce_mean(tf.square(r - y)))
        user_set, _ = tf.unique(self.input_u)
        uset_emb = tf.reduce_sum(tf.nn.embedding_lookup(self.Theta, user_set), axis=0)
        item_set, _ = tf.unique(self.input_g)
        gset_emb = tf.reduce_sum(tf.nn.embedding_lookup(self.Beta, item_set), axis=0)
        # lgamma=log(gamma())
        llk = tf.reduce_sum(r * tf.log(y) - tf.lgamma(r + 1)) - tf.reduce_sum(uset_emb * gset_emb)
        # llk = llk / n
        return llk, rmse

    def _check_rmse(self, rmse, stop = 1.2):
        if rmse < stop:
            return True
        else:
            return False

    def _train(self, u, g, r, max_iter, check_every = 10, verbose = True, stop_thr = 1e-3, val_df = None):
        last_rmse = 10
        last_llk = -1e20
        # fd = {self.input_u:df.user, self.input_g:df.item, self.input_r:df.ratings}
        fd = {self.input_u: u, self.input_g: g, self.input_r: r}
        for i in range(max_iter):
            self._log_f("Iteration {}".format(i))
            self.sess.run(self.batch_op, feed_dict=fd)
            # self.sess.run(self.tmp_val, feed_dict=fd)
            self.sess.run(self.assign_op, feed_dict=fd)
            if i % check_every == 0:
                llk, rmse = self.sess.run([self.llk, self.rmse], feed_dict=fd)
                if verbose:
                    if val_df is None:
                        self._log_f("Iteration {}, train-log-likelihood : {}, train-RMSE : {}".format(i, llk, rmse))
                    else:
                        val_llk, val_rmse = self.evaluate(val_df)
                        self._log_f("Interation {}, train-log-likelihood : {}, train-RMSE : {}, val-log-likelihood : {}, val-RMSE : {}".format(i, llk, rmse, val_llk, val_rmse))
                # has_converged = self._check_convergence(stop_thr, llk)
                # self.last_llk = llk
                if rmse <= last_rmse:
                    last_rmse = rmse
                else:
                    break
                if llk >= last_llk:
                    last_llk = llk
                else:
                    break
                    # has_converged = self._check_rmse(rmse)
                    # if has_converged:
                    #     break
        self._log_f("Optimization finished.")
        return last_rmse, last_llk

    def train(self, df, max_iter = 100, check_every = 10, verbose = True, stop_thr = 1e-3, val_df = None):
        df.columns = ['user', 'item', 'ratings']
        df = self._remove_zeros(df)
        if val_df is not None:
            val_df.columns = ['user', 'item', 'ratings']
            val_df = self._remove_zeros(val_df)
        u = self.u_mapper.fit_transform(df.user)
        g = self.g_mapper.fit_transform(df.item)
        r = self._generate_item_scores(df)
        start_time = now()
        self._log_f("Starting training ...", std_datetime(start_time))
        rmse, llk = self._train(u, g, r, max_iter, check_every=check_every, verbose=verbose, stop_thr=stop_thr, val_df=val_df)
        elapse = now() - start_time
        self._log_f('train finish ...', elapse)
        self.save(llk)

    def train_all(self, df, max_iter = 100, llk_step = 2):
        """因数据太大无法一次载入，所以需要手动控制训练过程
        循环最后一轮才更新全部变量，前面只更新psi和两个shp
        全部更新过一次变量之后才开始计算llk
        """
        df.columns = ['user', 'item', 'ratings']
        ul = self.u_mapper.fit_transform(df.user)
        gl = self.g_mapper.fit_transform(df.item)
        # rl = self._generate_item_scores(df)
        rl = np.array(df.ratings.tolist(), dtype=np.float32)
        mxln = len(df)
        step = 900 * 10000
        last_llk = -1.1e30
        finish = False
        for i in range(max_iter):
            start = 0
            self._log_f('step: ', i)
            while start < mxln:
                end = start + step
                u, g, r = ul[start:end], gl[start:end], rl[start:end]
                fd = {self.input_u: u, self.input_g: g, self.input_r: r}
                self._log_f(start, end, len(u))
                start = end
                if end < mxln:
                    self.sess.run(self.batch_op, feed_dict=fd)
                else:
                    self.sess.run(self.assign_op, feed_dict=fd)

            if i % llk_step == 0:
                start = 0
                while start < mxln:
                    end = start + step
                    self._log_f('eval', start, end)
                    u, g, r = ul[start:end], gl[start:end], rl[start:end]
                    start = end
                    if end < mxln:
                        llk, rmse = self.evaluate(u,g,r)
                        self._log_f(llk, rmse)
                        continue
                    llk, rmse = self.evaluate(u,g,r, total=True)
                    self._log_f("Iteration {}, train-log-likelihood : {}, train-RMSE : {}".format(i, llk, rmse))
                    if llk > last_llk and llk < 0:
                        last_llk = llk
                        self.save()
                    else:
                        if i > 10:
                            finish = True
            if finish:
                self._log_f('train finished')
                break

    def train_part(self, u, g, r, update_all = False):
        fd = {self.input_u: u, self.input_g: g, self.input_r: r}
        # self.sess.run(self.pre_val, feed_dict=fd)
        self.sess.run(self.batch_op, feed_dict=fd)
        if update_all:
            # self.sess.run(self.tmp_val, feed_dict=fd)
            self.sess.run(self.assign_op, feed_dict=fd)

    def predict(self, user, item):
        u = self.u_mapper.fit_transform(user)
        g = self.g_mapper.fit_transform(item)
        y = self.sess.run(self.y, feed_dict={self.input_u: u, self.input_g: g})
        return y

    def evaluate(self, u, g, r, total = False):
        fd = {self.input_u: u, self.input_g: g, self.input_r: r}
        llk, rmse = self.sess.run([self.llk, self.rmse], feed_dict=fd)
        # llk /= len(u)
        self.total_llk += llk
        self.total_rmse += rmse*len(u)
        self.total += len(u)
        if total and self.total > 0:
            llk = self.total_llk / self.total
            rmse = self.total_rmse / self.total
            self.total_llk = 0
            self.total_rmse = 0
            self.total = 0
        return llk/len(u), rmse

    def topN(self, user, item, n = 10):
        gids = item.copy()
        ul = np.zeros((len(gids)), dtype=np.int32)+user
        y = self.predict(ul, item)
        orders = np.argsort(y)[::-1][:n]
        return gids[orders]

    def save(self, tag = None):
        # tf的保存有大小限制，貌似是2G，所以改为手动保存
        folder = self.folder
        if tag is None:
            tag = 'none'
        str_dump(folder + '/index', tag)
        theta, beta = self.sess.run([self.Theta, self.Beta])
        np.save(folder + '/theta.npy', theta)
        np.save(folder + '/beta.npy', beta)
        # self.saver.save(self.sess, folder+'/{}.ckpt'.format(tag))

    def load(self, tag = None):
        folder = self.folder
        if tag is None:
            tag = str_load(folder + '/index')
        f = folder + '/{}.ckpt'.format(tag)
        theta = np.load(folder + '/theta.npy')
        beta = np.load(folder + '/beta.npy')
        op = [tf.assign(self.Theta, theta), tf.assign(self.Beta, beta)]
        self.sess.run(op)
        # self.saver.restore(sess=self.sess, save_path=f)

    def save4go(self, path):
        if os.path.exists(path):  # 目录存在则删除
            shutil.rmtree(path)
        builder = tf.saved_model.builder.SavedModelBuilder(path)
        # Tag the model, required for Go
        builder.add_meta_graph_and_variables(sess, ["myTag"])
        builder.save()


from typing import List, Optional, Tuple, Any

def not_implemented(*args, **kwargs):
    raise NotImplementedError

LOOKUPS = {
    'exact': not_implemented,
    'iexact': not_implemented,
    'contains': not_implemented,
    'icontains': not_implemented,
    'in': not_implemented,
    'gt': not_implemented,
    'gte': not_implemented,
    'lt': not_implemented,
    'lte': not_implemented,
    'startswith': not_implemented,
    'istartswith': not_implemented,
    'endswith': not_implemented,
    'iendswith': not_implemented,
    'range': not_implemented,
    'date': not_implemented,
    'year': not_implemented,
    'iso_year': not_implemented,
    'month': not_implemented,
    'day': not_implemented,
    'week': not_implemented,
    'week_day': not_implemented,
    'iso_week_day': not_implemented,
    'quarter': not_implemented,
    'time': not_implemented,
    'hour': not_implemented,
    'minute': not_implemented,
    'second': not_implemented,
    'isnull': not_implemented,
    'regex': not_implemented,
    'iregex': not_implemented,
}

class Condition:
    key: str
    operator: str
    value: str

    def __init__(
        self,
        key: str,
        value: str,
    ):
        self.key, self.operator, self.value = self._split_by_operator(
            key=key,
            value=value,
        )

    def __str__(self):
        return f'{self.key} {self.operator} ${self.value}'

    def _split_by_operator(
        self,
        key: str,
        value: str
    ) -> Tuple[str, str, str]:
        operator = '='
        keys_split = key.split('__')
        if len(keys_split) > 1:
            operator_function = LOOKUPS.get(keys_split[-1])
            if operator_function is not None:
                key, operator, value = operator_function(key, value)
        return key, operator, value

class Where:
    conditions_strings: List[str]

    def __init__(self):
        self.conditions_strings = []

    # def add(
    #     self,
    #     where_dict: dict,
    #     not_: bool = False,
    # ):
    #     self.conditions_strings.append(
    #         'NOT ' if not_ else ''
    #         + '('
    #         + 'AND, '.join([
    #             str(Condition(key=key, value=value))
    #             for key, value in where_dict.items()
    #         ])
    #         + ')'
    #     )

    def add(
        self,
        conditional_string: str
    ):
        self.conditions_strings.append(conditional_string)

    def __str__(self):
        if self.conditions_strings:
            where = 'WHERE '
            where += 'AND '.join([condition for condition in self.conditions_strings])
        else:
            where = ''
        return where

class SQL:
    table_name: str
    where: Optional[Where]

    def __init__(self, table_name: str):
        self.table_name = table_name
        self.values = ()
        self.where = Where()

    # def _extract_values(self, values_dict: dict) -> dict:
    #     new_values = tuple(values_dict.values())
    #     placeholder_values = list(range(len(self.values) + 1, len(new_values) + 1))
    #     self.values += new_values
    #     return {
    #         key: placeholder_values.pop(0)
    #         for key in values_dict
    #     }

    def _swap_value_with_placeholder(self, value: Any) -> str:
        self.values += (value,)
        return f'${len(self.values)}'

    # def add_where(
    #     self,
    #     where_dict: dict,
    #     not_: bool = False,
    # ):
    #     extracted_where_dict = self._extract_values(values_dict=where_dict)
    #     self.where.add(where_dict=extracted_where_dict, not_=not_)

    def add_where(
        self,
        field_name: str,
        symbol: str,
        field_value: Any,
    ):
        placeholder_value = self._swap_value_with_placeholder(
            value=field_value,
        )
        self.where.add(f'{field_name} {symbol} {placeholder_value}')

    def build_select(self) -> Tuple[str, Tuple]:
        return (
            f'SELECT * FROM {self.table_name} {self.where}',
            self.values,
        )

    def build_update(self, fields_dict: dict) -> Tuple[str, Tuple]:
        set_values = ', '.join(
            f'{key} = {self._swap_value_with_placeholder(value)}'
            for key, value in fields_dict.items()
        )
        return (
            f'UPDATE {self.table_name} '
            f'SET {set_values} {self.where} RETURNING *',
            self.values,
        )

    def build_delete(self) -> Tuple[str, Tuple]:
        return (
            f'DELETE FROM {self.table_name} {self.where} RETURNING *',
            self.values,
        )

    def build_count(self) -> Tuple[str, Tuple]:
        return (
            f'SELECT COUNT(*) FROM {self.table_name} {self.where}',
            self.values,
        )

    def build_insert(self, fields_dict: dict) -> Tuple[str, Tuple]:
        if not fields_dict:
            columns, values = 'DEFAULT', ''
        else:
            column_names = ', '.join(fields_dict.keys())
            columns = f'({column_names})'
            placeholders = ', '.join(
                f'{self._swap_value_with_placeholder(value)}'
                for value in fields_dict.values()
            )
            values = f'({placeholders})'
        return (
            f'INSERT INTO {self.table_name} {columns}'
            f' VALUES{values} RETURNING *',
            self.values,
        )


import os
import itertools

# get all the filenames in the results folder
HOME_PATH = os.getenv('HOME')
dir_names = next(os.walk(f"{HOME_PATH}/ray_results"))[1]

# sort all filenames in alphabetical order

sorted_dirs = sorted(dir_names)

# create another list containing the new names of each directory. Note that the list should be the same length.

SEEDS =  [1,10,100]
ENVS = ['ns3-v0']
WORKERS = [2,4,8,16,32,63]

new_names = []
for params in itertools.product(ENVS, WORKERS, SEEDS):
    new_names.append(f"{params[0]}_{params[1]}_{params[2]}")

nsdirs = [x for x in sorted_dirs if 'ns3' in x]
nsnewnames = [x for x in new_names if 'ns3' in x]
# execute a mv command for each folder in the list

if len(nsnewnames) == len(nsdirs):
    for i in range(len(nsdirs)):
        os.system(f"mv {HOME_PATH}/ray_results/{nsdirs[i]} {HOME_PATH}/ray_results/{nsnewnames[i]}")


#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import argparse
import asyncio
import configparser
import json
import logging
import os
import shutil
import tempfile
import time
from contextlib import AsyncExitStack
from distutils.util import strtobool
from pathlib import Path

import aiohttp
from mardor.reader import MarReader
from mardor.signing import get_keysize
from scriptworker.utils import get_hash, retry_async

log = logging.getLogger(__name__)

ROOT_URL = os.environ.get(
    "TASKCLUSTER_ROOT_URL", "https://firefox-ci-tc.services.mozilla.com"
)
QUEUE_PREFIX = f"{ROOT_URL}/api/queue/"
ALLOWED_URL_PREFIXES = (
    "http://download.cdn.mozilla.net/pub/mozilla.org/firefox/nightly/",
    "http://download.cdn.mozilla.net/pub/firefox/nightly/",
    "http://ftp.mozilla.org/",
    "http://download.mozilla.org/",
    "https://archive.mozilla.org/",
    "http://archive.mozilla.org/",
    QUEUE_PREFIX,
)
STAGING_URL_PREFIXES = (
    "http://ftp.stage.mozaws.net/",
    "https://ftp.stage.mozaws.net/",
)

BCJ_OPTIONS = {
    "x86": ["--x86"],
    "x86_64": ["--x86"],
    "aarch64": [],
    # macOS Universal Builds
    "macos-x86_64-aarch64": [],
}

def verify_signature(mar, cert):
    log.info("Checking %s signature", mar)
    with open(mar, "rb") as mar_fh:
        m = MarReader(mar_fh)
        if not m.verify(verify_key=cert):
            raise ValueError(
                "MAR Signature invalid: %s (%s) against %s", mar, m.signature_type, cert
            )

def process_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument("--artifacts-dir", required=True)
    parser.add_argument("--signing-cert", type=argparse.FileType("rb"), required=True)
    parser.add_argument("--task-definition", required=True, type=argparse.FileType("r"))
    parser.add_argument(
        "--allow-staging-prefixes",
        action="store_true",
        default=strtobool(os.environ.get("FUNSIZE_ALLOW_STAGING_PREFIXES", "false")),
        help="Allow files from staging buckets.",
    )
    parser.add_argument(
        "-q",
        "--quiet",
        dest="log_level",
        action="store_const",
        const=logging.INFO,
        default=logging.DEBUG,
    )
    parser.add_argument(
        "--arch",
        type=str,
        required=True,
        choices=BCJ_OPTIONS.keys(),
        help="The archtecture you are building.",
    )
    return parser.parse_args()

def validate_mar_channel_id(mar, channel_ids):
    log.info("Checking %s for MAR_CHANNEL_ID %s", mar, channel_ids)
    # We may get a string with a list representation, or a single entry string.
    channel_ids = set(channel_ids.split(","))

    product_info = MarReader(open(mar, "rb")).productinfo
    if not isinstance(product_info, tuple):
        raise ValueError(
            "Malformed product information in mar: {}".format(product_info)
        )

    found_channel_ids = set(product_info[1].split(","))

    if not found_channel_ids.issubset(channel_ids):
        raise ValueError(
            "MAR_CHANNEL_ID mismatch, {} not in {}".format(product_info[1], channel_ids)
        )

    log.info("%s channel %s in %s", mar, product_info[1], channel_ids)

async def retry_download(*args, semaphore=None, **kwargs):  # noqa: E999
    """Retry download() calls."""
    async with AsyncExitStack() as stack:
        if semaphore:
            await stack.enter_async_context(semaphore)
        await retry_async(
            download,
            retry_exceptions=(aiohttp.ClientError, asyncio.TimeoutError),
            args=args,
            kwargs=kwargs,
        )

def verify_allowed_url(mar, allowed_url_prefixes):
    if not any(mar.startswith(prefix) for prefix in allowed_url_prefixes):
        raise ValueError(
            "{mar} is not in allowed URL prefixes: {p}".format(
                mar=mar, p=allowed_url_prefixes
            )
        )

async def download(url, dest, mode=None):  # noqa: E999
    log.info("Downloading %s to %s", url, dest)
    chunk_size = 4096
    bytes_downloaded = 0
    async with aiohttp.ClientSession(raise_for_status=True) as session:
        start = time.time()
        async with session.get(url, timeout=120) as resp:
            # Additional early logging for download timeouts.
            log.debug("Fetching from url %s", resp.url)
            for history in resp.history:
                log.debug("Redirection history: %s", history.url)
            log.debug("Headers for %s: %s", resp.url, resp.headers)
            if "Content-Length" in resp.headers:
                log.debug(
                    "Content-Length expected for %s: %s",
                    url,
                    resp.headers["Content-Length"],
                )
            log_interval = chunk_size * 1024
            with open(dest, "wb") as fd:
                while True:
                    chunk = await resp.content.read(chunk_size)
                    if not chunk:
                        break
                    fd.write(chunk)
                    bytes_downloaded += len(chunk)
                    log_interval -= len(chunk)
                    if log_interval <= 0:
                        log.debug("Bytes downloaded for %s: %d", url, bytes_downloaded)
                        log_interval = chunk_size * 1024
            end = time.time()
            log.info(
                "Downloaded %s, %s bytes in %s seconds: sha256:%s",
                url,
                bytes_downloaded,
                int(end - start),
                get_hash(dest, hash_alg="sha256"),
            )
            if mode:
                log.info("chmod %o %s", mode, dest)
                os.chmod(dest, mode)

async def download_buildsystem_bits(partials_config, downloads, tools_dir):
    """Download external tools needed to make partials."""

    # We're making the assumption that the "to" mar is the same for all,
    # as that's the way this task is currently used.
    to_url = extract_download_urls(partials_config, mar_type="to").pop()

    repo = get_option(
        downloads[to_url]["extracted_path"],
        filename="platform.ini",
        section="Build",
        option="SourceRepository",
    )
    revision = get_option(
        downloads[to_url]["extracted_path"],
        filename="platform.ini",
        section="Build",
        option="SourceStamp",
    )

    urls = {
        "make_incremental_update.sh": f"{repo}/raw-file/{revision}/tools/"
        "update-packaging/make_incremental_update.sh",
        "common.sh": f"{repo}/raw-file/{revision}/tools/update-packaging/common.sh",
        "mar": "https://archive.mozilla.org/pub/mozilla.org/firefox/nightly/"
        "latest-mozilla-central/mar-tools/linux64/mar",
        "mbsdiff": "https://archive.mozilla.org/pub/mozilla.org/firefox/nightly/"
        "latest-mozilla-central/mar-tools/linux64/mbsdiff",
    }
    for filename, url in urls.items():
        filename = tools_dir / filename
        await retry_download(url, dest=filename, mode=0o755)

def find_file(directory, filename):
    log.debug("Searching for %s in %s", filename, directory)
    return next(Path(directory).rglob(filename))

def get_option(directory, filename, section, option):
    log.info("Extracting [%s]: %s from %s/**/%s", section, option, directory, filename)
    f = find_file(directory, filename)
    config = configparser.ConfigParser()
    config.read(f)
    rv = config.get(section, option)
    log.info("Found %s", rv)
    return rv

def extract_download_urls(partials_config, mar_type):
    """Extract a set of urls to download from the task configuration.

    mar_type should be one of "from", "to"
    """
    return {definition[f"{mar_type}_mar"] for definition in partials_config}

async def download_and_verify_mars(partials_config, allowed_url_prefixes, signing_cert):
    """Download, check signature, channel ID and unpack MAR files."""
    # Separate these categories so we can opt to perform checks on only 'to' downloads.
    from_urls = extract_download_urls(partials_config, mar_type="from")
    to_urls = extract_download_urls(partials_config, mar_type="to")
    tasks = list()
    downloads = dict()

    semaphore = asyncio.Semaphore(2)  # Magic 2 to reduce network timeout errors.
    for url in from_urls.union(to_urls):
        verify_allowed_url(url, allowed_url_prefixes)
        downloads[url] = {
            "download_path": Path(tempfile.mkdtemp()) / Path(url).name,
        }
        tasks.append(
            retry_download(url, downloads[url]["download_path"], semaphore=semaphore)
        )

    await asyncio.gather(*tasks)

    for url in downloads:
        # Verify signature, but not from an artifact as we don't
        # depend on the signing task
        if not os.getenv("MOZ_DISABLE_MAR_CERT_VERIFICATION") and not url.startswith(
            QUEUE_PREFIX
        ):
            verify_signature(downloads[url]["download_path"], signing_cert)

        # Only validate the target channel ID, as we update from beta->release
        if url in to_urls:
            validate_mar_channel_id(
                downloads[url]["download_path"], os.environ["MAR_CHANNEL_ID"]
            )

        downloads[url]["extracted_path"] = tempfile.mkdtemp()
        with open(downloads[url]["download_path"], "rb") as mar_fh:
            log.info(
                "Unpacking %s into %s",
                downloads[url]["download_path"],
                downloads[url]["extracted_path"],
            )
            m = MarReader(mar_fh)
            m.extract(downloads[url]["extracted_path"])

    return downloads

async def run_command(cmd, cwd="/", env=None, label=None, silent=False):
    log.info("Running: %s", cmd)
    if not env:
        env = dict()
    process = await asyncio.create_subprocess_shell(
        cmd,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
        cwd=cwd,
        env=env,
    )
    if label:
        label = "{}: ".format(label)
    else:
        label = ""

    async def read_output(stream, label, printcmd):
        while True:
            line = await stream.readline()
            if line == b"":
                break
            printcmd("%s%s", label, line.decode("utf-8").rstrip())

    if silent:
        await process.wait()
    else:
        await asyncio.gather(
            read_output(process.stdout, label, log.info),
            read_output(process.stderr, label, log.warning),
        )
        await process.wait()

async def generate_partial(from_dir, to_dir, dest_mar, mar_data, tools_dir, arch):
    log.info("Generating partial %s", dest_mar)
    env = os.environ.copy()
    env["LC_ALL"] = "C"
    env["MAR"] = tools_dir / "mar"
    env["MBSDIFF"] = tools_dir / "mbsdiff"
    if arch:
        env["BCJ_OPTIONS"] = " ".join(BCJ_OPTIONS[arch])
    env["MOZ_PRODUCT_VERSION"] = mar_data["version"]
    env["MAR_CHANNEL_ID"] = mar_data["MAR_CHANNEL_ID"]
    env["BRANCH"] = mar_data["branch"]

    make_incremental_update = tools_dir / "make_incremental_update.sh"
    cmd = f"{make_incremental_update} {dest_mar} {from_dir} {to_dir}"

    await run_command(cmd, cwd=dest_mar.parent, env=env, label=dest_mar.name)
    validate_mar_channel_id(dest_mar, mar_data["MAR_CHANNEL_ID"])

async def manage_partial(
    partial_def, artifacts_dir, tools_dir, downloads, semaphore, arch=None
):
    from_url = partial_def["from_mar"]
    to_url = partial_def["to_mar"]
    from_path = downloads[from_url]["extracted_path"]
    to_path = downloads[to_url]["extracted_path"]

    mar_data = {
        "MAR_CHANNEL_ID": os.environ["MAR_CHANNEL_ID"],
        "version": get_option(
            to_path, filename="application.ini", section="App", option="Version"
        ),
        "appName": get_option(
            from_path, filename="application.ini", section="App", option="Name"
        ),
        # Use Gecko repo and rev from platform.ini, not application.ini
        "repo": get_option(
            to_path, filename="platform.ini", section="Build", option="SourceRepository"
        ),
        "revision": get_option(
            to_path, filename="platform.ini", section="Build", option="SourceStamp"
        ),
        "locale": partial_def["locale"],
        "from_mar": partial_def["from_mar"],
        "from_size": os.path.getsize(downloads[from_url]["download_path"]),
        "from_hash": get_hash(downloads[from_url]["download_path"], hash_alg="sha512"),
        "from_buildid": get_option(
            from_path, filename="application.ini", section="App", option="BuildID"
        ),
        "to_mar": partial_def["to_mar"],
        "to_size": os.path.getsize(downloads[to_url]["download_path"]),
        "to_hash": get_hash(downloads[to_url]["download_path"], hash_alg="sha512"),
        "to_buildid": get_option(
            to_path, filename="application.ini", section="App", option="BuildID"
        ),
        "mar": partial_def["dest_mar"],
    }
    # if branch not set explicitly use repo-name
    mar_data["branch"] = partial_def.get("branch", Path(mar_data["repo"]).name)

    for field in (
        "update_number",
        "previousVersion",
        "previousBuildNumber",
        "toVersion",
        "toBuildNumber",
    ):
        if field in partial_def:
            mar_data[field] = partial_def[field]

    dest_mar = Path(artifacts_dir) / mar_data["mar"]

    async with semaphore:
        await generate_partial(from_path, to_path, dest_mar, mar_data, tools_dir, arch)

    mar_data["size"] = os.path.getsize(dest_mar)
    mar_data["hash"] = get_hash(dest_mar, hash_alg="sha512")
    return mar_data

async def async_main(args, signing_cert):
    tasks = []

    allowed_url_prefixes = list(ALLOWED_URL_PREFIXES)
    if args.allow_staging_prefixes:
        allowed_url_prefixes += STAGING_URL_PREFIXES

    task = json.load(args.task_definition)

    downloads = await download_and_verify_mars(
        task["extra"]["funsize"]["partials"], allowed_url_prefixes, signing_cert
    )

    tools_dir = Path(tempfile.mkdtemp())
    await download_buildsystem_bits(
        partials_config=task["extra"]["funsize"]["partials"],
        downloads=downloads,
        tools_dir=tools_dir,
    )

    # May want to consider os.cpu_count() if we ever run on osx/win.
    # sched_getaffinity is the list of cores we can run on, not the total.
    semaphore = asyncio.Semaphore(len(os.sched_getaffinity(0)))
    for definition in task["extra"]["funsize"]["partials"]:
        tasks.append(
            asyncio.ensure_future(
                retry_async(
                    manage_partial,
                    retry_exceptions=(aiohttp.ClientError, asyncio.TimeoutError),
                    kwargs=dict(
                        partial_def=definition,
                        artifacts_dir=args.artifacts_dir,
                        tools_dir=tools_dir,
                        arch=args.arch,
                        downloads=downloads,
                        semaphore=semaphore,
                    ),
                )
            )
        )
    manifest = await asyncio.gather(*tasks)

    for url in downloads:
        downloads[url]["download_path"].unlink()
        shutil.rmtree(downloads[url]["extracted_path"])
    shutil.rmtree(tools_dir)

    return manifest

def main():
    args = process_arguments()

    logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s")
    log.setLevel(args.log_level)

    signing_cert = args.signing_cert.read()
    assert get_keysize(signing_cert) == 4096

    artifacts_dir = Path(args.artifacts_dir)
    if not artifacts_dir.exists():
        artifacts_dir.mkdir()

    loop = asyncio.get_event_loop()
    manifest = loop.run_until_complete(async_main(args, signing_cert))
    loop.close()

    manifest_file = artifacts_dir / "manifest.json"
    with open(manifest_file, "w") as fp:
        json.dump(manifest, fp, indent=2, sort_keys=True)

    log.debug("{}".format(json.dumps(manifest, indent=2, sort_keys=True)))

if __name__ == "__main__":
    main()


import io
import os

input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

H, *commands = input().decode().split()
H = int(H)
size = 1 << (H + 1)

def drop() -> int:
    idx = 1
    if commands:
        for command in commands[0]:
            if command == "L":
                idx <<= 1
            else:
                idx <<= 1
                idx += 1
    return size - idx

if __name__ == "__main__":
    print(drop())


"""
Вспомогательные функции.
"""

# стандартная библиотека
from configparser import ConfigParser
from shutil import get_terminal_size

# проект
import data

def read_players() -> bool:
    """Возвращает True, если в файле игроков есть хотя бы одина запись, иначе Folse. """
    config = ConfigParser()
    config.read(data.PLAYERS_PATH, encoding="utf-8")
    config = {
        player_name: {
            key: int(value)
            for key, value in config[player_name].items()
        }
        for player_name in config.sections()
    }
    data.players_db = config
    return bool(config)

def write_players() -> None:
    """
    Перезаписывает файл players.ini.
    :return:Nane
    """
    config = ConfigParser()
    for elem in data.players_db:
        config[elem] = {key: value for key, value in data.players_db[elem].items()}

    with open(data.PLAYERS_PATH, "w", encoding="utf-8") as configfile:
        config.write(configfile)

def read_save() -> None:
    """
    Записывает в data.saves_db данные полученные из файла saves.txt.
    """
    saves = data.SAVES_PATH.read_text(encoding='utf-8').split('\n')
    if ''.join(saves):
        for save in saves:
            players, turns, dim = save.split('!')
            data.saves_db |= {
                tuple(players.split(',')): {
                    'dim': int(dim),
                    'turns': {
                        int(turn): data.TOKENS[i % 2]
                        for i, turn in enumerate(turns.split(','))
                        # условие при пустом turns
                    } if turns else {},
                }
            }

def write_saves() -> None:
    """
     Записывает незавершенные партии в файл saves.txt
    """
    texts = ''
    for key in data.saves_db:
        text = '!'.join([','.join(str(i) for i in key), ','.join(str(elem) for elem in data.saves_db[key]['turns']),
                         str(data.saves_db[key]['dim'])]) + '\n'
        texts += text

    with open(data.SAVES_PATH, "w", encoding="utf-8") as file:
        file.write(texts.rstrip('\n'))

def change_dim(new_dim: int) -> None:
    """
    Принимает int объект и перезаписывает переменные data.dim, data.dim_range, data.all_cell = new_dim**2.
    """
    data.dim = new_dim
    data.dim_range = range(new_dim)
    data.all_cells = new_dim**2
    # выигрышные комбинации
    data.winning_combinations = counts_combinations(new_dim)
    # словарь координат игрового поля c пробелами
    data.board = dict.fromkeys(range(1, data.all_cells + 1), ' ')
    # шаблон игрового поля
    data.field = field_template()

def dim_input() -> int:
    """
    Запрашивает число и проверяет ввод на соответствие шаблону, передает число как объект int.
    """
    while True:
        dim = input(f" {data.MESSAGES['ввод размерности']}{data.PROMPT}")
        if data.DIM_PATTERN.fullmatch(dim):
            return int(dim)
        print(f" {data.MESSAGES['некорректная размерность']} ")

def counts_combinations(dim: int) -> tuple[set[int]]:
    """Генерирует  и возвращает кортеж , который содержит множества выигрышных комбинаций.

    :param dim -- принимает размер игрового поля (int)
    """
    list_com = []
    list_ref = list(range(1, dim*dim + 1))

    step_row = 0
    step_col = 0

    # КОММЕНТАРИЙ: в один цикл — одобряю!
    for _ in range(dim):
        set_row = set(list_ref[step_row:dim+step_row])
        list_com.append(set_row)
        set_col = set(list_ref[step_col::dim])
        list_com.append(set_col)
        step_row += dim
        step_col += 1

    list_com.append(set(list_ref[dim-1::dim-1][:-1]))
    list_com.append(set(list_ref[::dim+1]))

    return tuple(list_com)

def field_template(data_width: int = None) -> str:
    """
    Генерирует и возвращает строку шаблона игрового поля.
    :param data_width: указывается максимальная длина элемента. Без аргумента возвращает шаблон для игрового полля, при
    указании аргумента выводит шаблон для координат игрового поля.
    :return: str
    """
    if data_width is None:
        field_width = data.dim * (3 + max(len(t) for t in data.TOKENS)) - 1
    else:
        field_width = data.dim * (3 + data_width) - 1
    v_sep, h_sep = '|', '—'
    v_sep = v_sep.join([' {} '] * data.dim)
    h_sep = f'\n{h_sep * field_width}\n'
    return h_sep.join([v_sep] * data.dim)

def save_game() -> None:
    """Сохраняет текущую партию в data.saves_db"""
    data.saves_db[tuple(data.players)] = dict(zip(('dim', 'turns'), (data.dim, data.turns)))

def concatenate_rows(
        matrix1: str,
        matrix2: str,
        *matrices: str,
        padding: int = 0
) -> str:
    """
    Принимает строки. Складывает каждую строчку до символа конца строки каждой строки через определенное количество
    пробелов.
    padding: принимает количество пробелов.
    """
    matrices = matrix1, matrix2, *matrices
    matrices = [m.split('\n') for m in matrices]
    padding = ' ' * padding
    return '\n'.join(
        padding.join(row)
        for row in zip(*matrices)
    )

def table_result() -> None:
    """Выводит в stdout таблицу статистики игроков"""
    list_columns = ['Игрок', 'Побед', 'Поражений', 'Ничьих']
    list_elem = []
    for key in data.players_db.values():
        list_elem.append(key)
        for elem in key:
            list_elem.append(elem)
    width = max(len(i) for i in list_elem)

    columns = ''.join(f"{column:<{width +5}}" for column in list_columns)
    separator = f"{'=' * (width+5) * 4}"

    list_str = []
    for key, val in data.players_db.items():
        list_line = ''
        for elem in val.values():
            list_line += f"{elem:<{width +5}}"
        list_str += [f"{key:<{width +5}}" + list_line + '\n']

    print(f'\n{columns}\n'f'{separator}\n'f"{''.join(list_str)}")

def important_message(list_text: list[str], align: str = '<') -> str:
    """
    Возвращает сгенерированную строку полученную из атрибута list_text
    """
    col = get_terminal_size()[0] - 1
    str_outer = f"#{'=' * (col - 2)}#"
    str_space = f"#{' ' * (col - 2)}#"
    content = []
    for line in list_text:
        if line != '\n':
            # если строка не вмещается по ширине в окно терминала, строка переносится с табуляцией(если исходная строка
            # имела табуляцию
            space = line.count('    ')
            line = line.rstrip('\n')
            line = line.replace('    ', '')
            while len(line) > 0:
                line = '    '*space + line
                content.append(f"#{line[:col - 6]:{align}{(col - 2)}}#")
                line = line[(col - 6):]
        else:
            content.append(str_space)

    return '\n'.join([str_outer, str_space, '\n'.join(content), str_space, str_outer])


import numpy as np
#import numdifftools as nd
from math import sqrt
from scipy.optimize import minimize_scalar

notations = True
dim = 3
#gr = (sqrt(5) - 1)/2

def BrentComb(a, c, func, eps):

	"""
	Комбинированный метод Брента минимизации ф-ии одной переменной
	 (применяется для фунцкий f_k(alpha) = J(u_k - alpha*p_k)
	"""
	K = (3 - sqrt(5)) / 2
	x = (a + c)/2
	w = (a + c)/2
	v = (a + c)/2
	u = 2*c
	f_x = func(x)
	f_w = func(w)
	f_v = func(v)
	while abs(u - x) >= eps:
		if ((x != w and w != v and x != v) and (f_x != f_w and f_w != f_v and f_x != f_v)):
			u = x - (((x - v)**2)*(f_x - f_w) - ((x - w)**2)*(f_x - f_v))/(2*((x - v)*(f_x - f_w) - (x - w)*(f_x - f_v)))
		if ((u >= a + eps) and (u <= c - eps) and abs(u - x) < g/2):
			d = abs(u - x)
		else:
			if x < (c - a)/2:
				u = x + K*(c - x) # золотое сечение на [x, c]
				d = c - x
			else:
				u = x - K*(x - a) # золотое сечение на [a, x]
				d = x - a
		if abs(u - x) < eps:
			u = x + eps*np.sign(u - x)
		f_u = func(u)
		if f_u <= f_x:
			if u >= x:
				a = x
			else:
				c = x
			v = w
			w = x
			x = u
			f_v = f_w
			f_w = f_x
			f_x = f_u
		else:
			if u >= x:
				c = u
			else:
				a = u
			if ((f_u <= f_w) or (w == x)):
				v = w
				w = u
				f_v = f_w
				f_w = f_u
			elif ((f_u <= f_v) or (v == x) or (v == w)):
				v = u
				f_v = f_u
	return u, f_u

def GSS(a, b, f, eps):
	"""
	Метод золотого сечения для минимизации функции одной переменной
	(применяется для фунцкий f_k(alpha) = J(u_k - alpha*p_k)

	"""

	c = b - (b - a) / gr
	d = a + (b - a) / gr

	while abs(c - d) > eps:
		if f(c) < f(d):
			b = d
		else:
			a = c

		c = b - (b - a) / gr
		d = a + (b - a) / gr

	return (b + a) / 2

def makeF(J, u, alph, p):
	return

def Gradient(func, point):
	grad = []
	h = 0.0001
	for pos, num in enumerate(point):
		dim_f = np.array(point)
		dim_b = np.array(point)
		dim_f[pos] += h
		dim_b[pos] -= h
		grad.append((func(dim_f) - func(dim_b)) / (2 * h))
	return np.array(grad)

def conj_vect(J, eps, u_0):
	"""
	Метод сопряженных векторов для минимизации функции многих переменных

	u_k+1 = u_k - alpha_k*p_k
		,где:
	u_0 - начальное приближение
	p_0 = grad(J)(u_0)
	p_k = grad(J)(u_k) - beta_k*u_(k-1)

	alpha_k >= 0 ;
	alpha_k = argmin(f_k(alpha) | alpha > 0),
		where f_k(alpha) = J(u_k - alpha*p_k)

	beta_k = - |grad(J)(u_k)|**2/
				|grad(J)(u_k-1)|**2

	"""

	#grad = nd.Gradient(J)
	u_old = np.array(u_0)
	p = Gradient(J, u_old)
	f = lambda alph: J(u_old - alph*p)
	print(f(1))
	print(f(0.5))
	#alpha = BrentComb(0, 2, f, 0.0001)[0]
	alpha = minimize_scalar(f, bounds=(0, 1), method='bounded').x
	beta = 0.
	if (notations == True):
			print("Шаг 1")
			print("  Alpha | Beta  - ", alpha, " | ", beta)
			print(" Значение аргумента - ", u_old)
			print(" Значение направления - ", p)
	k = 1
	while(np.linalg.norm(Gradient(J, u_old))>= eps):
		if (notations == True):
			print("Шаг ", k)
		u_new = u_old - alpha*p

		if (k % dim == 0):
			beta = 0.
		else:
			beta = - np.linalg.norm(Gradient(J, u_new))**2 / (np.linalg.norm(Gradient(J, u_old))**2)
		p = Gradient(J, u_new) - beta * u_old
		f = lambda alph: J(u_old - alph*p)
		print(f(1))
		#alpha = BrentComb(0, 1, f, 0.0001)[0]
		alpha = minimize_scalar(f, bounds=(0, 1), method='bounded').x

		u_old = u_new
		if (notations == True):
			print("  Alpha | Beta  - ", alpha, " | ", beta)
			print(" Значение аргумента - ", u_new)
			print(" Значение направления - ", p)
			print ("Минимум функции: ", J(u_old))
		k += 1
	print ("Результат получен через ", k, "шагов")
	print ("Значение аргумента:", np.array(u_old))
	print ("Минимум функции: ", J(u_old))
	return(u_new)

J =	lambda u: (u[0] + 3)**2 + 3*(u[1] - 1)**2 + 4*(u[2] - 2)**2  - 3
eps = 0.001
u_0 = [4.0, -2.0, 4.0]

conj_vect(J, eps, u_0)


#!/usr/bin/env python
#coding=utf-8

"""
severities.py: the set of arrays for severity measures.
"""

__author__      = "Francisco Maria Calisto"
__maintainer__  = "Francisco Maria Calisto"
__email__       = "francisco.calisto@tecnico.ulisboa.pt"
__license__     = "MIT"
__version__     = "1.0.0"
__status__      = "Development"
__copyright__   = "Copyright 2019, Instituto Superior Técnico (IST)"
__credits__     = [
  "Bruno Oliveira",
  "Carlos Santiago",
  "Jacinto C. Nascimento",
  "Pedro Miraldo",
  "Nuno Nunes"
]

import os
import sys
import logging

from os import path

# The current folder path.
basePath = os.path.dirname(__file__)

# The path to the repository "src" folder.
joinRepoSrcPath = os.path.join(basePath, '..')
pathRepoSrcAbsPath = os.path.abspath(joinRepoSrcPath)
# Add the directory containing the module to
# the Python path (wants absolute paths).
sys.path.append(pathRepoSrcAbsPath)

# Appending constants path
consPath = os.path.join(joinRepoSrcPath, 'constants')
consAbsPath = os.path.abspath(consPath)
sys.path.append(consAbsPath)
sys.path.insert(0, consAbsPath)

# Importing available variables
from sheets import *

import numpy as np

arr_birads_assis_l = df_birads_assis_l.values
arr_birads_assis_m = df_birads_assis_m.values
arr_birads_assis_h = df_birads_assis_h.values

arr_birads_crrnt_l = df_birads_crrnt_l.values
arr_birads_crrnt_m = df_birads_crrnt_m.values
arr_birads_crrnt_h = df_birads_crrnt_h.values

arr_birads_phys_l = df_birads_phys_l.values
arr_birads_phys_m = df_birads_phys_m.values
arr_birads_phys_h = df_birads_phys_h.values

arr_birads_real_l = df_birads_real_l.values
arr_birads_real_m = df_birads_real_m.values
arr_birads_real_h = df_birads_real_h.values


import sys
sys.path.append('..')

from discord import Embed

from bot.filterer import expand, get_invalid_gates, get_invalid_keys, get_invalid_filters, get_invalid_words, get_missing_gate
from bot.filterer import FilterContext
from bot.formatter import format_dotted_list

async def validate_filter(ctx, _filter: str, filter_context: FilterContext) -> bool:
    """Returns whether the filter was considered valid. If invalid, an appropriate response is sent
    where the command was called. Requires a filter context to determine filter validity."""
    try:
        expansion = expand(_filter)
    except ValueError as err:
        # E.g. parenthesis inequality.
        await ctx.respond(f"{str(err)}", embed=filters_embed(filter_context), ephemeral=True)
        return False

    invalid_gates = set(get_invalid_gates(_filter))
    if invalid_gates:
        invalids_formatted = "`" + "`, `".join(invalid_gates) + "`"
        await ctx.respond(f"✗ Invalid positioning of gate(s) {invalids_formatted} in expansion `{expansion}`.", embed=filters_embed(filter_context), ephemeral=True)
        return False

    invalid_keys = set(get_invalid_keys(_filter, filter_context))
    if invalid_keys:
        invalids_formatted = "`" + "`, `".join(invalid_keys) + "`"
        await ctx.respond(f"✗ Invalid key(s) {invalids_formatted} in expansion `{expansion}`.", embed=filters_embed(filter_context), ephemeral=True)
        return False

    invalid_filters = set(get_invalid_filters(_filter, filter_context))
    if invalid_filters:
        keys = []
        invalids_strs = []
        for key, value in invalid_filters:
            invalids_strs.append(f"{key}:{value}")
            keys.append(key)
        invalids_formatted = "`" + "`, `".join(invalids_strs) + "`"
        # `keys` will have at least one element, else `invalid_filters` would be falsy.
        await ctx.respond(f"✗ Invalid value(s) for key(s) {invalids_formatted} in expansion `{expansion}`.", embed=filter_embed(keys[0], filter_context), ephemeral=True)
        return False

    invalid_words = set(get_invalid_words(_filter))
    if invalid_words:
        invalids_formatted = "`" + "`, `".join(invalid_words) + "`"
        await ctx.respond(f"✗ Invalid word(s) {invalids_formatted} in expansion `{expansion}`.", embed=filters_embed(filter_context), ephemeral=True)
        return False

    parts = get_missing_gate(_filter)
    if parts:
        left_part, right_part = parts
        await ctx.respond(f"✗ Missing gate between `{left_part}` and `{right_part}` in expansion `{expansion}`.", embed=filters_embed(filter_context), ephemeral=True)
        return False

    if not hasattr(ctx.channel, "guild"):
        # Prevents excessive discord rate limiting (5 DMs per second globally).
        await ctx.respond("✗ Cannot subscribe in DM channels.", ephemeral=True)
        return False

    return True

def filters_embed(filter_context: FilterContext) -> Embed:
    """Returns an embed representing the given filter context; showing all tag names and examples."""
    embed = Embed()
    embed.title = f"Filter ({filter_context.name})"
    embed.description = """
            A string of key:value pairs (e.g. `key1:(value1 or value2) and key2:value3`).
            Keys and values are always case insensitive. Gates are `and`, `or`, and `not`.
            """

    keys = "\u2000".join("**`" + ("/".join(f"{name}" for name in tag.names) + "`**") for tag in filter_context.tags)
    embed.add_field(
        name   = "Keys" + (" (`/` denotes aliases)" if "/" in keys else ""),
        value  = keys,
        inline = True
    )
    embed.add_field(
        name   = "Example(s)",
        value  = format_dotted_list(filter_context.examples),
        inline = True
    )
    return embed

def filter_embed(key: str, filter_context: FilterContext) -> Embed:
    """Returns an embed representing the tag with the given name `key`, for this context.
    Goes more in detail regarding how to use this specific tag."""
    key = key.lower().strip()
    tag = filter_context.get_tag(key)
    keys = filter_context.get_tag(key).names

    embed = Embed()
    embed.add_field(
        name   = "/".join(f"**`{key}`**" for key in keys),
        value  = tag.description,
        inline = True
    )
    embed.add_field(
        name   = "Value(s)",
        value  = tag.value_hint,
        inline = True
    )
    embed.add_field(
        name   = "Example(s)",
        value  = format_dotted_list(f"`{key}:{value}`" for value in tag.example_values),
        inline = True
    )
    return embed


"""
This is an example of how to use the subprocess module for streaming
"""

import subprocess

with subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE) as p:
    assert p.stdout is not None
    for line in p.stdout:
        print(line.decode().rstrip())


from objs import PathFinder, BinarySearch
from matplotlib import pyplot as plt

if __name__ == "__main__":
    path_finder = PathFinder()

    while True:
        try:
            correct_time = int(input())

            path_finder.turn_dial(1000)
            path, time = path_finder.get_critical_path()

            if correct_time > time:
                print(1000)
                continue

            path_finder.turn_dial(0)
            path, time = path_finder.get_critical_path()

            if correct_time < time:
                print("Impossible")
                continue

            bs = BinarySearch(1000)

            path, time = path_finder.get_critical_path()
            prev_path = None

            while abs(correct_time - time) > 0.01:
                dial = bs.step(correct_time - time > 0)
                path_finder.turn_dial(dial)
                path, time = path_finder.get_critical_path()
                if path == prev_path and (time > correct_time > prev_time or time < correct_time < prev_time):
                    a = (prev_time - time) / (prev_dial - dial)
                    dial = ((correct_time - time) / a) + dial
                    break
                prev_dial = dial
                prev_path, prev_time = path, time

            #path_finder.turn_dial(dial)
            #print(path_finder.get_critical_path()[1])
            print(dial)
        except EOFError:
            break


import requests
from rest_framework.response import Response
from rest_framework.decorators import api_view

@api_view(('GET', ))
def get_languages(request, date):
    url = "https://api.github.com/search/repositories?q=created: >{0}&sort=stars&order=desc".format(
        date)
    repositories = requests.get(url).json()
    languages = {}
    for repo in repositories['items']:
        language = repo['language']
        if language not in languages:  # initaite language dictionary
            languages[language] = {}
            languages[language].setdefault('repos', [])
            languages[language].setdefault('num_repos', 0)
        languages[language]['repos'].append(repo)
        languages[language]['num_repos'] += 1
    return Response(languages)


# Authors: Federico Raimondo <f.raimondo@fz-juelich.de>
#          Sami Hamdan <s.hamdan@fz-juelich.de>
#          Kaustubh, Patil <k.patil@fz-juelich.de>
# License: AGPL

from typing import Callable, Optional

import numpy as np
from joblib import Parallel, delayed
from scipy.stats import pearsonr
from sklearn.base import BaseEstimator, TransformerMixin

from sklearn.utils.validation import check_is_fitted
from ..utils.versions import _joblib_parallel_args
from ..utils import warn_with_log

class CBPM(BaseEstimator, TransformerMixin):
    '''Transformer that aggregates together all features significantly
    correlated to the target.

    Significant negative and positive correlations are aggregateed separately.
    Non-significant ones are dropped.

    User can choose to use negative, positive or both correlations.

    In case that there are no significant correlations the mean of the
    target will be returned as the only feature.

    This transformer implements the procedure described in :
    Shen, X., Finn, E., Scheinost, D. et al. 2016
    https://doi.org/10.1038/nprot.2016.178

    Parameters
    ----------
    significance_threshold : float, default=0.05
        Threshold of p value.
    corr_method : callable, default=scipy.stats.pearsonr
        Callable which can be used to create tuple of arrays: (correlations,
        p values). Input has to be X, y.
    agg_method : callable, default=np.sum
        Callable to aggregate the features.
        Has to follow np convention using axis.
    corr_sign : str , default='posneg'
        Which correlations should be used:
        Options are:

         * `pos`: use positive correlations only
         * `neg`: use negative correlations only
         * `posneg`: use all correlations

        In case you use `posneg` and there are only `pos` or `neg` this
        will be used instead. The actually used correlation_values can be
        found in the attribute: `used_corr_sign_`
    n_jobs : int, default=None
        How many jobs should run in parallel to compute the correlations of
        each feature to the target.
        This parameter follows joblib and scikit-learn standards.
    verbose : int, default=0
        How verbose should the log of the parallel computing be.
        This parameter
        follows joblib and scikit-learn standards.

    Attributes
    ----------
    y_mean_ : np.array
        Contains the mean of the target, to be used for the transformation in
        case that there are no significant correlations.
    used_corr_sign_ : str
        This will show you whether pos, neg or posneg was applied.
    X_y_correlations_ : tuple(np.array, np.array)
        Output of the `corr_method` applied to the target and the features.
    significant_mask_ : np.array of bools
        Array of bools showing which of the original features had a
        significant correlation.
    pos_mask_ : np.array of bools
        Array of bools showing which of the original features had a
        positive correlation.
    pos_significant_mask_ : np.array of bools
        Array of bools showing which of the original features had a
        significant positive correlation.
    neg_significant_mask_ : np.array of bools
        Array of bools showing which of the original features had a
        significant negative correlation.
    used_significant_mask_ : np.array of bools
        Array of bools showing which of the original features will be used
        by this transformer.
   '''

    def __init__(
        self,
        significance_threshold: float = 0.05,
        corr_method: Callable = pearsonr,
        agg_method: Callable = np.sum,
        corr_sign: str = "posneg",
        n_jobs: Optional[int] = None,
        verbose: int = 0,
    ):
        self.significance_threshold = significance_threshold
        self.corr_method = corr_method
        self.agg_method = agg_method
        if corr_sign not in ["pos", "neg", "posneg"]:
            raise ValueError(
                "corr_sign must be one of pos, neg or posneg, "
                f"but is {corr_sign}"
            )
        self.corr_sign = corr_sign
        self.n_jobs = n_jobs
        self.verbose = verbose

    def fit(self, X: np.ndarray, y: np.ndarray) -> "CBPM":
        """Fit the transformer.

        Compute the correlations of each feature to the target, threhsold and
        create the respective masks.

        Parameters
        ----------
        X : np.array
            Input features.
        y : np.array
            Target.

        Returns
        -------
        self : CBPM
            The fitted transformer.
        """
        X, y = self._validate_data(X, y)  # type: ignore

        # compute correlations using joblib
        self.X_y_correlations_ = np.array(
            Parallel(
                n_jobs=self.n_jobs,
                verbose=self.verbose,
                **_joblib_parallel_args(),
            )(
                delayed(self.corr_method)(X[:, X_idx], y)
                for X_idx in range(X.shape[1])
            )
        )

        # Save the y mean to be used in transform in case no significant
        # correlation is present
        self.y_mean_ = y.mean()

        self._create_masks()

        return self

    def transform(self, X: np.ndarray) -> np.ndarray:
        """Transform the data.

        Replace each of the features that had a significant correlation on
        the training data with the mean of the features (mean is computed
        per sample).

        Parameters
        ----------
        X : np.array
            Input features.

        Returns
        -------
        np.array
            The transformed features.
        """

        X = self._validate_data(X)  # type: ignore

        if not any(self.used_significant_mask_):
            out = np.ones(X.shape[0]) * self.y_mean_
            return out

        elif self.used_corr_sign_ == 'posneg':
            X_meaned_pos = self.aggregate(
                X, mask=self.pos_significant_mask_
            )

            X_meaned_neg = self.aggregate(
                X, mask=self.neg_significant_mask_
            )

            X_meaned = np.concatenate(
                [
                    X_meaned_pos.reshape(-1, 1),
                    X_meaned_neg.reshape(-1, 1)],
                axis=1)

        elif self.used_corr_sign_ == 'pos':
            X_meaned = self.aggregate(X, self.pos_significant_mask_)

        elif self.used_corr_sign_ == 'neg':
            X_meaned = self.aggregate(X, self.neg_significant_mask_)

        return X_meaned

    def _create_masks(self) -> None:
        """Create the masks for the significant correlations."""

        # Find the correlations whose p-value is below the threshold
        self.significant_mask_ = (
            self.X_y_correlations_[:, 1] < self.significance_threshold
        )

        # Mask separately the positive and negative correlations
        self.pos_mask_ = self.X_y_correlations_[:, 0] > 0
        self.neg_mask_ = self.X_y_correlations_[:, 0] < 0

        # Mask the significant correlations separately by sign
        self.pos_significant_mask_ = np.logical_and(
            self.significant_mask_, self.pos_mask_
        )
        self.neg_significant_mask_ = np.logical_and(
            self.significant_mask_, self.neg_mask_
        )

        have_pos_feat = any(self.pos_significant_mask_)
        have_neg_feat = any(self.neg_significant_mask_)

        if self.corr_sign == "pos":
            self.used_corr_sign_ = "pos"
            self.used_significant_mask_ = self.pos_significant_mask_
            if not have_pos_feat:
                warn_with_log(
                    "No feature with significant positive correlations was "
                    "present. Therefore, the mean of the target will be used "
                    "for prediction instead."
                )
        elif self.corr_sign == "neg":
            self.used_corr_sign_ = "neg"
            self.used_significant_mask_ = self.neg_significant_mask_
            if not have_neg_feat:
                warn_with_log(
                    "No feature with significant negative correlations was "
                    "present. Therefore, the mean of the target will be used "
                    "for prediction instead."
                )
        else:  # "posneg"
            if not have_pos_feat and not have_neg_feat:
                self.used_corr_sign_ = "posneg"
                self.used_significant_mask_ = np.zeros(
                    self.significant_mask_.shape, dtype=bool
                )
                warn_with_log(
                    "No feature with significant negative or positive "
                    "correlations was present. Therefore, the mean of the "
                    "target will be used for prediction instead."
                )
            elif not have_pos_feat:
                self.used_corr_sign_ = "neg"
                self.used_significant_mask_ = self.neg_significant_mask_
                warn_with_log(
                    "No feature with significant positive correlations was "
                    "present. Only features with negative correlations will "
                    "be used. To get rid of this message, set "
                    "`corr_sign = 'neg'`."
                )
            elif not have_neg_feat:
                self.used_corr_sign_ = "pos"
                self.used_significant_mask_ = self.pos_significant_mask_
                warn_with_log(
                    "No feature with significant negative correlations was "
                    "present. Only features with positive correlations will "
                    "be used. To get rid of this message, set "
                    "`corr_sign = 'pos'`."
                )
            else:
                self.used_corr_sign_ = "posneg"
                self.used_significant_mask_ = self.significant_mask_

    def aggregate(self, X, mask):
        return self.agg_method(X[:, mask], axis=1)

    def get_feature_names_out(self, input_features=None):
        check_is_fitted(self)
        cols = (["positive"] if self.used_corr_sign_ == "pos" else
                ["negative"] if self.used_corr_sign_ == "neg" else
                ["positive", "negative"]
                )
        return np.array(cols, dtype=object)


import pandas as pd
# 创建空DataFrame实例，DataFrame就是数据帧
df = pd.DataFrame({'id':[1,2,3],'name':['张三','李四','王五']})
# pandas默认第一个列会自动创建索引，使用set_index()方法指定索引
df = df.set_index('id')
df.to_excel("/Users/sss/PycharmProjects/StandardResearch/ahp/output.xlsx") #导出xlsx表
print('Done!')

# index_col参数表示读取时就指定索引列
people = pd.read_excel("/Users/sss/PycharmProjects/StandardResearch/ahp/output.xlsx", index_col='id')

# header=1表示跳过第一行，从第二行开始；当第一行位脏数据时可以这么设置，如果第一行每一列全部为空，那么pandas自动从第二行开始
# people = pd.read_excel("D:\py_workspace\pandas_excel\People.xlsx",header=1)
# shape表示属性，返回元组,元组第一个表示行，第二表示列

# 如果我们excel表格没有列名，需要把header设置为None,那么默认给列名是0，1，2...形式
# people = pd.read_excel("D:\py_workspace\pandas_excel\People.xlsx",header=None)
# 设置列名
# people.columns= ['ID','Type','Title','FirstName','MiddleName','LastName']
# 设置ID列为索引，inplace=True表示直接修改这个DataFrame
# people.set_index('ID',inplace=True) # 注意区别people = people.set_index('ID') 代码
print(people.shape)  # 输出结果:(19972, 6)
# columns属性是列,类型<class 'pandas.core.indexes.base.Index'>
print(people.columns)  # 输出结果:Index(['ID', 'Type', 'Title', 'FirstName', 'MiddleName', 'LastName'], dtype='object')
# print("前五行\n", people.head(5))
# print("后三行\n", people.tail(3))
# 由于读取pepole文件时index_col指定了索引列，所以新产生的索引列就是ID，不会自动生成一个索引列


#!/usr/bin/python
import numpy as np
import sys,os
import pandas as pd
import scipy
import getopt
from sklearn import linear_model
from sklearn.metrics import mean_squared_error
from decimal import *

if len(sys.argv) < 2:
	print ("Please input parameter files or use -h for help")
	sys.exit()

def usage():
	print("-c or --coreset: specify the location of 'CoreSet.dat' (or a subset data file) in the CASF-2016 package")
	print("-s or --score: specify the directory containing your scoring files(e.g. 'XXXX_score.dat'). Remember the 1st column name is #code_ligand_num and the 2nd column name is score. Supported file separators are comma(,), tabs(\\t) and space character( )")
	print("-p or --prefer: input 'negative' or 'positive' string, depend on your scoring funtion preference")
	print("-o or --output: input the prefix of output result files. Default is My_Forward_Screening_Power")
	print("-t or --target: specify the location of 'TargetInfo.dat' in the CASF-2016 package")
	print("-h or --help: print help message")
	print("\nExample: python forward_screening_power.py -c CoreSet.dat -s ./examples/X-Score -t ./TargetInfo.dat -p 'positive' -o 'X-Score' > MyForwardScreeningPower.out")

try:
	options,args = getopt.getopt(sys.argv[1:],"hc:s:t:p:o:",["help","coreset=","score=","target=","prefer=","output="])
except getopt.GetoptError:
	sys.exit()

out='My_Forward_Screening_Power'
#Read the CoreSet.dat, TargetInfo.dat and scoring result files
for name,value in options:
	if name in ("-h","--help"):
		usage()
		sys.exit()
	if name in ("-c","--coreset"):
		f=open(value,'r')
		f1=open('cstemp','w+')
		for i in f.readlines():
			if i.startswith('#'):
				if i.startswith('#code'):
					f1.writelines(i)
				else:
					continue
			else:
				f1.writelines(i)
		f.close()
		f1.close()
		aa=pd.read_csv('cstemp',sep='[,,_,\t, ]+',engine='python')
		aa=aa.drop_duplicates(subset=['#code'],keep='first')
	if name in ("-s","--score"):
		scorefile=value
	if name in ("-t","--target"):
		f=open(value,'r')
		f1=open('tstemp','w+')
		for i in f.readlines():
			if i.startswith('#'):
				if i.startswith('#T'):
					f1.writelines(i)
				else:
					continue
			else:
				f1.writelines(i)
		f.close()
		f1.close()
		bb=pd.read_csv('tstemp',sep='[,,\t, ]+',engine='python')
		bb=bb.drop_duplicates(subset=['#T'],keep='first')
		cc=bb.set_index('#T')
	if name in ("-p","--prefer"):
		fav=value
	if name in ("-o","--output"):
		out=value

#Get the representative complex in each cluster
def top(df,n=1,column='logKa'):
        return df.sort_values(by=column)[-n:]
def dec(x,y):
        if y == 2:
                return Decimal(x).quantize(Decimal('0.01'),rounding=ROUND_HALF_UP)
        if y == 3:
                return Decimal(x).quantize(Decimal('0.001'),rounding=ROUND_HALF_UP)
        if y == 0:
                return Decimal(x).quantize(Decimal('0'),rounding=ROUND_HALF_UP)

#process data
pdb=aa['#code']
toptardf=aa.groupby('target').apply(top)
targetlst2=toptardf['#code'].tolist()
targetlst=[]
for i in targetlst2:
	if i in list(set(bb['#T'])):
		targetlst.append(i)
#define decoy list and cutoff
decoylst2=[]
for i in np.arange(1,11):
	decoylst2.extend((cc['L'+str(i)].tolist()))
decoylst=[_f for _f in list(set(decoylst2)) if _f]
t1=int(dec(len(decoylst)*0.01,0))
t5=int(dec(len(decoylst)*0.05,0))
t10=int(dec(len(decoylst)*0.10,0))
if t1 < 1:
	print(("The number of top 1%% ligands is %0.1f,less than 1"%(round(len(decoylst)*0.01))))
	print("In this case, we set the cutoff of top 1%% = 1")
	t1=1
if t5 < 1:
	print(("The number of top 5%% ligands is %0.1f,less than 1"%(round(len(decoylst)*0.01))))
	print("In this case, we set the cutoff of top 5%% = 1")
	t5=1
if t10 < 1:
	print(("The number of top 10%% ligands is %0.1f,less than 1"%(round(len(decoylst)*0.01))))
	print("In this case, we set the cutoff of top 10%% = 1")
	t10=1
#Build DataFrame to store results
Top1=pd.DataFrame(index=targetlst,columns=['success'])
Top5=pd.DataFrame(index=targetlst,columns=['success'])
Top10=pd.DataFrame(index=targetlst,columns=['success'])
EF1=pd.DataFrame(index=targetlst,columns=['enrichment'])
EF5=pd.DataFrame(index=targetlst,columns=['enrichment'])
EF10=pd.DataFrame(index=targetlst,columns=['enrichment'])
forwardf=pd.DataFrame(index=list(range(1,len(targetlst)+1)),columns=list(range(0,t10+1)))
forwardf.rename(columns={0:'Target'},inplace=True)
tmp=1
if fav == 'positive':

	for i in targetlst:
		scoredf=pd.read_csv(scorefile+'/'+str(i)+'_score.dat',sep='[ ,_,\t]+',engine='python')
		scoredf=scoredf[(False^scoredf['#code'].isin(decoylst))]
		group=scoredf.groupby('#code')
		testdf=pd.DataFrame(group['score'].max())
		dfsorted=testdf.sort_values('score',ascending=False)
		for m in range(1,t10+1):
			forwardf.loc[tmp][m]=''.join(dfsorted[m-1:m].index.tolist())
		forwardf.loc[tmp]['Target']=i
		tmp+=1
		Topligand=cc.loc[i]['L1']
		tartemp=cc.loc[i]
		Allactivelig=list(tartemp.dropna())
		NTBtotal=len(Allactivelig)
		dic={'1':t1,'5':t5,'10':t10}
		for name,j in list(dic.items()):
			lst=list(dfsorted[0:j].index)
			varname='Top'+str(name)
			top=locals()[varname]
			if Topligand in lst:
				top.loc[i]['success']=1
			else:
				top.loc[i]['success']=0
			varname2='EF'+str(name)
			ef=locals()[varname2]
			ntb=0
			for lig in Allactivelig:
				if lig in lst:
					ntb=ntb+1
				else:
					continue
			efvalue=float(ntb)/(float(NTBtotal)*int(name)*0.01)
			ef.loc[i]['enrichment']=efvalue
elif str(fav) == 'negative':
	for i in targetlst:
		scoredf=pd.read_csv(scorefile+'/'+str(i)+'_score.dat',sep='[ ,_,\t]+',engine='python')
		scoredf=scoredf[(False^scoredf['#code'].isin(decoylst))]
		group=scoredf.groupby('#code')
		testdf=pd.DataFrame(group['score'].min())
		dfsorted=testdf.sort_values('score',ascending=True)
		for m in range(1,t10+1):
			forwardf.loc[tmp][m]=''.join(dfsorted[m-1:m].index.tolist())
		forwardf.loc[tmp]['Target']=i
		tmp+=1
		Topligand=cc.loc[i]['L1']
		tartemp=cc.loc[i]
		Allactivelig=list(tartemp.dropna())
		NTBtotal=len(Allactivelig)
		dic={'1':t1,'5':t5,'10':t10}
		for name,j in list(dic.items()):
			lst=list(dfsorted[0:j].index)
			varname='Top'+str(name)
			top=locals()[varname]
			if Topligand in lst:
				top.loc[i]['success']=1
			else:
				top.loc[i]['success']=0
			varname2='EF'+str(name)
			ef=locals()[varname2]
			ntb=0
			for lig in Allactivelig:
				if lig in lst:
					ntb=ntb+1
				else:
					continue
			efvalue=float(ntb)/(float(NTBtotal)*int(name)*0.01)
			ef.loc[i]['enrichment']=efvalue

#                sp.drop(sp.index[[i]],inplace=True)
else:
	print('please input negative or positive')
	sys.exit()

#Calculate success rates  and enrichment factors
top1success=dec(float(Top1['success'].sum())/float(Top1.shape[0]),3)*100
top5success=dec(float(Top5['success'].sum())/float(Top5.shape[0]),3)*100
top10success=dec(float(Top10['success'].sum())/float(Top10.shape[0]),3)*100
ef1factor=dec(float(EF1['enrichment'].sum())/float(EF1.shape[0]),2)
ef5factor=dec(float(EF5['enrichment'].sum())/float(EF5.shape[0]),2)
ef10factor=dec(float(EF10['enrichment'].sum())/float(EF10.shape[0]),2)

if os.path.exists('cstemp'):
	os.remove('cstemp')
if os.path.exists('tstemp'):
	os.remove('tstemp')
#Print the output of forward screening power evluation
tmplen=len((Top1))
forwardf.style.set_properties(align="right")
pd.set_option('display.max_columns',None)
pd.set_option('display.max_rows',None)
print(forwardf)
print ("\nSummary of the forward screening power: =========================================================")
print(("Average enrichment factor among top 1%% = %0.2f"%(ef1factor)))
print(("Average enrichment factor among top 5%% = %0.2f"%(ef5factor)))
print(("Average enrichment factor among top 10%% = %0.2f"%(ef10factor)))
print(("The best ligand is found among top 1%% candidates for %2d cluster(s); success rate = %0.1f%%"%(Top1['success'].sum(),top1success)))
print(("The best ligand is found among top 5%% candidates for %2d cluster(s); success rate = %0.1f%%"%(Top5['success'].sum(),top5success)))
print(("The best ligand is found among top 10%% candidates for %2d cluster(s); success rate = %0.1f%%"%(Top10['success'].sum(),top10success)))
print ("================================================================================================")
print ("\nTemplate command for running the bootstrap in R program=========================================\n")
print(("rm(list=ls());\nrequire(boot);\ndata_all<-read.table(\"%s_Top1.results\",header=TRUE);\ndata<-as.matrix(data_all[,2]);"%(out)))
print(("mymean<-function(x,indices) sum(x[indices])/%d;"%(tmplen)))
print(("data.boot<-boot(aa,mymean,R=10000,stype=\"i\",sim=\"ordinary\");\nsink(\"%s_Top1-ci.results\");\na<-boot.ci(data.boot,conf=0.9,type=c(\"bca\"));\nprint(a);\nsink();\n"%(out)))
print ("===============================================================================================\n")

Top1.to_csv(out+'_Top1.dat',sep='\t',index_label='#Target')
Top5.to_csv(out+'_Top5.dat',sep='\t',index_label='#Target')
Top10.to_csv(out+'_Top10.dat',sep='\t',index_label='#Target')
EF1.to_csv(out+'_EF1.dat',sep='\t',index_label='#Target')
EF5.to_csv(out+'_EF5.dat',sep='\t',index_label='#Target')
EF10.to_csv(out+'_EF10.dat',sep='\t',index_label='#Target')


import tkinter as tk
from PIL import Image, ImageTk, ImageGrab
from tkinter import messagebox
import time
from tkinter.font import BOLD, ITALIC

ana_pencere = tk.Tk()
ana_pencere.title("Kredi Hesaplama")
çerçeve1 = tk.Frame(ana_pencere)
çerçeve2 = tk.Frame(ana_pencere)
çerçeve3 = tk.Frame(ana_pencere)
çerçeve4 = tk.Frame(ana_pencere)

def hesaplama():
    try:
        kredi_tutari = float(kredi_tutari_girisi.get())
        kkdf_bsmv = 1.20
        faiz_orani = float(faiz_orani_girisi.get()) / 100.0 * kkdf_bsmv
        taksit_sayisi = int(taksit_sayisi_girisi.get())
        taksit_miktari = float((kredi_tutari * faiz_orani * (1 + faiz_orani)**taksit_sayisi) / ((1 + faiz_orani)**taksit_sayisi - 1))

        toplam_tutar = taksit_miktari*taksit_sayisi

        # Taksit tablosu oluşturma

        taksit_tablosu = tk.Label(çerçeve1, text="Anapara\t    Faiz\t  Taksit\tKalan Borç", font=("Arial", 14),bg="#98F5FF")
        taksit_tablosu.grid(row=5, column=0, columnspan=2, padx=5, pady=5)

        kalan_borc = kredi_tutari
        for i in range(taksit_sayisi):
            faiz_tutari = kalan_borc * faiz_orani
            anapara_tutari = taksit_miktari - faiz_tutari
            kalan_borc -= anapara_tutari

            taksit_satiri = "{:.2f}\t{:.2f}\t{:.2f}\t{:.2f}".format(anapara_tutari, faiz_tutari, taksit_miktari, kalan_borc)
            taksit_satiri_widget = tk.Label(çerçeve1, text=taksit_satiri, font=("Arial", 14),background="#CAFF70")
            taksit_satiri_widget.grid(row=i+6, column=0, columnspan=2, padx=5, pady=5)
        sonuc.config(text="")
        sonuc.config(text="Taksit Tutarı: {:.2f} TL\nToplam Ödenecek Tutar: {:.2f} TL".format(taksit_miktari, toplam_tutar), bg="aquamarine", fg="black")
    except ValueError:
        sonuc.config(text="Lütfen geçerli bir değer girin.", fg="red")

# Girdi alanları için etiketler ve girdi kutuları oluşturma
kredi_tutari_etiket = tk.Label(çerçeve1, text="Kredi Tutarı:", font=("Arial", 14),width="16",background="#E3CF57",height="1")
kredi_tutari_etiket.grid(row=0, column=0)

kredi_tutari_girisi = tk.Entry(çerçeve1, font=("Arial", 14),width="10")
kredi_tutari_girisi.insert(0,10000)
kredi_tutari_girisi.grid(row=0, column=1)

faiz_orani_etiket = tk.Label(çerçeve1, text="Aylık Faiz Oranı (%):", font=("Arial", 14),width="16",background="#E3CF57")
faiz_orani_etiket.grid(row=1, column=0)

faiz_orani_girisi = tk.Entry(çerçeve1, font=("Arial", 14),width="10")
faiz_orani_girisi.insert(0,1.79)
faiz_orani_girisi.grid(row=1, column=1)

taksit_sayisi_etiket = tk.Label(çerçeve1, text="Taksit Sayısı:", font=("Arial", 14),width="16",background="#E3CF57")
taksit_sayisi_etiket.grid(row=2, column=0)

taksit_sayisi_girisi = tk.Entry(çerçeve1, font=("Arial", 14),width="10")
taksit_sayisi_girisi.insert(0,6)
taksit_sayisi_girisi.grid(row=2, column=1)

# Hesapla butonunu oluşturma
hesapla_butonu = tk.Button(çerçeve1, text="Hesapla", font=("Arial", 14), command=hesaplama)
hesapla_butonu.grid(row=3, column=0, padx=5, pady=5)

# Sonuç alanını oluşturma
sonuc = tk.Label(çerçeve1, text="", font=("Arial", 14))
sonuc.grid(row=4, column=0, columnspan=2, padx=5, pady=5)

def ekrankayit():
    x = çerçeve1.winfo_rootx()
    y = çerçeve1.winfo_rooty()
    width = çerçeve1.winfo_width()
    height = çerçeve1.winfo_height()
    screenshot = ImageGrab.grab(bbox=(x, y, x+width, y+height))
    screenshot.save("kredi_sonucu.png")
def take_screenshot():
    #'Emin misiniz?' mesaj kutusu göster
    if messagebox.askokcancel("Onay", "Ekran Görüntüsü \n Alınacaktır \n Emin misiniz?"):
        #Ekran görüntüsü almak için capture_screen() fonksiyonunu çağır
        çerçeve1.after(100, ekrankayit)
ekran_goruntusu = tk.Button(çerçeve1, text="Ekran Görüntüsü Al",command=take_screenshot)
ekran_goruntusu.grid(row=3,column=1)

imzaimage = tk.PhotoImage(file="ercancagimza.png")
image2=Image.open('caglayan.png')
boyutluimage2 = image2.resize((270,65))
my_img=ImageTk.PhotoImage(boyutluimage2)
kanvas = tk.Canvas(çerçeve2, height=70, width=250)
kanvas.create_image(135, 40, image=imzaimage)
kanvas.pack(padx=1, pady=1,side='bottom')
kanvas2 = tk.Canvas(çerçeve3, height=80, width=300)
kanvas2.create_image(155, 40, image=my_img)
kanvas2.pack(padx=1, pady=1, side="bottom")
########################### SAAT ###################################
saat_label = tk.Label(çerçeve4, font='Verdena', fg='black')
saat_label.pack(side="right", fill=tk.X)
günler = time.strftime("%A")
if günler == "Saturday":
    gün = "Cumartesi"
elif günler == "Sunday":
    gün = "Pazar"
elif günler == "Monday":
    gün = "Pazartesi"
elif günler == "Tuesday":
    gün = "Salı"
elif günler == "Wednesday":
    gün = "Çarşamba"
elif günler == "Thursday":
    gün = "Perşembe"
elif günler == "Friday":
    gün = "Cuma"
def digital_clock():
    time_live = time.strftime("%H:%M:%S" + "   " + gün + "   " "%d/%m/%Y")
    saat_label.config(text=time_live, font=('Clarendon Blk BT',20,BOLD), background="#DC143C",fg="white")
    saat_label.after(100, digital_clock)
digital_clock()

çerçeve1.pack()
çerçeve2.pack()
çerçeve3.pack()
çerçeve4.pack()
# Ana döngüyü başlatma
ana_pencere.mainloop()


#https://leetcode.com/problems/merge-intervals/
#Sol1: T: O(nlongn) S: O(n)/O(logn)

class Solution(object):
    def merge(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: List[List[int]]
        """
        intervals.sort()
        merged = []
        for interval in intervals:
            if not merged or merged [-1][1] < interval[0]:
                merged.append(interval)
            else:
                merged[-1][1] = max(merged[-1][1], interval[1])
        return merged